CodeSampleX

Beispiel

@webassemblyjs/helper-wasm-section 1.14.1: create, resize, and remove WebAssembly sections in binary buffer

Verifiziertes Beispiel für npm @webassemblyjs/helper-wasm-section 1.14.1: create, resize, and remove WebAssembly sections in binary buffer. Der Vertrag lief…

sha256:4fa6da4b57157a64e778239c64e3d2a8813f69b408f4fa08806bdc45cefe29e6

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
create, resize, and remove WebAssembly sections in binary buffer
Pakete
Symbole
  • createEmptySection
  • removeSections
  • resizeSectionByteSize
  • resizeSectionVecSize
Umgebung
node 22.23.2
Erstellt
2026-08-27T23:24:22Z

Contract

  1. createEmptySection inserts a new section into the AST metadata and encodes section bytes in the buffer
  2. resizeSectionByteSize adjusts the byte length of an existing section in the AST and buffer
  3. resizeSectionVecSize updates vector element count in section header and updates buffer
  4. removeSections deletes section from AST and removes corresponding bytes from buffer
  5. removeSections and resize functions throw an Error when section does not exist in AST

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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: create, resize, and remove WebAssembly sections in binary buffer
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40webassemblyjs/helper-wasm-section@1.14.1
Demonstrate these symbols/APIs:
  - createEmptySection
  - removeSections
  - resizeSectionByteSize
  - resizeSectionVecSize

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:da9575fe437c9afece9063a1761cd973a451c7e6a48e767e0c676a723e6b920e","contract":["createEmptySection inserts a new section into the AST metadata and encodes section bytes in the buffer","resizeSectionByteSize adjusts the byte length of an existing section in the AST and buffer","resizeSectionVecSize updates vector element count in section header and updates buffer","removeSections deletes section from AST and removes corresponding bytes from buffer","removeSections and resize functions throw an Error when section does not exist in AST"],"goal":"create, resize, and remove WebAssembly sections in binary buffer","kind":"HOW","packages":["pkg:npm/%40webassemblyjs/helper-wasm-section@1.14.1"],"schemaVersion":1,"symbols":["createEmptySection","removeSections","resizeSectionByteSize","resizeSectionVecSize"]},"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/%40webassemblyjs/helper-wasm-section@1.14.1"],"schemaVersion":1,"subject":"pkg:npm/%40webassemblyjs/helper-wasm-section@1.14.1","symbols":["createEmptySection","removeSections","resizeSectionByteSize","resizeSectionVecSize"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-webassemblyjs-helper-wasm-section",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-webassemblyjs-helper-wasm-section",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@webassemblyjs/helper-wasm-section": "1.14.1"
      }
    },
    "node_modules/@webassemblyjs/ast": {
      "version": "1.14.1",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.14.1.tgz",
      "integrity": "sha512-nuBEDgQfm1ccRp/8bCQrx1frohyufl4JlbMMZ4P1wpeOfDhF6FQkxZJ1b/e+PLwr6X1Nhw6OLme5usuBWYBvuQ==",
      "license": "MIT",
      "dependencies": {
        "@webassemblyjs/helper-numbers": "1.13.2",
        "@webassemblyjs/helper-wasm-bytecode": "1.13.2"
      }
    },
    "node_modules/@webassemblyjs/floating-point-hex-parser": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/floating-point-hex-parser/-/floating-point-hex-parser-1.13.2.tgz",
      "integrity": "sha512-6oXyTOzbKxGH4steLbLNOu71Oj+C8Lg34n6CqRvqfS2O71BxY6ByfMDRhBytzknj9yGUPVJ1qIKhRlAwO1AovA==",
      "license": "MIT"
    },
    "node_modules/@webassemblyjs/helper-api-error": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-api-error/-/helper-api-error-1.13.2.tgz",
      "integrity": "sha512-U56GMYxy4ZQCbDZd6JuvvNV/WFildOjsaWD3Tzzvmw/mas3cXzRJPMjP83JqEsgSbyrmaGjBfDtV7KDXV9UzFQ==",
      "license": "MIT"
    },
    "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"
    },
    "node_modules/@webassemblyjs/helper-numbers": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-numbers/-/helper-numbers-1.13.2.tgz",
      "integrity": "sha512-FE8aCmS5Q6eQYcV3gI35O4J789wlQA+7JrqTTpJqn5emA4U2hvwJmvFRC0HODS+3Ye6WioDklgd6scJ3+PLnEA==",
      "license": "MIT",
      "dependencies": {
        "@webassemblyjs/floating-point-hex-parser": "1.13.2",
        "@webassemblyjs/helper-api-error": "1.13.2",
        "@xtuc/long": "4.2.2"
      }
    },
    "node_modules/@webassemblyjs/helper-wasm-bytecode": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-wasm-bytecode/-/helper-wasm-bytecode-1.13.2.tgz",
      "integrity": "sha512-3QbLKy93F0EAIXLh0ogEVR6rOubA9AoZ+WRYhNbFyuB70j3dRdwH9g+qXhLAO0kiYGlg3TxDV+I4rQTr/YNXkA==",
      "license": "MIT"
    },
    "node_modules/@webassemblyjs/helper-wasm-section": {
      "version": "1.14.1",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-wasm-section/-/helper-wasm-section-1.14.1.tgz",
      "integrity": "sha512-ds5mXEqTJ6oxRoqjhWDU83OgzAYjwsCV8Lo/N+oRsNDmx/ZDpqalmrtgOMkHwxsG0iI//3BwWAErYRHtgn0dZw==",
      "license": "MIT",
      "dependencies": {
        "@webassemblyjs/ast": "1.14.1",
        "@webassemblyjs/helper-buffer": "1.14.1",
        "@webassemblyjs/helper-wasm-bytecode": "1.13.2",
        "@webassemblyjs/wasm-gen": "1.14.1"
      }
    },
    "node_modules/@webassemblyjs/ieee754": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/ieee754/-/ieee754-1.13.2.tgz",
      "integrity": "sha512-4LtOzh58S/5lX4ITKxnAK2USuNEvpdVV9AlgGQb8rJDHaLeHciwG4zlGr0j/SNWlr7x3vO1lDEsuePvtcDNCkw==",
      "license": "MIT",
      "dependencies": {
        "@xtuc/ieee754": "^1.2.0"
      }
    },
    "node_modules/@webassemblyjs/leb128": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/leb128/-/leb128-1.13.2.tgz",
      "integrity": "sha512-Lde1oNoIdzVzdkNEAWZ1dZ5orIbff80YPdHx20mrHwHrVNNTjNr8E3xz9BdpcGqRQbAEa+fkrCb+fRFTl/6sQw==",
      "license": "Apache-2.0",
      "dependencies": {
        "@xtuc/long": "4.2.2"
      }
    },
    "node_modules/@webassemblyjs/utf8": {
      "version": "1.13.2",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/utf8/-/utf8-1.13.2.tgz",
      "integrity": "sha512-3NQWGjKTASY1xV5m7Hr0iPeXD9+RDobLll3T9d2AO+g3my8xy5peVyjSag4I50mR1bBSN/Ct12lo+R9tJk0NZQ==",
      "license": "MIT"
    },
    "node_modules/@webassemblyjs/wasm-gen": {
      "version": "1.14.1",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-gen/-/wasm-gen-1.14.1.tgz",
      "integrity": "sha512-AmomSIjP8ZbfGQhumkNvgC33AY7qtMCXnN6bL2u2Js4gVCg8fp735aEiMSBbDR7UQIj90n4wKAFUSEd0QN2Ukg==",
      "license": "MIT",
      "dependencies": {
        "@webassemblyjs/ast": "1.14.1",
        "@webassemblyjs/helper-wasm-bytecode": "1.13.2",
        "@webassemblyjs/ieee754": "1.13.2",
        "@webassemblyjs/leb128": "1.13.2",
        "@webassemblyjs/utf8": "1.13.2"
      }
    },
    "node_modules/@xtuc/ieee754": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz",
      "integrity": "sha512-DX8nKgqcGwsc0eJSqYt5lwP4DH5FlHnmuWWBRy7X0NcaGR0ZtuyeESgMwTYVEtxmsNGY+qit4QYT/MIYTOTPeA==",
      "license": "BSD-3-Clause"
    },
    "node_modules/@xtuc/long": {
      "version": "4.2.2",
      "resolved": "https://registry.npmjs.org/@xtuc/long/-/long-4.2.2.tgz",
      "integrity": "sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==",
      "license": "Apache-2.0"
    }
  }
}
package.json
{
  "name": "sample-webassemblyjs-helper-wasm-section",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "@webassemblyjs/helper-wasm-section": "1.14.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "create, resize, and remove WebAssembly sections in binary buffer",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40webassemblyjs/helper-wasm-section@1.14.1"
  ],
  "symbols": [
    "createEmptySection",
    "removeSections",
    "resizeSectionByteSize",
    "resizeSectionVecSize"
  ]
}
src/index.mjs
import {
  createEmptySection,
  removeSections,
  resizeSectionByteSize,
  resizeSectionVecSize,
} from "@webassemblyjs/helper-wasm-section";

