forked from jonluca/hora-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
51 lines (46 loc) · 1.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// In pure ESM web bundles, you must call init() and wait for the promised result before you can
// call any module methods. To make that as easy as possible, this module directly exposes the
// init() promise result, and returns the methods at the end of the promise.
import init, * as horaJs from "./pkg/horajs.js";
import { InitInput } from "./pkg/horajs.js";
let initialized = false;
export type HoraJsType = typeof horaJs;
export const getHora = async (
module_or_path?: InitInput | Promise<InitInput>,
maybe_memory?: WebAssembly.Memory
): Promise<HoraJsType> => {
if (initialized) {
return horaJs;
}
let input: undefined | InitInput | Promise<InitInput> =
module_or_path || undefined;
let memory: WebAssembly.Memory =
maybe_memory ||
new WebAssembly.Memory({
initial: 8192, // 512 mb by default, these are 64k pages
maximum: 65536, // 4 gb maximum memory ? I wonder if this causes issues on machines/browsers with less memory, also would be nice to enable memory64
shared: true,
});
if (typeof window === "undefined") {
if (!input) {
const fs = await import("fs/promises");
const { fileURLToPath } = await import("url");
const { join, dirname } = await import("path");
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const path = join(__dirname, "./pkg/horajs_bg.wasm");
input = fs.readFile(path);
}
const nodeProcessVersion = process?.versions?.node;
if (Number(nodeProcessVersion.split(".").unshift() < 19)) {
const { webcrypto } = await import("node:crypto");
// @ts-ignore
globalThis.crypto = webcrypto;
}
}
await init(input, memory);
await horaJs.init_env();
initialized = true;
return horaJs;
};
export default getHora;