CodeSampleX

示例

ansi-escapes 4.3.2: default

已验证示例 — npm ansi-escapes 4.3.2: default. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: ansiEscapes.default provides the default export referencing…

sha256:9b8e817032c74d24bece7903eaf0ec9f5f23146bbb672bca64fb681ce60b75c0

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-05

案例

HOW
目标
verify ansi-escapes.default in pkg:npm/ansi-escapes@4.3.2
包
符号
  • ansi-escapes.default
环境
node 22.23.2
创建时间
2026-09-05T18:22:41Z

契约

  1. ansiEscapes.default provides the default export referencing the ansiEscapes function and property suite
  2. cursorTo generates absolute cursor positioning ANSI escape sequences for given column and row coordinates
  3. cursorTo throws a TypeError when the x coordinate argument is missing or not a number
  4. cursorMove generates relative cursor movement ANSI escape sequences for horizontal and vertical offsets
  5. cursorUp, cursorDown, cursorForward, and cursorBackward produce directional cursor displacement codes
  6. eraseLines produces ANSI escape sequences to clear the specified number of terminal lines
  7. link produces OSC 8 terminal hyperlink escape sequences for given text and URL
  8. clearScreen and clearTerminal emit terminal clearing ANSI escape sequences

文件

  • 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 ansi-escapes.default in pkg:npm/ansi-escapes@4.3.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ansi-escapes@4.3.2
Demonstrate these symbols/APIs:
  - ansi-escapes.default

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:0544c427bd52d56e611d9127f88b05f080732bb5f53433bfed1482e948792cff","contract":["ansiEscapes.default provides the default export referencing the ansiEscapes function and property suite","cursorTo generates absolute cursor positioning ANSI escape sequences for given column and row coordinates","cursorTo throws a TypeError when the x coordinate argument is missing or not a number","cursorMove generates relative cursor movement ANSI escape sequences for horizontal and vertical offsets","cursorUp, cursorDown, cursorForward, and cursorBackward produce directional cursor displacement codes","eraseLines produces ANSI escape sequences to clear the specified number of terminal lines","link produces OSC 8 terminal hyperlink escape sequences for given text and URL","clearScreen and clearTerminal emit terminal clearing ANSI escape sequences"],"goal":"verify ansi-escapes.default in pkg:npm/ansi-escapes@4.3.2","kind":"HOW","packages":["pkg:npm/ansi-escapes@4.3.2"],"schemaVersion":1,"symbols":["ansi-escapes.default"]},"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/ansi-escapes@4.3.2"],"schemaVersion":1,"subject":"pkg:npm/ansi-escapes@4.3.2","symbols":["ansi-escapes.default"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-ansi-escapes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-ansi-escapes",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ansi-escapes": "4.3.2"
      }
    },
    "node_modules/ansi-escapes": {
      "version": "4.3.2",
      "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz",
      "integrity": "sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==",
      "license": "MIT",
      "dependencies": {
        "type-fest": "^0.21.3"
      },
      "engines": {
        "node": ">=8"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    },
    "node_modules/type-fest": {
      "version": "0.21.3",
      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.21.3.tgz",
      "integrity": "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==",
      "license": "(MIT OR CC0-1.0)",
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    }
  }
}
package.json
{
  "name": "sample-ansi-escapes",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for ansi-escapes@4.3.2",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "ansi-escapes": "4.3.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ansi-escapes.default in pkg:npm/ansi-escapes@4.3.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ansi-escapes@4.3.2"
  ],
  "symbols": [
    "ansi-escapes.default"
  ]
}
src/index.js
'use strict';

const ansiEscapes = require('ansi-escapes');

/**
 * Returns the default ansiEscapes instance.
 * Verifies that ansiEscapes and ansiEscapes.default point to the API.
 *
 * @returns {object} The ansi-escapes API object.
 */
function getDefaultExport() {
  return ansiEscapes.default || ansiEscapes;
}

/**
 * Positions cursor at (x, y) coordinates using cursorTo.
 *
 * @param {number} x - 0-based column coordinate.
 * @param {number} [y] - 0-based row coordinate.
 * @returns {string} ANSI escape sequence.
 */
function positionCursor(x, y) {
  const escapes = getDefaultExport();
  return escapes.cursorTo(x, y);
}

/**
 * Moves cursor relative to current position using cursorMove.
 *
 * @param {number} x - Horizontal relative movement.
 * @param {number} [y] - Vertical relative movement.
 * @returns {string} ANSI escape sequence.
 */
function moveCursor(x, y) {
  const escapes = getDefaultExport();
  return escapes.cursorMove(x, y);
}

/**
 * Formats a terminal hyperlink using link(text, url).
 *
 * @param {string} text - Display text.
 * @param {string} url - Target URL.
 * @returns {string} OSC 8 ANSI escape sequence.
 */
function createHyperlink(text, url) {
  const escapes = getDefaultExport();
  return escapes.link(text, url);
}

/**
 * Generates ANSI sequence to erase specified number of lines.
 *
 * @param {number} count - Number of lines to erase.
 * @returns {string} ANSI escape sequence.
 */
function eraseLines(count) {
  const escapes = getDefaultExport();
  return escapes.eraseLines(count);
}

