libfx 0.0.15 → 0.0.16

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/fx-term.wasm CHANGED
Binary file
package/node-wasm.js ADDED
@@ -0,0 +1,63 @@
1
+ // The WebAssembly backend in Node. node.js imports this module only when the
2
+ // native addon is unavailable and the runtime supports JSPI, so a process
3
+ // that runs natively never loads the WebAssembly host.
4
+ import { readFile } from "node:fs/promises";
5
+ import { resolve } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+ import { bundledAssetUrl } from "./bundled-asset.js";
8
+ import { createFxEngine as createEngine } from "./engine.js";
9
+ import { loadModule, withModuleFailure } from "./wasm-module.js";
10
+ import { wasmRuntimeFactory } from "./wasm-runtime.js";
11
+
12
+ const wasmFilePromises = new Map();
13
+
14
+ // Each URL is a literal, so a bundler copies the module it names.
15
+ function packagedWasm(surface) {
16
+ return surface === "agent" ? new URL("./fx-core.wasm", import.meta.url) : new URL("./fx-term.wasm", import.meta.url);
17
+ }
18
+
19
+ // Reads a file URL or path once per absolute path; any other input passes
20
+ // through. readFile takes the input itself, which fs resolves as
21
+ // path.resolve() would: Turbopack traces this whole package for a read of a
22
+ // path computed from an unknown value.
23
+ function wasmInput(input) {
24
+ if (input instanceof URL && input.protocol === "") input = bundledAssetUrl(input);
25
+ const path = wasmFilePath(input);
26
+ if (path === null) {
27
+ if (input instanceof URL) return input.href;
28
+ return input;
29
+ }
30
+ const cached = wasmFilePromises.get(path);
31
+ if (cached) return cached;
32
+ const pendingRead = readFile(input);
33
+ let pending;
34
+ pending = withModuleFailure(pendingRead, () => {
35
+ if (wasmFilePromises.get(path) === pending) wasmFilePromises.delete(path);
36
+ });
37
+ wasmFilePromises.set(path, pending);
38
+ pendingRead.catch(() => {
39
+ if (wasmFilePromises.get(path) === pending) wasmFilePromises.delete(path);
40
+ });
41
+ return pending;
42
+ }
43
+
44
+ // The absolute path a file input names, or null for anything else.
45
+ function wasmFilePath(input) {
46
+ if (input instanceof URL) return input.protocol === "file:" ? fileURLToPath(input) : null;
47
+ if (typeof input === "string" && !URL.canParse(input)) return resolve(input);
48
+ return null;
49
+ }
50
+
51
+ // Compiles the module for `surface` without starting it.
52
+ export async function probeWasm(surface, wasm) {
53
+ await loadModule(await wasmInput(wasm ?? packagedWasm(surface)));
54
+ }
55
+
56
+ export async function createWasmEngine(options) {
57
+ return createEngine({ ...options, wasm: await wasmInput(options.wasm ?? packagedWasm("agent")) }, wasmRuntimeFactory());
58
+ }
59
+
60
+ export async function createWasmTerminal(options) {
61
+ const { createFxTerminal } = await import("./wasm-terminal.js");
62
+ return createFxTerminal({ ...options, wasm: await wasmInput(options.wasm ?? packagedWasm("terminal")) });
63
+ }