CodeSampleX

Ejemplo

compact-encoding 3.3.2: c.encode

Muestra verificada para npm compact-encoding 3.3.2: c.encode. El contrato se ejecutó en node 22 · linux debian/x64 · docker y pasó.

sha256:125e56ab8222164888548daf51943ae648cfcc6693f4bd1c748a7acc9d8cc785

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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

Caso

HOW
Objetivo
verify c.encode in pkg:npm/compact-encoding@3.3.2
Paquetes
Símbolos
  • c.encode
Creado
2026-09-02T15:41:49Z

Contrato

  1. c.encode encodes uint numbers into compact binary buffers using c.uint
  2. c.encode encodes utf8 strings with length prefix into binary buffers using c.string
  3. c.encode encodes boolean values into single-byte buffers using c.bool
  4. c.encode serializes array elements with length prefix using composite c.array encoding
  5. c.encode serializes custom structured records when provided an object with preencode and encode methods
  6. c.encode throws an error when attempting to encode numbers outside the safe unsigned integer range

Archivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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.encode 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.encode

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:187fab42cba0284b6e6d6148e1d7bbac577315f68f3fc80c6e7d2d943809ebae","contract":["c.encode encodes uint numbers into compact binary buffers using c.uint","c.encode encodes utf8 strings with length prefix into binary buffers using c.string","c.encode encodes boolean values into single-byte buffers using c.bool","c.encode serializes array elements with length prefix using composite c.array encoding","c.encode serializes custom structured records when provided an object with preencode and encode methods","c.encode throws an error when attempting to encode numbers outside the safe unsigned integer range"],"goal":"verify c.encode in pkg:npm/compact-encoding@3.3.2","kind":"HOW","packages":["pkg:npm/compact-encoding@3.3.2"],"schemaVersion":1,"symbols":["c.encode"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","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.encode"],"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",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "compact-encoding": "3.3.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify c.encode in pkg:npm/compact-encoding@3.3.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/compact-encoding@3.3.2"
  ],
  "symbols": [
    "c.encode"
  ]
}
src/index.mjs
import c from 'compact-encoding';

/**
 * Encode a primitive or composite value using a compact-encoding type descriptor.
 * @param {object} enc - Encoding descriptor with preencode and encode methods
 * @param {*} value - Value to encode
 * @returns {Uint8Array}
 */
export function encodeValue(enc, value) {
  return c.encode(enc, value);
}

/**
 * Encode an unsigned integer using c.encode and c.uint.
 * @param {number} value
 * @returns {Uint8Array}
 */
export function encodeUint(value) {
  return c.encode(c.uint, value);
}

/**
 * Encode a string using c.encode and c.string.
 * @param {string} value
 * @returns {Uint8Array}
 */
export function encodeString(value) {
  return c.encode(c.string, value);
}

/**
 * Encode a boolean using c.encode and c.bool.
 * @param {boolean} value
 * @returns {Uint8Array}
 */
export function encodeBool(value) {
  return c.encode(c.bool, value);
}

/**
 * Encode an array of values using c.encode and c.array.
 * @param {object} itemEncoding - Encoding descriptor for array elements
 * @param {Array<*>} list - Array of elements to encode
 * @returns {Uint8Array}
 */
export function encodeList(itemEncoding, list) {
  return c.encode(c.array(itemEncoding), list);
}

/**
 * Encode a structured object using a custom encoder definition.
 * @param {{ id: number, tag: string }} item
 * @returns {Uint8Array}
 */
export function encodeRecord(item) {
  const recordEncoding = {
    preencode(state, val) {
      c.uint.preencode(state, val.id);
      c.string.preencode(state, val.tag);
    },
    encode(state, val) {
      c.uint.encode(state, val.id);
      c.string.encode(state, val.tag);
    },
    decode(state) {
      return {
        id: c.uint.decode(state),
        tag: c.string.decode(state)
      };
    }
  };
  return c.encode(recordEncoding, item);
}

export default c;
test/contract.mjs
import assert from 'node:assert/strict';
import c, {
  encodeValue,
  encodeUint,
  encodeString,
  encodeBool,
  encodeList,
  encodeRecord
} from '../src/index.mjs';

// 1. c.encode encodes uint numbers into compact binary buffers using c.uint
{
  const values = [0, 1, 100, 252, 253, 65535, 4294967295];
  for (const val of values) {
    const buf = c.encode(c.uint, val);
    assert(buf instanceof Uint8Array);
    assert(buf.byteLength > 0);
    const decoded = c.decode(c.uint, buf);
    assert.equal(decoded, val);

    const helperBuf = encodeUint(val);
    assert.deepEqual(helperBuf, buf);
  }
}

// 2. c.encode encodes utf8 strings with length prefix into binary buffers using c.string
{
  const strings = ['', 'hello', 'CodeSampleX', 'UTF-8 🚀⚡'];
  for (const str of strings) {
    const buf = c.encode(c.string, str);
    assert(buf instanceof Uint8Array);
    const decoded = c.decode(c.string, buf);
    assert.equal(decoded, str);

    const helperBuf = encodeString(str);
    assert.deepEqual(helperBuf, buf);
  }
}

// 3. c.encode encodes boolean values into single-byte buffers using c.bool
{
  const trueBuf = c.encode(c.bool, true);
  const falseBuf = c.encode(c.bool, false);
  assert.equal(trueBuf.byteLength, 1);
  assert.equal(falseBuf.byteLength, 1);
  assert.equal(c.decode(c.bool, trueBuf), true);
  assert.equal(c.decode(c.bool, falseBuf), false);

  assert.deepEqual(encodeBool(true), trueBuf);
  assert.deepEqual(encodeBool(false), falseBuf);
}

// 4. c.encode serializes array elements with length prefix using composite c.array encoding
{
  const numbers = [10, 20, 30, 40];
  const buf = c.encode(c.array(c.uint), numbers);
  assert(buf instanceof Uint8Array);
  const decoded = c.decode(c.array(c.uint), buf);
  assert.deepEqual(decoded, numbers);

  const helperBuf = encodeList(c.uint, numbers);
  assert.deepEqual(helperBuf, buf);
}

// 5. c.encode serializes custom structured records when provided an object with preencode and encode methods
{
  const item = { id: 101, tag: 'verified' };
  const customEncoding = {
    preencode(state, val) {
      c.uint.preencode(state, val.id);
      c.string.preencode(state, val.tag);
    },
    encode(state, val) {
      c.uint.encode(state, val.id);
      c.string.encode(state, val.tag);
    },
    decode(state) {
      return {
        id: c.uint.decode(state),
        tag: c.string.decode(state)
      };
    }
  };

  const directBuf = c.encode(customEncoding, item);
  assert(directBuf instanceof Uint8Array);
  assert.deepEqual(c.decode(customEncoding, directBuf), item);

  const helperBuf = encodeRecord(item);
  assert.deepEqual(helperBuf, directBuf);
  assert.deepEqual(c.decode(customEncoding, helperBuf), item);
}

// 6. c.encode throws an error when attempting to encode numbers outside the safe unsigned integer range
{
  assert.throws(() => {
    c.encode(c.uint, -1);
  }, /uint must be between 0 and 9007199254740991/);

  assert.throws(() => {
    c.encode(c.uint, Number.MAX_SAFE_INTEGER + 1);
  }, /uint must be between 0 and 9007199254740991/);
}

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

Seeder de origen

anónimo