@noego/sqlego 0.0.3 → 0.1.2-dev.1
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/README.md +81 -20
- package/bin/sqlego +2 -2
- package/bin/sqlego.cjs +26 -0
- package/dist/client.d.ts +65 -8
- package/dist/index.d.ts +2 -2
- package/dist/index.js +288 -44
- package/dist/paths.d.ts +10 -0
- package/dist/protocol.d.ts +46 -1
- package/package.json +10 -3
- package/src/cli.ts +7 -11
- package/src/client.ts +312 -50
- package/src/index.ts +4 -2
- package/src/paths.ts +20 -2
- package/src/protocol.ts +75 -5
- package/src/server.ts +115 -13
package/README.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# sqlego
|
|
2
2
|
|
|
3
|
-
One owner
|
|
4
|
-
library, a Bun script — passes SQL over a local Unix socket
|
|
5
|
-
|
|
3
|
+
One owner per SQLite file. Every client — the `sqlego` CLI, the Node
|
|
4
|
+
library, a Bun script — passes SQL over a local Unix socket (a named pipe on
|
|
5
|
+
Windows) to the one process holding the file. One owner process serves any
|
|
6
|
+
number of files; it starts on first connect and exits when its last file has
|
|
7
|
+
been idle.
|
|
6
8
|
|
|
7
9
|
Why: SQLite's write lock is per-process. Two processes (an app, a log host, a
|
|
8
10
|
retention job, a debugging CLI) writing the same file contend, time out, and
|
|
@@ -14,9 +16,16 @@ process just asks the owner.
|
|
|
14
16
|
|
|
15
17
|
| Piece | Runtime | Role |
|
|
16
18
|
| --- | --- | --- |
|
|
17
|
-
| `bin/sqlego` | Bun, compiled single binary | CLI **and** server (`sqlego serve <file>` is what clients spawn) |
|
|
19
|
+
| `bin/sqlego[.exe]`, `bin/sqlego-<platform>-<arch>[.exe]` | Bun, compiled single binary | CLI **and** server (`sqlego serve <file>` is what clients spawn) |
|
|
20
|
+
| `bin/sqlego.cjs` | Node | The `sqlego` command npm installs: runs the binary for this platform (npm shims cannot launch a native `.exe` on Windows) |
|
|
18
21
|
| `@noego/sqlego` (`src/client.ts`) | Node or Bun; `node:net` only | Protocol-only client — no SQLite dependency, no native addon |
|
|
19
|
-
| `src/server.ts` | Bun (`bun:sqlite`) | The owner: one `Database
|
|
22
|
+
| `src/server.ts` | Bun (`bun:sqlite`) | The owner: per file one `Database` and one socket; many files per process; idle release |
|
|
23
|
+
|
|
24
|
+
Platforms: macOS (arm64, x64), Linux (x64, arm64), Windows x64. `bun run
|
|
25
|
+
build` compiles the binary for the host; `bun run build:all` compiles one per
|
|
26
|
+
platform into `bin/` (what the publish workflow ships). The Windows build is
|
|
27
|
+
cross-compiled and exercised by CI only as a build; it has not been run on a
|
|
28
|
+
Windows machine yet.
|
|
20
29
|
|
|
21
30
|
## Lifecycle
|
|
22
31
|
|
|
@@ -25,21 +34,56 @@ client: connect(socket) ──ok──▶ hello ──▶ requests
|
|
|
25
34
|
│ refused/absent
|
|
26
35
|
▼
|
|
27
36
|
claim <key>.lock (O_EXCL; stale lock of a dead pid is reclaimed)
|
|
28
|
-
│ won
|
|
29
|
-
▼
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
│ won │ lost
|
|
38
|
+
▼ ▼
|
|
39
|
+
an owner of this build running? ──yes──▶ `adopt <file>` wait for socket to appear
|
|
40
|
+
│ no (or fewer than maxOwners) │ │
|
|
41
|
+
▼ │ │
|
|
42
|
+
claim spawn.lock ▶ spawn `sqlego serve <file>` │ │
|
|
43
|
+
└──────────────────────────▶ connect ◀─────────┴────────────────┘
|
|
44
|
+
|
|
45
|
+
owner: per file: open db ▶ listen ▶ write <key>.json ▶ release <key>.lock ▶ serve
|
|
46
|
+
file's last client leaves ▶ idle timer (default 30 s) ▶ rm socket+meta ▶ close db
|
|
47
|
+
last file released ▶ exit 0
|
|
35
48
|
```
|
|
36
49
|
|
|
37
50
|
Socket, lock, and meta live in `$SQLEGO_RUNTIME_DIR` (default `<tmpdir>/sqlego/`)
|
|
38
51
|
keyed by a hash of the canonical file path — never beside the database, because
|
|
39
|
-
Unix socket paths cap at 104 bytes.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
Unix socket paths cap at 104 bytes. On Windows the socket is the named pipe
|
|
53
|
+
`\\.\pipe\sqlego-<runtime dir hash>-<key>` (lock and meta stay in the directory).
|
|
54
|
+
|
|
55
|
+
A client whose version differs from the owner's asks it to release the file
|
|
56
|
+
and places the file anew, so a rebuilt binary never keeps talking to an old
|
|
57
|
+
owner. The same happens when a client sets `serverPragmas` and the running
|
|
58
|
+
owner opened the file with a different list (`hello` and `status` report the
|
|
59
|
+
owner's pragmas). An owner of another build is never adopted into; its other
|
|
60
|
+
files are retired one by one as their own clients arrive.
|
|
61
|
+
|
|
62
|
+
### Owner processes
|
|
63
|
+
|
|
64
|
+
An owner is a Bun process (tens of MB), so one serves many files. A file
|
|
65
|
+
with no owner is adopted by the running owner (of the client's build, under
|
|
66
|
+
the same runtime directory) that serves the fewest files; a new process is
|
|
67
|
+
spawned only while fewer than `maxOwners` exist (`SQLEGO_MAX_OWNERS` or the
|
|
68
|
+
`maxOwners` client option). Default `1`: one process for every file, which
|
|
69
|
+
is the right shape for an app holding tens of small databases open. `N`
|
|
70
|
+
spreads files over up to N processes — SQL for files sharing a process runs
|
|
71
|
+
on one thread, so a long statement on one file delays the others. `0` means
|
|
72
|
+
one process per file, as before owners could adopt. Nothing is ever refused
|
|
73
|
+
for lack of a process.
|
|
74
|
+
|
|
75
|
+
Each file keeps its own socket, database connection, leased transaction and
|
|
76
|
+
idle timer. `sqlego <file> stop` and the idle timeout release that one file;
|
|
77
|
+
the process exits when its last file is released. Spawning is serialized
|
|
78
|
+
through `spawn.lock` in the runtime directory so 40 files opened at once
|
|
79
|
+
share one process instead of starting 40.
|
|
80
|
+
|
|
81
|
+
### Pragmas
|
|
82
|
+
|
|
83
|
+
An owner applies, in order, `auto_vacuum = INCREMENTAL`, `journal_mode = WAL`,
|
|
84
|
+
`busy_timeout = 5000`, `foreign_keys = ON` (foreign keys are per connection in
|
|
85
|
+
SQLite and off by default; the owner holds the only connection). `--pragma`
|
|
86
|
+
on `serve`, or `serverPragmas` on the client, replaces the whole list.
|
|
43
87
|
|
|
44
88
|
## Protocol
|
|
45
89
|
|
|
@@ -118,7 +162,7 @@ sqlego <file> "DELETE …"
|
|
|
118
162
|
sqlego <file> status | stop
|
|
119
163
|
sqlego <file> watch "SELECT count(*) FROM logs" --every 2s
|
|
120
164
|
sqlego ls
|
|
121
|
-
sqlego serve <file> [--idle-ms 30000] [--tx-lease-ms 10000] [--tx-max-ms 60000] [--tx-queue-ms 30000]
|
|
165
|
+
sqlego serve <file> [--idle-ms 30000] [--tx-lease-ms 10000] [--tx-max-ms 60000] [--tx-queue-ms 30000] [--pragma "<name = value>"]...
|
|
122
166
|
```
|
|
123
167
|
|
|
124
168
|
## Library
|
|
@@ -154,13 +198,30 @@ A `SqlegoClientError` with code `tx-expired` means the server rolled the
|
|
|
154
198
|
transaction back (lease, hard cap, or disconnect); `tx-busy` means another
|
|
155
199
|
client's transaction kept the request waiting too long.
|
|
156
200
|
|
|
157
|
-
Server location: `SQLEGO_BIN`, else the compiled
|
|
158
|
-
|
|
201
|
+
Server location: `SQLEGO_BIN`, else the compiled binary for this platform in
|
|
202
|
+
`bin/` next to the package (`sqlego[.exe]`, then `sqlego-<platform>-<arch>[.exe]`),
|
|
203
|
+
else `sqlego` on `PATH`; pass `serverCommand` to override.
|
|
204
|
+
|
|
205
|
+
Other `connect` options: `serverPragmas` (the owner must have these pragmas,
|
|
206
|
+
see above), `maxOwners` (owner processes, see above), `idleTimeoutMs`, `connectTimeoutMs`,
|
|
207
|
+
`requestTimeoutMs`, `spawn: false` (fail with `not-running` instead of spawning).
|
|
159
208
|
|
|
160
209
|
## Develop
|
|
161
210
|
|
|
162
211
|
```
|
|
163
212
|
bun install
|
|
164
213
|
bun test
|
|
165
|
-
bun run build
|
|
214
|
+
bun run build # → dist/ and bin/sqlego (host executable)
|
|
215
|
+
bun run build:bin:all # → bin/sqlego-{darwin,linux}-{arm64,x64}, bin/sqlego-win32-x64.exe
|
|
166
216
|
```
|
|
217
|
+
|
|
218
|
+
`bun run build` runs the fresh binary with `--version` and fails unless it
|
|
219
|
+
reports `<package.json version>+<build id>`, so a stale `bin/sqlego` cannot
|
|
220
|
+
be mistaken for the current one.
|
|
221
|
+
|
|
222
|
+
### Versions
|
|
223
|
+
|
|
224
|
+
`package.json` is the only version source: the publish workflow publishes it
|
|
225
|
+
to npm and tags the commit `v<version>`. The stray tag `v0.1.1` predates npm
|
|
226
|
+
(the first publish reset to 0.0.1); 0.1.2 moves past it so git tags and npm
|
|
227
|
+
versions order the same way from here on.
|
package/bin/sqlego
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
[diffend] Oversized file quarantined before diffing.
|
|
2
2
|
name: package/bin/sqlego
|
|
3
|
-
size:
|
|
4
|
-
sha256:
|
|
3
|
+
size: 70911986 bytes
|
|
4
|
+
sha256: f1045dc0853dc228db9081d881fe1d492121f91238f683a2ab8a90d1c43f3091
|
package/bin/sqlego.cjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
/**
|
|
4
|
+
* The `sqlego` command npm installs. It runs the compiled server binary for
|
|
5
|
+
* this platform from the same directory: `sqlego` / `sqlego.exe` (the host
|
|
6
|
+
* build) or `sqlego-<platform>-<arch>[.exe]` (a `build:bin:all` build). npm's
|
|
7
|
+
* command shims cannot launch a native executable directly on Windows, which
|
|
8
|
+
* is why this launcher exists; clients spawn the binary itself, not this.
|
|
9
|
+
*/
|
|
10
|
+
const { spawnSync } = require('node:child_process');
|
|
11
|
+
const { existsSync } = require('node:fs');
|
|
12
|
+
const path = require('node:path');
|
|
13
|
+
|
|
14
|
+
const suffix = process.platform === 'win32' ? '.exe' : '';
|
|
15
|
+
const candidates = [`sqlego${suffix}`, `sqlego-${process.platform}-${process.arch}${suffix}`].map((name) => path.join(__dirname, name));
|
|
16
|
+
const binary = candidates.find((candidate) => existsSync(candidate));
|
|
17
|
+
if (!binary) {
|
|
18
|
+
process.stderr.write(`sqlego: no server binary for ${process.platform}-${process.arch} in ${__dirname} (looked for ${candidates.map((c) => path.basename(c)).join(', ')}); run \`bun run build:bin\`\n`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
const result = spawnSync(binary, process.argv.slice(2), { stdio: 'inherit', windowsHide: true });
|
|
22
|
+
if (result.error) {
|
|
23
|
+
process.stderr.write(`sqlego: cannot run ${binary}: ${result.error.message}\n`);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
process.exit(result.status === null ? 1 : result.status);
|
package/dist/client.d.ts
CHANGED
|
@@ -2,10 +2,12 @@ import { type BatchResult, type BeginResult, type ExecResult, type HelloResult,
|
|
|
2
2
|
import { type FilePaths } from './paths.ts';
|
|
3
3
|
/**
|
|
4
4
|
* Runs in Node and Bun alike (node:net, node:child_process only; no SQLite
|
|
5
|
-
* dependency). Connects to the file's owner,
|
|
6
|
-
* listening. The sequence is always: connect → else
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* dependency). Connects to the file's owner, placing the file with an owner
|
|
6
|
+
* when none is listening for it. The sequence is always: connect → else
|
|
7
|
+
* claim the file's lock → ask a running owner process to adopt the file, or
|
|
8
|
+
* spawn one when there is none (or fewer than `maxOwners`) → wait for the
|
|
9
|
+
* socket → connect. A claimer that loses the lock waits for the winner's
|
|
10
|
+
* socket instead.
|
|
9
11
|
*/
|
|
10
12
|
export type ClientOptions = Readonly<{
|
|
11
13
|
/** Spawn a server when none is listening. Default true. */
|
|
@@ -14,13 +16,31 @@ export type ClientOptions = Readonly<{
|
|
|
14
16
|
serverCommand?: readonly string[];
|
|
15
17
|
/** Idle timeout passed to a spawned server. Default 30 000. */
|
|
16
18
|
idleTimeoutMs?: number;
|
|
17
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Pragmas the owner must have applied on open (`--pragma`), replacing its
|
|
21
|
+
* defaults. A spawned owner gets them; an owner already running with other
|
|
22
|
+
* pragmas is asked to stop and a fresh one is spawned with these, the way
|
|
23
|
+
* an owner from another build is retired. Two clients insisting on
|
|
24
|
+
* different pragmas for one file would keep restarting its owner: agree on
|
|
25
|
+
* one list per file, or leave this unset to accept whatever is running.
|
|
26
|
+
*/
|
|
18
27
|
serverPragmas?: readonly string[];
|
|
28
|
+
/**
|
|
29
|
+
* How many owner processes (of this client's build, under this runtime
|
|
30
|
+
* directory) may exist. A file with no owner is adopted by the running
|
|
31
|
+
* owner serving the fewest files; a new process is spawned only while
|
|
32
|
+
* there are fewer than this many. 1 (the default) means one process serves
|
|
33
|
+
* every file; N spreads files over up to N processes (SQL for files sharing
|
|
34
|
+
* a process runs on one thread); 0 means one process per file, as before
|
|
35
|
+
* owners could adopt. Default `SQLEGO_MAX_OWNERS`, else 1. Never refuses.
|
|
36
|
+
*/
|
|
37
|
+
maxOwners?: number;
|
|
19
38
|
/** Total time to obtain a connection, including spawn. Default 10 000. */
|
|
20
39
|
connectTimeoutMs?: number;
|
|
21
40
|
/** Per-request timeout. Default 60 000. */
|
|
22
41
|
requestTimeoutMs?: number;
|
|
23
42
|
}>;
|
|
43
|
+
export declare const DEFAULT_MAX_OWNERS = 1;
|
|
24
44
|
export type BeginOptions = Readonly<{
|
|
25
45
|
/** `immediate` takes the write lock at `begin`; `deferred` (default) on the first write. */
|
|
26
46
|
mode?: TransactionMode;
|
|
@@ -43,6 +63,8 @@ export declare class SqlegoClient {
|
|
|
43
63
|
private hello;
|
|
44
64
|
/** Why the server this client spawned did not come up, if it did not. */
|
|
45
65
|
private spawnFailure;
|
|
66
|
+
/** True while this client is the one spawning an owner process (see `placeFile`). */
|
|
67
|
+
private holdsSpawnLock;
|
|
46
68
|
private readonly options;
|
|
47
69
|
private constructor();
|
|
48
70
|
static connect(file: string, options?: ClientOptions): Promise<SqlegoClient>;
|
|
@@ -91,12 +113,41 @@ export declare class SqlegoClient {
|
|
|
91
113
|
close(): Promise<void>;
|
|
92
114
|
private stopServer;
|
|
93
115
|
private open;
|
|
116
|
+
/**
|
|
117
|
+
* Whether the running owner must be retired for this client: it was built
|
|
118
|
+
* from other source than this client, or it opened the file with other
|
|
119
|
+
* pragmas than this client requires (`serverPragmas` unset accepts any).
|
|
120
|
+
*/
|
|
121
|
+
private mustReplace;
|
|
94
122
|
private connectOrSpawn;
|
|
95
|
-
/**
|
|
123
|
+
/** The file's own claim lock: whoever holds it places the file with an owner. */
|
|
96
124
|
private claimLock;
|
|
125
|
+
private ownerBudget;
|
|
126
|
+
/** The runtime directory's spawn lock: at most one client spawns an owner process at a time. */
|
|
127
|
+
private spawnLockPath;
|
|
128
|
+
/**
|
|
129
|
+
* Gets this client's file served, holding the file's claim lock: by the
|
|
130
|
+
* running owner process (of this build) serving the fewest files, or by a
|
|
131
|
+
* new process while fewer than `maxOwners` are running. Spawning is
|
|
132
|
+
* serialized through the spawn lock so many files opened at once share
|
|
133
|
+
* one process instead of each starting their own; a client that loses
|
|
134
|
+
* that lock waits for the new owner to appear and adopts into it.
|
|
135
|
+
*/
|
|
136
|
+
private placeFile;
|
|
137
|
+
/**
|
|
138
|
+
* Owner processes of this build under this runtime directory, fewest files
|
|
139
|
+
* first, from the meta files their files wrote. An owner of another build
|
|
140
|
+
* is never adopted into (its files are retired one by one as their clients
|
|
141
|
+
* arrive); a dead one's metas are ignored.
|
|
142
|
+
*/
|
|
143
|
+
private currentOwners;
|
|
144
|
+
private releaseSpawnLock;
|
|
145
|
+
/** Waits until nothing answers on the file's socket (the old owner released it), or the deadline. */
|
|
146
|
+
private waitForRelease;
|
|
97
147
|
private spawnServer;
|
|
98
148
|
/** The spawn never started: record why, and release the lock so another claimer can try. */
|
|
99
149
|
private onSpawnError;
|
|
150
|
+
private releaseLock;
|
|
100
151
|
private attach;
|
|
101
152
|
private onClose;
|
|
102
153
|
private onData;
|
|
@@ -108,7 +159,6 @@ export declare class SqlegoClient {
|
|
|
108
159
|
/** Never rejects: every outcome, including transport failures, is a `Result`. The reply is `unknown` until parsed. */
|
|
109
160
|
private request;
|
|
110
161
|
private settleAll;
|
|
111
|
-
private waitForExit;
|
|
112
162
|
}
|
|
113
163
|
/**
|
|
114
164
|
* One open transaction on a `SqlegoClient`. Every method rejects with a
|
|
@@ -140,9 +190,16 @@ export declare class SqlegoTransaction {
|
|
|
140
190
|
commit(): Promise<void>;
|
|
141
191
|
rollback(): Promise<void>;
|
|
142
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* File names the compiled server binary may have in `bin/`, most specific
|
|
195
|
+
* last: the host build (`sqlego`, `sqlego.exe`) and the per-platform build
|
|
196
|
+
* (`sqlego-<platform>-<arch>[.exe]`, from `bun run build:bin:all`).
|
|
197
|
+
*/
|
|
198
|
+
export declare function platformBinaryNames(platform?: NodeJS.Platform, arch?: string): readonly string[];
|
|
143
199
|
/**
|
|
144
200
|
* How to start a server: SQLEGO_BIN if set; the compiled `sqlego` binary when
|
|
145
201
|
* running as one; the dev entry (`bun src/cli.ts`) when running from source
|
|
146
|
-
* under Bun;
|
|
202
|
+
* under Bun; the binary for this platform in the package's `bin/`; else
|
|
203
|
+
* `sqlego` on PATH.
|
|
147
204
|
*/
|
|
148
205
|
export declare function defaultServerCommand(): readonly string[];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { SqlegoClient, SqlegoClientError, SqlegoTransaction, defaultServerCommand, type BeginOptions, type ClientOptions } from './client.ts';
|
|
2
|
-
export { pathsFor, runtimeDirectory, canonicalFile, type FilePaths, type ServerMeta } from './paths.ts';
|
|
1
|
+
export { DEFAULT_MAX_OWNERS, SqlegoClient, SqlegoClientError, SqlegoTransaction, defaultServerCommand, platformBinaryNames, type BeginOptions, type ClientOptions, } from './client.ts';
|
|
2
|
+
export { USES_NAMED_PIPES, isNamedPipe, pathsFor, runtimeDirectory, canonicalFile, type FilePaths, type ServerMeta } from './paths.ts';
|
|
3
3
|
export { PROTOCOL_VERSION, type SqlParams, type SqlRow, type SqlValue, type Statement, type RunResult, type QueryResult, type BatchResult, type StatementResult, type ExecResult, type BeginResult, type StatusResult, type HelloResult, type TransactionMode, type TransactionStatus, } from './protocol.ts';
|
|
4
4
|
export { VERSION } from './version.ts';
|