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

fetch wasm file should append baseUrl base on currenScript running env #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion src/wasm-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ 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 ''
let baseUrl = ''
try {
baseUrl = new URL(currentScriptUrl).origin
} catch(err){}
return baseUrl
}
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