CodeSampleX

サンプル

safe-buffer 5.2.1

検証済みサンプル — npm safe-buffer 5.2.1. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: Buffer.alloc allocates zero-filled buffers or buffers…

sha256:fca5a9a66e867024c7735d7f770c4f1528c5f6501a687570219b8ede700cc1b5

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

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20
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-08

ケース

HOW
ゴール
verify pkg:npm/safe-buffer@5.2.1
パッケージ
環境
node 22.23.2
作成日
2026-08-20T10:12:32Z

コントラクト

  1. Buffer.alloc allocates zero-filled buffers or buffers initialized with custom byte/string values
  2. Buffer.allocUnsafe and Buffer.allocUnsafeSlow allocate uninitialized buffer instances
  3. Buffer.from creates buffers from strings, byte arrays, and existing buffer instances
  4. Buffer.concat concatenates multiple buffer instances into a single buffer
  5. Buffer.isBuffer differentiates Buffer instances from non-buffer values
  6. Buffer.isEncoding and Buffer.byteLength inspect valid encodings and byte lengths

ファイル

  • 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/safe-buffer@5.2.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/safe-buffer@5.2.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:60097c577387681fbc650495ed89d9ed39511ac2505b586deedeb65614ae964e","constraints":{"executionContext":"node"},"contract":["Buffer.alloc allocates zero-filled buffers or buffers initialized with custom byte/string values","Buffer.allocUnsafe and Buffer.allocUnsafeSlow allocate uninitialized buffer instances","Buffer.from creates buffers from strings, byte arrays, and existing buffer instances","Buffer.concat concatenates multiple buffer instances into a single buffer","Buffer.isBuffer differentiates Buffer instances from non-buffer values","Buffer.isEncoding and Buffer.byteLength inspect valid encodings and byte lengths"],"goal":"verify pkg:npm/safe-buffer@5.2.1","kind":"HOW","packages":["pkg:npm/safe-buffer@5.2.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/safe-buffer@5.2.1"],"schemaVersion":1,"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "safe-buffer-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "safe-buffer-sample",
      "version": "1.0.0",
      "dependencies": {
        "safe-buffer": "5.2.1"
      }
    },
    "node_modules/safe-buffer": {
      "version": "5.2.1",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
      "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "safe-buffer-sample",
  "version": "1.0.0",
  "dependencies": {
    "safe-buffer": "5.2.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/safe-buffer@5.2.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/safe-buffer@5.2.1"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const { Buffer } = require('safe-buffer');

/**
 * Allocates a safe, zero-filled or custom-filled buffer.
 *
 * @param {number} size
 * @param {string|number|Buffer} [fill=0]
 * @param {BufferEncoding} [encoding='utf8']
 * @returns {Buffer}
 */
function allocateBuffer(size, fill, encoding) {
  return Buffer.alloc(size, fill, encoding);
}

/**
 * Allocates an uninitialized buffer of the specified size.
 *
 * @param {number} size
 * @returns {Buffer}
 */
function allocateUnsafeBuffer(size) {
  return Buffer.allocUnsafe(size);
}

/**
 * Allocates an uninitialized buffer without using the internal buffer pool.
 *
 * @param {number} size
 * @returns {Buffer}
 */
function allocateUnsafeSlowBuffer(size) {
  return Buffer.allocUnsafeSlow(size);
}

/**
 * Creates a buffer from a string, array, ArrayBuffer, or another buffer.
 *
 * @param {string|Array<number>|ArrayBuffer|Buffer} value
 * @param {BufferEncoding} [encoding]
 * @returns {Buffer}
 */
function createBuffer(value, encoding) {
  return Buffer.from(value, encoding);
}

/**
 * Concatenates an array of buffers into a single buffer.
 *
 * @param {Buffer[]} list
 * @param {number} [totalLength]
 * @returns {Buffer}
 */
function concatenateBuffers(list, totalLength) {
  return Buffer.concat(list, totalLength);
}

/**
 * Determines whether the given object is a Buffer instance.
 *
 * @param {unknown} obj
 * @returns {boolean}
 */
function checkIsBuffer(obj) {
  return Buffer.isBuffer(obj);
}

/**
 * Validates if the encoding string is supported.
 *
 * @param {string} encoding
 * @returns {boolean}
 */
function checkIsEncoding(encoding) {
  return Buffer.isEncoding(encoding);
}

/**
 * Computes byte length of a string in the specified encoding.
 *
 * @param {string} str
 * @param {BufferEncoding} [encoding='utf8']
 * @returns {number}
 */
function computeByteLength(str, encoding) {
  return Buffer.byteLength(str, encoding);
}

module.exports = {
  Buffer,
  allocateBuffer,
  allocateUnsafeBuffer,
  allocateUnsafeSlowBuffer,
  createBuffer,
  concatenateBuffers,
  checkIsBuffer,
  checkIsEncoding,
  computeByteLength,
};
test/contract.js
const assert = require('node:assert/strict');
const {
  Buffer,
  allocateBuffer,
  allocateUnsafeBuffer,
  allocateUnsafeSlowBuffer,
  createBuffer,
  concatenateBuffers,
  checkIsBuffer,
  checkIsEncoding,
  computeByteLength,
} = require('../src/index.js');

// Contract 1: Buffer.alloc allocates zero-filled buffers or buffers initialized with custom byte/string values
{
  const zeroBuf = allocateBuffer(10);
  assert.strictEqual(zeroBuf.length, 10);
  for (let i = 0; i < 10; i++) {
    assert.strictEqual(zeroBuf[i], 0);
  }

  const charBuf = allocateBuffer(4, 'a');
  assert.strictEqual(charBuf.toString('utf8'), 'aaaa');

  const byteBuf = allocateBuffer(3, 0x5a);
  assert.deepStrictEqual([...byteBuf], [0x5a, 0x5a, 0x5a]);

  const base64Buf = allocateBuffer(4, 'aGVsbG8=', 'base64');
  assert.strictEqual(base64Buf.length, 4);
}

// Contract 2: Buffer.allocUnsafe and Buffer.allocUnsafeSlow allocate uninitialized buffer instances
{
  const unsafeBuf = allocateUnsafeBuffer(16);
  assert.strictEqual(unsafeBuf.length, 16);
  assert.strictEqual(checkIsBuffer(unsafeBuf), true);

  const slowBuf = allocateUnsafeSlowBuffer(32);
  assert.strictEqual(slowBuf.length, 32);
  assert.strictEqual(checkIsBuffer(slowBuf), true);
}

// Contract 3: Buffer.from creates buffers from strings, byte arrays, and existing buffer instances
{
  const strBuf = createBuffer('hello world', 'utf8');
  assert.strictEqual(strBuf.toString('utf8'), 'hello world');

  const arrBuf = createBuffer([10, 20, 30, 40]);
  assert.strictEqual(arrBuf.length, 4);
  assert.strictEqual(arrBuf[0], 10);
  assert.strictEqual(arrBuf[3], 40);

  const copyBuf = createBuffer(arrBuf);
  assert.deepStrictEqual([...copyBuf], [10, 20, 30, 40]);
  copyBuf[0] = 99;
  assert.strictEqual(arrBuf[0], 10, 'Original buffer should remain unchanged');
}

// Contract 4: Buffer.concat concatenates multiple buffer instances into a single buffer
{
  const part1 = createBuffer('foo');
  const part2 = createBuffer('bar');
  const part3 = createBuffer('baz');

  const combined = concatenateBuffers([part1, part2, part3]);
  assert.strictEqual(combined.toString('utf8'), 'foobarbaz');
  assert.strictEqual(combined.length, 9);

  const truncated = concatenateBuffers([part1, part2, part3], 6);
  assert.strictEqual(truncated.toString('utf8'), 'foobar');
  assert.strictEqual(truncated.length, 6);
}

// Contract 5: Buffer.isBuffer differentiates Buffer instances from non-buffer values
{
  assert.strictEqual(checkIsBuffer(allocateBuffer(5)), true);
  assert.strictEqual(checkIsBuffer(createBuffer('test')), true);
  assert.strictEqual(checkIsBuffer('test'), false);
  assert.strictEqual(checkIsBuffer(123), false);
  assert.strictEqual(checkIsBuffer(null), false);
  assert.strictEqual(checkIsBuffer(undefined), false);
  assert.strictEqual(checkIsBuffer({}), false);
  assert.strictEqual(checkIsBuffer([]), false);
  assert.strictEqual(checkIsBuffer(new Uint8Array(5)), false);
}

// Contract 6: Buffer.isEncoding and Buffer.byteLength inspect valid encodings and byte lengths
{
  assert.strictEqual(checkIsEncoding('utf8'), true);
  assert.strictEqual(checkIsEncoding('hex'), true);
  assert.strictEqual(checkIsEncoding('base64'), true);
  assert.strictEqual(checkIsEncoding('binary'), true);
  assert.strictEqual(checkIsEncoding('invalid_encoding_xyz'), false);

  assert.strictEqual(computeByteLength('hello', 'utf8'), 5);
  assert.strictEqual(computeByteLength('€', 'utf8'), 3);
  assert.strictEqual(computeByteLength('68656c6c6f', 'hex'), 5);
}

console.log('All pkg:npm/safe-buffer@5.2.1 contract tests passed successfully.');

オリジンシーダー

匿名