@balena/compose-parser

中文文档
1.1.0发布于 2026/11/11Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 1.1万

Wraps compose-go to parse balena-compatible docker-compose.yml files

原始使用文档
文档目录16 节
  1. Overview
  2. Requirements
  3. Installation
  4. Architecture
  5. Building
  6. Testing
  7. Unit Tests
  8. Integration Tests
  9. Development Scripts
  10. API
  11. Basic Usage
  12. Parse options
  13. Serializing
  14. Image Descriptors
  15. Balena-Specific Features
  16. Contract Labels

balena-compose-parser

A TypeScript wrapper around compose-go that parses and validates Docker Compose files for balena applications.

Overview

This project provides a Node.js library that uses a Go binary (built from compose-go) to parse Docker Compose files and normalize them for use with balena. It handles:

  • Parsing standard Docker Compose files
  • Validating balena-specific constraints
  • Converting compose configurations to balena-compatible formats
  • Transforming compositions into image descriptors for building/pulling

Requirements

  • Node.js 20+
  • Go 1.24+ (for local builds)

Installation

npm i @balena/compose-parser

Architecture

The project consists of two main components:

  1. Go Binary (lib/main.go): A wrapper around compose-go that outputs structured JSON
  2. TypeScript Library (lib/): Node.js library that calls the Go binary and processes results

Building

# Build everything
npm run build

# Build Go binary only
npm run build:go

Testing

Unit Tests

npm run test

Runs TypeScript unit tests that test the library functions directly. This does not cover the entirety of the functionality, as due to CI constraints, tests using the Go binary are performed in Docker.

Integration Tests

npm run test:compose

# Or the following if running build:go first:
npm run test:integration

The integration tests validate the functionality of the compose-go wrapper for parsing input compose files. npm run test:compose is for running the tests in Docker, but the tests can be run directly if a built Go binary is available as generated from npm run build:go.

Development Scripts

# Check for linting issues
npm run lint

# Fix auto-fixable linting issues
npm run lint-fix

API

Basic Usage

import { parse, defaultComposition, toImageDescriptors } from '@balena/compose-parser';

// Parse a compose file
const composition = await parse('docker-compose.yml');

// Parse multiple compose files (later files override earlier ones)
const composition = await parse([
  'docker-compose.yml', 
  'docker-compose.override.yml'
]);

// Convert composition to image descriptors for building/pulling
const imageDescriptors = toImageDescriptors(composition);

Parse options

By default parse() reads nothing outside the compose files you name. Not this process's environment, and not env_file, label_file, include or extends.file.

// Written by whoever is running this process
const trusted = await parse('docker-compose.yml', { hostEnvironment: true });

// Someone else's file. Keep the defaults, and skip interpolation if it has been
// interpolated already
const untrusted = await parse('docker-compose.yml', { skipInterpolation: true });
OptionDefaultEffect when on
hostEnvironmentfalse${VAR} and valueless environment entries resolve from this process's environment, and env_file, label_file, include and extends.file are followed and folded in
skipInterpolationfalse${VAR} references are left verbatim

The two options cover different things, which is why they are separate.

environment:
  - SOME_VARIABLE              # valueless, resolved by name, hostEnvironment
  - OTHER=${SOME_VARIABLE}     # substitution, skipInterpolation

Substitution still happens on the defaults, against an empty environment, so ${VAR} becomes an empty string while ${VAR:-fallback} gives its fallback. That keeps a compose file written around defaults working.

hostEnvironment also allows secondary files, and puts no limit on where they point. An untrusted file can name any path this process can read, including /proc/<pid>/environ, so leaving it off is what keeps a parse hermetic.

Both defaults were effectively on before v1.0.0, so a caller relying on either has to say so now.

Serializing

serialize() writes a parsed composition back out as a single, self-contained docker-compose.yml. Pair it with the parse options above to move a compose file from a machine that can resolve it to one that must not try.

import * as fs from 'node:fs/promises';
import { parse, serialize } from '@balena/compose-parser';

