CodeSampleX

示例

npm-bundled 4.0.0: sync

已验证示例 — npm npm-bundled 4.0.0: sync. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: assert bundled.sync returns direct bundleDependencies and hoisted…

sha256:972a3843907c0948c7b9cb2cad9e91dddf2927401ca36869efd34e2786badb6a

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

案例

HOW
目标
verify npm-bundled.sync in pkg:npm/npm-bundled@4.0.0
包
符号
  • npm-bundled.sync
创建时间
2026-10-18T03:06:27Z

契约

  1. assert bundled.sync returns direct bundleDependencies and hoisted transitive dependencies while excluding unbundled and nested dependencies
  2. assert bundled.sync resolves scoped packages (@scope/pkg) in top-level node_modules
  3. assert bundled.sync bundles all dependencies and optionalDependencies when bundleDependencies is true and populates packageJsonCache
  4. assert bundled.sync returns an empty array when no bundleDependencies are configured or package.json is missing

文件

  • 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 npm-bundled.sync in pkg:npm/npm-bundled@4.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/npm-bundled@4.0.0
Demonstrate these symbols/APIs:
  - npm-bundled.sync

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:c3ecdb49bc6a2f582810d359093592a7e99182d7cbcc57bfd0e82237fe2dca14","contract":["assert bundled.sync returns direct bundleDependencies and hoisted transitive dependencies while excluding unbundled and nested dependencies","assert bundled.sync resolves scoped packages (@scope/pkg) in top-level node_modules","assert bundled.sync bundles all dependencies and optionalDependencies when bundleDependencies is true and populates packageJsonCache","assert bundled.sync returns an empty array when no bundleDependencies are configured or package.json is missing"],"goal":"verify npm-bundled.sync in pkg:npm/npm-bundled@4.0.0","kind":"HOW","packages":["pkg:npm/npm-bundled@4.0.0"],"schemaVersion":1,"symbols":["npm-bundled.sync"]},"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/npm-bundled@4.0.0"],"schemaVersion":1,"subject":"pkg:npm/npm-bundled@4.0.0","symbols":["npm-bundled.sync"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const bundled = require('npm-bundled');

/**
 * Synchronously lists top-level node_modules packages that are bundledDependencies
 * or hoisted transitive dependencies of bundledDependencies.
 *
 * @param {string} pkgPath - Directory path of the target package.
 * @param {Map<string, object>} [packageJsonCache] - Optional cache map for parsed package.json objects.
 * @returns {string[]} List of bundled top-level dependency names.
 */
function listBundledSync(pkgPath, packageJsonCache) {
  const options = { path: pkgPath };
  if (packageJsonCache) {
    options.packageJsonCache = packageJsonCache;
  }
  return bundled.sync(options);
}

module.exports = {
  listBundledSync,
  bundled,
};
package-lock.json
{
  "name": "sample-npm-bundled-sync",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-npm-bundled-sync",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "npm-bundled": "4.0.0"
      }
    },
    "node_modules/npm-bundled": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/npm-bundled/-/npm-bundled-4.0.0.tgz",
      "integrity": "sha512-IxaQZDMsqfQ2Lz37VvyyEtKLe8FsRZuysmedy/N06TU1RyVppYKXrO4xIhR0F+7ubIBox6Q7nir6fQI3ej39iA==",
      "license": "ISC",
      "dependencies": {
        "npm-normalize-package-bin": "^4.0.0"
      },
      "engines": {
        "node": "^18.17.0 || >=20.5.0"
      }
    },
    "node_modules/npm-normalize-package-bin": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-4.0.0.tgz",
      "integrity": "sha512-TZKxPvItzai9kN9H/TkmCtx/ZN/hvr3vUycjlfmH0ootY9yFBzNOpiXAdIn1Iteqsvk4lQn6B5PTrt+n6h8k/w==",
      "license": "ISC",
      "engines": {
        "node": "^18.17.0 || >=20.5.0"
      }
    }
  }
}
package.json
{
  "name": "sample-npm-bundled-sync",
  "version": "1.0.0",
  "description": "Clean-room sample verifying npm-bundled.sync functionality",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "license": "MIT-0",
  "dependencies": {
    "npm-bundled": "4.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify npm-bundled.sync in pkg:npm/npm-bundled@4.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/npm-bundled@4.0.0"
  ],
  "symbols": [
    "npm-bundled.sync"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import bundled from 'npm-bundled';
import { listBundledSync } from '../index.js';

function writePkg(dir, pkgJson) {
  fs.mkdirSync(dir, { recursive: true });
  fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(pkgJson, null, 2));
}

const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'npm-bundled-test-'));

