CodeSampleX

Exemple

safer-buffer 2.1.2

Échantillon vérifié pour npm safer-buffer 2.1.2. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:af04ae001bb8b7c1ba4fa0f7e991a32d4dfb607f8d02d6694688be4c503ede36

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

Cas

HOW
Objectif
verify pkg:npm/safer-buffer@2.1.2
Paquets
Environnement
node 22.23.2
Créé
2026-08-20T14:20:29Z

Contrat

  1. Buffer() and new Buffer() throw TypeError to prevent unsafe direct invocation
  2. Buffer.alloc(size) returns a zero-filled Buffer of the requested size
  3. Buffer.alloc(size, fill, encoding) returns a Buffer filled with the specified pattern
  4. Buffer.alloc throws TypeError for non-numeric size and RangeError for negative size
  5. Buffer.from(string, encoding) returns a Buffer containing encoded string bytes
  6. Buffer.from(array) returns a Buffer containing elements from array of bytes
  7. Buffer.from(buffer) creates a copy of an existing Buffer
  8. Buffer.from(number) throws TypeError to avoid allocating uninitialized memory
  9. Buffer.concat(list) concatenates multiple Buffer instances into a single Buffer
  10. Buffer.isBuffer(obj) returns true for Buffer instances and false for other objects

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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/safer-buffer@2.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/safer-buffer@2.1.2
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:46190523cdc28d41c5aa1ab70a80076b78e963063d510448d4b662cf2bc1df5f","constraints":{"executionContext":"node"},"contract":["Buffer() and new Buffer() throw TypeError to prevent unsafe direct invocation","Buffer.alloc(size) returns a zero-filled Buffer of the requested size","Buffer.alloc(size, fill, encoding) returns a Buffer filled with the specified pattern","Buffer.alloc throws TypeError for non-numeric size and RangeError for negative size","Buffer.from(string, encoding) returns a Buffer containing encoded string bytes","Buffer.from(array) returns a Buffer containing elements from array of bytes","Buffer.from(buffer) creates a copy of an existing Buffer","Buffer.from(number) throws TypeError to avoid allocating uninitialized memory","Buffer.concat(list) concatenates multiple Buffer instances into a single Buffer","Buffer.isBuffer(obj) returns true for Buffer instances and false for other objects"],"goal":"verify pkg:npm/safer-buffer@2.1.2","kind":"HOW","packages":["pkg:npm/safer-buffer@2.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/safer-buffer@2.1.2"],"schemaVersion":1,"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-safer-buffer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-safer-buffer",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "safer-buffer": "2.1.2"
      }
    },
    "node_modules/safer-buffer": {
      "version": "2.1.2",
      "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-safer-buffer",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for safer-buffer",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "safer-buffer": "2.1.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/safer-buffer@2.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/safer-buffer@2.1.2"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const safer = require('safer-buffer');

/**
 * Allocate a zero-filled or fill-specified buffer using safer-buffer.
 * @param {number} size - Buffer byte size
 * @param {string|Buffer|number} [fill] - Value to pre-fill buffer with
 * @param {string} [encoding] - Encoding for string fill
 * @returns {Buffer} Allocated buffer
 */
function allocateBuffer(size, fill, encoding) {
  return safer.Buffer.alloc(size, fill, encoding);
}

/**
 * Create a buffer from an array, string, or existing buffer.
 * @param {Array|ArrayBuffer|Buffer|string} value - Source value
 * @param {string|number} [encodingOrOffset] - Encoding or byte offset
 * @param {number} [length] - Length when source is ArrayBuffer
 * @returns {Buffer} Created buffer
 */
function createBufferFrom(value, encodingOrOffset, length) {
  return safer.Buffer.from(value, encodingOrOffset, length);
}

/**
 * Concatenate a list of buffer instances into a single buffer.
 * @param {Buffer[]} list - Array of buffer instances
 * @param {number} [totalLength] - Total byte length
 * @returns {Buffer} Concatenated buffer
 */
function concatBuffers(list, totalLength) {
  return safer.Buffer.concat(list, totalLength);
}

/**
 * Check if the given object is a Buffer instance.
 * @param {*} obj - Object to check
 * @returns {boolean} True if object is a Buffer
 */
function isBufferInstance(obj) {
  return safer.Buffer.isBuffer(obj);
}

module.exports = {
  safer,
  Buffer: safer.Buffer,
  allocateBuffer,
  createBufferFrom,
  concatBuffers,
  isBufferInstance
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  safer,
  Buffer,
  allocateBuffer,
  createBufferFrom,
  concatBuffers,
  isBufferInstance
} = require('../src/index.js');

// 1. Export and safety checks
assert.equal(typeof safer, 'object', 'safer module export must be an object');
assert.equal(typeof Buffer, 'object', 'safer.Buffer must be an object');
assert.equal(typeof allocateBuffer, 'function', 'allocateBuffer must be a function');
assert.equal(typeof createBufferFrom, 'function', 'createBufferFrom must be a function');
assert.equal(typeof concatBuffers, 'function', 'concatBuffers must be a function');
assert.equal(typeof isBufferInstance, 'function', 'isBufferInstance must be a function');

// 2. Direct constructor invocation throws TypeError
assert.throws(() => Buffer(), TypeError, 'Buffer() without new must throw TypeError');
assert.throws(() => Buffer(10), TypeError, 'Buffer(number) without new must throw TypeError');
assert.throws(() => new Buffer(), TypeError, 'new Buffer() must throw TypeError');
assert.throws(() => new Buffer(10), TypeError, 'new Buffer(number) must throw TypeError');

// 3. Buffer.alloc allocates zero-filled buffer of exact length
const zeroBuf = allocateBuffer(16);
assert.equal(isBufferInstance(zeroBuf), true, 'allocated object must be a Buffer');
assert.equal(zeroBuf.length, 16, 'buffer length must match requested size');
for (let i = 0; i < zeroBuf.length; i++) {
  assert.equal(zeroBuf[i], 0, 'allocated buffer must be zero-filled');
}

// 4. Buffer.alloc with custom fill
const filledBuf = allocateBuffer(6, 'xyz', 'utf8');
assert.equal(filledBuf.toString('utf8'), 'xyzxyz', 'buffer should be filled with pattern');

const numericFillBuf = allocateBuffer(4, 0x42);
assert.deepEqual(Array.from(numericFillBuf), [0x42, 0x42, 0x42, 0x42]);

// 5. Buffer.alloc validates size parameter
assert.throws(() => allocateBuffer('invalid'), TypeError, 'Buffer.alloc(string) must throw TypeError');
assert.throws(() => allocateBuffer(-5), RangeError, 'Buffer.alloc(negative) must throw RangeError');
assert.throws(() => allocateBuffer(null), TypeError, 'Buffer.alloc(null) must throw TypeError');

// 6. Buffer.from string with encoding
const strBuf = createBufferFrom('hello world', 'utf8');
assert.equal(isBufferInstance(strBuf), true);
assert.equal(strBuf.toString('utf8'), 'hello world');
assert.equal(strBuf.length, 11);

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

// 7. Buffer.from array of bytes
const arrayBuf = createBufferFrom([0x68, 0x65, 0x6c, 0x6c, 0x6f]);
assert.equal(arrayBuf.toString('utf8'), 'hello');
assert.equal(arrayBuf.length, 5);

// 8. Buffer.from copy of existing buffer
const copyBuf = createBufferFrom(strBuf);
assert.equal(copyBuf.toString('utf8'), 'hello world');
assert.notEqual(copyBuf, strBuf, 'Buffer.from(buffer) should produce a copy');

// 9. Buffer.from number throws TypeError to prevent unsafe uninitialized allocations
assert.throws(() => createBufferFrom(10), TypeError, 'Buffer.from(number) must throw TypeError');
assert.throws(() => createBufferFrom(-1), TypeError, 'Buffer.from(negative) must throw TypeError');
assert.throws(() => createBufferFrom(null), TypeError, 'Buffer.from(null) must throw TypeError');
assert.throws(() => createBufferFrom(undefined), TypeError, 'Buffer.from(undefined) must throw TypeError');

// 10. Buffer.concat combines multiple buffers
const part1 = createBufferFrom('hello ');
const part2 = createBufferFrom('world');
const combined = concatBuffers([part1, part2]);
assert.equal(combined.toString('utf8'), 'hello world');
assert.equal(combined.length, 11);

// 11. Buffer helper methods
assert.equal(Buffer.isEncoding('utf8'), true);
assert.equal(Buffer.isEncoding('invalid-enc'), false);
assert.equal(Buffer.byteLength('hello', 'utf8'), 5);

console.log('Contract tests passed successfully.');

Seeder d'origine

anonyme