CodeSampleX

Beispiel

debug 4.3.7: formatters

Verifiziertes Beispiel für npm debug 4.3.7: formatters. Der Vertrag lief auf node 22 · linux debian/x64 · docker und bestand: debug.formatters exposes…

sha256:e3caad60e4f1940619693928bd5248f2394c464c6ef212489f88310f11e1b054

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 npm

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
verify debug.formatters in pkg:npm/debug@4.3.7
Pakete
Symbole
  • debug.formatters
Erstellt
2026-09-07T03:29:57Z

Contract

  1. debug.formatters exposes built-in formatters for %o (compact) and %O (multi-line) inspection
  2. debug.formatters allows registering custom formatters invoked during log formatting
  3. debug.formatters executes custom formatters with the logger instance as this context
  4. debug.formatters inlines the formatted token and shifts subsequent unformatted arguments
  5. debug.formatters supports uppercase and lowercase single-letter format specifiers

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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 debug.formatters in pkg:npm/debug@4.3.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/debug@4.3.7
Demonstrate these symbols/APIs:
  - debug.formatters

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:c84c74f590253dc275b9187b1a47e995d1a365b8d90a770f8c76291187272a70","contract":["debug.formatters exposes built-in formatters for %o (compact) and %O (multi-line) inspection","debug.formatters allows registering custom formatters invoked during log formatting","debug.formatters executes custom formatters with the logger instance as this context","debug.formatters inlines the formatted token and shifts subsequent unformatted arguments","debug.formatters supports uppercase and lowercase single-letter format specifiers"],"goal":"verify debug.formatters in pkg:npm/debug@4.3.7","kind":"HOW","packages":["pkg:npm/debug@4.3.7"],"schemaVersion":1,"symbols":["debug.formatters"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/debug@4.3.7"],"schemaVersion":1,"subject":"pkg:npm/debug@4.3.7","symbols":["debug.formatters"],"verifierAdapter":"node-typescript@1"}
index.mjs
import debug from 'debug';

/**
 * Registers a custom single-letter formatter on debug.formatters.
 *
 * @param {string} letter - A single letter format specifier.
 * @param {Function} formatter - Formatting function receiving the value and bound to logger.
 */
export function registerFormatter(letter, formatter) {
  debug.formatters[letter] = formatter;
}

/**
 * Retrieves a registered formatter function from debug.formatters.
 *
 * @param {string} letter - The single letter format specifier.
 * @returns {Function|undefined} The registered formatter function.
 */
export function getFormatter(letter) {
  return debug.formatters[letter];
}

/**
 * Creates a configured debug logger for a namespace.
 *
 * @param {string} namespace - Logger namespace.
 * @returns {Function} Debug logger function.
 */
export function createLogger(namespace) {
  return debug(namespace);
}

export default debug;
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "debug": "4.3.7"
      }
    },
    "node_modules/debug": {
      "version": "4.3.7",
      "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz",
      "integrity": "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==",
      "license": "MIT",
      "dependencies": {
        "ms": "^2.1.3"
      },
      "engines": {
        "node": ">=6.0"
      },
      "peerDependenciesMeta": {
        "supports-color": {
          "optional": true
        }
      }
    },
    "node_modules/ms": {
      "version": "2.1.3",
      "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
      "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "debug": "4.3.7"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify debug.formatters in pkg:npm/debug@4.3.7",
  "kind": "HOW",
  "packages": [
    "pkg:npm/debug@4.3.7"
  ],
  "symbols": [
    "debug.formatters"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import debug, {
  registerFormatter,
  getFormatter,
  createLogger
} from '../index.mjs';

// Case 1: debug.formatters exposes built-in formatters for %o (compact) and %O (multi-line) inspection
{
  assert.equal(typeof debug.formatters.o, 'function', 'debug.formatters.o is a function');
  assert.equal(typeof debug.formatters.O, 'function', 'debug.formatters.O is a function');

  const obj = { nested: { longProp1: 'value one', longProp2: 'value two', longProp3: 'value three', longProp4: 'value four' } };
  const single = debug.formatters.o.call({ inspectOpts: {}, useColors: false }, obj);
  assert.ok(!single.includes('\n'), 'o formatter flattens to a single line');

  const multi = debug.formatters.O.call({ inspectOpts: {}, useColors: false }, obj);
  assert.ok(multi.includes('\n'), 'O formatter retains multiple lines');
}

// Case 2: debug.formatters allows registering custom formatters invoked during log formatting
{
  registerFormatter('h', (val) => '0x' + Number(val).toString(16));
  assert.equal(typeof getFormatter('h'), 'function', 'h formatter registered');

  debug.enable('test:custom');
  const log = createLogger('test:custom');
  log.useColors = false;

  let captured = '';
  log.log = (...args) => {
    captured = args.join(' ');
  };

  log('offset: %h', 255);
  assert.match(captured, /offset: 0xff/, '%h formatter renders hex string');
}

// Case 3: debug.formatters executes custom formatters with the logger instance as this context
{
  registerFormatter('u', function(val) {
    return `[${this.namespace}] ${String(val).toUpperCase()}`;
  });

  debug.enable('test:context');
  const log = createLogger('test:context');
  log.useColors = false;

  let captured = '';
  log.log = (...args) => {
    captured = args.join(' ');
  };

  log('user: %u', 'alice');
  assert.match(captured, /user: \[test:context\] ALICE/, 'formatter accesses this.namespace');
}

// Case 4: debug.formatters inlines the formatted token and shifts subsequent unformatted arguments
{
  registerFormatter('b', (val) => (val ? 'TRUE' : 'FALSE'));

  debug.enable('test:shift');
  const log = createLogger('test:shift');
  log.useColors = false;

  let capturedArgs = [];
  log.log = (...args) => {
    capturedArgs = args;
  };

  log('active: %b result', true, 'trailing-arg');
  assert.match(capturedArgs[0], /active: TRUE result/, 'inlines formatted argument');
  assert.equal(capturedArgs[1], 'trailing-arg', 'shifts subsequent argument');
}

// Case 5: debug.formatters supports uppercase and lowercase single-letter format specifiers
{
  registerFormatter('J', (val) => JSON.stringify(val));
  registerFormatter('j', (val) => JSON.stringify(val).toLowerCase());

  debug.enable('test:cases');
  const log = createLogger('test:cases');
  log.useColors = false;

  let captured = '';
  log.log = (...args) => {
    captured = args.join(' ');
  };

  log('upper: %J lower: %j', { State: 'OK' }, { State: 'OK' });
  assert.match(captured, /upper: \{"State":"OK"\} lower: \{"state":"ok"\}/, 'supports both uppercase and lowercase keys');
}

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

Ursprungs-Seeder

anonym