CodeSampleX

示例

bare-buffer 3.7.0: Buffer.prototype.swap16

已验证示例 — npm bare-buffer 3.7.0: Buffer.prototype.swap16. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: Buffer.prototype.swap16 swaps 16-bit byte…

sha256:a0d07bc223f84970e43f68fa5c9f7b444073592855a6fbe7a9a786bc7eee0bdc

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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.swap16 in pkg:npm/bare-buffer@3.7.0
包
符号
  • Buffer.prototype.swap16
环境
node 22.23.2
创建时间
2026-08-27T05:38:05Z

契约

  1. Buffer.prototype.swap16 swaps 16-bit byte pairs in-place and returns the same Buffer instance
  2. Buffer.prototype.swap16 called twice restores the original byte sequence
  3. Buffer.prototype.swap16 throws RangeError when buffer byte length is not a multiple of 16 bits
  4. Buffer.prototype.swap16 succeeds on an empty buffer without modification
  5. Buffer.prototype.swap16 operates correctly on buffer subarrays with non-zero byte offsets
  6. Buffer.prototype.swap16 handles buffers of 128 bytes or more using native block swapping

文件

  • 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.swap16 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.swap16

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:62fc98da66ea3c994be81281f57f998578290782807223ae1975921c961f9d1c","contract":["Buffer.prototype.swap16 swaps 16-bit byte pairs in-place and returns the same Buffer instance","Buffer.prototype.swap16 called twice restores the original byte sequence","Buffer.prototype.swap16 throws RangeError when buffer byte length is not a multiple of 16 bits","Buffer.prototype.swap16 succeeds on an empty buffer without modification","Buffer.prototype.swap16 operates correctly on buffer subarrays with non-zero byte offsets","Buffer.prototype.swap16 handles buffers of 128 bytes or more using native block swapping"],"goal":"verify Buffer.prototype.swap16 in pkg:npm/bare-buffer@3.7.0","kind":"HOW","packages":["pkg:npm/bare-buffer@3.7.0"],"schemaVersion":1,"symbols":["Buffer.prototype.swap16"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","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.swap16"],"verifierAdapter":"node-typescript@1"}
index.js
import { createRequire } from 'node:module';
import path from 'node:path';

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

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);
    },
    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 swap16Buffer(buffer) {
  return buffer.swap16();
}

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

// 1. Buffer.prototype.swap16 swaps 16-bit byte pairs in-place and returns the same Buffer instance
const buf1 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
const returned1 = buf1.swap16();
assert.strictEqual(returned1, buf1, 'swap16 should return the same buffer instance');
assert.deepStrictEqual(Array.from(buf1), [0x02, 0x01, 0x04, 0x03, 0x06, 0x05], '16-bit byte pairs should be swapped');

// 2. Buffer.prototype.swap16 called twice restores the original byte sequence
const original = [0xaa, 0xbb, 0xcc, 0xdd];
const buf2 = Buffer.from(original);
buf2.swap16().swap16();
assert.deepStrictEqual(Array.from(buf2), original, 'swapping twice should restore original byte sequence');

// 3. Buffer.prototype.swap16 throws RangeError when buffer byte length is not a multiple of 16 bits
const oddBuf = Buffer.from([0x01, 0x02, 0x03]);
assert.throws(
  () => oddBuf.swap16(),
  (err) => {
    assert(err instanceof RangeError, 'error must be RangeError');
    assert.strictEqual(err.message, 'Buffer size must be a multiple of 16-bits');
    return true;
  },
  'odd byte length must throw RangeError'
);

// 4. Buffer.prototype.swap16 succeeds on an empty buffer without modification
const emptyBuf = Buffer.alloc(0);
const returnedEmpty = swap16Buffer(emptyBuf);
assert.strictEqual(returnedEmpty, emptyBuf, 'empty buffer swap16 should return same instance');
assert.strictEqual(emptyBuf.byteLength, 0, 'empty buffer byte length must remain 0');

// 5. Buffer.prototype.swap16 operates correctly on buffer subarrays with non-zero byte offsets
const fullBuf = Buffer.from([0x00, 0x11, 0x22, 0x33, 0x44, 0xff]);
const sub = fullBuf.subarray(1, 5); // [0x11, 0x22, 0x33, 0x44]
sub.swap16();
assert.deepStrictEqual(Array.from(fullBuf), [0x00, 0x22, 0x11, 0x44, 0x33, 0xff], 'sub-slice swapping should mutate parent array in-place at correct offset');

// 6. Buffer.prototype.swap16 handles buffers of 128 bytes or more using native block swapping
const largeArr = new Uint8Array(256);
for (let i = 0; i < 256; i++) {
  largeArr[i] = i;
}
const largeBuf = Buffer.from(largeArr);
largeBuf.swap16();
assert.strictEqual(largeBuf[0], 1, 'byte 0 should swap with byte 1');
assert.strictEqual(largeBuf[1], 0, 'byte 1 should swap with byte 0');
assert.strictEqual(largeBuf[254], 255, 'byte 254 should swap with byte 255');
assert.strictEqual(largeBuf[255], 254, 'byte 255 should swap with byte 254');

console.log('All contract assertions passed successfully.');

原始种子者

匿名