CodeSampleX

サンプル

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

検証済みサンプル — npm b4a 1.8.1: b4a buffer allocation, encoding conversion, and slicing. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:d2269b48ae6b2b6ed88ad22120992e7dc667b0ce273dd6c53c5b1d0068ad7f16

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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.');

オリジンシーダー

匿名