CodeSampleX

Sample

bare-buffer 3.7.0: Buffer.prototype.swap32

Verified sample for npm bare-buffer 3.7.0: Buffer.prototype.swap32. The contract ran on node 22 · linux alpine/x64 · docker and passed.

sha256:0ea9d6653863b2e822d653a7c3a242c470a528a3ba5e2123aad921282196526a

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Verification-run environments

Environment Contract Stages Run
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

Case

HOW
Goal
verify Buffer.prototype.swap32 in pkg:npm/bare-buffer@3.7.0
Packages
Symbols
  • Buffer.prototype.swap32
Environment
node 22.23.2
Created
2026-08-27T05:39:39Z

Contract

  1. swap32 in-places reverses 4-byte word endianness for single 32-bit values
  2. swap32 reverses endianness across multiple consecutive 32-bit integer chunks
  3. swap32 returns the same buffer instance reference after in-place mutation
  4. swap32 applied twice restores the original byte sequence
  5. swap32 throws RangeError when buffer length is not a multiple of 4 bytes
  6. swap32 successfully processes large buffers exceeding SWAP_MIN_LENGTH threshold
  7. swap32 works on 0-byte empty buffers without error

Files

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

Download the source artifact (tar.gz)

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 Buffer.prototype.swap32 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.swap32

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:4eee177cdc898fbbdee35da0b91adedc9a9564123fb241d05b1ffcf8d6cdc4be","contract":["swap32 in-places reverses 4-byte word endianness for single 32-bit values","swap32 reverses endianness across multiple consecutive 32-bit integer chunks","swap32 returns the same buffer instance reference after in-place mutation","swap32 applied twice restores the original byte sequence","swap32 throws RangeError when buffer length is not a multiple of 4 bytes","swap32 successfully processes large buffers exceeding SWAP_MIN_LENGTH threshold","swap32 works on 0-byte empty buffers without error"],"goal":"verify Buffer.prototype.swap32 in pkg:npm/bare-buffer@3.7.0","kind":"HOW","packages":["pkg:npm/bare-buffer@3.7.0"],"schemaVersion":1,"symbols":["Buffer.prototype.swap32"]},"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.prototype.swap32"],"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 swap32(buf) {
  return buf.swap32();
}

export function createBuffer(input) {
  if (typeof input === 'number') {
    return Buffer.alloc(input);
  }
  return Buffer.from(input);
}

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

// 1. swap32 in-places reverses 4-byte word endianness for single 32-bit values
{
  const buf = Buffer.from([0x12, 0x34, 0x56, 0x78]);
  const result = buf.swap32();
  assert.strictEqual(result, buf, 'swap32 must return the same buffer instance');
  assert.deepStrictEqual(
    Array.from(buf),
    [0x78, 0x56, 0x34, 0x12],
    '4-byte buffer endianness must be reversed (0x12, 0x34, 0x56, 0x78 -> 0x78, 0x56, 0x34, 0x12)'
  );
}

// 2. swap32 reverses endianness across multiple consecutive 32-bit integer chunks
{
  const buf = Buffer.from([
    0x01, 0x02, 0x03, 0x04,
    0x0a, 0x0b, 0x0c, 0x0d,
    0xaa, 0xbb, 0xcc, 0xdd
  ]);
  swap32(buf);
  assert.deepStrictEqual(
    Array.from(buf),
    [
      0x04, 0x03, 0x02, 0x01,
      0x0d, 0x0c, 0x0b, 0x0a,
      0xdd, 0xcc, 0xbb, 0xaa
    ],
    'Each 4-byte segment in multi-chunk buffer must be individually swapped'
  );
}

// 3. swap32 returns the same buffer instance reference after in-place mutation
{
  const buf = Buffer.alloc(8);
  buf[0] = 1;
  buf[3] = 4;
  buf[4] = 5;
  buf[7] = 8;
  const returned = buf.swap32();
  assert.strictEqual(returned, buf, 'swap32 must mutate in-place and return the mutated reference');
  assert.strictEqual(buf[0], 4);
  assert.strictEqual(buf[3], 1);
  assert.strictEqual(buf[4], 8);
  assert.strictEqual(buf[7], 5);
}

// 4. swap32 applied twice restores the original byte sequence
{
  const original = [0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe];
  const buf = Buffer.from(original);
  buf.swap32();
  assert.notDeepStrictEqual(Array.from(buf), original, 'First swap must alter byte order');
  buf.swap32();
  assert.deepStrictEqual(Array.from(buf), original, 'Second swap must restore original byte sequence');
}

// 5. swap32 throws RangeError when buffer length is not a multiple of 4 bytes
{
  const invalidLengths = [1, 2, 3, 5, 6, 7, 9, 11, 15];
  for (const len of invalidLengths) {
    const buf = Buffer.alloc(len);
    assert.throws(
      () => { buf.swap32(); },
      {
        name: 'RangeError',
        message: /Buffer size must be a multiple of 32-bits/
      },
      `swap32 must throw RangeError for buffer length ${len}`
    );
  }
}

// 6. swap32 successfully processes large buffers exceeding SWAP_MIN_LENGTH threshold
{
  const count = 64; // 64 * 4 = 256 bytes (> 128 SWAP_MIN_LENGTH)
  const size = count * 4;
  const buf = Buffer.alloc(size);
  for (let i = 0; i < count; i++) {
    buf[i * 4 + 0] = 0x10;
    buf[i * 4 + 1] = 0x20;
    buf[i * 4 + 2] = 0x30;
    buf[i * 4 + 3] = 0x40;
  }
  buf.swap32();
  for (let i = 0; i < count; i++) {
    assert.strictEqual(buf[i * 4 + 0], 0x40, `byte 0 of chunk ${i} must be 0x40`);
    assert.strictEqual(buf[i * 4 + 1], 0x30, `byte 1 of chunk ${i} must be 0x30`);
    assert.strictEqual(buf[i * 4 + 2], 0x20, `byte 2 of chunk ${i} must be 0x20`);
    assert.strictEqual(buf[i * 4 + 3], 0x10, `byte 3 of chunk ${i} must be 0x10`);
  }
}

// 7. swap32 works on 0-byte empty buffers without error
{
  const empty = Buffer.alloc(0);
  const res = empty.swap32();
  assert.strictEqual(res, empty);
  assert.strictEqual(empty.length, 0);
}

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

Origin Seeder

anonymous