Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't require --allow-net #158

Merged
merged 7 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/wasmbuild@0.10.2/main.ts --out ./src/lib"
"test": "deno test -A",
"wasmbuild": "deno run -A https://deno.land/x/wasmbuild@0.10.2/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/std@0.182.0/path/mod.ts";
export { readAll } from "https://deno.land/std@0.182.0/streams/read_all.ts";
export { readerFromStreamReader } from "https://deno.land/std@0.182.0/streams/reader_from_stream_reader.ts";
export { writeAll, writeAllSync } from "https://deno.land/std@0.182.0/streams/write_all.ts";
export { default as localDataDir } from "https://deno.land/x/dir@1.5.1/data_local_dir/mod.ts";
export { outdent } from "https://deno.land/x/outdent@v0.8.0/src/index.ts";
export { RealEnvironment as DenoWhichRealEnvironment, which, whichSync } from "https://deno.land/x/which@0.3.0/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