示例
source-map 0.6.1: SourceMapGenerator
已验证示例 — npm source-map 0.6.1: SourceMapGenerator. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: SourceMapGenerator constructor creates an instance…
sha256:67dc4f809fcde2a0e6bdb76e782cca73df9b62095408462ac24d37b7df940974
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 2
- 构建过它的签名密钥
- 1
声明的环境
node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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-23 |
| 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-07 |
案例
HOW- 目标
- verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1
- 符号
-
- source-map.SourceMapGenerator
- 环境
- node 22.23.2
- 创建时间
- 2026-08-23T13:40:32Z
契约
- SourceMapGenerator constructor creates an instance configured with optional file and sourceRoot
- addMapping adds line and column mappings between generated code and original source positions
- setSourceContent embeds raw source file contents directly into the source map
- toJSON produces a valid V3 source map object with version, file, sources, names, mappings, and sourcesContent
- toString serializes the source map object to a valid JSON string
- fromSourceMap static method creates a new SourceMapGenerator initialized from an existing SourceMapConsumer
文件
- PROMPT.md
- csx.json
- index.js
- package-lock.json
- package.json
- spec.json
- test/contract.cjs
源代码
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.SourceMapGenerator in pkg:npm/source-map@0.6.1
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/source-map@0.6.1
Demonstrate these symbols/APIs:
- source-map.SourceMapGenerator
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.
{"case":{"caseId":"case:sha256:75b652c08e35671e7e7ba274c632276bcf82a86e0b5ca076cbc11446b2bbbf95","constraints":{"executionContext":"node"},"contract":["SourceMapGenerator constructor creates an instance configured with optional file and sourceRoot","addMapping adds line and column mappings between generated code and original source positions","setSourceContent embeds raw source file contents directly into the source map","toJSON produces a valid V3 source map object with version, file, sources, names, mappings, and sourcesContent","toString serializes the source map object to a valid JSON string","fromSourceMap static method creates a new SourceMapGenerator initialized from an existing SourceMapConsumer"],"goal":"verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1","kind":"HOW","packages":["pkg:npm/source-map@0.6.1"],"schemaVersion":1,"symbols":["source-map.SourceMapGenerator"]},"contractCommand":["node","test/contract.cjs"],"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@0.6.1"],"schemaVersion":1,"subject":"pkg:npm/source-map@0.6.1","symbols":["source-map.SourceMapGenerator"],"verifierAdapter":"node-typescript@1"}
'use strict';
const { SourceMapGenerator, SourceMapConsumer } = require('source-map');
/**
* Creates a source map for generated code given mappings and source contents.
*
* @param {Object} options
* @param {string} [options.file] - The generated file name.
* @param {string} [options.sourceRoot] - The root URL/path for sources.
* @param {Array<Object>} mappings - List of mapping objects.
* @param {Object<string, string>} [sourcesContent] - Map of source filename to source text.
* @returns {SourceMapGenerator} The populated SourceMapGenerator instance.
*/
function createSourceMap(options, mappings, sourcesContent) {
const generator = new SourceMapGenerator(options);
if (Array.isArray(mappings)) {
for (const mapping of mappings) {
generator.addMapping(mapping);
}
}
if (sourcesContent && typeof sourcesContent === 'object') {
for (const [sourceFile, content] of Object.entries(sourcesContent)) {
generator.setSourceContent(sourceFile, content);
}
}
return generator;
}
/**
* Clones a source map using SourceMapGenerator.fromSourceMap.
*
* @param {SourceMapGenerator|Object} mapOrConsumer
* @returns {SourceMapGenerator}
*/
function cloneSourceMap(mapOrConsumer) {
if (typeof mapOrConsumer.toJSON === 'function') {
const consumer = new SourceMapConsumer(mapOrConsumer.toJSON());
return SourceMapGenerator.fromSourceMap(consumer);
}
return SourceMapGenerator.fromSourceMap(mapOrConsumer);
}
module.exports = {
SourceMapGenerator,
createSourceMap,
cloneSourceMap
};
{
"name": "sample-source-map-generator",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-source-map-generator",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"source-map": "0.6.1"
}
},
"node_modules/source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"license": "BSD-3-Clause",
"engines": {
"node": ">=0.10.0"
}
}
}
}
{
"name": "sample-source-map-generator",
"version": "1.0.0",
"private": true,
"description": "verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1",
"main": "index.js",
"scripts": {
"test": "node test/contract.cjs"
},
"dependencies": {
"source-map": "0.6.1"
},
"license": "MIT-0"
}
{
"schemaVersion": 1,
"goal": "verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1",
"kind": "HOW",
"packages": [
"pkg:npm/source-map@0.6.1"
],
"symbols": [
"source-map.SourceMapGenerator"
]
}
'use strict';
const assert = require('assert');
const { SourceMapGenerator, SourceMapConsumer } = require('source-map');
const { createSourceMap, cloneSourceMap } = require('../index.js');
async function runContractTests() {
// 1. SourceMapGenerator constructor creates an instance configured with optional file and sourceRoot
const generator = new SourceMapGenerator({
file: 'bundle.js',
sourceRoot: 'http://example.com/src/'
});
assert.ok(generator instanceof SourceMapGenerator, 'Must create an instance of SourceMapGenerator');
// 2. addMapping adds line and column mappings between generated code and original source positions
generator.addMapping({
generated: { line: 1, column: 0 },
original: { line: 10, column: 4 },
source: 'foo.js',
name: 'bar'
});
generator.addMapping({
generated: { line: 2, column: 0 },
original: { line: 20, column: 8 },
source: 'baz.js'
});
// 3. setSourceContent embeds raw source file contents directly into the source map
const fooContent = 'function bar() { return true; }';
const bazContent = 'const val = 123;';
generator.setSourceContent('foo.js', fooContent);
generator.setSourceContent('baz.js', bazContent);
// 4. toJSON produces a valid V3 source map object with version, file, sources, names, mappings, and sourcesContent
const jsonMap = generator.toJSON();
assert.strictEqual(jsonMap.version, 3, 'Source map version must be 3');
assert.strictEqual(jsonMap.file, 'bundle.js', 'File name must match option');
assert.strictEqual(jsonMap.sourceRoot, 'http://example.com/src/', 'sourceRoot must match option');
assert.deepStrictEqual(jsonMap.sources, ['foo.js', 'baz.js'], 'sources list must contain added sources');
assert.deepStrictEqual(jsonMap.names, ['bar'], 'names list must contain added names');
assert.deepStrictEqual(jsonMap.sourcesContent, [fooContent, bazContent], 'sourcesContent must contain embedded contents');
assert.ok(typeof jsonMap.mappings === 'string' && jsonMap.mappings.length > 0, 'mappings string must not be empty');
// 5. toString serializes the source map object to a valid JSON string
const stringified = generator.toString();
assert.strictEqual(typeof stringified, 'string', 'toString must return a string');
const parsed = JSON.parse(stringified);
assert.deepStrictEqual(parsed, jsonMap, 'Parsed toString output must match toJSON output');
// 6. Verify mapping resolution with SourceMapConsumer
const consumer = new SourceMapConsumer(jsonMap);
const pos1 = consumer.originalPositionFor({ line: 1, column: 0 });
assert.strictEqual(pos1.line, 10);
assert.strictEqual(pos1.column, 4);
assert.strictEqual(pos1.source, 'http://example.com/src/foo.js');
assert.strictEqual(pos1.name, 'bar');
const pos2 = consumer.originalPositionFor({ line: 2, column: 0 });
assert.strictEqual(pos2.line, 20);
assert.strictEqual(pos2.column, 8);
assert.strictEqual(pos2.source, 'http://example.com/src/baz.js');
assert.strictEqual(pos2.name, null);
// 7. fromSourceMap static method creates a new SourceMapGenerator initialized from an existing consumer
const clonedGen = SourceMapGenerator.fromSourceMap(consumer);
assert.ok(clonedGen instanceof SourceMapGenerator, 'fromSourceMap must return a SourceMapGenerator instance');
const clonedJson = clonedGen.toJSON();
assert.strictEqual(clonedJson.version, 3);
assert.strictEqual(clonedJson.file, 'bundle.js');
assert.deepStrictEqual(clonedJson.sources, ['foo.js', 'baz.js']);
assert.deepStrictEqual(clonedJson.names, ['bar']);
// 8. Test helper functions from index.js
const helperMap = createSourceMap(
{ file: 'app.min.js' },
[
{
generated: { line: 1, column: 0 },
original: { line: 1, column: 0 },
source: 'app.js'
}
],
{
'app.js': 'console.log("hello");'
}
);
assert.ok(helperMap instanceof SourceMapGenerator);
const helperJson = helperMap.toJSON();
assert.strictEqual(helperJson.file, 'app.min.js');
assert.deepStrictEqual(helperJson.sources, ['app.js']);
assert.deepStrictEqual(helperJson.sourcesContent, ['console.log("hello");']);
const clonedViaHelper = cloneSourceMap(helperMap);
assert.ok(clonedViaHelper instanceof SourceMapGenerator);
assert.strictEqual(clonedViaHelper.toJSON().file, 'app.min.js');
console.log('Contract tests passed successfully.');
}
runContractTests().catch((err) => {
console.error('Contract test failed:', err);
process.exit(1);
});
原始种子者
匿名