CodeSampleX

샘플

diff 9.0.0

검증된 샘플 — npm diff 9.0.0. node 22 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: diffLines returns ordered change objects with boolean added and removed…

sha256:148e5d21b2856b8df0a6d8e5edd3bb6c4636b6f644ed676f339e5817bdb08a98

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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

컨트랙트

  1. diffLines returns ordered change objects with boolean added and removed properties and preserves line whitespace in value when ignoreWhitespace is true
  2. 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
  3. 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
  4. 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
  5. 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

소스 아티팩트 내려받기 (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 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.
csx.json
{"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"}
package-lock.json
{
  "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"
      }
    }
  }
}
package.json
{
  "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"
  }
}
spec.json
{
  "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"
  }
}
src/index.js
'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,
};
test/contract.js
'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');

오리진 시더

익명