@mermanjs/node-wasm 0.8.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE-APACHE +201 -0
  3. package/LICENSE-MIT +21 -0
  4. package/README.md +47 -0
  5. package/THIRD_PARTY_LICENSES/beautiful-mermaid/LICENSE +21 -0
  6. package/THIRD_PARTY_LICENSES/cose-base-v1/LICENSE +21 -0
  7. package/THIRD_PARTY_LICENSES/cose-base-v2/LICENSE +21 -0
  8. package/THIRD_PARTY_LICENSES/cytoscape/LICENSE +19 -0
  9. package/THIRD_PARTY_LICENSES/cytoscape-cose-bilkent/LICENSE +21 -0
  10. package/THIRD_PARTY_LICENSES/cytoscape-fcose/LICENSE +19 -0
  11. package/THIRD_PARTY_LICENSES/d3-shape/LICENSE +13 -0
  12. package/THIRD_PARTY_LICENSES/dagre/LICENSE +19 -0
  13. package/THIRD_PARTY_LICENSES/dompurify/LICENSE +202 -0
  14. package/THIRD_PARTY_LICENSES/eclipse-elk/LICENSE.md +264 -0
  15. package/THIRD_PARTY_LICENSES/elkjs/LICENSE.md +264 -0
  16. package/THIRD_PARTY_LICENSES/fmin/LICENSE +27 -0
  17. package/THIRD_PARTY_LICENSES/graphlib/LICENSE +19 -0
  18. package/THIRD_PARTY_LICENSES/katex-fonts/FONT_NOTICE.txt +12 -0
  19. package/THIRD_PARTY_LICENSES/katex-fonts/OFL.txt +85 -0
  20. package/THIRD_PARTY_LICENSES/layout-base-v1/LICENSE +21 -0
  21. package/THIRD_PARTY_LICENSES/layout-base-v2/LICENSE +21 -0
  22. package/THIRD_PARTY_LICENSES/mermaid/LICENSE +21 -0
  23. package/THIRD_PARTY_LICENSES/mermaid-ascii/LICENSE +21 -0
  24. package/THIRD_PARTY_LICENSES/mermaid-rs-renderer/LICENSE +21 -0
  25. package/THIRD_PARTY_LICENSES/non-layered-tidy-tree-layout/LICENSE +21 -0
  26. package/THIRD_PARTY_LICENSES/ratex/LICENSE +27 -0
  27. package/THIRD_PARTY_LICENSES/ratex/THIRD_PARTY_NOTICES.txt +19 -0
  28. package/THIRD_PARTY_LICENSES/rough-rs/LICENSE +21 -0
  29. package/THIRD_PARTY_LICENSES/roughjs/LICENSE +21 -0
  30. package/THIRD_PARTY_LICENSES/rust-cargo-dependencies.json +5581 -0
  31. package/THIRD_PARTY_LICENSES/sanitize-url/LICENSE +21 -0
  32. package/THIRD_PARTY_LICENSES/tree-sitter/LICENSE +21 -0
  33. package/THIRD_PARTY_LICENSES/tree-sitter-mermaid-monaqa/LICENSE +21 -0
  34. package/THIRD_PARTY_LICENSES/tree-sitter-mermaid-pappasam/LICENSE +21 -0
  35. package/THIRD_PARTY_LICENSES/tree-sitter-mermaid-singularity/LICENSE +22 -0
  36. package/THIRD_PARTY_LICENSES/venn-js/LICENSE +22 -0
  37. package/THIRD_PARTY_LICENSES/wasm-minimal-protocol/LICENSE +24 -0
  38. package/THIRD_PARTY_LICENSES/zenuml-core/LICENSE +21 -0
  39. package/THIRD_PARTY_NOTICES.md +648 -0
  40. package/artifact/merman_node.cjs +302 -0
  41. package/artifact/merman_node_bg.wasm +0 -0
  42. package/dist/bounded-executor.mjs +135 -0
  43. package/dist/candidates/wasm.mjs +87 -0
  44. package/dist/candidates/wrap-engine.mjs +36 -0
  45. package/dist/engine.mjs +1340 -0
  46. package/dist/errors.mjs +1318 -0
  47. package/dist/generated/binding-contract.mjs +1019 -0
  48. package/dist/generated/capability-surface.mjs +66 -0
  49. package/dist/generated/node-wire-contract.json +122 -0
  50. package/dist/index.d.ts +283 -0
  51. package/dist/index.mjs +37 -0
  52. package/dist/native-loader.mjs +125 -0
  53. package/dist/transport-contract.mjs +87 -0
  54. package/package.json +44 -0
