CodeSampleX

Exemple

diff 9.0.0

Échantillon vérifié pour npm diff 9.0.0. Le contrat s'est exécuté sur node 22 · linux debian/x64 · docker et a réussi : diffLines returns ordered change…

sha256:148e5d21b2856b8df0a6d8e5edd3bb6c4636b6f644ed676f339e5817bdb08a98

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify pkg:npm/diff@9.0.0
Paquets
Environnement
node 22.23.2
Créé
2026-11-28T02:19:29Z

Contrat

  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

Fichiers

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

Télécharger l’artefact source (tar.gz)

Code source

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');

Seeder d'origine

anonyme