CodeSampleX

示例

bare-os 3.9.3: endianness

已验证示例 — npm bare-os 3.9.3: endianness. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: package manifest specifies Bare addon configuration, Bare…

sha256:80aad914fe6742c2eaccff6d33ed3b748d40483d80b1c85bbb039f3ed33fa3aa

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-30

案例

HOW
目标
verify bare-os.endianness in pkg:npm/bare-os@3.9.3
包
符号
  • bare-os.endianness
环境
node 22.23.2
创建时间
2026-08-30T13:21:32Z

契约

  1. package manifest specifies Bare addon configuration, Bare engine requirement, and exports for root, constants, errors, and package subpaths
  2. index.d.ts declares export function endianness(): 'LE' | 'BE' for retrieving platform byte ordering
  3. index.js exports endianness function that evaluates binding.isLittleEndian and returns 'LE' or 'BE'
  4. bare-os.endianness returns endianness string ('LE' or 'BE') matching platform byte ordering
  5. getEndianness, isLittleEndian, and isBigEndian helpers accurately reflect bare-os.endianness return value
  6. bare-os.endianness returns 'LE' when binding.isLittleEndian is true and 'BE' when false
  7. bare-os.endianness executes deterministically and idempotently across multiple invocations
  8. binding.c determines endianness using uint16_t byte order test and registers isLittleEndian boolean property in addon exports

文件

  • 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 bare-os.endianness 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:
  - bare-os.endianness
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:fbd69768a3bebaf6ce4260a1e02314f3afa12bec20362c567d05f55623f09491","constraints":{"executionContext":"node"},"contract":["package manifest specifies Bare addon configuration, Bare engine requirement, and exports for root, constants, errors, and package subpaths","index.d.ts declares export function endianness(): 'LE' | 'BE' for retrieving platform byte ordering","index.js exports endianness function that evaluates binding.isLittleEndian and returns 'LE' or 'BE'","bare-os.endianness returns endianness string ('LE' or 'BE') matching platform byte ordering","getEndianness, isLittleEndian, and isBigEndian helpers accurately reflect bare-os.endianness return value","bare-os.endianness returns 'LE' when binding.isLittleEndian is true and 'BE' when false","bare-os.endianness executes deterministically and idempotently across multiple invocations","binding.c determines endianness using uint16_t byte order test and registers isLittleEndian boolean property in addon exports"],"goal":"verify bare-os.endianness in pkg:npm/bare-os@3.9.3","kind":"HOW","packages":["pkg:npm/bare-os@3.9.3"],"schemaVersion":1,"symbols":["bare-os.endianness"]},"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/bare-os@3.9.3"],"schemaVersion":1,"subject":"pkg:npm/bare-os@3.9.3","symbols":["bare-os.endianness"],"verifierAdapter":"node-typescript@1"}
index.js
import { createRequire } from 'node:module';
import path from 'node:path';
import nodeOS from 'node:os';

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

// Ensure native binding is provided in Node.js environments where require.addon is absent
if (typeof Bare === 'undefined') {
  const mockBinding = {
    platform: process.platform,
    arch: process.arch,
    type() { return nodeOS.type(); },
    version() { return nodeOS.version ? nodeOS.version() : nodeOS.release(); },
    release() { return nodeOS.release(); },
    machine() { return nodeOS.machine ? nodeOS.machine() : process.arch; },
    execPath() { return process.execPath; },
    pid() { return process.pid; },
    ppid() { return process.ppid; },
    cwd() { return process.cwd(); },
    chdir(dir) { process.chdir(dir); },
    tmpdir() { return nodeOS.tmpdir(); },
    homedir() { return nodeOS.homedir(); },
    hostname() { return nodeOS.hostname(); },
    userInfo(uid) { return nodeOS.userInfo(); },
    groupInfo(gid) { return null; },
    networkInterfaces() {
      const ifaces = nodeOS.networkInterfaces();
      const result = [];
      for (const [name, addrs] of Object.entries(ifaces)) {
        if (addrs) {
          for (const addr of addrs) {
            result.push({ name, ...addr });
          }
        }
      }
      return result;
    },
    kill(pid, sig) { process.kill(pid, sig); },
    isLittleEndian: nodeOS.endianness() === 'LE',
    availableParallelism() { return nodeOS.availableParallelism ? nodeOS.availableParallelism() : nodeOS.cpus().length; },
    cpuUsage() { return process.cpuUsage(); },
    threadCpuUsage() { return process.cpuUsage(); },
    resourceUsage() { return process.resourceUsage ? process.resourceUsage() : {}; },
    memoryUsage() { return process.memoryUsage(); },
    freemem() { return nodeOS.freemem(); },
    totalmem() { return nodeOS.totalmem(); },
    availableMemory() { return typeof nodeOS.freemem === 'function' ? nodeOS.freemem() : 0; },
    constrainedMemory() { return 0; },
    uptime() { return nodeOS.uptime(); },
    loadavg() { return nodeOS.loadavg(); },
    cpus() { return nodeOS.cpus(); },
    getProcessTitle() { return process.title; },
    setProcessTitle(t) { process.title = t; },
    getPriority(pid) { return nodeOS.getPriority ? nodeOS.getPriority(pid) : 0; },
    setPriority(pid, p) { if (nodeOS.setPriority) nodeOS.setPriority(pid, p); },
    getEnvKeys() { return Object.keys(process.env); },
    getEnv(n) { return process.env[n]; },
    hasEnv(n) { return Object.prototype.hasOwnProperty.call(process.env, n); },
    setEnv(n, v) { process.env[n] = v; },
    unsetEnv(n) { delete process.env[n]; }
  };

  try {
    const mainPath = require.resolve('bare-os');
    const bindingPath = path.join(path.dirname(mainPath), 'binding.js');
    require.cache[bindingPath] = {
      id: bindingPath,
      filename: bindingPath,
      loaded: true,
      exports: mockBinding
    };
  } catch (err) {
    // node_modules might not be resolved if uninstalled
  }
}

