CodeSampleX

示例

buffer 5.7.1: Buffer

已验证示例 — npm buffer 5.7.1: Buffer. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: buffer.Buffer.from creates Buffer instances from utf8 string, hex…

sha256:fcf545ed6e41782df23291367263acb7bf005ec1adb16f3f5d6e91d6a7ed20de

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify buffer.Buffer in pkg:npm/buffer@5.7.1
包
符号
  • buffer.Buffer
创建时间
2026-09-06T12:28:32Z

契约

  1. buffer.Buffer.from creates Buffer instances from utf8 string, hex string, and byte array
  2. buffer.Buffer.alloc allocates fixed-size zero-filled and filled buffers
  3. buffer.Buffer.isBuffer accurately checks for Buffer instances
  4. buffer.Buffer.concat combines multiple buffers into a single buffer
  5. buffer.Buffer.byteLength measures correct byte length across utf8, hex, and base64 encodings
  6. buffer.Buffer instance supports slice, copy, write, and toString encoding conversions
  7. buffer.Buffer.compare compares buffer instances lexicographically
  8. buffer.Buffer throws TypeError or RangeError on invalid constructor and allocation arguments

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • 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 buffer.Buffer in pkg:npm/buffer@5.7.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/buffer@5.7.1
Demonstrate these symbols/APIs:
  - buffer.Buffer

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:ee4cad80291545610dfbe88805a90a430b85f85d00c2ab73a28e5286cf81b6ca","contract":["buffer.Buffer.from creates Buffer instances from utf8 string, hex string, and byte array","buffer.Buffer.alloc allocates fixed-size zero-filled and filled buffers","buffer.Buffer.isBuffer accurately checks for Buffer instances","buffer.Buffer.concat combines multiple buffers into a single buffer","buffer.Buffer.byteLength measures correct byte length across utf8, hex, and base64 encodings","buffer.Buffer instance supports slice, copy, write, and toString encoding conversions","buffer.Buffer.compare compares buffer instances lexicographically","buffer.Buffer throws TypeError or RangeError on invalid constructor and allocation arguments"],"goal":"verify buffer.Buffer in pkg:npm/buffer@5.7.1","kind":"HOW","packages":["pkg:npm/buffer@5.7.1"],"schemaVersion":1,"symbols":["buffer.Buffer"]},"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/buffer@5.7.1"],"schemaVersion":1,"subject":"pkg:npm/buffer@5.7.1","symbols":["buffer.Buffer"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const bufferPkg = require('buffer/');
export const Buffer = bufferPkg.Buffer;
export default bufferPkg;
package-lock.json
{
  "name": "sample-buffer-buffer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-buffer-buffer",
      "version": "1.0.0",
      "dependencies": {
        "buffer": "5.7.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"
    },
    "node_modules/buffer": {
      "version": "5.7.1",
      "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
      "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
      "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",
      "dependencies": {
        "base64-js": "^1.3.1",
        "ieee754": "^1.1.13"
      }
    },
    "node_modules/ieee754": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
      "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
      "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": "BSD-3-Clause"
    }
  }
}
package.json
{
  "name": "sample-buffer-buffer",
  "version": "1.0.0",
  "type": "module",
  "main": "index.mjs",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "buffer": "5.7.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify buffer.Buffer in pkg:npm/buffer@5.7.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/buffer@5.7.1"
  ],
  "symbols": [
    "buffer.Buffer"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const buffer = require('buffer/');
const { Buffer } = buffer;

// 1. buffer.Buffer.from creates Buffer instances from utf8 string, hex string, and byte array
{
  const utf8Buf = Buffer.from('hello world', 'utf8');
  assert.ok(Buffer.isBuffer(utf8Buf));
  assert.equal(utf8Buf.toString('utf8'), 'hello world');
  assert.equal(utf8Buf.length, 11);

  const hexBuf = Buffer.from('68656c6c6f', 'hex');
  assert.ok(Buffer.isBuffer(hexBuf));
  assert.equal(hexBuf.toString('utf8'), 'hello');

  const byteArrBuf = Buffer.from([104, 101, 108, 108, 111]);
  assert.ok(Buffer.isBuffer(byteArrBuf));
  assert.equal(byteArrBuf.toString('utf8'), 'hello');
}

// 2. buffer.Buffer.alloc allocates fixed-size zero-filled and filled buffers
{
  const zeroBuf = Buffer.alloc(8);
  assert.ok(Buffer.isBuffer(zeroBuf));
  assert.equal(zeroBuf.length, 8);
  assert.deepEqual(Array.from(zeroBuf), [0, 0, 0, 0, 0, 0, 0, 0]);

  const filledBuf = Buffer.alloc(4, 0x41);
  assert.equal(filledBuf.toString('utf8'), 'AAAA');
}

// 3. buffer.Buffer.isBuffer accurately checks for Buffer instances
{
  assert.equal(Buffer.isBuffer(Buffer.alloc(4)), true);
  assert.equal(Buffer.isBuffer(Buffer.from('test')), true);
  assert.equal(Buffer.isBuffer(new Uint8Array(4)), false);
  assert.equal(Buffer.isBuffer('not a buffer'), false);
  assert.equal(Buffer.isBuffer(null), false);
  assert.equal(Buffer.isBuffer(undefined), false);
  assert.equal(Buffer.isBuffer({}), false);
}

// 4. buffer.Buffer.concat combines multiple buffers into a single buffer
{
  const part1 = Buffer.from('Hello, ');
  const part2 = Buffer.from('world!');
  const combined = Buffer.concat([part1, part2]);
  assert.ok(Buffer.isBuffer(combined));
  assert.equal(combined.toString('utf8'), 'Hello, world!');
  assert.equal(combined.length, 13);
}

// 5. buffer.Buffer.byteLength measures correct byte length across utf8, hex, and base64 encodings
{
  assert.equal(Buffer.byteLength('hello', 'utf8'), 5);
  assert.equal(Buffer.byteLength('가나다', 'utf8'), 9);
  assert.equal(Buffer.byteLength('68656c6c6f', 'hex'), 5);
  assert.equal(Buffer.byteLength('aGVsbG8=', 'base64'), 5);
}

// 6. buffer.Buffer instance supports slice, copy, write, and toString encoding conversions
{
  const buf = Buffer.alloc(10);
  const written = buf.write('hello', 0, 5, 'utf8');
  assert.equal(written, 5);
  assert.equal(buf.slice(0, 5).toString('utf8'), 'hello');

  const target = Buffer.alloc(5);
  buf.copy(target, 0, 0, 5);
  assert.equal(target.toString('utf8'), 'hello');
  assert.equal(target.toString('hex'), '68656c6c6f');
  assert.equal(target.toString('base64'), 'aGVsbG8=');
}

// 7. buffer.Buffer.compare compares buffer instances lexicographically
{
  const bufA = Buffer.from('apple');
  const bufB = Buffer.from('banana');
  assert.equal(Buffer.compare(bufA, bufB), -1);
  assert.equal(Buffer.compare(bufB, bufA), 1);
  assert.equal(Buffer.compare(bufA, bufA), 0);
  assert.equal(bufA.compare(bufB), -1);
}

// 8. buffer.Buffer throws TypeError or RangeError on invalid constructor and allocation arguments
{
  assert.throws(
    () => Buffer.from(123),
    {
      name: 'TypeError'
    }
  );
  assert.throws(
    () => Buffer.alloc(-1),
    {
      name: 'RangeError'
    }
  );
  assert.throws(
    () => Buffer.alloc('invalid'),
    {
      name: 'TypeError'
    }
  );
  assert.throws(
    () => Buffer.concat('not-an-array'),
    {
      name: 'TypeError'
    }
  );
}

console.log('All buffer.Buffer contract assertions passed.');

原始种子者

匿名