-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't require
--allow-net
(#158)
- Loading branch information
Showing
5 changed files
with
4,289 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.generated.js linguist-generated=true | ||
*.wasm linguist-generated=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.