CodeSampleX

示例

obug 2.1.4

已验证示例 — npm obug 2.1.4. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: createDebug creates a namespaced debugger instance with the specified…

sha256:75cf2a1e50ff57ca527c700288e382013fef0017a7774968baedee7238167e3e

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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-28

案例

HOW
目标
verify pkg:npm/obug@2.1.4
包
环境
node 22.23.2
创建时间
2026-08-28T05:56:02Z

契约

  1. createDebug creates a namespaced debugger instance with the specified namespace string
  2. enable activates debug output for matching namespaces including wildcard patterns
  3. disable deactivates all debug namespaces and returns the previously active namespace string
  4. enabled reports whether a given namespace is currently active
  5. namespaces returns the string of currently active debug namespaces
  6. Debugger.extend creates a child debugger whose namespace is prefixed with the parent namespace
  7. createDebug supports custom formatters and custom log sink functions

文件

  • 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/obug@2.1.4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/obug@2.1.4
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:2d228f05cdf069830938562877f687234a55be3f51087d5afea4e6edf3b3f6b4","constraints":{"executionContext":"node"},"contract":["createDebug creates a namespaced debugger instance with the specified namespace string","enable activates debug output for matching namespaces including wildcard patterns","disable deactivates all debug namespaces and returns the previously active namespace string","enabled reports whether a given namespace is currently active","namespaces returns the string of currently active debug namespaces","Debugger.extend creates a child debugger whose namespace is prefixed with the parent namespace","createDebug supports custom formatters and custom log sink functions"],"goal":"verify pkg:npm/obug@2.1.4","kind":"HOW","packages":["pkg:npm/obug@2.1.4"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/obug@2.1.4"],"schemaVersion":1,"subject":"pkg:npm/obug@2.1.4","verifierAdapter":"node-typescript@1"}
index.mjs
import { createDebug, enable, disable, enabled, namespaces } from 'obug';

export function createLogger(namespace, options) {
  return createDebug(namespace, options);
}

export function configureNamespaces(pattern) {
  enable(pattern);
}

export function resetNamespaces() {
  return disable();
}

export function isNamespaceEnabled(name) {
  return enabled(name);
}

export function getActiveNamespaces() {
  return namespaces();
}

