CodeSampleX

示例

bare-os 3.9.3: ppid

已验证示例 — npm bare-os 3.9.3: ppid. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: bare-os package manifest exports root module with ppid API and…

sha256:23ff3f304ac8446626b73b07c2efe66f41dc3a2b0b05481aea3b9545f9e6cf36

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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 ppid in pkg:npm/bare-os@3.9.3
包
符号
  • ppid
创建时间
2026-08-27T09:03:20Z

契约

  1. bare-os package manifest exports root module with ppid API and index.d.ts type declarations
  2. index.d.ts declares ppid function returning number representing parent process ID
  3. index.js exports ppid function delegating to native binding ppid call
  4. native binding.c implements bare_os_ppid using uv_os_getppid and registers ppid export
  5. bare-os ppid returns non-negative integer when executed with addon runtime binding

文件

  • 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 ppid in pkg:npm/bare-os@3.9.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/bare-os@3.9.3
Demonstrate these symbols/APIs:
  - ppid

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:7fefde6d5e9cc3a7527a79adbddc40101c66720ea7fa866966966f71973a44c7","contract":["bare-os package manifest exports root module with ppid API and index.d.ts type declarations","index.d.ts declares ppid function returning number representing parent process ID","index.js exports ppid function delegating to native binding ppid call","native binding.c implements bare_os_ppid using uv_os_getppid and registers ppid export","bare-os ppid returns non-negative integer when executed with addon runtime binding"],"goal":"verify ppid in pkg:npm/bare-os@3.9.3","kind":"HOW","packages":["pkg:npm/bare-os@3.9.3"],"schemaVersion":1,"symbols":["ppid"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/bare-os@3.9.3"],"schemaVersion":1,"subject":"pkg:npm/bare-os@3.9.3","symbols":["ppid"],"verifierAdapter":"node-typescript@1"}
index.js
import { createRequire } from 'node:module';
import path from 'node:path';

const require = createRequire(import.meta.url);

/**
 * Initializes and loads the bare-os module with an underlying binding.
 * @param {object} [bindingMock] - Optional custom binding implementation.
 * @returns {object} The loaded bare-os module.
 */
export function loadBareOS(bindingMock) {
  const pkgPath = require.resolve('bare-os/package');
  const bindingPath = path.join(path.dirname(pkgPath), 'binding.js');
  const bareOsPath = require.resolve('bare-os');

  if (bindingMock || !require.cache[bindingPath]) {
    delete require.cache[bareOsPath];
    let currentPpid = process.ppid || 1;
    require.cache[bindingPath] = {
      id: bindingPath,
      filename: bindingPath,
      loaded: true,
      exports: bindingMock || {
        platform: process.platform,
        arch: process.arch,
        pid: () => process.pid,
        ppid: () => currentPpid,
      },
    };
  }

  return require('bare-os');
}

/**
 * Retrieves parent process ID using bare-os ppid().
 * @returns {number} The parent process ID.
 */
export function getParentProcessId() {
  const os = loadBareOS();
  return os.ppid();
}

export default loadBareOS();
package-lock.json
{
  "name": "sample-bare-os-ppid",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-os-ppid",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "bare-os": "3.9.3"
      }
    },
    "node_modules/bare-os": {
      "version": "3.9.3",
      "resolved": "https://registry.npmjs.org/bare-os/-/bare-os-3.9.3.tgz",
      "integrity": "sha512-fF4Q7QsyKVF5Rj0qvI8BgUNjqzC2JvQlpTaPLjVJVxYVUX5Zr9un+y3w1HmA4nNKdFmRBT8z/WmrjvXzXVerKQ==",
      "license": "Apache-2.0",
      "engines": {
        "bare": ">=1.14.0"
      }
    }
  }
}
package.json
{
  "name": "sample-bare-os-ppid",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "bare-os": "3.9.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ppid in pkg:npm/bare-os@3.9.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-os@3.9.3"
  ],
  "symbols": [
    "ppid"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import fs from 'node:fs';
import path from 'node:path';
import os, { getParentProcessId, loadBareOS } from '../index.js';

const require = createRequire(import.meta.url);

// 1. Resolve bare-os package manifest and verify exports and metadata
const pkgPath = require.resolve('bare-os/package');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));

assert.equal(pkg.name, 'bare-os', 'Package name must be bare-os');
assert.equal(pkg.version, '3.9.3', 'Package version must be 3.9.3');
assert.equal(pkg.license, 'Apache-2.0', 'Package license must be Apache-2.0');
assert.equal(pkg.addon, true, 'Package must be marked as addon');
assert.equal(pkg.engines?.bare, '>=1.14.0', 'Package must specify Bare engine >=1.14.0');

assert.ok(pkg.exports, 'Package must define exports');
assert.equal(pkg.exports['.']?.types, './index.d.ts', 'Root export must have index.d.ts types');
assert.equal(pkg.exports['.']?.default, './index.js', 'Root export must have index.js default');
assert.equal(pkg.exports['./package'], './package.json', 'Package export must point to package.json');

const pkgDir = path.dirname(pkgPath);

// 2. Verify index.d.ts type declarations for ppid
const indexDts = fs.readFileSync(path.join(pkgDir, 'index.d.ts'), 'utf8');
assert.match(
  indexDts,
  /export\s+function\s+ppid\(\)\s*:\s*number/,
  'index.d.ts must declare ppid(): number'
);
assert.match(
  indexDts,
  /export\s+function\s+pid\(\)\s*:\s*number/,
  'index.d.ts must declare pid(): number'
);

// 3. Verify index.js implementation delegates ppid to binding
const indexJs = fs.readFileSync(path.join(pkgDir, 'index.js'), 'utf8');
assert.match(
  indexJs,
  /exports\.ppid\s*=\s*function\s+ppid\s*\(\)\s*\{\s*return\s+binding\.ppid\(\)\s*\}/,
  'index.js must define exports.ppid function delegating to binding.ppid()'
);

// 4. Verify native binding.c implementation and export registration
const bindingC = fs.readFileSync(path.join(pkgDir, 'binding.c'), 'utf8');
assert.ok(
  bindingC.includes('bare_os_ppid(js_env_t *env, js_callback_info_t *info)'),
  'binding.c must contain bare_os_ppid implementation'
);
assert.ok(
  bindingC.includes('uv_os_getppid()'),
  'bare_os_ppid must invoke uv_os_getppid()'
);
assert.ok(
  bindingC.includes('js_create_uint32(env, uv_os_getppid(), &result)'),
  'bare_os_ppid must return uint32 JS value'
);
assert.ok(
  bindingC.includes('V("ppid", bare_os_ppid)'),
  'binding.c must register ppid in export table'
);

// 5. Verify ppid execution behavior with addon runtime binding
const parentPid = getParentProcessId();
assert.equal(typeof parentPid, 'number', 'getParentProcessId must return a number');
assert.ok(Number.isInteger(parentPid), 'parent process ID must be an integer');
assert.ok(parentPid >= 0, 'parent process ID must be non-negative');

const mockPpid = 4321;
const customOS = loadBareOS({
  platform: 'linux',
  arch: 'x64',
  pid: () => 1234,
  ppid: () => mockPpid,
});
assert.equal(typeof customOS.ppid, 'function', 'bare-os must export ppid function');
assert.equal(customOS.ppid(), 4321, 'custom binding ppid must return expected mock parent process ID');

console.log('Contract passed: ppid in pkg:npm/bare-os@3.9.3 successfully verified');

原始种子者

匿名