@tonbo/sqlite-ursula 0.0.0-stage → 0.6.2
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.
- package/LICENSE +201 -0
- package/README.md +62 -2
- package/dist/index.d.ts +99 -0
- package/dist/index.js +78 -0
- package/dist/platform.d.ts +14 -0
- package/dist/platform.js +42 -0
- package/package.json +39 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 The Ursula contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tonbo/sqlite-ursula
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
SQLite databases replicated through [Ursula](https://ursula.tonbo.io) streams, for Node.js.
|
|
4
|
+
|
|
5
|
+
The package loads the `sqlite-ursula-vfs` SQLite extension into `node:sqlite`. Once a file is attached to a stream, every commit is appended to the stream before it reaches the local file, and any host can rebuild the database from the stream. Your code keeps using SQLite as before. `openUrsulaPiStorage` opens Pi Durable's official `SqliteStorage` on a replicated file.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @tonbo/sqlite-ursula
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
It needs Node.js 22.19 or later. The extension comes prebuilt: npm installs the build for your platform, `@tonbo/sqlite-ursula-<platform>`, as an optional dependency.
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { openUrsulaPiStorage } from "@tonbo/sqlite-ursula";
|
|
19
|
+
|
|
20
|
+
const storage = await openUrsulaPiStorage("/data/harness-1.db", "https://ursula.example.com/pi/harness-1");
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Hand `storage` to the Pi harness, and close it with `storage.close(ctx)` on shutdown. `openUrsulaPiStorage` attaches the file to the stream, which claims the stream for this process, and then opens the file with Pi's node driver.
|
|
24
|
+
|
|
25
|
+
With plain `node:sqlite`, attach the file first and then open it as usual:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { DatabaseSync } from "node:sqlite";
|
|
29
|
+
import { attach } from "@tonbo/sqlite-ursula";
|
|
30
|
+
|
|
31
|
+
attach("/data/app.db", "https://ursula.example.com/my-bucket/app-db");
|
|
32
|
+
const db = new DatabaseSync("/data/app.db");
|
|
33
|
+
db.exec("PRAGMA journal_mode=WAL");
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The extension is loaded once per process and becomes SQLite's default VFS there. Files that were never attached are not affected.
|
|
37
|
+
|
|
38
|
+
Read [SQLite on Ursula](https://ursula.tonbo.io/docs/examples/sqlite-vfs) before you rely on it. It covers what the cluster needs, the rules (WAL mode only, one process per file), and what fencing means for your writes.
|
|
39
|
+
|
|
40
|
+
## Platforms
|
|
41
|
+
|
|
42
|
+
The extension is prebuilt for:
|
|
43
|
+
|
|
44
|
+
- Linux x64 and arm64 with glibc 2.28 or later, such as Debian 11, Ubuntu 20.04, Amazon Linux 2023 and RHEL 8.
|
|
45
|
+
- Linux x64 with musl, such as Alpine.
|
|
46
|
+
- macOS 11 or later on Apple silicon.
|
|
47
|
+
|
|
48
|
+
On any other platform, build the extension yourself and set `SQLITE_URSULA_VFS`.
|
|
49
|
+
|
|
50
|
+
## SQLITE_URSULA_VFS
|
|
51
|
+
|
|
52
|
+
When `SQLITE_URSULA_VFS` is set, the package loads the library at that path instead of the prebuilt one. Passing a path to `loadUrsulaVfs(path)` before any other call does the same. To build the extension from source (Rust):
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
git clone https://github.com/tonbo-io/ursula
|
|
56
|
+
cd ursula/clients/sqlite-vfs
|
|
57
|
+
cargo build --release
|
|
58
|
+
export SQLITE_URSULA_VFS=$PWD/target/release/libsqlite_ursula_vfs.so # .dylib on macOS
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { SqliteStorage } from "@earendil-works/pi-durable/storage/sqlite";
|
|
3
|
+
import { type NodeSqliteStorageOptions } from "@earendil-works/pi-durable/storage/sqlite/node";
|
|
4
|
+
/**
|
|
5
|
+
* Loads the extension once per process and returns the control connection. The library is `path`,
|
|
6
|
+
* else SQLITE_URSULA_VFS when set, else the prebuilt one for this platform
|
|
7
|
+
* (`@tonbo/sqlite-ursula-<platform>`, an optional dependency).
|
|
8
|
+
*/
|
|
9
|
+
export declare function loadUrsulaVfs(path?: string | undefined): DatabaseSync;
|
|
10
|
+
/**
|
|
11
|
+
* Catches `file` up from the stream (installing the stream's latest snapshot first when the file is
|
|
12
|
+
* missing or behind it), claims the stream for this process (fencing every earlier owner)
|
|
13
|
+
* and attaches the file. No connection to `file` may be open; the process keeps a host lock on the file
|
|
14
|
+
* for its lifetime. Returns the stream offset the file reflects (a {@link StreamOffset}). A file that has a sidecar (`<file>-ursula`,
|
|
15
|
+
* written by its first attach) opens in this process only while attached here: not before an attach
|
|
16
|
+
* of it succeeds, nor after one fails (unless refused up front for open connections, another thread
|
|
17
|
+
* attaching it, or another process holding it, which leaves the file as it was).
|
|
18
|
+
*/
|
|
19
|
+
export declare function attach(file: string, streamUrl: string): StreamOffset;
|
|
20
|
+
/**
|
|
21
|
+
* A stream offset as the server wrote it (`Stream-Next-Offset`): opaque. Compare two offsets of one
|
|
22
|
+
* stream only as strings (lexicographically, e.g. `a < b`); never parse or do arithmetic on them.
|
|
23
|
+
* `"-1"` is the beginning of the stream, and stands for "none" where an offset may be absent.
|
|
24
|
+
*/
|
|
25
|
+
export type StreamOffset = string;
|
|
26
|
+
export interface AttachStatus {
|
|
27
|
+
/** Stream offset after the last acknowledged commit. */
|
|
28
|
+
readonly offset: StreamOffset;
|
|
29
|
+
/** This owner's producer epoch. */
|
|
30
|
+
readonly epoch: number;
|
|
31
|
+
/** Every later commit fails until the file is re-attached. */
|
|
32
|
+
readonly poisoned: boolean;
|
|
33
|
+
/** Poisoned because a newer owner claimed the stream, the stream was deleted and recreated, or another writer appended with a higher `Stream-Seq`. */
|
|
34
|
+
readonly fenced: boolean;
|
|
35
|
+
readonly reason: string | null;
|
|
36
|
+
/** Offset of the latest snapshot known readable (published and read back, or found at attach); `"-1"` for none. */
|
|
37
|
+
readonly snapshot: StreamOffset;
|
|
38
|
+
/** Retention this owner advanced the stream to (`"-1"`: none yet). */
|
|
39
|
+
readonly retained: StreamOffset;
|
|
40
|
+
/** Stream offset of the local state attach started from; `"-1"` when it rebuilt the file from nothing (a fresh host, or local files it could not trust and discarded: another boot, a replaced file, a sidecar ahead of its WAL, another incarnation of the stream: deleted and recreated). */
|
|
41
|
+
readonly local: StreamOffset;
|
|
42
|
+
/** Offset of the snapshot attach installed (`"-1"`: none). */
|
|
43
|
+
readonly installed: StreamOffset;
|
|
44
|
+
}
|
|
45
|
+
export declare function status(file: string): AttachStatus;
|
|
46
|
+
/** One replicated commit, as measured inside the VFS. */
|
|
47
|
+
export interface VfsCommitStat {
|
|
48
|
+
/** Frame bytes on the stream (compressed). */
|
|
49
|
+
readonly bytes: number;
|
|
50
|
+
/** Uncompressed record bytes. */
|
|
51
|
+
readonly raw: number;
|
|
52
|
+
/** Page images in the record. */
|
|
53
|
+
readonly pages: number;
|
|
54
|
+
/** Append requests (more than one after an unknown outcome). */
|
|
55
|
+
readonly attempts: number;
|
|
56
|
+
/** The append requests alone. */
|
|
57
|
+
readonly append_us: number;
|
|
58
|
+
/** Commit hook: record build + append + local WAL write. */
|
|
59
|
+
readonly vfs_us: number;
|
|
60
|
+
}
|
|
61
|
+
/** One published (and read back) snapshot. */
|
|
62
|
+
export interface VfsSnapshotStat {
|
|
63
|
+
/** Stream offset it reflects. */
|
|
64
|
+
readonly offset: StreamOffset;
|
|
65
|
+
/** Body bytes (compressed). */
|
|
66
|
+
readonly bytes: number;
|
|
67
|
+
/** Database bytes. */
|
|
68
|
+
readonly raw: number;
|
|
69
|
+
/** Page copy under the read transaction. */
|
|
70
|
+
readonly copy_us: number;
|
|
71
|
+
/** Checkpoint, copy, compression, publish and read-back. */
|
|
72
|
+
readonly total_us: number;
|
|
73
|
+
}
|
|
74
|
+
export interface VfsStats {
|
|
75
|
+
readonly commits: VfsCommitStat[];
|
|
76
|
+
/** Checkpoints of the file since the last drain (time holding the checkpoint lock). */
|
|
77
|
+
readonly checkpoints_us: number[];
|
|
78
|
+
readonly snapshots: VfsSnapshotStat[];
|
|
79
|
+
}
|
|
80
|
+
/** Drains the per-commit and per-checkpoint stats of an attached file. */
|
|
81
|
+
export declare function drainStats(file: string): VfsStats;
|
|
82
|
+
/**
|
|
83
|
+
* A commit the VFS could not replicate: fenced (by a newer owner, because the stream was deleted and
|
|
84
|
+
* recreated, or by another writer's append with a higher `Stream-Seq`), rejected, or with no answer in
|
|
85
|
+
* time.
|
|
86
|
+
*/
|
|
87
|
+
export declare class UrsulaReplicationError extends Error {
|
|
88
|
+
readonly fenced: boolean;
|
|
89
|
+
constructor(message: string, fenced: boolean, cause: unknown);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Pi Durable's official node SqliteStorage on a replicated file: attaches `file` to `streamUrl`, then
|
|
93
|
+
* opens it with Pi's node driver, unmodified. A commit the VFS fails surfaces as an
|
|
94
|
+
* `UrsulaReplicationError` (SQLite has already rolled the transaction back, so the driver's own
|
|
95
|
+
* rollback attempt would otherwise turn it into an AggregateError). The storage is unusable for
|
|
96
|
+
* writes after that; re-open it to take the stream over again (after a delete and recreate, re-opening
|
|
97
|
+
* rebuilds the file from the new stream).
|
|
98
|
+
*/
|
|
99
|
+
export declare function openUrsulaPiStorage(file: string, streamUrl: string, options?: NodeSqliteStorageOptions): Promise<SqliteStorage>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// SQLite replicated through an Ursula stream by the sqlite-ursula-vfs loadable extension
|
|
2
|
+
// (clients/sqlite-vfs). The extension registers the "ursula" VFS as SQLite's default; after
|
|
3
|
+
// `attach(path, streamUrl)` every connection that opens `path` (plain node:sqlite, Pi's official node
|
|
4
|
+
// driver) is replicated: each commit is appended to the stream before it reaches the local WAL.
|
|
5
|
+
import { DatabaseSync } from "node:sqlite";
|
|
6
|
+
import { SqliteStorage } from "@earendil-works/pi-durable/storage/sqlite";
|
|
7
|
+
import { openNodeSqliteDatabase } from "@earendil-works/pi-durable/storage/sqlite/node";
|
|
8
|
+
import { prebuiltExtension } from "./platform.js";
|
|
9
|
+
let control;
|
|
10
|
+
/**
|
|
11
|
+
* Loads the extension once per process and returns the control connection. The library is `path`,
|
|
12
|
+
* else SQLITE_URSULA_VFS when set, else the prebuilt one for this platform
|
|
13
|
+
* (`@tonbo/sqlite-ursula-<platform>`, an optional dependency).
|
|
14
|
+
*/
|
|
15
|
+
export function loadUrsulaVfs(path = process.env.SQLITE_URSULA_VFS) {
|
|
16
|
+
if (control !== undefined)
|
|
17
|
+
return control;
|
|
18
|
+
const library = path === undefined || path.length === 0 ? prebuiltExtension() : path;
|
|
19
|
+
const db = new DatabaseSync(":memory:", { allowExtension: true });
|
|
20
|
+
db.loadExtension(library);
|
|
21
|
+
control = db;
|
|
22
|
+
return db;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Catches `file` up from the stream (installing the stream's latest snapshot first when the file is
|
|
26
|
+
* missing or behind it), claims the stream for this process (fencing every earlier owner)
|
|
27
|
+
* and attaches the file. No connection to `file` may be open; the process keeps a host lock on the file
|
|
28
|
+
* for its lifetime. Returns the stream offset the file reflects (a {@link StreamOffset}). A file that has a sidecar (`<file>-ursula`,
|
|
29
|
+
* written by its first attach) opens in this process only while attached here: not before an attach
|
|
30
|
+
* of it succeeds, nor after one fails (unless refused up front for open connections, another thread
|
|
31
|
+
* attaching it, or another process holding it, which leaves the file as it was).
|
|
32
|
+
*/
|
|
33
|
+
export function attach(file, streamUrl) {
|
|
34
|
+
const row = loadUrsulaVfs().prepare("SELECT ursula_attach(?, ?) AS n").get(file, streamUrl);
|
|
35
|
+
return row.n;
|
|
36
|
+
}
|
|
37
|
+
export function status(file) {
|
|
38
|
+
const row = loadUrsulaVfs().prepare("SELECT ursula_status(?) AS s").get(file);
|
|
39
|
+
return JSON.parse(row.s);
|
|
40
|
+
}
|
|
41
|
+
/** Drains the per-commit and per-checkpoint stats of an attached file. */
|
|
42
|
+
export function drainStats(file) {
|
|
43
|
+
const row = loadUrsulaVfs().prepare("SELECT ursula_stats(?) AS s").get(file);
|
|
44
|
+
return JSON.parse(row.s);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A commit the VFS could not replicate: fenced (by a newer owner, because the stream was deleted and
|
|
48
|
+
* recreated, or by another writer's append with a higher `Stream-Seq`), rejected, or with no answer in
|
|
49
|
+
* time.
|
|
50
|
+
*/
|
|
51
|
+
export class UrsulaReplicationError extends Error {
|
|
52
|
+
fenced;
|
|
53
|
+
constructor(message, fenced, cause) {
|
|
54
|
+
super(message, { cause });
|
|
55
|
+
this.name = "UrsulaReplicationError";
|
|
56
|
+
this.fenced = fenced;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Pi Durable's official node SqliteStorage on a replicated file: attaches `file` to `streamUrl`, then
|
|
61
|
+
* opens it with Pi's node driver, unmodified. A commit the VFS fails surfaces as an
|
|
62
|
+
* `UrsulaReplicationError` (SQLite has already rolled the transaction back, so the driver's own
|
|
63
|
+
* rollback attempt would otherwise turn it into an AggregateError). The storage is unusable for
|
|
64
|
+
* writes after that; re-open it to take the stream over again (after a delete and recreate, re-opening
|
|
65
|
+
* rebuilds the file from the new stream).
|
|
66
|
+
*/
|
|
67
|
+
export async function openUrsulaPiStorage(file, streamUrl, options = {}) {
|
|
68
|
+
attach(file, streamUrl);
|
|
69
|
+
const db = await openNodeSqliteDatabase(file, options);
|
|
70
|
+
const transaction = db.transaction.bind(db);
|
|
71
|
+
db.transaction = (callback) => transaction(callback).catch((error) => {
|
|
72
|
+
const s = status(file);
|
|
73
|
+
if (!s.poisoned)
|
|
74
|
+
throw error;
|
|
75
|
+
throw new UrsulaReplicationError(`commit not replicated: ${s.reason ?? "unknown"}`, s.fenced, error instanceof AggregateError ? error.errors[0] : error);
|
|
76
|
+
});
|
|
77
|
+
return SqliteStorage.open(db);
|
|
78
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface Prebuilt {
|
|
2
|
+
/** The package is `@tonbo/sqlite-ursula-<name>`. */
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly os: NodeJS.Platform;
|
|
5
|
+
readonly cpu: NodeJS.Architecture;
|
|
6
|
+
/** Linux only: the C library the extension links against. */
|
|
7
|
+
readonly libc?: "glibc" | "musl";
|
|
8
|
+
/** The library, at the package's root. */
|
|
9
|
+
readonly file: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const PREBUILT: readonly Prebuilt[];
|
|
12
|
+
export declare const packageName: (p: Prebuilt) => string;
|
|
13
|
+
/** The path of this platform's prebuilt extension, from its package. */
|
|
14
|
+
export declare function prebuiltExtension(): string;
|
package/dist/platform.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// The prebuilt extension ships as one npm package per platform, `@tonbo/sqlite-ursula-<name>`, each an
|
|
2
|
+
// optional dependency of @tonbo/sqlite-ursula that npm installs only where its os, cpu and libc
|
|
3
|
+
// match. scripts/pack.mjs builds those packages from PREBUILT.
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
export const PREBUILT = [
|
|
6
|
+
{ name: "linux-x64-gnu", os: "linux", cpu: "x64", libc: "glibc", file: "libsqlite_ursula_vfs.so" },
|
|
7
|
+
{ name: "linux-arm64-gnu", os: "linux", cpu: "arm64", libc: "glibc", file: "libsqlite_ursula_vfs.so" },
|
|
8
|
+
{ name: "linux-x64-musl", os: "linux", cpu: "x64", libc: "musl", file: "libsqlite_ursula_vfs.so" },
|
|
9
|
+
{ name: "darwin-arm64", os: "darwin", cpu: "arm64", file: "libsqlite_ursula_vfs.dylib" },
|
|
10
|
+
];
|
|
11
|
+
export const packageName = (p) => `@tonbo/sqlite-ursula-${p.name}`;
|
|
12
|
+
/** The C library of this (Linux) process: Node's report carries glibc's version, and none on musl. */
|
|
13
|
+
function libc() {
|
|
14
|
+
// Without the network section, which looks up the name of every open socket's peer.
|
|
15
|
+
const report = process.report;
|
|
16
|
+
const excludeNetwork = report.excludeNetwork;
|
|
17
|
+
report.excludeNetwork = true;
|
|
18
|
+
try {
|
|
19
|
+
const { header } = report.getReport();
|
|
20
|
+
return header?.glibcVersionRuntime === undefined ? "musl" : "glibc";
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
report.excludeNetwork = excludeNetwork;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** The path of this platform's prebuilt extension, from its package. */
|
|
27
|
+
export function prebuiltExtension() {
|
|
28
|
+
const { platform, arch } = process;
|
|
29
|
+
const c = platform === "linux" ? libc() : undefined;
|
|
30
|
+
const p = PREBUILT.find((p) => p.os === platform && p.cpu === arch && p.libc === c);
|
|
31
|
+
if (p === undefined) {
|
|
32
|
+
const here = c === undefined ? `${platform}-${arch}` : `${platform}-${arch} (${c})`;
|
|
33
|
+
throw new Error(`@tonbo/sqlite-ursula has no prebuilt extension for ${here}: build clients/sqlite-vfs and set SQLITE_URSULA_VFS to the library`);
|
|
34
|
+
}
|
|
35
|
+
const name = packageName(p);
|
|
36
|
+
try {
|
|
37
|
+
return createRequire(import.meta.url).resolve(`${name}/${p.file}`);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
throw new Error(`${name} is not installed: reinstall @tonbo/sqlite-ursula without omitting optional dependencies, or set SQLITE_URSULA_VFS to the extension`, { cause: error });
|
|
41
|
+
}
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tonbo/sqlite-ursula",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.2",
|
|
4
|
+
"description": "SQLite replicated through an Ursula stream by the sqlite-ursula-vfs extension, with Pi Durable's official SqliteStorage on top",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/tonbo-io/ursula.git",
|
|
9
|
+
"directory": "clients/sqlite-ursula"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://ursula.tonbo.io/docs/examples/sqlite-vfs",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/tonbo-io/ursula/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.19.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@earendil-works/pi-durable": "^1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"optionalDependencies": {
|
|
36
|
+
"@tonbo/sqlite-ursula-linux-x64-gnu": "0.6.2",
|
|
37
|
+
"@tonbo/sqlite-ursula-linux-arm64-gnu": "0.6.2",
|
|
38
|
+
"@tonbo/sqlite-ursula-linux-x64-musl": "0.6.2",
|
|
39
|
+
"@tonbo/sqlite-ursula-darwin-arm64": "0.6.2"
|
|
40
|
+
}
|
|
41
|
+
}
|