@evolu/sqlite-wasm 2.2.4 → 3.53.4-build1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -218
  3. package/dist/src/CApi.d.ts +2108 -0
  4. package/dist/src/CApi.d.ts.map +1 -0
  5. package/dist/src/CApi.js +1919 -0
  6. package/dist/src/Constants.d.ts +868 -0
  7. package/dist/src/Constants.d.ts.map +1 -0
  8. package/dist/src/Constants.js +602 -0
  9. package/dist/src/Database.d.ts +641 -0
  10. package/dist/src/Database.d.ts.map +1 -0
  11. package/dist/src/Database.js +1177 -0
  12. package/dist/src/Memory.d.ts +119 -0
  13. package/dist/src/Memory.d.ts.map +1 -0
  14. package/dist/src/Memory.js +207 -0
  15. package/dist/src/Pointer.d.ts +100 -0
  16. package/dist/src/Pointer.d.ts.map +1 -0
  17. package/dist/src/Pointer.js +15 -0
  18. package/dist/src/SahPool.d.ts +744 -0
  19. package/dist/src/SahPool.d.ts.map +1 -0
  20. package/dist/src/SahPool.js +1985 -0
  21. package/dist/src/Wasm.d.ts +315 -0
  22. package/dist/src/Wasm.d.ts.map +1 -0
  23. package/dist/src/Wasm.js +756 -0
  24. package/dist/src/WasmUrl.d.ts +21 -0
  25. package/dist/src/WasmUrl.d.ts.map +1 -0
  26. package/dist/src/WasmUrl.js +20 -0
  27. package/dist/src/c-api/index.d.ts +9 -0
  28. package/dist/src/c-api/index.d.ts.map +1 -0
  29. package/dist/src/c-api/index.js +8 -0
  30. package/dist/src/index.d.ts +15 -0
  31. package/dist/src/index.d.ts.map +1 -0
  32. package/dist/src/index.js +13 -0
  33. package/dist/wasm/sqlite3.wasm +0 -0
  34. package/package.json +60 -60
  35. package/src/CApi.test.ts +217 -0
  36. package/src/CApi.ts +3294 -0
  37. package/src/Constants.ts +803 -0
  38. package/src/Database.ts +1714 -0
  39. package/src/Memory.test.ts +319 -0
  40. package/src/Memory.ts +237 -0
  41. package/src/Pointer.ts +121 -0
  42. package/src/SahPool.test.ts +180 -0
  43. package/src/SahPool.ts +2627 -0
  44. package/src/Wasm.test.ts +444 -0
  45. package/src/Wasm.ts +1026 -0
  46. package/src/WasmUrl.ts +26 -0
  47. package/src/c-api/index.ts +9 -0
  48. package/src/index.ts +15 -0
  49. package/bin/index.js +0 -110
  50. package/index.d.ts +0 -8118
  51. package/index.mjs +0 -7
  52. package/node.mjs +0 -3
  53. package/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs +0 -13659
  54. package/sqlite-wasm/jswasm/sqlite3-node.mjs +0 -11671
  55. package/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js +0 -691
  56. package/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly.mjs +0 -35
  57. package/sqlite-wasm/jswasm/sqlite3-worker1-promiser.js +0 -193
  58. package/sqlite-wasm/jswasm/sqlite3-worker1-promiser.mjs +0 -187
  59. package/sqlite-wasm/jswasm/sqlite3-worker1.js +0 -46
  60. package/sqlite-wasm/jswasm/sqlite3.js +0 -13697
  61. package/sqlite-wasm/jswasm/sqlite3.mjs +0 -13661
  62. package/sqlite-wasm/jswasm/sqlite3.wasm +0 -0
