CodeSampleX

示例

safe-buffer 5.1.2

已验证示例 — npm safe-buffer 5.1.2. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: Buffer.alloc allocates zero-filled buffer of requested size or filled…

sha256:a843debe1e0809f76992637a25306ee12b01ba1098dce6b6357faca4116a6c1f

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

执行证据

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

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
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-23
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-07

案例

HOW
目标
verify pkg:npm/safe-buffer@5.1.2
包
环境
node 22.23.2
创建时间
2026-08-23T07:35:27Z

契约

  1. Buffer.alloc allocates zero-filled buffer of requested size or filled with specified value
  2. Buffer.from creates buffer from string with specified encoding
  3. Buffer.from creates buffer from array of byte values
  4. Buffer.from creates buffer copy from existing buffer or arrayBuffer slice
  5. Buffer.allocUnsafe allocates uninitialized buffer of requested size
  6. Buffer.concat joins multiple buffer instances into a single buffer
  7. Buffer.isBuffer correctly identifies buffer instances and rejects non-buffers
  8. Buffer.byteLength accurately measures string byte lengths for various encodings
  9. SlowBuffer creates un-pooled buffer instance of requested size

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 pkg:npm/safe-buffer@5.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/safe-buffer@5.1.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:95a762b237a9553587d2a2607c85fb36d47124f1b006483c0550041e2f04c37f","contract":["Buffer.alloc allocates zero-filled buffer of requested size or filled with specified value","Buffer.from creates buffer from string with specified encoding","Buffer.from creates buffer from array of byte values","Buffer.from creates buffer copy from existing buffer or arrayBuffer slice","Buffer.allocUnsafe allocates uninitialized buffer of requested size","Buffer.concat joins multiple buffer instances into a single buffer","Buffer.isBuffer correctly identifies buffer instances and rejects non-buffers","Buffer.byteLength accurately measures string byte lengths for various encodings","SlowBuffer creates un-pooled buffer instance of requested size"],"goal":"verify pkg:npm/safe-buffer@5.1.2","kind":"HOW","packages":["pkg:npm/safe-buffer@5.1.2"],"schemaVersion":1},"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/safe-buffer@5.1.2"],"schemaVersion":1,"subject":"pkg:npm/safe-buffer@5.1.2","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-safe-buffer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-safe-buffer",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "safe-buffer": "5.1.2"
      }
    },
    "node_modules/safe-buffer": {
      "version": "5.1.2",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
      "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-safe-buffer",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for safe-buffer@5.1.2",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "safe-buffer": "5.1.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/safe-buffer@5.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/safe-buffer@5.1.2"
  ]
}
src/index.js
const { Buffer, SlowBuffer } = require('safe-buffer');

/**
 * Creates a zero-filled or custom-filled Buffer of the specified size.
 *
 * @param {number} size - Number of bytes to allocate.
 * @param {string|Buffer|number} [fill=0] - Value to pre-fill the buffer with.
 * @param {string} [encoding='utf8'] - The encoding if fill is a string.
 * @returns {Buffer} The allocated Buffer instance.
 */
function createAllocatedBuffer(size, fill, encoding) {
  return Buffer.alloc(size, fill, encoding);
}

/**
 * Creates a Buffer from a string, array, ArrayBuffer, or existing Buffer.
 *
 * @param {string|Array|ArrayBuffer|Buffer} value - The input value to copy into a Buffer.
 * @param {string|number} [encodingOrOffset] - Encoding (if string) or offset (if ArrayBuffer).
 * @param {number} [length] - Length (if ArrayBuffer).
 * @returns {Buffer} The newly created Buffer instance.
 */
function createBufferFrom(value, encodingOrOffset, length) {
  return Buffer.from(value, encodingOrOffset, length);
}

/**
 * Creates an uninitialized Buffer of the specified size.
 *
 * @param {number} size - Number of bytes to allocate.
 * @returns {Buffer} The allocated Buffer instance.
 */
function createUnsafeBuffer(size) {
  return Buffer.allocUnsafe(size);
}

/**
 * Concatenates a list of Buffer instances into a single Buffer.
 *
 * @param {Buffer[]} list - Array of Buffer instances to concatenate.
 * @param {number} [totalLength] - Total length of the concatenated Buffer.
 * @returns {Buffer} The concatenated Buffer.
 */
function concatBuffers(list, totalLength) {
  return Buffer.concat(list, totalLength);
}

/**
 * Checks whether an object is a Buffer.
 *
 * @param {*} value - The value to test.
 * @returns {boolean} True if the value is a Buffer, false otherwise.
 */
function checkIsBuffer(value) {
  return Buffer.isBuffer(value);
}

module.exports = {
  Buffer,
  SlowBuffer,
  createAllocatedBuffer,
  createBufferFrom,
  createUnsafeBuffer,
  concatBuffers,
  checkIsBuffer
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  Buffer,
  SlowBuffer,
  createAllocatedBuffer,
  createBufferFrom,
  createUnsafeBuffer,
  concatBuffers,
  checkIsBuffer
} = require('../src/index.js');

