CodeSampleX

示例

bare-env 3.0.1: env

已验证示例 — npm bare-env 3.0.1: env. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: package manifest specifies Apache-2.0 license, bare-os dependency…

sha256:33c459bc715c6adbe0e652bb7ab6c9db46766d66f8c352ed06b99595d8f4a3c6

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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/bare-env@3.0.1
包
符号
  • env
环境
node 22.23.2
创建时间
2026-08-27T13:16:32Z

契约

  1. package manifest specifies Apache-2.0 license, bare-os dependency, and exports for root and package subpaths
  2. declares Env index signature interface and env constant in TypeScript definitions (index.d.ts)
  3. exports a Proxy object delegating environment variable access to bare-os
  4. supports getting, setting, and checking presence of environment variables via property access and the in operator
  5. coerces number and boolean values to strings when setting environment variables
  6. throws an Error when attempting to set non-primitive values such as objects, arrays, functions, or symbols
  7. delegates ownKeys and getOwnPropertyDescriptor traps allowing Object.keys enumeration and descriptor lookup
  8. unsets environment variables on deleteProperty while the trap returns falsish causing Reflect.deleteProperty to evaluate to false

文件

  • 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 pkg:npm/bare-env@3.0.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/bare-env@3.0.1
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:807e74d79db72a2cb5c9595d3a8c03a5fc75a9eae8701bdd9b9fe27a3ecc5cc4","constraints":{"executionContext":"node"},"contract":["package manifest specifies Apache-2.0 license, bare-os dependency, and exports for root and package subpaths","declares Env index signature interface and env constant in TypeScript definitions (index.d.ts)","exports a Proxy object delegating environment variable access to bare-os","supports getting, setting, and checking presence of environment variables via property access and the in operator","coerces number and boolean values to strings when setting environment variables","throws an Error when attempting to set non-primitive values such as objects, arrays, functions, or symbols","delegates ownKeys and getOwnPropertyDescriptor traps allowing Object.keys enumeration and descriptor lookup","unsets environment variables on deleteProperty while the trap returns falsish causing Reflect.deleteProperty to evaluate to false"],"goal":"verify pkg:npm/bare-env@3.0.1","kind":"HOW","packages":["pkg:npm/bare-env@3.0.1"],"schemaVersion":1,"symbols":["env"]},"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-env@3.0.1"],"schemaVersion":1,"subject":"pkg:npm/bare-env@3.0.1","symbols":["env"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-bare-env",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-env",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "bare-env": "3.0.1"
      }
    },
    "node_modules/bare-env": {
      "version": "3.0.1",
      "resolved": "https://registry.npmjs.org/bare-env/-/bare-env-3.0.1.tgz",
      "integrity": "sha512-BdoLvnzaWR0YyyJreEx4zG2Od0AC/s9JSZ+EXyMZKunpn8zEgR/dGv37hdFUaY1bjDkcRKvcKkFyJIwlJ0CaYA==",
      "license": "Apache-2.0",
      "dependencies": {
        "bare-os": "^3.0.1"
      }
    },
    "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-env",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "bare-env": "3.0.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/bare-env@3.0.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-env@3.0.1"
  ],
  "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';

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

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

assert.equal(pkg.name, 'bare-env', 'Package name must be bare-env');
assert.equal(pkg.version, '3.0.1', 'Package version must be 3.0.1');
assert.equal(pkg.license, 'Apache-2.0', 'Package license must be Apache-2.0');
assert.deepEqual(pkg.dependencies, { 'bare-os': '^3.0.1' }, 'Package must depend on bare-os ^3.0.1');

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

assert.deepEqual(
  pkg.files,
  ['index.js', 'index.d.ts'],
  'Package files array must contain expected source files'
);

const pkgDir = path.dirname(pkgPath);

// 2. Verify TypeScript declarations (index.d.ts)
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('interface Env'), 'index.d.ts must declare interface Env');
assert.ok(indexDts.includes('[key: string]: string | undefined'), 'index.d.ts must declare string index signature');
assert.ok(indexDts.includes('declare const env: Env'), 'index.d.ts must declare const env');
assert.ok(indexDts.includes('export = env'), 'index.d.ts must export env');

// 3. Verify JavaScript implementation and Proxy traps
const indexJsPath = path.join(pkgDir, 'index.js');
assert.ok(fs.existsSync(indexJsPath), 'index.js must exist');
const indexJsCode = fs.readFileSync(indexJsPath, 'utf8');

