@noego/sqlego 0.1.6-dev.7 → 0.1.6-dev.9
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 +67 -10
- package/dist/client.d.ts +57 -9
- package/dist/index.js +487 -85
- package/dist/lease.d.ts +39 -0
- package/dist/legacy_lock.d.ts +20 -0
- package/dist/paths.d.ts +6 -0
- package/dist/protocol.d.ts +28 -2
- package/dist/version.d.ts +17 -3
- package/package.json +8 -7
- package/src/cli.ts +121 -2
- package/src/client.ts +188 -85
- package/src/lease.ts +265 -0
- package/src/legacy_lock.ts +123 -0
- package/src/paths.ts +4 -0
- package/src/protocol.ts +56 -6
- package/src/server.ts +355 -30
- package/src/version.ts +75 -7
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The claim lock of builds before leases (main 1ec410c and earlier):
|
|
3
|
+
* `<key>.lock`, created exclusively, holding the claimer's pid in decimal.
|
|
4
|
+
* Such a build treats it as held while that pid lives and reclaims (removes)
|
|
5
|
+
* it when the pid is dead or unrecorded. It cannot see claim leases, so a
|
|
6
|
+
* process holding a file's claim lease also holds this lock, naming itself:
|
|
7
|
+
* an older client then finds the file claimed and waits for its socket
|
|
8
|
+
* instead of placing it with a second owner. Only the lease holder writes it
|
|
9
|
+
* (the lease serializes processes of this build), and it is always written
|
|
10
|
+
* whole — linked or renamed into place — so a reader never sees it empty.
|
|
11
|
+
*/
|
|
12
|
+
import { linkSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import { Result, Trust } from '@noego/trust';
|
|
14
|
+
import { isProcessAlive } from './paths.ts';
|
|
15
|
+
import { errnoCode, errorMessage } from './protocol.ts';
|
|
16
|
+
|
|
17
|
+
type LegacyLockFailure = { readonly kind: 'legacy-lock-failed'; readonly message: string };
|
|
18
|
+
|
|
19
|
+
const legacyFailure = (cause: unknown): LegacyLockFailure => ({ kind: 'legacy-lock-failed', message: errorMessage(cause) });
|
|
20
|
+
|
|
21
|
+
let staged = 0;
|
|
22
|
+
|
|
23
|
+
/** A file beside `lock` holding `pid`, ready to be linked or renamed into place. */
|
|
24
|
+
function stage(lock: string, pid: number) {
|
|
25
|
+
staged += 1;
|
|
26
|
+
const file = `${lock}.${process.pid}.${staged}.tmp`;
|
|
27
|
+
return Trust.try(() => {
|
|
28
|
+
writeFileSync(file, String(pid));
|
|
29
|
+
return file;
|
|
30
|
+
}, legacyFailure);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function unstage(file: string): void {
|
|
34
|
+
const removed = Trust.try(() => rmSync(file, { force: true }), legacyFailure);
|
|
35
|
+
if (!removed.ok && removed.error.kind === 'legacy-lock-failed') {
|
|
36
|
+
// Never read as a lock: its name is not `<key>.lock`.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The pid `lock` names; 0 when it names none (empty or not a number, as older builds read it); null when there is no lock. */
|
|
41
|
+
function holderOf(lock: string): Result<number | null, LegacyLockFailure> {
|
|
42
|
+
const text = Trust.try(
|
|
43
|
+
() => readFileSync(lock, 'utf8'),
|
|
44
|
+
(cause) => (errnoCode(cause) === 'ENOENT' ? { kind: 'absent' as const } : legacyFailure(cause)),
|
|
45
|
+
);
|
|
46
|
+
if (!text.ok) {
|
|
47
|
+
const failure = text.error;
|
|
48
|
+
if (failure.kind === 'absent') return Result.ok(null);
|
|
49
|
+
return Result.err(failure);
|
|
50
|
+
}
|
|
51
|
+
const pid = Number(text.value.trim());
|
|
52
|
+
return Result.ok(Number.isInteger(pid) && pid > 0 ? pid : 0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Creates `lock` naming `pid`; false when it exists. */
|
|
56
|
+
function create(lock: string, pid: number): Result<boolean, LegacyLockFailure> {
|
|
57
|
+
const file = stage(lock, pid);
|
|
58
|
+
if (!file.ok) return file;
|
|
59
|
+
const linked = Trust.try(
|
|
60
|
+
() => linkSync(file.value, lock),
|
|
61
|
+
(cause) => (errnoCode(cause) === 'EEXIST' ? { kind: 'exists' as const } : legacyFailure(cause)),
|
|
62
|
+
);
|
|
63
|
+
unstage(file.value);
|
|
64
|
+
if (linked.ok) return Result.ok(true);
|
|
65
|
+
const failure = linked.error;
|
|
66
|
+
if (failure.kind === 'exists') return Result.ok(false);
|
|
67
|
+
return Result.err(failure);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Replaces `lock` with one naming `pid`, in one step. */
|
|
71
|
+
function replace(lock: string, pid: number): Result<boolean, LegacyLockFailure> {
|
|
72
|
+
const file = stage(lock, pid);
|
|
73
|
+
if (!file.ok) return file;
|
|
74
|
+
const renamed = Trust.try(() => renameSync(file.value, lock), legacyFailure);
|
|
75
|
+
if (!renamed.ok) {
|
|
76
|
+
unstage(file.value);
|
|
77
|
+
return renamed;
|
|
78
|
+
}
|
|
79
|
+
return Result.ok(true);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Takes `lock` for `pid`, as an older build would: created if absent,
|
|
84
|
+
* reclaimed if its holder is dead or unrecorded. False when a live process
|
|
85
|
+
* other than `pid` holds it — an older build's client placing the file.
|
|
86
|
+
*/
|
|
87
|
+
export function takeLegacyLock(lock: string, pid: number): Result<boolean, LegacyLockFailure> {
|
|
88
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
89
|
+
const created = create(lock, pid);
|
|
90
|
+
if (!created.ok || created.value) return created;
|
|
91
|
+
const holder = holderOf(lock);
|
|
92
|
+
if (!holder.ok) return holder;
|
|
93
|
+
if (holder.value === pid) return Result.ok(true);
|
|
94
|
+
if (holder.value !== null && holder.value > 0 && isProcessAlive(holder.value)) return Result.ok(false);
|
|
95
|
+
// A dead or unrecorded holder (or the lock just went): reclaim it as older builds do, and try again.
|
|
96
|
+
const removed = Trust.try(() => rmSync(lock, { force: true }), legacyFailure);
|
|
97
|
+
if (!removed.ok) return removed;
|
|
98
|
+
}
|
|
99
|
+
return Result.ok(false);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Moves `lock` from `from` (the process the claim lease was handed over by)
|
|
104
|
+
* to `to`, keeping the file claimed throughout. False when a live process
|
|
105
|
+
* other than these two holds it.
|
|
106
|
+
*/
|
|
107
|
+
export function passLegacyLock(lock: string, from: number | null, to: number): Result<boolean, LegacyLockFailure> {
|
|
108
|
+
const holder = holderOf(lock);
|
|
109
|
+
if (!holder.ok) return holder;
|
|
110
|
+
if (holder.value === to) return Result.ok(true);
|
|
111
|
+
if (holder.value !== null && holder.value === from) return replace(lock, to);
|
|
112
|
+
return takeLegacyLock(lock, to);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Removes `lock` if it names one of `pids`; a lock another process holds is left alone. */
|
|
116
|
+
export function dropLegacyLock(lock: string, pids: readonly number[]): Result<boolean, LegacyLockFailure> {
|
|
117
|
+
const holder = holderOf(lock);
|
|
118
|
+
if (!holder.ok) return holder;
|
|
119
|
+
if (holder.value === null || !pids.includes(holder.value)) return Result.ok(false);
|
|
120
|
+
const removed = Trust.try(() => rmSync(lock, { force: true }), legacyFailure);
|
|
121
|
+
if (!removed.ok) return removed;
|
|
122
|
+
return Result.ok(true);
|
|
123
|
+
}
|
package/src/paths.ts
CHANGED
|
@@ -52,7 +52,10 @@ export type FilePaths = Readonly<{
|
|
|
52
52
|
key: string;
|
|
53
53
|
/** Unix domain socket path, or a `\\.\pipe\` named pipe on Windows (see `isNamedPipe`). */
|
|
54
54
|
socket: string;
|
|
55
|
+
/** The claim lock older builds take (a pid in one file); still honoured while its process lives. */
|
|
55
56
|
lock: string;
|
|
57
|
+
/** The file's claim as a lease (`lease.ts`): held by the client placing the file, then by the owner serving it. */
|
|
58
|
+
lease: string;
|
|
56
59
|
meta: string;
|
|
57
60
|
}>;
|
|
58
61
|
|
|
@@ -104,6 +107,7 @@ export function resolvePaths(file: string) {
|
|
|
104
107
|
key,
|
|
105
108
|
socket,
|
|
106
109
|
lock: path.join(directory.value, `${key}.lock`),
|
|
110
|
+
lease: path.join(directory.value, `${key}.lease`),
|
|
107
111
|
meta: path.join(directory.value, `${key}.json`),
|
|
108
112
|
});
|
|
109
113
|
return Result.ok(paths);
|
package/src/protocol.ts
CHANGED
|
@@ -73,10 +73,12 @@ export type Request =
|
|
|
73
73
|
| { id: number; op: 'stop' }
|
|
74
74
|
/**
|
|
75
75
|
* Asks this owner process to serve one more file: open it with `pragmas`
|
|
76
|
-
* (its defaults when absent)
|
|
77
|
-
*
|
|
76
|
+
* (its defaults when absent) and listen on that file's own socket. `claim`
|
|
77
|
+
* is the file's claim lease the asking client holds (`<generation>:<token>`);
|
|
78
|
+
* the owner takes it over before opening the file and refuses if it cannot
|
|
79
|
+
* (`lease.ts`). The reply names the socket to connect to.
|
|
78
80
|
*/
|
|
79
|
-
| { id: number; op: 'adopt'; file: string; pragmas?: readonly string[]; idleTimeoutMs?: number };
|
|
81
|
+
| { id: number; op: 'adopt'; file: string; pragmas?: readonly string[]; idleTimeoutMs?: number; claim?: string };
|
|
80
82
|
|
|
81
83
|
export type RunResult = Readonly<{ changes: number; lastInsertRowid: number | bigint }>;
|
|
82
84
|
export type QueryResult = Readonly<{ rows: SqlRow[] }>;
|
|
@@ -639,7 +641,9 @@ export function parseRequest(input: unknown) {
|
|
|
639
641
|
}
|
|
640
642
|
const idleTimeoutMs = fields.idleTimeoutMs;
|
|
641
643
|
if (idleTimeoutMs !== undefined && (typeof idleTimeoutMs !== 'number' || !(idleTimeoutMs > 0))) return reject(id, 'idleTimeoutMs must be a positive number');
|
|
642
|
-
const
|
|
644
|
+
const claim = fields.claim;
|
|
645
|
+
if (claim !== undefined && (typeof claim !== 'string' || claim.length === 0)) return reject(id, 'adopt claim must be a non-empty string');
|
|
646
|
+
const request: Request = { id, op: 'adopt', file: fields.file, pragmas: pragmas === undefined ? undefined : [...pragmas], idleTimeoutMs, claim };
|
|
643
647
|
return Result.ok(request);
|
|
644
648
|
}
|
|
645
649
|
default:
|
|
@@ -1126,6 +1130,13 @@ export function splitFrames(buffer: string): { frames: string[]; rest: string }
|
|
|
1126
1130
|
return { frames: lines.filter((line) => line.length > 0), rest };
|
|
1127
1131
|
}
|
|
1128
1132
|
|
|
1133
|
+
/**
|
|
1134
|
+
* The longest request frame the server reads, in decoded characters (the
|
|
1135
|
+
* newline not counted). A longer one is never complete in memory: the
|
|
1136
|
+
* server drops the connection once an unfinished frame passes it.
|
|
1137
|
+
*/
|
|
1138
|
+
export const DEFAULT_MAX_FRAME_LENGTH = 64 * 1024 * 1024;
|
|
1139
|
+
|
|
1129
1140
|
/**
|
|
1130
1141
|
* The frames of one socket, read chunk by chunk in work proportional to what
|
|
1131
1142
|
* arrives, however finely it is chunked: only the new chunk is searched for
|
|
@@ -1137,38 +1148,67 @@ export function splitFrames(buffer: string): { frames: string[]; rest: string }
|
|
|
1137
1148
|
*/
|
|
1138
1149
|
export class FrameReader {
|
|
1139
1150
|
private pieces: string[] = [];
|
|
1151
|
+
/** Characters held in `pieces`: the length of the unfinished frame so far. */
|
|
1152
|
+
private held = 0;
|
|
1153
|
+
private overflowed = false;
|
|
1140
1154
|
private decoder: TextDecoder | null = null;
|
|
1141
1155
|
private searched = 0;
|
|
1142
1156
|
|
|
1157
|
+
/**
|
|
1158
|
+
* `maxFrameLength` bounds one frame in characters (its newline not
|
|
1159
|
+
* counted); unbounded by default, as a client reads replies of any size.
|
|
1160
|
+
*/
|
|
1161
|
+
constructor(private readonly maxFrameLength = Number.POSITIVE_INFINITY) {}
|
|
1162
|
+
|
|
1143
1163
|
/** Characters searched for a newline so far: each one pushed is searched once. */
|
|
1144
1164
|
get scannedCharacters(): number {
|
|
1145
1165
|
return this.searched;
|
|
1146
1166
|
}
|
|
1147
1167
|
|
|
1168
|
+
/**
|
|
1169
|
+
* Whether a frame passed `maxFrameLength`. Its pieces were dropped and
|
|
1170
|
+
* nothing more is read until `reset`: the stream has no trustworthy next
|
|
1171
|
+
* frame boundary.
|
|
1172
|
+
*/
|
|
1173
|
+
get oversized(): boolean {
|
|
1174
|
+
return this.overflowed;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1148
1177
|
/** The frames `chunk` completes, in order; its unterminated tail waits for the next chunk. */
|
|
1149
1178
|
push(chunk: string): string[] {
|
|
1179
|
+
if (this.overflowed) return [];
|
|
1150
1180
|
let newline = chunk.indexOf('\n');
|
|
1151
1181
|
if (newline < 0) {
|
|
1152
1182
|
this.searched += chunk.length;
|
|
1153
|
-
if (chunk.length > 0)
|
|
1183
|
+
if (chunk.length > 0) {
|
|
1184
|
+
this.held += chunk.length;
|
|
1185
|
+
if (this.held > this.maxFrameLength) return this.overflow([]);
|
|
1186
|
+
this.pieces.push(chunk);
|
|
1187
|
+
}
|
|
1154
1188
|
return [];
|
|
1155
1189
|
}
|
|
1156
1190
|
const frames: string[] = [];
|
|
1157
1191
|
let start = 0;
|
|
1158
1192
|
while (newline >= 0) {
|
|
1159
1193
|
this.searched += newline + 1 - start;
|
|
1194
|
+
if (this.held + (newline - start) > this.maxFrameLength) return this.overflow(frames);
|
|
1160
1195
|
let frame = chunk.slice(start, newline);
|
|
1161
1196
|
if (this.pieces.length > 0) {
|
|
1162
1197
|
this.pieces.push(frame);
|
|
1163
1198
|
frame = this.pieces.join('');
|
|
1164
1199
|
this.pieces = [];
|
|
1165
1200
|
}
|
|
1201
|
+
this.held = 0;
|
|
1166
1202
|
if (frame.length > 0) frames.push(frame);
|
|
1167
1203
|
start = newline + 1;
|
|
1168
1204
|
newline = chunk.indexOf('\n', start);
|
|
1169
1205
|
}
|
|
1170
1206
|
this.searched += chunk.length - start;
|
|
1171
|
-
if (start < chunk.length)
|
|
1207
|
+
if (start < chunk.length) {
|
|
1208
|
+
this.held = chunk.length - start;
|
|
1209
|
+
if (this.held > this.maxFrameLength) return this.overflow(frames);
|
|
1210
|
+
this.pieces.push(chunk.slice(start));
|
|
1211
|
+
}
|
|
1172
1212
|
return frames;
|
|
1173
1213
|
}
|
|
1174
1214
|
|
|
@@ -1186,8 +1226,18 @@ export class FrameReader {
|
|
|
1186
1226
|
/** Drops an unfinished frame (and a partly received character): the stream is starting over or was given up. */
|
|
1187
1227
|
reset(): void {
|
|
1188
1228
|
this.pieces = [];
|
|
1229
|
+
this.held = 0;
|
|
1230
|
+
this.overflowed = false;
|
|
1189
1231
|
this.decoder = null;
|
|
1190
1232
|
}
|
|
1233
|
+
|
|
1234
|
+
/** Gives up the unfinished frame; `frames` are the ones completed before it. */
|
|
1235
|
+
private overflow(frames: string[]): string[] {
|
|
1236
|
+
this.pieces = [];
|
|
1237
|
+
this.held = 0;
|
|
1238
|
+
this.overflowed = true;
|
|
1239
|
+
return frames;
|
|
1240
|
+
}
|
|
1191
1241
|
}
|
|
1192
1242
|
|
|
1193
1243
|
function replaceBigInt(_key: string, value: unknown): unknown {
|