Skip to content

Commit

Permalink
fix: don't require --allow-net (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
NfNitLoop authored Aug 5, 2023
1 parent 0a64a05 commit b9397a5
Show file tree
Hide file tree
Showing 5 changed files with 4,289 additions and 182 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.generated.js linguist-generated=true
*.wasm linguist-generated=true
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"tasks": {
"wasmbuild": "deno run -A https://deno.land/x/[email protected]/main.ts --out ./src/lib"
"test": "deno test -A",
"wasmbuild": "deno run -A https://deno.land/x/[email protected]/main.ts --sync --out ./src/lib"
},
"fmt": {
"proseWrap": "preserve",
Expand Down
1 change: 0 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ export * as path from "https://deno.land/[email protected]/path/mod.ts";
export { readAll } from "https://deno.land/[email protected]/streams/read_all.ts";
export { readerFromStreamReader } from "https://deno.land/[email protected]/streams/reader_from_stream_reader.ts";
export { writeAll, writeAllSync } from "https://deno.land/[email protected]/streams/write_all.ts";
export { default as localDataDir } from "https://deno.land/x/[email protected]/data_local_dir/mod.ts";
export { outdent } from "https://deno.land/x/[email protected]/src/index.ts";
export { RealEnvironment as DenoWhichRealEnvironment, which, whichSync } from "https://deno.land/x/[email protected]/mod.ts";
62 changes: 1 addition & 61 deletions src/lib/mod.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,4 @@
import { fs, localDataDir, path } from "../deps.ts";
import { instantiate } from "./rs_lib.generated.js";

export type WasmInstance = Awaited<ReturnType<typeof instantiate>>;

async function getWasmFileUrl() {
const url = new URL("rs_lib_bg.wasm", import.meta.url);
if (url.protocol !== "file:") {
return (await cacheLocalDir(url)) ?? url;
}
return url;
}

async function cacheLocalDir(url: URL) {
const localPath = await getUrlLocalPath(url);
if (localPath == null) {
return undefined;
}
if (!await fs.exists(localPath)) {
const fileBytes = await getUrlBytes(url);
await Deno.writeFile(localPath, new Uint8Array(fileBytes));
}
return path.toFileUrl(localPath);
}

async function getUrlLocalPath(url: URL) {
try {
const dataDirPath = await getInitializedLocalDataDirPath();
const version = getUrlVersion(url);
return path.join(dataDirPath, version + ".wasm");
} catch {
return undefined;
}
}

async function getInitializedLocalDataDirPath() {
const dataDir = localDataDir();
if (dataDir == null) {
throw new Error(`Could not find local data directory.`);
}
const dirPath = path.join(dataDir, "dax");
await fs.ensureDir(dirPath);
return dirPath;
}

function getUrlVersion(url: URL) {
const version = url.pathname.match(/([0-9]+)\.([0-9]+)\.([0-9]+)/)?.[0];
if (version == null) {
throw new Error(`Could not find version in url: ${url}`);
}
return version;
}

async function getUrlBytes(url: URL) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Error downloading ${url}: ${response.statusText}`);
}
return await response.arrayBuffer();
}

export const wasmInstance = await instantiate({
url: await getWasmFileUrl(),
});
export const wasmInstance = await instantiate();
Loading

0 comments on commit b9397a5

Please sign in to comment.