CodeSampleX

示例

require-asset 1.2.2: require-asset

已验证示例 — npm require-asset 1.2.2: require-asset. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: package manifest specifies exports, Apache-2.0 license…

sha256:3c0eb33df1bd80671e4f284d2f8ca472ab6118f219fc87897852d8ceb5548737

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

契约

  1. package manifest specifies exports, Apache-2.0 license, and bare-module-resolve dependency
  2. exports asset resolution function accepting specifier and parent module path string or URL instance
  3. resolves relative asset paths using filesystem path string or URL instance as parentURL
  4. reads package metadata and evaluates resolution conditions for assets
  5. throws ASSET_NOT_FOUND error with specifier, referrer, and candidates properties when asset is missing

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs
  • test/fixtures/data.txt
  • test/fixtures/info.json

下载源代码构件 (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/require-asset@1.2.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/require-asset@1.2.2
Demonstrate these symbols/APIs:
  - require-asset

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:f2750ecbba990cb46153ab52326518f045a8dad0ae0af2736135dfbca76ee884","contract":["package manifest specifies exports, Apache-2.0 license, and bare-module-resolve dependency","exports asset resolution function accepting specifier and parent module path string or URL instance","resolves relative asset paths using filesystem path string or URL instance as parentURL","reads package metadata and evaluates resolution conditions for assets","throws ASSET_NOT_FOUND error with specifier, referrer, and candidates properties when asset is missing"],"goal":"verify pkg:npm/require-asset@1.2.2","kind":"HOW","packages":["pkg:npm/require-asset@1.2.2"],"schemaVersion":1,"symbols":["require-asset"]},"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/require-asset@1.2.2"],"schemaVersion":1,"subject":"pkg:npm/require-asset@1.2.2","symbols":["require-asset"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-require-asset",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-require-asset",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "require-asset": "1.2.2"
      }
    },
    "node_modules/bare-module-resolve": {
      "version": "1.12.4",
      "resolved": "https://registry.npmjs.org/bare-module-resolve/-/bare-module-resolve-1.12.4.tgz",
      "integrity": "sha512-xcfgg2u7HqgJiBmah71O9vvdFAgHCvkqC/WSC2O7Bbgosoc1eC/BWe/6IDJ4OsfKlkxuvC/TDWXC+oH5yeW8mA==",
      "license": "Apache-2.0",
      "dependencies": {
        "bare-semver": "^1.0.0"
      },
      "peerDependencies": {
        "bare-url": "*"
      },
      "peerDependenciesMeta": {
        "bare-url": {
          "optional": true
        }
      }
    },
    "node_modules/bare-semver": {
      "version": "1.1.0",
      "resolved": "https://registry.npmjs.org/bare-semver/-/bare-semver-1.1.0.tgz",
      "integrity": "sha512-1Hw5qJ7hXdVt3uPUqjeFTuxyvBUJauvz5A1I2jk8gzjZMHp04n//6nV9MDbG9CMw78JHY2lGV0w6s//LrASm2w==",
      "license": "Apache-2.0"
    },
    "node_modules/require-asset": {
      "version": "1.2.2",
      "resolved": "https://registry.npmjs.org/require-asset/-/require-asset-1.2.2.tgz",
      "integrity": "sha512-uc8nWKhqAxVD9Z4rST2b4yaemrC9Xb5vA1wlCz5AiCYnBYdwGRWNqFjneA5zRILzlo1MNrB63ry9+pc8wNR26w==",
      "license": "Apache-2.0",
      "dependencies": {
        "bare-module-resolve": "^1.6.2"
      },
      "engines": {
        "bare": ">=1.10.0",
        "node": ">=18"
      }
    }
  }
}
package.json
{
  "name": "sample-require-asset",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "require-asset": "1.2.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/require-asset@1.2.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/require-asset@1.2.2"
  ],
  "symbols": [
    "require-asset"
  ]
}
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 { fileURLToPath, pathToFileURL } from 'node:url';

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

