CodeSampleX

Пример

pvutils 1.2.0: fromBase64

Проверенный пример — npm pvutils 1.2.0: fromBase64. Контракт выполнен на node 22 · linux debian/x64 · docker и пройден: fromBase64 decodes standard Base64…

sha256:803c3ba97793181cf374ab3f8fca39c05d0a364097c737f2a6b7e6a80d62070f

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
1
Ключи подписи, собравшие его
1
Заявленная среда linux 24 · ubuntu · glibc 2.39 x64 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-05

Кейс

HOW
Цель
verify pvutils.fromBase64 in pkg:npm/pvutils@1.2.0
Пакеты
Символы
  • pvutils.fromBase64
Создан
2026-09-05T19:31:26Z

Контракт

  1. fromBase64 decodes standard Base64 encoded strings into binary strings
  2. fromBase64 returns an empty string when given an empty string input
  3. fromBase64 performs round-trip decoding of strings encoded with toBase64
  4. fromBase64 supports URL-safe Base64 decoding with hyphen and underscore characters when useUrlTemplate is true
  5. fromBase64 preserves trailing zero bytes in decoded output when cutTailZeros is false
  6. fromBase64 strips trailing zero bytes in decoded output when cutTailZeros is true
  7. fromBase64 decodes binary byte sequences spanning the full 8-bit range

Файлы

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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 pvutils.fromBase64 in pkg:npm/pvutils@1.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/pvutils@1.2.0
Demonstrate these symbols/APIs:
  - pvutils.fromBase64

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:201627440c3013ba7bbe91c2b99a385d6ce63eccfc44d8765407c4adeaa90fb7","contract":["fromBase64 decodes standard Base64 encoded strings into binary strings","fromBase64 returns an empty string when given an empty string input","fromBase64 performs round-trip decoding of strings encoded with toBase64","fromBase64 supports URL-safe Base64 decoding with hyphen and underscore characters when useUrlTemplate is true","fromBase64 preserves trailing zero bytes in decoded output when cutTailZeros is false","fromBase64 strips trailing zero bytes in decoded output when cutTailZeros is true","fromBase64 decodes binary byte sequences spanning the full 8-bit range"],"goal":"verify pvutils.fromBase64 in pkg:npm/pvutils@1.2.0","kind":"HOW","packages":["pkg:npm/pvutils@1.2.0"],"schemaVersion":1,"symbols":["pvutils.fromBase64"]},"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/pvutils@1.2.0"],"schemaVersion":1,"subject":"pkg:npm/pvutils@1.2.0","symbols":["pvutils.fromBase64"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-pvutils-frombase64",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-pvutils-frombase64",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "pvutils": "1.2.0"
      }
    },
    "node_modules/pvutils": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/pvutils/-/pvutils-1.2.0.tgz",
      "integrity": "sha512-BbubeCEyTuQjVMakvJQ/Sxbc93F2pwmbsxONT/ZRrwU7Ua38d8unYTwXpTVLAKJ4BDuH9IGztCjQcd/N/39Dvg==",
      "license": "MIT",
      "engines": {
        "node": ">=16.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-pvutils-frombase64",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "description": "verify pvutils.fromBase64 in pkg:npm/pvutils@1.2.0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "pvutils": "1.2.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pvutils.fromBase64 in pkg:npm/pvutils@1.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/pvutils@1.2.0"
  ],
  "symbols": [
    "pvutils.fromBase64"
  ]
}
src/index.mjs
import { fromBase64, toBase64 } from 'pvutils';

/**
 * Decode a base64 encoded string to a binary string.
 * @param {string} input - Base64 encoded input string
 * @param {boolean} [useUrlTemplate=false] - Whether to use URL-safe base64 alphabet (- and _)
 * @param {boolean} [cutTailZeros=false] - Whether to trim trailing zero/null bytes
 * @returns {string} Decoded binary string
 */
