CodeSampleX

示例

debug 2.6.9: disable, enable, enabled

已验证示例 — npm debug 2.6.9: disable, enable, enabled. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: debug creates a debugger instance with the given…

sha256:ac8aeec2da7f977a865578b4bb788f80a1a853c85386361848d10a6c007d3a6b

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify pkg:npm/debug@2.6.9
包
符号
  • debug
  • debug.disable
  • debug.enable
  • debug.enabled
  • debug.formatters
创建时间
2026-08-27T12:47:28Z

契约

  1. debug creates a debugger instance with the given namespace property and enabled status
  2. debug.enable configures active namespace patterns matching wildcards and prefixes
  3. debug.disable deactivates all active namespace patterns
  4. debug.enabled evaluates whether a namespace string matches current enabled configuration
  5. debug.formatters allows registering custom argument formatters invoked during logging
  6. debug executes custom log function when enabled and does not invoke log function when disabled

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • 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@2.6.9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/debug@2.6.9
Demonstrate these symbols/APIs:
  - debug
  - debug.disable
  - debug.enable
  - debug.enabled
  - 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:5477608183eadba0a8bc835f9e72c740b76570ef10443c2d61e157452cbb2532","contract":["debug creates a debugger instance with the given namespace property and enabled status","debug.enable configures active namespace patterns matching wildcards and prefixes","debug.disable deactivates all active namespace patterns","debug.enabled evaluates whether a namespace string matches current enabled configuration","debug.formatters allows registering custom argument formatters invoked during logging","debug executes custom log function when enabled and does not invoke log function when disabled"],"goal":"verify pkg:npm/debug@2.6.9","kind":"HOW","packages":["pkg:npm/debug@2.6.9"],"schemaVersion":1,"symbols":["debug","debug.disable","debug.enable","debug.enabled","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@2.6.9"],"schemaVersion":1,"subject":"pkg:npm/debug@2.6.9","symbols":["debug","debug.disable","debug.enable","debug.enabled","debug.formatters"],"verifierAdapter":"node-typescript@1"}
index.mjs
import debug from 'debug';

/**
 * Creates a scoped debug logger for the given namespace.
 *
 * @param {string} namespace - The namespace to associate with the logger.
 * @returns {Function} A configured debug instance.
 */
export function createLogger(namespace) {
  return debug(namespace);
}

/**
 * Enables debug output for namespaces matching the given pattern.
 *
 * @param {string} namespaces - Comma- or space-separated pattern of namespaces to enable.
 */
export function enableLogging(namespaces) {
  debug.enable(namespaces);
}

/**
 * Disables all debug logging namespaces.
 */
export function disableLogging() {
  debug.disable();
}

/**
 * Checks whether logging is enabled for the specified namespace.
 *
 * @param {string} namespace - The namespace to check.
 * @returns {boolean} True if the namespace is enabled, false otherwise.
 */
export function isLoggingEnabled(namespace) {
  return debug.enabled(namespace);
}

/**
 * Registers a custom formatter for a format specifier character.
 *
 * @param {string} letter - Single character format specifier.
 * @param {Function} formatter - Formatting function receiving the value.
 */
