CodeSampleX

示例

compact-encoding 3.3.2: c.string

已验证示例 — npm compact-encoding 3.3.2: c.string. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: c.string is exported as a codec object with preencode…

sha256:d5385b6d113f7dfe9becb65cdaa6168cd300f1635cdd335be544944677cb2e69

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify c.string in pkg:npm/compact-encoding@3.3.2
包
符号
  • c.string
环境
node 22.23.2
创建时间
2026-09-02T15:25:05Z

契约

  1. c.string is exported as a codec object with preencode, encode, decode, and fixed
  2. c.string encodes and decodes standard UTF-8 strings
  3. c.string handles empty strings correctly
  4. c.string handles multi-byte unicode characters and emojis
  5. c.string supports manual state preencoding, encoding, and decoding
  6. c.string supports sequential encoding of multiple strings with state tracking
  7. c.string.fixed creates a fixed-length string codec without length prefix
  8. c.string throws an out of bounds error when decoding truncated buffers
  9. c.string.fixed throws an out of bounds error when decoding insufficient byte buffers

文件

  • 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 c.string in pkg:npm/compact-encoding@3.3.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/compact-encoding@3.3.2
Demonstrate these symbols/APIs:
  - c.string

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:4617fbdc69794887bdf1f3cb15ab94abf44b8e4cf00274598f77c3b5819ca4ff","contract":["c.string is exported as a codec object with preencode, encode, decode, and fixed","c.string encodes and decodes standard UTF-8 strings","c.string handles empty strings correctly","c.string handles multi-byte unicode characters and emojis","c.string supports manual state preencoding, encoding, and decoding","c.string supports sequential encoding of multiple strings with state tracking","c.string.fixed creates a fixed-length string codec without length prefix","c.string throws an out of bounds error when decoding truncated buffers","c.string.fixed throws an out of bounds error when decoding insufficient byte buffers"],"goal":"verify c.string in pkg:npm/compact-encoding@3.3.2","kind":"HOW","packages":["pkg:npm/compact-encoding@3.3.2"],"schemaVersion":1,"symbols":["c.string"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/compact-encoding@3.3.2"],"schemaVersion":1,"subject":"pkg:npm/compact-encoding@3.3.2","symbols":["c.string"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-compact-encoding-string",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-compact-encoding-string",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "compact-encoding": "3.3.2"
      }
    },
    "node_modules/b4a": {
      "version": "1.8.1",
      "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.8.1.tgz",
      "integrity": "sha512-aiqre1Nr0B/6DgE2N5vwTc+2/oQZ4Wh1t4NznYY4E00y8LCt6NqdRv81so00oo27D8MVKTpUa/MwUUtBLXCoDw==",
      "license": "Apache-2.0",
      "peerDependencies": {
        "react-native-b4a": "*"
      },
      "peerDependenciesMeta": {
        "react-native-b4a": {
          "optional": true
        }
      }
    },
    "node_modules/compact-encoding": {
      "version": "3.3.2",
      "resolved": "https://registry.npmjs.org/compact-encoding/-/compact-encoding-3.3.2.tgz",
      "integrity": "sha512-bgZ84oVSzjQmVHXBYWON+wQUkdVflrr8fM5+0X6UwQHZyzolsGIkdsjNK3vLLn22dhdASGFz5mS6Lf7rfrPdlg==",
      "license": "Apache-2.0",
      "dependencies": {
        "b4a": "^1.3.0"
      }
    }
  }
}
package.json
{
  "name": "sample-compact-encoding-string",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of c.string in compact-encoding",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "compact-encoding": "3.3.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify c.string in pkg:npm/compact-encoding@3.3.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/compact-encoding@3.3.2"
  ],
  "symbols": [
    "c.string"
  ]
}
src/index.js
'use strict';

const c = require('compact-encoding');

/**
 * Encode a string using compact-encoding string codec.
 * @param {string} value - string to encode
 * @returns {Buffer|Uint8Array} encoded buffer
 */
function encodeString(value) {
  return c.encode(c.string, value);
}

/**
 * Decode a string from binary buffer using compact-encoding string codec.
 * @param {Buffer|Uint8Array} buffer - binary buffer to decode
 * @returns {string} decoded string
 */
function decodeString(buffer) {
  return c.decode(c.string, buffer);
}

/**
 * Create a fixed-length string codec.
 * @param {number} length - byte length of the string
 * @returns {object} codec
 */
function createFixedStringCodec(length) {
  return c.string.fixed(length);
}

module.exports = {
  c,
  compactEncoding: c,
  string: c.string,
  encodeString,
  decodeString,
  createFixedStringCodec
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  c,
  compactEncoding,
  string,
  encodeString,
  decodeString,
  createFixedStringCodec
} = require('../src/index.js');