const os = require('bare-os');

/**
 * Returns the operating system endianness ('LE' or 'BE').
 * @returns {'LE' | 'BE'}
 */
export function getEndianness() {
  return os.endianness();
}

/**
 * Checks whether the platform is little-endian ('LE').
 * @returns {boolean}
 */
export function isLittleEndian() {
  return os.endianness() === 'LE';
}

/**
 * Checks whether the platform is big-endian ('BE').
 * @returns {boolean}
 */
export function isBigEndian() {
  return os.endianness() === 'BE';
}

export default os;
package-lock.json
{
  "name": "sample-bare-os-endianness",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-os-endianness",
      "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-endianness",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "bare-os": "3.9.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify bare-os.endianness in pkg:npm/bare-os@3.9.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-os@3.9.3"
  ],
  "symbols": [
    "bare-os.endianness"
  ],
  "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/strict';
import { createRequire } from 'node:module';
import fs from 'node:fs';
import path from 'node:path';
import os, { getEndianness, isLittleEndian, isBigEndian } 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['./constants'].types, './lib/constants.d.ts', 'Constants export must have constants.d.ts types');
assert.equal(pkg.exports['./constants'].default, './lib/constants.js', 'Constants export must have constants.js default');
assert.equal(pkg.exports['./errors'].types, './lib/errors.d.ts', 'Errors export must have errors.d.ts types');
assert.equal(pkg.exports['./errors'].default, './lib/errors.js', 'Errors export must have errors.js default');
assert.equal(pkg.exports['./package'].default || pkg.exports['./package'], './package.json', 'Package export must point to package.json');

const pkgDir = path.dirname(pkgPath);
const indexDts = fs.readFileSync(path.join(pkgDir, 'index.d.ts'), 'utf8');
const indexJs = fs.readFileSync(path.join(pkgDir, 'index.js'), 'utf8');
const bindingC = fs.readFileSync(path.join(pkgDir, 'binding.c'), 'utf8');

// 2. Verify endianness symbol declarations in TypeScript definition
assert.ok(
  indexDts.includes("export function endianness(): 'LE' | 'BE'"),
  'index.d.ts must declare export function endianness(): "LE" | "BE"'
);

// 3. Verify endianness implementation in JavaScript module
assert.ok(indexJs.includes('exports.endianness = function endianness()'), 'index.js must export endianness function');
assert.ok(indexJs.includes("return binding.isLittleEndian ? 'LE' : 'BE'"), 'endianness function must evaluate binding.isLittleEndian');

// 4. Verify endianness invocation behavior on bare-os
assert.equal(typeof os.endianness, 'function', 'bare-os must export endianness function');
const endiannessVal = os.endianness();
assert.ok(endiannessVal === 'LE' || endiannessVal === 'BE', 'bare-os.endianness() must return either "LE" or "BE"');

// Helper function tests
assert.equal(getEndianness(), endiannessVal, 'getEndianness() must match os.endianness()');
assert.equal(isLittleEndian(), endiannessVal === 'LE', 'isLittleEndian() must return true when endianness is LE');
assert.equal(isBigEndian(), endiannessVal === 'BE', 'isBigEndian() must return true when endianness is BE');

// 5. Verify endianness returns expected values for both little-endian and big-endian modes
const bindingFilePath = path.join(pkgDir, 'binding.js');

// Test little-endian mode
require.cache[bindingFilePath].exports.isLittleEndian = true;
assert.equal(os.endianness(), 'LE', 'bare-os.endianness() must return "LE" when binding.isLittleEndian is true');
assert.equal(isLittleEndian(), true, 'isLittleEndian() must return true when binding.isLittleEndian is true');
assert.equal(isBigEndian(), false, 'isBigEndian() must return false when binding.isLittleEndian is true');

// Test big-endian mode
require.cache[bindingFilePath].exports.isLittleEndian = false;
assert.equal(os.endianness(), 'BE', 'bare-os.endianness() must return "BE" when binding.isLittleEndian is false');
assert.equal(isLittleEndian(), false, 'isLittleEndian() must return false when binding.isLittleEndian is false');
assert.equal(isBigEndian(), true, 'isBigEndian() must return true when binding.isLittleEndian is false');

// Restore original value
require.cache[bindingFilePath].exports.isLittleEndian = (endiannessVal === 'LE');

// 6. Verify deterministic and idempotent execution across multiple calls
const firstCall = os.endianness();
const secondCall = os.endianness();
const thirdCall = os.endianness();
assert.equal(firstCall, secondCall, 'repeated calls to endianness must return identical values');
assert.equal(secondCall, thirdCall, 'repeated calls to endianness must remain stable');

// 7. Verify C binding implementation and symbol registration
assert.ok(bindingC.includes('byte_order.u16 == 1'), 'binding.c must evaluate byte order using 16-bit union comparison');
assert.ok(bindingC.includes('"isLittleEndian"'), 'binding.c must set isLittleEndian property on exports');

console.log('Contract verification finished successfully.');

原始种子者

匿名