Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/proud-beers-win.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@birchill/nice-sqlite-wasm": minor
---

Allow returning a `Response` or `Promise<Response>` from `locateFile`
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This is a custom build of SQLite that only supports the "opfs-sahpool" VFS.
It's "nice" because:

- It remove the "opfs" VFS and worker parts of the JS bindings making for a
- It removes the "opfs" VFS and worker parts of the JS bindings making for a
smaller bundle size.
- It allows passing in a custom path for the WASM module in order to support
cache-busting filenames / bundlers.
Expand Down Expand Up @@ -85,6 +85,7 @@ Then update the table below.
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `0001-locatefile-nullish-coalesce.patch` | Allow a user-provided `locateFile` function to be used (rather than clobbered). |
| `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. |
| `0003-locatefile-with-response.patch` | Allows a user-provided `locateFile` function to return a `Response` or a `Promise<Response>`. |

### Building the WASM module

Expand Down
28 changes: 28 additions & 0 deletions patches/0003-locatefile-with-response.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
diff --git a/ext/wasm/api/pre-js.c-pp.js b/ext/wasm/api/pre-js.c-pp.js
index da399df..9ec48b3 100644
--- a/ext/wasm/api/pre-js.c-pp.js
+++ b/ext/wasm/api/pre-js.c-pp.js
@@ -101,7 +101,9 @@
? "" : scriptDirectory)
);
sims.debugModule("instantiateWasm() uri =", uri, "sIMS =",this);
- const wfetch = ()=>fetch(uri, {credentials: 'same-origin'});
+ const response = uri instanceof Response || uri instanceof Promise
+ ? uri
+ : fetch(uri, { credentials: 'same-origin' });
const finalThen = (arg)=>{
arg.imports = imports;
sims.instantiateWasm = arg /* used by sqlite3-api-prologue.c-pp.js */;
@@ -110,10 +112,10 @@
const loadWasm = WebAssembly.instantiateStreaming
? async ()=>
WebAssembly
- .instantiateStreaming(wfetch(), imports)
+ .instantiateStreaming(response, imports)
.then(finalThen)
: async ()=>// Safari < v15
- wfetch()
+ Promise.resolve(response)
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, imports))
.then(finalThen)
5 changes: 4 additions & 1 deletion src/sqlite3.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,10 @@ declare type Sqlite3Static = {
};

declare type InitOptions = {
locateFile?: (path: string, prefix: string) => string;
locateFile?: (
path: string,
prefix: string,
) => string | Response | Promise<Response>;
print?: (msg: string) => void;
printErr?: (msg: string) => void;
};
Expand Down