CodeSampleX

示例

exit 0.1.2: exit

已验证示例 — npm exit 0.1.2: exit. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert exit is a function accepting exit code and streams array

sha256:6411e70962709c59faba8e022aecae68ba674f2d4bd1c7d04d1eb64049a2448f

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

案例

HOW
目标
verify pkg:npm/exit@0.1.2
包
符号
  • exit
环境
node 22.23.2
创建时间
2026-08-27T12:41:36Z

契约

  1. assert exit is a function accepting exit code and streams array
  2. assert exit immediately terminates process with given code when all streams have bufferSize 0
  3. assert exit waits for non-empty streams to drain before calling process.exit
  4. assert exit replaces stream.write with noop function during termination
  5. assert exit synchronously flushes output and terminates child process with exit code
  6. assert exit registers process exit listener to ensure exit code propagation

文件

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • 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 pkg:npm/exit@0.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/exit@0.1.2
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:0caf1222df127738e39d1c9a83391bc54ae7a42c6548f3cb74a750581eaa5b5f","constraints":{"executionContext":"node"},"contract":["assert exit is a function accepting exit code and streams array","assert exit immediately terminates process with given code when all streams have bufferSize 0","assert exit waits for non-empty streams to drain before calling process.exit","assert exit replaces stream.write with noop function during termination","assert exit synchronously flushes output and terminates child process with exit code","assert exit registers process exit listener to ensure exit code propagation"],"goal":"verify pkg:npm/exit@0.1.2","kind":"HOW","packages":["pkg:npm/exit@0.1.2"],"schemaVersion":1,"symbols":["exit"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/exit@0.1.2"],"schemaVersion":1,"subject":"pkg:npm/exit@0.1.2","symbols":["exit"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const exit = require('exit');

/**
 * Invokes exit function to flush streams and terminate process.
 *
 * @param {number} [exitCode] - Process exit status code.
 * @param {Array<object>} [streams] - Streams to drain before exit.
 */
function safeExit(exitCode, streams) {
  return exit(exitCode, streams);
}

module.exports = {
  exit,
  safeExit,
};
package-lock.json
{
  "name": "sample-exit",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-exit",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "exit": "0.1.2"
      }
    },
    "node_modules/exit": {
      "version": "0.1.2",
      "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz",
      "integrity": "sha512-Zk/eNKV2zbjpKzrsQ+n1G6poVbErQxJ0LBOJXaKZ1EViLzH+hrLu9cdXI4zw9dBQJslwBEpbQ2P1oS7nDxs6jQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8.0"
      }
    }
  }
}
package.json
{
  "name": "sample-exit",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for exit",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "exit": "0.1.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/exit@0.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/exit@0.1.2"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert';
import { spawnSync } from 'node:child_process';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const { exit, safeExit } = require('../index.js');

// Contract 1: assert exit is a function accepting exit code and streams array
assert.strictEqual(typeof exit, 'function', 'exit must be a function');
assert.strictEqual(typeof safeExit, 'function', 'safeExit must be a function');
assert.strictEqual(exit.length, 2, 'exit should accept 2 formal parameters');

// Contract 2: assert exit immediately terminates process with given code when all streams have bufferSize 0
{
  const origExit = process.exit;
  const origListeners = process.rawListeners('exit');
  let invokedCode = null;
  process.exit = (code) => {
    invokedCode = code;
  };

  try {
    const mockStream1 = { bufferSize: 0, write: () => {} };
    const mockStream2 = { bufferSize: 0, write: () => {} };
    safeExit(42, [mockStream1, mockStream2]);
    assert.strictEqual(invokedCode, 42, 'Expected process.exit to be called immediately with code 42');
  } finally {
    process.exit = origExit;
    process.removeAllListeners('exit');
    origListeners.forEach((fn) => process.on('exit', fn));
  }
}

// Contract 3: assert exit waits for non-empty streams to drain before calling process.exit
{
  const origExit = process.exit;
  const origListeners = process.rawListeners('exit');
  let invokedCode = null;
  process.exit = (code) => {
    invokedCode = code;
  };

  try {
    let drainCallback1 = null;
    let drainCallback2 = null;
    const mockStream1 = {
      bufferSize: 128,
      write: (str, enc, cb) => {
        drainCallback1 = cb;
      },
    };
    const mockStream2 = {
      bufferSize: 64,
      write: (str, enc, cb) => {
        drainCallback2 = cb;
      },
    };

    safeExit(7, [mockStream1, mockStream2]);
    assert.strictEqual(invokedCode, null, 'Process should not exit while streams are not drained');
    assert.strictEqual(typeof drainCallback1, 'function', 'Write callback 1 should be set');
    assert.strictEqual(typeof drainCallback2, 'function', 'Write callback 2 should be set');

    drainCallback1();
    assert.strictEqual(invokedCode, null, 'Process should not exit when only one stream drained');

    drainCallback2();
    assert.strictEqual(invokedCode, 7, 'Process should exit with code 7 after all streams drain');
  } finally {
    process.exit = origExit;
    process.removeAllListeners('exit');
    origListeners.forEach((fn) => process.on('exit', fn));
  }
}

// Contract 4: assert exit replaces stream.write with noop function during termination
{
  const origExit = process.exit;
  const origListeners = process.rawListeners('exit');
  let writeCalledAfterExit = false;
  process.exit = () => {};

  try {
    const mockStream = {
      bufferSize: 0,
      write: () => {
        writeCalledAfterExit = true;
      },
    };
    safeExit(0, [mockStream]);
    assert.strictEqual(typeof mockStream.write, 'function');
    mockStream.write('should be ignored');
    assert.strictEqual(writeCalledAfterExit, false, 'stream.write should be replaced with no-op');
  } finally {
    process.exit = origExit;
    process.removeAllListeners('exit');
    origListeners.forEach((fn) => process.on('exit', fn));
  }
}

// Contract 5: assert exit synchronously flushes output and terminates child process with exit code
{
  const testScript = `
    const { exit } = require(${JSON.stringify(require.resolve('../index.js'))});
    process.stdout.write('contract-stdout-payload\\n');
    process.stderr.write('contract-stderr-payload\\n');
    exit(13);
  `;
  const result = spawnSync(process.execPath, ['-e', testScript], { encoding: 'utf8' });
  assert.strictEqual(result.status, 13, 'Child process must exit with code 13');
  assert.strictEqual(result.stdout, 'contract-stdout-payload\n', 'Child process stdout must match written payload');
  assert.strictEqual(result.stderr, 'contract-stderr-payload\n', 'Child process stderr must match written payload');
}

// Contract 6: assert exit registers process exit listener to ensure exit code propagation
{
  const origExit = process.exit;
  const origListeners = process.rawListeners('exit');
  process.exit = () => {};

  try {
    const initialListenerCount = process.listenerCount('exit');
    const mockStream = { bufferSize: 0, write: () => {} };
    safeExit(9, [mockStream]);
    const postListenerCount = process.listenerCount('exit');
    assert.strictEqual(postListenerCount, initialListenerCount + 1, 'exit should attach an exit event listener');
  } finally {
    process.exit = origExit;
    process.removeAllListeners('exit');
    origListeners.forEach((fn) => process.on('exit', fn));
  }
}

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

原始种子者

匿名