@andrian.yablonskyy/thub-common

中文文档
2.2.6发布于 2026/11/03SEE LICENSE IN LICENSE.mdCommonJS近 30 天下载 2590

Shared job-spec schema, state enums and a thin API client used by the Agent and Client

原始使用文档
文档目录9 节
  1. Install
  2. What's in it
  3. Job spec schema and validation
  4. State enums and exit codes
  5. API client
  6. Reserved resource remote channels
  7. Version checks and self-update helpers
  8. Development
  9. License

@andrian.yablonskyy/thub-common

Shared code for TestHub — a self-hosted job network that lets CI/CD pipelines and individual developers run firmware tests on real hardware or emulators in a private lab. This package has no server and no CLI of its own; it's the small, dependency-light core that the Agent, Coordinator and Client all depend on, so the three speak exactly the same protocol.

Install

npm install @andrian.yablonskyy/thub-common

What's in it

Job spec schema and validation

An Ajv JSON schema for the job specification the Agent sends to the Coordinator, and a thin wrapper that validates a spec and fills in its defaults.

const { validateJobSpec } = require('@andrian.yablonskyy/thub-common');

const { valid, spec, errors } = validateJobSpec({
  target: { type: 'hw', labels: ['board:nucleo-f401re'] },
  command: './ci/test.sh',
  downloads: [{ url: 'https://artifactory.example.com/app.bin' }],
  git: { url: 'https://github.com/yourorg/firmware-tests.git', ref: 'v1.4.0' }
});

A job is a task: command (required — the shell command the Client runs as its entry point, args as "$@"), with optional inputs downloads ([{ url }]), git (url, optional ref — branch, tag or commit — and depth, default 1) and, for SW jobs, image (a Docker image to run as the DUT). Other top-level fields: target (type, labels, optional group/client), suite, timeoutSec, priority, source (ci/cli), user (a free-text job-owner label), meta (arbitrary key/value metadata), and dryRun. A spec in the old shape (firmware/tests, from an Agent older than --command) is refused with a message to update the Agent.

State enums and exit codes

const { RESOURCE_STATES, JOB_STATES, ACTIVE_JOB_STATES, TERMINAL_JOB_STATES, BUSY_SOURCES, EXIT_CODES, exitCodeForJobState } = require('@andrian.yablonskyy/thub-common');
  • RESOURCE_STATES: REGISTERED, IDLE, BUSY, OUT_OF_SERVICE, MAINTENANCE.
  • JOB_STATES: QUEUED, ASSIGNED, PREPARING, RUNNING, PASSED, FAILED, ERROR, TIMEOUT, CANCELED, LOST. ACTIVE_JOB_STATES/TERMINAL_JOB_STATES are the two natural partitions of that set.
  • BUSY_SOURCES: ci, cli, local — why a resource is currently busy.
  • EXIT_CODES / exitCodeForJobState(state): the Agent CLI's process exit codes (PASSED → 0, FAILED → 1, ERROR/TIMEOUT/LOST → 2, CANCELED → 3, usage/auth error → 4, detached with Ctrl-C → 130), so a CI step can branch on $? without parsing output.

API client

A minimal fetch-based HTTP client for the Coordinator's /api/v1 surface, with a hand-rolled Server-Sent-Events parser (no external SSE dependency) — used by both the Agent and the Client so they speak the exact same protocol.

const { ApiClient } = require('@andrian.yablonskyy/thub-common');

// userAgent is optional; the Coordinator records "thub-agent/<v>" / "thub-client/<v>" versions for its dashboard
const client = new ApiClient({ baseUrl: 'https://thub.example.com', token: 'agt_...', userAgent: 'thub-agent/1.0.8' });
const job = await client.post('/jobs', spec);
await client.streamEvents(`/jobs/${job.jobId}/logs/stream`, {
  onEvent: ({ event, id, data }) => console.log(event, data)
});

request()/get()/post() handle JSON bodies, query strings and non-2xx errors (throwing with .status/.body set); streamEvents() reconnects the caller is expected to drive (it just parses one connection's worth of events and resolves when the stream ends) and honors Last-Event-ID for resuming after a drop.

Reserved resource remote channels

The Agent, Coordinator and Client share a WebSocket channel protocol for reserved resources. Besides UART, SWD, RTT and video channels, a holder can request a named service channel with a Client port. RTT opens an OpenOCD session over the selected ST-Link, searches a configured target RAM range for the SEGGER control block, and streams one up-channel. The Client connects a service channel only to 127.0.0.1:<port> on its own host; it never exposes a Client-side listener to the network.

Version checks and self-update helpers

PACKAGES (the Coordinator/Agent/Client npm names), fetchLatestVersion(pkg, { registry }) (the latest dist-tag from the npm registry), compareVersions/isNewer/isValidVersion (strict X.Y.Z[-pre]), npmBin() (the npm next to the running node) and npmInstallGlobal(pkg, version) (only for the three TestHub packages, only a valid version).

Development

npm install
npm test    # node --test test/*.test.js
npm run lint

License

See LICENSE.md.

版本与发布记录

@andrian.yablonskyy/thub-common 共发布 55 个版本,首次发布于 2026/09/22;过去 12 个月发布了 55 个版本。

版本发布日期距今
2.2.6latest2026/11/034 天前
2.2.52026/11/025 天前
2.2.42026/10/1523 天前
2.2.32026/10/1523 天前
2.2.22026/10/1424 天前
2.2.12026/10/1425 天前

关于 @andrian.yablonskyy/thub-common 的常见问题

@andrian.yablonskyy/thub-common 是什么?

Shared job-spec schema, state enums and a thin API client used by the Agent and Client

如何安装 @andrian.yablonskyy/thub-common?

在项目目录运行 npm install @andrian.yablonskyy/thub-common,或使用 pnpm add / yarn add / bun add。

@andrian.yablonskyy/thub-common 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/andrian.yablonskyy__thub-common 是否存在。

@andrian.yablonskyy/thub-common 支持 ESM 还是 CommonJS?

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

@andrian.yablonskyy/thub-common 需要什么版本的 Node.js?

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

@andrian.yablonskyy/thub-common 可以商用吗?

@andrian.yablonskyy/thub-common 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@andrian.yablonskyy/thub-common 的下载量有多少?

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

@andrian.yablonskyy/thub-common 还在维护吗?

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