CodeSampleX

示例

chalk 4.1.2

已验证示例 — npm chalk 4.1.2. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: chalk default export is a styling function with Instance constructor and…

sha256:4b029aaeb5b85c0dcedd38477e2ad9d2f7168e6b2492fbbd98fd1513aae12733

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-26

案例

HOW
目标
verify pkg:npm/chalk@4.1.2
包
环境
node 22.23.2
创建时间
2026-08-26T09:09:22Z

契约

  1. chalk default export is a styling function with Instance constructor and undefined Chalk property in chalk 4.x
  2. chalk.Instance with level 3 produces 24-bit TrueColor ANSI escape sequences for rgb, hex, and bgHex colors
  3. chalk.Instance with level 2 downsamples 24-bit rgb and hex colors to 256-color palette ANSI escape codes
  4. chalk.Instance with level 1 downsamples rgb and hex colors to basic 16-color ANSI escape codes
  5. chalk.Instance with level 0 disables ANSI color sequences and returns plain unstyled strings
  6. mutating the level property dynamically updates color downsampling on an existing chalk instance
  7. nested styling re-opens the enclosing color code immediately after the nested color close code
  8. chained style modifiers apply opening and closing ANSI codes in inverse order
  9. variadic arguments passed to style functions are concatenated with spaces
  10. passing a Symbol to style functions throws a TypeError during string coercion

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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/chalk@4.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/chalk@4.1.2
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:a208246d224d51e927259ac57db2be38884ed90169def227e405a3bd9819747e","constraints":{"executionContext":"node"},"contract":["chalk default export is a styling function with Instance constructor and undefined Chalk property in chalk 4.x","chalk.Instance with level 3 produces 24-bit TrueColor ANSI escape sequences for rgb, hex, and bgHex colors","chalk.Instance with level 2 downsamples 24-bit rgb and hex colors to 256-color palette ANSI escape codes","chalk.Instance with level 1 downsamples rgb and hex colors to basic 16-color ANSI escape codes","chalk.Instance with level 0 disables ANSI color sequences and returns plain unstyled strings","mutating the level property dynamically updates color downsampling on an existing chalk instance","nested styling re-opens the enclosing color code immediately after the nested color close code","chained style modifiers apply opening and closing ANSI codes in inverse order","variadic arguments passed to style functions are concatenated with spaces","passing a Symbol to style functions throws a TypeError during string coercion"],"goal":"verify pkg:npm/chalk@4.1.2","kind":"HOW","packages":["pkg:npm/chalk@4.1.2"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"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/chalk@4.1.2"],"schemaVersion":1,"subject":"pkg:npm/chalk@4.1.2","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-319676251",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-319676251",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "chalk": "4.1.2"
      }
    },
    "node_modules/ansi-styles": {
      "version": "4.3.0",
      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
      "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
      "license": "MIT",
      "dependencies": {
        "color-convert": "^2.0.1"
      },
      "engines": {
        "node": ">=8"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-styles?sponsor=1"
      }
    },
    "node_modules/chalk": {
      "version": "4.1.2",
      "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
      "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
      "license": "MIT",
      "dependencies": {
        "ansi-styles": "^4.1.0",
        "supports-color": "^7.1.0"
      },
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/chalk/chalk?sponsor=1"
      }
    },
    "node_modules/color-convert": {
      "version": "2.0.1",
      "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
      "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
      "license": "MIT",
      "dependencies": {
        "color-name": "~1.1.4"
      },
      "engines": {
        "node": ">=7.0.0"
      }
    },
    "node_modules/color-name": {
      "version": "1.1.4",
      "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
      "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
      "license": "MIT"
    },
    "node_modules/has-flag": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
      "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
      "license": "MIT",
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/supports-color": {
      "version": "7.2.0",
      "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
      "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
      "license": "MIT",
      "dependencies": {
        "has-flag": "^4.0.0"
      },
      "engines": {
        "node": ">=8"
      }
    }
  }
}
package.json
{
  "name": "sample-chalk",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for chalk@4.1.2",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "chalk": "4.1.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/chalk@4.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/chalk@4.1.2"
  ],
  "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 chalk = require('chalk');

