CodeSampleX

サンプル

@emnapi/wasi-threads 1.2.2

検証済みサンプル — npm @emnapi/wasi-threads 1.2.2. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: isSharedArrayBuffer identifies SharedArrayBuffer…

sha256:0058c6d5052ca9b8155b5813c220b2704b65217dda70b4d56b013ccb234e3a6a

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

実行証拠

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

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

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-19

ケース

HOW
ゴール
verify pkg:npm/%40emnapi/wasi-threads@1.2.2
パッケージ
作成日
2026-09-18T20:32:22Z

コントラクト

  1. isSharedArrayBuffer identifies SharedArrayBuffer instances and rejects other types
  2. isTrapError detects WebAssembly.RuntimeError instances and rejects standard Errors
  3. createInstanceProxy wraps a WebAssembly instance and preserves exports
  4. WASIThreads initializes main thread configuration and provides wasi thread-spawn import
  5. WASIThreads enforces postMessage requirement in child thread configuration
  6. ThreadMessageHandler validates postMessage handler and exposes message handling methods
  7. ThreadManager initializes worker pool and tracks worker identifiers

ファイル

  • PROMPT.md
  • csx.json
  • index.mjs
  • 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 pkg:npm/%40emnapi/wasi-threads@1.2.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40emnapi/wasi-threads@1.2.2

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:b1448bb1293d89be5a357297d80d0d72b737386813f7a25771f6398e6772bde0","contract":["isSharedArrayBuffer identifies SharedArrayBuffer instances and rejects other types","isTrapError detects WebAssembly.RuntimeError instances and rejects standard Errors","createInstanceProxy wraps a WebAssembly instance and preserves exports","WASIThreads initializes main thread configuration and provides wasi thread-spawn import","WASIThreads enforces postMessage requirement in child thread configuration","ThreadMessageHandler validates postMessage handler and exposes message handling methods","ThreadManager initializes worker pool and tracks worker identifiers"],"goal":"verify pkg:npm/%40emnapi/wasi-threads@1.2.2","kind":"HOW","packages":["pkg:npm/%40emnapi/wasi-threads@1.2.2"],"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/%40emnapi/wasi-threads@1.2.2"],"schemaVersion":1,"subject":"pkg:npm/%40emnapi/wasi-threads@1.2.2","verifierAdapter":"node-typescript@1"}
index.mjs
import {
  WASIThreads,
  ThreadManager,
  ThreadMessageHandler,
  createInstanceProxy,
  isSharedArrayBuffer,
  isTrapError
} from '@emnapi/wasi-threads';

/**
 * Creates a WASIThreads manager configured for the given WASI instance.
 *
 * @param {object} wasi - WASI instance conforming to WASIInstance interface.
 * @param {Function} onCreateWorker - Worker factory callback.
 * @param {object} [options] - Additional configuration options.
 * @returns {WASIThreads} Initialized WASIThreads instance.
 */
export function createWASIThreads(wasi, onCreateWorker, options = {}) {
  return new WASIThreads({
    wasi,
    onCreateWorker,
    ...options
  });
}

/**
 * Checks whether a given buffer is a SharedArrayBuffer.
 *
 * @param {*} value - The value to test.
 * @returns {boolean} True if the value is a SharedArrayBuffer.
 */
export function checkSharedArrayBuffer(value) {
  return isSharedArrayBuffer(value);
}

/**
 * Checks whether an error is a WebAssembly trap runtime error.
 *
 * @param {Error} error - The error to inspect.
 * @returns {boolean} True if the error is a WebAssembly.RuntimeError.
 */
export function checkTrapError(error) {
  return isTrapError(error);
}

