示例
diff 9.0.0
已验证示例 — npm diff 9.0.0. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: diffLines returns ordered change objects with boolean added and removed…
sha256:148e5d21b2856b8df0a6d8e5edd3bb6c4636b6f644ed676f339e5817bdb08a98
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 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-11-28 |
案例
HOW- 目标
- verify pkg:npm/diff@9.0.0
- 环境
- node 22.23.2
- 创建时间
- 2026-11-28T02:19:29Z
契约
- diffLines returns ordered change objects with boolean added and removed properties and preserves line whitespace in value when ignoreWhitespace is true
- createPatch with headerOptions: FILE_HEADERS_ONLY emits file headers without Index or underline lines, OMIT_HEADERS emits hunk-only output, applyPatch reproduces the patched text and returns false on mismatched context, and reversePatch undoes the applied patch
- parsePatch parses Git extended headers (diff --git, rename from/to, new file mode) into isGit, isRename, isCreate, and newMode fields including hunkless renames, and formatPatch and reversePatch preserve Git patch metadata
- formatPatch quotes and escapes special and non-ASCII filenames with C-style escapes without emitting trailing tabs on empty headers or mutating zero-line hunks, and parsePatch unescapes them back to the original filename
- parsePatch throws on unpaired file headers and mismatched hunk line counts while tolerating non-hunk trailing metadata lines
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.js
- test/contract.js
源代码
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 pkg:npm/diff@9.0.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/diff@9.0.0
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.
{"case":{"caseId":"case:sha256:e65edc8eb224fd4fcc2f69173a22ce264a1cf9cc1e9eb2644acf54f1707983c8","constraints":{"executionContext":"node"},"contract":["diffLines returns ordered change objects with boolean added and removed properties and preserves line whitespace in value when ignoreWhitespace is true","createPatch with headerOptions: FILE_HEADERS_ONLY emits file headers without Index or underline lines, OMIT_HEADERS emits hunk-only output, applyPatch reproduces the patched text and returns false on mismatched context, and reversePatch undoes the applied patch","parsePatch parses Git extended headers (diff --git, rename from/to, new file mode) into isGit, isRename, isCreate, and newMode fields including hunkless renames, and formatPatch and reversePatch preserve Git patch metadata","formatPatch quotes and escapes special and non-ASCII filenames with C-style escapes without emitting trailing tabs on empty headers or mutating zero-line hunks, and parsePatch unescapes them back to the original filename","parsePatch throws on unpaired file headers and mismatched hunk line counts while tolerating non-hunk trailing metadata lines"],"goal":"verify pkg:npm/diff@9.0.0","kind":"HOW","packages":["pkg:npm/diff@9.0.0"],"schemaVersion":1},"contractCommand":["node","test/contract.js"],"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/diff@9.0.0"],"schemaVersion":1,"subject":"pkg:npm/diff@9.0.0","verifierAdapter":"node-typescript@1"}
{
"name": "diff-9-0-0-sample",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "diff-9-0-0-sample",
"version": "1.0.0",
"dependencies": {
"diff": "9.0.0"
}
},
"node_modules/diff": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-9.0.0.tgz",
"integrity": "sha512-svtcdpS8CgJyqAjEQIXdb3OjhFVVYjzGAPO8WGCmRbrml64SPw/jJD4GoE98aR7r25A0XcgrK3F02yw9R/vhQw==",
"license": "BSD-3-Clause",
"engines": {
"node": ">=0.3.1"
}
}
}
}
{
"name": "diff-9-0-0-sample",
"version": "1.0.0",
"private": true,
"description": "Minimal contract sample verifying diff@9.0.0",
"scripts": {
"test": "node test/contract.js"
},
"dependencies": {
"diff": "9.0.0"
}
}
{
"schemaVersion": 1,
"goal": "verify pkg:npm/diff@9.0.0",
"kind": "HOW",
"packages": [
"pkg:npm/diff@9.0.0"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
'use strict';
const {
diffLines,
createPatch,
parsePatch,
formatPatch,
applyPatch,
reversePatch,
FILE_HEADERS_ONLY,
OMIT_HEADERS,
} = require('diff');
function compareLines(oldText, newText, options) {
return diffLines(oldText, newText, options);
}
function buildPatch(fileName, oldText, newText, options) {
return createPatch(fileName, oldText, newText, '', '', options);
}
function roundTripPatch(source, patchText) {
const parsed = parsePatch(patchText);
if (parsed.length !== 1) {
throw new Error(`Expected exactly 1 patch, received ${parsed.length}`);
}
const applied = applyPatch(source, parsed[0]);
if (applied === false) {
return { applied: false, reverted: false, formatted: formatPatch(parsed[0]) };
}
const reversed = reversePatch(parsed[0]);
const reverted = applyPatch(applied, reversed);
return {
applied,
reverted,
formatted: formatPatch(parsed[0]),
};
}
module.exports = {
compareLines,
buildPatch,
roundTripPatch,
FILE_HEADERS_ONLY,
OMIT_HEADERS,
};
'use strict';
const assert = require('node:assert/strict');
const {
parsePatch,
formatPatch,
applyPatch,
reversePatch,
structuredPatch,
} = require('diff');
const {
compareLines,
buildPatch,
roundTripPatch,
FILE_HEADERS_ONLY,
OMIT_HEADERS,
} = require('../src/index.js');
// 1. diffLines change objects always set boolean added/removed and preserve whitespace in value
{
const changes = compareLines('alpha\nbeta\n', 'alpha\ngamma\n');
assert.equal(changes.length, 3);
assert.deepEqual(
changes.map((c) => ({
value: c.value,
count: c.count,
added: c.added,
removed: c.removed,
})),
[
{ value: 'alpha\n', count: 1, added: false, removed: false },
{ value: 'beta\n', count: 1, added: false, removed: true },
{ value: 'gamma\n', count: 1, added: true, removed: false },
]
);
const wsChanges = compareLines(' alpha \nbeta\n', 'alpha\nbeta\n', {
ignoreWhitespace: true,
});
assert.equal(wsChanges.length, 1);
assert.equal(wsChanges[0].added, false);
assert.equal(wsChanges[0].removed, false);
assert.equal(wsChanges[0].count, 2);
assert.equal(wsChanges[0].value, 'alpha\nbeta\n');
}
// 2. createPatch headerOptions, applyPatch round-trip, reversePatch undo, and failure on mismatch
{
const oldText = 'line1\nline2\nline3\n';
const newText = 'line1\nline2-updated\nline3\n';
const fileHeadersPatch = buildPatch('notes.txt', oldText, newText, {
headerOptions: FILE_HEADERS_ONLY,
});
assert.ok(!fileHeadersPatch.includes('Index:'));
assert.ok(!fileHeadersPatch.includes('==='));
assert.ok(fileHeadersPatch.startsWith('--- notes.txt\n+++ notes.txt\n'));
const hunkOnlyPatch = buildPatch('notes.txt', oldText, newText, {
headerOptions: OMIT_HEADERS,
});
assert.ok(hunkOnlyPatch.startsWith('@@ '));
const result = roundTripPatch(oldText, fileHeadersPatch);
assert.equal(result.applied, newText);
assert.equal(result.reverted, oldText);
assert.equal(applyPatch('unrelated\ncontent\n', fileHeadersPatch), false);
}
// 3. parsePatch and reversePatch handle Git extended headers including hunkless renames
{
const gitRenamePatch = [
'diff --git a/old-name.txt b/new-name.txt',
'similarity index 100%',
'rename from old-name.txt',
'rename to new-name.txt',
'',
].join('\n');
const parsed = parsePatch(gitRenamePatch);
assert.equal(parsed.length, 1);
assert.equal(parsed[0].isGit, true);
assert.equal(parsed[0].isRename, true);
assert.equal(parsed[0].oldFileName, 'a/old-name.txt');
assert.equal(parsed[0].newFileName, 'b/new-name.txt');
assert.equal(parsed[0].hunks.length, 0);
const formatted = formatPatch(parsed[0]);
assert.ok(formatted.includes('diff --git a/old-name.txt b/new-name.txt'));
assert.ok(formatted.includes('rename from old-name.txt'));
assert.ok(formatted.includes('rename to new-name.txt'));
assert.ok(!formatted.includes('--- '));
const reversed = reversePatch(parsed[0]);
assert.equal(reversed.isGit, true);
assert.equal(reversed.isRename, true);
assert.equal(reversed.oldFileName, 'a/new-name.txt');
assert.equal(reversed.newFileName, 'b/old-name.txt');
const gitCreatePatch = [
'diff --git a/added.txt b/added.txt',
'new file mode 100644',
'--- /dev/null',
'+++ b/added.txt',
'@@ -0,0 +1 @@',
'+hello',
'',
].join('\n');
const parsedCreate = parsePatch(gitCreatePatch);
assert.equal(parsedCreate.length, 1);
assert.equal(parsedCreate[0].isGit, true);
assert.equal(parsedCreate[0].isCreate, true);
assert.equal(parsedCreate[0].newMode, '100644');
}
// 4. C-style quoted filenames, no trailing tab on empty headers, and non-mutation of zero-line hunks
{
const specialName = 'dir/file\t"caf\u00e9".txt';
const sp = structuredPatch(specialName, specialName, '', 'created\n', '', '');
const oldLinesBefore = sp.hunks[0].oldLines;
const formatted = formatPatch(sp);
assert.equal(sp.hunks[0].oldLines, oldLinesBefore);
assert.ok(formatted.includes('--- "dir/file\\t\\"caf\\303\\251\\".txt"\n'));
assert.ok(!formatted.includes('\t\n'));
const reparsed = parsePatch(formatted);
assert.equal(reparsed.length, 1);
assert.equal(reparsed[0].oldFileName, specialName);
assert.equal(reparsed[0].newFileName, specialName);
}
// 5. parsePatch rejects unpaired file headers and mismatched hunk counts, tolerates trailing metadata
{
assert.throws(() => {
parsePatch('--- only-old.txt\n@@ -1 +1 @@\n-a\n+b\n');
}, Error);
assert.throws(() => {
parsePatch('--- a.txt\n+++ b.txt\n@@ -1,5 +1,5 @@\n-a\n+b\n');
}, Error);
const withTrailing = [
'--- a.txt',
'+++ b.txt',
'@@ -1 +1 @@',
'-old',
'+new',
'Property changes on: a.txt',
'',
].join('\n');
const parsed = parsePatch(withTrailing);
assert.equal(parsed.length, 1);
assert.equal(parsed[0].hunks.length, 1);
assert.equal(applyPatch('old\n', parsed[0]), 'new\n');
}
console.log('diff@9.0.0 contract checks passed');
原始种子者
匿名