Skip to content

Commit 80da105

Browse files
Try to fix #2296 and support to get wasm file through http protocol (#2297)
* Update mod.rs * Update mod.rs * Support http protocol to obtain wasm file remotely * Another stupid mistake... * Fix errors caused by `{}` * `bg` is missing * RealPathSync cannot be used to get the parent directory of file under linux, so substring and lastIndexOf will be used to get the parent directory under linux * Missing readFileSync * Use asynchronous functions instead of synchronous functions. * Change `includes` to switch-case. And change the synchronous file operation to asynchronous. * Change to @RReverser suggestion. * throw a proper Error instance Co-authored-by: Ingvar Stepanyan <[email protected]> * Reuse url instance Co-authored-by: Ingvar Stepanyan <[email protected]> * Use URL instead of previous backward writing. * reuse unnecessary code. Co-authored-by: Ingvar Stepanyan <[email protected]>
1 parent 41a6a43 commit 80da105

File tree

1 file changed

+21
-5
lines changed
  • crates/cli-support/src/js

1 file changed

+21
-5
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,28 @@ impl<'a> Context<'a> {
322322
// Deno removed support for .wasm imports in https://github.com/denoland/deno/pull/5135
323323
// the issue for bringing it back is https://github.com/denoland/deno/issues/5609.
324324
format!(
325-
"const file = new URL(import.meta.url).pathname;
326-
const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\\\' : '/') + 1) + '{}_bg.wasm';
327-
const wasmModule = new WebAssembly.Module(Deno.readFileSync(wasmFile));
328-
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
325+
"const wasm_url = new URL('{module_name}_bg.wasm', import.meta.url);
326+
let wasmCode = '';
327+
switch (wasm_url.protocol) {{
328+
case 'file:':
329+
let wasm_pathname = wasm_url.pathname;
330+
if (Deno.build.os === 'windows' && wasm_pathname.startsWith('/')) {{
331+
wasm_pathname = wasm_pathname.substr(1);
332+
}}
333+
wasmCode = await Deno.readFile(wasm_pathname);
334+
break
335+
case 'https:':
336+
case 'http:':
337+
wasmCode = await (await fetch(wasm_url)).arrayBuffer();
338+
break
339+
default:
340+
throw new Error(`Unsupported protocol: ${{wasm_url.protocol}}`);
341+
break
342+
}}
343+
344+
const wasmInstance = (await WebAssembly.instantiate(wasmCode, imports)).instance;
329345
const wasm = wasmInstance.exports;",
330-
module_name
346+
module_name = module_name
331347
)
332348
}
333349

0 commit comments

Comments
 (0)