d2/d2js/js/src/platform.js
Alexander Wang 8d71e8fff8
init d2.js
2025-01-12 10:50:05 -07:00

37 lines
1.2 KiB
JavaScript

export async function loadFile(path) {
if (typeof window === "undefined") {
const fs = await import("node:fs/promises");
const { fileURLToPath } = await import("node:url");
const { join, dirname } = await import("node:path");
const __dirname = dirname(fileURLToPath(import.meta.url));
try {
return await fs.readFile(join(__dirname, path));
} catch (err) {
if (err.code === "ENOENT") {
return await fs.readFile(join(__dirname, "../wasm", path.replace("./", "")));
}
throw err;
}
}
try {
const response = await fetch(new URL(path, import.meta.url));
return await response.arrayBuffer();
} catch {
const response = await fetch(
new URL(`../wasm/${path.replace("./", "")}`, import.meta.url)
);
return await response.arrayBuffer();
}
}
export async function createWorker() {
if (typeof window === "undefined") {
const { Worker } = await import("node:worker_threads");
const { fileURLToPath } = await import("node:url");
const { join, dirname } = await import("node:path");
const __dirname = dirname(fileURLToPath(import.meta.url));
return new Worker(join(__dirname, "worker.js"));
}
return new window.Worker(new URL("./worker.js", import.meta.url));
}