export {
  WASIThreads,
  ThreadManager,
  ThreadMessageHandler,
  createInstanceProxy,
  isSharedArrayBuffer,
  isTrapError
};
package-lock.json
{
  "name": "sample-wasi-threads",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-wasi-threads",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@emnapi/wasi-threads": "1.2.2"
      }
    },
    "node_modules/@emnapi/wasi-threads": {
      "version": "1.2.2",
      "resolved": "https://registry.npmjs.org/@emnapi/wasi-threads/-/wasi-threads-1.2.2.tgz",
      "integrity": "sha512-c95qOXkHdydNKhscBTebqEC1CVAZpyqOfVfBzQ1qgzyl3gfeldUjIggDbIZgDKsHLgnsM+igH7TJ/eAasaVuMA==",
      "license": "MIT",
      "dependencies": {
        "tslib": "^2.4.0"
      }
    },
    "node_modules/tslib": {
      "version": "2.8.1",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
      "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
      "license": "0BSD"
    }
  }
}
package.json
{
  "name": "sample-wasi-threads",
  "version": "1.0.0",
  "type": "module",
  "main": "index.mjs",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@emnapi/wasi-threads": "1.2.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40emnapi/wasi-threads@1.2.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40emnapi/wasi-threads@1.2.2"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import {
  WASIThreads,
  ThreadManager,
  ThreadMessageHandler,
  createInstanceProxy,
  isSharedArrayBuffer,
  isTrapError,
  createWASIThreads,
  checkSharedArrayBuffer,
  checkTrapError
} from '../index.mjs';

// 1. isSharedArrayBuffer identifies SharedArrayBuffer instances and rejects other types
assert.equal(isSharedArrayBuffer(new SharedArrayBuffer(16)), true);
assert.equal(isSharedArrayBuffer(new ArrayBuffer(16)), false);
assert.equal(isSharedArrayBuffer(null), false);
assert.equal(isSharedArrayBuffer({}), false);
assert.equal(isSharedArrayBuffer('string'), false);
assert.equal(checkSharedArrayBuffer(new SharedArrayBuffer(8)), true);

// 2. isTrapError detects WebAssembly.RuntimeError instances and rejects standard Errors
assert.equal(isTrapError(new WebAssembly.RuntimeError('unreachable')), true);
assert.equal(isTrapError(new Error('general error')), false);
assert.equal(isTrapError(new TypeError('type error')), false);
assert.equal(checkTrapError(new WebAssembly.RuntimeError('trap')), true);

// 3. createInstanceProxy wraps a WebAssembly instance and preserves exports
const wasmBytes = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
const wasmModule = new WebAssembly.Module(wasmBytes);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const proxied = createInstanceProxy(wasmInstance);
assert.ok(typeof proxied === 'object' && proxied !== null);

// 4. WASIThreads initializes main thread configuration and provides wasi thread-spawn import
const mockWasi = { wasiImport: {}, initialize: () => {}, start: () => 0 };
const wtInstance = createWASIThreads(mockWasi, () => ({}));
assert.equal(wtInstance.childThread, false);
assert.ok(wtInstance.PThread instanceof ThreadManager);
const imports = wtInstance.getImportObject();
assert.equal(typeof imports.wasi['thread-spawn'], 'function');

// 5. WASIThreads enforces postMessage requirement in child thread configuration
assert.throws(
  () => new WASIThreads({ wasi: mockWasi, childThread: true }),
  {
    name: 'TypeError',
    message: 'options.postMessage is not a function'
  }
);
const childWt = new WASIThreads({ wasi: mockWasi, childThread: true, postMessage: () => {} });
assert.equal(childWt.childThread, true);
assert.equal(childWt.PThread, undefined);

// 6. ThreadMessageHandler validates postMessage handler and exposes message handling methods
assert.throws(
  () => new ThreadMessageHandler(),
  {
    name: 'TypeError',
    message: 'options.postMessage is not a function'
  }
);
const handler = new ThreadMessageHandler({ postMessage: () => {} });
assert.equal(typeof handler.handle, 'function');
assert.equal(typeof handler.instantiate, 'function');

// 7. ThreadManager initializes worker pool and tracks worker identifiers
const tm = new ThreadManager({ onCreateWorker: () => ({}) });
assert.deepEqual(tm.unusedWorkers, []);
assert.deepEqual(Object.keys(tm.pthreads), []);
assert.equal(typeof tm.nextWorkerID, 'number');

console.log('All @emnapi/wasi-threads contract assertions passed.');

オリジンシーダー

匿名