/**
 * Creates a configured Chalk instance with the specified color level.
 *
 * @param {number} level - Color support level (0 = disabled, 1 = 16 colors, 2 = 256 colors, 3 = truecolor).
 * @returns {chalk.Chalk} A configured Chalk instance.
 */
function createChalkInstance(level) {
  return new chalk.Instance({ level });
}

/**
 * Styles a message with custom foreground and background hex colors.
 *
 * @param {string} text - Text to format.
 * @param {string} fgHex - Foreground hex color code.
 * @param {string} bgHex - Background hex color code.
 * @param {number} [level=3] - Target color level.
 * @returns {string} Styled ANSI string or plain text if disabled.
 */
function styleWithHex(text, fgHex, bgHex, level = 3) {
  const instance = new chalk.Instance({ level });
  let styled = instance.hex(fgHex);
  if (bgHex) {
    styled = styled.bgHex(bgHex);
  }
  return styled(text);
}

/**
 * Formats a log entry with severity-based coloring and timestamp.
 *
 * @param {'info' | 'warn' | 'error' | 'success'} severity - Log severity level.
 * @param {string} message - Log message.
 * @param {chalk.Chalk} [instance=chalk] - Chalk instance to use.
 * @returns {string} Formatted log string.
 */
function formatLog(severity, message, instance = chalk) {
  switch (severity) {
    case 'error':
      return instance.red.bold(`[ERROR] ${message}`);
    case 'warn':
      return instance.yellow(`[WARN] ${message}`);
    case 'success':
      return instance.green(`[SUCCESS] ${message}`);
    case 'info':
    default:
      return instance.blue(`[INFO] ${message}`);
  }
}