@@ -0,0 +1,302 @@
1
+ /* @ts-self-types="./merman_node.d.ts" */
2
+
3
+ class WasmEngine {
4
+ __destroy_into_raw() {
5
+ const ptr = this.__wbg_ptr;
6
+ this.__wbg_ptr = 0;
7
+ WasmEngineFinalization.unregister(this);
8
+ return ptr;
9
+ }
10
+ free() {
11
+ const ptr = this.__destroy_into_raw();
12
+ wasm.__wbg_wasmengine_free(ptr, 0);
13
+ }
14
+ dispose() {
15
+ wasm.wasmengine_dispose(this.__wbg_ptr);
16
+ }
17
+ /**
18
+ * @param {string} request_json
19
+ * @param {number | null} [timeout_ms]
20
+ * @returns {string}
21
+ */
22
+ execute(request_json, timeout_ms) {
23
+ let deferred3_0;
24
+ let deferred3_1;
25
+ try {
26
+ const ptr0 = passStringToWasm0(request_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
27
+ const len0 = WASM_VECTOR_LEN;
28
+ const ret = wasm.wasmengine_execute(this.__wbg_ptr, ptr0, len0, isLikeNone(timeout_ms) ? Number.MAX_SAFE_INTEGER : (timeout_ms) >>> 0);
29
+ var ptr2 = ret[0];
30
+ var len2 = ret[1];
31
+ if (ret[3]) {
32
+ ptr2 = 0; len2 = 0;
33
+ throw takeFromExternrefTable0(ret[2]);
34
+ }
35
+ deferred3_0 = ptr2;
36
+ deferred3_1 = len2;
37
+ return getStringFromWasm0(ptr2, len2);
38
+ } finally {
39
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
40
+ }
41
+ }
42
+ /**
43
+ * @param {string} request_json
44
+ * @param {number | null} [timeout_ms]
45
+ * @returns {string}
46
+ */
47
+ executeSync(request_json, timeout_ms) {
48
+ let deferred3_0;
49
+ let deferred3_1;
50
+ try {
51
+ const ptr0 = passStringToWasm0(request_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
52
+ const len0 = WASM_VECTOR_LEN;
53
+ const ret = wasm.wasmengine_executeSync(this.__wbg_ptr, ptr0, len0, isLikeNone(timeout_ms) ? Number.MAX_SAFE_INTEGER : (timeout_ms) >>> 0);
54
+ var ptr2 = ret[0];
55
+ var len2 = ret[1];
56
+ if (ret[3]) {
57
+ ptr2 = 0; len2 = 0;
58
+ throw takeFromExternrefTable0(ret[2]);
59
+ }
60
+ deferred3_0 = ptr2;
61
+ deferred3_1 = len2;
62
+ return getStringFromWasm0(ptr2, len2);
63
+ } finally {
64
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
65
+ }
66
+ }
67
+ /**
68
+ * @param {string} id
69
+ * @returns {string}
70
+ */
71
+ metadataJson(id) {
72
+ let deferred3_0;
73
+ let deferred3_1;
74
+ try {
75
+ const ptr0 = passStringToWasm0(id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
76
+ const len0 = WASM_VECTOR_LEN;
77
+ const ret = wasm.wasmengine_metadataJson(this.__wbg_ptr, ptr0, len0);
78
+ var ptr2 = ret[0];
79
+ var len2 = ret[1];
80
+ if (ret[3]) {
81
+ ptr2 = 0; len2 = 0;
82
+ throw takeFromExternrefTable0(ret[2]);
83
+ }
84
+ deferred3_0 = ptr2;
85
+ deferred3_1 = len2;
86
+ return getStringFromWasm0(ptr2, len2);
87
+ } finally {
88
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
89
+ }
90
+ }
91
+ /**
92
+ * @param {string} options_json
93
+ */
94
+ constructor(options_json) {
95
+ const ptr0 = passStringToWasm0(options_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
96
+ const len0 = WASM_VECTOR_LEN;
97
+ const ret = wasm.wasmengine_new(ptr0, len0);
98
+ if (ret[2]) {
99
+ throw takeFromExternrefTable0(ret[1]);
100
+ }
101
+ this.__wbg_ptr = ret[0];
102
+ WasmEngineFinalization.register(this, this.__wbg_ptr, this);
103
+ return this;
104
+ }
105
+ /**
106
+ * @returns {string}
107
+ */
108
+ runtimeCatalogJson() {
109
+ let deferred2_0;
110
+ let deferred2_1;
111
+ try {
112
+ const ret = wasm.wasmengine_runtimeCatalogJson(this.__wbg_ptr);
113
+ var ptr1 = ret[0];
114
+ var len1 = ret[1];
115
+ if (ret[3]) {
116
+ ptr1 = 0; len1 = 0;
117
+ throw takeFromExternrefTable0(ret[2]);
118
+ }
119
+ deferred2_0 = ptr1;
120
+ deferred2_1 = len1;
121
+ return getStringFromWasm0(ptr1, len1);
122
+ } finally {
123
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
124
+ }
125
+ }
126
+ }
127
+ if (Symbol.dispose) WasmEngine.prototype[Symbol.dispose] = WasmEngine.prototype.free;
128
+ exports.WasmEngine = WasmEngine;
129
+
130
+ /**
131
+ * @returns {string}
132
+ */
133
+ function transportIdentityJson() {
134
+ let deferred2_0;
135
+ let deferred2_1;
136
+ try {
137
+ const ret = wasm.transportIdentityJson();
138
+ var ptr1 = ret[0];
139
+ var len1 = ret[1];
140
+ if (ret[3]) {
141
+ ptr1 = 0; len1 = 0;
142
+ throw takeFromExternrefTable0(ret[2]);
143
+ }
144
+ deferred2_0 = ptr1;
145
+ deferred2_1 = len1;
146
+ return getStringFromWasm0(ptr1, len1);
147
+ } finally {
148
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
149
+ }
150
+ }
151
+ exports.transportIdentityJson = transportIdentityJson;
152
+ function __wbg_get_imports() {
153
+ const import0 = {
154
+ __proto__: null,
155
+ __wbg___wbindgen_is_undefined_c05833b95a3cf397: function(arg0) {
156
+ const ret = arg0 === undefined;
157
+ return ret;
158
+ },
159
+ __wbg___wbindgen_throw_344f42d3211c4765: function(arg0, arg1) {
160
+ throw new Error(getStringFromWasm0(arg0, arg1));
161
+ },
162
+ __wbg_now_e7c6795a7f81e10f: function(arg0) {
163
+ const ret = arg0.now();
164
+ return ret;
165
+ },
166
+ __wbg_performance_3fcf6e32a7e1ed0a: function(arg0) {
167
+ const ret = arg0.performance;
168
+ return ret;
169
+ },
170
+ __wbg_static_accessor_GLOBAL_4ef717fb391d88b7: function() {
171
+ const ret = typeof global === 'undefined' ? null : global;
172
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
173
+ },
174
+ __wbg_static_accessor_GLOBAL_THIS_8d1badc68b5a74f4: function() {
175
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
176
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
177
+ },
178
+ __wbg_static_accessor_SELF_146583524fe1469b: function() {
179
+ const ret = typeof self === 'undefined' ? null : self;
180
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
181
+ },
182
+ __wbg_static_accessor_WINDOW_f2829a2234d7819e: function() {
183
+ const ret = typeof window === 'undefined' ? null : window;
184
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
185
+ },
186
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
187
+ // Cast intrinsic for `Ref(String) -> Externref`.
188
+ const ret = getStringFromWasm0(arg0, arg1);
189
+ return ret;
190
+ },
191
+ __wbindgen_init_externref_table: function() {
192
+ const table = wasm.__wbindgen_externrefs;
193
+ const offset = table.grow(4);
194
+ table.set(0, undefined);
195
+ table.set(offset + 0, undefined);
196
+ table.set(offset + 1, null);
197
+ table.set(offset + 2, true);
198
+ table.set(offset + 3, false);
199
+ },
200
+ };
201
+ return {
202
+ __proto__: null,
203
+ "./merman_node_bg.js": import0,
204
+ };
205
+ }
206
+
207
+ const WasmEngineFinalization = (typeof FinalizationRegistry === 'undefined')
208
+ ? { register: () => {}, unregister: () => {} }
209
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmengine_free(ptr, 1));
210
+
211
+ function addToExternrefTable0(obj) {
212
+ const idx = wasm.__externref_table_alloc();
213
+ wasm.__wbindgen_externrefs.set(idx, obj);
214
+ return idx;
215
+ }
216
+
217
+ function getStringFromWasm0(ptr, len) {
218
+ return decodeText(ptr >>> 0, len);
219
+ }
220
+
221
+ let cachedUint8ArrayMemory0 = null;
222
+ function getUint8ArrayMemory0() {
223
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
224
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
225
+ }
226
+ return cachedUint8ArrayMemory0;
227
+ }
228
+
229
+ function isLikeNone(x) {
230
+ return x === undefined || x === null;
231
+ }
232
+
233
+ function passStringToWasm0(arg, malloc, realloc) {
234
+ if (realloc === undefined) {
235
+ const buf = cachedTextEncoder.encode(arg);
236
+ const ptr = malloc(buf.length, 1) >>> 0;
237
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
238
+ WASM_VECTOR_LEN = buf.length;
239
+ return ptr;
240
+ }
241
+
242
+ let len = arg.length;
243
+ let ptr = malloc(len, 1) >>> 0;
244
+
245
+ const mem = getUint8ArrayMemory0();
246
+
247
+ let offset = 0;
248
+
249
+ for (; offset < len; offset++) {
250
+ const code = arg.charCodeAt(offset);
251
+ if (code > 0x7F) break;
252
+ mem[ptr + offset] = code;
253
+ }
254
+ if (offset !== len) {
255
+ if (offset !== 0) {
256
+ arg = arg.slice(offset);
257
+ }
258
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
259
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
260
+ const ret = cachedTextEncoder.encodeInto(arg, view);
261
+
262
+ offset += ret.written;
263
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
264
+ }
265
+
266
+ WASM_VECTOR_LEN = offset;
267
+ return ptr;
268
+ }
269
+
270
+ function takeFromExternrefTable0(idx) {
271
+ const value = wasm.__wbindgen_externrefs.get(idx);
272
+ wasm.__externref_table_dealloc(idx);
273
+ return value;
274
+ }
275
+
276
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
277
+ cachedTextDecoder.decode();
278
+ function decodeText(ptr, len) {
279
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
280
+ }
281
+
282
+ const cachedTextEncoder = new TextEncoder();
283
+
284
+ if (!('encodeInto' in cachedTextEncoder)) {
285
+ cachedTextEncoder.encodeInto = function (arg, view) {
286
+ const buf = cachedTextEncoder.encode(arg);
287
+ view.set(buf);
288
+ return {
289
+ read: arg.length,
290
+ written: buf.length
291
+ };
292
+ };
293
+ }
294
+
295
+ let WASM_VECTOR_LEN = 0;
296
+
297
+ const wasmPath = `${__dirname}/merman_node_bg.wasm`;
298
+ const wasmBytes = require('fs').readFileSync(wasmPath);
299
+ const wasmModule = new WebAssembly.Module(wasmBytes);
300
+ let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());
301
+ let wasm = wasmInstance.exports;
302
+ wasm.__wbindgen_start();
Binary file
@@ -0,0 +1,135 @@
1
+ import {
2
+ MermanDisposedError,
3
+ MermanLifecycleError,
4
+ MermanQueueSaturatedError,
5
+ abortError,
6
+ } from "./errors.mjs";
7
+
8
+ export class BoundedExecutor {
9
+ #active = 0;
10
+ #concurrency;
11
+ #disposePromise = null;
12
+ #finishDispose = null;
13
+ #maxQueue;
14
+ #pending = [];
15
+ #state = "open";
16
+
17
+ constructor({ concurrency, maxQueue }) {
18
+ if (!Number.isSafeInteger(concurrency) || concurrency < 1) {
19
+ throw new RangeError("concurrency must be a positive integer.");
20
+ }
21
+ if (!Number.isSafeInteger(maxQueue) || maxQueue < 0) {
22
+ throw new RangeError("maxQueue must be a non-negative integer.");
23
+ }
24
+ this.#concurrency = concurrency;
25
+ this.#maxQueue = maxQueue;
26
+ }
27
+
28
+ get snapshot() {
29
+ return Object.freeze({
30
+ active: this.#active,
31
+ pending: this.#pending.length,
32
+ concurrency: this.#concurrency,
33
+ maxQueue: this.#maxQueue,
34
+ state: this.#state,
35
+ });
36
+ }
37
+
38
+ submit(run, { signal } = {}) {
39
+ if (this.#state !== "open") return Promise.reject(new MermanDisposedError());
40
+ if (signal?.aborted) return Promise.reject(abortError());
41
+
42
+ if (this.#active < this.#concurrency) {
43
+ return this.#start({ run, signal });
44
+ }
45
+ if (this.#pending.length >= this.#maxQueue) {
46
+ return Promise.reject(new MermanQueueSaturatedError(this.#maxQueue));
47
+ }
48
+
49
+ return new Promise((resolve, reject) => {
50
+ const job = { abortListener: null, reject, resolve, run, signal, started: false };
51
+ if (signal) {
52
+ job.abortListener = () => {
53
+ if (job.started) return;
54
+ const index = this.#pending.indexOf(job);
55
+ if (index === -1) return;
56
+ this.#pending.splice(index, 1);
57
+ reject(abortError());
58
+ };
59
+ signal.addEventListener("abort", job.abortListener, { once: true });
60
+ }
61
+ this.#pending.push(job);
62
+ // Close the race between the initial admission check and listener registration. Calling the
63
+ // idempotent listener is safe even when an abort event already removed the job.
64
+ if (signal?.aborted) job.abortListener?.();
65
+ });
66
+ }
67
+
68
+ assertSyncAvailable() {
69
+ this.assertOpen();
70
+ if (this.#active !== 0 || this.#pending.length !== 0) {
71
+ throw new MermanLifecycleError(
72
+ "renderSvgSync() cannot run while asynchronous operations are active or queued.",
73
+ );
74
+ }
75
+ }
76
+
77
+ assertOpen() {
78
+ if (this.#state !== "open") throw new MermanDisposedError();
79
+ }
80
+
81
+ dispose() {
82
+ if (this.#disposePromise) return this.#disposePromise;
83
+ this.#state = "disposing";
84
+ const disposed = new MermanDisposedError();
85
+ for (const job of this.#pending.splice(0)) {
86
+ this.#removeAbortListener(job);
87
+ job.reject(disposed);
88
+ }
89
+ this.#disposePromise = new Promise((resolve) => {
90
+ this.#finishDispose = resolve;
91
+ this.#completeDisposeIfIdle();
92
+ });
93
+ return this.#disposePromise;
94
+ }
95
+
96
+ #start(job) {
97
+ job.started = true;
98
+ this.#removeAbortListener(job);
99
+ this.#active += 1;
100
+ const operation = Promise.resolve().then(job.run);
101
+ const observed = job.resolve || job.reject
102
+ ? operation.then(job.resolve, job.reject)
103
+ : operation;
104
+ return observed.finally(() => {
105
+ this.#active -= 1;
106
+ this.#pump();
107
+ this.#completeDisposeIfIdle();
108
+ });
109
+ }
110
+
111
+ #pump() {
112
+ while (
113
+ this.#state === "open" &&
114
+ this.#active < this.#concurrency &&
115
+ this.#pending.length > 0
116
+ ) {
117
+ const job = this.#pending.shift();
118
+ void this.#start(job);
119
+ }
120
+ }
121
+
122
+ #completeDisposeIfIdle() {
123
+ if (this.#state !== "disposing" || this.#active !== 0) return;
124
+ this.#state = "disposed";
125
+ this.#finishDispose?.();
126
+ this.#finishDispose = null;
127
+ }
128
+
129
+ #removeAbortListener(job) {
130
+ if (job.signal && job.abortListener) {
131
+ job.signal.removeEventListener("abort", job.abortListener);
132
+ job.abortListener = null;
133
+ }
134
+ }
135
+ }
@@ -0,0 +1,87 @@
1
+ import path from "node:path";
2
+ import { pathToFileURL } from "node:url";
3
+
4
+ import {
5
+ MermanInvalidTransportError,
6
+ decodeWireCreationError,
7
+ validateTransportIdentityExport,
8
+ } from "../errors.mjs";
9
+ import {
10
+ assertRuntimePackageVersion,
11
+ nodeLoaderPackageVersion,
12
+ } from "../native-loader.mjs";
13
+ import { wrapCandidateEngine } from "./wrap-engine.mjs";
14
+
15
+ const WINDOWS_DRIVE_PATH = /^[a-zA-Z]:[\\/]/;
16
+ const URL_SCHEME = /^[a-zA-Z][a-zA-Z+.-]*:/;
17
+
18
+ export function nodeWasmModuleSpecifier(
19
+ modulePath,
20
+ { cwd = process.cwd() } = {},
21
+ ) {
22
+ if (WINDOWS_DRIVE_PATH.test(modulePath)) {
23
+ if (process.platform === "win32") return pathToFileURL(modulePath).href;
24
+ const normalized = path.win32.normalize(modulePath).replaceAll("\\", "/");
25
+ return pathToFileURL(`/${normalized}`).href;
26
+ }
27
+ if (URL_SCHEME.test(modulePath)) return modulePath;
28
+ return pathToFileURL(path.resolve(cwd, modulePath)).href;
29
+ }
30
+
31
+ export async function loadNodeWasmTransport(
32
+ optionsJson,
33
+ {
34
+ modulePath = process.env.MERMAN_NODE_WASM_BINDING,
35
+ loadModule = (specifier) => import(specifier),
36
+ } = {},
37
+ ) {
38
+ if (!modulePath) {
39
+ throw new MermanInvalidTransportError(
40
+ "MERMAN_NODE_WASM_BINDING must point to the explicit Node-targeted WASM candidate.",
41
+ );
42
+ }
43
+ if (modulePath.includes("@mermanjs/web") || modulePath.includes("platforms/web")) {
44
+ throw new MermanInvalidTransportError(
45
+ "Browser WASM packages cannot be used as the Node-targeted WASM candidate.",
46
+ );
47
+ }
48
+ const specifier = nodeWasmModuleSpecifier(modulePath);
49
+ const binding = await loadModule(specifier);
50
+ const WasmEngine = binding?.WasmEngine ?? binding?.default?.WasmEngine;
51
+ if (typeof WasmEngine !== "function") {
52
+ throw new MermanInvalidTransportError(
53
+ "The Node-targeted WASM candidate does not export WasmEngine.",
54
+ );
55
+ }
56
+ validateTransportIdentityExport(binding?.transportIdentityJson, {
57
+ expectedPackageVersion: nodeLoaderPackageVersion(),
58
+ expectedTransport: "wasm",
59
+ label: "The Node-targeted WASM candidate",
60
+ });
61
+ let transport;
62
+ try {
63
+ transport = wrapCandidateEngine(
64
+ new WasmEngine(optionsJson),
65
+ "The Node-targeted WASM candidate",
66
+ { forwardsAbortSignal: false },
67
+ );
68
+ } catch (cause) {
69
+ throw decodeWireCreationError(cause, "The Node-targeted WASM candidate");
70
+ }
71
+ try {
72
+ const runtimeCatalogJson = assertRuntimePackageVersion(
73
+ transport.runtimeCatalogJson(),
74
+ );
75
+ return {
76
+ ...transport,
77
+ runtimeCatalogJson: () => runtimeCatalogJson,
78
+ };
79
+ } catch (cause) {
80
+ try {
81
+ await transport.dispose();
82
+ } catch {
83
+ // Preserve the package-version failure that made the transport unusable.
84
+ }
85
+ throw cause;
86
+ }
87
+ }
@@ -0,0 +1,36 @@
1
+ import { MermanDisposedError, MermanInvalidTransportError } from "../errors.mjs";
2
+
3
+ export function wrapCandidateEngine(
4
+ engine,
5
+ label,
6
+ { forwardsAbortSignal = false } = {},
7
+ ) {
8
+ if (
9
+ typeof engine?.execute !== "function" ||
10
+ typeof engine?.executeSync !== "function" ||
11
+ typeof engine?.runtimeCatalogJson !== "function" ||
12
+ typeof engine?.metadataJson !== "function"
13
+ ) {
14
+ throw new MermanInvalidTransportError(`${label} does not implement the operation contract.`);
15
+ }
16
+ let ownedEngine = engine;
17
+ const currentEngine = () => {
18
+ if (!ownedEngine) throw new MermanDisposedError();
19
+ return ownedEngine;
20
+ };
21
+ return {
22
+ execute: (requestJson, signal, timeoutMs) =>
23
+ forwardsAbortSignal
24
+ ? currentEngine().execute(requestJson, signal, timeoutMs)
25
+ : currentEngine().execute(requestJson, timeoutMs),
26
+ executeSync: (requestJson, timeoutMs) =>
27
+ currentEngine().executeSync(requestJson, timeoutMs),
28
+ runtimeCatalogJson: () => currentEngine().runtimeCatalogJson(),
29
+ metadataJson: (id) => currentEngine().metadataJson(id),
30
+ dispose: () => {
31
+ const disposing = ownedEngine;
32
+ ownedEngine = null;
33
+ return disposing?.dispose?.();
34
+ },
35
+ };
36
+ }