CodeSampleX

示例

b4a 1.8.1: b4a buffer allocation, encoding conversion, and slicing

已验证示例 — npm b4a 1.8.1: b4a buffer allocation, encoding conversion, and slicing. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.

sha256:d2269b48ae6b2b6ed88ad22120992e7dc667b0ce273dd6c53c5b1d0068ad7f16

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
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-23

案例

HOW
目标
b4a buffer allocation, encoding conversion, and slicing
包
符号
  • b4a.alloc
  • b4a.concat
  • b4a.from
  • b4a.toString
创建时间
2026-08-23T15:55:56Z

契约

  1. assert b4a.alloc allocates zero-filled buffer of requested byte length
  2. assert b4a.from creates buffer from string with utf8, hex, and base64 encodings
  3. assert b4a.toString decodes buffer into utf8, hex, and base64 strings
  4. assert b4a.concat joins multiple buffer chunks into a single contiguous buffer

文件

  • 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: b4a buffer allocation, encoding conversion, and slicing
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/b4a@1.8.1
Demonstrate these symbols/APIs:
  - b4a.alloc
  - b4a.concat
  - b4a.from
  - b4a.toString

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:dd4e46ad4a90d7fac24b0987244ca3804caaff9e1f2f2f70b5916425cecdf6f3","contract":["assert b4a.alloc allocates zero-filled buffer of requested byte length","assert b4a.from creates buffer from string with utf8, hex, and base64 encodings","assert b4a.toString decodes buffer into utf8, hex, and base64 strings","assert b4a.concat joins multiple buffer chunks into a single contiguous buffer"],"goal":"b4a buffer allocation, encoding conversion, and slicing","kind":"HOW","packages":["pkg:npm/b4a@1.8.1"],"schemaVersion":1,"symbols":["b4a.alloc","b4a.concat","b4a.from","b4a.toString"]},"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/b4a@1.8.1"],"schemaVersion":1,"subject":"pkg:npm/b4a@1.8.1","symbols":["b4a.alloc","b4a.concat","b4a.from","b4a.toString"],"verifierAdapter":"node-typescript@1"}
index.js
const b4a = require('b4a');

/**
 * Allocates a zero-filled buffer of the specified byte size.
 *
 * @param {number} size - Number of bytes to allocate.
 * @returns {Uint8Array} Zero-initialized buffer.
 */
function allocateBuffer(size) {
  return b4a.alloc(size);
}

/**
 * Converts input string or array into a buffer with specified encoding.
 *
 * @param {string|Array<number>|Uint8Array} input - Data to convert.
 * @param {string} [encoding='utf8'] - Encoding name ('utf8', 'hex', 'base64', etc.).
 * @returns {Uint8Array} Created buffer.
 */
function createBuffer(input, encoding) {
  return b4a.from(input, encoding);
}

/**
 * Decodes buffer into a string using specified encoding.
 *
 * @param {Uint8Array} buf - Buffer to convert.
 * @param {string} [encoding='utf8'] - Target string encoding.
 * @returns {string} Decoded string.
 */
function bufferToString(buf, encoding) {
  return b4a.toString(buf, encoding);
}

/**
 * Concatenates multiple buffer chunks into a single buffer.
 *
 * @param {Array<Uint8Array>} chunks - Array of buffer chunks.
 * @param {number} [totalLength] - Total byte length of resulting buffer.
 * @returns {Uint8Array} Concatenated buffer.
 */
function concatBuffers(chunks, totalLength) {
  return b4a.concat(chunks, totalLength);
}

module.exports = {
  allocateBuffer,
  createBuffer,
  bufferToString,
  concatBuffers,
  b4a,
};
package-lock.json
{
  "name": "sample-b4a-buffer-operations",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-b4a-buffer-operations",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "b4a": "1.8.1"
      }
    },
    "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
        }
      }
    }
  }
}
package.json
{
  "name": "sample-b4a-buffer-operations",
  "version": "1.0.0",
  "description": "Clean-room sample verifying b4a buffer allocation, encoding conversion, and slicing",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "b4a": "1.8.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "b4a buffer allocation, encoding conversion, and slicing",
  "kind": "HOW",
  "packages": [
    "pkg:npm/b4a@1.8.1"
  ],
  "symbols": [
    "b4a.alloc",
    "b4a.concat",
    "b4a.from",
    "b4a.toString"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import b4a from 'b4a';
import {
  allocateBuffer,
  createBuffer,
  bufferToString,
  concatBuffers,
} from '../index.js';

// 1. Assert b4a.alloc allocates zero-filled buffer of requested byte length
const zeroBuf = allocateBuffer(8);
assert.strictEqual(zeroBuf.byteLength, 8, 'alloc must return buffer with matching byte length');
assert.strictEqual(b4a.isBuffer(zeroBuf), true, 'alloc must return valid buffer');
for (let i = 0; i < zeroBuf.length; i++) {
  assert.strictEqual(zeroBuf[i], 0, 'alloc buffer elements must be initialized to zero');
}

// 2. Assert b4a.from creates buffer from string with utf8, hex, and base64 encodings
const utf8Buf = createBuffer('Hello b4a', 'utf8');
assert.deepStrictEqual(
  Array.from(utf8Buf),
  [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x62, 0x34, 0x61],
  'from must parse utf8 string bytes'
);

const hexBuf = createBuffer('68656c6c6f', 'hex');
assert.deepStrictEqual(
  Array.from(hexBuf),
  [0x68, 0x65, 0x6c, 0x6c, 0x6f],
  'from must decode hex string'
);

const base64Buf = createBuffer('aGVsbG8gd29ybGQ=', 'base64');
assert.deepStrictEqual(
  Array.from(base64Buf),
  [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64],
  'from must decode base64 string'
);

// 3. Assert b4a.toString decodes buffer into utf8, hex, and base64 strings
const testBuf = createBuffer('CodeSampleX');
assert.strictEqual(bufferToString(testBuf, 'utf8'), 'CodeSampleX', 'toString utf8 must decode accurately');
assert.strictEqual(bufferToString(testBuf, 'hex'), '436f646553616d706c6558', 'toString hex must decode accurately');
assert.strictEqual(bufferToString(testBuf, 'base64'), 'Q29kZVNhbXBsZVg=', 'toString base64 must decode accurately');

// 4. Assert b4a.concat joins multiple buffer chunks into a single contiguous buffer
const chunk1 = createBuffer('Fast ');
const chunk2 = createBuffer('Universal ');
const chunk3 = createBuffer('Buffer');
const combined = concatBuffers([chunk1, chunk2, chunk3]);
assert.strictEqual(bufferToString(combined, 'utf8'), 'Fast Universal Buffer', 'concat must combine all chunks in order');
assert.strictEqual(combined.byteLength, chunk1.byteLength + chunk2.byteLength + chunk3.byteLength, 'concat byteLength must match sum');

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

原始种子者

匿名