@noego/sqlego 0.1.4 → 0.1.6-dev.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.
- package/README.md +49 -5
- package/dist/client.d.ts +3 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +142 -58
- package/dist/protocol.d.ts +36 -0
- package/package.json +6 -6
- package/src/cli.ts +12 -2
- package/src/client.ts +9 -6
- package/src/index.ts +1 -0
- package/src/paths.ts +3 -2
- package/src/protocol.ts +189 -50
- package/src/reader_pool.ts +408 -0
- package/src/reader_worker.ts +189 -0
- package/src/server.ts +422 -180
- package/src/statement_cache.ts +131 -0
package/src/protocol.ts
CHANGED
|
@@ -137,6 +137,30 @@ export type StatusResult = Readonly<{
|
|
|
137
137
|
idleTimeoutMs: number;
|
|
138
138
|
requestsServed: number;
|
|
139
139
|
transaction: TransactionStatus | null;
|
|
140
|
+
/**
|
|
141
|
+
* The owner process's reader-worker pool, as `status` found it: null for a
|
|
142
|
+
* non-WAL file (no reader), absent from an owner from before the pool.
|
|
143
|
+
*/
|
|
144
|
+
readers?: ReaderPoolStatus | null;
|
|
145
|
+
/** Opted-in plain reads of this file so far: on a worker, or on the owner thread's reader. Absent from an older owner. */
|
|
146
|
+
detachedReads?: Readonly<{ pooled: number; inline: number }>;
|
|
147
|
+
}>;
|
|
148
|
+
|
|
149
|
+
/** The reader-worker pool of an owner process (see `reader_pool.ts`); counters are per process. */
|
|
150
|
+
export type ReaderPoolStatus = Readonly<{
|
|
151
|
+
/** Workers at most; 0 means the pool is disabled. */
|
|
152
|
+
size: number;
|
|
153
|
+
/** Reads waiting for a worker at most; past it the owner reads in-thread. */
|
|
154
|
+
queueLimit: number;
|
|
155
|
+
workers: number;
|
|
156
|
+
busy: number;
|
|
157
|
+
queued: number;
|
|
158
|
+
served: number;
|
|
159
|
+
refused: number;
|
|
160
|
+
failed: number;
|
|
161
|
+
restarts: number;
|
|
162
|
+
queueMsMax: number;
|
|
163
|
+
execMsMax: number;
|
|
140
164
|
}>;
|
|
141
165
|
|
|
142
166
|
export type ErrorCode =
|
|
@@ -172,10 +196,16 @@ export type Response =
|
|
|
172
196
|
* or a `toJSON` that throws; both are this one failure.
|
|
173
197
|
*/
|
|
174
198
|
export function toJson(value: unknown, indent?: number) {
|
|
175
|
-
const
|
|
176
|
-
(): unknown => JSON.stringify(value,
|
|
199
|
+
const fast = Trust.try(
|
|
200
|
+
(): unknown => (indent === undefined ? JSON.stringify(value) : JSON.stringify(value, undefined, indent)),
|
|
177
201
|
(cause) => ({ kind: 'encode-failed' as const, message: errorMessage(cause) }),
|
|
178
202
|
);
|
|
203
|
+
const encoded = fast.ok
|
|
204
|
+
? fast
|
|
205
|
+
: Trust.try(
|
|
206
|
+
(): unknown => JSON.stringify(value, replaceBigInt, indent),
|
|
207
|
+
(cause) => ({ kind: 'encode-failed' as const, message: errorMessage(cause) }),
|
|
208
|
+
);
|
|
179
209
|
if (!encoded.ok) return encoded;
|
|
180
210
|
if (typeof encoded.value !== 'string') return Result.err({ kind: 'encode-failed', message: `${typeof value} has no JSON form` });
|
|
181
211
|
return Result.ok(encoded.value);
|
|
@@ -210,20 +240,28 @@ function encodeRequestParams(request: Request) {
|
|
|
210
240
|
if (request.params === undefined) return Result.ok<unknown>(request);
|
|
211
241
|
const params = encodeSqlParams(request.params);
|
|
212
242
|
if (!params.ok) return params;
|
|
243
|
+
if (params.value === request.params) return Result.ok<unknown>(request);
|
|
213
244
|
return Result.ok<unknown>({ ...request, params: params.value });
|
|
214
245
|
}
|
|
215
246
|
case 'batch': {
|
|
216
|
-
|
|
217
|
-
|
|
247
|
+
let changed = false;
|
|
248
|
+
const statements: unknown[] = new Array(request.statements.length);
|
|
249
|
+
for (let index = 0; index < request.statements.length; index += 1) {
|
|
250
|
+
const statement = request.statements[index];
|
|
218
251
|
if (statement.params === undefined) {
|
|
219
|
-
statements
|
|
252
|
+
statements[index] = statement;
|
|
220
253
|
continue;
|
|
221
254
|
}
|
|
222
255
|
const params = encodeSqlParams(statement.params);
|
|
223
256
|
if (!params.ok) return invalid(`statements[${index}].${params.error.message}`);
|
|
224
|
-
|
|
257
|
+
if (params.value === statement.params) {
|
|
258
|
+
statements[index] = statement;
|
|
259
|
+
} else {
|
|
260
|
+
changed = true;
|
|
261
|
+
statements[index] = { ...statement, params: params.value };
|
|
262
|
+
}
|
|
225
263
|
}
|
|
226
|
-
return Result.ok<unknown>({ ...request, statements });
|
|
264
|
+
return Result.ok<unknown>(changed ? { ...request, statements } : request);
|
|
227
265
|
}
|
|
228
266
|
default:
|
|
229
267
|
return Result.ok<unknown>(request);
|
|
@@ -235,21 +273,46 @@ export function encodeSqlParams(params: SqlParams) {
|
|
|
235
273
|
if (typeof params !== 'object' || params === null) return invalid('params must be an array or an object');
|
|
236
274
|
const shape = paramsShape(params);
|
|
237
275
|
if (shape.kind === 'positional') {
|
|
238
|
-
const
|
|
239
|
-
|
|
276
|
+
const items = shape.values;
|
|
277
|
+
let values: unknown[] | null = null;
|
|
278
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
279
|
+
const item = items[index];
|
|
280
|
+
if (item === null || typeof item === 'string' || typeof item === 'boolean' || (typeof item === 'number' && Number.isFinite(item))) {
|
|
281
|
+
if (values !== null) values[index] = item;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
240
284
|
const value = encodeParamValue(item);
|
|
241
285
|
if (!value.ok) return invalid(`params[${index}]: ${value.error.message}`);
|
|
242
|
-
values
|
|
286
|
+
if (values === null) {
|
|
287
|
+
values = new Array(items.length);
|
|
288
|
+
for (let prev = 0; prev < index; prev += 1) values[prev] = items[prev];
|
|
289
|
+
}
|
|
290
|
+
values[index] = value.value;
|
|
243
291
|
}
|
|
244
|
-
return Result.ok<WireParams>(values);
|
|
292
|
+
return Result.ok<WireParams>(values ?? items);
|
|
245
293
|
}
|
|
246
|
-
const
|
|
247
|
-
|
|
294
|
+
const fields = shape.values;
|
|
295
|
+
const keys = Object.keys(fields);
|
|
296
|
+
let entries: Array<[string, unknown]> | null = null;
|
|
297
|
+
for (let index = 0; index < keys.length; index += 1) {
|
|
298
|
+
const name = keys[index];
|
|
299
|
+
const item = fields[name];
|
|
300
|
+
if (item === null || typeof item === 'string' || typeof item === 'boolean' || (typeof item === 'number' && Number.isFinite(item))) {
|
|
301
|
+
if (entries !== null) entries.push([name, item]);
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
248
304
|
const value = encodeParamValue(item);
|
|
249
305
|
if (!value.ok) return invalid(`params.${name}: ${value.error.message}`);
|
|
306
|
+
if (entries === null) {
|
|
307
|
+
entries = [];
|
|
308
|
+
for (let prev = 0; prev < index; prev += 1) {
|
|
309
|
+
const prevKey = keys[prev];
|
|
310
|
+
entries.push([prevKey, fields[prevKey]]);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
250
313
|
entries.push([name, value.value]);
|
|
251
314
|
}
|
|
252
|
-
return Result.ok<WireParams>(Object.fromEntries(entries));
|
|
315
|
+
return Result.ok<WireParams>(entries === null ? fields : Object.fromEntries(entries));
|
|
253
316
|
}
|
|
254
317
|
|
|
255
318
|
/**
|
|
@@ -314,20 +377,31 @@ export function encodeResultValue(value: unknown) {
|
|
|
314
377
|
}
|
|
315
378
|
|
|
316
379
|
/** Rows as SQLite returned them (`unknown`), in wire form. */
|
|
380
|
+
//trust:validator_function
|
|
317
381
|
export function encodeResultRows(input: unknown) {
|
|
318
382
|
if (!Array.isArray(input)) return invalid('rows must be an array');
|
|
319
|
-
const rows: WireRow[] =
|
|
320
|
-
for (
|
|
383
|
+
const rows: WireRow[] = new Array(input.length);
|
|
384
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
385
|
+
const item: unknown = input[index];
|
|
321
386
|
const fields = fieldsOf(item);
|
|
322
387
|
if (!fields) return invalid(`rows[${index}] must be an object`);
|
|
323
|
-
const
|
|
324
|
-
for (const
|
|
388
|
+
const record = item as Record<string, WireValue>;
|
|
389
|
+
for (const column in fields) {
|
|
390
|
+
const raw = fields[column];
|
|
391
|
+
if (raw === null || typeof raw === 'string' || typeof raw === 'boolean') continue;
|
|
392
|
+
if (typeof raw === 'number') {
|
|
393
|
+
if (!Number.isFinite(raw)) return invalid(`rows[${index}].${column}: the REAL ${raw} has no JSON form`);
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
if (typeof raw === 'bigint') {
|
|
397
|
+
record[column] = encodeWireInteger(raw);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
325
400
|
const value = encodeResultValue(raw);
|
|
326
401
|
if (!value.ok) return invalid(`rows[${index}].${column}: ${value.error.message}`);
|
|
327
|
-
|
|
402
|
+
record[column] = value.value;
|
|
328
403
|
}
|
|
329
|
-
|
|
330
|
-
rows.push(row);
|
|
404
|
+
rows[index] = record;
|
|
331
405
|
}
|
|
332
406
|
return Result.ok(rows);
|
|
333
407
|
}
|
|
@@ -398,17 +472,36 @@ function parseWireBigInt(input: unknown) {
|
|
|
398
472
|
/** Canonical padded base64; the empty string is a zero-length blob. */
|
|
399
473
|
//trust:validator_function
|
|
400
474
|
function parseWireBlob(input: unknown) {
|
|
401
|
-
if (typeof input !== 'string' || !BASE64.test(input)
|
|
475
|
+
if (typeof input !== 'string' || !BASE64.test(input) || !hasCanonicalBase64Padding(input)) {
|
|
476
|
+
return invalid('$blob must be canonical padded base64');
|
|
477
|
+
}
|
|
402
478
|
const decoded = Trust.try(
|
|
403
|
-
() =>
|
|
404
|
-
const bytes = Buffer.from(input, 'base64');
|
|
405
|
-
return { bytes: new Uint8Array(bytes), canonical: bytes.toString('base64') };
|
|
406
|
-
},
|
|
479
|
+
() => new Uint8Array(Buffer.from(input, 'base64')),
|
|
407
480
|
(cause) => ({ kind: 'invalid' as const, message: `$blob: ${errorMessage(cause)}` }),
|
|
408
481
|
);
|
|
409
482
|
if (!decoded.ok) return decoded;
|
|
410
|
-
|
|
411
|
-
|
|
483
|
+
return Result.ok<SqlValue>(decoded.value);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* A string matching `BASE64` is canonical unless its padding (`=` or `==`)
|
|
488
|
+
* leaves non-zero unused low bits in the character before `=`.
|
|
489
|
+
*/
|
|
490
|
+
function hasCanonicalBase64Padding(input: string): boolean {
|
|
491
|
+
const length = input.length;
|
|
492
|
+
if (length === 0 || input.charCodeAt(length - 1) !== 0x3d) return true;
|
|
493
|
+
if (input.charCodeAt(length - 2) === 0x3d) {
|
|
494
|
+
// 2 padding chars (`==`): 4-bit unused tail of char at length - 3 must be 0 (A, Q, g, w).
|
|
495
|
+
const c = input.charCodeAt(length - 3);
|
|
496
|
+
return c === 0x41 || c === 0x51 || c === 0x67 || c === 0x77;
|
|
497
|
+
}
|
|
498
|
+
// 1 padding char (`=`): 2-bit unused tail of char at length - 2 must be 0 (sextet % 4 === 0).
|
|
499
|
+
const c = input.charCodeAt(length - 2);
|
|
500
|
+
const sextet = c >= 0x41 && c <= 0x5a ? c - 0x41
|
|
501
|
+
: c >= 0x61 && c <= 0x7a ? c - 0x61 + 26
|
|
502
|
+
: c >= 0x30 && c <= 0x39 ? c - 0x30 + 52
|
|
503
|
+
: c === 0x2b ? 62 : 63;
|
|
504
|
+
return (sextet & 3) === 0;
|
|
412
505
|
}
|
|
413
506
|
|
|
414
507
|
/** An integer result: a JSON number or a `$bigint` tag. */
|
|
@@ -426,23 +519,26 @@ function parseWireInteger(input: unknown, at: string) {
|
|
|
426
519
|
//trust:validator_function
|
|
427
520
|
export function parseSqlParams(input: unknown) {
|
|
428
521
|
if (Array.isArray(input)) {
|
|
429
|
-
|
|
430
|
-
|
|
522
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
523
|
+
const item: unknown = input[index];
|
|
524
|
+
if (item === null || typeof item === 'string' || typeof item === 'boolean' || (typeof item === 'number' && Number.isFinite(item))) continue;
|
|
431
525
|
const value = parseSqlValue(item);
|
|
432
526
|
if (!value.ok) return invalid(`params[${index}]: ${value.error.message}`);
|
|
433
|
-
|
|
527
|
+
input[index] = value.value;
|
|
434
528
|
}
|
|
435
|
-
return Result.ok<SqlParams>(
|
|
529
|
+
return Result.ok<SqlParams>(input as SqlValue[]);
|
|
436
530
|
}
|
|
437
531
|
const fields = fieldsOf(input);
|
|
438
532
|
if (!fields) return invalid('params must be an array or an object');
|
|
439
|
-
const
|
|
440
|
-
for (const
|
|
533
|
+
const record = input as Record<string, SqlValue>;
|
|
534
|
+
for (const name in fields) {
|
|
535
|
+
const item = fields[name];
|
|
536
|
+
if (item === null || typeof item === 'string' || typeof item === 'boolean' || (typeof item === 'number' && Number.isFinite(item))) continue;
|
|
441
537
|
const value = parseSqlValue(item);
|
|
442
538
|
if (!value.ok) return invalid(`params.${name}: ${value.error.message}`);
|
|
443
|
-
|
|
539
|
+
record[name] = value.value;
|
|
444
540
|
}
|
|
445
|
-
return Result.ok<SqlParams>(
|
|
541
|
+
return Result.ok<SqlParams>(record);
|
|
446
542
|
}
|
|
447
543
|
|
|
448
544
|
/** `params` when present; absent (`undefined`) is allowed. */
|
|
@@ -586,18 +682,28 @@ export function parseResponse(input: unknown) {
|
|
|
586
682
|
export function parseRow(input: unknown) {
|
|
587
683
|
const fields = fieldsOf(input);
|
|
588
684
|
if (!fields) return invalid('a row must be an object');
|
|
589
|
-
const row
|
|
685
|
+
const row = input as SqlRow;
|
|
686
|
+
for (const column in fields) {
|
|
687
|
+
const raw = fields[column];
|
|
688
|
+
if (typeof raw === 'bigint') row[column] = Number(raw);
|
|
689
|
+
}
|
|
590
690
|
return Result.ok(row);
|
|
591
691
|
}
|
|
592
692
|
|
|
593
693
|
//trust:validator_function
|
|
594
694
|
export function parseRows(input: unknown) {
|
|
595
695
|
if (!Array.isArray(input)) return invalid('rows must be an array');
|
|
596
|
-
const rows: SqlRow[] =
|
|
597
|
-
for (
|
|
598
|
-
const
|
|
599
|
-
|
|
600
|
-
rows
|
|
696
|
+
const rows: SqlRow[] = new Array(input.length);
|
|
697
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
698
|
+
const item: unknown = input[index];
|
|
699
|
+
const fields = fieldsOf(item);
|
|
700
|
+
if (!fields) return invalid(`rows[${index}]: a row must be an object`);
|
|
701
|
+
const row = item as SqlRow;
|
|
702
|
+
for (const column in fields) {
|
|
703
|
+
const raw = fields[column];
|
|
704
|
+
if (typeof raw === 'bigint') row[column] = Number(raw);
|
|
705
|
+
}
|
|
706
|
+
rows[index] = row;
|
|
601
707
|
}
|
|
602
708
|
return Result.ok(rows);
|
|
603
709
|
}
|
|
@@ -641,19 +747,20 @@ export function parseBatchResult(input: unknown) {
|
|
|
641
747
|
//trust:validator_function
|
|
642
748
|
function parseWireRows(input: unknown) {
|
|
643
749
|
if (!Array.isArray(input)) return invalid('rows must be an array');
|
|
644
|
-
const rows: SqlRow[] =
|
|
645
|
-
for (
|
|
750
|
+
const rows: SqlRow[] = new Array(input.length);
|
|
751
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
752
|
+
const item: unknown = input[index];
|
|
646
753
|
const fields = fieldsOf(item);
|
|
647
754
|
if (!fields) return invalid(`rows[${index}] must be an object`);
|
|
648
|
-
const
|
|
649
|
-
for (const
|
|
755
|
+
const record = item as SqlRow;
|
|
756
|
+
for (const column in fields) {
|
|
757
|
+
const raw = fields[column];
|
|
758
|
+
if (raw === null || typeof raw === 'string' || typeof raw === 'boolean' || (typeof raw === 'number' && Number.isFinite(raw))) continue;
|
|
650
759
|
const value = parseSqlValue(raw);
|
|
651
760
|
if (!value.ok) return invalid(`rows[${index}].${column}: ${value.error.message}`);
|
|
652
|
-
|
|
761
|
+
record[column] = value.value;
|
|
653
762
|
}
|
|
654
|
-
|
|
655
|
-
const row: SqlRow = Object.fromEntries(entries);
|
|
656
|
-
rows.push(row);
|
|
763
|
+
rows[index] = record;
|
|
657
764
|
}
|
|
658
765
|
return Result.ok(rows);
|
|
659
766
|
}
|
|
@@ -785,15 +892,47 @@ export function parseStatusResult(input: unknown) {
|
|
|
785
892
|
// An owner from before multi-file owners served exactly one file.
|
|
786
893
|
const files = f.files === undefined ? 1 : f.files;
|
|
787
894
|
if (typeof files !== 'number') return invalid('status.files must be a number');
|
|
895
|
+
const readers = parseReaderPoolStatus(f.readers);
|
|
896
|
+
if (!readers.ok) return readers;
|
|
897
|
+
const detachedReads = parseDetachedReads(f.detachedReads);
|
|
898
|
+
if (!detachedReads.ok) return detachedReads;
|
|
788
899
|
const result: StatusResult = {
|
|
789
900
|
file: f.file, socket: f.socket, pid: f.pid, serverVersion: f.serverVersion, protocol: f.protocol, pragmas: pragmas.value, files,
|
|
790
901
|
clients: f.clients, uptimeMs: f.uptimeMs, idleMs: f.idleMs, idleTimeoutMs: f.idleTimeoutMs, requestsServed: f.requestsServed,
|
|
791
902
|
transaction: transaction.value,
|
|
792
903
|
capabilities: capabilities.value,
|
|
904
|
+
...(readers.value === undefined ? {} : { readers: readers.value }),
|
|
905
|
+
...(detachedReads.value === undefined ? {} : { detachedReads: detachedReads.value }),
|
|
793
906
|
};
|
|
794
907
|
return Result.ok(result);
|
|
795
908
|
}
|
|
796
909
|
|
|
910
|
+
const READER_POOL_COUNTERS = ['size', 'queueLimit', 'workers', 'busy', 'queued', 'served', 'refused', 'failed', 'restarts', 'queueMsMax', 'execMsMax'] as const;
|
|
911
|
+
|
|
912
|
+
/** `status.readers`: absent (older owner), null (no reader), or every counter a number. */
|
|
913
|
+
//trust:validator_function
|
|
914
|
+
function parseReaderPoolStatus(input: unknown) {
|
|
915
|
+
if (input === undefined) return Result.ok<ReaderPoolStatus | null | undefined>(undefined);
|
|
916
|
+
if (input === null) return Result.ok<ReaderPoolStatus | null | undefined>(null);
|
|
917
|
+
const f = fieldsOf(input);
|
|
918
|
+
if (!f) return invalid('status.readers must be null or an object');
|
|
919
|
+
const counters: Partial<Record<(typeof READER_POOL_COUNTERS)[number], number>> = {};
|
|
920
|
+
for (const name of READER_POOL_COUNTERS) {
|
|
921
|
+
const value = f[name];
|
|
922
|
+
if (typeof value !== 'number') return invalid(`status.readers.${name} must be a number`);
|
|
923
|
+
counters[name] = value;
|
|
924
|
+
}
|
|
925
|
+
return Result.ok<ReaderPoolStatus | null | undefined>(counters as ReaderPoolStatus);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
//trust:validator_function
|
|
929
|
+
function parseDetachedReads(input: unknown) {
|
|
930
|
+
if (input === undefined) return Result.ok<StatusResult['detachedReads']>(undefined);
|
|
931
|
+
const f = fieldsOf(input);
|
|
932
|
+
if (!f || typeof f.pooled !== 'number' || typeof f.inline !== 'number') return invalid('status.detachedReads must carry pooled and inline counts');
|
|
933
|
+
return Result.ok<StatusResult['detachedReads']>({ pooled: f.pooled, inline: f.inline });
|
|
934
|
+
}
|
|
935
|
+
|
|
797
936
|
/**
|
|
798
937
|
* The transaction-control statement a SQL text starts with, or null. Leading
|
|
799
938
|
* whitespace and comments are skipped so `/* x *\/ BEGIN` is still seen.
|