export function decodeBase64(input, useUrlTemplate = false, cutTailZeros = false) {
  return fromBase64(input, useUrlTemplate, cutTailZeros);
}

/**
 * Encode a binary string to base64 string.
 * @param {string} input - Input string
 * @param {boolean} [useUrlTemplate=false] - Whether to use URL-safe base64 alphabet
 * @param {boolean} [skipPadding=false] - Whether to omit trailing padding '='
 * @param {boolean} [skipLeadingZeros=false] - Whether to skip leading zero bytes
 * @returns {string} Base64 encoded string
 */
export function encodeBase64(input, useUrlTemplate = false, skipPadding = false, skipLeadingZeros = false) {
  return toBase64(input, useUrlTemplate, skipPadding, skipLeadingZeros);
}

export { fromBase64, toBase64 };
export default fromBase64;
test/contract.mjs
import assert from 'node:assert/strict';
import { fromBase64, toBase64, decodeBase64, encodeBase64 } from '../src/index.mjs';

// 1. fromBase64 decodes standard Base64 encoded strings into binary strings
{
  const encoded = 'SGVsbG8sIFdvcmxkIQ==';
  const decoded = fromBase64(encoded);
  assert.equal(decoded, 'Hello, World!');

  const helperDecoded = decodeBase64(encoded);
  assert.equal(helperDecoded, 'Hello, World!');
}

// 2. fromBase64 returns an empty string when given an empty string input
{
  assert.equal(fromBase64(''), '');
  assert.equal(decodeBase64(''), '');
}

// 3. fromBase64 performs round-trip decoding of strings encoded with toBase64
{
  const original = 'The quick brown fox jumps over the lazy dog';
  const encoded = toBase64(original);
  assert.equal(fromBase64(encoded), original);

  const helperEncoded = encodeBase64(original);
  assert.equal(decodeBase64(helperEncoded), original);
}

// 4. fromBase64 supports URL-safe Base64 decoding with hyphen and underscore characters when useUrlTemplate is true
{
  const urlSafe = 'c3ViamVjdHM_X2Q=';
  const decodedUrl = fromBase64(urlSafe, true);
  assert.equal(decodedUrl, 'subjects?_d');

  const helperDecodedUrl = decodeBase64(urlSafe, true);
  assert.equal(helperDecodedUrl, 'subjects?_d');

  const roundTripUrl = encodeBase64('subjects?_d', true);
  assert.equal(roundTripUrl.includes('_') || roundTripUrl.includes('-'), true);
  assert.equal(decodeBase64(roundTripUrl, true), 'subjects?_d');
}

// 5. fromBase64 preserves trailing zero bytes in decoded output when cutTailZeros is false
{
  const withZeros = toBase64('data\x00\x00\x00');
  const preserved = fromBase64(withZeros, false, false);
  assert.equal(preserved, 'data\x00\x00\x00');
  assert.equal(preserved.length, 7);

  const helperPreserved = decodeBase64(withZeros, false, false);
  assert.equal(helperPreserved, 'data\x00\x00\x00');
}

// 6. fromBase64 strips trailing zero bytes in decoded output when cutTailZeros is true
{
  const withZeros = toBase64('data\x00\x00\x00');
  const trimmed = fromBase64(withZeros, false, true);
  assert.equal(trimmed, 'data');
  assert.equal(trimmed.length, 4);

  const helperTrimmed = decodeBase64(withZeros, false, true);
  assert.equal(helperTrimmed, 'data');

  const allZeros = toBase64('\x00\x00');
  assert.equal(fromBase64(allZeros, false, true), '');
}

// 7. fromBase64 decodes binary byte sequences spanning the full 8-bit range
{
  const binaryPayload = String.fromCharCode(0, 1, 64, 127, 128, 200, 255);
  const encodedBin = toBase64(binaryPayload);
  const decodedBin = fromBase64(encodedBin);
  assert.equal(decodedBin, binaryPayload);
}

console.log('All pvutils.fromBase64 contract assertions passed.');

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

аноним