assert.ok(indexJsCode.includes("require('bare-os')"), 'index.js must require bare-os');
assert.ok(indexJsCode.includes('new Proxy(Object.create(null)'), 'index.js must instantiate Proxy over null prototype');

// Test Proxy behaviour in isolated environment with bare-os mock
const mockStore = new Map();
const mockBareOs = {
  getEnvKeys: () => Array.from(mockStore.keys()),
  getEnv: (name) => mockStore.get(name),
  hasEnv: (name) => mockStore.has(name),
  setEnv: (name, val) => { mockStore.set(name, val); },
  unsetEnv: (name) => { mockStore.delete(name); }
};

const cjsModule = { exports: {} };
const customRequire = (id) => {
  if (id === 'bare-os') return mockBareOs;
  return require(id);
};
const fn = new Function('require', 'module', 'exports', indexJsCode);
fn(customRequire, cjsModule, cjsModule.exports);

const env = cjsModule.exports;

// 4. Test setting, getting, and checking existence of environment variables
env.PORT = '8080';
assert.equal(env.PORT, '8080', 'Getting set property must return value');
assert.equal(mockStore.get('PORT'), '8080', 'Setting property must store value in bare-os');
assert.equal('PORT' in env, true, 'in operator must return true for set variable');
assert.equal('NON_EXISTENT_VAR' in env, false, 'in operator must return false for missing variable');

// 5. Test type coercion for number and boolean values
env.TIMEOUT = 5000;
assert.equal(env.TIMEOUT, '5000', 'Number must be coerced to string');
assert.equal(mockStore.get('TIMEOUT'), '5000', 'Stored value must be string');

env.FEATURE_FLAG = true;
assert.equal(env.FEATURE_FLAG, 'true', 'Boolean true must be coerced to string');

env.DEBUG_MODE = false;
assert.equal(env.DEBUG_MODE, 'false', 'Boolean false must be coerced to string');

// 6. Test rejection of non-primitive types
assert.throws(
  () => { env.INVALID_OBJ = { key: 'value' }; },
  /Environment variable must be of type string, number, or boolean/,
  'Setting object must throw validation error'
);

assert.throws(
  () => { env.INVALID_ARR = [1, 2, 3]; },
  /Environment variable must be of type string, number, or boolean/,
  'Setting array must throw validation error'
);

assert.throws(
  () => { env.INVALID_FN = () => {}; },
  /Environment variable must be of type string, number, or boolean/,
  'Setting function must throw validation error'
);

assert.throws(
  () => { env.INVALID_SYM = Symbol('env'); },
  /Environment variable must be of type string, number, or boolean/,
  'Setting symbol must throw validation error'
);

// 7. Test ownKeys trap and Object.keys enumeration
const envKeys = Object.keys(env);
assert.deepEqual(
  envKeys.sort(),
  ['DEBUG_MODE', 'FEATURE_FLAG', 'PORT', 'TIMEOUT'].sort(),
  'Object.keys must enumerate all active environment variables'
);

// 8. Test getOwnPropertyDescriptor trap
const descriptor = Object.getOwnPropertyDescriptor(env, 'PORT');
assert.deepEqual(descriptor, {
  value: '8080',
  enumerable: true,
  configurable: true,
  writable: true
}, 'getOwnPropertyDescriptor must return full enumerable, configurable, writable descriptor');

// 9. Test deleteProperty trap
// Strict mode delete throws because deleteProperty trap in bare-env does not return true
assert.throws(
  () => { delete env.PORT; },
  TypeError,
  'Strict mode delete must throw TypeError due to missing explicit return in deleteProperty trap'
);
// Verify that despite the falsish return, unsetEnv was executed
assert.equal(mockStore.has('PORT'), false, 'Variable must be deleted from backing store');
assert.equal(env.PORT, undefined, 'Deleted variable must return undefined');
assert.equal('PORT' in env, false, 'Deleted variable must not be in env');

// Reflect.deleteProperty returns false without throwing
const deletedTimeout = Reflect.deleteProperty(env, 'TIMEOUT');
assert.equal(deletedTimeout, false, 'Reflect.deleteProperty returns false for bare-env proxy');
assert.equal(mockStore.has('TIMEOUT'), false, 'Reflect.deleteProperty must unset variable in backing store');

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

原始种子者

匿名