module.exports = {
  ansiEscapes,
  getDefaultExport,
  positionCursor,
  moveCursor,
  createHyperlink,
  eraseLines
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import ansiEscapesDirect from 'ansi-escapes';

const require = createRequire(import.meta.url);
const {
  ansiEscapes,
  getDefaultExport,
  positionCursor,
  moveCursor,
  createHyperlink,
  eraseLines
} = require('../src/index.js');

async function testAll() {
  // 1. Default export verification
  assert.ok(ansiEscapes, 'ansiEscapes export must exist');
  assert.equal(typeof ansiEscapes, 'object', 'ansiEscapes must be an object');
  assert.equal(ansiEscapes.default, ansiEscapes, 'ansiEscapes.default must reference ansiEscapes itself');
  assert.equal(ansiEscapesDirect, ansiEscapes, 'ESM default import must match CJS export');
  assert.equal(getDefaultExport(), ansiEscapes, 'getDefaultExport must return the default instance');

  // 2. cursorTo behavior and validation
  assert.equal(typeof ansiEscapes.cursorTo, 'function', 'cursorTo must be a function');
  assert.equal(ansiEscapes.cursorTo(0, 0), '\u001B[1;1H', 'cursorTo(0, 0) should position at row 1 col 1');
  assert.equal(ansiEscapes.cursorTo(4, 2), '\u001B[3;5H', 'cursorTo(4, 2) should position at row 3 col 5');
  assert.equal(ansiEscapes.cursorTo(7), '\u001B[8G', 'cursorTo(7) should position at col 8');
  assert.equal(positionCursor(2, 3), '\u001B[4;3H', 'positionCursor helper matches cursorTo output');

  assert.throws(
    () => ansiEscapes.cursorTo(),
    {
      name: 'TypeError',
      message: 'The `x` argument is required'
    },
    'cursorTo() must throw TypeError when x is missing'
  );

  assert.throws(
    () => ansiEscapes.cursorTo('0'),
    {
      name: 'TypeError',
      message: 'The `x` argument is required'
    },
    'cursorTo("0") must throw TypeError when x is not a number'
  );

  // 3. cursorMove behavior and validation
  assert.equal(typeof ansiEscapes.cursorMove, 'function', 'cursorMove must be a function');
  assert.equal(ansiEscapes.cursorMove(2, 3), '\u001B[2C\u001B[3B', 'cursorMove(2, 3) moves right and down');
  assert.equal(ansiEscapes.cursorMove(-1, -4), '\u001B[1D\u001B[4A', 'cursorMove(-1, -4) moves left and up');
  assert.equal(ansiEscapes.cursorMove(0, 0), '', 'cursorMove(0, 0) returns empty string');
  assert.equal(moveCursor(1, -1), '\u001B[1C\u001B[1A', 'moveCursor helper matches cursorMove output');

  assert.throws(
    () => ansiEscapes.cursorMove(),
    {
      name: 'TypeError',
      message: 'The `x` argument is required'
    },
    'cursorMove() must throw TypeError when x is missing'
  );

  // 4. Directional cursor helpers and constants
  assert.equal(ansiEscapes.cursorUp(), '\u001B[1A', 'cursorUp default is 1');
  assert.equal(ansiEscapes.cursorUp(3), '\u001B[3A', 'cursorUp(3) moves up 3');
  assert.equal(ansiEscapes.cursorDown(), '\u001B[1B', 'cursorDown default is 1');
  assert.equal(ansiEscapes.cursorDown(5), '\u001B[5B', 'cursorDown(5) moves down 5');
  assert.equal(ansiEscapes.cursorForward(2), '\u001B[2C', 'cursorForward(2) moves forward 2');
  assert.equal(ansiEscapes.cursorBackward(2), '\u001B[2D', 'cursorBackward(2) moves backward 2');
  assert.equal(ansiEscapes.cursorLeft, '\u001B[G', 'cursorLeft constant matches');
  assert.equal(ansiEscapes.cursorHide, '\u001B[?25l', 'cursorHide constant matches');
  assert.equal(ansiEscapes.cursorShow, '\u001B[?25h', 'cursorShow constant matches');

  // 5. Erase methods and constants
  assert.equal(typeof ansiEscapes.eraseLines, 'function', 'eraseLines must be a function');
  assert.equal(ansiEscapes.eraseLines(0), '', 'eraseLines(0) returns empty string');
  assert.equal(ansiEscapes.eraseLines(1), '\u001B[2K\u001B[G', 'eraseLines(1) clears line and moves left');
  assert.equal(
    ansiEscapes.eraseLines(2),
    '\u001B[2K\u001B[1A\u001B[2K\u001B[G',
    'eraseLines(2) clears 2 lines with cursor movement'
  );
  assert.equal(eraseLines(1), '\u001B[2K\u001B[G', 'eraseLines helper matches output');
  assert.equal(ansiEscapes.eraseLine, '\u001B[2K', 'eraseLine matches');
  assert.equal(ansiEscapes.eraseEndLine, '\u001B[K', 'eraseEndLine matches');
  assert.equal(ansiEscapes.eraseStartLine, '\u001B[1K', 'eraseStartLine matches');
  assert.equal(ansiEscapes.eraseScreen, '\u001B[2J', 'eraseScreen matches');

  // 6. Clear screen and terminal sequences
  assert.equal(ansiEscapes.clearScreen, '\u001Bc', 'clearScreen escape code matches');
  assert.equal(ansiEscapes.clearTerminal, '\u001B[2J\u001B[3J\u001B[H', 'clearTerminal escape code matches Linux terminal');
  assert.equal(ansiEscapes.beep, '\u0007', 'beep matches BEL');

  // 7. Hyperlink formatting
  assert.equal(typeof ansiEscapes.link, 'function', 'link must be a function');
  const expectedLink = '\u001B]8;;https://example.com\u0007Example Text\u001B]8;;\u0007';
  assert.equal(ansiEscapes.link('Example Text', 'https://example.com'), expectedLink, 'link generates OSC 8 escape code');
  assert.equal(createHyperlink('Example Text', 'https://example.com'), expectedLink, 'createHyperlink helper matches output');

  console.log('All ansi-escapes.default contract tests passed successfully.');
}

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

原始种子者

匿名