/**
 * Creates an empty AST container suitable for WebAssembly modules with section metadata.
 * @returns {object} WebAssembly Program AST node
 */
export function createEmptyModuleAst() {
  return {
    type: "Program",
    body: [
      {
        type: "Module",
        id: null,
        fields: [],
        metadata: {
          type: "ModuleMetadata",
          sections: [],
        },
      },
    ],
  };
}

/**
 * Inserts an empty section into the WebAssembly binary buffer and AST.
 * @param {object} ast
 * @param {Uint8Array|Buffer} uint8Buffer
 * @param {string} sectionName
 * @returns {{ uint8Buffer: Uint8Array, sectionMetadata: object }}
 */
export function addEmptySection(ast, uint8Buffer, sectionName) {
  return createEmptySection(ast, uint8Buffer, sectionName);
}

/**
 * Removes a section from the AST and deletes its bytes from the buffer.
 * @param {object} ast
 * @param {Uint8Array|Buffer} uint8Buffer
 * @param {string} sectionName
 * @returns {Uint8Array|Buffer}
 */
export function deleteSection(ast, uint8Buffer, sectionName) {
  return removeSections(ast, uint8Buffer, sectionName);
}

/**
 * Resizes the byte size field of a section by deltaBytes.
 * @param {object} ast
 * @param {Uint8Array|Buffer} uint8Buffer
 * @param {string} sectionName
 * @param {number} deltaBytes
 * @returns {Uint8Array|Buffer}
 */