// On the user's machine: resolve everything against their environment and files
const composition = await parse('docker-compose.yml', { hostEnvironment: true });
await fs.writeFile('folded.yml', serialize(composition));

// On the builder: re-read it without touching the environment or the filesystem
const reparsed = await parse('folded.yml', { skipInterpolation: true });
// deep-equals `composition`

The output carries no env_file, label_file, include or extends, since parse() has already folded those in.

Whatever reads the output must parse it with skipInterpolation: true. Values were already interpolated on the first parse, and a $ still in the output is meant literally; interpolating again would change it, or fill it from the reader's own environment.

On Windows, serialize() writes build.context with / separators, and throws if the context is on a different drive from the compose file.

Image Descriptors

The toImageDescriptors() function converts a composition into descriptors that can be used for image operations:

const descriptors = toImageDescriptors(composition);
// Returns array of:
// {
//   serviceName: string,
//   image: string | BuildConfig,
//   contract?: ContractObject  // if service has contract requirement labels
// }

Balena-Specific Features

Contract Labels

Supports validation of contract requirement labels:

  • io.balena.features.requires.sw.supervisor: Semver range for Supervisor version
  • io.balena.features.requires.hw.device-type: Device type slug
  • io.balena.features.requires.arch.sw: Architecture (aarch64, amd64, armv7hf, etc.)
  • io.balena.features.requires.sw.l4t: L4T version range

版本与发布记录

@balena/compose-parser 共发布 87 个版本,首次发布于 2025/09/10;过去 12 个月发布了 55 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/11/11今天
1.1.0-build-serialize-75e2efb6d99eaa72cf1afaf19f39b6cb2f2e2cc0-12026/11/11今天
1.1.0-build-serialize-5befb41ea84e6b463d919bd359a11143864a772b-12026/11/101 天前
1.1.0-build-serialize-d79c62b4d0ae84da7fa53e84bd30ba573be749a1-12026/11/056 天前
1.1.0-build-serialize-64bd0e7d01ced3f7d476f89ef8d673114b927f91-12026/11/037 天前
1.1.0-build-reject-new-compose-fields-84eaafa49643b999877c330cfbd5e6f3676536b5-12026/10/3012 天前

其他发布通道:build-setup-compose-go → 0.0.2-build-setup-compose-go-af4d6a8bc4adef8fbbdbd912bb6e40a873a77f6c-1,build-remove-default-composition-from-docs → 0.0.3-build-remove-default-composition-from-docs-d8c1906cce76a39f7a13aca175588bc8148ed394-1,build-pin-flowzone-workflow-to-a5e50b8 → 0.0.4-build-pin-flowzone-workflow-to-a5e50b8-24b77c164a3343efa54c766e3df85e280933d450-1,build-release-binaries-with-node-extension → 0.0.5-build-release-binaries-with-node-extension-9d0581b9ac30ce105c74e04902ec4e66cae2fb74-1,build-binary-snakecase → 0.0.6-build-binary-snakecase-764fdcf67831120284d2b0ecd3951ac906fd846f-1,build-update-go-usage-message → 0.0.7-build-update-go-usage-message-11aff93ea10bf27c5152df723b6b4c373fb0d9cf-1。

关于 @balena/compose-parser 的常见问题

@balena/compose-parser 是什么?

Wraps compose-go to parse balena-compatible docker-compose.yml files

如何安装 @balena/compose-parser?

在项目目录运行 npm install @balena/compose-parser,或使用 pnpm add / yarn add / bun add。

@balena/compose-parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@balena/compose-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@balena/compose-parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 22.14.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@balena/compose-parser 可以商用吗?

@balena/compose-parser 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@balena/compose-parser 的下载量有多少?

根据 npm 官方下载统计,@balena/compose-parser 在 2026/10/08 – 2026/11/06 这 30 天内被下载 10,880 次,日均约 363 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@balena/compose-parser 还在维护吗?

最新版本 1.1.0 发布于 2026/11/11(今天);至今共发布 87 个版本,其中过去 12 个月发布了 55 个;首次发布于 2025/09/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。