CodeSampleX

サンプル

bare-buffer 3.7.0: Buffer.prototype.writeUInt16BE

検証済みサンプル — npm bare-buffer 3.7.0: Buffer.prototype.writeUInt16BE. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: Buffer.prototype.writeUInt16BE…

sha256:9c6d52d3dcbd9848bf5422d59c60a97d41d10811531d08d89f64626434149a52

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
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.writeUInt16BE in pkg:npm/bare-buffer@3.7.0
パッケージ
シンボル
  • Buffer.prototype.writeUInt16BE
作成日
2026-08-27T05:44:03Z

コントラクト

  1. Buffer.prototype.writeUInt16BE is a function aliasing Buffer.prototype.writeUint16BE
  2. buf.writeUInt16BE(value) defaults to offset 0, writes big-endian 16-bit unsigned integer, and returns offset + 2
  3. buf.writeUInt16BE(value, offset) writes 16-bit big-endian unsigned integer at specified byte offset
  4. buf.writeUInt16BE masks values exceeding 16 bits to lower 16 bits
  5. buf.writeUInt16BE throws RangeError when offset is out of bounds, negative, or non-integer
  6. buf.writeUInt16BE throws TypeError when offset is not a number

ファイル

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

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:b4f25230567cbe34cd222410fc02f31c5beba65fda524f171494fffdb1dced03","contract":["Buffer.prototype.writeUInt16BE is a function aliasing Buffer.prototype.writeUint16BE","buf.writeUInt16BE(value) defaults to offset 0, writes big-endian 16-bit unsigned integer, and returns offset + 2","buf.writeUInt16BE(value, offset) writes 16-bit big-endian unsigned integer at specified byte offset","buf.writeUInt16BE masks values exceeding 16 bits to lower 16 bits","buf.writeUInt16BE throws RangeError when offset is out of bounds, negative, or non-integer","buf.writeUInt16BE throws TypeError when offset is not a number"],"goal":"verify Buffer.prototype.writeUInt16BE in pkg:npm/bare-buffer@3.7.0","kind":"HOW","packages":["pkg:npm/bare-buffer@3.7.0"],"schemaVersion":1,"symbols":["Buffer.prototype.writeUInt16BE"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","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.writeUInt16BE"],"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) {
      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 writeUInt16BE(buffer, value, offset = 0) {
  return buffer.writeUInt16BE(value, offset);
}

export function writeUInt16BEArray(buffer, values, offset = 0) {
  let currentOffset = offset;
  for (const val of values) {
    currentOffset = buffer.writeUInt16BE(val, currentOffset);
  }
  return currentOffset;
}

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

// 1. Buffer.prototype.writeUInt16BE exists and aliases writeUint16BE
assert.strictEqual(typeof Buffer.prototype.writeUInt16BE, 'function', 'writeUInt16BE must be a prototype method');
assert.strictEqual(Buffer.prototype.writeUInt16BE, Buffer.prototype.writeUint16BE, 'writeUInt16BE must alias writeUint16BE');

// 2. Default offset is 0 and writes big-endian 16-bit unsigned integer
const buf1 = Buffer.alloc(4);
const ret1 = buf1.writeUInt16BE(0x1234);
assert.strictEqual(ret1, 2, 'Default offset should return offset + 2 (2)');
assert.strictEqual(buf1[0], 0x12, 'First byte must be high byte 0x12');
assert.strictEqual(buf1[1], 0x34, 'Second byte must be low byte 0x34');
assert.strictEqual(buf1.readUInt16BE(0), 0x1234, 'Read back must match 0x1234');

// 3. Big-endian byte order verification across numeric boundaries
const maxBuf = Buffer.alloc(2);
maxBuf.writeUInt16BE(65535, 0);
assert.deepStrictEqual(Array.from(maxBuf), [0xff, 0xff], '65535 should write [0xff, 0xff]');

