CodeSampleX

示例

source-map-js 1.2.1: SourceMapConsumer

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

sha256:79be3fe3f2da7597e14043e28e347d5ab28f108cd04c548d14cc731f00c0020b

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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.SourceMapConsumer in pkg:npm/source-map-js@1.2.1
包
符号
  • source-map-js.SourceMapConsumer
环境
node 22.23.2
创建时间
2026-09-06T01:35:45Z

契约

  1. assert SourceMapConsumer initializes from raw source map object and parses file and sources
  2. assert SourceMapConsumer resolves original position and symbol name for generated coordinates
  3. assert SourceMapConsumer applies GREATEST_LOWER_BOUND and LEAST_UPPER_BOUND bias for unmapped column queries
  4. assert SourceMapConsumer resolves generated position and all generated positions for original coordinate
  5. assert SourceMapConsumer retrieves embedded source content and validates source completeness
  6. assert SourceMapConsumer iterates mappings sequentially via eachMapping

文件

  • 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.SourceMapConsumer 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.SourceMapConsumer

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:e6b64c853b8effa53d4b325a434b8639a325d133003e39f614c88e22621d38b1","contract":["assert SourceMapConsumer initializes from raw source map object and parses file and sources","assert SourceMapConsumer resolves original position and symbol name for generated coordinates","assert SourceMapConsumer applies GREATEST_LOWER_BOUND and LEAST_UPPER_BOUND bias for unmapped column queries","assert SourceMapConsumer resolves generated position and all generated positions for original coordinate","assert SourceMapConsumer retrieves embedded source content and validates source completeness","assert SourceMapConsumer iterates mappings sequentially via eachMapping"],"goal":"verify source-map-js.SourceMapConsumer 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.SourceMapConsumer"]},"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.SourceMapConsumer"],"verifierAdapter":"node-typescript@1"}
index.js
const { SourceMapConsumer } = require('source-map-js');

/**
 * Creates a SourceMapConsumer instance from a raw source map object or JSON string.
 *
 * @param {Object|string} rawSourceMap - Raw source map object or JSON string.
 * @returns {SourceMapConsumer}
 */
function createConsumer(rawSourceMap) {
  return new SourceMapConsumer(rawSourceMap);
}

/**
 * Resolves original position for a generated coordinate.
 *
 * @param {SourceMapConsumer} consumer - The consumer instance.
 * @param {number} line - Generated line number (1-based).
 * @param {number} column - Generated column number (0-based).
 * @param {number} [bias] - Bias (GREATEST_LOWER_BOUND or LEAST_UPPER_BOUND).
 * @returns {{ source: string|null, line: number|null, column: number|null, name: string|null }}
 */
function resolveOriginalPosition(consumer, line, column, bias) {
  const query = { line, column };
  if (bias !== undefined) {
    query.bias = bias;
  }
  return consumer.originalPositionFor(query);
}

/**
 * Resolves generated position for an original source coordinate.
 *
 * @param {SourceMapConsumer} consumer - The consumer instance.
 * @param {string} source - Source file path.
 * @param {number} line - Original line number (1-based).
 * @param {number} column - Original column number (0-based).
 * @returns {{ line: number|null, column: number|null, lastColumn: number|null }}
 */
function resolveGeneratedPosition(consumer, source, line, column) {
  return consumer.generatedPositionFor({ source, line, column });
}

