Skip to content

Commit 8d2a76d

Browse files
committed
feat: update to SQLite 3.53.1
1 parent f999164 commit 8d2a76d

8 files changed

Lines changed: 83 additions & 91 deletions

.changeset/hot-sloths-agree.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@birchill/nice-sqlite-wasm": minor
3+
---
4+
5+
Update to SQLite 3.53.1
6+
7+
BREAKING CHANGE: Rather than overriding `locateFile` to load the WASM file, use
8+
the `emscriptenLocateFile` option instead.

README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ It's "nice" because:
55

66
- It removes the "opfs" VFS and worker parts of the JS bindings making for a
77
smaller bundle size.
8-
- It allows overriding the `locateFile` function so that you can provide a
9-
custom path for the WASM module (e.g. in order to support cache-busting
10-
filenames) or even a `Response` object (e.g. so you can abort the download).
8+
- It allows returning a `Response` object (e.g. so you can abort the download)
9+
from `emscriptenLocateFile`.
1110
- It fixes some warnings that otherwise might occur at build or run-time
1211
(e.g. the COOP/COEP header warning which is only relevant to the "opfs" VFS
1312
and a warning about dependencies based on expressions).
@@ -85,8 +84,8 @@ export default defineConfig((env) => {
8584
Then in your worker code:
8685

8786
```js
88-
import wasmUrl from '@birchill/nice-sqlite-wasm/sqlite3.wasm?url';
89-
import sqlite3InitModule from '@birchill/nice-sqlite-wasm';
87+
import wasmUrl from "@birchill/nice-sqlite-wasm/sqlite3.wasm?url";
88+
import sqlite3InitModule from "@birchill/nice-sqlite-wasm";
9089
```
9190

9291
Then when you initialize SQLite:
@@ -96,8 +95,8 @@ const sqlite = await sqlite3InitModule({
9695
// Override SQLite's locateFile implementation which wants to resolve
9796
// the SQLite WASM binary relative to the source directory instead of
9897
// the asset name assigned by rspack.
99-
locateFile: (file) => {
100-
if (file === 'sqlite3.wasm') {
98+
emscriptenLocateFile: (file) => {
99+
if (file === "sqlite3.wasm") {
101100
// Since we strip the query string in our `assetModuleFilename`
102101
// option in rspack.config.js we don't need to worry about dropping
103102
// it here.
@@ -109,7 +108,7 @@ const sqlite = await sqlite3InitModule({
109108
//
110109
// instead.
111110
return fetch(wasmUrl, {
112-
credentials: 'same-origin',
111+
credentials: "same-origin",
113112
// If you want to make the fetch abortable...
114113
signal: abortController.signal,
115114
});
@@ -120,8 +119,8 @@ const sqlite = await sqlite3InitModule({
120119
});
121120
```
122121

123-
You can also just return `wasmUrl` from `locateFile` if don't need to control
124-
the fetch yourself.
122+
You can also just return `wasmUrl` from `emscriptenLocateFile` if don't need to
123+
control the fetch yourself.
125124

126125
### vite
127126

@@ -132,10 +131,10 @@ module docs](https://github.com/sqlite/sqlite-wasm/#usage-with-vite):
132131

133132
```js
134133
// vitest.config.js
135-
import { defineConfig } from 'vite';
134+
import { defineConfig } from "vite";
136135

137136
export default defineConfig({
138-
optimizeDeps: { exclude: ['@birchill/nice-sqlite-wasm'] },
137+
optimizeDeps: { exclude: ["@birchill/nice-sqlite-wasm"] },
139138
});
140139
```
141140

@@ -194,11 +193,10 @@ Then update the table below.
194193

195194
#### Current patches
196195

197-
| Patch name | Purpose |
198-
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
199-
| `0001-locatefile-nullish-coalesce.patch` | Allow a user-provided `locateFile` function to be used (rather than clobbered). |
200-
| `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. |
201-
| `0003-locatefile-with-response.patch` | Allows a user-provided `locateFile` function to return a `Response` or a `Promise<Response>`. |
196+
| Patch name | Purpose |
197+
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
198+
| `0001-hardcode-locatefile-path.patch` | Hardcodes the path used in the default `locateFile` implementation so that bundlers don't complain about dependencies based on expressions. |
199+
| `0002-locatefile-with-response.patch` | Allows a user-provided `emscriptenLocateFile` function to return a `Response` or a `Promise<Response>`. |
202200

203201
### Building the WASM module
204202

patches/0002-hardcode-locatefile-path.patch renamed to patches/0001-hardcode-locatefile-path.patch

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
diff --git a/ext/wasm/api/pre-js.c-pp.js b/ext/wasm/api/pre-js.c-pp.js
2-
index b2c3147..da399df 100644
2+
index fbb48f9..da314b9 100644
33
--- a/ext/wasm/api/pre-js.c-pp.js
44
+++ b/ext/wasm/api/pre-js.c-pp.js
5-
@@ -48,7 +48,17 @@
6-
*/
7-
Module['locateFile'] ??= function(path, prefix) {
5+
@@ -81,7 +81,17 @@
6+
return this.emscriptenLocateFile(path, prefix);
7+
}
88
//#if target:es6-module
99
- return new URL(path, import.meta.url).href;
1010
+ // Bundlers like rspack will complain if we pass a variable to the URL()

patches/0001-locatefile-nullish-coalesce.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.

patches/0003-locatefile-with-response.patch renamed to patches/0002-locatefile-with-response.patch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
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
2+
index da314b9..b047b28 100644
33
--- a/ext/wasm/api/pre-js.c-pp.js
44
+++ b/ext/wasm/api/pre-js.c-pp.js
5-
@@ -101,7 +101,9 @@
5+
@@ -140,7 +140,9 @@
66
? "" : scriptDirectory)
77
);
88
sims.debugModule("instantiateWasm() uri =", uri, "sIMS =",this);
@@ -13,7 +13,7 @@ index da399df..9ec48b3 100644
1313
const finalThen = (arg)=>{
1414
arg.imports = imports;
1515
sims.instantiateWasm = arg /* used by sqlite3-api-prologue.c-pp.js */;
16-
@@ -110,10 +112,10 @@
16+
@@ -149,10 +151,10 @@
1717
const loadWasm = WebAssembly.instantiateStreaming
1818
? async ()=>
1919
WebAssembly

scripts/build.js

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import {
55
mkdirSync,
66
openSync,
77
rmSync,
8-
} from 'node:fs';
9-
import { execFileSync } from 'node:child_process';
10-
import path from 'node:path';
8+
} from "node:fs";
9+
import { execFileSync } from "node:child_process";
10+
import path from "node:path";
1111

12-
const WORK_DIR = path.resolve('work');
13-
const SQLITE_DIR = path.join(WORK_DIR, 'sqlite-src');
14-
const EXT_WASM = path.join(SQLITE_DIR, 'ext', 'wasm');
15-
const DIST_DIR = path.resolve('dist');
16-
const SRC_DIR = path.resolve('src');
12+
const WORK_DIR = path.resolve("work");
13+
const SQLITE_DIR = path.join(WORK_DIR, "sqlite-src");
14+
const EXT_WASM = path.join(SQLITE_DIR, "ext", "wasm");
15+
const DIST_DIR = path.resolve("dist");
16+
const SRC_DIR = path.resolve("src");
1717

1818
// 1) Clean dist
1919
if (existsSync(DIST_DIR)) {
@@ -22,74 +22,73 @@ if (existsSync(DIST_DIR)) {
2222
mkdirSync(DIST_DIR, { recursive: true });
2323

2424
// 2) Build
25-
console.log('Building SQLite...');
26-
execFileSync(path.join(SQLITE_DIR, 'configure'), ['--enable-all'], {
25+
console.log("Building SQLite...");
26+
execFileSync(path.join(SQLITE_DIR, "configure"), ["--enable-all"], {
2727
// It's important to set the current working directory to the SQLite source
2828
// directory or else it will treat it as an out-of-tree build and not look for
2929
// EMSDK.
3030
cwd: SQLITE_DIR,
31-
stdio: 'inherit',
31+
stdio: "inherit",
3232
});
33-
execFileSync('make', ['-C', SQLITE_DIR, 'clean'], { stdio: 'inherit' });
34-
execFileSync('make', ['-C', SQLITE_DIR, 'sqlite3.c'], { stdio: 'inherit' });
33+
execFileSync("make", ["-C", SQLITE_DIR, "clean"], { stdio: "inherit" });
34+
execFileSync("make", ["-C", SQLITE_DIR, "sqlite3.c"], { stdio: "inherit" });
3535

36-
console.log('Building WASM target...');
37-
execFileSync('make', ['-C', EXT_WASM, 'clean'], { stdio: 'inherit' });
36+
console.log("Building WASM target...");
37+
execFileSync("make", ["-C", EXT_WASM, "clean"], { stdio: "inherit" });
3838
execFileSync(
39-
'make',
39+
"make",
4040
[
41-
'-C',
41+
"-C",
4242
EXT_WASM,
43-
'b-esm',
44-
'emcc_opt=-Oz',
45-
'c-pp.D.esm=-Dtarget:es6-module',
46-
'sqlite3-api.jses=$(sqlite3-license-version.js) ' +
47-
'$(dir.api)/sqlite3-api-prologue.js ' +
48-
'$(dir.common)/whwasmutil.js ' +
49-
'$(dir.jacc)/jaccwabyt.js ' +
50-
'$(dir.api)/sqlite3-api-glue.c-pp.js ' +
51-
'$(sqlite3-api-build-version.js) ' +
52-
'$(dir.api)/sqlite3-api-oo1.c-pp.js ' +
43+
"b-esm",
44+
"emcc_opt=-Oz",
45+
"c-pp.D.esm=-Dtarget:es6-module",
46+
"sqlite3-api.jses=$(sqlite3-license-version.js) " +
47+
"$(dir.api)/sqlite3-api-prologue.js " +
48+
"$(dir.common)/whwasmutil.js " +
49+
"$(dir.jacc)/jaccwabyt.js " +
50+
"$(dir.api)/sqlite3-api-glue.c-pp.js " +
51+
"$(sqlite3-api-build-version.js) " +
52+
"$(dir.api)/sqlite3-api-oo1.c-pp.js " +
5353
// Omit $(dir.api)/sqlite3-api-worker1.c-pp.js
54-
'$(dir.api)/sqlite3-vfs-helper.c-pp.js ' +
55-
'$(dir.api)/sqlite3-vtab-helper.c-pp.js ' +
54+
"$(dir.api)/sqlite3-vfs-helper.c-pp.js " +
55+
"$(dir.api)/sqlite3-vtab-helper.c-pp.js " +
5656
// Omit $(dir.api)/sqlite3-vfs-opfs.c-pp.js
57-
'$(dir.api)/sqlite3-vfs-opfs-sahpool.c-pp.js ' +
58-
'$(dir.api)/sqlite3-api-cleanup.js',
57+
"$(dir.api)/sqlite3-vfs-opfs-sahpool.c-pp.js",
5958
],
60-
{ stdio: 'inherit' }
59+
{ stdio: "inherit" },
6160
);
6261

6362
// 3) Strip comments
64-
console.log('Building tool to strip comments...');
65-
execFileSync('make', ['-C', EXT_WASM, 't-stripccomments'], {
66-
stdio: 'inherit',
63+
console.log("Building tool to strip comments...");
64+
execFileSync("make", ["-C", EXT_WASM, "t-stripccomments"], {
65+
stdio: "inherit",
6766
});
68-
console.log('Stripping C comments from output...');
69-
const inFd = openSync(path.join(EXT_WASM, 'jswasm', 'sqlite3.mjs'), 'r');
70-
const apiFileOut = path.join(EXT_WASM, 'jswasm', 'sqlite3-stripped.mjs');
71-
const outFd = openSync(apiFileOut, 'w');
67+
console.log("Stripping C comments from output...");
68+
const inFd = openSync(path.join(EXT_WASM, "jswasm", "sqlite3.mjs"), "r");
69+
const apiFileOut = path.join(EXT_WASM, "jswasm", "sqlite3-stripped.mjs");
70+
const outFd = openSync(apiFileOut, "w");
7271
try {
73-
execFileSync(path.join(SQLITE_DIR, 'tool', 'stripccomments'), ['-k', '-k'], {
72+
execFileSync(path.join(SQLITE_DIR, "tool", "stripccomments"), ["-k", "-k"], {
7473
cwd: SQLITE_DIR,
75-
stdio: [inFd, outFd, 'inherit'],
74+
stdio: [inFd, outFd, "inherit"],
7675
});
7776
} finally {
7877
closeSync(inFd);
7978
closeSync(outFd);
8079
}
8180

8281
// 4) Format output
83-
console.log('Formatting output with oxfmt...');
84-
execFileSync('pnpm', ['oxfmt', apiFileOut], { stdio: 'inherit' });
82+
console.log("Formatting output with oxfmt...");
83+
execFileSync("pnpm", ["oxfmt", apiFileOut], { stdio: "inherit" });
8584

8685
// 5) Copy output to dist
87-
const wasmModule = path.join(EXT_WASM, 'jswasm', 'esm', 'sqlite3.wasm');
88-
cpSync(wasmModule, path.join(DIST_DIR, 'sqlite3.wasm'));
89-
cpSync(apiFileOut, path.join(DIST_DIR, 'sqlite3.js'));
90-
const types = path.join(SRC_DIR, 'sqlite3.d.ts');
91-
cpSync(types, path.join(DIST_DIR, 'sqlite3.d.ts'));
92-
const revFile = path.join(WORK_DIR, 'sqlite-rev.txt');
93-
cpSync(revFile, path.join(DIST_DIR, 'sqlite-rev.txt'));
86+
const wasmModule = path.join(EXT_WASM, "jswasm", "esm", "sqlite3.wasm");
87+
cpSync(wasmModule, path.join(DIST_DIR, "sqlite3.wasm"));
88+
cpSync(apiFileOut, path.join(DIST_DIR, "sqlite3.js"));
89+
const types = path.join(SRC_DIR, "sqlite3.d.ts");
90+
cpSync(types, path.join(DIST_DIR, "sqlite3.d.ts"));
91+
const revFile = path.join(WORK_DIR, "sqlite-rev.txt");
92+
cpSync(revFile, path.join(DIST_DIR, "sqlite-rev.txt"));
9493

95-
console.log('Build complete: dist/sqlite3.{js,wasm,d.ts}');
94+
console.log("Build complete: dist/sqlite3.{js,wasm,d.ts}");

sqlite-revision.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version-3.51.3
1+
version-3.53.1

src/sqlite3.d.ts

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

22432243
declare type InitOptions = {
2244-
locateFile?: (
2244+
emscriptenLocateFile?: (
22452245
path: string,
22462246
prefix: string,
22472247
) => string | Response | Promise<Response>;
@@ -7622,7 +7622,7 @@ declare type CAPI = {
76227622
sqlite3_js_db_export: (
76237623
db: Database | WasmPointer,
76247624
schema?: string | WasmPointer,
7625-
) => Uint8Array<ArrayBuffer>;
7625+
) => Uint8Array;
76267626

76277627
/**
76287628
* Given a `sqlite3*` and a database name (JS string or WASM C-string pointer,

0 commit comments

Comments
 (0)