async function testAll() {
  // 1. Buffer.alloc allocates zero-filled buffer of requested size or filled with specified value
  const zeroBuf = createAllocatedBuffer(5);
  assert.equal(zeroBuf.length, 5, 'Buffer.alloc length must equal requested size');
  assert.deepEqual(Array.from(zeroBuf), [0, 0, 0, 0, 0], 'Buffer.alloc default fill must be 0');

  const filledBuf = createAllocatedBuffer(4, 0xa5);
  assert.equal(filledBuf.length, 4);
  assert.deepEqual(Array.from(filledBuf), [0xa5, 0xa5, 0xa5, 0xa5], 'Buffer.alloc must fill with specified byte');

  const stringFilledBuf = createAllocatedBuffer(6, 'ab', 'utf8');
  assert.equal(stringFilledBuf.toString('utf8'), 'ababab', 'Buffer.alloc must fill with repeated pattern');

  // 2. Buffer.from creates buffer from string with specified encoding
  const utf8Buf = createBufferFrom('hello world', 'utf8');
  assert.equal(utf8Buf.toString('utf8'), 'hello world');
  assert.equal(utf8Buf.length, 11);

  const hexBuf = createBufferFrom('deadbeef', 'hex');
  assert.equal(hexBuf.length, 4);
  assert.deepEqual(Array.from(hexBuf), [0xde, 0xad, 0xbe, 0xef]);

  const base64Buf = createBufferFrom('aGVsbG8=', 'base64');
  assert.equal(base64Buf.toString('utf8'), 'hello');

  // 3. Buffer.from creates buffer from array of byte values
  const arrayBuf = createBufferFrom([10, 20, 30, 40]);
  assert.equal(arrayBuf.length, 4);
  assert.equal(arrayBuf[0], 10);
  assert.equal(arrayBuf[3], 40);

  // 4. Buffer.from creates buffer copy from existing buffer or arrayBuffer slice
  const sourceBuf = createBufferFrom('original');
  const copyBuf = createBufferFrom(sourceBuf);
  assert.equal(copyBuf.toString(), 'original');
  copyBuf[0] = 0x58; // 'X'
  assert.equal(sourceBuf.toString(), 'original', 'copied buffer modification must not mutate source');

  const ab = new ArrayBuffer(8);
  const u8View = new Uint8Array(ab);
  u8View.set([1, 2, 3, 4, 5, 6, 7, 8]);
  const slicedFromAb = createBufferFrom(ab, 2, 4);
  assert.equal(slicedFromAb.length, 4);
  assert.deepEqual(Array.from(slicedFromAb), [3, 4, 5, 6]);

  // 5. Buffer.allocUnsafe allocates uninitialized buffer of requested size
  const unsafeBuf = createUnsafeBuffer(16);
  assert.equal(unsafeBuf.length, 16);
  assert.ok(checkIsBuffer(unsafeBuf));

  // 6. Buffer.concat joins multiple buffer instances into a single buffer
  const b1 = createBufferFrom('part1-');
  const b2 = createBufferFrom('part2');
  const merged = concatBuffers([b1, b2]);
  assert.equal(merged.toString('utf8'), 'part1-part2');
  assert.equal(merged.length, 11);

  const truncatedConcat = concatBuffers([b1, b2], 5);
  assert.equal(truncatedConcat.length, 5);
  assert.equal(truncatedConcat.toString('utf8'), 'part1');

  // 7. Buffer.isBuffer correctly identifies buffer instances and rejects non-buffers
  assert.equal(checkIsBuffer(createAllocatedBuffer(1)), true);
  assert.equal(checkIsBuffer(createBufferFrom('abc')), true);
  assert.equal(checkIsBuffer(new Uint8Array(4)), false);
  assert.equal(checkIsBuffer('string'), false);
  assert.equal(checkIsBuffer({}), false);
  assert.equal(checkIsBuffer(null), false);
  assert.equal(checkIsBuffer(undefined), false);

  // 8. Buffer.byteLength accurately measures string byte lengths for various encodings
  assert.equal(Buffer.byteLength('hello', 'utf8'), 5);
  assert.equal(Buffer.byteLength('안녕하세요', 'utf8'), 15);
  assert.equal(Buffer.byteLength('deadbeef', 'hex'), 4);
  assert.equal(Buffer.byteLength('aGVsbG8=', 'base64'), 5);

  // 9. SlowBuffer creates un-pooled buffer instance of requested size
  const slow = new SlowBuffer(8);
  assert.equal(slow.length, 8);
  assert.ok(checkIsBuffer(slow));

  console.log('All safe-buffer contract tests passed successfully.');
}

testAll().catch((err) => {
  console.error('Contract test failure:', err);
  process.exit(1);
});

原始种子者

匿名