CodeSampleX

Пример

bare-buffer 3.7.0: Buffer.prototype.copy

Проверенный пример — npm bare-buffer 3.7.0: Buffer.prototype.copy. Контракт выполнен на node 22 · linux alpine/x64 · docker и пройден.

sha256:5e276b67722d035e1f424addf40f326b0e5ee4b3f366692436f049aa563c09ac

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
1
Ключи подписи, собравшие его
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-27

Кейс

HOW
Цель
verify Buffer.prototype.copy in pkg:npm/bare-buffer@3.7.0
Пакеты
Символы
  • Buffer.prototype.copy
Окружение
node 22.23.2
Создан
2026-08-27T13:15:37Z

Контракт

  1. Buffer.prototype.copy copies full byte content from source buffer to target buffer and returns bytes copied
  2. Buffer.prototype.copy respects targetStart offset when writing into target buffer
  3. Buffer.prototype.copy copies a slice of source buffer using sourceStart and sourceEnd offsets
  4. Buffer.prototype.copy truncates copied bytes when target remaining capacity is smaller than source range
  5. Buffer.prototype.copy performs in-place overlapping memory copy when source and target are the same buffer
  6. Buffer.prototype.copy returns 0 without modification when sourceEnd is less than or equal to sourceStart
  7. Buffer.prototype.copy returns 0 when targetStart exceeds target buffer length

Файлы

  • 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: verify Buffer.prototype.copy in pkg:npm/bare-buffer@3.7.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/bare-buffer@3.7.0
Demonstrate these symbols/APIs:
  - Buffer.prototype.copy

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:89adca9e92cf06b5d57ab4708ace73889f8a6669b4715e97ac61667e1845d332","contract":["Buffer.prototype.copy copies full byte content from source buffer to target buffer and returns bytes copied","Buffer.prototype.copy respects targetStart offset when writing into target buffer","Buffer.prototype.copy copies a slice of source buffer using sourceStart and sourceEnd offsets","Buffer.prototype.copy truncates copied bytes when target remaining capacity is smaller than source range","Buffer.prototype.copy performs in-place overlapping memory copy when source and target are the same buffer","Buffer.prototype.copy returns 0 without modification when sourceEnd is less than or equal to sourceStart","Buffer.prototype.copy returns 0 when targetStart exceeds target buffer length"],"goal":"verify Buffer.prototype.copy in pkg:npm/bare-buffer@3.7.0","kind":"HOW","packages":["pkg:npm/bare-buffer@3.7.0"],"schemaVersion":1,"symbols":["Buffer.prototype.copy"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","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/bare-buffer@3.7.0"],"schemaVersion":1,"subject":"pkg:npm/bare-buffer@3.7.0","symbols":["Buffer.prototype.copy"],"verifierAdapter":"node-typescript@1"}
index.js
import { createRequire } from 'node:module';
import path from 'node:path';

const require = createRequire(import.meta.url);

