CodeSampleX

示例

tinyexec 1.3.0: NonZeroExitError

已验证示例 — npm tinyexec 1.3.0: NonZeroExitError. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: NonZeroExitError is an Error subclass representing…

sha256:72b59841986713e9c5eff359f36e9ec3254a95ff24d73fc4c29a157e31bb0a71

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

案例

HOW
目标
verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0
包
符号
  • tinyexec.NonZeroExitError
环境
node 22.23.2
创建时间
2026-09-06T14:42:53Z

契约

  1. NonZeroExitError is an Error subclass representing command exit failure
  2. NonZeroExitError constructor populates exitCode, signalCode, output, and non-enumerable result
  3. NonZeroExitError formats error message with command and arguments on non-zero exit
  4. NonZeroExitError formats error message with termination signal when process is killed
  5. x throws NonZeroExitError containing stdout, stderr, and exitCode when throwOnError is enabled
  6. xSync throws NonZeroExitError containing output and exitCode when throwOnError is enabled
  7. ExecProcess async iteration throws NonZeroExitError when process terminates with non-zero exit code
  8. NonZeroExitError signalCode captures process signal when process is killed with throwOnError enabled

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/tinyexec@1.3.0
Demonstrate these symbols/APIs:
  - tinyexec.NonZeroExitError

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c4c1b270cab4700de29b180fb76b7423b339c1f5d220f6cec080021cf61a815a","contract":["NonZeroExitError is an Error subclass representing command exit failure","NonZeroExitError constructor populates exitCode, signalCode, output, and non-enumerable result","NonZeroExitError formats error message with command and arguments on non-zero exit","NonZeroExitError formats error message with termination signal when process is killed","x throws NonZeroExitError containing stdout, stderr, and exitCode when throwOnError is enabled","xSync throws NonZeroExitError containing output and exitCode when throwOnError is enabled","ExecProcess async iteration throws NonZeroExitError when process terminates with non-zero exit code","NonZeroExitError signalCode captures process signal when process is killed with throwOnError enabled"],"goal":"verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0","kind":"HOW","packages":["pkg:npm/tinyexec@1.3.0"],"schemaVersion":1,"symbols":["tinyexec.NonZeroExitError"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/tinyexec@1.3.0"],"schemaVersion":1,"subject":"pkg:npm/tinyexec@1.3.0","symbols":["tinyexec.NonZeroExitError"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-tinyexec-nonzeroexiterror",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-tinyexec-nonzeroexiterror",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "tinyexec": "1.3.0"
      }
    },
    "node_modules/tinyexec": {
      "version": "1.3.0",
      "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.0.tgz",
      "integrity": "sha512-QKAl9m8gWWGHV8jZcPeym6j+XULi6tOf1mT83WYJ4Lk2ytW/uwAWkrP0uFsdoYMdueVJ0qs26wZ+23xeB4ibNQ==",
      "license": "MIT",
      "engines": {
        "node": ">=18"
      }
    }
  }
}
package.json
{
  "name": "sample-tinyexec-nonzeroexiterror",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0",
  "type": "module",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "tinyexec": "1.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify tinyexec.NonZeroExitError in pkg:npm/tinyexec@1.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/tinyexec@1.3.0"
  ],
  "symbols": [
    "tinyexec.NonZeroExitError"
  ]
}
src/index.js
import { NonZeroExitError, x, xSync, ExecProcess } from 'tinyexec';

/**
 * Creates a NonZeroExitError instance.
 * @param {import('tinyexec').CommonOutputApi} result - Execution output API instance
 * @param {import('tinyexec').Output} [output] - Output object with stdout, stderr, exitCode
 * @param {string} [command] - Command name
 * @param {string[]} [args] - Command arguments
 * @returns {NonZeroExitError}
 */
export function createNonZeroExitError(result, output, command, args) {
  return new NonZeroExitError(result, output, command, args);
}

/**
 * Executes a command asynchronously with throwOnError enabled.
 * @param {string} command - Command to run
 * @param {string[]} [args] - Arguments
 * @param {object} [options] - Options
 * @returns {import('tinyexec').Result}
 */
export function runWithThrowOnError(command, args, options) {
  return x(command, args, { ...options, throwOnError: true });
}

/**
 * Executes a command synchronously with throwOnError enabled.
 * @param {string} command - Command to run
 * @param {string[]} [args] - Arguments
 * @param {object} [options] - Options
 * @returns {import('tinyexec').SyncResult}
 */
export function runSyncWithThrowOnError(command, args, options) {
  return xSync(command, args, { ...options, throwOnError: true });
}

export { NonZeroExitError, x, xSync, ExecProcess };
test/contract.mjs
import assert from 'node:assert/strict';
import {
  NonZeroExitError,
  x,
  xSync,
  ExecProcess,
  createNonZeroExitError,
  runWithThrowOnError,
  runSyncWithThrowOnError
} from '../src/index.js';

