@mermanjs/node-wasm 0.9.2 → 0.10.0-alpha.1

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.
package/dist/engine.mjs CHANGED
@@ -93,6 +93,7 @@ const MERMAN_ENGINE_CONSTRUCTION_TOKEN = Symbol("MermanEngine construction");
93
93
  export async function createNodeEngine(
94
94
  {
95
95
  bindingOptions = {},
96
+ iconPacks,
96
97
  concurrency = 1,
97
98
  maxQueue = 64,
98
99
  } = {},
@@ -107,7 +108,8 @@ export async function createNodeEngine(
107
108
  "binding options",
108
109
  NODE_TRANSPORT_LIMITS.binding_options,
109
110
  );
110
- const transport = await loadTransport(optionsJson);
111
+ const iconPacksJson = encodeIconPacks(iconPacks);
112
+ const transport = await loadTransport(optionsJson, iconPacksJson);
111
113
  try {
112
114
  assertTransport(transport);
113
115
  const runtimeCatalog = validateRuntimeCatalog(await transport.runtimeCatalogJson());
@@ -123,6 +125,28 @@ export async function createNodeEngine(
123
125
  }
124
126
  }
125
127
 
128
+ function encodeIconPacks(packs) {
129
+ if (packs === undefined) return undefined;
130
+ if (!Array.isArray(packs)) throw new TypeError("iconPacks must be an array.");
131
+ const normalized = [];
132
+ for (const pack of packs) {
133
+ if (!isPlainObject(pack) || typeof pack.json !== "string") {
134
+ throw new TypeError("iconPacks entries must contain an already loaded JSON string.");
135
+ }
136
+ if (Object.keys(pack).some((key) => key !== "json" && key !== "registrationName")) {
137
+ throw new TypeError("iconPacks entries only accept json and registrationName.");
138
+ }
139
+ if (pack.registrationName !== undefined && typeof pack.registrationName !== "string") {
140
+ throw new TypeError("iconPacks registrationName must be a string.");
141
+ }
142
+ normalized.push({
143
+ json: pack.json,
144
+ ...(pack.registrationName === undefined ? {} : { registrationName: pack.registrationName }),
145
+ });
146
+ }
147
+ return JSON.stringify(normalized);
148
+ }
149
+
126
150
  export function normalizeBindingOptions(value = {}) {
127
151
  if (!isPlainObject(value)) throw new TypeError("bindingOptions must be a plain object.");
128
152
  const normalized = cloneBoundedJsonValue(value, "bindingOptions");
@@ -152,10 +176,11 @@ export function normalizeBindingOptions(value = {}) {
152
176
  }
153
177
  if (
154
178
  normalized.environment.text_measurement !== undefined &&
179
+ normalized.environment.text_measurement !== null &&
155
180
  normalized.environment.text_measurement !== "deterministic"
156
181
  ) {
157
182
  throw new RangeError(
158
- "bindingOptions.environment.text_measurement must be `deterministic`.",
183
+ "bindingOptions.environment.text_measurement must be `deterministic` or null.",
159
184
  );
160
185
  }
161
186
  }
@@ -194,39 +219,35 @@ export class MermanEngine {
194
219
  }
195
220
 
196
221
  renderSvg(source, options = {}) {
197
- return this.executeOperation(
222
+ return this.#execute(
198
223
  { operationId: "svg", source, optionsJson: options.optionsJson },
199
224
  { signal: options.signal, timeoutMs: options.timeoutMs },
200
- ).then((result) => result.data);
225
+ false,
226
+ );
201
227
  }
202
228
 
203
229
  renderSvgSync(source, options = {}) {
204
- return this.executeOperationSync(
205
- {
206
- operationId: "svg",
207
- source,
208
- optionsJson: options.optionsJson,
209
- },
230
+ return this.#executeSync(
231
+ { operationId: "svg", source, optionsJson: options.optionsJson },
210
232
  { timeoutMs: options.timeoutMs },
211
- ).data;
233
+ false,
234
+ );
212
235
  }
213
236
 
