strictjs-runtime 2.0.5 → 2.0.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/index.js +29 -15
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
1
|
import initWASM, {
|
|
5
2
|
StrictNumber,
|
|
6
3
|
StrictString,
|
|
@@ -13,24 +10,41 @@ import initWASM, {
|
|
|
13
10
|
Schema
|
|
14
11
|
} from "./pkg/strictjs_runtime.js";
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
18
|
-
const __dirname = path.dirname(__filename);
|
|
13
|
+
let nodeInitWASM = null;
|
|
19
14
|
|
|
20
15
|
/**
|
|
21
|
-
*
|
|
22
|
-
* - Works in both Node.js and browser
|
|
23
|
-
* - Automatically detects environment and loads WASM correctly
|
|
16
|
+
* This function dynamically loads WASM in Node.js
|
|
24
17
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
async function setupNodeLoader() {
|
|
19
|
+
const fs = await import("fs");
|
|
20
|
+
const path = await import("path");
|
|
21
|
+
const { fileURLToPath } = await import("url");
|
|
22
|
+
|
|
23
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
24
|
+
const __dirname = path.dirname(__filename);
|
|
25
|
+
|
|
26
|
+
return async () => {
|
|
28
27
|
const wasmPath = path.resolve(__dirname, "pkg/strictjs_runtime_bg.wasm");
|
|
29
28
|
const wasmBuffer = fs.readFileSync(wasmPath);
|
|
30
29
|
await initWASM(wasmBuffer);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Lazy initialize Node loader if in Node.js environment
|
|
34
|
+
if (typeof window === "undefined") {
|
|
35
|
+
nodeInitWASM = await setupNodeLoader();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Initializes StrictJS Runtime
|
|
40
|
+
* - Node: Loads WASM from filesystem
|
|
41
|
+
* - Browser: WASM auto-fetch
|
|
42
|
+
*/
|
|
43
|
+
export default async function strictInit() {
|
|
44
|
+
if (typeof window === "undefined") {
|
|
45
|
+
await nodeInitWASM();
|
|
31
46
|
} else {
|
|
32
|
-
// Browser
|
|
33
|
-
await initWASM();
|
|
47
|
+
await initWASM(); // Browser automatically fetches the `.wasm` file
|
|
34
48
|
}
|
|
35
49
|
|
|
36
50
|
return {
|
|
@@ -46,7 +60,7 @@ export default async function strictInit() {
|
|
|
46
60
|
};
|
|
47
61
|
}
|
|
48
62
|
|
|
49
|
-
// Named exports
|
|
63
|
+
// Named exports
|
|
50
64
|
export {
|
|
51
65
|
get_memory,
|
|
52
66
|
HeapType,
|