try {
  // 1. Direct bundleDependencies & hoisted transitive dependencies
  const proj1 = path.join(tmpRoot, 'proj1');
  writePkg(proj1, {
    name: 'root-pkg',
    version: '1.0.0',
    dependencies: {
      'bundled-a': '1.0.0',
      'unbundled-b': '1.0.0',
    },
    bundleDependencies: ['bundled-a'],
  });

  // bundled-a depends on hoisted-trans-c and nested-trans-d, plus optional hoisted-opt-e
  writePkg(path.join(proj1, 'node_modules', 'bundled-a'), {
    name: 'bundled-a',
    version: '1.0.0',
    dependencies: {
      'hoisted-trans-c': '1.0.0',
      'nested-trans-d': '1.0.0',
    },
    optionalDependencies: {
      'hoisted-opt-e': '1.0.0',
    },
  });

  // nested-trans-d is inside bundled-a/node_modules (should NOT appear in top-level list)
  writePkg(path.join(proj1, 'node_modules', 'bundled-a', 'node_modules', 'nested-trans-d'), {
    name: 'nested-trans-d',
    version: '1.0.0',
  });

  // hoisted-trans-c and hoisted-opt-e are hoisted to top-level node_modules
  writePkg(path.join(proj1, 'node_modules', 'hoisted-trans-c'), {
    name: 'hoisted-trans-c',
    version: '1.0.0',
  });
  writePkg(path.join(proj1, 'node_modules', 'hoisted-opt-e'), {
    name: 'hoisted-opt-e',
    version: '1.0.0',
  });

  // unbundled-b is in top-level node_modules but not bundled
  writePkg(path.join(proj1, 'node_modules', 'unbundled-b'), {
    name: 'unbundled-b',
    version: '1.0.0',
  });

  const list1 = listBundledSync(proj1);
  assert(Array.isArray(list1), 'bundled.sync must return an array');
  assert.deepStrictEqual(
    list1.slice().sort(),
    ['bundled-a', 'hoisted-opt-e', 'hoisted-trans-c'],
    'bundled.sync must include direct bundled deps and hoisted transitive deps, excluding unbundled and nested deps'
  );

  // 2. Scoped packages (@scope/pkg) as bundled and transitive dependencies
  const proj2 = path.join(tmpRoot, 'proj2');
  writePkg(proj2, {
    name: 'scoped-root',
    version: '1.0.0',
    bundledDependencies: ['@scope/alpha'],
  });
  writePkg(path.join(proj2, 'node_modules', '@scope', 'alpha'), {
    name: '@scope/alpha',
    version: '1.0.0',
    dependencies: {
      '@scope/beta': '1.0.0',
    },
  });
  writePkg(path.join(proj2, 'node_modules', '@scope', 'beta'), {
    name: '@scope/beta',
    version: '1.0.0',
  });

  const list2 = bundled.sync({ path: proj2 });
  assert.deepStrictEqual(
    list2.slice().sort(),
    ['@scope/alpha', '@scope/beta'],
    'bundled.sync must resolve scoped packages (@scope/pkg) in top-level node_modules'
  );

  // 3. bundleDependencies: true bundles all dependencies and optionalDependencies, and populates packageJsonCache
  const proj3 = path.join(tmpRoot, 'proj3');
  writePkg(proj3, {
    name: 'bundle-all-root',
    version: '1.0.0',
    dependencies: { 'dep-one': '1.0.0' },
    optionalDependencies: { 'dep-two': '1.0.0' },
    bundleDependencies: true,
  });
  writePkg(path.join(proj3, 'node_modules', 'dep-one'), {
    name: 'dep-one',
    version: '1.0.0',
  });
  writePkg(path.join(proj3, 'node_modules', 'dep-two'), {
    name: 'dep-two',
    version: '1.0.0',
  });

  const cache = new Map();
  const list3 = listBundledSync(proj3, cache);
  assert.deepStrictEqual(
    list3.slice().sort(),
    ['dep-one', 'dep-two'],
    'bundled.sync must bundle all dependencies and optionalDependencies when bundleDependencies is true'
  );
  assert(
    cache.has(path.resolve(proj3, 'package.json')),
    'bundled.sync must populate packageJsonCache with parsed package.json entries'
  );

  // 4. Package with no bundledDependencies or missing package.json returns empty array
  const proj4 = path.join(tmpRoot, 'proj4');
  writePkg(proj4, {
    name: 'no-bundled-root',
    version: '1.0.0',
    dependencies: { 'dep-one': '1.0.0' },
  });
  assert.deepStrictEqual(
    bundled.sync({ path: proj4 }),
    [],
    'bundled.sync must return empty array when no bundleDependencies are configured'
  );
  assert.deepStrictEqual(
    bundled.sync({ path: path.join(tmpRoot, 'nonexistent') }),
    [],
    'bundled.sync must return empty array when package.json does not exist'
  );

  console.log('All contract assertions passed.');
} finally {
  fs.rmSync(tmpRoot, { recursive: true, force: true });
}

原始种子者

匿名