@otwdesign/db-core 1.4.1 → 1.5.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 +38 -0
- package/dist/backup/api.js +2 -33
- package/dist/backup/api.js.map +1 -1
- package/dist/body.d.ts +17 -0
- package/dist/body.js +88 -0
- package/dist/body.js.map +1 -0
- package/dist/browser.d.ts +1 -0
- package/dist/drivers/mysql-connection.d.ts +101 -0
- package/dist/drivers/mysql-connection.js +204 -0
- package/dist/drivers/mysql-connection.js.map +1 -0
- package/dist/drivers/mysql-import.d.ts +22 -0
- package/dist/drivers/mysql-import.js +688 -0
- package/dist/drivers/mysql-import.js.map +1 -0
- package/dist/drivers/mysql.d.ts +11 -1
- package/dist/drivers/mysql.js +87 -97
- package/dist/drivers/mysql.js.map +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js.map +1 -1
- package/dist/handler.d.ts +31 -0
- package/dist/handler.js +93 -9
- package/dist/handler.js.map +1 -1
- package/dist/import/api.d.ts +65 -0
- package/dist/import/api.js +1282 -0
- package/dist/import/api.js.map +1 -0
- package/dist/import/audit.d.ts +47 -0
- package/dist/import/audit.js +434 -0
- package/dist/import/audit.js.map +1 -0
- package/dist/import/charsets.d.ts +25 -0
- package/dist/import/charsets.js +39 -0
- package/dist/import/charsets.js.map +1 -0
- package/dist/import/frames.d.ts +26 -0
- package/dist/import/frames.js +158 -0
- package/dist/import/frames.js.map +1 -0
- package/dist/import/grants.d.ts +26 -0
- package/dist/import/grants.js +122 -0
- package/dist/import/grants.js.map +1 -0
- package/dist/import/protocol.d.ts +462 -0
- package/dist/import/protocol.js +72 -0
- package/dist/import/protocol.js.map +1 -0
- package/dist/import/shared.d.ts +3 -0
- package/dist/import/shared.js +9 -0
- package/dist/import/shared.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/types.d.ts +308 -2
- package/package.json +7 -2
- package/src/backup/api.ts +2 -33
- package/src/body.ts +94 -0
- package/src/browser.ts +24 -0
- package/src/drivers/mysql-connection.ts +282 -0
- package/src/drivers/mysql-import.ts +769 -0
- package/src/drivers/mysql.ts +96 -107
- package/src/errors.ts +2 -0
- package/src/handler.ts +126 -8
- package/src/import/api.ts +1530 -0
- package/src/import/audit.ts +467 -0
- package/src/import/charsets.ts +46 -0
- package/src/import/frames.ts +191 -0
- package/src/import/grants.ts +117 -0
- package/src/import/protocol.ts +540 -0
- package/src/import/shared.ts +8 -0
- package/src/index.ts +24 -0
- package/src/types.ts +319 -2
package/dist/types.d.ts
CHANGED
|
@@ -48,6 +48,11 @@ export interface Capabilities {
|
|
|
48
48
|
* them with `session()`.
|
|
49
49
|
*/
|
|
50
50
|
backup?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* SQL files imported a statement at a time, as the `mysql` client runs them: MySQL and MariaDB.
|
|
53
|
+
* The API sets it: a driver offers them with `importer()`.
|
|
54
|
+
*/
|
|
55
|
+
import?: boolean;
|
|
51
56
|
}
|
|
52
57
|
/** What a schema-tree entry is: SQLite and MySQL have tables and views, PostgreSQL the rest too. */
|
|
53
58
|
export type TableKind = 'table' | 'view' | 'materialized-view' | 'foreign-table' | 'partitioned-table';
|
|
@@ -280,6 +285,18 @@ export interface Driver {
|
|
|
280
285
|
* database in the host's own process) doesn't.
|
|
281
286
|
*/
|
|
282
287
|
readonly pgUrl?: string;
|
|
288
|
+
/**
|
|
289
|
+
* A connection of its own for one import of an SQL file, open until its `end()`: what the file
|
|
290
|
+
* sets, locks and leaves uncommitted stays on it from one statement to the next. MySQL and
|
|
291
|
+
* MariaDB.
|
|
292
|
+
*
|
|
293
|
+
* `idleMs` is the longest the import may sit unused between two statements, in milliseconds
|
|
294
|
+
* (5 minutes, given none): the server is asked to keep the connection that long and a minute
|
|
295
|
+
* more, where its own setting would drop it sooner.
|
|
296
|
+
*/
|
|
297
|
+
importer?(options?: {
|
|
298
|
+
idleMs?: number;
|
|
299
|
+
}): Promise<Importer>;
|
|
283
300
|
close(): Promise<void>;
|
|
284
301
|
}
|
|
285
302
|
/** A backup's or a restore's transaction. Values come back as the server's text. */
|
|
@@ -302,6 +319,203 @@ export interface Session {
|
|
|
302
319
|
*/
|
|
303
320
|
inTransaction?(): boolean;
|
|
304
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* One import's connection. It is the file's alone: no other request runs on it, and it never goes
|
|
324
|
+
* back to a pool. It can't run two statements in one query, whatever the connection's URL says,
|
|
325
|
+
* and it proved that to itself when it opened.
|
|
326
|
+
*
|
|
327
|
+
* A kill names the connection, not a statement: it stops whatever runs there when it arrives. So
|
|
328
|
+
* while one is on its way, from `stop()` or from a statement's own time limit, nothing else is
|
|
329
|
+
* sent on the connection: `use()`, `run()` and `state()` reject, and `end()` waits for the kill
|
|
330
|
+
* to be answered before its rollback. A kill the server takes after the statement has already
|
|
331
|
+
* ended stops nothing later.
|
|
332
|
+
*/
|
|
333
|
+
export interface Importer {
|
|
334
|
+
/** The server it reached, and its session as it opened. */
|
|
335
|
+
readonly target: ImportTarget;
|
|
336
|
+
/**
|
|
337
|
+
* Selects `database`. The answer's `database` is what the server then says is selected, to
|
|
338
|
+
* compare with the name the person typed.
|
|
339
|
+
*/
|
|
340
|
+
use(database: string): Promise<ImportSession>;
|
|
341
|
+
/**
|
|
342
|
+
* One statement of the file, sent as written and as one query: never split, never prepared (a
|
|
343
|
+
* `?` or a `:name` is the file's own), and as UTF-8 whatever character set the file has set.
|
|
344
|
+
* It answers when the server has: with what the statement did, or with the error the server
|
|
345
|
+
* refused it with, which is an answer and not a rejection. Past `timeoutMs` the statement is
|
|
346
|
+
* stopped as `stop()` stops it, and answers as refused.
|
|
347
|
+
*
|
|
348
|
+
* A statement longer than the server takes in one packet (its `max_allowed_packet`), or than
|
|
349
|
+
* 16,777,208 bytes, is answered as refused too, with errno 1153, and is not sent: sent, it
|
|
350
|
+
* would cost the connection. So is a statement the server would read otherwise than the
|
|
351
|
+
* browser cut it, with errno 1235: one with text beyond ASCII while the file has the session
|
|
352
|
+
* read statements in gbk, big5, sjis or another set of several bytes a character but UTF-8's,
|
|
353
|
+
* and any statement at all under swe7 or a set the import doesn't know. Both errors say
|
|
354
|
+
* `unsent`.
|
|
355
|
+
*
|
|
356
|
+
* It answers only once every kill sent while it ran has been answered too, its own time
|
|
357
|
+
* limit's included: a statement that ends just as it is stopped answers as it ended, and by
|
|
358
|
+
* then no kill is left on its way to land on the next statement.
|
|
359
|
+
*
|
|
360
|
+
* Whatever the server answered for the statement is what `run()` answers, even when the
|
|
361
|
+
* connection is gone by then: a statement that ran is answered for as run, with `lost` in the
|
|
362
|
+
* place of the session it could no longer tell (see `ImportRanLast`). Count that statement.
|
|
363
|
+
*
|
|
364
|
+
* It rejects only when the server gave the statement no answer: the connection went under it
|
|
365
|
+
* or before it (a ConnectionError, and then whether it ran is in doubt, unless `end()` says
|
|
366
|
+
* otherwise), a statement is already running on the connection, a kill is on its way, or the
|
|
367
|
+
* import has ended.
|
|
368
|
+
*/
|
|
369
|
+
run(sql: string, options: {
|
|
370
|
+
timeoutMs: number;
|
|
371
|
+
}): Promise<ImportResult>;
|
|
372
|
+
/**
|
|
373
|
+
* Asks the server what state the session is in, with a statement that does nothing. For after
|
|
374
|
+
* a refused statement: an error says nothing of whether the transaction it ran in is still
|
|
375
|
+
* open, and a deadlock or a lock timeout has rolled it back.
|
|
376
|
+
*/
|
|
377
|
+
state(): Promise<ImportSession>;
|
|
378
|
+
/**
|
|
379
|
+
* Stops the statement that is running, with KILL QUERY from a connection of its own, and
|
|
380
|
+
* answers when the server has taken the kill: the statement's own `run()` then answers, as
|
|
381
|
+
* refused unless it had already ended. With nothing running it sends nothing, and a second
|
|
382
|
+
* call while one is on its way waits for that one.
|
|
383
|
+
*/
|
|
384
|
+
stop(): Promise<void>;
|
|
385
|
+
/**
|
|
386
|
+
* Rolls back a transaction left open, as the `mysql` client does at the end of a file, and
|
|
387
|
+
* closes the connection. If a statement is still running, one `stop()` could not end, the
|
|
388
|
+
* connection is cut under it instead. After it, nothing runs; a second call answers as the
|
|
389
|
+
* first did.
|
|
390
|
+
*
|
|
391
|
+
* It can take long: a statement that runs is stopped first, with ten seconds for the kill's
|
|
392
|
+
* answer and then two minutes for the statement's own, and the rollback has two minutes.
|
|
393
|
+
*/
|
|
394
|
+
end(): Promise<ImportClosed>;
|
|
395
|
+
/**
|
|
396
|
+
* `listener` is told, once, when the connection is gone other than by `end()`: the server
|
|
397
|
+
* dropped it, or the network did. Straight away if it already is.
|
|
398
|
+
*
|
|
399
|
+
* It is told before a `run()` that was waiting answers. That `run()` still answers, or
|
|
400
|
+
* rejects, and what it answers still counts: a statement the server had answered for is
|
|
401
|
+
* answered for as it ran, with the same error as its `lost`.
|
|
402
|
+
*/
|
|
403
|
+
onLost(listener: (error: Error) => void): void;
|
|
404
|
+
}
|
|
405
|
+
/** The server an import's connection reached. */
|
|
406
|
+
export interface ImportTarget {
|
|
407
|
+
/** `MySQL` or `MariaDB`. */
|
|
408
|
+
product: string;
|
|
409
|
+
/** Such as `8.4.3` or `11.4.2`. */
|
|
410
|
+
version: string;
|
|
411
|
+
/**
|
|
412
|
+
* The connection's id on the server, CONNECTION_ID(): what PROCESSLIST shows and KILL names.
|
|
413
|
+
* After a restart, a new import can wait until this one has gone from the server.
|
|
414
|
+
*/
|
|
415
|
+
thread: number;
|
|
416
|
+
/** The session's sql_mode as it opened, whole. */
|
|
417
|
+
sqlMode: string;
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* An import's session after a statement, as the server reports it with the statement's result:
|
|
421
|
+
* never worked out from the statements' text.
|
|
422
|
+
*/
|
|
423
|
+
export interface ImportSession {
|
|
424
|
+
/** A transaction is open. */
|
|
425
|
+
inTransaction: boolean;
|
|
426
|
+
/** sql_mode has NO_BACKSLASH_ESCAPES: a backslash in a string is a backslash. */
|
|
427
|
+
noBackslashEscapes: boolean;
|
|
428
|
+
/** sql_mode has ANSI_QUOTES: double quotes hold a name, not a string. */
|
|
429
|
+
ansiQuotes: boolean;
|
|
430
|
+
/** The database selected, which a `USE` in the file changes; null with none. */
|
|
431
|
+
database: string | null;
|
|
432
|
+
/**
|
|
433
|
+
* The character set the session reads statements in (character_set_client), by its name in
|
|
434
|
+
* small letters, which a `SET NAMES` in the file changes.
|
|
435
|
+
*/
|
|
436
|
+
characterSet: string;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* What one statement of an import did, or the error the server refused it with. Tell them apart
|
|
440
|
+
* by `error`, and then by `lost`.
|
|
441
|
+
*/
|
|
442
|
+
export type ImportResult = ImportRan | ImportRanLast | ImportRefused;
|
|
443
|
+
/** What a statement that ran did, as the server answered for it. */
|
|
444
|
+
export interface ImportDone {
|
|
445
|
+
/** Rows it changed, as the server counts them; 0 for a statement that changes none. */
|
|
446
|
+
affected: number;
|
|
447
|
+
/**
|
|
448
|
+
* How many warnings the server raised. Always 0 for a statement that returned rows: mysql2
|
|
449
|
+
* does not pass on the count that ends them.
|
|
450
|
+
*/
|
|
451
|
+
warnings: number;
|
|
452
|
+
/**
|
|
453
|
+
* Rows it returned, counted and dropped as they arrived: an import shows no results. A
|
|
454
|
+
* statement that is nothing on this server, such as a versioned comment for another (errno
|
|
455
|
+
* 1065), ran with nothing changed and nothing returned.
|
|
456
|
+
*/
|
|
457
|
+
rows: number;
|
|
458
|
+
}
|
|
459
|
+
/** A statement that ran, and the session after it. */
|
|
460
|
+
export interface ImportRan extends ImportDone, ImportSession {
|
|
461
|
+
error?: undefined;
|
|
462
|
+
lost?: undefined;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* A statement that ran, and the last one this connection will: the connection went after the
|
|
466
|
+
* server had answered for it, before it could say what its session then was. The statement is
|
|
467
|
+
* done, as much as any that ran, and counts; nothing after it can be split or sent.
|
|
468
|
+
*
|
|
469
|
+
* Whether it is kept is the server's to say, as after any lost connection: outside a
|
|
470
|
+
* transaction it is, and inside one it went with the transaction.
|
|
471
|
+
*/
|
|
472
|
+
export interface ImportRanLast extends ImportDone {
|
|
473
|
+
error?: undefined;
|
|
474
|
+
/** Why the connection is gone: the error `onLost` listeners were told, and later calls reject with. */
|
|
475
|
+
lost: Error;
|
|
476
|
+
}
|
|
477
|
+
/** A statement the server refused, or one that was stopped. `state()` tells the session after it. */
|
|
478
|
+
export interface ImportRefused {
|
|
479
|
+
error: ImportError;
|
|
480
|
+
}
|
|
481
|
+
/** The server's error, as the `mysql` client prints it: `ERROR 1062 (23000): Duplicate entry`. */
|
|
482
|
+
export interface ImportError {
|
|
483
|
+
/** The server's error number, such as 1062; 1317 for a statement that was stopped. */
|
|
484
|
+
errno: number;
|
|
485
|
+
/** Its SQLSTATE, such as `23000`. */
|
|
486
|
+
code: string;
|
|
487
|
+
message: string;
|
|
488
|
+
/**
|
|
489
|
+
* The import refused the statement itself and never sent it, with the server's number for
|
|
490
|
+
* such a refusal (1153, too long; 1235, a character set that would read it otherwise): the
|
|
491
|
+
* server changed nothing. Absent on every error the server gave.
|
|
492
|
+
*/
|
|
493
|
+
unsent?: true;
|
|
494
|
+
}
|
|
495
|
+
/** How an import's connection closed. */
|
|
496
|
+
export interface ImportClosed {
|
|
497
|
+
/**
|
|
498
|
+
* What became of a transaction the file left open, as far as the server said:
|
|
499
|
+
* - `none`: none was open when the server was asked, at the end.
|
|
500
|
+
* - `rolled-back`: one was open, and the server took the rollback.
|
|
501
|
+
* - `abandoned`: the connection went, or was closed, with no rollback taken: it was lost
|
|
502
|
+
* before the server could be asked, or the server refused, as it does inside an XA
|
|
503
|
+
* transaction. Whether one was still open by then is not known here. A server undoes what
|
|
504
|
+
* is open on a connection that goes, with one exception: an XA transaction already
|
|
505
|
+
* prepared stays, locks and all, until an `XA COMMIT` or `XA ROLLBACK` names it (`XA
|
|
506
|
+
* RECOVER` lists those).
|
|
507
|
+
*
|
|
508
|
+
* MySQL takes a transaction off its connection at `XA PREPARE` (8.0.29 on, unless
|
|
509
|
+
* `xa_detach_on_prepare` is off): from then on none is open here, and it is the server's.
|
|
510
|
+
*/
|
|
511
|
+
transaction: 'none' | 'rolled-back' | 'abandoned';
|
|
512
|
+
/**
|
|
513
|
+
* A statement of the file's was still unanswered when the connection went, or was cut under
|
|
514
|
+
* it: whether that statement finished is not known. False when every statement sent was
|
|
515
|
+
* answered, a last one `run()` answered for with `lost` included.
|
|
516
|
+
*/
|
|
517
|
+
inDoubt: boolean;
|
|
518
|
+
}
|
|
305
519
|
/** Who is asking, as the host's `authorize()` says. */
|
|
306
520
|
export interface Viewer {
|
|
307
521
|
/** Recorded in the audit log, such as a user id or an email. */
|
|
@@ -319,11 +533,103 @@ export interface AuditEvent {
|
|
|
319
533
|
connection: string;
|
|
320
534
|
/** The database it ran on, when it wasn't the connection's own. */
|
|
321
535
|
database?: string;
|
|
322
|
-
action: 'query' | 'rows' | 'count' | 'apply' | 'export' | 'cancel' | 'backup' | 'restore';
|
|
323
|
-
/**
|
|
536
|
+
action: 'query' | 'rows' | 'count' | 'apply' | 'export' | 'cancel' | 'backup' | 'restore' | 'import';
|
|
537
|
+
/**
|
|
538
|
+
* The SQL run, or for `rows`, the table read. For `import`, a batch's statements, each ended
|
|
539
|
+
* with `;` and a new line, logged before the first is sent. The other events of an import say
|
|
540
|
+
* in words what they are about.
|
|
541
|
+
*
|
|
542
|
+
* An import's statements are each whole, a `CREATE USER` with its password included. One kind
|
|
543
|
+
* is shortened, to keep a dump's rows out of the log: an INSERT or REPLACE of more than 1,024
|
|
544
|
+
* characters, not counting white space around it and between its first words, that is nothing
|
|
545
|
+
* but rows of plain values. It is logged as its start,
|
|
546
|
+
* `… [its length in bytes, sha256 its SHA-256] …`, and its end:
|
|
547
|
+
* - its start is 256 characters from its first word, with the words up to the table's name
|
|
548
|
+
* one space apart, whatever white space the file has before and between them, so it always
|
|
549
|
+
* says which table;
|
|
550
|
+
* - its end is the last 256 characters before any white space it ends on.
|
|
551
|
+
*
|
|
552
|
+
* Nothing but rows means: after the table's name, the columns' names in brackets or none,
|
|
553
|
+
* `VALUES`, and rows of numbers, strings in single quotes, `NULL`, `TRUE`, `FALSE`, `DEFAULT`
|
|
554
|
+
* and hex or bit values, as mysqldump writes them, to the statement's last character. An
|
|
555
|
+
* INSERT or REPLACE with anything else in it is whole: a `SELECT`, a function, a variable or
|
|
556
|
+
* another column in a row, `SET`, `ON DUPLICATE KEY UPDATE`, `RETURNING`, a comment anywhere.
|
|
557
|
+
* So what is left out of the log is data the file holds, never something the statement read
|
|
558
|
+
* from the server. It is whole, too:
|
|
559
|
+
* - when the table's name can't be read, or doesn't end within what is logged of the start;
|
|
560
|
+
* - when a string or a name in backticks could end elsewhere in a character set the file
|
|
561
|
+
* chose (`SET NAMES gbk`): a backtick, or backslashes before a quote, right after a
|
|
562
|
+
* character beyond ASCII. A table or a column named so has all its rows in the log.
|
|
563
|
+
*/
|
|
324
564
|
statement: string;
|
|
325
565
|
durationMs: number;
|
|
326
566
|
/** Rows returned or changed. */
|
|
327
567
|
rows?: number;
|
|
328
568
|
error?: string;
|
|
569
|
+
/** For `import`: which import, and which part of it. */
|
|
570
|
+
import?: ImportAudit;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* What an audit event says of an import. Every event of one import has its `thread`, and the
|
|
574
|
+
* event's `database` is the one the import started in.
|
|
575
|
+
*/
|
|
576
|
+
export type ImportAudit = ImportAuditStart | ImportAuditBatch | ImportAuditEnd;
|
|
577
|
+
/** An import was opened: its connection is made, and nothing of the file has run. */
|
|
578
|
+
export interface ImportAuditStart {
|
|
579
|
+
step: 'start';
|
|
580
|
+
/** The import's connection on the server, CONNECTION_ID(). */
|
|
581
|
+
thread: number;
|
|
582
|
+
/** The file as the browser said it is: the server never sees it whole. */
|
|
583
|
+
file: {
|
|
584
|
+
name: string;
|
|
585
|
+
/** In bytes, as chosen: a `.sql.gz`'s compressed size. */
|
|
586
|
+
size: number;
|
|
587
|
+
/** How many statements the browser's check counted. */
|
|
588
|
+
statements: number;
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* A batch of an import. It is logged twice: before its first statement is sent, with its
|
|
593
|
+
* statements as the event's `statement`, and when it ends, with `ended`, `done` and the rows it
|
|
594
|
+
* changed.
|
|
595
|
+
*/
|
|
596
|
+
export interface ImportAuditBatch {
|
|
597
|
+
step: 'batch';
|
|
598
|
+
thread: number;
|
|
599
|
+
/** The number of the batch's first statement in the file, counting from 1. */
|
|
600
|
+
start: number;
|
|
601
|
+
/** How many statements the batch holds. */
|
|
602
|
+
count: number;
|
|
603
|
+
/**
|
|
604
|
+
* The number of a refused statement the batch leaves out, when the person chose to skip it:
|
|
605
|
+
* it is in none of the batch's statements, and counts as done with those before it.
|
|
606
|
+
*/
|
|
607
|
+
skip?: number;
|
|
608
|
+
/**
|
|
609
|
+
* Absent before the batch runs. Then how it ended: `done`, nothing in it failed; `failed`, a
|
|
610
|
+
* statement was refused (the event's `error`); `lost`, the connection went; `stopped`, the
|
|
611
|
+
* import was ended while it ran.
|
|
612
|
+
*/
|
|
613
|
+
ended?: 'done' | 'failed' | 'lost' | 'stopped';
|
|
614
|
+
/** With `ended`: the number of the last statement done, which the next batch starts after. */
|
|
615
|
+
done?: number;
|
|
616
|
+
}
|
|
617
|
+
/** An import was closed, and its connection with it. */
|
|
618
|
+
export interface ImportAuditEnd {
|
|
619
|
+
step: 'end';
|
|
620
|
+
thread: number;
|
|
621
|
+
/**
|
|
622
|
+
* Why: `asked`, by import-end; `failed`, a statement was refused and the import could not go
|
|
623
|
+
* on after it; `pause`, nobody chose in time after a refused statement; `idle`, no request
|
|
624
|
+
* came for it in time; `unheard`, the request that opened it had gone before it could be
|
|
625
|
+
* answered, so nobody could have named it; `closed`, the host closed the tool; `lost`, the
|
|
626
|
+
* connection went.
|
|
627
|
+
*/
|
|
628
|
+
why: 'asked' | 'failed' | 'pause' | 'idle' | 'unheard' | 'closed' | 'lost';
|
|
629
|
+
/** The number of the last statement done. */
|
|
630
|
+
done: number;
|
|
631
|
+
/** A transaction the file left open was rolled back. */
|
|
632
|
+
rolledBack: boolean;
|
|
633
|
+
/** The statement that was running and would not stop: whether it finished is not known. */
|
|
634
|
+
doubt?: number;
|
|
329
635
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@otwdesign/db-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "A database manager's engine and HTTP API, as one Fetch handler: browse, query and inspect PostgreSQL, MySQL and SQLite from any app.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "OTW SRL",
|
|
@@ -40,6 +40,11 @@
|
|
|
40
40
|
"types": "./dist/format.d.ts",
|
|
41
41
|
"default": "./dist/format.js"
|
|
42
42
|
},
|
|
43
|
+
"./import": {
|
|
44
|
+
"@otwdesign/source": "./src/import/shared.ts",
|
|
45
|
+
"types": "./dist/import/shared.d.ts",
|
|
46
|
+
"default": "./dist/import/shared.js"
|
|
47
|
+
},
|
|
43
48
|
"./routes": {
|
|
44
49
|
"@otwdesign/source": "./src/routes.ts",
|
|
45
50
|
"types": "./dist/routes.d.ts",
|
|
@@ -52,7 +57,7 @@
|
|
|
52
57
|
"NOTICE"
|
|
53
58
|
],
|
|
54
59
|
"dependencies": {
|
|
55
|
-
"@otwdesign/db-ui": "1.
|
|
60
|
+
"@otwdesign/db-ui": "1.5.0"
|
|
56
61
|
},
|
|
57
62
|
"peerDependencies": {
|
|
58
63
|
"mysql2": "^3.11.0",
|
package/src/backup/api.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { join } from 'node:path'
|
|
|
5
5
|
import { pipeline } from 'node:stream/promises'
|
|
6
6
|
import { Readable } from 'node:stream'
|
|
7
7
|
import { createGzip } from 'node:zlib'
|
|
8
|
+
import { megabytes, readBytes } from '../body.ts'
|
|
8
9
|
import { DbAdminError, QueryError } from '../errors.ts'
|
|
9
10
|
import type { Access, AuditEvent, ConnectionConfig, Driver, Session, Viewer } from '../types.ts'
|
|
10
11
|
import {
|
|
@@ -248,7 +249,7 @@ export async function chunk(ctx: BackupContext, request: Request): Promise<{ rec
|
|
|
248
249
|
mayRestore(ctx)
|
|
249
250
|
const params = new URL(request.url).searchParams
|
|
250
251
|
const seq = Number(params.get('seq'))
|
|
251
|
-
const data = await
|
|
252
|
+
const data = await readBytes(request, staging.CHUNK_BYTES, 'A piece of an upload')
|
|
252
253
|
if (!data.byteLength) throw bad('The piece is empty.')
|
|
253
254
|
await write(ctx, async (s) => {
|
|
254
255
|
const upload = await staging.findUpload(s, String(params.get('upload')), ctx.viewer.id)
|
|
@@ -535,34 +536,6 @@ function headerOf(value: unknown): BackupHeader {
|
|
|
535
536
|
return readHeader(`${HEADER_PREFIX}${JSON.stringify(value)}`)
|
|
536
537
|
}
|
|
537
538
|
|
|
538
|
-
/** The request's body, refused past `max` bytes. */
|
|
539
|
-
async function bytes(request: Request, max: number): Promise<Uint8Array> {
|
|
540
|
-
const tooBig = () =>
|
|
541
|
-
new DbAdminError(`A piece of an upload is ${megabytes(max)} at most.`, 413, 'too-large')
|
|
542
|
-
if (Number(request.headers.get('content-length') ?? 0) > max) throw tooBig()
|
|
543
|
-
const reader = request.body?.getReader()
|
|
544
|
-
if (!reader) return new Uint8Array()
|
|
545
|
-
const parts: Uint8Array[] = []
|
|
546
|
-
let size = 0
|
|
547
|
-
for (;;) {
|
|
548
|
-
const { done, value } = await reader.read()
|
|
549
|
-
if (done) break
|
|
550
|
-
size += value.byteLength
|
|
551
|
-
if (size > max) {
|
|
552
|
-
await reader.cancel()
|
|
553
|
-
throw tooBig()
|
|
554
|
-
}
|
|
555
|
-
parts.push(value)
|
|
556
|
-
}
|
|
557
|
-
const all = new Uint8Array(size)
|
|
558
|
-
let at = 0
|
|
559
|
-
for (const part of parts) {
|
|
560
|
-
all.set(part, at)
|
|
561
|
-
at += part.byteLength
|
|
562
|
-
}
|
|
563
|
-
return all
|
|
564
|
-
}
|
|
565
|
-
|
|
566
539
|
async function gzipAll(pieces: AsyncIterable<string>): Promise<Buffer> {
|
|
567
540
|
const gzip = createGzip()
|
|
568
541
|
const out: Buffer[] = []
|
|
@@ -602,10 +575,6 @@ function file(body: ReadableStream<Uint8Array>, name: string, type: string): Res
|
|
|
602
575
|
})
|
|
603
576
|
}
|
|
604
577
|
|
|
605
|
-
function megabytes(bytes: number): string {
|
|
606
|
-
return `${(bytes / 1_048_576).toLocaleString('en', { maximumFractionDigits: 1 })} MB`
|
|
607
|
-
}
|
|
608
|
-
|
|
609
578
|
function bad(message: string): DbAdminError {
|
|
610
579
|
return new DbAdminError(message, 400, 'bad-request')
|
|
611
580
|
}
|
package/src/body.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { DbAdminError } from './errors.ts'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* A request's body, for every endpoint that takes one: a piece of a restore's upload, a batch of
|
|
5
|
+
* an import's statements, the JSON of the others. Counted as it arrives, so a body is never held
|
|
6
|
+
* past its limit, whatever length it says it has or doesn't say.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** The longest a timer waits: past it Node fires at once. */
|
|
10
|
+
const LONGEST_MS = 2_147_483_647
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The request's body, refused past `max` bytes. `what` names it in the refusal, such as
|
|
14
|
+
* "A piece of an upload". A length the request declares is believed only when it is already too
|
|
15
|
+
* long: the bytes are counted as they arrive, and the rest is left unread.
|
|
16
|
+
*
|
|
17
|
+
* With `withinMs`, the whole body has that long to arrive, from now: a body that stops, or comes
|
|
18
|
+
* a byte at a time, is let go and refused (408) instead of holding what its request holds. An
|
|
19
|
+
* import's batch has it, since its import is marked busy while the batch is read.
|
|
20
|
+
*/
|
|
21
|
+
export async function readBytes(
|
|
22
|
+
request: Request,
|
|
23
|
+
max: number,
|
|
24
|
+
what: string,
|
|
25
|
+
withinMs?: number,
|
|
26
|
+
): Promise<Uint8Array> {
|
|
27
|
+
if (withinMs !== undefined && !(withinMs > 0 && withinMs <= LONGEST_MS)) {
|
|
28
|
+
throw new RangeError(`A body's time to arrive is 1 to ${LONGEST_MS} ms, not ${withinMs}.`)
|
|
29
|
+
}
|
|
30
|
+
const tooBig = () => new DbAdminError(`${what} is ${megabytes(max)} at most.`, 413, 'too-large')
|
|
31
|
+
if (Number(request.headers.get('content-length') ?? 0) > max) throw tooBig()
|
|
32
|
+
const reader = request.body?.getReader()
|
|
33
|
+
if (!reader) return new Uint8Array()
|
|
34
|
+
let late = false
|
|
35
|
+
// Letting go of the body ends the read that waits on it, which then finds it was late.
|
|
36
|
+
const timer =
|
|
37
|
+
withinMs === undefined
|
|
38
|
+
? undefined
|
|
39
|
+
: setTimeout(() => {
|
|
40
|
+
late = true
|
|
41
|
+
reader.cancel().catch(() => {})
|
|
42
|
+
}, withinMs)
|
|
43
|
+
try {
|
|
44
|
+
const parts: Uint8Array[] = []
|
|
45
|
+
let size = 0
|
|
46
|
+
for (;;) {
|
|
47
|
+
const { done, value } = await reader.read()
|
|
48
|
+
if (late) throw tooSlow(what, withinMs!)
|
|
49
|
+
if (done) break
|
|
50
|
+
size += value.byteLength
|
|
51
|
+
if (size > max) {
|
|
52
|
+
await reader.cancel()
|
|
53
|
+
throw tooBig()
|
|
54
|
+
}
|
|
55
|
+
parts.push(value)
|
|
56
|
+
}
|
|
57
|
+
return joined(parts, size)
|
|
58
|
+
} finally {
|
|
59
|
+
clearTimeout(timer)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The request's body as text, read as `readBytes` reads it and refused (400) unless it is UTF-8:
|
|
65
|
+
* a byte that isn't would otherwise become another character in what is then run or stored.
|
|
66
|
+
*/
|
|
67
|
+
export async function readText(request: Request, max: number, what: string): Promise<string> {
|
|
68
|
+
const bytes = await readBytes(request, max, what)
|
|
69
|
+
try {
|
|
70
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(bytes)
|
|
71
|
+
} catch {
|
|
72
|
+
throw new DbAdminError(`${what} isn't UTF-8.`, 400, 'bad-request')
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function joined(parts: readonly Uint8Array[], size: number): Uint8Array {
|
|
77
|
+
const all = new Uint8Array(size)
|
|
78
|
+
let at = 0
|
|
79
|
+
for (const part of parts) {
|
|
80
|
+
all.set(part, at)
|
|
81
|
+
at += part.byteLength
|
|
82
|
+
}
|
|
83
|
+
return all
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function tooSlow(what: string, withinMs: number): DbAdminError {
|
|
87
|
+
const seconds = (withinMs / 1_000).toLocaleString('en', { maximumFractionDigits: 1 })
|
|
88
|
+
return new DbAdminError(`${what} didn't arrive within ${seconds} s.`, 408, 'too-slow')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Bytes as megabytes to show, to one decimal: `4 MB`, `1.5 MB`. */
|
|
92
|
+
export function megabytes(bytes: number): string {
|
|
93
|
+
return `${(bytes / 1_048_576).toLocaleString('en', { maximumFractionDigits: 1 })} MB`
|
|
94
|
+
}
|
package/src/browser.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* What the browser app takes from the core: the API's types, and nothing that runs on a server.
|
|
3
3
|
* The app builds against this, so no driver, Node module or database client reaches its bundle.
|
|
4
|
+
* Code the two share has entries of its own, `format` and `import`, which the app's build finds
|
|
5
|
+
* in the core's source: this one is types alone, and stays so.
|
|
4
6
|
*/
|
|
5
7
|
export type * from './types.ts'
|
|
6
8
|
export type { UiConfig } from './ui.ts'
|
|
@@ -9,3 +11,25 @@ export type { SchemaChoice } from './backup/dump.ts'
|
|
|
9
11
|
export type { BackupHeader, BackupSummary } from './backup/format.ts'
|
|
10
12
|
export type { PgTool } from './backup/pg-tools.ts'
|
|
11
13
|
export type { RestorePlan, RestoreResult } from './backup/restore.ts'
|
|
14
|
+
export type {
|
|
15
|
+
ImportAcceptedLine,
|
|
16
|
+
ImportBatch,
|
|
17
|
+
ImportBeatLine,
|
|
18
|
+
ImportDatabase,
|
|
19
|
+
ImportDoneLine,
|
|
20
|
+
ImportEnded,
|
|
21
|
+
ImportEndRequest,
|
|
22
|
+
ImportFailedLine,
|
|
23
|
+
ImportFailureState,
|
|
24
|
+
ImportLimits,
|
|
25
|
+
ImportLine,
|
|
26
|
+
ImportLostLine,
|
|
27
|
+
ImportModeLine,
|
|
28
|
+
ImportOptions,
|
|
29
|
+
ImportRunQuery,
|
|
30
|
+
ImportStarted,
|
|
31
|
+
ImportStartRequest,
|
|
32
|
+
ImportStatementLine,
|
|
33
|
+
ImportStatus,
|
|
34
|
+
ImportStoppedLine,
|
|
35
|
+
} from './import/protocol.ts'
|