// Ensure binding is available in Node.js environments where require.addon is absent
if (typeof Bare === 'undefined') {
  const nodeBuffer = globalThis.Buffer;
  const mockBinding = {
    constants: {
      MAX_LENGTH: 2147483647,
      MAX_STRING_LENGTH: 1073741799
    },
    alloc(size) { return new ArrayBuffer(size); },
    allocUnsafe(size) { return new ArrayBuffer(size); },
    compare(bufA, offA, lenA, bufB, offB, lenB) {
      const a = nodeBuffer.from(bufA, offA, lenA);
      const b = nodeBuffer.from(bufB, offB, lenB);
      return a.compare(b);
    },
    toStringHex(buf, offset, len) {
      return nodeBuffer.from(buf, offset, len).toString('hex');
    },
    writeHex(buf, offset, len, string) {
      return nodeBuffer.from(buf, offset, len).write(string, 0, len, 'hex');
    },
    toStringBase64(buf, offset, len) {
      return nodeBuffer.from(buf, offset, len).toString('base64');
    },
    writeBase64(buf, offset, len, string) {
      return nodeBuffer.from(buf, offset, len).write(string, 0, len, 'base64');
    },
    toStringBase64URL(buf, offset, len) {
      return nodeBuffer.from(buf, offset, len).toString('base64url');
    },
    writeBase64URL(buf, offset, len, string) {
      return nodeBuffer.from(buf, offset, len).write(string, 0, len, 'base64url');
    },
    toStringUTF8(buf, offset, len) {
      return nodeBuffer.from(buf, offset, len).toString('utf8');
    },
    writeUTF8(buf, offset, len, string) {
      return nodeBuffer.from(buf, offset, len).write(string, 0, len, 'utf8');
    },
    validateUTF8(buf, offset, len) {
      return nodeBuffer.isUtf8 ? nodeBuffer.isUtf8(nodeBuffer.from(buf, offset, len)) : true;
    },
    byteLengthUTF8(string) {
      return nodeBuffer.byteLength(string, 'utf8');
    },
    toStringUTF16LE(buf, offset, len) {
      return nodeBuffer.from(buf, offset, len).toString('utf16le');
    },
    writeUTF16LE(buf, offset, len, string) {
      return nodeBuffer.from(buf, offset, len).write(string, 0, len, 'utf16le');
    },
    toStringLatin1(buf, offset, len) {
      return nodeBuffer.from(buf, offset, len).toString('latin1');
    },
    writeLatin1(buf, offset, len, string) {
      return nodeBuffer.from(buf, offset, len).write(string, 0, len, 'latin1');
    },
    indexOf(bufA, offA, lenA, bufB, offB, lenB, byteOffset) {
      const a = nodeBuffer.from(bufA, offA, lenA);
      const b = nodeBuffer.from(bufB, offB, lenB);
      return a.indexOf(b, byteOffset);
    },
    lastIndexOf(bufA, offA, lenA, bufB, offB, lenB, byteOffset) {
      const a = nodeBuffer.from(bufA, offA, lenA);
      const b = nodeBuffer.from(bufB, offB, lenB);
      return a.lastIndexOf(b, byteOffset);
    },
    swap16(buf, offset, len) {
      nodeBuffer.from(buf, offset, len).swap16();
    },
    swap32(buf, offset, len) {
      nodeBuffer.from(buf, offset, len).swap32();
    },
    swap64(buf, offset, len) {
      nodeBuffer.from(buf, offset, len).swap64();
    }
  };

  const mainPath = require.resolve('bare-buffer');
  const bindingPath = path.join(path.dirname(mainPath), 'binding.js');
  require.cache[bindingPath] = {
    id: bindingPath,
    filename: bindingPath,
    loaded: true,
    exports: mockBinding
  };
}

const Buffer = require('bare-buffer');

export function copyBuffer(source, target, targetStart = 0, sourceStart = 0, sourceEnd = source.byteLength) {
  return source.copy(target, targetStart, sourceStart, sourceEnd);
}

