CodeSampleX

示例

debug 4.3.7

已验证示例 — npm debug 4.3.7. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: debug creates namespaced loggers and dynamically enables or disables matching…

sha256:9d5f995b08e2b1a4e37460cd26604aa1ed33a9419a6d593a3e4386f2aff4833a

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

案例

HOW
目标
verify pkg:npm/debug@4.3.7
包
创建时间
2026-08-19T17:29:00Z

契约

  1. debug creates namespaced loggers and dynamically enables or disables matching patterns
  2. debug extend creates hierarchical child loggers with custom delimiters
  3. debug formatters allows custom argument formatters and output stream interception

文件

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

下载源代码构件 (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/debug@4.3.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/debug@4.3.7

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:1ede214d11da13cd36bc748d43780775d1aa1e6e0689be71a6f24c87d88809fa","contract":["debug creates namespaced loggers and dynamically enables or disables matching patterns","debug extend creates hierarchical child loggers with custom delimiters","debug formatters allows custom argument formatters and output stream interception"],"goal":"verify pkg:npm/debug@4.3.7","kind":"HOW","packages":["pkg:npm/debug@4.3.7"],"schemaVersion":1},"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,"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "debug-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "debug-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": "debug-sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "debug": "4.3.7"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/debug@4.3.7",
  "kind": "HOW",
  "packages": [
    "pkg:npm/debug@4.3.7"
  ]
}
src/index.mjs
import debug from 'debug';

/**
 * Creates a debug logger instance for a given namespace.
 *
 * @param {string} namespace
 * @returns {debug.Debugger}
 */
export function createLogger(namespace) {
  return debug(namespace);
}

/**
 * Enables namespaces matching the given pattern string.
 *
 * @param {string} namespaces
 */
export function enableNamespaces(namespaces) {
  debug.enable(namespaces);
}

/**
 * Disables all debug logging namespaces.
 *
 * @returns {string} The previously enabled namespaces string.
 */
export function disableNamespaces() {
  return debug.disable();
}

/**
 * Checks whether a given namespace is currently enabled.
 *
 * @param {string} namespace
 * @returns {boolean}
 */
export function isNamespaceEnabled(namespace) {
  return debug.enabled(namespace);
}

/**
 * Extends an existing debug logger to create a child logger with hierarchical namespace.
 *
 * @param {debug.Debugger} parentLogger
 * @param {string} subNamespace
 * @param {string} [delimiter]
 * @returns {debug.Debugger}
 */
export function extendLogger(parentLogger, subNamespace, delimiter) {
  if (delimiter !== undefined) {
    return parentLogger.extend(subNamespace, delimiter);
  }
  return parentLogger.extend(subNamespace);
}

/**
 * Registers a custom format specifier on the global debug formatters.
 *
 * @param {string} identifier Single character formatter identifier (e.g. 'u', 'j').
 * @param {(value: any) => string} formatterFn
 */
export function registerFormatter(identifier, formatterFn) {
  debug.formatters[identifier] = formatterFn;
}

/**
 * Captures log output produced by calling a callback with a logger.
 *
 * @param {debug.Debugger} logger
 * @param {(log: debug.Debugger) => void} fn
 * @returns {string[]}
 */
export function captureLogOutput(logger, fn) {
  const originalLog = logger.log;
  const captured = [];
  try {
    logger.log = (...args) => {
      captured.push(args.join(' '));
    };
    fn(logger);
  } finally {
    logger.log = originalLog;
  }
  return captured;
}
test/contract.mjs
import assert from 'node:assert/strict';
import {
  createLogger,
  enableNamespaces,
  disableNamespaces,
  isNamespaceEnabled,
  extendLogger,
  registerFormatter,
  captureLogOutput,
} from '../src/index.mjs';

// Contract 1: debug creates namespaced loggers and dynamically enables or disables matching patterns
{
  const logA = createLogger('app:http');
  const logB = createLogger('app:db');
  const logC = createLogger('worker:job');

  assert.strictEqual(logA.namespace, 'app:http', 'logA should have namespace app:http');
  assert.strictEqual(logB.namespace, 'app:db', 'logB should have namespace app:db');
  assert.strictEqual(logC.namespace, 'worker:job', 'logC should have namespace worker:job');

  // Enable app:* and exclude app:db
  enableNamespaces('app:*,-app:db');

  assert.strictEqual(isNamespaceEnabled('app:http'), true, 'app:http should be enabled');
  assert.strictEqual(isNamespaceEnabled('app:db'), false, 'app:db should be disabled by exclusion');
  assert.strictEqual(isNamespaceEnabled('worker:job'), false, 'worker:job should not match pattern');
  assert.strictEqual(logA.enabled, true, 'logA instance should be enabled');
  assert.strictEqual(logB.enabled, false, 'logB instance should be disabled');

  // Disable all
  const prev = disableNamespaces();
  assert.strictEqual(prev, 'app:*,-app:db', 'disable should return previous namespaces string');
  assert.strictEqual(isNamespaceEnabled('app:http'), false, 'app:http should now be disabled');
}

// Contract 2: debug extend creates hierarchical child loggers with custom delimiters
{
  enableNamespaces('server:*');

  const serverLog = createLogger('server');
  const authLog = extendLogger(serverLog, 'auth');
  const sessionLog = extendLogger(authLog, 'session');

  assert.strictEqual(authLog.namespace, 'server:auth', 'Child namespace should be server:auth');
  assert.strictEqual(sessionLog.namespace, 'server:auth:session', 'Nested child namespace should be server:auth:session');
  assert.strictEqual(authLog.enabled, true, 'Extended logger should be enabled under server:*');
  assert.strictEqual(sessionLog.enabled, true, 'Nested extended logger should be enabled under server:*');

  // Custom delimiter
  const customDelimLog = extendLogger(serverLog, 'metrics', '/');
  assert.strictEqual(customDelimLog.namespace, 'server/metrics', 'Custom delimiter / should be used in namespace');
}

// Contract 3: debug formatters allows custom argument formatters and output stream interception
{
  enableNamespaces('test:formatter');
  const testLog = createLogger('test:formatter');

  // Register custom uppercase formatter %u
  registerFormatter('u', (val) => String(val).toUpperCase());

  const messages = captureLogOutput(testLog, (log) => {
    log('processing item %u with id %d', 'alpha', 42);
  });

  assert.strictEqual(messages.length, 1, 'Should capture exactly 1 log line');
  assert.ok(messages[0].includes('ALPHA'), 'Custom formatter %u should uppercase input to ALPHA');
  assert.ok(messages[0].includes('42'), 'Standard %d formatter should format number 42');
  assert.ok(messages[0].includes('test:formatter'), 'Log line should contain the logger namespace');
}

console.log('All pkg:npm/debug@4.3.7 contract tests passed successfully.');

原始种子者

匿名