export function registerFormatter(letter, formatter) {
  debug.formatters[letter] = formatter;
}

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": "2.6.9"
      }
    },
    "node_modules/debug": {
      "version": "2.6.9",
      "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
      "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
      "license": "MIT",
      "dependencies": {
        "ms": "2.0.0"
      }
    },
    "node_modules/ms": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
      "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "debug": "2.6.9"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/debug@2.6.9",
  "kind": "HOW",
  "packages": [
    "pkg:npm/debug@2.6.9"
  ],
  "symbols": [
    "debug",
    "debug.disable",
    "debug.enable",
    "debug.enabled",
    "debug.formatters"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import debug from 'debug';
import {
  createLogger,
  enableLogging,
  disableLogging,
  isLoggingEnabled,
  registerFormatter
} from '../index.mjs';

// Case 1: debug creates a debugger instance with the given namespace property and enabled status
{
  debug.disable();
  const log = debug('app:worker');
  assert.equal(typeof log, 'function', 'debug() should return a logger function');
  assert.equal(log.namespace, 'app:worker', 'logger should retain assigned namespace');
  assert.equal(log.enabled, false, 'logger should default to disabled when no namespaces are enabled');

  const helper = createLogger('app:service');
  assert.equal(typeof helper, 'function', 'createLogger helper should return a logger function');
  assert.equal(helper.namespace, 'app:service', 'createLogger helper should set namespace');
  assert.equal(helper.enabled, false, 'createLogger helper should default to disabled');
}

// Case 2: debug.enable configures active namespace patterns matching wildcards and prefixes
{
  debug.disable();
  enableLogging('app:*,db:*,-db:hidden');

  assert.equal(debug.enabled('app:server'), true, 'app:server matches app:*');
  assert.equal(debug.enabled('app:client:auth'), true, 'app:client:auth matches app:*');
  assert.equal(debug.enabled('db:query'), true, 'db:query matches db:*');
  assert.equal(debug.enabled('db:hidden'), false, 'db:hidden is excluded by -db:hidden');
  assert.equal(debug.enabled('other:task'), false, 'other:task does not match active patterns');

  const appLog = createLogger('app:server');
  assert.equal(appLog.enabled, true, 'newly created debug instance for matched namespace is enabled');
  const otherLog = createLogger('other:task');
  assert.equal(otherLog.enabled, false, 'newly created debug instance for unmatched namespace is disabled');
}

// Case 3: debug.disable deactivates all active namespace patterns
{
  enableLogging('app:*,db:*');
  assert.equal(debug.enabled('app:server'), true);
  assert.equal(debug.enabled('db:query'), true);

  disableLogging();

  assert.equal(debug.enabled('app:server'), false, 'app:server disabled after disableLogging()');
  assert.equal(debug.enabled('db:query'), false, 'db:query disabled after disableLogging()');

  const log = createLogger('app:server');
  assert.equal(log.enabled, false, 'debug instance created after disable is disabled');
}

// Case 4: debug.enabled evaluates whether a namespace string matches current enabled configuration
{
  enableLogging('worker:*,api:v1:*,-api:v1:internal');
  assert.equal(isLoggingEnabled('worker:pool'), true, 'matches worker:*');
  assert.equal(isLoggingEnabled('api:v1:users'), true, 'matches api:v1:*');
  assert.equal(isLoggingEnabled('api:v1:internal'), false, 'excluded by -api:v1:internal');
  assert.equal(isLoggingEnabled('api:v2:users'), false, 'does not match api:v1:*');
}

// Case 5: debug.formatters allows registering custom argument formatters invoked during logging
{
  registerFormatter('h', (val) => '0x' + Number(val).toString(16));
  enableLogging('test:format');
  const log = createLogger('test:format');

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

  log('address: %h', 255);
  assert.match(output, /0xff/, 'custom %h formatter formats 255 to 0xff');
}

// Case 6: debug executes custom log function when enabled and does not invoke log function when disabled
{
  disableLogging();
  let disabledCalls = 0;
  const disabledLog = createLogger('test:disabled');
  disabledLog.log = () => {
    disabledCalls++;
  };
  disabledLog('this should not be logged');
  assert.equal(disabledCalls, 0, 'disabled logger must not call log function');

  enableLogging('test:enabled');
  let enabledCalls = 0;
  let loggedArgs = null;
  const enabledLog = createLogger('test:enabled');
  enabledLog.log = (...args) => {
    enabledCalls++;
    loggedArgs = args;
  };
  enabledLog('hello', 'world');
  assert.equal(enabledCalls, 1, 'enabled logger must invoke custom log function');
  assert.ok(loggedArgs !== null, 'loggedArgs should be captured');
  assert.equal(loggedArgs[1], 'world', 'second argument should be forwarded');
}

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

原始种子者

匿名