// 1. Verify package manifest and metadata
const pkgPath = require.resolve('require-asset/package');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));

assert.equal(pkg.name, 'require-asset', 'Package name must be require-asset');
assert.equal(pkg.version, '1.2.2', 'Package version must be 1.2.2');
assert.equal(pkg.license, 'Apache-2.0', 'Package license must be Apache-2.0');
assert.ok(pkg.exports, 'Package must define exports');
assert.equal(pkg.exports['./package'], './package.json', 'Package export must point to ./package.json');
assert.equal(pkg.exports['.'].node, './lib/node.js', 'Root node export must point to ./lib/node.js');
assert.equal(pkg.exports['.'].bare, './lib/bare.js', 'Root bare export must point to ./lib/bare.js');
assert.equal(pkg.exports['.'].default, './lib/default.js', 'Root default export must point to ./lib/default.js');
assert.ok(pkg.dependencies['bare-module-resolve'], 'Package must depend on bare-module-resolve');

// 2. Verify imported module function
const asset = require('require-asset');
assert.equal(typeof asset, 'function', 'require-asset must export a function');

// 3. Verify asset resolution with filesystem path string and URL object
const currentFilePath = fileURLToPath(import.meta.url);
const fixtureTxtPath = path.resolve(path.dirname(currentFilePath), 'fixtures', 'data.txt');
const fixtureJsonPath = path.resolve(path.dirname(currentFilePath), 'fixtures', 'info.json');

// Resolving with filesystem path string (as in CommonJS __filename or fileURLToPath)
const resolvedTxtFromPath = asset('./fixtures/data.txt', currentFilePath);
assert.equal(resolvedTxtFromPath, fixtureTxtPath, 'Resolved text path must match expected fixture path');
assert.equal(fs.readFileSync(resolvedTxtFromPath, 'utf8').trim(), 'hello require-asset');

const resolvedJsonFromPath = asset('./fixtures/info.json', currentFilePath);
assert.equal(resolvedJsonFromPath, fixtureJsonPath, 'Resolved JSON path must match expected fixture path');
const jsonContent = JSON.parse(fs.readFileSync(resolvedJsonFromPath, 'utf8'));
assert.equal(jsonContent.type, 'sample');
assert.equal(jsonContent.value, 42);

// Resolving with URL object (new URL(import.meta.url) or pathToFileURL(filePath))
const currentUrlObj = new URL(import.meta.url);
const resolvedTxtFromUrlObj = asset('./fixtures/data.txt', currentUrlObj);
assert.equal(resolvedTxtFromUrlObj, fixtureTxtPath, 'Resolved path using URL object must match');

const currentPathToFileUrl = pathToFileURL(currentFilePath);
const resolvedJsonFromPathUrl = asset('./fixtures/info.json', currentPathToFileUrl);
assert.equal(resolvedJsonFromPathUrl, fixtureJsonPath, 'Resolved path using pathToFileURL must match');

// 4. Verify ASSET_NOT_FOUND error when asset cannot be resolved
let threw = false;
try {
  asset('./fixtures/nonexistent_asset.bin', currentFilePath);
} catch (err) {
  threw = true;
  assert.equal(err.code, 'ASSET_NOT_FOUND', 'Error code must be ASSET_NOT_FOUND');
  assert.equal(err.specifier, './fixtures/nonexistent_asset.bin', 'Error must contain specifier');
  assert.ok(err.referrer instanceof URL || typeof err.referrer === 'object', 'Error must contain referrer URL');
  assert.ok(Array.isArray(err.candidates), 'Error must contain candidates array');
  assert.ok(err.message.includes("Cannot find asset './fixtures/nonexistent_asset.bin'"), 'Error message must describe missing asset');
}
assert.equal(threw, true, 'Calling asset with non-existent file must throw');

console.log('Contract tests passed successfully.');
test/fixtures/data.txt
hello require-asset
test/fixtures/info.json
{"type":"sample","value":42}

原始种子者

匿名