space-data-module-sdk 0.9.6 → 0.9.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "space-data-module-sdk",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Module SDK for building, validating, signing, and deploying WebAssembly modules on the Space Data Network.",
6
6
  "type": "module",
@@ -29,6 +29,10 @@ import {
29
29
  } from "../bundle/signing.js";
30
30
  import { attachHostDispatch, inspectModule } from "./isomorphicLoaderCore.js";
31
31
  import { assertArtifactRuntimeTarget } from "./runtimeTargetGate.js";
32
+ import { DEFAULT_HOSTCALL_IMPORT_MODULE } from "./abi.js";
33
+
34
+ /** The typed failure the WasmEdge command harness's runner answers for any operation but plugin.getConfig. */
35
+ export const WASMEDGE_UNSUPPORTED_MESSAGE = "the WasmEdge command harness serves only plugin.getConfig";
32
36
 
33
37
  export { inspectModule, attachHostDispatch };
34
38
 
@@ -67,38 +71,73 @@ async function createWasmEdgeCommandHarness(options = {}) {
67
71
 
68
72
  const threaded = inspection.imports.some((entry) =>
69
73
  entry.module === "wasi" && entry.name === "thread-spawn");
74
+ // The plain WasmEdge CLI provides WASI and nothing else: an artifact that
75
+ // imports the typed hostcall bridge (space_data_module_host) fails to
76
+ // instantiate there ("unknown import"). The SDK's runner serves the bridge,
77
+ // threaded or not, so a bridged artifact always runs on it; plain wasmedge
78
+ // stays for WASI-only artifacts.
79
+ const bridged = inspection.imports.some((entry) =>
80
+ entry.module === DEFAULT_HOSTCALL_IMPORT_MODULE);
81
+ const useRunner = threaded || bridged;
70
82
  const hasCommand = inspection.exports.includes(DefaultInvokeExports.commandSymbol);
71
83
  const hasDirect = inspection.exports.includes(DefaultInvokeExports.invokeSymbol);
72
84
  // The one-shot wasi-threads runner serves the direct surface of a reactor,
73
85
  // and of a command artifact when the caller asks for it (--sdm-direct): the
74
86
  // module's constructors then run through its initializer export, never main.
75
- const surface = hasCommand && !(options.surface === "direct" && threaded && hasDirect)
87
+ const surface = hasCommand && !(options.surface === "direct" && useRunner && hasDirect)
76
88
  ? "command"
77
89
  : "direct";
78
- if (surface === "direct" && (!threaded || !hasDirect)) {
90
+ if (surface === "direct" && (!useRunner || !hasDirect)) {
79
91
  throw new Error(
80
92
  "Standalone WasmEdge loading requires a command-surface artifact with the _start export.",
81
93
  );
82
94
  }
83
95
 
84
- const command = threaded
96
+ const command = useRunner
85
97
  ? await (await import("../testing/buildWasmEdgeRunner.js")).resolveWasmEdgeWasiThreadsRunner(options)
86
98
  : options.wasmEdgeRunnerBinary ?? options.wasmEdgeBinary ?? "wasmedge";
87
99
  let launchWasmPath = wasmPath;
88
100
  let tempArtifactDir = null;
101
+ const ensureTempDir = async () => {
102
+ if (!tempArtifactDir) {
103
+ const [{ mkdtemp }, osModule] = await Promise.all([
104
+ import("node:fs/promises"),
105
+ import("node:os"),
106
+ ]);
107
+ const os = osModule.default ?? osModule;
108
+ tempArtifactDir = await mkdtemp(path.join(os.tmpdir(), "sdm-module-"));
109
+ }
110
+ return tempArtifactDir;
111
+ };
89
112
  if (loadableBytes.byteLength !== wasmBytes.byteLength) {
90
- const [{ mkdtemp, writeFile }, osModule] = await Promise.all([
113
+ const { writeFile } = await import("node:fs/promises");
114
+ launchWasmPath = path.join(await ensureTempDir(), "module.wasm");
115
+ await writeFile(launchWasmPath, loadableBytes);
116
+ }
117
+ // The runner replays staged $PIV answers for the bridge, as on the parity
118
+ // lanes: plugin.getConfig answers the module's config (options.config, else
119
+ // the host's plugin.getConfig(), else {}), and every other operation answers
120
+ // a typed "unsupported" failure.
121
+ const hostcallArgs = [];
122
+ if (bridged) {
123
+ const [{ writeFile }, { encodeParityHostcallAnswers, PARITY_HOSTCALL_FILES }] = await Promise.all([
91
124
  import("node:fs/promises"),
92
- import("node:os"),
125
+ import("../testing/parityHostcalls.js"),
93
126
  ]);
94
- const os = osModule.default ?? osModule;
95
- tempArtifactDir = await mkdtemp(path.join(os.tmpdir(), "sdm-module-"));
96
- launchWasmPath = path.join(tempArtifactDir, "module.wasm");
97
- await writeFile(launchWasmPath, loadableBytes);
127
+ const config = options.config ??
128
+ (typeof options.host?.plugin?.getConfig === "function" ? await options.host.plugin.getConfig() : {});
129
+ const answers = encodeParityHostcallAnswers(config, WASMEDGE_UNSUPPORTED_MESSAGE);
130
+ const dir = await ensureTempDir();
131
+ const configPath = path.join(dir, PARITY_HOSTCALL_FILES.config);
132
+ const unsupportedPath = path.join(dir, PARITY_HOSTCALL_FILES.unsupported);
133
+ await writeFile(configPath, answers.config);
134
+ await writeFile(unsupportedPath, answers.unsupported);
135
+ hostcallArgs.push("--sdm-hostcall-config", configPath, "--sdm-hostcall-unsupported", unsupportedPath);
98
136
  }
99
137
 
100
138
  const args = [
101
139
  ...(surface === "direct" && hasCommand ? ["--sdm-direct"] : []),
140
+ ...hostcallArgs,
102
141
  ...(options.enableThreads === false ? [] : ["--enable-threads"]),
103
142
  ...Object.entries(options.env ?? {}).flatMap(([key, value]) => ["--env", `${key}=${value}`]),
104
143
  launchWasmPath,
@@ -202,6 +241,8 @@ async function createWasmEdgeCommandHarness(options = {}) {
202
241
  * @param {string[]} [options.args] - WASI args.
203
242
  * @param {Object} [options.env] - WASI environment variables.
204
243
  * @param {string} [options.surface] - "direct" or "command".
244
+ * @param {Object} [options.config] - The module config plugin.getConfig answers
245
+ * on the WasmEdge leg (default: options.host's plugin.getConfig(), else {}).
205
246
  * @param {Object} [options.runtimeHost] - Runtime host for row/region ops.
206
247
  * @returns {Promise<Object>} Harness with invoke(), readManifest(), destroy().
207
248
  */
@@ -278,10 +319,12 @@ export async function loadModule(options = {}) {
278
319
  if (needsCompiledModule) {
279
320
  const inspection = await inspectModule(wasmModule);
280
321
  const threaded = inspection.imports.some((entry) => entry.module === "wasi" && entry.name === "thread-spawn");
322
+ const bridged = inspection.imports.some((entry) => entry.module === DEFAULT_HOSTCALL_IMPORT_MODULE);
323
+ const useRunner = threaded || bridged;
281
324
  if (
282
325
  (inspection.profile === "standalone" || inspection.profile === "module-host-abi") &&
283
- (inspection.exports.includes("_start") || (threaded && inspection.exports.includes("plugin_invoke_stream"))) &&
284
- (!options.wasmEdgeRunnerBinary || options.surface === "command" || threaded)
326
+ (inspection.exports.includes("_start") || (useRunner && inspection.exports.includes("plugin_invoke_stream"))) &&
327
+ (!options.wasmEdgeRunnerBinary || options.surface === "command" || useRunner)
285
328
  ) {
286
329
  return attachHostDispatch(
287
330
  await createWasmEdgeCommandHarness(options),
@@ -76,6 +76,8 @@ export async function createStandaloneHarness(runtimeKind, wasmPath, options = {
76
76
  args: options.args,
77
77
  env: options.env,
78
78
  cwd: options.cwd,
79
+ // plugin.getConfig answers the same config the browser lane's host serves (hostOptions.config).
80
+ config: options.config ?? options.hostOptions?.config ?? options.host?.plugin?.getConfig?.(),
79
81
  });
80
82
  }
81
83
 
@@ -27,8 +27,11 @@ export function parityHostcallDispatch(config) {
27
27
  };
28
28
  }
29
29
 
30
- /** The $PIV answers the WasmEdge runner replays for one case: {config, unsupported} bytes. */
31
- export function encodeParityHostcallAnswers(config) {
30
+ /**
31
+ * The $PIV answers the WasmEdge runner replays for one case: {config, unsupported} bytes. `message` is the unsupported
32
+ * failure's text (the parity harness's by default).
33
+ */
34
+ export function encodeParityHostcallAnswers(config, message = PARITY_UNSUPPORTED_MESSAGE) {
32
35
  const failure = (error) => encodeHostCallResult({ ok: false, error: { code: hostcallErrorCode(error), message: String(error?.message ?? error) } });
33
36
  let answer;
34
37
  try {
@@ -36,5 +39,5 @@ export function encodeParityHostcallAnswers(config) {
36
39
  } catch (error) {
37
40
  answer = failure(error);
38
41
  }
39
- return { config: answer, unsupported: failure(unsupported()) };
42
+ return { config: answer, unsupported: failure(Object.assign(new Error(message), { hostcallCode: "unsupported" })) };
40
43
  }