CodeSampleX

示例

base64-js 1.5.1: fromByteArray

已验证示例 — npm base64-js 1.5.1: fromByteArray. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert empty Uint8Array encodes to empty base64 string

sha256:36598f94f9cdf6646b5ed9a966915bd58f8ec1f5051f737b681510db42dc051c

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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 fromByteArray in pkg:npm/base64-js@1.5.1
包
符号
  • fromByteArray
环境
node 22.23.2
创建时间
2026-08-26T11:24:37Z

契约

  1. assert empty Uint8Array encodes to empty base64 string
  2. assert single byte encodes with double equals padding
  3. assert two bytes encode with single equals padding
  4. assert three bytes encode without padding
  5. assert text converted to Uint8Array encodes to standard base64 string
  6. assert binary byte sequence with 0x00 and 0xFF encodes correctly
  7. assert large byte buffer encodes correctly and round-trips with toByteArray

文件

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • 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 fromByteArray in pkg:npm/base64-js@1.5.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/base64-js@1.5.1
Demonstrate these symbols/APIs:
  - fromByteArray

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:f4eccb31b2f273dc7183ca6fe99791bf9c69b58b74aaef3aaccbb7bab1c7eafb","contract":["assert empty Uint8Array encodes to empty base64 string","assert single byte encodes with double equals padding","assert two bytes encode with single equals padding","assert three bytes encode without padding","assert text converted to Uint8Array encodes to standard base64 string","assert binary byte sequence with 0x00 and 0xFF encodes correctly","assert large byte buffer encodes correctly and round-trips with toByteArray"],"goal":"verify fromByteArray in pkg:npm/base64-js@1.5.1","kind":"HOW","packages":["pkg:npm/base64-js@1.5.1"],"schemaVersion":1,"symbols":["fromByteArray"]},"contractCommand":["node","test/contract.mjs"],"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","symbols":["fromByteArray"],"verifierAdapter":"node-typescript@1"}
index.js
const { fromByteArray, toByteArray, byteLength } = require('base64-js');

function encodeBytes(uint8Array) {
  return fromByteArray(uint8Array);
}

function encodeText(text) {
  const encoder = new TextEncoder();
  const bytes = encoder.encode(text);
  return fromByteArray(bytes);
}

function decodeToBytes(base64Str) {
  return toByteArray(base64Str);
}

module.exports = {
  fromByteArray,
  toByteArray,
  byteLength,
  encodeBytes,
  encodeText,
  decodeToBytes,
};
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==",
      "funding": [
        {
          "type": "github",
          "url": "https://github.com/sponsors/feross"
        },
        {
          "type": "patreon",
          "url": "https://www.patreon.com/feross"
        },
        {
          "type": "consulting",
          "url": "https://feross.org/support"
        }
      ],
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-base64-js",
  "version": "1.0.0",
  "description": "Minimal clean-room sample verifying fromByteArray in base64-js",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "base64-js": "1.5.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify fromByteArray in pkg:npm/base64-js@1.5.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/base64-js@1.5.1"
  ],
  "symbols": [
    "fromByteArray"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import { fromByteArray, toByteArray, byteLength } from 'base64-js';
import { encodeBytes, encodeText, decodeToBytes } from '../index.js';

// 1. Empty Uint8Array encodes to empty base64 string
const emptyBytes = new Uint8Array([]);
const emptyResult = encodeBytes(emptyBytes);
assert.strictEqual(emptyResult, '', 'Empty byte array must produce empty string');

// 2. Single byte encodes with double equals padding
const singleByte = new Uint8Array([72]); // 'H'
const singleResult = encodeBytes(singleByte);
assert.strictEqual(singleResult, 'SA==', '1 byte must encode with == padding');

// 3. Two bytes encode with single equals padding
const twoBytes = new Uint8Array([72, 101]); // 'He'
const twoResult = encodeBytes(twoBytes);
assert.strictEqual(twoResult, 'SGU=', '2 bytes must encode with = padding');

// 4. Three bytes encode without padding
const threeBytes = new Uint8Array([72, 101, 108]); // 'Hel'
const threeResult = encodeBytes(threeBytes);
assert.strictEqual(threeResult, 'SGVs', '3 bytes (multiple of 3) must encode without padding');

// 5. Text converted to Uint8Array encodes to standard base64 string
const helloWorld = encodeText('Hello, World!');
assert.strictEqual(helloWorld, 'SGVsbG8sIFdvcmxkIQ==', 'Hello, World! must encode accurately');

// 6. Binary byte sequence with 0x00 and 0xFF encodes correctly
const binaryBytes = new Uint8Array([0x00, 0x01, 0x02, 0xFE, 0xFF]);
const binaryResult = encodeBytes(binaryBytes);
assert.strictEqual(binaryResult, 'AAEC/v8=', 'Binary byte sequence must encode correctly');

// 7. Large byte buffer encodes correctly and round-trips with toByteArray
const size = 256;
const buffer = new Uint8Array(size);
for (let i = 0; i < size; i++) {
  buffer[i] = i & 0xff;
}
const encoded = fromByteArray(buffer);
assert.strictEqual(typeof encoded, 'string', 'fromByteArray must return a string');
const decoded = toByteArray(encoded);
assert.strictEqual(decoded.length, size, 'Decoded byte length must match original');
assert.deepStrictEqual(Array.from(decoded), Array.from(buffer), 'Round-trip decoding must preserve byte values exactly');

console.log('All fromByteArray contract assertions passed.');

原始种子者

匿名