CodeSampleX

示例

real-require 1.0.0: realImport

已验证示例 — npm real-require 1.0.0: realImport. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: realImport is exported as a callable function from…

sha256:9658e8538cebb3037499f2b00be62dce628572c412b530b082039f8d2d3021ed

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

案例

HOW
目标
verify real-require.realImport in pkg:npm/real-require@1.0.0
包
符号
  • real-require.realImport
创建时间
2026-10-12T09:16:38Z

契约

  1. realImport is exported as a callable function from real-require
  2. realImport returns a Promise resolving to the module namespace for built-in modules
  3. realImport dynamically imports installed packages by package name
  4. realImport dynamically imports local ES modules via file URL and exposes named and default exports
  5. realImport rejects with an error when attempting to import a nonexistent module
  6. dynamicImport helper wraps realImport to load modules dynamically
  7. importNamedExport helper retrieves a specific named export from a dynamically imported module

文件

  • PROMPT.md
  • csx.json
  • fixtures/helper.mjs
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 real-require.realImport in pkg:npm/real-require@1.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/real-require@1.0.0
Demonstrate these symbols/APIs:
  - real-require.realImport

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:55094863593fc5ccd36c51f9b52421bd7d8e5d48bb013db35ea28c2a5b0e0359","contract":["realImport is exported as a callable function from real-require","realImport returns a Promise resolving to the module namespace for built-in modules","realImport dynamically imports installed packages by package name","realImport dynamically imports local ES modules via file URL and exposes named and default exports","realImport rejects with an error when attempting to import a nonexistent module","dynamicImport helper wraps realImport to load modules dynamically","importNamedExport helper retrieves a specific named export from a dynamically imported module"],"goal":"verify real-require.realImport in pkg:npm/real-require@1.0.0","kind":"HOW","packages":["pkg:npm/real-require@1.0.0"],"schemaVersion":1,"symbols":["real-require.realImport"]},"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/real-require@1.0.0"],"schemaVersion":1,"subject":"pkg:npm/real-require@1.0.0","symbols":["real-require.realImport"],"verifierAdapter":"node-typescript@1"}
fixtures/helper.mjs
export const greeting = 'hello from fixture';

export function calculateSum(a, b) {
  return a + b;
}

export default {
  type: 'fixture-module',
  active: true
};
package-lock.json
{
  "name": "sample-real-require",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-real-require",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "real-require": "1.0.0"
      }
    },
    "node_modules/real-require": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/real-require/-/real-require-1.0.0.tgz",
      "integrity": "sha512-P4nbQYQfePJxRSmY+v/KINxVucm4NF3p3s7pJveMTtom52FR4YGltUQLB8idDXwDDWW+eYrWDFbuzUnjoWHF7g==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-real-require",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for real-require@1.0.0",
  "main": "src/index.js",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "real-require": "1.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify real-require.realImport in pkg:npm/real-require@1.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/real-require@1.0.0"
  ],
  "symbols": [
    "real-require.realImport"
  ]
}
src/index.js
'use strict';

const { realImport, realRequire } = require('real-require');

/**
 * Dynamically imports a module by specifier or file URL using realImport.
 *
 * @param {string} specifier - Module specifier, path, or file URL.
 * @returns {Promise<any>} Module namespace object.
 */
async function dynamicImport(specifier) {
  return realImport(specifier);
}

/**
 * Dynamically imports a module and retrieves a specific named export.
 *
 * @param {string} specifier - Module specifier, path, or file URL.
 * @param {string} exportName - Name of the exported property to retrieve.
 * @returns {Promise<any>} The exported value.
 */
async function importNamedExport(specifier, exportName) {
  const moduleNamespace = await realImport(specifier);
  return moduleNamespace[exportName];
}

module.exports = {
  realImport,
  realRequire,
  dynamicImport,
  importNamedExport
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  realImport,
  realRequire,
  dynamicImport,
  importNamedExport
} = require('../src/index.js');

async function main() {
  // 1. realImport is exported as a callable function from real-require
  assert.equal(typeof realImport, 'function', 'realImport should be a function');
  assert.equal(typeof realRequire, 'function', 'realRequire should be a function');

  // 2. realImport returns a Promise resolving to the module namespace for built-in modules
  const pathModulePromise = realImport('node:path');
  assert(pathModulePromise instanceof Promise, 'realImport should return a Promise');
  const pathModule = await pathModulePromise;
  assert(pathModule !== null && typeof pathModule === 'object', 'Resolved module should be an object');
  assert.equal(typeof pathModule.join, 'function', 'pathModule should expose join function');
  assert.equal(pathModule.posix.join('foo', 'bar'), 'foo/bar', 'join should concatenate path segments');

  // 3. realImport dynamically imports installed packages by package name
  const selfPkg = await realImport('real-require');
  assert(selfPkg !== null && typeof selfPkg === 'object', 'Installed package should resolve to object');
  assert.equal(typeof selfPkg.realImport, 'function', 'Imported package should expose realImport');
  assert.equal(typeof selfPkg.realRequire, 'function', 'Imported package should expose realRequire');

  // 4. realImport dynamically imports local ES modules via file URL and exposes named and default exports
  const fixtureUrl = new URL('../fixtures/helper.mjs', import.meta.url).href;
  const fixture = await realImport(fixtureUrl);
  assert(fixture !== null && typeof fixture === 'object', 'Fixture module should resolve to namespace');
  assert.equal(fixture.greeting, 'hello from fixture', 'Named export greeting should match');
  assert.equal(typeof fixture.calculateSum, 'function', 'calculateSum should be a function');
  assert.equal(fixture.calculateSum(3, 4), 7, 'calculateSum(3, 4) should return 7');
  assert.equal(fixture.default.type, 'fixture-module', 'Default export type should match');
  assert.equal(fixture.default.active, true, 'Default export active should be true');

  // 5. realImport rejects with an error when attempting to import a nonexistent module
  await assert.rejects(
    async () => {
      await realImport('node:nonexistent_builtin_for_testing_purposes');
    },
    (err) => {
      assert(err instanceof Error, 'Rejected value should be an Error');
      assert(
        err.code === 'ERR_MODULE_NOT_FOUND' || err.code === 'ERR_UNKNOWN_BUILTIN_MODULE',
        `Unexpected error code: ${err.code}`
      );
      return true;
    }
  );

  // 6. dynamicImport helper wraps realImport to load modules dynamically
  const urlModule = await dynamicImport('node:url');
  assert(urlModule !== null && typeof urlModule === 'object', 'URL module should resolve');
  assert.equal(typeof urlModule.fileURLToPath, 'function', 'fileURLToPath should be a function');

  // 7. importNamedExport helper retrieves a specific named export from a dynamically imported module
  const joinFn = await importNamedExport('node:path', 'join');
  assert.equal(typeof joinFn, 'function', 'Retrieved named export join should be a function');
  const fixtureDefault = await importNamedExport(fixtureUrl, 'default');
  assert.equal(fixtureDefault.type, 'fixture-module', 'Retrieved default export property should match');

  console.log('All contract assertions passed successfully.');
}

main().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

原始种子者

匿名