export default Buffer;
package-lock.json
{
  "name": "sample-bare-buffer-copy",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-buffer-copy",
      "version": "1.0.0",
      "dependencies": {
        "bare-buffer": "3.7.0"
      }
    },
    "node_modules/bare-buffer": {
      "version": "3.7.0",
      "resolved": "https://registry.npmjs.org/bare-buffer/-/bare-buffer-3.7.0.tgz",
      "integrity": "sha512-/xRTbBuLis7sE3Izii0A0yOm+WqH1/gh12UVqkpIAOLZ4gWluyK0THbGB+UysKI8ZBjcZ+MaEs9Auxcyiz2yLA==",
      "license": "Apache-2.0",
      "engines": {
        "bare": ">=1.20.0"
      }
    }
  }
}
package.json
{
  "name": "sample-bare-buffer-copy",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "bare-buffer": "3.7.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify Buffer.prototype.copy in pkg:npm/bare-buffer@3.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-buffer@3.7.0"
  ],
  "symbols": [
    "Buffer.prototype.copy"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import Buffer, { copyBuffer } from '../index.js';

// 1. Buffer.prototype.copy copies full byte content from source buffer to target buffer and returns bytes copied
const src1 = Buffer.from([1, 2, 3, 4]);
const dst1 = Buffer.alloc(4);
const copied1 = copyBuffer(src1, dst1);
assert.strictEqual(copied1, 4, 'Full copy must return the count of copied bytes (4)');
assert.deepStrictEqual(Array.from(dst1), [1, 2, 3, 4], 'Target buffer content must match source buffer');

// 2. Buffer.prototype.copy respects targetStart offset when writing into target buffer
const src2 = Buffer.from([10, 20]);
const dst2 = Buffer.alloc(5);
const copied2 = copyBuffer(src2, dst2, 2);
assert.strictEqual(copied2, 2, 'Copy with targetStart must return count of copied bytes (2)');
assert.deepStrictEqual(Array.from(dst2), [0, 0, 10, 20, 0], 'Target buffer must contain copied bytes starting at offset 2');

// 3. Buffer.prototype.copy copies a slice of source buffer using sourceStart and sourceEnd offsets
const src3 = Buffer.from([1, 2, 3, 4, 5, 6]);
const dst3 = Buffer.alloc(3);
const copied3 = copyBuffer(src3, dst3, 0, 2, 5);
assert.strictEqual(copied3, 3, 'Sliced copy must return length of source range (3)');
assert.deepStrictEqual(Array.from(dst3), [3, 4, 5], 'Target buffer must contain source bytes from index 2 up to 5');

// 4. Buffer.prototype.copy truncates copied bytes when target remaining capacity is smaller than source range
const src4 = Buffer.from([1, 2, 3, 4, 5]);
const dst4 = Buffer.alloc(3);
const copied4 = copyBuffer(src4, dst4, 1);
assert.strictEqual(copied4, 2, 'Copy must be truncated to remaining capacity of target (2 bytes)');
assert.deepStrictEqual(Array.from(dst4), [0, 1, 2], 'Target buffer must contain truncated bytes starting at targetStart');

// 5. Buffer.prototype.copy performs in-place overlapping memory copy when source and target are the same buffer
const buf5 = Buffer.from([1, 2, 3, 4, 5]);
const copied5 = copyBuffer(buf5, buf5, 1, 0, 3);
assert.strictEqual(copied5, 3, 'In-place copy must return count of copied bytes (3)');
assert.deepStrictEqual(Array.from(buf5), [1, 1, 2, 3, 5], 'Buffer must handle overlapping copy without corrupting intermediate bytes');

// 6. Buffer.prototype.copy returns 0 without modification when sourceEnd is less than or equal to sourceStart
const src6 = Buffer.from([1, 2, 3]);
const dst6 = Buffer.alloc(3);
const copied6 = copyBuffer(src6, dst6, 0, 2, 1);
assert.strictEqual(copied6, 0, 'Copy with sourceEnd <= sourceStart must return 0');
assert.deepStrictEqual(Array.from(dst6), [0, 0, 0], 'Target buffer must remain untouched when copy length is 0');

// 7. Buffer.prototype.copy returns 0 when targetStart exceeds target buffer length
const src7 = Buffer.from([1, 2, 3]);
const dst7 = Buffer.alloc(2);
const copied7 = copyBuffer(src7, dst7, 5);
assert.strictEqual(copied7, 0, 'Copy with out-of-bounds targetStart must return 0');
assert.deepStrictEqual(Array.from(dst7), [0, 0], 'Target buffer must remain untouched when targetStart is out of bounds');

console.log('Contract verification finished successfully.');

Исходный сидер

аноним