@mermanjs/node-wasm 0.10.0 → 0.12.0

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.
@@ -3,6 +3,7 @@ import {
3
3
  MermanLifecycleError,
4
4
  MermanQueueSaturatedError,
5
5
  abortError,
6
+ normalizeOperationSignal,
6
7
  } from "./errors.mjs";
7
8
 
8
9
  export class BoundedExecutor {
@@ -37,6 +38,11 @@ export class BoundedExecutor {
37
38
 
38
39
  submit(run, { signal } = {}) {
39
40
  if (this.#state !== "open") return Promise.reject(new MermanDisposedError());
41
+ try {
42
+ signal = normalizeOperationSignal(signal);
43
+ } catch (error) {
44
+ return Promise.reject(error);
45
+ }
40
46
  if (signal?.aborted) return Promise.reject(abortError());
41
47
 
42
48
  if (this.#active < this.#concurrency) {
@@ -65,11 +71,11 @@ export class BoundedExecutor {
65
71
  });
66
72
  }
67
73
 
68
- assertSyncAvailable() {
74
+ assertSyncAvailable(method) {
69
75
  this.assertOpen();
70
76
  if (this.#active !== 0 || this.#pending.length !== 0) {
71
77
  throw new MermanLifecycleError(
72
- "renderSvgSync() cannot run while asynchronous operations are active or queued.",
78
+ `${method}() cannot run while asynchronous operations are active or queued.`,
73
79
  );
74
80
  }
75
81
  }
@@ -98,14 +104,13 @@ export class BoundedExecutor {
98
104
  this.#removeAbortListener(job);
99
105
  this.#active += 1;
100
106
  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(() => {
107
+ // Release capacity and admit FIFO successors before callers observe either outcome.
108
+ const finished = operation.finally(() => {
105
109
  this.#active -= 1;
106
110
  this.#pump();
107
111
  this.#completeDisposeIfIdle();
108
112
  });
113
+ return job.resolve || job.reject ? finished.then(job.resolve, job.reject) : finished;
109
114
  }
110
115
 
111
116
  #pump() {
@@ -7,7 +7,7 @@ import {
7
7
  validateTransportIdentityExport,
8
8
  } from "../errors.mjs";
9
9
  import {
10
- assertRuntimePackageVersion,
10
+ cacheRuntimeCatalog,
11
11
  nodeLoaderPackageVersion,
12
12
  } from "../native-loader.mjs";
13
13
  import { wrapCandidateEngine } from "./wrap-engine.mjs";
@@ -33,6 +33,8 @@ export async function loadNodeWasmTransport(
33
33
  {
34
34
  modulePath = process.env.MERMAN_NODE_WASM_BINDING,
35
35
  loadModule = (specifier) => import(specifier),
36
+ localLayout = null,
37
+ directSvgResponse = false,
36
38
  } = {},
37
39
  iconPacksJson,
38
40
  ) {
@@ -58,6 +60,7 @@ export async function loadNodeWasmTransport(
58
60
  expectedPackageVersion: nodeLoaderPackageVersion(),
59
61
  expectedTransport: "wasm",
60
62
  label: "The Node-targeted WASM candidate",
63
+ localLayout,
61
64
  });
62
65
  let transport;
63
66
  try {
@@ -66,19 +69,13 @@ export async function loadNodeWasmTransport(
66
69
  ? new WasmEngine(optionsJson)
67
70
  : new WasmEngine(optionsJson, iconPacksJson),
68
71
  "The Node-targeted WASM candidate",
69
- { forwardsAbortSignal: false },
72
+ { forwardsAbortSignal: false, directSvgResponse },
70
73
  );
71
74
  } catch (cause) {
72
75
  throw decodeWireCreationError(cause, "The Node-targeted WASM candidate");
73
76
  }
74
77
  try {
75
- const runtimeCatalogJson = assertRuntimePackageVersion(
76
- transport.runtimeCatalogJson(),
77
- );
78
- return {
79
- ...transport,
80
- runtimeCatalogJson: () => runtimeCatalogJson,
81
- };
78
+ return cacheRuntimeCatalog(transport);
82
79
  } catch (cause) {
83
80
  try {
84
81
  await transport.dispose();
@@ -3,7 +3,7 @@ import { MermanDisposedError, MermanInvalidTransportError } from "../errors.mjs"
3
3
  export function wrapCandidateEngine(
4
4
  engine,
5
5
  label,
6
- { forwardsAbortSignal = false } = {},
6
+ { forwardsAbortSignal = false, directSvgAddon = false, directSvgResponse = false } = {},
7
7
  ) {
8
8
  if (
9
9
  typeof engine?.execute !== "function" ||
@@ -18,7 +18,13 @@ export function wrapCandidateEngine(
18
18
  if (!ownedEngine) throw new MermanDisposedError();
19
19
  return ownedEngine;
20
20
  };
21
- return {
21
+ const executeSvg = directSvgAddon ? engine.executeSvg : undefined;
22
+ const executeSvgSync = directSvgAddon ? engine.executeSvgSync : undefined;
23
+ if (directSvgAddon && (typeof executeSvg !== "function" || typeof executeSvgSync !== "function")) {
24
+ throw new MermanInvalidTransportError(`${label} does not implement the direct SVG contract.`);
25
+ }
26
+ const transport = {
27
+ __proto__: null,
22
28
  execute: (requestJson, signal, timeoutMs) =>
23
29
  forwardsAbortSignal
24
30
  ? currentEngine().execute(requestJson, signal, timeoutMs)
@@ -30,7 +36,49 @@ export function wrapCandidateEngine(
30
36
  dispose: () => {
31
37
  const disposing = ownedEngine;
32
38
  ownedEngine = null;
33
- return disposing?.dispose?.();
39
+ try {
40
+ return disposing?.dispose?.();
41
+ } finally {
42
+ disposing?.free?.();
43
+ }
34
44
  },
35
45
  };
46
+ // wasm-bindgen installs the optional method directly on the engine's prototype.
47
+ const wasmPrototype = directSvgResponse ? Object.getPrototypeOf(engine) : null;
48
+ const responseMethod = wasmPrototype && wasmPrototype !== Object.prototype
49
+ ? Object.getOwnPropertyDescriptor(wasmPrototype, "executeSvgResponseSync")
50
+ : undefined;
51
+ if (responseMethod && Object.hasOwn(responseMethod, "value") && typeof responseMethod.value === "function") {
52
+ const executeSvgResponseSync = responseMethod.value;
53
+ transport.executeSvgResponseSync = (requestJson, timeoutMs) => {
54
+ const engine = currentEngine();
55
+ try {
56
+ return executeSvgResponseSync.call(engine, requestJson, timeoutMs);
57
+ } catch (cause) {
58
+ // wasm-bindgen throws the Rust operation error envelope as a string.
59
+ if (typeof cause === "string") return { errorJson: cause };
60
+ throw cause;
61
+ }
62
+ };
63
+ }
64
+ if (directSvgAddon) {
65
+ transport.executeSvg = (source, optionsJson, signal, timeoutMs) =>
66
+ executeSvg.call(currentEngine(), source, optionsJson, signal, timeoutMs);
67
+ transport.executeSvgSync = (source, optionsJson, timeoutMs) =>
68
+ executeSvgSync.call(currentEngine(), source, optionsJson, timeoutMs);
69
+ const { executeSvgWithId, executeSvgWithIdSync, executeDrawList, executeDrawListSync } = engine;
70
+ if (typeof executeSvgWithId === "function" && typeof executeSvgWithIdSync === "function") {
71
+ transport.executeSvgWithId = (source, diagramId, signal, timeoutMs) =>
72
+ executeSvgWithId.call(currentEngine(), source, diagramId, signal, timeoutMs);
73
+ transport.executeSvgWithIdSync = (source, diagramId, timeoutMs) =>
74
+ executeSvgWithIdSync.call(currentEngine(), source, diagramId, timeoutMs);
75
+ }
76
+ if (typeof executeDrawList === "function" && typeof executeDrawListSync === "function") {
77
+ transport.executeDrawList = (source, optionsJson, signal, timeoutMs) =>
78
+ executeDrawList.call(currentEngine(), source, optionsJson, signal, timeoutMs);
79
+ transport.executeDrawListSync = (source, optionsJson, timeoutMs) =>
80
+ executeDrawListSync.call(currentEngine(), source, optionsJson, timeoutMs);
81
+ }
82
+ }
83
+ return transport;
36
84
  }