@cldmv/sqlite-wasm 0.0.1-placeholder → 0.1.0

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 (50) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +565 -3
  3. package/dist/chunk-IEYL6MVW.mjs +2 -0
  4. package/dist/chunk-IRDSVJEM.mjs +2 -0
  5. package/dist/chunk-IXZKMCUU.mjs +2 -0
  6. package/dist/chunk-VOHQWANF.mjs +2 -0
  7. package/dist/chunk-VRZKKYFL.mjs +2 -0
  8. package/dist/chunk-YTH6FRG3.mjs +2 -0
  9. package/dist/drizzle.cjs +26 -0
  10. package/dist/drizzle.mjs +2 -0
  11. package/dist/fs-B55OKCP5.mjs +2 -0
  12. package/dist/host.mjs +2 -0
  13. package/dist/index.cjs +26 -0
  14. package/dist/index.mjs +2 -0
  15. package/dist/opfs-CREACZOE.mjs +2 -0
  16. package/dist/sqlite3.wasm +0 -0
  17. package/dist/worker.mjs +2 -0
  18. package/package.json +150 -3
  19. package/types/src/api/connection.d.mts +86 -0
  20. package/types/src/api/database.d.mts +109 -0
  21. package/types/src/api/error.d.mts +25 -0
  22. package/types/src/api/functions.d.mts +50 -0
  23. package/types/src/api/open.d.mts +90 -0
  24. package/types/src/api/runtime.d.mts +46 -0
  25. package/types/src/api/statement.d.mts +106 -0
  26. package/types/src/api/transaction.d.mts +37 -0
  27. package/types/src/api/values.d.mts +35 -0
  28. package/types/src/core/browser-loader.d.mts +45 -0
  29. package/types/src/core/callbacks.d.mts +49 -0
  30. package/types/src/core/codes.d.mts +17 -0
  31. package/types/src/core/constants.d.mts +75 -0
  32. package/types/src/core/loader.d.mts +126 -0
  33. package/types/src/core/memory.d.mts +48 -0
  34. package/types/src/core/platform.d.mts +40 -0
  35. package/types/src/core/raw.d.mts +48 -0
  36. package/types/src/core/router.d.mts +50 -0
  37. package/types/src/core/vfs-imports.d.mts +58 -0
  38. package/types/src/core/wasi.d.mts +28 -0
  39. package/types/src/drizzle.d.mts +64 -0
  40. package/types/src/index.d.mts +47 -0
  41. package/types/src/node.d.mts +25 -0
  42. package/types/src/vfs/fs.d.mts +72 -0
  43. package/types/src/vfs/memory.d.mts +39 -0
  44. package/types/src/vfs/opfs.d.mts +98 -0
  45. package/types/src/vfs/registry.d.mts +76 -0
  46. package/types/src/worker/client.d.mts +142 -0
  47. package/types/src/worker/engine.d.mts +46 -0
  48. package/types/src/worker/host.d.mts +50 -0
  49. package/types/src/worker/protocol.d.mts +202 -0
  50. package/types/src/worker/share.d.mts +69 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/vfs/opfs.d.mts