// 1. c.string is exported as a codec object with preencode, encode, decode, and fixed
assert.equal(typeof string, 'object', 'string codec must be exported as an object');
assert.equal(typeof string.preencode, 'function', 'string.preencode must be a function');
assert.equal(typeof string.encode, 'function', 'string.encode must be a function');
assert.equal(typeof string.decode, 'function', 'string.decode must be a function');
assert.equal(typeof string.fixed, 'function', 'string.fixed must be a function');
assert.equal(typeof c.string, 'object', 'c.string must match exported string');
assert.equal(typeof encodeString, 'function', 'encodeString helper must be a function');
assert.equal(typeof decodeString, 'function', 'decodeString helper must be a function');
assert.equal(typeof createFixedStringCodec, 'function', 'createFixedStringCodec helper must be a function');

// 2. c.string encodes and decodes standard UTF-8 strings
{
  const expected = 'Hello, CodeSampleX clean-room verification!';
  const encoded = encodeString(expected);
  const decoded = decodeString(encoded);
  assert.equal(decoded, expected, 'Decoded string must match original standard ASCII/UTF-8 string');
}

// 3. c.string handles empty strings correctly
{
  const empty = '';
  const encoded = encodeString(empty);
  assert.equal(encoded.length, 1, 'Empty string should encode to a single length prefix byte (0)');
  const decoded = decodeString(encoded);
  assert.equal(decoded, empty, 'Decoded empty string must match empty string');
}

// 4. c.string handles multi-byte unicode characters and emojis
{
  const unicodeText = 'Hello 🌍 세계 🚀 Привет 🦔';
  const encoded = encodeString(unicodeText);
  const decoded = decodeString(encoded);
  assert.equal(decoded, unicodeText, 'Decoded string must match unicode string with emojis');
}

// 5. c.string supports manual state preencoding, encoding, and decoding
{
  const text = 'state-based manual buffer encoding';
  const state = c.state();
  string.preencode(state, text);
  assert.ok(state.end > 0, 'State end pointer must advance after preencode');
  state.buffer = Buffer.allocUnsafe(state.end);
  string.encode(state, text);
  assert.equal(state.start, state.end, 'State start pointer must equal end pointer after encoding');
  state.start = 0;
  const decoded = string.decode(state);
  assert.equal(decoded, text, 'Decoded string from manual state must match original');
}

// 6. c.string supports sequential encoding of multiple strings with state tracking
{
  const state = c.state();
  const first = 'first segment';
  const second = 'second segment';
  string.preencode(state, first);
  string.preencode(state, second);
  state.buffer = Buffer.allocUnsafe(state.end);
  string.encode(state, first);
  string.encode(state, second);
  state.start = 0;
  const decodedFirst = string.decode(state);
  const decodedSecond = string.decode(state);
  assert.equal(decodedFirst, first, 'First sequentially decoded string must match');
  assert.equal(decodedSecond, second, 'Second sequentially decoded string must match');
  assert.equal(state.start, state.end, 'State start pointer must reach end after sequential decoding');
}

// 7. c.string.fixed creates a fixed-length string codec without length prefix
{
  const fixedLen = 11;
  const fixedCodec = createFixedStringCodec(fixedLen);
  assert.equal(typeof fixedCodec.preencode, 'function', 'fixedCodec.preencode must be a function');
  assert.equal(typeof fixedCodec.encode, 'function', 'fixedCodec.encode must be a function');
  assert.equal(typeof fixedCodec.decode, 'function', 'fixedCodec.decode must be a function');

  const text = 'fixedlength';
  const encoded = c.encode(fixedCodec, text);
  assert.equal(encoded.length, fixedLen, 'Fixed string encoding must not include a length prefix byte');
  const decoded = c.decode(fixedCodec, encoded);
  assert.equal(decoded, text, 'Decoded fixed-length string must match original');
}

// 8. c.string throws an out of bounds error when decoding truncated buffers
{
  const truncated = Buffer.from([10, 0x41]);
  assert.throws(
    () => decodeString(truncated),
    /Out of bounds/,
    'c.string must throw Out of bounds error on truncated buffer'
  );
}

// 9. c.string.fixed throws an out of bounds error when decoding insufficient byte buffers
{
  const fixedCodec = createFixedStringCodec(10);
  const truncated = Buffer.from([0x41, 0x42, 0x43]);
  assert.throws(
    () => c.decode(fixedCodec, truncated),
    /Out of bounds/,
    'c.string.fixed must throw Out of bounds error on buffer shorter than fixed length'
  );
}

console.log('Contract assertions passed.');

原始种子者

匿名