CodeSampleX

示例

source-map-js 1.2.1: SourceMapGenerator

已验证示例 — npm source-map-js 1.2.1: SourceMapGenerator. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: assert SourceMapGenerator initializes with file…

sha256:607151d61b5428653adff162b742aa696c39f2836a3e25ce49ddb6b52d5735ea

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

案例

HOW
目标
verify source-map-js.SourceMapGenerator in pkg:npm/source-map-js@1.2.1
包
符号
  • source-map-js.SourceMapGenerator
环境
node 22.23.2
创建时间
2026-09-06T01:50:17Z

契约

  1. assert SourceMapGenerator initializes with file and sourceRoot options and produces v3 source map JSON
  2. assert SourceMapGenerator.prototype.addMapping records generated to original line and column mappings with optional symbol names
  3. assert SourceMapGenerator.prototype.setSourceContent embeds original file source contents into sourcesContent array
  4. assert SourceMapGenerator.prototype.toString serializes identical output to JSON.stringify of toJSON
  5. assert SourceMapGenerator.fromSourceMap clones mappings from an existing SourceMapConsumer
  6. assert SourceMapGenerator.prototype.applySourceMap composes multi-step source transforms into a unified map
  7. assert SourceMapGenerator.prototype.addMapping rejects invalid mappings with generated line less than 1

文件

  • 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 source-map-js.SourceMapGenerator in pkg:npm/source-map-js@1.2.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/source-map-js@1.2.1
Demonstrate these symbols/APIs:
  - source-map-js.SourceMapGenerator
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:b7dacebd7ab71256600936453bf0e1db4b094be48b612006b035660d74f0122d","constraints":{"executionContext":"node"},"contract":["assert SourceMapGenerator initializes with file and sourceRoot options and produces v3 source map JSON","assert SourceMapGenerator.prototype.addMapping records generated to original line and column mappings with optional symbol names","assert SourceMapGenerator.prototype.setSourceContent embeds original file source contents into sourcesContent array","assert SourceMapGenerator.prototype.toString serializes identical output to JSON.stringify of toJSON","assert SourceMapGenerator.fromSourceMap clones mappings from an existing SourceMapConsumer","assert SourceMapGenerator.prototype.applySourceMap composes multi-step source transforms into a unified map","assert SourceMapGenerator.prototype.addMapping rejects invalid mappings with generated line less than 1"],"goal":"verify source-map-js.SourceMapGenerator in pkg:npm/source-map-js@1.2.1","kind":"HOW","packages":["pkg:npm/source-map-js@1.2.1"],"schemaVersion":1,"symbols":["source-map-js.SourceMapGenerator"]},"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/source-map-js@1.2.1"],"schemaVersion":1,"subject":"pkg:npm/source-map-js@1.2.1","symbols":["source-map-js.SourceMapGenerator"],"verifierAdapter":"node-typescript@1"}
index.js
const { SourceMapGenerator, SourceMapConsumer } = require('source-map-js');

/**
 * Creates a new SourceMapGenerator instance with the specified options.
 *
 * @param {Object} [options] - Configuration options for the source map (file, sourceRoot, skipValidation).
 * @returns {SourceMapGenerator} A new SourceMapGenerator instance.
 */
function createGenerator(options) {
  return new SourceMapGenerator(options);
}

/**
 * Adds a single mapping from generated position to original position.
 *
 * @param {SourceMapGenerator} generator - The generator to add mapping to.
 * @param {Object} mapping - Mapping object containing generated and optional original/source/name fields.
 */
function addMapping(generator, mapping) {
  generator.addMapping(mapping);
}

/**
 * Sets the original source code content for a source file.
 *
 * @param {SourceMapGenerator} generator - The generator instance.
 * @param {string} sourceFile - Name or URL of the source file.
 * @param {string} sourceContent - Content of the source file.
 */
function setSourceContent(generator, sourceFile, sourceContent) {
  generator.setSourceContent(sourceFile, sourceContent);
}

module.exports = {
  SourceMapGenerator,
  SourceMapConsumer,
  createGenerator,
  addMapping,
  setSourceContent,
};
package-lock.json
{
  "name": "sample-source-map-js",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-source-map-js",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "source-map-js": "1.2.1"
      }
    },
    "node_modules/source-map-js": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz",
      "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==",
      "license": "BSD-3-Clause",
      "engines": {
        "node": ">=0.10.0"
      }
    }
  }
}
package.json
{
  "name": "sample-source-map-js",
  "version": "1.0.0",
  "description": "Clean-room verification sample for source-map-js.SourceMapGenerator",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "source-map-js": "1.2.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify source-map-js.SourceMapGenerator in pkg:npm/source-map-js@1.2.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/source-map-js@1.2.1"
  ],
  "symbols": [
    "source-map-js.SourceMapGenerator"
  ],
  "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 {
  SourceMapGenerator,
  SourceMapConsumer,
  createGenerator,
  addMapping,
  setSourceContent
} from '../index.js';

