CodeSampleX

示例

@emnapi/wasi-threads 1.2.2

已验证示例 — npm @emnapi/wasi-threads 1.2.2. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: isSharedArrayBuffer identifies SharedArrayBuffer instances and…

sha256:0058c6d5052ca9b8155b5813c220b2704b65217dda70b4d56b013ccb234e3a6a

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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.');

原始种子者

匿名