CodeSampleX

示例

bare-posix 1.0.1: geteuid

已验证示例 — npm bare-posix 1.0.1: geteuid. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: declares geteuid returning number and seteuid accepting number…

sha256:8e311052ef07a8e2e6519bf53ee60d340d7620402949cc4d8391799ce7e254b9

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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-07

案例

HOW
目标
verify bare-posix.geteuid in pkg:npm/bare-posix@1.0.1
包
符号
  • bare-posix.geteuid
创建时间
2026-09-07T02:42:26Z

契约

  1. declares geteuid returning number and seteuid accepting number or string in TypeScript definition index.d.ts
  2. unsupported platform fallback implements geteuid returning -1 and seteuid throwing platform not supported error
  3. native C binding implements bare_posix_geteuid invoking POSIX geteuid and bare_posix_seteuid invoking seteuid
  4. module wrapper exports geteuid delegating to native binding returning effective user ID
  5. module wrapper seteuid accepts numeric user ID and forwards directly to native binding
  6. module wrapper seteuid resolves string username to UID via getpwnam before calling native binding

文件

  • PROMPT.md
  • csx.json
  • 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 bare-posix.geteuid in pkg:npm/bare-posix@1.0.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/bare-posix@1.0.1
Demonstrate these symbols/APIs:
  - bare-posix.geteuid

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:a2aa3d417fb61c1bc578ea4ddc71ceb2d85ad61677cfce7140400caeb88ea4d8","contract":["declares geteuid returning number and seteuid accepting number or string in TypeScript definition index.d.ts","unsupported platform fallback implements geteuid returning -1 and seteuid throwing platform not supported error","native C binding implements bare_posix_geteuid invoking POSIX geteuid and bare_posix_seteuid invoking seteuid","module wrapper exports geteuid delegating to native binding returning effective user ID","module wrapper seteuid accepts numeric user ID and forwards directly to native binding","module wrapper seteuid resolves string username to UID via getpwnam before calling native binding"],"goal":"verify bare-posix.geteuid in pkg:npm/bare-posix@1.0.1","kind":"HOW","packages":["pkg:npm/bare-posix@1.0.1"],"schemaVersion":1,"symbols":["bare-posix.geteuid"]},"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-posix@1.0.1"],"schemaVersion":1,"subject":"pkg:npm/bare-posix@1.0.1","symbols":["bare-posix.geteuid"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-bare-posix-geteuid",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-posix-geteuid",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "bare-posix": "1.0.1"
      }
    },
    "node_modules/bare-posix": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/bare-posix/-/bare-posix-1.0.1.tgz",
      "integrity": "sha512-b4quA8dyRVvX0aFIAIiev5zFATHwIUGr42Gmt+CqiBNg/bdsNkrj4bbkeotMOMiljJS4DCME5kywJO/TFwKo1A==",
      "license": "Apache-2.0"
    }
  }
}
package.json
{
  "name": "sample-bare-posix-geteuid",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "bare-posix": "1.0.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify bare-posix.geteuid in pkg:npm/bare-posix@1.0.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-posix@1.0.1"
  ],
  "symbols": [
    "bare-posix.geteuid"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import fs from 'node:fs';
import path from 'node:path';

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

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

assert.equal(pkg.name, 'bare-posix', 'Package name must be bare-posix');
assert.equal(pkg.version, '1.0.1', 'Package version must be 1.0.1');
assert.equal(pkg.addon, true, 'Package must be marked as Bare addon');

const pkgDir = path.dirname(pkgPath);

// 2. Verify TypeScript declarations for bare-posix.geteuid and bare-posix.seteuid
const indexDtsPath = path.join(pkgDir, 'index.d.ts');
assert.ok(fs.existsSync(indexDtsPath), 'index.d.ts must exist');
const indexDts = fs.readFileSync(indexDtsPath, 'utf8');

assert.ok(indexDts.includes('export function geteuid(): number'), 'index.d.ts must declare geteuid');
assert.ok(indexDts.includes('export function seteuid(id: number | string): void'), 'index.d.ts must declare seteuid');

// 3. Verify unsupported platform fallback implementation in unsupported.js
const unsupportedPath = path.join(pkgDir, 'unsupported.js');
assert.ok(fs.existsSync(unsupportedPath), 'unsupported.js must exist');
const unsupported = require(unsupportedPath);

assert.equal(typeof unsupported.geteuid, 'function', 'unsupported.geteuid must be a function');
assert.equal(unsupported.geteuid(), -1, 'unsupported.geteuid() must return -1');
assert.throws(() => unsupported.seteuid(0), /Platform not supported/, 'unsupported.seteuid must throw Platform not supported');

// 4. Verify C native addon source in binding.c
const bindingCPath = path.join(pkgDir, 'binding.c');
assert.ok(fs.existsSync(bindingCPath), 'binding.c must exist');
const bindingC = fs.readFileSync(bindingCPath, 'utf8');

assert.ok(bindingC.includes('bare_posix_geteuid'), 'binding.c must implement bare_posix_geteuid');
assert.ok(bindingC.includes('geteuid()'), 'binding.c must call POSIX geteuid()');
assert.ok(bindingC.includes('bare_posix_seteuid'), 'binding.c must implement bare_posix_seteuid');
assert.ok(bindingC.includes('seteuid('), 'binding.c must call POSIX seteuid()');
assert.ok(bindingC.includes('BARE_MODULE(bare_posix, bare_posix_exports)'), 'binding.c must export BARE_MODULE');

// 5. Verify index.js wrapper logic with mock native binding
const bindingJsPath = path.join(pkgDir, 'binding.js');
const indexJsPath = path.join(pkgDir, 'index.js');
assert.ok(fs.existsSync(bindingJsPath), 'binding.js must exist');
assert.ok(fs.existsSync(indexJsPath), 'index.js must exist');

const mockCalls = [];
const mockBinding = {
  geteuid: () => 1000,
  seteuid: (id) => { mockCalls.push(['seteuid', id]); },
  getpwnam: (name) => {
    if (name === 'root') return { username: 'root', passwd: 'x', uid: 0, gid: 0, gecos: 'root', homedir: '/root', shell: '/bin/bash' };
    if (name === 'nobody') return { username: 'nobody', passwd: 'x', uid: 65534, gid: 65534, gecos: 'nobody', homedir: '/nonexistent', shell: '/usr/sbin/nologin' };
    return null;
  }
};

require.cache[bindingJsPath] = {
  id: bindingJsPath,
  filename: bindingJsPath,
  loaded: true,
  exports: mockBinding
};

delete require.cache[indexJsPath];
const posix = require(indexJsPath);

assert.equal(typeof posix.geteuid, 'function', 'posix.geteuid must be exported as a function');
assert.equal(posix.geteuid(), 1000, 'posix.geteuid() must delegate to binding.geteuid');

// Test seteuid with numeric ID
posix.seteuid(500);
assert.deepEqual(mockCalls[mockCalls.length - 1], ['seteuid', 500], 'posix.seteuid with numeric ID must pass integer directly to binding');

// Test seteuid with string username (delegating to getpwnam)
posix.seteuid('root');
assert.deepEqual(mockCalls[mockCalls.length - 1], ['seteuid', 0], 'posix.seteuid with root must resolve to uid 0');

posix.seteuid('nobody');
assert.deepEqual(mockCalls[mockCalls.length - 1], ['seteuid', 65534], 'posix.seteuid with nobody must resolve to uid 65534');

console.log('Contract tests passed successfully.');

原始种子者

匿名