const zeroBuf = Buffer.alloc(2);
zeroBuf.writeUInt16BE(0, 0);
assert.deepStrictEqual(Array.from(zeroBuf), [0x00, 0x00], '0 should write [0x00, 0x00]');

const midBuf = Buffer.alloc(2);
midBuf.writeUInt16BE(32768, 0);
assert.deepStrictEqual(Array.from(midBuf), [0x80, 0x00], '32768 should write [0x80, 0x00]');

const asymmetryBuf = Buffer.alloc(2);
asymmetryBuf.writeUInt16BE(0x0102, 0);
assert.deepStrictEqual(Array.from(asymmetryBuf), [0x01, 0x02], '0x0102 must write [0x01, 0x02], not little-endian [0x02, 0x01]');

// 4. Writing at arbitrary positive offsets in a larger buffer
const multiBuf = Buffer.alloc(6);
const ret2 = multiBuf.writeUInt16BE(0x1234, 1);
assert.strictEqual(ret2, 3, 'Offset 1 write should return 3');
const ret3 = multiBuf.writeUInt16BE(0x5678, 3);
assert.strictEqual(ret3, 5, 'Offset 3 write should return 5');
assert.deepStrictEqual(Array.from(multiBuf), [0x00, 0x12, 0x34, 0x56, 0x78, 0x00], 'Multi-offset write layout must match');

// 5. Overflow masking (values exceeding 16 bits truncate to lower 16 bits)
const maskBuf = Buffer.alloc(2);
maskBuf.writeUInt16BE(0x123456, 0);
assert.deepStrictEqual(Array.from(maskBuf), [0x34, 0x56], '0x123456 should be masked to 0x3456');

// 6. Helper function wrappers
const helperBuf = Buffer.alloc(4);
assert.strictEqual(writeUInt16BE(helperBuf, 0xabcd, 1), 3, 'writeUInt16BE helper should return 3');
assert.deepStrictEqual(Array.from(helperBuf.subarray(1, 3)), [0xab, 0xcd], 'writeUInt16BE helper should write correct bytes');

const seqBuf = Buffer.alloc(6);
const finalOffset = writeUInt16BEArray(seqBuf, [10, 20, 30], 0);
assert.strictEqual(finalOffset, 6, 'writeUInt16BEArray should return final offset 6');
assert.deepStrictEqual(Array.from(seqBuf), [0x00, 0x0a, 0x00, 0x14, 0x00, 0x1e], 'writeUInt16BEArray should write uint16 BE sequence');

// 7. RangeError thrown on bounds violation
const smallBuf = Buffer.alloc(2);
assert.throws(() => {
  smallBuf.writeUInt16BE(0x1234, 1);
}, { name: 'RangeError' }, 'Offset with less than 2 bytes remaining must throw RangeError');

assert.throws(() => {
  smallBuf.writeUInt16BE(0x1234, 2);
}, { name: 'RangeError' }, 'Offset at end of buffer must throw RangeError');

assert.throws(() => {
  smallBuf.writeUInt16BE(0x1234, -1);
}, { name: 'RangeError' }, 'Negative offset must throw RangeError');

const emptyBuf = Buffer.alloc(0);
assert.throws(() => {
  emptyBuf.writeUInt16BE(0x1234, 0);
}, { name: 'RangeError' }, 'Writing to empty buffer must throw RangeError');

assert.throws(() => {
  smallBuf.writeUInt16BE(0x1234, 0.5);
}, { name: 'RangeError' }, 'Non-integer offset must throw RangeError');

// 8. TypeError thrown on non-numeric offset type
assert.throws(() => {
  smallBuf.writeUInt16BE(0x1234, '0');
}, { name: 'TypeError' }, 'String offset must throw TypeError');

assert.throws(() => {
  smallBuf.writeUInt16BE(0x1234, null);
}, { name: 'TypeError' }, 'Null offset must throw TypeError');

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

オリジンシーダー

匿名