214
237
  svgPlanJson(source, options = {}) {
215
- return this.executeOperation(
238
+ return this.#execute(
216
239
  { operationId: "svg-plan-json", source, optionsJson: options.optionsJson },
217
240
  { signal: options.signal, timeoutMs: options.timeoutMs },
218
- ).then((result) => result.data);
241
+ false,
242
+ );
219
243
  }
220
244
 
221
245
  svgPlanJsonSync(source, options = {}) {
222
- return this.executeOperationSync(
223
- {
224
- operationId: "svg-plan-json",
225
- source,
226
- optionsJson: options.optionsJson,
227
- },
246
+ return this.#executeSync(
247
+ { operationId: "svg-plan-json", source, optionsJson: options.optionsJson },
228
248
  { timeoutMs: options.timeoutMs },
229
- ).data;
249
+ false,
250
+ );
230
251
  }
231
252
 
232
253
  metadataJson(id) {
@@ -253,7 +274,11 @@ export class MermanEngine {
253
274
  return value;
254
275
  }
255
276
 
256
- executeOperation(request, { signal, timeoutMs } = {}) {
277
+ executeOperation(request, options = {}) {
278
+ return this.#execute(request, options, true);
279
+ }
280
+
281
+ #execute(request, { signal, timeoutMs } = {}, includeMetadata) {
257
282
  try {
258
283
  this.#executor.assertOpen();
259
284
  } catch (error) {
@@ -261,7 +286,7 @@ export class MermanEngine {
261
286
  }
262
287
  const prepared = prepareOperationRequest(request, { timeoutMs });
263
288
  if (signal?.aborted) return Promise.reject(abortError());
264
- const encoded = operationRequestJson(prepared);
289
+ const encoded = operationRequestJson(prepared, includeMetadata);
265
290
  return this.#executor.submit(
266
291
  async () => {
267
292
  // A signal can flip after queue admission but before this start microtask runs. That work
@@ -279,17 +304,26 @@ export class MermanEngine {
279
304
  throw decodeWireInvocationError(cause, "Merman operation");
280
305
  }
281
306
  return decodeWireResponse(responseJson, encoded.expectation, {
307
+ includeMetadata,
282
308
  allowedCancellationReasons: invocationCancellationReasons(signal, timeoutMs),
283
309
  requireUnavailable: !this.#operationIds.has(encoded.expectation.operation_id),
310
+ availableCapabilityIds: this.#runtimeCatalog.capabilities.capability_ids,
284
311
  });
285
312
  },
286
313
  { signal },
287
314
  );
288
315
  }
289
316
 
290
- executeOperationSync(request, { timeoutMs } = {}) {
317
+ executeOperationSync(request, options = {}) {
318
+ return this.#executeSync(request, options, true);
319
+ }
320
+
321
+ #executeSync(request, { timeoutMs } = {}, includeMetadata) {
291
322
  this.#executor.assertSyncAvailable();
292
- const encoded = operationRequestJson(prepareOperationRequest(request, { timeoutMs }));
323
+ const encoded = operationRequestJson(
324
+ prepareOperationRequest(request, { timeoutMs }),
325
+ includeMetadata,
326
+ );
293
327
  let responseJson;
294
328
  try {
295
329
  responseJson = this.#transport.executeSync(encoded.json, timeoutMs);
@@ -297,8 +331,10 @@ export class MermanEngine {
297
331
  throw decodeWireInvocationError(cause, "Merman operation");
298
332
  }
299
333
  return decodeWireResponse(responseJson, encoded.expectation, {
334
+ includeMetadata,
300
335
  allowedCancellationReasons: invocationCancellationReasons(undefined, timeoutMs),
301
336
  requireUnavailable: !this.#operationIds.has(encoded.expectation.operation_id),
337
+ availableCapabilityIds: this.#runtimeCatalog.capabilities.capability_ids,
302
338
  });
303
339
  }
304
340
 
@@ -384,7 +420,8 @@ function prepareOperationRequest(value, { timeoutMs } = {}) {
384
420
  return { expectation, request, value };
385
421
  }
386
422
 
387
- function operationRequestJson({ expectation, request, value }) {
423
+ function operationRequestJson({ expectation, request, value }, includeMetadata) {
424
+ if (!includeMetadata) request.include_metadata = false;
388
425
  const optionsJson = value.optionsJson;
389
426
  if (optionsJson !== undefined) {
390
427
  if (typeof optionsJson !== "string") {
package/dist/errors.mjs CHANGED
@@ -102,7 +102,9 @@ export class MermanOperationError extends MermanError {
102
102
  this.codeName = payload.code_name ?? null;
103
103
  this.kind = payload.kind ?? "generic";
104
104
  this.capabilityId = payload.capability_id ?? null;
105
+ this.details = payload.details ?? null;
105
106
  this.resourceDetails = payload.details?.resource ?? null;
107
+ this.iconRegistryDetails = payload.details?.icon_registry ?? null;
106
108
  this.diagnosticDetails = payload.details?.diagnostic ?? null;
107
109
  this.cancellationDetails = payload.details?.cancellation ?? null;
108
110
  }
@@ -132,7 +134,7 @@ export class MermanLifecycleError extends MermanError {
132
134
  export class MermanUnsupportedTargetError extends MermanError {
133
135
  constructor({ platform, arch, libc = null, reason = null }) {
134
136
  const target = [platform, arch, libc].filter(Boolean).join("-");
135
- super(reason ?? `Unsupported Merman Node target: ${target}.`, {
137
+ super(`${reason ?? `Unsupported Merman Node target: ${target}.`} Use @mermanjs/node-wasm explicitly for a portable Node transport.`, {
136
138
  code: "MERMAN_UNSUPPORTED_TARGET",
137
139
  });
138
140
  this.platform = platform;
@@ -172,6 +174,18 @@ export class MermanInvalidTransportError extends MermanError {
172
174
  export { NODE_TRANSPORT_FIELD_LIMITS, NODE_TRANSPORT_LIMITS, NODE_WIRE_CONTRACT };
173
175
 
174
176
  export function parseTransportJsonText(value, label, limits) {
177
+ assertTransportJsonTextLimits(value, label, limits);
178
+ try {
179
+ return JSON.parse(value);
180
+ } catch (cause) {
181
+ throw new MermanInvalidTransportError(
182
+ `Merman transport returned invalid ${label} JSON.`,
183
+ cause,
184
+ );
185
+ }
186
+ }
187
+
188
+ function assertTransportJsonTextLimits(value, label, limits) {
175
189
  if (typeof value !== "string") {
176
190
  throw new MermanInvalidTransportError(`Merman transport ${label} must be JSON text.`);
177
191
  }
@@ -185,14 +199,6 @@ export function parseTransportJsonText(value, label, limits) {
185
199
  );
186
200
  }
187
201
  scanBoundedJsonText(value, label, limits);
188
- try {
189
- return JSON.parse(value);
190
- } catch (cause) {
191
- throw new MermanInvalidTransportError(
192
- `Merman transport returned invalid ${label} JSON.`,
193
- cause,
194
- );
195
- }
196
202
  }
197
203
 
198
204
  export function encodeTransportJson(value, label, limits) {
@@ -218,7 +224,8 @@ export function encodeTransportJson(value, label, limits) {
218
224
  cause,
219
225
  );
220
226
  }
221
- parseTransportJsonText(text, label, limits);
227
+ // JSON.stringify guarantees syntax for the controlled clone; retain all wire-limit checks.
228
+ assertTransportJsonTextLimits(text, label, limits);
222
229
  return text;
223
230
  }
224
231
 
@@ -863,7 +870,12 @@ export function abortError() {
863
870
  export function decodeWireResponse(
864
871
  value,
865
872
  expectation,
866
- { allowedCancellationReasons = [], requireUnavailable = false } = {},
873
+ {
874
+ includeMetadata = true,
875
+ allowedCancellationReasons = [],
876
+ requireUnavailable = false,
877
+ availableCapabilityIds = [],
878
+ } = {},
867
879
  ) {
868
880
  const cancellationReasons = validateAllowedCancellationReasons(allowedCancellationReasons);
869
881
  const envelope = parseTransportJsonText(
@@ -889,6 +901,7 @@ export function decodeWireResponse(
889
901
  expectation,
890
902
  requireUnavailable,
891
903
  cancellationReasons,
904
+ availableCapabilityIds,
892
905
  );
893
906
  throw new MermanOperationError(error);
894
907
  }
@@ -902,7 +915,7 @@ export function decodeWireResponse(
902
915
  "Merman transport executed an operation that its runtime catalog does not advertise.",
903
916
  );
904
917
  }
905
- return validateSuccessResult(envelope.result, expectation);
918
+ return validateSuccessResult(envelope.result, expectation, includeMetadata);
906
919
  }
907
920
 
908
921
  function validateAllowedCancellationReasons(reasons) {
@@ -958,7 +971,7 @@ function validateEnvelopeHeader(envelope, label) {
958
971
  }
959
972
  }
960
973
 
961
- function validateSuccessResult(result, expectation) {
974
+ function validateSuccessResult(result, expectation, includeMetadata) {
962
975
  if (
963
976
  !isPlainJsonObject(result) ||
964
977
  typeof result.operation_id !== "string" ||
@@ -966,7 +979,7 @@ function validateSuccessResult(result, expectation) {
966
979
  typeof result.media_type !== "string" ||
967
980
  result.media_type.length === 0 ||
968
981
  typeof result.data !== "string" ||
969
- typeof result.metadata_json !== "string"
982
+ (includeMetadata && typeof result.metadata_json !== "string")
970
983
  ) {
971
984
  throw new MermanInvalidTransportError("Merman transport returned an invalid result envelope.");
972
985
  }
@@ -985,11 +998,6 @@ function validateSuccessResult(result, expectation) {
985
998
  "response data",
986
999
  NODE_TRANSPORT_FIELD_LIMITS.data_utf8_bytes,
987
1000
  );
988
- assertUtf8Field(
989
- result.metadata_json,
990
- "response metadata_json",
991
- NODE_TRANSPORT_FIELD_LIMITS.metadata_json_utf8_bytes,
992
- );
993
1001
  if (
994
1002
  !expectation ||
995
1003
  result.operation_id !== expectation.operation_id ||
@@ -999,6 +1007,19 @@ function validateSuccessResult(result, expectation) {
999
1007
  "Merman transport result identity does not match the requested operation contract.",
1000
1008
  );
1001
1009
  }
1010
+ if (!includeMetadata) {
1011
+ if (Object.hasOwn(result, "metadata_json")) {
1012
+ throw new MermanInvalidTransportError(
1013
+ "Merman transport returned metadata for a data-only operation.",
1014
+ );
1015
+ }
1016
+ return result.data;
1017
+ }
1018
+ assertUtf8Field(
1019
+ result.metadata_json,
1020
+ "response metadata_json",
1021
+ NODE_TRANSPORT_FIELD_LIMITS.metadata_json_utf8_bytes,
1022
+ );
1002
1023
  const metadata = parseTransportJsonText(
1003
1024
  result.metadata_json,
1004
1025
  "nested operation metadata",
@@ -1031,6 +1052,7 @@ function validateErrorPayload(
1031
1052
  expectation = null,
1032
1053
  requireUnavailable = false,
1033
1054
  allowedCancellationReasons = new Set(),
1055
+ availableCapabilityIds = [],
1034
1056
  ) {
1035
1057
  if (
1036
1058
  !isPlainJsonObject(error) ||
@@ -1095,7 +1117,11 @@ function validateErrorPayload(
1095
1117
  }
1096
1118
  } else if (
1097
1119
  expectation &&
1098
- (error.kind === "missing-capability" || error.kind === "unknown-operation")
1120
+ (error.kind === "unknown-operation" ||
1121
+ (error.kind === "missing-capability" &&
1122
+ (error.capability_id === expectation.availability_capability_id ||
1123
+ // The environment policy may disable a compiled math backend for this request.
1124
+ (error.capability_id !== "math" && availableCapabilityIds.includes(error.capability_id)))))
1099
1125
  ) {
1100
1126
  throw new MermanInvalidTransportError(
1101
1127
  "Merman transport contradicted its advertised operation catalog.",