module.exports = {
  chalk,
  createChalkInstance,
  styleWithHex,
  formatLog
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const { chalk, createChalkInstance, styleWithHex, formatLog } = require('../src/index.js');

async function testChalkContract() {
  // 1. Export verification and constructor availability in chalk 4.x
  assert.equal(typeof chalk, 'function', 'chalk export must be a styling function');
  assert.equal(typeof chalk.Instance, 'function', 'chalk.Instance constructor must be available');
  assert.equal(chalk.Chalk, undefined, 'chalk.Chalk is undefined in chalk 4.x');

  // 2. Color level 3 (TrueColor) escape sequences
  const ctx3 = createChalkInstance(3);
  assert.equal(ctx3.level, 3, 'instance level should be 3');
  assert.equal(ctx3.blue('test'), '\u001b[34mtest\u001b[39m', 'blue foreground ANSI code');
  assert.equal(ctx3.rgb(255, 100, 50)('rgb'), '\u001b[38;2;255;100;50mrgb\u001b[39m', '24-bit RGB escape code');
  assert.equal(ctx3.hex('#FF0000')('hex'), '\u001b[38;2;255;0;0mhex\u001b[39m', '24-bit hex escape code');
  assert.equal(ctx3.bgHex('#00FF00')('bg'), '\u001b[48;2;0;255;0mbg\u001b[49m', '24-bit background hex escape code');

  // 3. Color level 2 (256-color) downsampling
  const ctx2 = createChalkInstance(2);
  assert.equal(ctx2.level, 2, 'instance level should be 2');
  assert.equal(ctx2.rgb(255, 0, 0)('downsampled256'), '\u001b[38;5;196mdownsampled256\u001b[39m', 'rgb downsamples to 256-color palette code');
  assert.equal(ctx2.hex('#FF0000')('downsampled256'), '\u001b[38;5;196mdownsampled256\u001b[39m', 'hex downsamples to 256-color palette code');

  // 4. Color level 1 (16-color basic) downsampling
  const ctx1 = createChalkInstance(1);
  assert.equal(ctx1.level, 1, 'instance level should be 1');
  assert.equal(ctx1.rgb(255, 0, 0)('downsampled16'), '\u001b[91mdownsampled16\u001b[39m', 'rgb downsamples to basic 16-color ANSI code');
  assert.equal(ctx1.hex('#FF0000')('downsampled16'), '\u001b[91mdownsampled16\u001b[39m', 'hex downsamples to basic 16-color ANSI code');

  // 5. Color level 0 (disabled colors)
  const ctx0 = createChalkInstance(0);
  assert.equal(ctx0.level, 0, 'instance level should be 0');
  assert.equal(ctx0.blue('plain'), 'plain', 'level 0 disables blue style');
  assert.equal(ctx0.red.bold('plain'), 'plain', 'level 0 disables chained styles');
  assert.equal(ctx0.rgb(255, 0, 0)('plain'), 'plain', 'level 0 disables rgb styles');

  // 6. Dynamic level mutation on instance
  const dynInstance = createChalkInstance(3);
  assert.equal(dynInstance.red('dynamic'), '\u001b[31mdynamic\u001b[39m');
  dynInstance.level = 0;
  assert.equal(dynInstance.red('dynamic'), 'dynamic', 'mutating level to 0 immediately disables color codes');
  dynInstance.level = 1;
  assert.equal(dynInstance.hex('#FF0000')('dynamic'), '\u001b[91mdynamic\u001b[39m', 'mutating level to 1 applies 16-color downsampling');

  // 7. Nested styling behavior
  const nested = ctx3.red('foo', ctx3.blue('bar'), 'baz');
  assert.equal(nested, '\u001b[31mfoo \u001b[34mbar\u001b[39m\u001b[31m baz\u001b[39m', 'nested style re-opens outer red after inner blue closes');

  // 8. Chained modifiers and order of opening/closing escape sequences
  assert.equal(ctx3.bold.red('alert'), '\u001b[1m\u001b[31malert\u001b[39m\u001b[22m', 'bold.red opens bold then red, closes red then bold');
  assert.equal(ctx3.red.bold('alert'), '\u001b[31m\u001b[1malert\u001b[22m\u001b[39m', 'red.bold opens red then bold, closes bold then red');

  // 9. Variadic arguments and non-string argument handling
  assert.equal(ctx3.blue('hello', 'world'), '\u001b[34mhello world\u001b[39m', 'variadic arguments are space-joined');
  assert.equal(ctx3.blue(42), '\u001b[34m42\u001b[39m', 'number argument is converted to string');
  assert.equal(ctx3.blue(false), '\u001b[34mfalse\u001b[39m', 'boolean argument is converted to string');
  assert.equal(ctx3.blue({ id: 1 }), '\u001b[34m[object Object]\u001b[39m', 'object argument coerces with Object.prototype.toString');
  assert.throws(() => ctx3.blue(Symbol('key')), { name: 'TypeError' }, 'Symbol throws TypeError on string coercion');

  // 10. Helper function verifications
  const hexOutput = styleWithHex('custom', '#FF8800', '#0000FF', 3);
  assert.equal(hexOutput, '\u001b[38;2;255;136;0m\u001b[48;2;0;0;255mcustom\u001b[49m\u001b[39m', 'styleWithHex applies fg and bg hex in level 3');

  const logError = formatLog('error', 'disk full', ctx3);
  assert.equal(logError, '\u001b[31m\u001b[1m[ERROR] disk full\u001b[22m\u001b[39m', 'formatLog applies red.bold to error');

  const logWarn = formatLog('warn', 'low memory', ctx3);
  assert.equal(logWarn, '\u001b[33m[WARN] low memory\u001b[39m', 'formatLog applies yellow to warn');

  const logSuccess = formatLog('success', 'build complete', ctx3);
  assert.equal(logSuccess, '\u001b[32m[SUCCESS] build complete\u001b[39m', 'formatLog applies green to success');

  const logInfo = formatLog('info', 'starting service', ctx3);
  assert.equal(logInfo, '\u001b[34m[INFO] starting service\u001b[39m', 'formatLog applies blue to info');

  console.log('All chalk 4.1.2 contract assertions passed.');
}

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

原始种子者

匿名