CodeSampleX

Exemplo

bare-buffer 3.7.0: Buffer.alloc

Amostra verificada para npm bare-buffer 3.7.0: Buffer.alloc. O contrato rodou em node 22 · linux alpine/x64 · docker e passou.

sha256:f791713018704e96e8e97dfe5b923a87b94d8456c98d46bbdcc05f655da61bfd

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
verify Buffer.alloc in pkg:npm/bare-buffer@3.7.0
Pacotes
Símbolos
  • Buffer.alloc
Ambiente
node 22.23.2
Criado
2026-08-27T05:23:12Z

Contrato

  1. Buffer.alloc allocates a zero-filled buffer of the specified length
  2. Buffer.alloc with numeric fill populates all bytes with the masked byte value
  3. Buffer.alloc with single-char string fill repeats the character byte across the buffer
  4. Buffer.alloc with multi-char string fill repeats pattern across the buffer
  5. Buffer.alloc with hex string fill decodes and repeats hex bytes across the buffer
  6. Buffer.alloc with Buffer instance fill repeats the source buffer content
  7. Buffer.alloc with size 0 returns an empty buffer of length 0
  8. Buffer.alloc throws RangeError for negative sizes or sizes exceeding MAX_LENGTH

Arquivos

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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.alloc 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.alloc

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:b620791c03897ecf710020e42d78336911bd66cea0d3a48e7faf5653b6a6b62f","constraints":{"executionContext":"node"},"contract":["Buffer.alloc allocates a zero-filled buffer of the specified length","Buffer.alloc with numeric fill populates all bytes with the masked byte value","Buffer.alloc with single-char string fill repeats the character byte across the buffer","Buffer.alloc with multi-char string fill repeats pattern across the buffer","Buffer.alloc with hex string fill decodes and repeats hex bytes across the buffer","Buffer.alloc with Buffer instance fill repeats the source buffer content","Buffer.alloc with size 0 returns an empty buffer of length 0","Buffer.alloc throws RangeError for negative sizes or sizes exceeding MAX_LENGTH"],"goal":"verify Buffer.alloc in pkg:npm/bare-buffer@3.7.0","kind":"HOW","packages":["pkg:npm/bare-buffer@3.7.0"],"schemaVersion":1,"symbols":["Buffer.alloc"]},"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/bare-buffer@3.7.0"],"schemaVersion":1,"subject":"pkg:npm/bare-buffer@3.7.0","symbols":["Buffer.alloc"],"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 allocateZeroed(size) {
  return Buffer.alloc(size);
}

export function allocateWithByte(size, fillByte) {
  return Buffer.alloc(size, fillByte);
}

export function allocateWithString(size, fillString, encoding = 'utf8') {
  return Buffer.alloc(size, fillString, encoding);
}

export function allocateWithHex(size, hexString) {
  return Buffer.alloc(size, hexString, 'hex');
}

export default Buffer;
package-lock.json
{
  "name": "sample-bare-buffer-alloc",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-buffer-alloc",
      "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-alloc",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "bare-buffer": "3.7.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify Buffer.alloc in pkg:npm/bare-buffer@3.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-buffer@3.7.0"
  ],
  "symbols": [
    "Buffer.alloc"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import Buffer, {
  allocateZeroed,
  allocateWithByte,
  allocateWithString,
  allocateWithHex,
} from '../index.js';

// 1. Buffer.alloc allocates a zero-filled buffer of the specified length
const zeroBuf = allocateZeroed(8);
assert.strictEqual(zeroBuf.length, 8, 'Allocated buffer must have requested length');
assert.strictEqual(zeroBuf.byteLength, 8, 'byteLength must match length');
assert.deepStrictEqual(Array.from(zeroBuf), [0, 0, 0, 0, 0, 0, 0, 0], 'Default allocated buffer must be zero-filled');

// 2. Buffer.alloc with numeric fill populates all bytes with the masked byte value
const byteBuf = allocateWithByte(5, 0xab);
assert.strictEqual(byteBuf.length, 5, 'Numeric filled buffer length must be 5');
assert.deepStrictEqual(Array.from(byteBuf), [0xab, 0xab, 0xab, 0xab, 0xab], 'All bytes must equal fill byte');

// 3. Buffer.alloc with single-char string fill repeats the character byte across the buffer
const charBuf = allocateWithString(4, 'x', 'utf8');
assert.strictEqual(charBuf.length, 4, 'Char filled buffer length must be 4');
assert.deepStrictEqual(Array.from(charBuf), [0x78, 0x78, 0x78, 0x78], 'All bytes must equal character ASCII value (0x78 for x)');

// 4. Buffer.alloc with multi-char string fill repeats pattern across the buffer
const patternBuf = allocateWithString(7, 'abc', 'utf8');
assert.strictEqual(patternBuf.length, 7, 'Pattern filled buffer length must be 7');
assert.deepStrictEqual(Array.from(patternBuf), [0x61, 0x62, 0x63, 0x61, 0x62, 0x63, 0x61], 'Pattern must repeat across buffer length');

// 5. Buffer.alloc with hex string fill decodes and repeats hex bytes across the buffer
const hexBuf = allocateWithHex(6, 'deadbeef');
assert.strictEqual(hexBuf.length, 6, 'Hex filled buffer length must be 6');
assert.deepStrictEqual(Array.from(hexBuf), [0xde, 0xad, 0xbe, 0xef, 0xde, 0xad], 'Hex sequence must repeat to fill buffer');

// 6. Buffer.alloc with Buffer instance fill repeats the source buffer content
const fillPattern = Buffer.from('xy');
const fromBuf = Buffer.alloc(5, fillPattern);
assert.strictEqual(fromBuf.length, 5, 'Buffer-filled buffer length must be 5');
assert.deepStrictEqual(Array.from(fromBuf), [0x78, 0x79, 0x78, 0x79, 0x78], 'Buffer pattern must repeat across buffer');

// 7. Buffer.alloc with size 0 returns an empty buffer of length 0
const emptyBuf = allocateZeroed(0);
assert.strictEqual(emptyBuf.length, 0, 'Zero-size allocation must have length 0');
assert.deepStrictEqual(Array.from(emptyBuf), [], 'Zero-size allocation must be empty');

// 8. Buffer.alloc throws RangeError for negative sizes or sizes exceeding MAX_LENGTH
assert.throws(() => {
  Buffer.alloc(-1);
}, { name: 'RangeError' }, 'Negative size must throw RangeError');

assert.throws(() => {
  Buffer.alloc(Buffer.constants.MAX_LENGTH + 1);
}, { name: 'RangeError' }, 'Size exceeding MAX_LENGTH must throw RangeError');

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

Seeder de origem

anônimo