// 1. NonZeroExitError is an Error subclass representing command exit failure
assert.equal(typeof NonZeroExitError, 'function', 'NonZeroExitError must be a constructor function');
assert.ok(NonZeroExitError.prototype instanceof Error, 'NonZeroExitError must extend Error');
assert.equal(typeof createNonZeroExitError, 'function');
assert.equal(typeof runWithThrowOnError, 'function');
assert.equal(typeof runSyncWithThrowOnError, 'function');

// 2. NonZeroExitError constructor populates exitCode, signalCode, output, and non-enumerable result
{
  const mockResult = {
    exitCode: 42,
    signalCode: null,
    pid: 1234,
    killed: false
  };
  const mockOutput = {
    stdout: 'mock stdout',
    stderr: 'mock stderr',
    exitCode: 42
  };
  const err = createNonZeroExitError(mockResult, mockOutput, 'my-cmd', ['--flag', 'value with spaces']);
  assert.ok(err instanceof Error);
  assert.ok(err instanceof NonZeroExitError);
  assert.equal(err.exitCode, 42);
  assert.equal(err.signalCode, null);
  assert.deepEqual(err.output, mockOutput);
  assert.equal(err.result, mockResult);
  assert.equal(Object.prototype.propertyIsEnumerable.call(err, 'result'), false);
}

// 3. NonZeroExitError formats error message with command and arguments on non-zero exit
{
  const mockResult = {
    exitCode: 2,
    signalCode: null,
    pid: 2222,
    killed: false
  };
  const errWithArgs = createNonZeroExitError(mockResult, undefined, 'test-bin', ['arg1', 'arg with space']);
  assert.equal(errWithArgs.message, 'The command `test-bin arg1 "arg with space"` exited with a non-zero status (2)');

  const errNoCommand = createNonZeroExitError(mockResult);
  assert.equal(errNoCommand.message, 'The process exited with a non-zero status (2)');
}

// 4. NonZeroExitError formats error message with termination signal when process is killed
{
  const mockResult = {
    exitCode: null,
    signalCode: 'SIGKILL',
    pid: 3333,
    killed: true
  };
  const err = createNonZeroExitError(mockResult, undefined, 'long-runner');
  assert.equal(err.signalCode, 'SIGKILL');
  assert.equal(err.exitCode, 1);
  assert.equal(err.message, 'The command `long-runner` was killed by the signal SIGKILL');
}

// 5. x throws NonZeroExitError containing stdout, stderr, and exitCode when throwOnError is enabled
{
  await assert.rejects(
    async () => {
      await runWithThrowOnError('node', [
        '-e',
        'process.stdout.write("standard message\\n"); process.stderr.write("error message\\n"); process.exit(7);'
      ]);
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.exitCode, 7);
      assert.equal(err.signalCode, null);
      assert.equal(err.output?.stdout, 'standard message\n');
      assert.equal(err.output?.stderr, 'error message\n');
      assert.equal(err.output?.exitCode, 7);
      return true;
    }
  );
}

// 6. xSync throws NonZeroExitError containing output and exitCode when throwOnError is enabled
{
  assert.throws(
    () => {
      runSyncWithThrowOnError('node', [
        '-e',
        'process.stdout.write("sync stdout\\n"); process.stderr.write("sync stderr\\n"); process.exit(9);'
      ]);
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.exitCode, 9);
      assert.equal(err.signalCode, null);
      assert.equal(err.output?.stdout, 'sync stdout\n');
      assert.equal(err.output?.stderr, 'sync stderr\n');
      assert.equal(err.output?.exitCode, 9);
      return true;
    }
  );
}

// 7. ExecProcess async iteration throws NonZeroExitError when process terminates with non-zero exit code
{
  const proc = new ExecProcess(
    'node',
    ['-e', 'console.log("line-one"); console.log("line-two"); process.exit(11);'],
    { throwOnError: true }
  );
  proc.spawn();
  const receivedLines = [];
  await assert.rejects(
    async () => {
      for await (const line of proc) {
        receivedLines.push(line.trim());
      }
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.exitCode, 11);
      return true;
    }
  );
  assert.deepEqual(receivedLines, ['line-one', 'line-two']);
}

// 8. NonZeroExitError signalCode captures process signal when process is killed with throwOnError enabled
{
  await assert.rejects(
    async () => {
      const proc = runWithThrowOnError('node', ['-e', 'setInterval(() => {}, 1000);']);
      await new Promise((resolve) => setTimeout(resolve, 50));
      proc.kill('SIGTERM');
      await proc;
    },
    (err) => {
      assert.ok(err instanceof NonZeroExitError, 'Expected instance of NonZeroExitError');
      assert.equal(err.signalCode, 'SIGTERM');
      assert.ok(err.message.includes('was killed by the signal SIGTERM'));
      return true;
    }
  );
}

console.log('All contract assertions passed.');

原始种子者

匿名