CodeSampleX

示例

compact-encoding 3.3.2: c.decode

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

sha256:030633ff9dd643d7b4695e7a30029f83ed272e85a4fac49547f58c2f3c2ce32b

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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.decode in pkg:npm/compact-encoding@3.3.2
包
符号
  • c.decode
环境
node 22.23.2
创建时间
2026-09-02T15:21:41Z

契约

  1. c.decode is exported as a function
  2. c.decode decodes UTF-8 strings from binary buffers
  3. c.decode decodes compact variable-length unsigned integers
  4. c.decode decodes fixed-size integers and signed integers
  5. c.decode decodes boolean values
  6. c.decode decodes arrays using composite array codecs
  7. c.decode decodes JSON objects from binary buffers
  8. c.decode decodes prefixed and raw binary buffers
  9. c.decode decodes structured custom records with multi-field codecs
  10. c.decode decodes from Uint8Array instances
  11. c.decode throws an out of bounds error when buffer length is insufficient

文件

  • 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.decode 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.decode

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:59a97621c25985b0ca1eb0fe44e0c174e83b1da6227cb53f28769f9a2082a93e","contract":["c.decode is exported as a function","c.decode decodes UTF-8 strings from binary buffers","c.decode decodes compact variable-length unsigned integers","c.decode decodes fixed-size integers and signed integers","c.decode decodes boolean values","c.decode decodes arrays using composite array codecs","c.decode decodes JSON objects from binary buffers","c.decode decodes prefixed and raw binary buffers","c.decode decodes structured custom records with multi-field codecs","c.decode decodes from Uint8Array instances","c.decode throws an out of bounds error when buffer length is insufficient"],"goal":"verify c.decode in pkg:npm/compact-encoding@3.3.2","kind":"HOW","packages":["pkg:npm/compact-encoding@3.3.2"],"schemaVersion":1,"symbols":["c.decode"]},"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.decode"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-compact-encoding",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-compact-encoding",
      "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",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of compact-encoding",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "compact-encoding": "3.3.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify c.decode in pkg:npm/compact-encoding@3.3.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/compact-encoding@3.3.2"
  ],
  "symbols": [
    "c.decode"
  ]
}
src/index.js
'use strict';

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

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

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

module.exports = {
  c,
  compactEncoding: c,
  decode,
  encode,
  state: c.state,
  string: c.string,
  uint: c.uint,
  uint8: c.uint8,
  uint16: c.uint16,
  uint32: c.uint32,
  int: c.int,
  int8: c.int8,
  bool: c.bool,
  buffer: c.buffer,
  raw: c.raw,
  json: c.json,
  array: c.array,
  frame: c.frame
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  c,
  decode,
  encode,
  state,
  string,
  uint,
  uint8,
  uint16,
  uint32,
  int,
  int8,
  bool,
  buffer,
  raw,
  json,
  array,
  frame
} = require('../src/index.js');

// 1. Export verification
assert.equal(typeof c.decode, 'function', 'c.decode must be exported as a function');
assert.equal(typeof decode, 'function', 'decode wrapper must be exported as a function');
assert.equal(typeof encode, 'function', 'encode wrapper must be exported as a function');
assert.equal(typeof state, 'function', 'state helper must be exported as a function');

// 2. c.decode decodes UTF-8 strings from binary buffers
{
  const expected = 'compact encoding verification';
  const encoded = encode(string, expected);
  const result = decode(string, encoded);
  assert.equal(result, expected);
}

// 3. c.decode decodes compact variable-length unsigned integers
{
  const testValues = [0, 42, 252, 253, 65535, 65536, 4294967295];
  for (const val of testValues) {
    const encoded = encode(uint, val);
    const result = decode(uint, encoded);
    assert.equal(result, val);
  }
}

// 4. c.decode decodes fixed-size integers and signed integers
{
  assert.equal(decode(uint8, encode(uint8, 255)), 255);
  assert.equal(decode(uint16, encode(uint16, 60000)), 60000);
  assert.equal(decode(uint32, encode(uint32, 3000000000)), 3000000000);
  assert.equal(decode(int8, encode(int8, -128)), -128);
  assert.equal(decode(int, encode(int, -50000)), -50000);
}

// 5. c.decode decodes boolean values
{
  assert.equal(decode(bool, encode(bool, true)), true);
  assert.equal(decode(bool, encode(bool, false)), false);
}

// 6. c.decode decodes arrays using composite array codecs
{
  const list = ['first', 'second', 'third'];
  const listCodec = array(string);
  const encoded = encode(listCodec, list);
  const result = decode(listCodec, encoded);
  assert.deepEqual(result, list);
}

// 7. c.decode decodes JSON objects from binary buffers
{
  const payload = { id: 101, tags: ['alpha', 'beta'], active: true };
  const encoded = encode(json, payload);
  const result = decode(json, encoded);
  assert.deepEqual(result, payload);
}

// 8. c.decode decodes prefixed and raw binary buffers
{
  const sampleData = Buffer.from([0xaa, 0xbb, 0xcc, 0xdd]);
  const encodedPrefixed = encode(buffer, sampleData);
  const decodedPrefixed = decode(buffer, encodedPrefixed);
  assert.deepEqual(decodedPrefixed, sampleData);

  const encodedRaw = encode(raw, sampleData);
  const decodedRaw = decode(raw, encodedRaw);
  assert.deepEqual(decodedRaw, sampleData);
}

// 9. c.decode decodes structured custom records with multi-field codecs
{
  const recordCodec = {
    preencode(stateObj, rec) {
      string.preencode(stateObj, rec.title);
      uint.preencode(stateObj, rec.count);
      bool.preencode(stateObj, rec.verified);
    },
    encode(stateObj, rec) {
      string.encode(stateObj, rec.title);
      uint.encode(stateObj, rec.count);
      bool.encode(stateObj, rec.verified);
    },
    decode(stateObj) {
      return {
        title: string.decode(stateObj),
        count: uint.decode(stateObj),
        verified: bool.decode(stateObj)
      };
    }
  };

  const record = { title: 'Sample Spec', count: 42, verified: true };
  const encoded = encode(recordCodec, record);
  const result = decode(recordCodec, encoded);
  assert.deepEqual(result, record);
}

// 10. c.decode decodes from Uint8Array instances
{
  const text = 'typed array payload';
  const encodedBuf = encode(string, text);
  const uint8View = new Uint8Array(encodedBuf.buffer, encodedBuf.byteOffset, encodedBuf.byteLength);
  const result = decode(string, uint8View);
  assert.equal(result, text);
}

// 11. c.decode throws an out of bounds error when buffer length is insufficient
{
  const truncatedBuffer = Buffer.alloc(1);
  assert.throws(
    () => decode(uint32, truncatedBuffer),
    /Out of bounds/
  );
}

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

原始种子者

匿名