module.exports = {
  SourceMapConsumer,
  createConsumer,
  resolveOriginalPosition,
  resolveGeneratedPosition,
};
package-lock.json
{
  "name": "sample-source-map-js-consumer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-source-map-js-consumer",
      "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-consumer",
  "version": "1.0.0",
  "description": "Clean-room sample verifying source-map-js.SourceMapConsumer in pkg:npm/source-map-js@1.2.1",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "source-map-js": "1.2.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify source-map-js.SourceMapConsumer in pkg:npm/source-map-js@1.2.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/source-map-js@1.2.1"
  ],
  "symbols": [
    "source-map-js.SourceMapConsumer"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import sourceMapJs from 'source-map-js';
const { SourceMapConsumer } = sourceMapJs;
import {
  createConsumer,
  resolveOriginalPosition,
  resolveGeneratedPosition
} from '../index.js';

// Pre-computed valid v3 source map:
// Source file: "src/math.js"
// Original lines:
//   line 1: export function add(a, b) {
//   line 2:   return a + b;
//   line 3: }
// Generated file: "dist/bundle.js"
// Mappings:
//   gen line 1, col 0  -> src/math.js line 1, col 0, symbol 'add'
//   gen line 1, col 10 -> src/math.js line 2, col 2
//   gen line 1, col 20 -> src/math.js line 2, col 9
const rawMap = {
  version: 3,
  sources: ['src/math.js'],
  names: ['add'],
  mappings: 'AAAAA,UACE,UAAO',
  file: 'dist/bundle.js',
  sourceRoot: 'http://example.com/project/',
  sourcesContent: ['export function add(a, b) {\n  return a + b;\n}\n']
};

// 1. assert SourceMapConsumer initializes from raw source map object and parses file and sources
const consumer = createConsumer(rawMap);
assert.ok(consumer instanceof SourceMapConsumer, 'createConsumer returns a SourceMapConsumer instance');
assert.strictEqual(consumer.file, 'dist/bundle.js', 'consumer parses file property');
assert.strictEqual(consumer.sourceRoot, 'http://example.com/project/', 'consumer parses sourceRoot property');
assert.deepStrictEqual(
  consumer.sources,
  ['http://example.com/project/src/math.js'],
  'consumer prepends sourceRoot to sources'
);

// 2. assert SourceMapConsumer resolves original position and symbol name for generated coordinates
const pos1 = resolveOriginalPosition(consumer, 1, 0);
assert.strictEqual(pos1.source, 'http://example.com/project/src/math.js', 'resolves source path for mapping');
assert.strictEqual(pos1.line, 1, 'resolves original line');
assert.strictEqual(pos1.column, 0, 'resolves original column');
assert.strictEqual(pos1.name, 'add', 'resolves mapped identifier name');

const pos2 = resolveOriginalPosition(consumer, 1, 10);
assert.strictEqual(pos2.source, 'http://example.com/project/src/math.js', 'resolves source for second mapping');
assert.strictEqual(pos2.line, 2, 'resolves line 2');
assert.strictEqual(pos2.column, 2, 'resolves column 2');
assert.strictEqual(pos2.name, null, 'returns null name for mapping without identifier');

// 3. assert SourceMapConsumer applies GREATEST_LOWER_BOUND and LEAST_UPPER_BOUND bias for unmapped column queries
const lowerPos = resolveOriginalPosition(
  consumer,
  1,
  15,
  SourceMapConsumer.GREATEST_LOWER_BOUND
);
assert.strictEqual(lowerPos.line, 2, 'lower bound resolves previous mapping line');
assert.strictEqual(lowerPos.column, 2, 'lower bound resolves previous mapping column');

const upperPos = resolveOriginalPosition(
  consumer,
  1,
  15,
  SourceMapConsumer.LEAST_UPPER_BOUND
);
assert.strictEqual(upperPos.line, 2, 'upper bound resolves next mapping line');
assert.strictEqual(upperPos.column, 9, 'upper bound resolves next mapping column');

// 4. assert SourceMapConsumer resolves generated position and all generated positions for original coordinate
const genPos = resolveGeneratedPosition(
  consumer,
  'http://example.com/project/src/math.js',
  2,
  2
);
assert.strictEqual(genPos.line, 1, 'resolves generated line');
assert.strictEqual(genPos.column, 10, 'resolves generated column');

const allGen = consumer.allGeneratedPositionsFor({
  source: 'http://example.com/project/src/math.js',
  line: 2,
  column: 2
});
assert.ok(Array.isArray(allGen), 'allGeneratedPositionsFor returns an array');
assert.strictEqual(allGen.length, 1, 'returns one matching generated position');
assert.strictEqual(allGen[0].line, 1, 'matching generated line');
assert.strictEqual(allGen[0].column, 10, 'matching generated column');

// 5. assert SourceMapConsumer retrieves embedded source content and validates source completeness
assert.strictEqual(consumer.hasContentsOfAllSources(), true, 'hasContentsOfAllSources returns true');
assert.strictEqual(
  consumer.sourceContentFor('src/math.js'),
  'export function add(a, b) {\n  return a + b;\n}\n',
  'retrieves content by relative source path'
);
assert.strictEqual(
  consumer.sourceContentFor('http://example.com/project/src/math.js'),
  'export function add(a, b) {\n  return a + b;\n}\n',
  'retrieves content by full resolved source URL'
);
assert.strictEqual(
  consumer.sourceContentFor('nonexistent.js', true),
  null,
  'returns null for nonexistent source with returnNullOnMissing true'
);

// 6. assert SourceMapConsumer iterates mappings sequentially via eachMapping
const iterated = [];
consumer.eachMapping((m) => {
  iterated.push({
    genLine: m.generatedLine,
    genCol: m.generatedColumn,
    origLine: m.originalLine,
    origCol: m.originalColumn,
    name: m.name
  });
});
assert.strictEqual(iterated.length, 3, 'eachMapping iterates exactly 3 mappings');
assert.deepStrictEqual(
  iterated[0],
  { genLine: 1, genCol: 0, origLine: 1, origCol: 0, name: 'add' },
  'first mapping matches'
);
assert.deepStrictEqual(
  iterated[1],
  { genLine: 1, genCol: 10, origLine: 2, origCol: 2, name: null },
  'second mapping matches'
);
assert.deepStrictEqual(
  iterated[2],
  { genLine: 1, genCol: 20, origLine: 2, origCol: 9, name: null },
  'third mapping matches'
);

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

原始种子者

匿名