CodeSampleX

Пример

@webassemblyjs/wasm-opt 1.14.1: shrink padded LEB128 section sizes in WebAssembly binary using shrinkPaddedLEB128

Проверенный пример — npm @webassemblyjs/wasm-opt 1.14.1: shrink padded LEB128 section sizes in WebAssembly binary using shrinkPaddedLEB128. Контракт выполнен…

sha256:cfa6933475aece3d2719faf05996ce8ff7092a5e19fc627dc37516ad6634bc96

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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
Цель
shrink padded LEB128 section sizes in WebAssembly binary using shrinkPaddedLEB128
Пакеты
Символы
  • shrinkPaddedLEB128
Создан
2026-08-27T23:30:39Z

Контракт

  1. shrinkPaddedLEB128 rewrites padded LEB128 section sizes in a WebAssembly binary to their minimal LEB128 encoding length
  2. shrinkPaddedLEB128 preserves already minimal WebAssembly binary without altering byte content
  3. shrinkPaddedLEB128 produces valid WebAssembly bytes that compile and instantiate in WebAssembly engine
  4. shrinkPaddedLEB128 throws an Error when decoding malformed WebAssembly binary buffer

Файлы

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

Скачать артефакт с исходным кодом (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: shrink padded LEB128 section sizes in WebAssembly binary using shrinkPaddedLEB128
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40webassemblyjs/wasm-opt@1.14.1
Demonstrate these symbols/APIs:
  - shrinkPaddedLEB128

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:08f318c5adb7a02ffbd5ba6336e377c7eee8861c8e9842f46bbf1983f6527ff4","contract":["shrinkPaddedLEB128 rewrites padded LEB128 section sizes in a WebAssembly binary to their minimal LEB128 encoding length","shrinkPaddedLEB128 preserves already minimal WebAssembly binary without altering byte content","shrinkPaddedLEB128 produces valid WebAssembly bytes that compile and instantiate in WebAssembly engine","shrinkPaddedLEB128 throws an Error when decoding malformed WebAssembly binary buffer"],"goal":"shrink padded LEB128 section sizes in WebAssembly binary using shrinkPaddedLEB128","kind":"HOW","packages":["pkg:npm/%40webassemblyjs/wasm-opt@1.14.1"],"schemaVersion":1,"symbols":["shrinkPaddedLEB128"]},"contractCommand":["node","test/contract.js"],"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/wasm-opt@1.14.1"],"schemaVersion":1,"subject":"pkg:npm/%40webassemblyjs/wasm-opt@1.14.1","symbols":["shrinkPaddedLEB128"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-wasm-opt-shrink-leb128",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-wasm-opt-shrink-leb128",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@webassemblyjs/wasm-opt": "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/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/@webassemblyjs/wasm-opt": {
      "version": "1.14.1",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-opt/-/wasm-opt-1.14.1.tgz",
      "integrity": "sha512-PTcKLUNvBqnY2U6E5bdOQcSM+oVP/PmrDY9NzowJjislEjwP/C4an2303MCVS2Mg9d3AJpIGdUFIQQWbPds0Sw==",
      "license": "MIT",
      "dependencies": {
        "@webassemblyjs/ast": "1.14.1",
        "@webassemblyjs/helper-buffer": "1.14.1",
        "@webassemblyjs/wasm-gen": "1.14.1",
        "@webassemblyjs/wasm-parser": "1.14.1"
      }
    },
    "node_modules/@webassemblyjs/wasm-parser": {
      "version": "1.14.1",
      "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-parser/-/wasm-parser-1.14.1.tgz",
      "integrity": "sha512-JLBl+KZ0R5qB7mCnud/yyX08jWFw5MsoalJ1pQ4EdFlgj9VdXKGuENGsiCIjegI1W7p91rUlcB/LB5yRJKNTcQ==",
      "license": "MIT",
      "dependencies": {
        "@webassemblyjs/ast": "1.14.1",
        "@webassemblyjs/helper-api-error": "1.13.2",
        "@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-wasm-opt-shrink-leb128",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for @webassemblyjs/wasm-opt shrinkPaddedLEB128",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "@webassemblyjs/wasm-opt": "1.14.1"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "shrink padded LEB128 section sizes in WebAssembly binary using shrinkPaddedLEB128",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40webassemblyjs/wasm-opt@1.14.1"
  ],
  "symbols": [
    "shrinkPaddedLEB128"
  ]
}
src/index.js
const { shrinkPaddedLEB128 } = require('@webassemblyjs/wasm-opt');

/**
 * Optimizes a WebAssembly binary buffer by shrinking any padded LEB128 section sizes.
 * @param {Uint8Array} wasmBuffer - Raw WebAssembly binary bytes.
 * @returns {Uint8Array} - Optimized WebAssembly binary bytes.
 */
function optimizeWasmSectionSizes(wasmBuffer) {
  return shrinkPaddedLEB128(wasmBuffer);
}

module.exports = {
  optimizeWasmSectionSizes,
  shrinkPaddedLEB128
};
test/contract.js
const assert = require('node:assert');
const { shrinkPaddedLEB128 } = require('@webassemblyjs/wasm-opt');
const { optimizeWasmSectionSizes } = require('../src/index.js');

// 1. shrinkPaddedLEB128 rewrites padded LEB128 section sizes in a WebAssembly binary to their minimal LEB128 encoding length
// Padded LEB128 section size: 4 encoded as 2 bytes [0x84, 0x00]
const paddedWasm = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, // WebAssembly header (\0asm\1\0\0\0)
  0x01,                                           // section id: 1 (Type section)
  0x84, 0x00,                                     // section size: 4 padded in 2 bytes
  0x01, 0x60, 0x00, 0x00                          // section payload (4 bytes: 1 type ())
]);

const shrunkDirect = shrinkPaddedLEB128(paddedWasm);
const shrunkHelper = optimizeWasmSectionSizes(paddedWasm);

const expectedMinimal = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
  0x01,
  0x04,                                           // section size: 4 encoded in 1 byte
  0x01, 0x60, 0x00, 0x00
]);

assert.strictEqual(shrunkDirect.length, expectedMinimal.length, 'shrunk buffer length should match minimal encoding length');
assert.deepStrictEqual(Array.from(shrunkDirect), Array.from(expectedMinimal), 'bytes should have section size rewritten to single byte 0x04');
assert.deepStrictEqual(Array.from(shrunkHelper), Array.from(expectedMinimal), 'helper output should match minimal encoding');

// 2. shrinkPaddedLEB128 preserves already minimal WebAssembly binary without altering byte content
const minimalWasm = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
const preserved = shrinkPaddedLEB128(minimalWasm);
assert.deepStrictEqual(Array.from(preserved), Array.from(minimalWasm), 'unpadded binary should remain unaltered');

// 3. shrinkPaddedLEB128 produces valid WebAssembly bytes that compile and instantiate in WebAssembly engine
const wasmModule = new WebAssembly.Module(shrunkDirect);
const wasmInstance = new WebAssembly.Instance(wasmModule);
assert(wasmInstance instanceof WebAssembly.Instance, 'optimized wasm binary should instantiate cleanly');

// 4. shrinkPaddedLEB128 throws an Error when decoding malformed WebAssembly binary buffer
const malformedWasm = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
assert.throws(
  () => shrinkPaddedLEB128(malformedWasm),
  (err) => {
    return err instanceof Error && err.message.includes('Error while optimizing: shrinkPaddedLEB128');
  },
  'decoding malformed wasm buffer should throw an Error wrapping optimizer error'
);

console.log('Contract tests passed successfully.');

Исходный сидер

аноним