5
+ * @Date: 2026-11-25T02:10:00-08:00 (1795601400)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:13-08:00 (1795642213)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ import type { VfsDriver } from "../core/vfs-imports.mts";
17
+ /** The subset of `FileSystemSyncAccessHandle` the driver uses. */
18
+ export interface handle {
19
+ read(buffer: Uint8Array, options: {
20
+ at: number;
21
+ }): number;
22
+ write(buffer: Uint8Array, options: {
23
+ at: number;
24
+ }): number;
25
+ truncate(size: number): void;
26
+ getSize(): number;
27
+ flush(): void;
28
+ close(): void;
29
+ }
30
+ /** The subset of `FileSystemFileHandle` the driver uses. */
31
+ export interface file {
32
+ createSyncAccessHandle(): Promise<handle>;
33
+ }
34
+ /** The subset of `FileSystemDirectoryHandle` the driver uses. */
35
+ export interface directory {
36
+ getDirectoryHandle(name: string, options?: {
37
+ create?: boolean;
38
+ }): Promise<directory>;
39
+ getFileHandle(name: string, options?: {
40
+ create?: boolean;
41
+ }): Promise<file>;
42
+ removeEntry(name: string): Promise<void>;
43
+ }
44
+ /** The subset of the Web Locks `LockManager` the driver uses. */
45
+ export interface locks {
46
+ request(name: string, options: {
47
+ ifAvailable?: boolean;
48
+ signal?: AbortSignal;
49
+ }, callback: (lock: unknown) => Promise<void> | void): Promise<unknown>;
50
+ }
51
+ /** Options for `create()`. */
52
+ export interface options {
53
+ /** The directory databases live in; `navigator.storage.getDirectory()` when omitted. */
54
+ root?: directory | Promise<directory>;
55
+ /** The Web Locks manager; `navigator.locks` when omitted. Pass `null` to rely on the exclusive sync handles alone. */
56
+ locks?: locks | null;
57
+ /**
58
+ * How long `prepare()` waits, in milliseconds, for another tab to close the database. `0` fails at once,
59
+ * `Infinity` waits for as long as it takes. Defaults to `timeout`, else 0.
60
+ */
61
+ wait?: number;
62
+ /** The open's busy timeout; `sqlite.open()` passes it, and it is the default for `wait`. */
63
+ timeout?: number;
64
+ }
65
+ /** Options for `prepare()`. */
66
+ export interface preparation {
67
+ /** Create the database file when it doesn't exist (default true). */
68
+ create?: boolean;
69
+ }
70
+ /** The OPFS driver: a VFS driver plus the asynchronous steps around opening and closing a database. */
71
+ export interface driver extends VfsDriver {
72
+ /**
73
+ * Takes the database's lock and opens its files. Await it before `sqlite3_open_v2`, and pass the returned name to
74
+ * it: SQLite derives the journal and WAL names from that exact string.
75
+ */
76
+ prepare(path: string, options?: preparation): Promise<string>;
77
+ /** Closes what `prepare()` opened and drops the lock. Call it after the connection is closed. */
78
+ release(path: string): Promise<void>;
79
+ /** The lock that keeps other tabs out: "web" with Web Locks, "none" when they are switched off (`locks: null`). */
80
+ held(): string;
81
+ }
82
+ /**
83
+ * Turns a path into the canonical name SQLite uses for it: `/`-separated, with a leading `/`, no empty or `.`
84
+ * segments. `..` is refused since OPFS has no parent of the root.
85
+ */
86
+ export declare function normalize(path: string): string;
87
+ /**
88
+ * Creates an OPFS driver. One driver can serve any number of databases; each database is opened by one connection.
89
+ * `sqlite.open()` calls it with its own options ({ path, readonly, create, timeout, lock }); everything this driver
90
+ * doesn't know is ignored, and `root` / `locks` fall back to the browser's own.
91
+ */
92
+ export declare function create(options?: options): driver;
93
+ /** The driver factory, as a namespace: `opfs.create(options)`. */
94
+ declare const opfs: {
95
+ create: typeof create;
96
+ normalize: typeof normalize;
97
+ };
98
+ export default opfs;
@@ -0,0 +1,76 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/vfs/registry.d.mts
5
+ * @Date: 2026-11-25T01:43:39-08:00 (1795599819)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:13-08:00 (1795642213)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ import type { VfsDriver } from "../core/vfs-imports.mts";
17
+ import { MemoryDriver } from "./memory.mts";
18
+ /** A concrete storage kind ("auto" resolves to one of these). */
19
+ export type StorageKind = "memory" | "fs" | "opfs";
20
+ /** What a driver factory is told about the file it is opened for. */
21
+ export interface DriverOptions {
22
+ /** The database path as the caller passed it. */
23
+ path: string;
24
+ /** Opened read-only. */
25
+ readonly: boolean;
26
+ /** The file may be created. */
27
+ create: boolean;
28
+ /** Busy timeout in milliseconds; also how long opening waits for another process or tab to let go. */
29
+ timeout: number;
30
+ /** The cross-process lock: "beside", "temp", "db" or a folder path (node-fs only; other drivers ignore it). */
31
+ lock: string;
32
+ }
33
+ /**
34
+ * A driver as a module's `create()` returns it. Drivers whose files must be made ready asynchronously before SQLite
35
+ * touches them (OPFS: a Web Lock plus sync access handles for the database, -journal and -wal; node-fs: the owner
36
+ * lock) add hooks:
37
+ * - `prepare(path, options)` runs before sqlite3_open_v2 / ATTACH and resolves to the name SQLite opens;
38
+ * - `release(name)` runs after sqlite3_close_v2 / DETACH (and when an open or attach fails after `prepare`). Close
39
+ * is synchronous, so a promise it returns is not awaited and its rejection is ignored;
40
+ * - `held(name)` reports the lock kind in effect for a prepared name (`db.info.lock`).
41
+ */
42
+ export interface StorageDriver extends VfsDriver {
43
+ prepare?(path: string, options: DriverOptions): Promise<string>;
44
+ release?(name: string): unknown;
45
+ held?(name: string): string;
46
+ }
47
+ /** The shape of a driver module (src/vfs/<kind>.mts). */
48
+ export interface DriverModule {
49
+ create(options: DriverOptions): StorageDriver | Promise<StorageDriver>;
50
+ }
51
+ /** The process-wide memory driver; also the router's fallback for temporary files. */
52
+ export declare function sharedMemory(): MemoryDriver;
53
+ /**
54
+ * Replaces the loader for `kind` and returns the previous one. Internal: tests use it to stand in drivers, and a phase
55
+ * that adds a driver module can try it before the static import above is switched over.
56
+ */
57
+ export declare function install(kind: StorageKind, loader: () => Promise<DriverModule>): () => Promise<DriverModule>;
58
+ /** The globals `detect()` looks at. */
59
+ export interface Environment {
60
+ process?: {
61
+ versions?: {
62
+ node?: string;
63
+ };
64
+ };
65
+ FileSystemSyncAccessHandle?: unknown;
66
+ }
67
+ /**
68
+ * The storage "auto" means here: files in Node, OPFS in a dedicated browser worker. Anywhere else (a browser main
69
+ * thread, a shared or service worker) there is no persistent synchronous storage, and "auto" refuses with an error
70
+ * that says what to do instead; `storage: "memory"` still works there.
71
+ */
72
+ export declare function detect(scope?: Environment): StorageKind;
73
+ /** Whether `value` names a storage kind or "auto". */
74
+ export declare function isStorage(value: unknown): value is StorageKind | "auto";
75
+ /** Loads the driver module for `kind` and creates a driver for one database file. */
76
+ export declare function load(kind: StorageKind, options: DriverOptions): Promise<StorageDriver>;
@@ -0,0 +1,142 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/worker/client.d.mts
5
+ * @Date: 2026-11-25T02:10:00-08:00 (1795601400)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:13-08:00 (1795642213)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ import { error, type endpoint, type settings, type value } from "./protocol.mts";
17
+ import { type watcher } from "./share.mts";
18
+ /** Options for `connect()`: the worker's open settings, and `share` to use one database from several tabs. */
19
+ export type options = settings & {
20
+ /** Share the database with the origin's other tabs that connect with `share: true` (needs the worker's URL). */
21
+ share?: boolean;
22
+ };
23
+ /** What `connect()` talks to: a worker script's URL (a module worker is started), or a Worker / MessagePort already running the host. */
24
+ export type target = string | URL | endpoint;
25
+ /** A row as an object keyed by column name. */
26
+ export type row = Record<string, unknown>;
27
+ /** What `run()` resolves to. */
28
+ export interface result {
29
+ changes: number;
30
+ rowid: number | bigint;
31
+ }
32
+ /** Row readers that return positional arrays, the first column, or objects grouped by table. */
33
+ export interface reader<item> {
34
+ get(...params: unknown[]): Promise<item | undefined>;
35
+ all(...params: unknown[]): Promise<item[]>;
36
+ }
37
+ /** A statement on the client. Nothing is compiled until the first call; the worker caches the compiled statement. */
38
+ export interface statement {
39
+ run(...params: unknown[]): Promise<result>;
40
+ get(...params: unknown[]): Promise<row | undefined>;
41
+ all(...params: unknown[]): Promise<row[]>;
42
+ iterate(...params: unknown[]): AsyncGenerator<row>;
43
+ iter(...params: unknown[]): AsyncGenerator<row>;
44
+ raw: reader<unknown[]> & {
45
+ iterate(...params: unknown[]): AsyncGenerator<unknown[]>;
46
+ iter(...params: unknown[]): AsyncGenerator<unknown[]>;
47
+ };
48
+ pluck: reader<unknown>;
49
+ expand: reader<Record<string, row>>;
50
+ bind(...params: unknown[]): statement;
51
+ columns(): Promise<unknown[]>;
52
+ cols(): Promise<unknown[]>;
53
+ close(): void;
54
+ info: {
55
+ readonly sql: string;
56
+ };
57
+ }
58
+ /** The PRAGMA helpers. */
59
+ export interface pragma {
60
+ get(name: string): Promise<unknown>;
61
+ set(name: string, value: value): Promise<void>;
62
+ all(name: string, argument?: value): Promise<row[]>;
63
+ }
64
+ /** What a transaction callback receives. */
65
+ export interface transaction {
66
+ prepare(sql: string): statement;
67
+ prep(sql: string): statement;
68
+ execute(sql: string): Promise<void>;
69
+ exec(sql: string): Promise<void>;
70
+ pragma: pragma;
71
+ }
72
+ /** Runs `callback` as one transaction: committed when it resolves, rolled back when it throws. */
73
+ export interface runner {
74
+ <value>(callback: (tx: transaction) => Promise<value> | value): Promise<value>;
75
+ deferred<value>(callback: (tx: transaction) => Promise<value> | value): Promise<value>;
76
+ immediate<value>(callback: (tx: transaction) => Promise<value> | value): Promise<value>;
77
+ exclusive<value>(callback: (tx: transaction) => Promise<value> | value): Promise<value>;
78
+ }
79
+ /** The drizzle `sqlite-proxy` callback. */
80
+ export type proxy = (sql: string, params: unknown[], method: "run" | "all" | "values" | "get") => Promise<{
81
+ rows: unknown;
82
+ }>;
83
+ /** `db.fn` on the client: every method throws, since functions have to be registered in the worker. */
84
+ export interface fn {
85
+ scalar(...ignored: unknown[]): never;
86
+ aggregate(...ignored: unknown[]): never;
87
+ agg(...ignored: unknown[]): never;
88
+ collation(...ignored: unknown[]): never;
89
+ }
90
+ /** A connection to a database in a worker. */
91
+ export interface database extends transaction {
92
+ transaction: runner;
93
+ tx: runner;
94
+ fn: fn;
95
+ attach(path: string, schema: string): Promise<void>;
96
+ detach(schema: string): Promise<void>;
97
+ /** Copies the database (or the attached `options.schema`) into `target` in the worker's storage. */
98
+ backup(target: string, options?: {
99
+ schema?: string;
100
+ }): Promise<void>;
101
+ /** The database (or attached `schema`) as the bytes of an SQLite file; the buffer is transferred from the worker. */
102
+ serialize(schema?: string): Promise<Uint8Array>;
103
+ checkpoint(mode?: "passive" | "full" | "restart" | "truncate"): Promise<row | undefined>;
104
+ ckpt(mode?: "passive" | "full" | "restart" | "truncate"): Promise<row | undefined>;
105
+ close(): Promise<void>;
106
+ proxy: proxy;
107
+ info: {
108
+ readonly path: string;
109
+ readonly storage: string;
110
+ readonly lock: string;
111
+ readonly open: boolean;
112
+ readonly transaction: boolean;
113
+ };
114
+ }
115
+ /** A connection made with `share: true`: `info` also says whether this tab leads, and reports changes. */
116
+ export interface shared extends database {
117
+ info: database["info"] & {
118
+ /** Whether this tab's worker holds the database (the leader) or this tab goes through another tab (a follower). */
119
+ readonly leader: boolean;
120
+ /** Calls `listener` with the new value whenever `leader` changes; returns the function that stops it. */
121
+ watch(listener: watcher): () => void;
122
+ };
123
+ }
124
+ /**
125
+ * Opens `path` in a worker running the host (`host.listen()`).
126
+ * @param target the worker script's URL (started as a module worker, and terminated on close), or a Worker /
127
+ * MessagePort already connected to a host
128
+ * @param path the database path in OPFS (or a name, with `storage: "memory"`)
129
+ * @param options storage, journal mode, readonly, create, and `share` to share the database between tabs
130
+ */
131
+ export declare function connect(target: target, path: string, options: options & {
132
+ share: true;
133
+ }): Promise<shared>;
134
+ export declare function connect(target: target, path: string, options?: options): Promise<database>;
135
+ /** The client as a namespace: `client.connect(...)`; `client.error` (alias `err`) is the class calls reject with. */
136
+ declare const client: {
137
+ connect: typeof connect;
138
+ error: typeof error;
139
+ err: typeof error;
140
+ };
141
+ export default client;
142
+ export { error, error as err };
@@ -0,0 +1,46 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/worker/engine.d.mts
5
+ * @Date: 2026-11-25T02:10:00-08:00 (1795601400)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:13-08:00 (1795642213)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ /**
17
+ * @fileoverview The connection the worker host drives: the public API (`sqlite.open()`, `db.prepare()`) behind the
18
+ * small interface the host's batching works against. Statements are prepared once per SQL text and kept in a
19
+ * least-recently-used cache, since the client sends SQL text rather than statement handles.
20
+ */
21
+ import { database } from "../api/database.mts";
22
+ import { type options } from "../api/open.mts";
23
+ import type { column } from "../api/statement.mts";
24
+ import type { parameters, shape } from "./protocol.mts";
25
+ /** Column metadata, as `statement.columns()` returns it. */
26
+ export type { column };
27
+ /** What the host needs from a connection. */
28
+ export interface engine {
29
+ /** The database itself (the host's `setup` hook and the operations without a shortcut here use it). */
30
+ readonly db: database;
31
+ /** Runs every statement in `sql`, discarding rows. */
32
+ execute(sql: string): void;
33
+ /** Runs one statement. `run` returns a change; otherwise all rows, or the first row (undefined when none) with `first`. */
34
+ statement(sql: string, params: parameters, shape: shape, first: boolean): unknown;
35
+ /** Column metadata of the statement's result. */
36
+ columns(sql: string): column[];
37
+ /** Whether no transaction is open. */
38
+ autocommit(): boolean;
39
+ /** Closes the cached statements and the connection; settles when its files are released (OPFS handles, locks). */
40
+ close(): Promise<void>;
41
+ }
42
+ /**
43
+ * Opens `path` through the public API. `extra` reaches the storage driver along with the options (the host's OPFS
44
+ * root, lock manager and wait).
45
+ */
46
+ export declare function open(path: string, options: options, extra?: Record<string, unknown>): Promise<engine>;
@@ -0,0 +1,50 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/worker/host.d.mts
5
+ * @Date: 2026-11-25T02:10:00-08:00 (1795601400)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:13-08:00 (1795642213)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ import { type source } from "../core/browser-loader.mts";
17
+ import type { database } from "../api/database.mts";
18
+ import type { options as opfsoptions } from "../vfs/opfs.mts";
19
+ import { type engine } from "./engine.mts";
20
+ import { type endpoint, type settings } from "./protocol.mts";
21
+ /** Options for `listen()`: where the wasm comes from, the OPFS driver's options, and a hook run after open. */
22
+ export interface options extends Omit<opfsoptions, "timeout"> {
23
+ /** sqlite3.wasm as a URL, bytes, a Response or a compiled module; the package's own file when omitted. */
24
+ wasm?: source;
25
+ /** Runs after each open with the database (register functions and collations here); a throw fails the open. */
26
+ setup?: (db: database) => unknown;
27
+ }
28
+ /** What the host depends on; `listen()` fills it in, tests swap parts out. */
29
+ export interface dependencies {
30
+ connect(path: string, settings: settings): Promise<engine>;
31
+ setup?: (db: database) => unknown;
32
+ /**
33
+ * Takes the Web Lock `name` for the rest of the worker's life; resolves to whether it could (false without Web
34
+ * Locks). A shared connection's leader tab waits on the same lock to learn that its worker is gone.
35
+ */
36
+ hold?: (name: string) => Promise<boolean>;
37
+ }
38
+ /** Answers requests arriving on `scope`. Returns a function that stops listening (the database stays as it is). */
39
+ export declare function serve(scope: endpoint, dependencies: dependencies): () => void;
40
+ /**
41
+ * Starts answering the client. Call it once at the top of the worker file.
42
+ * @param scope where requests arrive; the worker's global scope by default
43
+ * @param options the wasm location, the OPFS driver's options, and the `setup(db)` hook
44
+ */
45
+ export declare function listen(scope?: endpoint, options?: options): () => void;
46
+ /** The host as a namespace: `host.listen()`. */
47
+ declare const host: {
48
+ listen: typeof listen;
49
+ };
50
+ export default host;
@@ -0,0 +1,202 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/worker/protocol.d.mts
5
+ * @Date: 2026-11-25T02:10:00-08:00 (1795601400)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:14-08:00 (1795642214)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ /**
17
+ * @fileoverview The messages between the main-thread client and the worker host, and how errors cross between them.
18
+ *
19
+ * The client sends a request: an id and a list of calls. The host answers once per request with one outcome per
20
+ * call, in order. Calls the client makes in the same tick travel in one request. A request marked `tx` belongs to a
21
+ * transaction the client is running: after a failing call the host skips the rest and rolls back. Calls in an
22
+ * ordinary request are independent; the host may still run neighbouring statements inside one transaction so they
23
+ * share a single commit.
24
+ *
25
+ * Values are sent with the structured clone algorithm, which keeps bigint, Uint8Array and null as they are. A
26
+ * serialized database's buffer is transferred rather than copied.
27
+ *
28
+ * Nested transactions travel as `savepoint` / `release` / `revert` (ROLLBACK TO and RELEASE) calls inside the
29
+ * outer transaction's requests; a failing call inside a savepoint undoes only that savepoint.
30
+ *
31
+ * `hold` asks the worker to take a Web Lock and keep it for the rest of its life. A shared connection's leader waits on
32
+ * that lock to learn when its worker has ended.
33
+ */
34
+ import { error } from "../api/error.mts";
35
+ /** A bound parameter value. */
36
+ export type value = null | number | bigint | string | Uint8Array | boolean;
37
+ /** Positional parameters, or one object of named parameters. */
38
+ export type parameters = value[] | Record<string, value>;
39
+ /** How a statement's rows come back: objects, positional arrays, the first column, or grouped by table. */
40
+ export type shape = "run" | "object" | "raw" | "pluck" | "expand";
41
+ /** Journal modes `open` accepts. */
42
+ export type journal = "wal" | "truncate" | "delete";
43
+ /**
44
+ * Options for opening a database in the worker: `sqlite.open()`'s options, except that storage is "opfs" (the
45
+ * default) or "memory", and there is no `lock` (the browser always uses Web Locks).
46
+ */
47
+ export interface settings {
48
+ /** "opfs" (the default) or "memory". */
49
+ storage?: "opfs" | "memory";
50
+ journal?: journal;
51
+ sync?: "off" | "normal" | "full" | "extra";
52
+ foreign?: boolean;
53
+ readonly?: boolean;
54
+ create?: boolean;
55
+ timeout?: number;
56
+ bigint?: boolean;
57
+ }
58
+ /** One operation the host performs. */
59
+ export type call = {
60
+ op: "open";
61
+ path: string;
62
+ options: settings;
63
+ } | {
64
+ op: "execute";
65
+ sql: string;
66
+ } | {
67
+ op: "statement";
68
+ sql: string;
69
+ params: parameters;
70
+ shape: shape;
71
+ first: boolean;
72
+ } | {
73
+ op: "columns";
74
+ sql: string;
75
+ } | {
76
+ op: "attach";
77
+ path: string;
78
+ schema: string;
79
+ } | {
80
+ op: "detach";
81
+ schema: string;
82
+ } | {
83
+ op: "backup";
84
+ target: string;
85
+ schema?: string;
86
+ } | {
87
+ op: "serialize";
88
+ schema?: string;
89
+ } | {
90
+ op: "begin";
91
+ mode: string;
92
+ } | {
93
+ op: "commit";
94
+ } | {
95
+ op: "rollback";
96
+ } | {
97
+ op: "savepoint";
98
+ name: string;
99
+ } | {
100
+ op: "release";
101
+ name: string;
102
+ } | {
103
+ op: "revert";
104
+ name: string;
105
+ } | {
106
+ op: "close";
107
+ } | {
108
+ op: "hold";
109
+ name: string;
110
+ };
111
+ /** Client -> host. */
112
+ export interface request {
113
+ id: number;
114
+ tx: boolean;
115
+ calls: call[];
116
+ }
117
+ /** An error in transit. `number` is set for SQLite errors. */
118
+ export interface failure {
119
+ name: string;
120
+ message: string;
121
+ code?: string;
122
+ number?: number;
123
+ sql?: string;
124
+ }
125
+ /** The result of one call. */
126
+ export type outcome = {
127
+ ok: true;
128
+ value: unknown;
129
+ } | {
130
+ ok: false;
131
+ error: failure;
132
+ };
133
+ /** Host -> client. */
134
+ export interface response {
135
+ id: number;
136
+ outcomes: outcome[];
137
+ }
138
+ /** The minimal message endpoint both sides use: a Worker, a worker's global scope, or a MessagePort. */
139
+ export interface endpoint {
140
+ postMessage(message: unknown, transfer?: Transferable[]): void;
141
+ addEventListener(type: "message", listener: (event: {
142
+ data: unknown;
143
+ }) => void): void;
144
+ removeEventListener(type: "message", listener: (event: {
145
+ data: unknown;
146
+ }) => void): void;
147
+ start?(): void;
148
+ }
149
+ /**
150
+ * The error a call rejects with on the client: `sqlite.error`, the same class the worker throws. An Error loses its
151
+ * class and extra fields in postMessage (structured clone keeps only name and message), so the host sends the fields
152
+ * and the client rebuilds the error from them. The published entries share one copy of the class (dist/ is built with
153
+ * code splitting), so `client.error` is `sqlite.error` and an error from `@cldmv/sqlite-wasm/worker` is an instance of
154
+ * the class the main entry exports.
155
+ */
156
+ export { error };
157
+ /**
158
+ * Prepares an error for postMessage. Anything with a numeric `number` is an SQLite error; its own `sql` wins over the
159
+ * statement the call ran (they differ only for errors that don't come from a statement).
160
+ */
161
+ export declare function serialize(thrown: unknown, sql?: string): failure;
162
+ /** Turns a received failure back into an Error: `sqlite.error` for SQLite errors, a plain Error (with its name) otherwise. */
163
+ export declare function revive(data: failure): Error;
164
+ /** What opening a database reports back: the resolved path, the storage and the lock in effect. */
165
+ export interface opened {
166
+ path: string;
167
+ storage: string;
168
+ lock: string;
169
+ }
170
+ /**
171
+ * Messages between tabs that share one database (`connect(..., { share: true })`), sent over a BroadcastChannel named
172
+ * `sqlite:<path>`. Every tab sees every message, so each one names the tab it is for or from.
173
+ *
174
+ * - `hello`: a tab joined and wants to know the leader.
175
+ * - `leader`: the leader's announcement, sent when it has opened the database and in answer to `hello`.
176
+ * - `request`: a follower's request for the leader named in `to`; a leader ignores requests addressed to another tab.
177
+ * - `started`: the leader is about to run a follower's write, so a retry after a handover could run it twice.
178
+ * - `response`: the leader's answer to a follower's request.
179
+ */
180
+ export type notice = {
181
+ type: "hello";
182
+ tab: string;
183
+ } | {
184
+ type: "leader";
185
+ tab: string;
186
+ info: opened;
187
+ } | {
188
+ type: "request";
189
+ to: string;
190
+ tab: string;
191
+ write: boolean;
192
+ request: request;
193
+ } | {
194
+ type: "started";
195
+ leader: string;
196
+ tab: string;
197
+ id: number;
198
+ } | {
199
+ type: "response";
200
+ tab: string;
201
+ response: response;
202
+ };
@@ -0,0 +1,69 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/sqlite-wasm
4
+ * @Filename: types/src/worker/share.d.mts
5
+ * @Date: 2026-11-25T05:14:19-08:00 (1795612459)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-11-25T13:30:14-08:00 (1795642214)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+
16
+ import { type endpoint, type request } from "./protocol.mts";
17
+ /** The parts of a LockManager sharing uses. */
18
+ export interface locks {
19
+ request(name: string, options: {
20
+ signal?: AbortSignal;
21
+ }, callback: (lock: unknown) => unknown): Promise<unknown>;
22
+ }
23
+ /** The parts of a BroadcastChannel sharing uses. */
24
+ export interface broadcast {
25
+ postMessage(message: unknown): void;
26
+ addEventListener(type: "message", listener: (event: {
27
+ data: unknown;
28
+ }) => void): void;
29
+ removeEventListener(type: "message", listener: (event: {
30
+ data: unknown;
31
+ }) => void): void;
32
+ close(): void;
33
+ }
34
+ /** A started worker. Its "error" event is an uncaught error inside it. */
35
+ export interface worker extends endpoint {
36
+ terminate(): void;
37
+ addEventListener(type: "message" | "error", listener: (event: {
38
+ data: unknown;
39
+ }) => void): void;
40
+ }
41
+ /** What a shared connection needs from the page: Web Locks, BroadcastChannel, a way to start the worker, tab ids. */
42
+ export interface environment {
43
+ locks: locks;
44
+ channel(name: string): broadcast;
45
+ spawn(): worker;
46
+ id(): string;
47
+ }
48
+ /** Called with the new value whenever this tab becomes, or stops being, the leader. */
49
+ export type watcher = (leader: boolean) => void;
50
+ /** Whether running `request` twice is harmless. */
51
+ export declare function reads(request: request): boolean;
52
+ /**
53
+ * The page's Web Locks, BroadcastChannel, Worker and crypto, read once when a shared connection starts.
54
+ * @param target the worker script, started again by each tab that becomes the leader
55
+ */
56
+ export declare function ambient(target: string | URL): environment;
57
+ /** The stand-in for the worker in a shared connection. */
58
+ export declare class coordinator {
59
+ #private;
60
+ constructor(target: unknown, env: environment);
61
+ /** Whether this tab is the leader. */
62
+ get leader(): boolean;
63
+ /** Calls `listener` when this tab becomes, or stops being, the leader; returns the function that stops it. */
64
+ readonly watch: (listener: watcher) => (() => void);
65
+ addEventListener(type: string, listener: (event: never) => void): void;
66
+ removeEventListener(type: string, listener: (event: never) => void): void;
67
+ /** A request from the client's link. */
68
+ postMessage(message: unknown): void;
69
+ }