Skip to content

Commit 58a4557

Browse files
committed
feat: allow returning a Response or Promise<Response> from locateFile
1 parent e693f32 commit 58a4557

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

.changeset/proud-beers-win.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@birchill/nice-sqlite-wasm": minor
3+
---
4+
5+
Allow returning a `Response` or `Promise<Response>` from `locateFile`

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Then update the table below.
8585
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
8686
| `0001-locatefile-nullish-coalesce.patch` | Allow a user-provided `locateFile` function to be used (rather than clobbered). |
8787
| `0002-hardcode-locatefile-path.patch` | Hardcodes the path used in the default `locateFile` implementation so that bundlers don't complain about dependencies based on expressions. |
88+
| `0003-locatefile-with-response.patch` | Allows a user-provided `locateFile` function to return a `Response` or a `Promise<Response>`. |
8889

8990
### Building the WASM module
9091

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
diff --git a/ext/wasm/api/pre-js.c-pp.js b/ext/wasm/api/pre-js.c-pp.js
2+
index da399df..9ec48b3 100644
3+
--- a/ext/wasm/api/pre-js.c-pp.js
4+
+++ b/ext/wasm/api/pre-js.c-pp.js
5+
@@ -101,7 +101,9 @@
6+
? "" : scriptDirectory)
7+
);
8+
sims.debugModule("instantiateWasm() uri =", uri, "sIMS =",this);
9+
- const wfetch = ()=>fetch(uri, {credentials: 'same-origin'});
10+
+ const response = uri instanceof Response || uri instanceof Promise
11+
+ ? uri
12+
+ : fetch(uri, { credentials: 'same-origin' });
13+
const finalThen = (arg)=>{
14+
arg.imports = imports;
15+
sims.instantiateWasm = arg /* used by sqlite3-api-prologue.c-pp.js */;
16+
@@ -110,10 +112,10 @@
17+
const loadWasm = WebAssembly.instantiateStreaming
18+
? async ()=>
19+
WebAssembly
20+
- .instantiateStreaming(wfetch(), imports)
21+
+ .instantiateStreaming(response, imports)
22+
.then(finalThen)
23+
: async ()=>// Safari < v15
24+
- wfetch()
25+
+ Promise.resolve(response)
26+
.then(response => response.arrayBuffer())
27+
.then(bytes => WebAssembly.instantiate(bytes, imports))
28+
.then(finalThen)

src/sqlite3.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,10 @@ declare type Sqlite3Static = {
22412241
};
22422242

22432243
declare type InitOptions = {
2244-
locateFile?: (path: string, prefix: string) => string;
2244+
locateFile?: (
2245+
path: string,
2246+
prefix: string,
2247+
) => string | Response | Promise<Response>;
22452248
print?: (msg: string) => void;
22462249
printErr?: (msg: string) => void;
22472250
};

0 commit comments

Comments
 (0)