export { createDebug, enable, disable, enabled, namespaces };
package-lock.json
{
  "name": "sample-obug",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-obug",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "obug": "2.1.4"
      }
    },
    "node_modules/obug": {
      "version": "2.1.4",
      "resolved": "https://registry.npmjs.org/obug/-/obug-2.1.4.tgz",
      "integrity": "sha512-4a+OsYv9UktOJKE+l1A4OufDgdRF9PifWj+tJnHURo/P+WOxpG4GzUFL9qCalmWauao6ogiG+QvnCovwPoyAWA==",
      "funding": [
        "https://github.com/sponsors/sxzz",
        "https://opencollective.com/debug"
      ],
      "license": "MIT",
      "engines": {
        "node": ">=12.20.0"
      }
    }
  }
}
package.json
{
  "name": "sample-obug",
  "version": "1.0.0",
  "private": true,
  "description": "verify pkg:npm/obug@2.1.4",
  "type": "module",
  "main": "index.mjs",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "obug": "2.1.4"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/obug@2.1.4",
  "kind": "HOW",
  "packages": [
    "pkg:npm/obug@2.1.4"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "esm",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert';
import util from 'node:util';
import { createDebug, enable, disable, enabled, namespaces } from 'obug';
import {
  createLogger,
  configureNamespaces,
  resetNamespaces,
  isNamespaceEnabled,
  getActiveNamespaces
} from '../index.mjs';

async function main() {
  // Test 1: createDebug creates a namespaced debugger instance
  const logs = [];
  const logSink = (...args) => {
    logs.push(args);
  };

  const debug = createDebug('sample:app', {
    useColors: false,
    log: logSink
  });

  assert.strictEqual(typeof debug, 'function', 'createDebug must return a callable function');
  assert.strictEqual(debug.namespace, 'sample:app', 'Debugger must retain its namespace');
  assert.strictEqual(typeof debug.extend, 'function', 'Debugger must have an extend method');

  // Initially disable all namespaces
  disable();
  assert.strictEqual(debug.enabled, false, 'Debugger should be disabled after disable()');
  assert.strictEqual(enabled('sample:app'), false, 'enabled() should return false for sample:app when disabled');
  assert.strictEqual(namespaces(), '', 'namespaces() should return empty string after disable()');

  // Test 2: enable activates matching namespaces including wildcards
  enable('sample:*');
  assert.strictEqual(debug.enabled, true, 'Debugger should become enabled when namespace matches pattern');
  assert.strictEqual(enabled('sample:app'), true, 'enabled() must return true for matching namespace');
  assert.strictEqual(enabled('other:app'), false, 'enabled() must return false for non-matching namespace');
  assert.strictEqual(namespaces(), 'sample:*', 'namespaces() must return active namespace pattern');

  // Test 3: Calling debugger outputs formatted message when enabled
  logs.length = 0;
  debug('processing item %d: %s', 42, 'test-item');
  assert.strictEqual(logs.length, 1, 'Calling enabled debugger must invoke custom log function');
  const formattedOutput = util.format(...logs[0]);
  assert.ok(formattedOutput.includes('sample:app'), 'Formatted output must include the namespace');
  assert.ok(formattedOutput.includes('processing item 42: test-item'), 'Formatted output must format arguments');

  // Test 4: Debugger.extend creates hierarchical sub-namespaces
  const subWorker = debug.extend('worker');
  assert.strictEqual(subWorker.namespace, 'sample:app:worker', 'extend must create colon-delimited child namespace');
  assert.strictEqual(subWorker.enabled, true, 'Extended debugger inherits enablement');

  logs.length = 0;
  subWorker('worker task completed');
  assert.strictEqual(logs.length, 1, 'Extended debugger must invoke log function');
  const subFormatted = util.format(...logs[0]);
  assert.ok(subFormatted.includes('sample:app:worker'), 'Sub-debugger output must include extended namespace');
  assert.ok(subFormatted.includes('worker task completed'), 'Sub-debugger output must include message');

  // Test 5: disable deactivates all namespaces and returns previous pattern
  const previous = disable();
  assert.strictEqual(previous, 'sample:*', 'disable() must return the previous pattern string');
  assert.strictEqual(debug.enabled, false, 'Debugger enabled property must be false after disable()');
  assert.strictEqual(subWorker.enabled, false, 'Extended debugger enabled property must be false after disable()');

  logs.length = 0;
  debug('this message should not be logged');
  assert.strictEqual(logs.length, 0, 'Disabled debugger must not produce log output');

  // Test 6: Custom formatters
  enable('sample:fmt');
  const fmtLogs = [];
  const fmtDebug = createDebug('sample:fmt', {
    useColors: false,
    log: (...args) => fmtLogs.push(args),
    formatters: {
      h: (val) => Buffer.from(val).toString('hex')
    }
  });

  fmtDebug('data in hex: %h', 'csx-test');
  assert.strictEqual(fmtLogs.length, 1, 'Custom formatter debugger must log output');
  const fmtOutput = util.format(...fmtLogs[0]);
  assert.ok(fmtOutput.includes('6373782d74657374'), 'Custom formatter %h must transform string to hex');

  // Test 7: Helper functions from index.mjs
  configureNamespaces('helper:*');
  assert.strictEqual(isNamespaceEnabled('helper:db'), true, 'configureNamespaces must enable namespace');
  assert.strictEqual(getActiveNamespaces(), 'helper:*', 'getActiveNamespaces must return active pattern');

  const helperLogger = createLogger('helper:db', {
    useColors: false,
    log: logSink
  });
  assert.strictEqual(helperLogger.namespace, 'helper:db', 'createLogger helper must return configured instance');

  resetNamespaces();
  assert.strictEqual(isNamespaceEnabled('helper:db'), false, 'resetNamespaces must disable all namespaces');

  console.log('obug contract verification passed.');
  process.exit(0);
}

main().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

原始种子者

匿名