@@ -0,0 +1,756 @@
1
+ /**
2
+ * SQLite's Emscripten wasm, loaded without Emscripten's or SQLite's JavaScript.
3
+ *
4
+ * The package ships SQLite's own wasm, built with SQLite's own makefile, and
5
+ * replaces the generated JavaScript with a small loader and the standalone
6
+ * functions of `@evolu/sqlite-wasm/c-api`.
7
+ *
8
+ * ## Loading
9
+ *
10
+ * {@link createSqliteWasm} runs these steps and fails with a
11
+ * {@link SqliteWasmError} when one fails. Steps 1 to 3 fail before any wasm code
12
+ * runs, and steps 4 and 5 run only the stack setup, static constructors and
13
+ * `sqlite3__wasm_enum_json()`, before any SQLite API call:
14
+ *
15
+ * 1. Compile, while downloading a `Response` of type `application/wasm`.
16
+ * 2. Import guard: every import the binary declares must be one the loader
17
+ * implements, or instantiation never happens. A new Emscripten or SQLite
18
+ * release that adds an import is caught here, not at the first call that
19
+ * reaches it. The list matches the release recipe exactly.
20
+ * 3. A fresh `WebAssembly.Memory` of at least the size the binary declares, which
21
+ * the JavaScript API does not expose (256 pages covers every build so far),
22
+ * and a maximum of 32768 pages, so pointers stay positive i32 values. An
23
+ * engine that cannot allocate it, as on a device low on memory, fails
24
+ * loading with {@link SqliteWasmCompileError}.
25
+ * 4. Instantiate, then set up the C stack for Emscripten's stack check: call
26
+ * `emscripten_stack_init`, then `__set_stack_limits` with what
27
+ * `emscripten_stack_get_base` and `emscripten_stack_get_end` return. The
28
+ * check compares each new stack pointer with limits that start at 0, so they
29
+ * are set before any code moves it. Then call `__wasm_call_ctors` exactly
30
+ * once. A binary without one of these functions fails the build guard.
31
+ * 5. Build guard: the hash {@link sqliteWasmBuildHash} defines, computed from the
32
+ * string `sqlite3__wasm_enum_json()` returns and the names
33
+ * `WebAssembly.Module.exports` lists, must equal it. A match means the
34
+ * binary has the pinned build's export names, so it exports every function
35
+ * in {@link SqliteCExports}, and the generated constants, struct layouts and
36
+ * {@link SQLITE_WASM_DEALLOC} describe it. The hash does not cover wasm
37
+ * types: the generator checked the bindings against the pinned types, and
38
+ * `scripts/generate.mts --verify-build` checks a new binary's. A binary
39
+ * without `sqlite3__wasm_enum_json`, or whose call returns NULL, hashes the
40
+ * string as empty and fails. No JSON is parsed at runtime.
41
+ * 6. `sqlite3_initialize()`, whose result code is checked.
42
+ * 7. Register the default VFS, then run {@link initializeSqliteWasm}, which makes
43
+ * it the default and calls `sqlite3_randomness(0, 0)`. SQLite's random
44
+ * number generator seeds itself from the default VFS at its first use, and
45
+ * again at the first use after that call resets it. Without Emscripten's
46
+ * file system, SQLite's unix VFS seeds it from the time and a constant pid,
47
+ * so two instances seeded in the same second would share `randomblob()`
48
+ * values and journal nonces. Nothing uses the generator while loading, so
49
+ * the reset is defensive here; it matters after `sqlite3_shutdown()`. It
50
+ * also unregisters SQLite's kvvfs, which `sqlite3_initialize()` registers
51
+ * but whose storage only SQLite's JavaScript provides, so opening it, as the
52
+ * names `:localStorage:` and `:sessionStorage:` do on any VFS, fails with
53
+ * SQLITE_ERROR instead of trapping. Allocating, finding or registering the
54
+ * VFS fails with {@link SqliteWasmInitializeError}, and compiling the
55
+ * generated module that installs its methods, which a Content Security
56
+ * Policy without 'wasm-unsafe-eval' forbids, fails with
57
+ * {@link SqliteWasmCompileError}. `sqlite3_initialize()` after
58
+ * `sqlite3_shutdown()` makes the unix VFS the default again, so
59
+ * {@link initializeSqliteWasm} must run instead.
60
+ *
61
+ * ## The default VFS
62
+ *
63
+ * `evolu-memory`, of version 2, has no files, so only `:memory:` databases use
64
+ * it:
65
+ *
66
+ * - `xFullPathname` fails with SQLITE_CANTOPEN, so opening a file path fails
67
+ * before SQLite would open a file. `xOpen` is NULL: SQLite opens a file
68
+ * without a path only for a temporary file, which this build keeps in memory.
69
+ * `xAccess` finds no file, and `xDelete` returns SQLITE_IOERR_DELETE_NOENT.
70
+ * - `xRandomness` fills memory from {@link RandomBytes}, in chunks of at most
71
+ * 65536 bytes, as `crypto.getRandomValues` requires.
72
+ * - `xCurrentTime` and `xCurrentTimeInt64` read `Time.now` of {@link Time}.
73
+ * - `xSleep` returns at once, having slept 0 microseconds. Contention can only
74
+ * come from connections in the same thread, which sleeping cannot resolve.
75
+ * - `xGetLastError` reports no system error.
76
+ *
77
+ * ## Imports
78
+ *
79
+ * About ten imports do real work:
80
+ *
81
+ * - `memory`, see step 3.
82
+ * - `__handle_stack_overflow`, which the build's stack check
83
+ * (`-sSTACK_OVERFLOW_CHECK=2`) calls instead of moving the stack pointer
84
+ * outside the C stack, throws a `WebAssembly.RuntimeError`, as a trap does.
85
+ * The C stack has 512 KiB directly above SQLite's static data, and SQL that
86
+ * is legal but nests deeply enough, such as a chain of 600 triggers, would
87
+ * otherwise overwrite that data silently.
88
+ * - `emscripten_resize_heap` grows memory to the requested size, read as
89
+ * unsigned, and returns 1, or 0 when it cannot, never throwing.
90
+ * - The clocks read {@link Time}: `emscripten_date_now` returns `Time.now`,
91
+ * `emscripten_get_now` returns `Time.performance.now`, and WASI
92
+ * `clock_time_get` writes BigInt nanoseconds of the first for the realtime
93
+ * clock and of the second for the monotonic and CPU-time clocks, and returns
94
+ * EINVAL for any other.
95
+ * - `_localtime_js`, for SQL `localtime`, reads `Date` in the local time zone and
96
+ * fails for a time `Date` cannot represent. It fills only the `struct tm`
97
+ * fields SQLite reads, from `tm_sec` to `tm_year`, and zeroes the others.
98
+ * `_tzset_js` writes nothing: only `localtime_r` reads what it would write,
99
+ * to set `tm_zone`, which nothing reads.
100
+ * - `environ_sizes_get` and `environ_get` describe an empty environment.
101
+ * - `fd_write`, for C-side diagnostics, writes complete lines of stdout with
102
+ * {@link Console.log} and of stderr with {@link Console.error}, and fails any
103
+ * other descriptor with EBADF.
104
+ *
105
+ * The `__syscall_*` imports return -ENOSYS (-52), and the remaining WASI `fd_*`
106
+ * imports return ENOSYS (52), as WASI errors are positive: only `openat` of
107
+ * `/dev/urandom` is ever reached, and step 7 covers it.
108
+ *
109
+ * Every import's wasm type and the memory limits are pinned in
110
+ * `scripts/upstream/sqlite3-wasm-imports.json`, and `scripts/generate.mts
111
+ * --verify-build` checks a binary against them.
112
+ *
113
+ * ## Rules for everything that uses the instance
114
+ *
115
+ * - Synchronous only. The build has neither Asyncify nor JSPI, and nothing in the
116
+ * SQLite call path returns a promise.
117
+ * - Any call that can allocate, including a VFS callback running inside
118
+ * `sqlite3_step`, can grow memory, which detaches the previous `ArrayBuffer`.
119
+ * Take heap views with {@link SqliteWasm.getHeapU8} after such calls and never
120
+ * keep one across them.
121
+ * - No JavaScript exception may cross into wasm. Every function installed with
122
+ * {@link installWasmFunctions} catches what it throws, reports it as a defect
123
+ * and returns SQLITE_ERROR.
124
+ * - A trap or a C stack overflow throws a `WebAssembly.RuntimeError`, which
125
+ * installed functions let through. When the static constructors trap, loading
126
+ * fails with {@link SqliteWasmCompileError}.
127
+ * - Any exception or trap that escapes a wasm call is a defect. It unwound C
128
+ * frames without cleanup, so the C stack and SQLite's state are left
129
+ * inconsistent: it is rethrown, the instance refuses every later call, and
130
+ * its worker should end. {@link SqliteWasm.call} enforces this. The CApi
131
+ * functions return the exports themselves, so code that calls them directly
132
+ * must call through it. An installed function that returns once the instance
133
+ * is broken, as one that caught what a nested call rethrew, or whose
134
+ * {@link ReportDefect} broke it, throws that refusal instead, so SQLite never
135
+ * resumes on that state.
136
+ *
137
+ * @module
138
+ */
139
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
140
+ if (value !== null && value !== void 0) {
141
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
142
+ var dispose, inner;
143
+ if (async) {
144
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
145
+ dispose = value[Symbol.asyncDispose];
146
+ }
147
+ if (dispose === void 0) {
148
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
149
+ dispose = value[Symbol.dispose];
150
+ if (async) inner = dispose;
151
+ }
152
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
153
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
154
+ env.stack.push({ value: value, dispose: dispose, async: async });
155
+ }
156
+ else if (async) {
157
+ env.stack.push({ async: true });
158
+ }
159
+ return value;
160
+ };
161
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
162
+ return function (env) {
163
+ function fail(e) {
164
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
165
+ env.hasError = true;
166
+ }
167
+ var r, s = 0;
168
+ function next() {
169
+ while (r = env.stack.pop()) {
170
+ try {
171
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
172
+ if (r.dispose) {
173
+ var result = r.dispose.call(r.value);
174
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
175
+ }
176
+ else s |= 1;
177
+ }
178
+ catch (e) {
179
+ fail(e);
180
+ }
181
+ }
182
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
183
+ if (env.hasError) throw env.error;
184
+ }
185
+ return next();
186
+ };
187
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
188
+ var e = new Error(message);
189
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
190
+ });
191
+ import { constVoid, disposable, err, isNonEmptyArray, mapArray, mapObject, ok, reportDefectAfterMicrotask, tryAsync, trySync, zipArray, } from "@evolu/common";
192
+ import { SQLITE_CANTOPEN, SQLITE_ERROR, SQLITE_IOERR_DELETE_NOENT, SQLITE_MISUSE, SQLITE_NOMEM, SQLITE_OK, sqlite3_file_layout, sqlite3_vfs_layout, sqliteWasmBuildHash, } from "./Constants.js";
193
+ import { allocCString, allocWasm } from "./Memory.js";
194
+ /**
195
+ * Loads the wasm, following the steps in the module documentation.
196
+ *
197
+ * Start it as early as possible, in the worker's composition root, and pass the
198
+ * instance to everything else through {@link SqliteWasmDep}.
199
+ */
200
+ export const createSqliteWasm = (source) => async (run) => {
201
+ const toCompileError = (cause) => ({
202
+ type: "SqliteWasmCompileError",
203
+ cause,
204
+ });
205
+ // 1. Compile.
206
+ const compiled = await tryAsync(async () => {
207
+ if (source instanceof WebAssembly.Module)
208
+ return source;
209
+ if (source instanceof ArrayBuffer || ArrayBuffer.isView(source))
210
+ return WebAssembly.compile(source);
211
+ const response = await source;
212
+ // A wrong URL's error page would otherwise fail to compile as HTML.
213
+ if (!response.ok)
214
+ throw new TypeError(`Fetching the SQLite wasm failed with HTTP status ${response.status}.`);
215
+ // The only type every engine's compileStreaming accepts: browsers reject
216
+ // any parameter, as the spec says, and Node.js also rejects another
217
+ // case, although the spec matches the type case-insensitively.
218
+ return response.headers.get("Content-Type") === "application/wasm"
219
+ ? WebAssembly.compileStreaming(response)
220
+ : WebAssembly.compile(await response.arrayBuffer());
221
+ }, toCompileError);
222
+ if (!compiled.ok)
223
+ return compiled;
224
+ const module = compiled.value;
225
+ // 2. Import guard.
226
+ const unknownImports = WebAssembly.Module.imports(module)
227
+ .filter((entry) => !Object.hasOwn(sqliteWasmImports, entry.module) ||
228
+ !Object.hasOwn(sqliteWasmImports[entry.module], entry.name))
229
+ .map((entry) => `${entry.module}.${entry.name}`);
230
+ if (isNonEmptyArray(unknownImports))
231
+ return err({ type: "SqliteWasmUnknownImport", imports: unknownImports });
232
+ // 3. Memory.
233
+ const createdMemory = trySync(() => new WebAssembly.Memory({
234
+ initial: initialMemoryPages,
235
+ maximum: maximumMemoryPages,
236
+ }), toCompileError);
237
+ if (!createdMemory.ok)
238
+ return createdMemory;
239
+ const memory = createdMemory.value;
240
+ let heapU8 = new Uint8Array(memory.buffer);
241
+ let heapDataView = new DataView(memory.buffer);
242
+ const getHeapU8 = () => {
243
+ if (heapU8.buffer !== memory.buffer)
244
+ heapU8 = new Uint8Array(memory.buffer);
245
+ return heapU8;
246
+ };
247
+ const getHeapDataView = () => {
248
+ if (heapDataView.buffer !== memory.buffer)
249
+ heapDataView = new DataView(memory.buffer);
250
+ return heapDataView;
251
+ };
252
+ // 4. Instantiate, set the C stack's limits and run the static constructors.
253
+ const context = {
254
+ ...run.deps,
255
+ memory,
256
+ getHeapU8,
257
+ getHeapDataView,
258
+ };
259
+ const imports = mapObject(sqliteWasmImports, (namespace) => mapObject(namespace, (createImport) => createImport(context)));
260
+ const instantiated = await tryAsync(async () => {
261
+ const instance = await WebAssembly.instantiate(module, imports);
262
+ // As Emscripten's stackCheckInit and setStackLimits do.
263
+ callExport(instance, "emscripten_stack_init");
264
+ callExport(instance, "__set_stack_limits", callExport(instance, "emscripten_stack_get_base"), callExport(instance, "emscripten_stack_get_end"));
265
+ callExport(instance, "__wasm_call_ctors");
266
+ return instance;
267
+ }, toCompileError);
268
+ if (!instantiated.ok)
269
+ return instantiated;
270
+ const instance = instantiated.value;
271
+ // 5. Build guard.
272
+ let hash = 0x811c9dc5;
273
+ const hashBytes = (bytes) => {
274
+ for (const byte of bytes)
275
+ hash = Math.imul(hash ^ byte, 0x01000193);
276
+ };
277
+ const enumJsonPtr = callExport(instance, "sqlite3__wasm_enum_json");
278
+ const heap = getHeapU8();
279
+ hashBytes(enumJsonPtr === 0
280
+ ? Uint8Array.of(0)
281
+ : heap.subarray(enumJsonPtr, heap.indexOf(0, enumJsonPtr) + 1));
282
+ const encoder = new TextEncoder();
283
+ for (const name of WebAssembly.Module.exports(module)
284
+ .map((entry) => entry.name)
285
+ .toSorted())
286
+ hashBytes(encoder.encode(`${name}\0`));
287
+ const actualHash = hash >>> 0;
288
+ if (actualHash !== sqliteWasmBuildHash)
289
+ return err({
290
+ type: "SqliteWasmBuildMismatch",
291
+ expectedHash: sqliteWasmBuildHash,
292
+ actualHash,
293
+ });
294
+ const exports = instance.exports;
295
+ // 6. Initialize.
296
+ const code = exports.sqlite3_initialize();
297
+ if (code !== SQLITE_OK)
298
+ return err({ type: "SqliteWasmInitializeError", code });
299
+ // What escaped a call and broke the instance.
300
+ let brokenBy = null;
301
+ const sqliteWasm = {
302
+ exports,
303
+ functionTable: instance.exports
304
+ .__indirect_function_table,
305
+ getHeapU8,
306
+ getHeapDataView,
307
+ call: (fn) => {
308
+ if (brokenBy)
309
+ throw new Error("The SQLite wasm instance is broken: an exception escaped a wasm call.", { cause: brokenBy.error });
310
+ try {
311
+ return fn();
312
+ }
313
+ catch (error) {
314
+ brokenBy ??= { error };
315
+ throw error;
316
+ }
317
+ },
318
+ isBroken: () => brokenBy != null,
319
+ };
320
+ // 7. Register the default VFS.
321
+ const vfsName = encoder.encode(`${defaultVfsName}\0`);
322
+ const allocated = allocWasm({ sqliteWasm })(sqlite3_vfs_layout.sizeof + vfsName.length);
323
+ if (!allocated.ok)
324
+ return err({ type: "SqliteWasmInitializeError", code: SQLITE_NOMEM });
325
+ const vfs = allocated.value;
326
+ const zName = vfs + sqlite3_vfs_layout.sizeof;
327
+ const { members } = sqlite3_vfs_layout;
328
+ const vfsMethods = [
329
+ [
330
+ "xAccess",
331
+ (_vfs, _zName, _flags, pResOut) => {
332
+ getHeapDataView().setInt32(pResOut, 0, true);
333
+ return SQLITE_OK;
334
+ },
335
+ ],
336
+ ["xDelete", () => SQLITE_IOERR_DELETE_NOENT],
337
+ ["xFullPathname", () => SQLITE_CANTOPEN],
338
+ [
339
+ "xRandomness",
340
+ (_vfs, byteLength, pOut) => {
341
+ for (let offset = 0; offset < byteLength; offset += 65536)
342
+ getHeapU8().set(run.deps.randomBytes.create(Math.min(65536, byteLength - offset)), pOut + offset);
343
+ return byteLength;
344
+ },
345
+ ],
346
+ ["xSleep", () => 0],
347
+ ["xGetLastError", () => 0],
348
+ [
349
+ "xCurrentTime",
350
+ (_vfs, pTime) => {
351
+ getHeapDataView().setFloat64(pTime, context.time.now() / 86_400_000 + 2_440_587.5, true);
352
+ return SQLITE_OK;
353
+ },
354
+ ],
355
+ [
356
+ "xCurrentTimeInt64",
357
+ (_vfs, pTime) => {
358
+ // Milliseconds since the Julian day epoch, noon in Greenwich on
359
+ // November 24, 4714 BC.
360
+ getHeapDataView().setBigInt64(pTime, BigInt(context.time.now()) + 210866760000000n, true);
361
+ return SQLITE_OK;
362
+ },
363
+ ],
364
+ ];
365
+ const installed = trySync(() => installWasmFunctions({ ...run.deps, sqliteWasm })(mapArray(vfsMethods, ([name, fn]) => ({
366
+ signature: members[name].signature,
367
+ fn,
368
+ }))), toCompileError);
369
+ if (!installed.ok)
370
+ return installed;
371
+ const { pointers } = installed.value;
372
+ getHeapU8().fill(0, vfs, zName);
373
+ getHeapU8().set(vfsName, zName);
374
+ const vfsView = getHeapDataView();
375
+ for (const [name, value] of [
376
+ ["iVersion", 2],
377
+ ["szOsFile", sqlite3_file_layout.sizeof],
378
+ ["mxPathname", 1024],
379
+ ["zName", zName],
380
+ ])
381
+ vfsView.setInt32(vfs + members[name].offset, value, true);
382
+ for (const [[name], pointer] of zipArray([vfsMethods, pointers]))
383
+ vfsView.setInt32(vfs + members[name].offset, pointer, true);
384
+ const registered = exports.sqlite3_vfs_register(vfs, 0);
385
+ if (registered !== SQLITE_OK)
386
+ return err({ type: "SqliteWasmInitializeError", code: registered });
387
+ const initialized = initializeSqliteWasm({ sqliteWasm })();
388
+ if (!initialized.ok)
389
+ return initialized;
390
+ return ok(sqliteWasm);
391
+ };
392
+ /**
393
+ * Initializes the library as loading steps 6 and 7 do, for use after
394
+ * `sqlite3_shutdown` and `sqlite3_config`.
395
+ *
396
+ * `sqlite3_initialize` alone, or any call that initializes the library itself,
397
+ * such as an open, makes SQLite's `unix-none` VFS the default again and
398
+ * registers kvvfs again, so opening `:localStorage:` or `:sessionStorage:`
399
+ * traps and breaks the instance. Initializing leaves SQLite's random number
400
+ * generator as it is, but while `unix-none` is the default, a generator reset
401
+ * with `sqlite3_randomness(0, 0)`, or not used since loading, seeds itself at
402
+ * its next use from the time and a constant pid instead of {@link RandomBytes},
403
+ * so instances seeded in the same second share `randomblob()` values and
404
+ * journal nonces. This function resets the generator once `evolu-memory` is the
405
+ * default.
406
+ *
407
+ * Fails with {@link SqliteWasmInitializeError}, with SQLITE_MISUSE when
408
+ * `sqlite3_vfs_unregister` removed `evolu-memory`.
409
+ */
410
+ export const initializeSqliteWasm = (deps) => () => deps.sqliteWasm.call(() => {
411
+ const { exports } = deps.sqliteWasm;
412
+ const code = exports.sqlite3_initialize();
413
+ if (code !== SQLITE_OK)
414
+ return err({ type: "SqliteWasmInitializeError", code });
415
+ // Both names in one allocation, the second after the first's NUL.
416
+ const names = allocCString(deps)(`${defaultVfsName}\0kvvfs`);
417
+ if (!names.ok)
418
+ return err({ type: "SqliteWasmInitializeError", code: SQLITE_NOMEM });
419
+ const vfs = exports.sqlite3_vfs_find(names.value);
420
+ const kvvfs = exports.sqlite3_vfs_find((names.value + defaultVfsName.length + 1));
421
+ exports.sqlite3_free(names.value);
422
+ // Only sqlite3_vfs_unregister removes it.
423
+ if (vfs === 0)
424
+ return err({ type: "SqliteWasmInitializeError", code: SQLITE_MISUSE });
425
+ // sqlite3_initialize registers SQLite's kvvfs, whose storage only
426
+ // SQLite's JavaScript provides, so an open of it, which the names
427
+ // :localStorage: and :sessionStorage: are, would call NULL and trap.
428
+ if (kvvfs !== 0)
429
+ exports.sqlite3_vfs_unregister(kvvfs);
430
+ const registered = exports.sqlite3_vfs_register(vfs, 1);
431
+ if (registered !== SQLITE_OK)
432
+ return err({ type: "SqliteWasmInitializeError", code: registered });
433
+ exports.sqlite3_randomness(0, 0);
434
+ return ok();
435
+ });
436
+ const defaultVfsName = "evolu-memory";
437
+ /**
438
+ * Calls a function the instance exports with i32 arguments and returns its
439
+ * result, or returns 0 when it exports no function of that name. Loading uses
440
+ * it before the build guard, which fails a binary without the function.
441
+ */
442
+ const callExport = (instance, name, ...args) => {
443
+ const fn = instance.exports[name];
444
+ return typeof fn === "function"
445
+ ? fn(...args)
446
+ : 0;
447
+ };
448
+ // The JavaScript API does not expose the minimum a binary declares for its
449
+ // memory import, so this covers every build so far: the pinned one declares 128.
450
+ const initialMemoryPages = 256;
451
+ // 2 GiB, so every address is a non-negative i32.
452
+ const maximumMemoryPages = 32768;
453
+ const wasmPageSize = 65536;
454
+ // WASI's ENOSYS, "function not supported". WASI functions return it, and
455
+ // Emscripten's syscalls return it negated, as Linux does.
456
+ const ENOSYS = 52;
457
+ const failSyscall = () => -ENOSYS;
458
+ // WASI's EINVAL, "invalid argument".
459
+ const EINVAL = 28;
460
+ // WASI's EBADF, "bad file descriptor".
461
+ const EBADF = 8;
462
+ // Every import the loader implements, by module and name, each created from
463
+ // the instance's context.
464
+ const sqliteWasmImports = {
465
+ env: {
466
+ // Emscripten's version aborts. It must not return, because the function
467
+ // that called it then moves the stack pointer anyway.
468
+ __handle_stack_overflow: () => () => {
469
+ throw new WebAssembly.RuntimeError("SQLite's C stack overflowed.");
470
+ },
471
+ __syscall_chmod: () => failSyscall,
472
+ __syscall_faccessat: () => failSyscall,
473
+ __syscall_fchmod: () => failSyscall,
474
+ __syscall_fchown32: () => failSyscall,
475
+ __syscall_fcntl64: () => failSyscall,
476
+ __syscall_fstat64: () => failSyscall,
477
+ __syscall_ftruncate64: () => failSyscall,
478
+ __syscall_getcwd: () => failSyscall,
479
+ __syscall_ioctl: () => failSyscall,
480
+ __syscall_lstat64: () => failSyscall,
481
+ __syscall_mkdirat: () => failSyscall,
482
+ __syscall_newfstatat: () => failSyscall,
483
+ __syscall_openat: () => failSyscall,
484
+ __syscall_readlinkat: () => failSyscall,
485
+ __syscall_rmdir: () => failSyscall,
486
+ __syscall_stat64: () => failSyscall,
487
+ __syscall_unlinkat: () => failSyscall,
488
+ __syscall_utimensat: () => failSyscall,
489
+ _localtime_js: ({ getHeapDataView }) => (time, pTm) => {
490
+ const date = new Date(Number(time) * 1000);
491
+ if (Number.isNaN(date.getTime()))
492
+ return 1;
493
+ const heap = getHeapDataView();
494
+ // The fields of struct tm from tm_sec to tm_year, each 4 bytes. They
495
+ // are all SQLite's toLocaltime reads. Of tm_wday, tm_yday, tm_isdst and
496
+ // tm_gmtoff, which follow, the pinned binary reads only tm_isdst, in
497
+ // Emscripten's localtime_r, to point tm_zone at a time zone name that
498
+ // nothing reads either, so they are zero.
499
+ for (const [index, value] of [
500
+ date.getSeconds(),
501
+ date.getMinutes(),
502
+ date.getHours(),
503
+ date.getDate(),
504
+ date.getMonth(),
505
+ date.getFullYear() - 1900,
506
+ 0,
507
+ 0,
508
+ 0,
509
+ 0,
510
+ ].entries())
511
+ heap.setInt32(pTm + index * 4, value, true);
512
+ return 0;
513
+ },
514
+ // Emscripten's tzset calls it once to fill timezone, daylight and the time
515
+ // zone names. In the pinned binary, only localtime_r reads them, to point
516
+ // tm_zone at a name, which nothing reads, so they stay zero.
517
+ _tzset_js: () => () => { },
518
+ emscripten_date_now: ({ time }) => () => time.now(),
519
+ emscripten_get_now: ({ time }) => () => time.performance.now(),
520
+ emscripten_resize_heap: ({ memory }) => (requestedSize) => {
521
+ try {
522
+ memory.grow(Math.ceil(((requestedSize >>> 0) - memory.buffer.byteLength) / wasmPageSize));
523
+ return 1;
524
+ }
525
+ catch {
526
+ return 0;
527
+ }
528
+ },
529
+ memory: ({ memory }) => memory,
530
+ },
531
+ wasi_snapshot_preview1: {
532
+ clock_time_get: ({ time, getHeapDataView }) => (clockId, _precision, pTime) => {
533
+ // CLOCK_REALTIME is 0; the monotonic and CPU-time clocks are 1 to 3.
534
+ if (clockId < 0 || clockId > 3)
535
+ return EINVAL;
536
+ const millis = clockId === 0 ? time.now() : time.performance.now();
537
+ // Nanoseconds since 1970 exceed 2^53, so only microseconds, which
538
+ // clocks report at most anyway, are computed as a number.
539
+ getHeapDataView().setBigInt64(pTime, BigInt(Math.round(millis * 1000)) * 1000n, true);
540
+ return 0;
541
+ },
542
+ environ_get: () => () => 0,
543
+ environ_sizes_get: ({ getHeapDataView }) => (pCount, pBufferSize) => {
544
+ const heap = getHeapDataView();
545
+ heap.setUint32(pCount, 0, true);
546
+ heap.setUint32(pBufferSize, 0, true);
547
+ return 0;
548
+ },
549
+ fd_close: () => () => ENOSYS,
550
+ fd_fdstat_get: () => () => ENOSYS,
551
+ fd_read: () => () => ENOSYS,
552
+ fd_seek: () => () => ENOSYS,
553
+ fd_sync: () => () => ENOSYS,
554
+ fd_write: ({ console, getHeapU8, getHeapDataView }) => {
555
+ // Lines of stdout and stderr, written when complete.
556
+ const streams = new Map([console.log, console.error].map((write, index) => [
557
+ index + 1,
558
+ { write, decoder: new TextDecoder(), line: "" },
559
+ ]));
560
+ return (fd, iovs, iovsLength, pWritten) => {
561
+ const stream = streams.get(fd);
562
+ if (stream == null)
563
+ return EBADF;
564
+ const heap = getHeapDataView();
565
+ let written = 0;
566
+ for (let index = 0; index < iovsLength; index++) {
567
+ const ptr = heap.getUint32(iovs + index * 8, true);
568
+ const length = heap.getUint32(iovs + index * 8 + 4, true);
569
+ stream.line += stream.decoder.decode(getHeapU8().subarray(ptr, ptr + length), { stream: true });
570
+ written += length;
571
+ }
572
+ const lines = stream.line.split("\n");
573
+ // split returns at least one string, the line still incomplete.
574
+ stream.line = lines.pop();
575
+ for (const line of lines)
576
+ stream.write(line);
577
+ // A fresh view, because a Console that calls back into this instance
578
+ // can grow memory, which detaches the one taken before it wrote.
579
+ getHeapDataView().setUint32(pWritten, written, true);
580
+ return 0;
581
+ };
582
+ },
583
+ },
584
+ };
585
+ /**
586
+ * Installs functions into the function table.
587
+ *
588
+ * No engine exposes `WebAssembly.Function`, so a JavaScript function becomes a
589
+ * wasm function by being imported into a generated module that re-exports it.
590
+ * All functions of one call share one generated module. A call through one
591
+ * costs a few nanoseconds more than a call to a plain import, for the wrapper
592
+ * that catches exceptions: 12 ns against 9 ns in Node.js 24. A signature the
593
+ * notation does not cover throws. Compiling the generated module needs
594
+ * 'wasm-unsafe-eval' under a Content Security Policy; without it, this throws a
595
+ * `WebAssembly.CompileError`.
596
+ *
597
+ * No exception crosses into wasm. What a function throws is reported with
598
+ * {@link ReportDefect}, and the call returns SQLITE_ERROR, or nothing for a `v`
599
+ * result. So a commit hook rolls back, an `sqlite3_exec` callback aborts, and a
600
+ * VFS method fails, but a busy handler retries, forever if it always throws,
601
+ * and a SQL function's `xFunc` returns NULL. A function whose failure must mean
602
+ * something else catches its own errors, such as an `xFunc` calling
603
+ * `sqlite3_result_error`. An `i` function that returns something other than a
604
+ * number, or undefined for 0, is also reported as a defect and returns
605
+ * SQLITE_ERROR. Two kinds pass through, because they unwound C frames without
606
+ * cleanup: a `WebAssembly.RuntimeError`, which comes from a trap or a C stack
607
+ * overflow in wasm the function called, and anything thrown once the instance
608
+ * is broken, such as a JavaScript stack overflow that escaped a nested
609
+ * {@link SqliteWasm.call}. A function that returns once the instance is broken,
610
+ * as one that caught what a nested call rethrew, or whose ReportDefect broke
611
+ * it, throws the refusal of a broken instance instead, so SQLite never resumes
612
+ * on that state.
613
+ *
614
+ * Disposing the result empties its slots and keeps them for later installs, so
615
+ * per-database callbacks, such as an update hook, do not grow the table with
616
+ * each open. Dispose only once SQLite can no longer call the functions, for
617
+ * example after closing the database that holds them or replacing a hook: a
618
+ * call through an emptied slot traps, and a reused slot calls another function.
619
+ * A VFS's methods stay installed for the lifetime of the instance.
620
+ */
621
+ export const installWasmFunctions = (deps) => (functions) => {
622
+ const env_1 = { stack: [], error: void 0, hasError: false };
623
+ try {
624
+ const table = deps.sqliteWasm.functionTable;
625
+ const freeSlots = freeSlotsByTable.get(table) ?? [];
626
+ freeSlotsByTable.set(table, freeSlots);
627
+ // The module imports each function from "" under its index, with the type
628
+ // its signature describes, and exports it under the same name.
629
+ const types = functions.map(({ signature }) => {
630
+ const parsed = /^([iv])\(([ipsjd]*)\)$/u.exec(signature);
631
+ if (parsed == null)
632
+ throw new Error(`Invalid wasm function signature: ${signature}`);
633
+ const [, result, params] = parsed;
634
+ return [
635
+ 0x60,
636
+ ...wasmVector(Array.from(params, (letter) => [wasmValueTypes[letter]])),
637
+ ...wasmVector(result === "v" ? [] : [[wasmValueTypes.i]]),
638
+ ];
639
+ });
640
+ const names = functions.map((_, index) => wasmName(String(index)));
641
+ const module = new WebAssembly.Module(Uint8Array.from([
642
+ ...wasmModuleHeader,
643
+ ...wasmSection(wasmTypeSection, types),
644
+ ...wasmSection(wasmImportSection, names.map((name, index) => [
645
+ ...wasmName(""),
646
+ ...name,
647
+ wasmFunctionKind,
648
+ ...wasmUnsigned(index),
649
+ ])),
650
+ ...wasmSection(wasmExportSection, names.map((name, index) => [
651
+ ...name,
652
+ wasmFunctionKind,
653
+ ...wasmUnsigned(index),
654
+ ])),
655
+ ]));
656
+ const imports = functions.map(({ signature, fn }) => {
657
+ const call = fn;
658
+ const hasResult = signature.startsWith("i");
659
+ const failure = hasResult ? SQLITE_ERROR : undefined;
660
+ return (...args) => {
661
+ try {
662
+ const result = call(...args);
663
+ // A call the function made broke the instance, and the function
664
+ // caught what escaped it. Returning would resume the C frames below
665
+ // on the state that left inconsistent, so this throws the refusal of
666
+ // a broken instance, whose cause is what broke it, and the catch
667
+ // below lets it through.
668
+ if (deps.sqliteWasm.isBroken())
669
+ deps.sqliteWasm.call(constVoid);
670
+ if (!hasResult)
671
+ return;
672
+ // Converting a result to i32 happens after this function returns, so
673
+ // a bigint, symbol or object that cannot become a number would throw
674
+ // into wasm, past this catch.
675
+ if (result === undefined || typeof result === "number")
676
+ return result;
677
+ throw new TypeError(`A wasm function with an int result returned a ${typeof result}`);
678
+ }
679
+ catch (error) {
680
+ if (error instanceof WebAssembly.RuntimeError ||
681
+ deps.sqliteWasm.isBroken())
682
+ throw error;
683
+ try {
684
+ deps.reportDefect(error);
685
+ }
686
+ catch (reporterError) {
687
+ reportDefectAfterMicrotask(new AggregateError([error, reporterError], "ReportDefect failed while reporting a defect"));
688
+ }
689
+ // ReportDefect can break the instance too, by a nested call, so this
690
+ // throws the refusal as above instead of returning into SQLite.
691
+ if (deps.sqliteWasm.isBroken())
692
+ deps.sqliteWasm.call(constVoid);
693
+ return failure;
694
+ }
695
+ };
696
+ });
697
+ const { exports } = new WebAssembly.Instance(module, {
698
+ "": Object.fromEntries(imports.entries()),
699
+ });
700
+ const pointers = mapArray(functions, (_, index) => {
701
+ const pointer = freeSlots.pop() ?? table.grow(1);
702
+ table.set(pointer, exports[index]);
703
+ return pointer;
704
+ });
705
+ const disposer = __addDisposableResource(env_1, new DisposableStack(), false);
706
+ disposer.defer(() => {
707
+ for (const pointer of pointers) {
708
+ table.set(pointer, null);
709
+ freeSlots.push(pointer);
710
+ }
711
+ });
712
+ return disposable({ pointers }, disposer);
713
+ }
714
+ catch (e_1) {
715
+ env_1.error = e_1;
716
+ env_1.hasError = true;
717
+ }
718
+ finally {
719
+ __disposeResources(env_1);
720
+ }
721
+ };
722
+ // The emptied slots of each instance's function table.
723
+ const freeSlotsByTable = /*#__PURE__*/ new WeakMap();
724
+ // The binary format's magic number and version 1.
725
+ const wasmModuleHeader = [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
726
+ const wasmTypeSection = 1;
727
+ const wasmImportSection = 2;
728
+ const wasmExportSection = 7;
729
+ const wasmFunctionKind = 0x00;
730
+ // The encodings of i32, i64 and f64, by signature letter.
731
+ const wasmValueTypes = {
732
+ i: 0x7f,
733
+ p: 0x7f,
734
+ s: 0x7f,
735
+ j: 0x7e,
736
+ d: 0x7c,
737
+ };
738
+ /** Encodes an unsigned integer as LEB128. */
739
+ const wasmUnsigned = (value) => {
740
+ const bytes = [];
741
+ do {
742
+ const low = value & 0x7f;
743
+ value >>>= 7;
744
+ bytes.push(value === 0 ? low : low | 0x80);
745
+ } while (value !== 0);
746
+ return bytes;
747
+ };
748
+ /** Encodes items, each already encoded, as a vector: their count, then them. */
749
+ const wasmVector = (items) => [...wasmUnsigned(items.length), ...items.flat()];
750
+ /** Encodes an ASCII name. */
751
+ const wasmName = (name) => wasmVector(Array.from(name, (char) => [char.charCodeAt(0)]));
752
+ /** Encodes a section of encoded items. */
753
+ const wasmSection = (id, items) => {
754
+ const body = wasmVector(items);
755
+ return [id, ...wasmUnsigned(body.length), ...body];
756
+ };