export function adjustSectionByteSize(ast, uint8Buffer, sectionName, deltaBytes) {
  return resizeSectionByteSize(ast, uint8Buffer, sectionName, deltaBytes);
}

/**
 * Resizes the vector element count field of a section by deltaElements.
 * @param {object} ast
 * @param {Uint8Array|Buffer} uint8Buffer
 * @param {string} sectionName
 * @param {number} deltaElements
 * @returns {Uint8Array|Buffer}
 */
export function adjustSectionVecSize(ast, uint8Buffer, sectionName, deltaElements) {
  return resizeSectionVecSize(ast, uint8Buffer, sectionName, deltaElements);
}

export {
  createEmptySection,
  removeSections,
  resizeSectionByteSize,
  resizeSectionVecSize,
};
test/contract.mjs
import assert from "node:assert/strict";
import {
  createEmptyModuleAst,
  addEmptySection,
  deleteSection,
  adjustSectionByteSize,
  adjustSectionVecSize,
  createEmptySection,
  removeSections,
  resizeSectionByteSize,
  resizeSectionVecSize,
} from "../src/index.mjs";

const wasmHeader = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);

// Contract Assertion 1: createEmptySection inserts a new section into the AST metadata and encodes section bytes in the buffer
{
  const ast = createEmptyModuleAst();
  const { uint8Buffer: buf1, sectionMetadata: meta1 } = addEmptySection(ast, wasmHeader, "type");

  assert.equal(meta1.section, "type");
  assert.equal(meta1.size.value, 1);
  assert.equal(meta1.vectorOfSize.value, 0);
  assert.equal(ast.body[0].metadata.sections.length, 1);
  assert.equal(ast.body[0].metadata.sections[0].section, "type");
  assert.equal(buf1.length, 11);
  assert.equal(buf1[8], 1); // section ID for "type" section
  assert.equal(buf1[9], 1); // section byte size
  assert.equal(buf1[10], 0); // vector size

  const { uint8Buffer: buf2, sectionMetadata: meta2 } = createEmptySection(ast, buf1, "import");
  assert.equal(meta2.section, "import");
  assert.equal(ast.body[0].metadata.sections.length, 2);
  assert.equal(ast.body[0].metadata.sections[1].section, "import");
  assert.equal(buf2.length, 14);
  assert.equal(buf2[11], 2); // section ID for "import" section
}