// 1. assert SourceMapGenerator initializes with file and sourceRoot options and produces v3 source map JSON
{
  const gen = createGenerator({
    file: 'bundle.js',
    sourceRoot: 'https://example.com/src/'
  });
  const json = gen.toJSON();
  assert.equal(json.version, 3);
  assert.equal(json.file, 'bundle.js');
  assert.equal(json.sourceRoot, 'https://example.com/src/');
  assert.deepEqual(json.sources, []);
  assert.deepEqual(json.names, []);
}

// 2. assert SourceMapGenerator.prototype.addMapping records generated to original line and column mappings with optional symbol names
{
  const gen = createGenerator({ file: 'out.js' });
  addMapping(gen, {
    generated: { line: 1, column: 0 },
    original: { line: 10, column: 4 },
    source: 'input.js',
    name: 'myVar'
  });
  const json = gen.toJSON();
  assert.deepEqual(json.sources, ['input.js']);
  assert.deepEqual(json.names, ['myVar']);
  assert.ok(typeof json.mappings === 'string' && json.mappings.length > 0);

  const consumer = new SourceMapConsumer(json);
  const orig = consumer.originalPositionFor({ line: 1, column: 0 });
  assert.equal(orig.source, 'input.js');
  assert.equal(orig.line, 10);
  assert.equal(orig.column, 4);
  assert.equal(orig.name, 'myVar');
}

// 3. assert SourceMapGenerator.prototype.setSourceContent embeds original file source contents into sourcesContent array
{
  const gen = createGenerator({ file: 'out.js' });
  const content = 'const x = 42;\nexport default x;';
  setSourceContent(gen, 'input.js', content);
  addMapping(gen, {
    generated: { line: 1, column: 0 },
    original: { line: 1, column: 0 },
    source: 'input.js'
  });
  const json = gen.toJSON();
  assert.deepEqual(json.sourcesContent, [content]);
}

// 4. assert SourceMapGenerator.prototype.toString serializes identical output to JSON.stringify of toJSON
{
  const gen = createGenerator({ file: 'out.js' });
  addMapping(gen, {
    generated: { line: 1, column: 5 },
    original: { line: 2, column: 10 },
    source: 'src.js'
  });
  const json = gen.toJSON();
  const str = gen.toString();
  assert.equal(str, JSON.stringify(json));
}

// 5. assert SourceMapGenerator.fromSourceMap clones mappings from an existing SourceMapConsumer
{
  const gen1 = createGenerator({ file: 'initial.js' });
  addMapping(gen1, {
    generated: { line: 1, column: 0 },
    original: { line: 5, column: 2 },
    source: 'orig.js',
    name: 'sym'
  });
  const consumer = new SourceMapConsumer(gen1.toJSON());
  const gen2 = SourceMapGenerator.fromSourceMap(consumer);
  assert.deepEqual(gen2.toJSON(), gen1.toJSON());
}

// 6. assert SourceMapGenerator.prototype.applySourceMap composes multi-step source transforms into a unified map
{
  // Step 1: orig.ts -> trans.js
  const map1 = createGenerator({ file: 'trans.js' });
  addMapping(map1, {
    generated: { line: 1, column: 0 },
    original: { line: 10, column: 5 },
    source: 'orig.ts'
  });
  const consumer1 = new SourceMapConsumer(map1.toJSON());

  // Step 2: trans.js -> min.js
  const map2 = createGenerator({ file: 'min.js' });
  addMapping(map2, {
    generated: { line: 1, column: 0 },
    original: { line: 1, column: 0 },
    source: 'trans.js'
  });
  map2.applySourceMap(consumer1, 'trans.js');

  const finalJson = map2.toJSON();
  assert.deepEqual(finalJson.sources, ['orig.ts']);
  const finalConsumer = new SourceMapConsumer(finalJson);
  const pos = finalConsumer.originalPositionFor({ line: 1, column: 0 });
  assert.equal(pos.source, 'orig.ts');
  assert.equal(pos.line, 10);
  assert.equal(pos.column, 5);
}

// 7. assert SourceMapGenerator.prototype.addMapping rejects invalid mappings with generated line less than 1
{
  const gen = createGenerator();
  assert.throws(
    () => {
      addMapping(gen, { generated: { line: 0, column: 0 } });
    },
    /Invalid mapping/,
    'Must throw Error for generated line < 1'
  );
}

console.log('All source-map-js.SourceMapGenerator contract assertions passed.');

原始种子者

匿名