CodeSampleX

Пример

base64-js 1.5.1

Проверенный пример — npm base64-js 1.5.1. Контракт выполнен на node 22 · linux alpine/x64 · docker и пройден: byteLength calculates byte count for unpadded…

sha256:77c714b0e375b3c21567e693954d8f1ac48ac78306263c7090798534fe824abf

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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/base64-js@1.5.1
Пакеты
Окружение
node 22.23.2
Создан
2026-08-26T07:37:45Z

Контракт

  1. byteLength calculates byte count for unpadded, single-padded, and double-padded base64 strings
  2. toByteArray decodes standard base64 strings into Uint8Array with exact byte values
  3. toByteArray decodes URL-safe base64 strings containing '-' and '_' characters
  4. fromByteArray encodes Uint8Array byte sequences into standard base64 strings
  5. fromByteArray and toByteArray round-trip binary data without corruption

Файлы

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

Скачать артефакт с исходным кодом (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/base64-js@1.5.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/base64-js@1.5.1
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:8ddabc5749953138b1d924d9d3267e1fd6848df6f455fb413047e904e620bc33","constraints":{"executionContext":"node"},"contract":["byteLength calculates byte count for unpadded, single-padded, and double-padded base64 strings","toByteArray decodes standard base64 strings into Uint8Array with exact byte values","toByteArray decodes URL-safe base64 strings containing '-' and '_' characters","fromByteArray encodes Uint8Array byte sequences into standard base64 strings","fromByteArray and toByteArray round-trip binary data without corruption"],"goal":"verify pkg:npm/base64-js@1.5.1","kind":"HOW","packages":["pkg:npm/base64-js@1.5.1"],"schemaVersion":1},"contractCommand":["node","test/contract.js"],"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/base64-js@1.5.1"],"schemaVersion":1,"subject":"pkg:npm/base64-js@1.5.1","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-base64-js",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-base64-js",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "base64-js": "1.5.1"
      }
    },
    "node_modules/base64-js": {
      "version": "1.5.1",
      "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
      "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-base64-js",
  "version": "1.0.0",
  "description": "verify pkg:npm/base64-js@1.5.1",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "base64-js": "1.5.1"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/base64-js@1.5.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/base64-js@1.5.1"
  ],
  "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 base64 = require('base64-js');

/**
 * Calculate the byte length of a base64 string.
 * @param {string} b64 - Base64 encoded string
 * @returns {number} Decoded byte length
 */
function getByteLength(b64) {
  return base64.byteLength(b64);
}

/**
 * Decode a base64 string into a Uint8Array.
 * @param {string} b64 - Base64 encoded string
 * @returns {Uint8Array} Decoded bytes
 */
function decode(b64) {
  return base64.toByteArray(b64);
}

/**
 * Encode a Uint8Array into a base64 string.
 * @param {Uint8Array} uint8 - Byte array
 * @returns {string} Base64 encoded string
 */
function encode(uint8) {
  return base64.fromByteArray(uint8);
}

module.exports = {
  base64,
  getByteLength,
  decode,
  encode
};
test/contract.js
'use strict';

const assert = require('assert');
const base64 = require('base64-js');
const { getByteLength, decode, encode } = require('../src/index');

// 1. Export verification
{
  assert.strictEqual(typeof base64.byteLength, 'function');
  assert.strictEqual(typeof base64.toByteArray, 'function');
  assert.strictEqual(typeof base64.fromByteArray, 'function');
  assert.strictEqual(typeof getByteLength, 'function');
  assert.strictEqual(typeof decode, 'function');
  assert.strictEqual(typeof encode, 'function');
}

// 2. byteLength calculates byte count for unpadded, single-padded, and double-padded base64 strings
{
  assert.strictEqual(base64.byteLength(''), 0);
  // 'Zg==' (f) -> 1 byte (double padded)
  assert.strictEqual(base64.byteLength('Zg=='), 1);
  assert.strictEqual(getByteLength('Zg=='), 1);
  // 'Zm8=' (fo) -> 2 bytes (single padded)
  assert.strictEqual(base64.byteLength('Zm8='), 2);
  assert.strictEqual(getByteLength('Zm8='), 2);
  // 'Zm9v' (foo) -> 3 bytes (no padding)
  assert.strictEqual(base64.byteLength('Zm9v'), 3);
  assert.strictEqual(getByteLength('Zm9v'), 3);
  // 'SGVsbG8sIFdvcmxkIQ==' -> 13 bytes
  assert.strictEqual(base64.byteLength('SGVsbG8sIFdvcmxkIQ=='), 13);
}

// 3. toByteArray decodes standard base64 strings into Uint8Array with exact byte values
{
  const empty = decode('');
  assert.ok(empty instanceof Uint8Array);
  assert.strictEqual(empty.length, 0);

  const singleByte = decode('Zg==');
  assert.deepStrictEqual(Array.from(singleByte), [102]); // 'f' ASCII

  const twoBytes = decode('Zm8=');
  assert.deepStrictEqual(Array.from(twoBytes), [102, 111]); // 'fo' ASCII

  const threeBytes = decode('Zm9v');
  assert.deepStrictEqual(Array.from(threeBytes), [102, 111, 111]); // 'foo' ASCII

  const hello = decode('SGVsbG8sIFdvcmxkIQ==');
  assert.strictEqual(Buffer.from(hello).toString('utf8'), 'Hello, World!');
}

// 4. toByteArray decodes URL-safe base64 strings containing '-' and '_' characters
{
  // Test bytes with high bits producing + / or - _
  // 0xfb, 0xff, 0xbf -> standard "+/+/"; url-safe "-_-_"
  const standard = decode('+/+/');
  const urlSafe = decode('-_-_');
  assert.deepStrictEqual(Array.from(standard), [0xfb, 0xff, 0xbf]);
  assert.deepStrictEqual(Array.from(urlSafe), [0xfb, 0xff, 0xbf]);
}

// 5. fromByteArray encodes Uint8Array byte sequences into standard base64 strings
{
  assert.strictEqual(encode(new Uint8Array([])), '');
  assert.strictEqual(encode(new Uint8Array([102])), 'Zg==');
  assert.strictEqual(encode(new Uint8Array([102, 111])), 'Zm8=');
  assert.strictEqual(encode(new Uint8Array([102, 111, 111])), 'Zm9v');
  assert.strictEqual(encode(Buffer.from('Hello, World!', 'utf8')), 'SGVsbG8sIFdvcmxkIQ==');
  assert.strictEqual(encode(new Uint8Array([0, 1, 2, 255])), 'AAEC/w==');
}

// 6. fromByteArray and toByteArray round-trip binary data without corruption
{
  const testBytes = new Uint8Array(256);
  for (let i = 0; i < 256; i++) {
    testBytes[i] = i;
  }
  const encoded = encode(testBytes);
  const decoded = decode(encoded);
  assert.strictEqual(decoded.length, 256);
  assert.deepStrictEqual(Array.from(decoded), Array.from(testBytes));
}

console.log('All base64-js contract tests passed.');

Исходный сидер

аноним