CodeSampleX

示例

@webassemblyjs/helper-buffer 1.14.1

已验证示例 — npm @webassemblyjs/helper-buffer 1.14.1. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: overrideBytesInBuffer replaces a slice of Uint8Array…

sha256:7b01f30bd733fa15233169801a39bf67cd23d3b4ae03578f644a3d7887752962

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

契约

  1. overrideBytesInBuffer replaces a slice of Uint8Array at specified start and end offsets with replacement bytes
  2. overrideBytesInBuffer supports inserting bytes without removing any when startLoc equals endLoc
  3. overrideBytesInBuffer supports deleting bytes when replacement byte array is empty
  4. overrideBytesInBuffer throws TypeError or Error if input buffer is not Uint8Array
  5. makeBuffer flattens nested byte arrays and returns an ArrayBuffer
  6. fromHexdump parses multi-line formatted hex dump strings omitting offset column into a Uint8Array

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 pkg:npm/%40webassemblyjs/helper-buffer@1.14.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40webassemblyjs/helper-buffer@1.14.1

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:c07324f428955ebac8b330afd49912287b3aa788a51ccdbbeb223ca40dfc7956","contract":["overrideBytesInBuffer replaces a slice of Uint8Array at specified start and end offsets with replacement bytes","overrideBytesInBuffer supports inserting bytes without removing any when startLoc equals endLoc","overrideBytesInBuffer supports deleting bytes when replacement byte array is empty","overrideBytesInBuffer throws TypeError or Error if input buffer is not Uint8Array","makeBuffer flattens nested byte arrays and returns an ArrayBuffer","fromHexdump parses multi-line formatted hex dump strings omitting offset column into a Uint8Array"],"goal":"verify pkg:npm/%40webassemblyjs/helper-buffer@1.14.1","kind":"HOW","packages":["pkg:npm/%40webassemblyjs/helper-buffer@1.14.1"],"schemaVersion":1},"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/%40webassemblyjs/helper-buffer@1.14.1"],"schemaVersion":1,"subject":"pkg:npm/%40webassemblyjs/helper-buffer@1.14.1","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-webassemblyjs-helper-buffer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-webassemblyjs-helper-buffer",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@webassemblyjs/helper-buffer": "1.14.1"
      }
    },
    "node_modules/@webassemblyjs/helper-buffer": {
      "version": "1.14.1",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-buffer/-/helper-buffer-1.14.1.tgz",
      "integrity": "sha512-jyH7wtcHiKssDtFPRB+iQdxlDf96m0E39yb0k5uJVhFGleZFoNw1c4aeIcVUPPbXUVJ94wwnMOAqUHyzoEPVMA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-webassemblyjs-helper-buffer",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for @webassemblyjs/helper-buffer@1.14.1",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "@webassemblyjs/helper-buffer": "1.14.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40webassemblyjs/helper-buffer@1.14.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40webassemblyjs/helper-buffer@1.14.1"
  ]
}
src/index.js
const {
  overrideBytesInBuffer,
  makeBuffer,
  fromHexdump
} = require('@webassemblyjs/helper-buffer');

/**
 * Replace a range of bytes in a Uint8Array buffer with new bytes.
 *
 * @param {Uint8Array} buffer - Source Uint8Array buffer.
 * @param {number} startLoc - Zero-based index at which to start replacing.
 * @param {number} endLoc - Zero-based index at which to stop replacing.
 * @param {Array<number>|Uint8Array} newBytes - Bytes to splice in.
 * @returns {Uint8Array} New Uint8Array containing modified buffer.
 */
function replaceBytes(buffer, startLoc, endLoc, newBytes) {
  return overrideBytesInBuffer(buffer, startLoc, endLoc, newBytes);
}

/**
 * Construct an ArrayBuffer from byte numbers or byte arrays.
 *
 * @param {...(number|Array<number>)} byteArrays - Byte lists to concatenate into an ArrayBuffer.
 * @returns {ArrayBuffer} Newly created ArrayBuffer.
 */
function createArrayBuffer(...byteArrays) {
  return makeBuffer(...byteArrays);
}

/**
 * Parse formatted hexdump text into a Uint8Array byte sequence.
 *
 * @param {string} dump - Multi-line string in standard hexdump format.
 * @returns {Uint8Array} Parsed binary byte sequence.
 */
function parseHexdump(dump) {
  return fromHexdump(dump);
}

module.exports = {
  overrideBytesInBuffer,
  makeBuffer,
  fromHexdump,
  replaceBytes,
  createArrayBuffer,
  parseHexdump
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  overrideBytesInBuffer,
  makeBuffer,
  fromHexdump,
  replaceBytes,
  createArrayBuffer,
  parseHexdump
} = require('../src/index.js');

async function runTests() {
  // 1. overrideBytesInBuffer replaces a slice of Uint8Array at specified start and end offsets with replacement bytes
  const initial = new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05]);
  const replaced = replaceBytes(initial, 1, 3, [0xAA, 0xBB]);
  assert.equal(replaced instanceof Uint8Array, true, 'Result must be a Uint8Array');
  assert.deepEqual(
    Array.from(replaced),
    [0x01, 0xAA, 0xBB, 0x04, 0x05],
    'overrideBytesInBuffer should replace indices [1, 3) with [0xAA, 0xBB]'
  );

  // 2. overrideBytesInBuffer supports inserting bytes without removing any when startLoc equals endLoc
  const inserted = overrideBytesInBuffer(initial, 2, 2, [0xFF, 0xEE]);
  assert.deepEqual(
    Array.from(inserted),
    [0x01, 0x02, 0xFF, 0xEE, 0x03, 0x04, 0x05],
    'Inserting at startLoc == endLoc should not remove existing elements'
  );

  // 3. overrideBytesInBuffer supports deleting bytes when replacement byte array is empty
  const deleted = overrideBytesInBuffer(initial, 1, 4, []);
  assert.deepEqual(
    Array.from(deleted),
    [0x01, 0x05],
    'Empty replacement should delete the slice [1, 4)'
  );

  // 4. overrideBytesInBuffer throws TypeError or Error if input buffer is not Uint8Array
  assert.throws(
    () => {
      overrideBytesInBuffer('not-a-buffer', 0, 1, [0x00]);
    },
    (err) => err instanceof Error,
    'Passing invalid buffer type must throw an Error'
  );

  // 5. makeBuffer flattens nested byte arrays and returns an ArrayBuffer
  const arrayBuffer = createArrayBuffer([0x00, 0x61], [0x73, 0x6d, 0x01]);
  assert.equal(arrayBuffer instanceof ArrayBuffer, true, 'makeBuffer must return an ArrayBuffer');
  assert.equal(arrayBuffer.byteLength, 5, 'ArrayBuffer byteLength must match total number of bytes');
  assert.deepEqual(
    Array.from(new Uint8Array(arrayBuffer)),
    [0x00, 0x61, 0x73, 0x6d, 0x01],
    'makeBuffer must assemble bytes in order'
  );

  // 6. fromHexdump parses multi-line formatted hex dump strings omitting offset column into a Uint8Array
  const hexdumpText = `
    0000000 00 61 73 6d
    0000004 01 00 00 00
  `;
  const parsed = parseHexdump(hexdumpText);
  assert.equal(parsed instanceof Uint8Array, true, 'fromHexdump must return a Uint8Array');
  assert.deepEqual(
    Array.from(parsed),
    [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00],
    'fromHexdump must strip offsets and parse hexadecimal byte pairs into Uint8Array'
  );

  console.log('All contract assertions for @webassemblyjs/helper-buffer passed successfully.');
}

runTests().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

原始种子者

匿名