Skip to content

Commit

Permalink
fetch wasm file should append baseUrl base on currenScript running env
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyrenhuang committed Dec 28, 2023
1 parent f9718b9 commit a731f81
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/wasm-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ const wasmHelper = async (opts = {}, url: string) => {
// a lot of static file servers, so we just work around it by getting the
// raw buffer.
// @ts-ignore
const response = await fetch(url);
const getBaseUrl = () => {
const currentScriptUrl = getRunningScript()()
if (!currentScriptUrl) return ''
return new URL(currentScriptUrl).origin
}
const getRunningScript = ()=>{
return ()=>{
return new Error().stack.match(/([^ \n])*([a-z]*:\/\/\/?)*?[a-z0-9\/\\]*\.js/ig)?.[0]
}
}
const baseUrl = typeof window !== 'undefined' ? getBaseUrl() : ''
const response = await fetch(baseUrl + url);
const contentType = response.headers.get("Content-Type") || "";
if ("instantiateStreaming" in WebAssembly && contentType.startsWith("application/wasm")) {
result = await WebAssembly.instantiateStreaming(response, opts);
Expand Down

0 comments on commit a731f81

Please sign in to comment.