// Contract Assertion 2: resizeSectionByteSize adjusts the byte length of an existing section in the AST and buffer
{
  const ast = createEmptyModuleAst();
  const { uint8Buffer: buf, sectionMetadata: meta } = createEmptySection(ast, wasmHeader, "type");
  const updatedBuf = adjustSectionByteSize(ast, buf, "type", 4);

  assert.equal(meta.size.value, 5);
  assert.equal(updatedBuf[9], 5);

  const directBuf = resizeSectionByteSize(ast, updatedBuf, "type", -2);
  assert.equal(meta.size.value, 3);
  assert.equal(directBuf[9], 3);
}

// Contract Assertion 3: resizeSectionVecSize updates vector element count in section header and updates buffer
{
  const ast = createEmptyModuleAst();
  const { uint8Buffer: buf, sectionMetadata: meta } = createEmptySection(ast, wasmHeader, "type");
  const updatedBuf = adjustSectionVecSize(ast, buf, "type", 3);

  assert.equal(meta.vectorOfSize.value, 3);
  assert.equal(updatedBuf[10], 3);

  const directBuf = resizeSectionVecSize(ast, updatedBuf, "type", 2);
  assert.equal(meta.vectorOfSize.value, 5);
  assert.equal(directBuf[10], 5);
}

// Contract Assertion 4: removeSections deletes section from AST and removes corresponding bytes from buffer
{
  const ast = createEmptyModuleAst();
  const { uint8Buffer: buf1 } = createEmptySection(ast, wasmHeader, "type");
  const { uint8Buffer: buf2 } = createEmptySection(ast, buf1, "import");
  assert.equal(ast.body[0].metadata.sections.length, 2);

  const bufAfterRemove = deleteSection(ast, buf2, "type");
  assert.equal(ast.body[0].metadata.sections.length, 1);
  assert.equal(ast.body[0].metadata.sections[0].section, "import");
  assert.deepEqual(
    Buffer.from(bufAfterRemove),
    Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00])
  );

  const bufEmpty = removeSections(ast, bufAfterRemove, "import");
  assert.equal(ast.body[0].metadata.sections.length, 0);
  assert.deepEqual(Buffer.from(bufEmpty), wasmHeader);
}

// Contract Assertion 5: removeSections and resize functions throw an Error when section does not exist in AST
{
  const ast = createEmptyModuleAst();
  const buf = Buffer.from(wasmHeader);

  assert.throws(() => {
    deleteSection(ast, buf, "nonexistent");
  }, /Section metadata not found/);

  assert.throws(() => {
    removeSections(ast, buf, "custom");
  }, /Section metadata not found/);

  assert.throws(() => {
    adjustSectionByteSize(ast, buf, "nonexistent", 1);
  }, /Section metadata not found/);

  assert.throws(() => {
    resizeSectionByteSize(ast, buf, "custom", 1);
  }, /Section metadata not found/);

  assert.throws(() => {
    adjustSectionVecSize(ast, buf, "nonexistent", 1);
  }, /Section metadata not found/);

  assert.throws(() => {
    resizeSectionVecSize(ast, buf, "custom", 1);
  }, /Section metadata not found/);
}

console.log("All contract tests passed successfully.");

Ursprungs-Seeder

anonym