@otwdesign/db-core 1.4.2 → 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.
Files changed (62) hide show
  1. package/README.md +38 -0
  2. package/dist/backup/api.js +2 -33
  3. package/dist/backup/api.js.map +1 -1
  4. package/dist/body.d.ts +17 -0
  5. package/dist/body.js +88 -0
  6. package/dist/body.js.map +1 -0
  7. package/dist/browser.d.ts +1 -0
  8. package/dist/drivers/mysql-connection.d.ts +101 -0
  9. package/dist/drivers/mysql-connection.js +204 -0
  10. package/dist/drivers/mysql-connection.js.map +1 -0
  11. package/dist/drivers/mysql-import.d.ts +22 -0
  12. package/dist/drivers/mysql-import.js +688 -0
  13. package/dist/drivers/mysql-import.js.map +1 -0
  14. package/dist/drivers/mysql.d.ts +11 -1
  15. package/dist/drivers/mysql.js +87 -97
  16. package/dist/drivers/mysql.js.map +1 -1
  17. package/dist/errors.d.ts +4 -0
  18. package/dist/errors.js.map +1 -1
  19. package/dist/handler.d.ts +31 -0
  20. package/dist/handler.js +93 -9
  21. package/dist/handler.js.map +1 -1
  22. package/dist/import/api.d.ts +65 -0
  23. package/dist/import/api.js +1282 -0
  24. package/dist/import/api.js.map +1 -0
  25. package/dist/import/audit.d.ts +47 -0
  26. package/dist/import/audit.js +434 -0
  27. package/dist/import/audit.js.map +1 -0
  28. package/dist/import/charsets.d.ts +25 -0
  29. package/dist/import/charsets.js +39 -0
  30. package/dist/import/charsets.js.map +1 -0
  31. package/dist/import/frames.d.ts +26 -0
  32. package/dist/import/frames.js +158 -0
  33. package/dist/import/frames.js.map +1 -0
  34. package/dist/import/grants.d.ts +26 -0
  35. package/dist/import/grants.js +122 -0
  36. package/dist/import/grants.js.map +1 -0
  37. package/dist/import/protocol.d.ts +462 -0
  38. package/dist/import/protocol.js +72 -0
  39. package/dist/import/protocol.js.map +1 -0
  40. package/dist/import/shared.d.ts +3 -0
  41. package/dist/import/shared.js +9 -0
  42. package/dist/import/shared.js.map +1 -0
  43. package/dist/index.d.ts +1 -0
  44. package/dist/types.d.ts +308 -2
  45. package/package.json +7 -2
  46. package/src/backup/api.ts +2 -33
  47. package/src/body.ts +94 -0
  48. package/src/browser.ts +24 -0
  49. package/src/drivers/mysql-connection.ts +282 -0
  50. package/src/drivers/mysql-import.ts +769 -0
  51. package/src/drivers/mysql.ts +96 -107
  52. package/src/errors.ts +2 -0
  53. package/src/handler.ts +126 -8
  54. package/src/import/api.ts +1530 -0
  55. package/src/import/audit.ts +467 -0
  56. package/src/import/charsets.ts +46 -0
  57. package/src/import/frames.ts +191 -0
  58. package/src/import/grants.ts +117 -0
  59. package/src/import/protocol.ts +540 -0
  60. package/src/import/shared.ts +8 -0
  61. package/src/index.ts +24 -0
  62. package/src/types.ts +319 -2
@@ -0,0 +1,462 @@
1
+ import type { ImportError } from '../types.ts';
2
+ /** A batch's body at most, in bytes: 4 MiB, lengths included. */
3
+ export declare const IMPORT_BATCH_BYTES = 4194304;
4
+ /** A batch's statements at most. */
5
+ export declare const IMPORT_BATCH_STATEMENTS = 1000;
6
+ /** What a frame's length takes, before its statement: four bytes, the big end first. */
7
+ export declare const IMPORT_LENGTH_BYTES = 4;
8
+ /** One statement at most, in bytes of UTF-8: a batch to itself. A longer one can't be sent. */
9
+ export declare const IMPORT_STATEMENT_BYTES: number;
10
+ /**
11
+ * How often import-run writes a line while one statement runs, in milliseconds. A proxy or a
12
+ * browser that sees nothing for longer has lost the stream, not a slow statement.
13
+ */
14
+ export declare const IMPORT_HEARTBEAT_MS = 10000;
15
+ /**
16
+ * How long a batch's body has to arrive whole, in milliseconds: 2 minutes. The import is marked
17
+ * busy while it is read, and its connection may hold locks, so a body that stops is let go. A
18
+ * line too slow for 4 MiB in that time is too slow for the file.
19
+ */
20
+ export declare const IMPORT_BODY_MS = 120000;
21
+ /** What an import may take, for the dialog to say and the browser to keep to. */
22
+ export interface ImportLimits {
23
+ /** A batch's body at most, in bytes. */
24
+ batchBytes: number;
25
+ /** A batch's statements at most. */
26
+ batchStatements: number;
27
+ /** One statement at most, in bytes of UTF-8. */
28
+ statementBytes: number;
29
+ /** How often import-run writes a line while a statement runs, in milliseconds. */
30
+ heartbeatMs: number;
31
+ /** How long a batch's body has to arrive whole, in milliseconds. */
32
+ bodyMs: number;
33
+ /** How long one statement may run before it is stopped, in milliseconds. */
34
+ statementTimeoutMs: number;
35
+ /**
36
+ * How long an import stays open with no run, status or end request for it, in milliseconds.
37
+ * The browser asks its status more often than that for as long as its dialog is open.
38
+ */
39
+ idleMs: number;
40
+ /** How long the choice after a failed statement may take, in milliseconds. */
41
+ pauseMs: number;
42
+ }
43
+ /** What the Import SQL dialog shows before a file is chosen. */
44
+ export interface ImportOptions {
45
+ /** `MySQL` or `MariaDB`. */
46
+ product: string;
47
+ /** Such as `8.4.3`. */
48
+ version: string;
49
+ /** The database the connection opens, offered first; null when its URL names none. */
50
+ database: string | null;
51
+ /** The databases the login sees, empty ones too, the server's own left out. */
52
+ databases: ImportDatabase[];
53
+ limits: ImportLimits;
54
+ /**
55
+ * This viewer's open or last import on the connection, null before the first. Never its id:
56
+ * only the dialog that started an import can go on with it. Any window of the viewer's can
57
+ * end it while it is open, with import-end and `current`.
58
+ */
59
+ current: ImportStatus | null;
60
+ }
61
+ /** A database an import can go into. */
62
+ export interface ImportDatabase {
63
+ name: string;
64
+ /**
65
+ * What the login may do in it, in MySQL's own words and capitals (`CREATE`, `ALTER`, `DROP`,
66
+ * `INSERT`, `LOCK TABLES`, `TRIGGER`…), from its grants on everything and on this database;
67
+ * `ALL PRIVILEGES` stands for them all. Grants on single tables aren't counted. Null when the
68
+ * grants couldn't be read: the check then says nothing about them.
69
+ */
70
+ privileges: string[] | null;
71
+ }
72
+ /** What import-start is sent. */
73
+ export interface ImportStartRequest {
74
+ connection: string;
75
+ /** The database to import into. */
76
+ database: string;
77
+ /** The same name as the person typed it: checked against what the server says is selected. */
78
+ confirm: string;
79
+ /** The file as the browser's check found it, for the audit log. */
80
+ file: {
81
+ name: string;
82
+ /** In bytes, as chosen: a `.sql.gz`'s compressed size. */
83
+ size: number;
84
+ /** How many statements the check counted. */
85
+ statements: number;
86
+ };
87
+ /**
88
+ * The `thread` of this browser's last import on the connection, sent when the server answered
89
+ * that it doesn't know that import: it restarted while the import was open, and the statement
90
+ * that ran then may still be running on the database server. For as long as it is, the new
91
+ * import is refused (409 `import-thread`) with nothing opened, and the browser asks again.
92
+ */
93
+ previousThread?: number;
94
+ }
95
+ /** An import, open: its connection waits for the first batch. */
96
+ export interface ImportStarted {
97
+ /** Names the import in every later request. Random, and good for this viewer only. */
98
+ import: string;
99
+ /** The connection's id on the server, as PROCESSLIST shows it. */
100
+ thread: number;
101
+ /** The session's sql_mode, whole. */
102
+ sqlMode: string;
103
+ /** sql_mode has NO_BACKSLASH_ESCAPES: split the file with a backslash as a plain character. */
104
+ noBackslashEscapes: boolean;
105
+ /** sql_mode has ANSI_QUOTES: split the file with double quotes around names. */
106
+ ansiQuotes: boolean;
107
+ /** The character set the session reads statements in, by its name in small letters. */
108
+ characterSet: string;
109
+ /** The database selected: the one asked for. */
110
+ database: string | null;
111
+ limits: ImportLimits;
112
+ }
113
+ /** What import-run takes in its URL, beside the batch in its body. */
114
+ export interface ImportRunQuery {
115
+ connection: string;
116
+ import: string;
117
+ /**
118
+ * The number of the batch's first statement: always the one after the last done, whether it
119
+ * is sent or left out by `skip`.
120
+ */
121
+ start: number;
122
+ /** How many frames the body holds: 1 to 1,000, or with `skip`, 0 to 1,000 (0 has no body). */
123
+ count: number;
124
+ /** 1 when the batch was split with NO_BACKSLASH_ESCAPES on, 0 with it off. */
125
+ nbe: 0 | 1;
126
+ /** 1 when the batch was split with ANSI_QUOTES on, 0 with it off. */
127
+ aq: 0 | 1;
128
+ /**
129
+ * The number of a refused statement to leave out, as `mysql --force` would. The batch is then
130
+ * statements `start` to `start` + `count` less this one: its frames are the others in the
131
+ * file's order, and it counts as done once those before it are. So it is `start` at the least
132
+ * and `start` + `count` at the most (see `batchNumbers` in frames.ts).
133
+ *
134
+ * After a failure that is `uncommitted` or `committed` it is `start` itself. After one that is
135
+ * `rolled-back`, `start` is the transaction's first statement, sent again with those after it,
136
+ * and `skip` lies further on: in a later batch when they don't all fit in this one. When no
137
+ * statement is left to send with it, `count` is 0.
138
+ *
139
+ * Taken only for a statement the server refused and that hasn't been done or left out since:
140
+ * the server remembers each until then, also while those before it run again.
141
+ */
142
+ skip?: number;
143
+ }
144
+ /** Which statements of the file a batch is, as its URL says. */
145
+ export type ImportBatch = Pick<ImportRunQuery, 'start' | 'count' | 'skip'>;
146
+ /** One line of import-run's answer. The last is `done`, `failed`, `lost` or `stopped`. */
147
+ export type ImportLine = ImportAcceptedLine | ImportStatementLine | ImportBeatLine | ImportModeLine | ImportDoneLine | ImportFailedLine | ImportLostLine | ImportStoppedLine;
148
+ /** The first line: the batch was read whole and checked, and its first statement is next. */
149
+ export interface ImportAcceptedLine {
150
+ type: 'accepted';
151
+ start: number;
152
+ count: number;
153
+ }
154
+ /** Statement `n` ran. The server counted it before it wrote this. */
155
+ export interface ImportStatementLine {
156
+ type: 'statement';
157
+ /** Its number in the file: with `skip`, the numbers step over the statement left out. */
158
+ n: number;
159
+ /** Rows it changed. */
160
+ affected: number;
161
+ warnings: number;
162
+ /** Rows it returned, counted and not kept. */
163
+ rows: number;
164
+ /** How long it took, in milliseconds. */
165
+ ms: number;
166
+ }
167
+ /** Statement `n` has been running for `ms` milliseconds: written every 10 seconds while it does. */
168
+ export interface ImportBeatLine {
169
+ type: 'beat';
170
+ n: number;
171
+ ms: number;
172
+ }
173
+ /**
174
+ * Statement `after` changed the session's reading: how it reads strings, how its character set
175
+ * reads the file's text (another set that reads it the same doesn't count), or the database
176
+ * selected. So the batch ended there: the rest of it, split and checked under the old reading,
177
+ * did not run. `done` follows. The browser splits the file again from `after` + 1 under these.
178
+ * Written when `after` is the batch's last statement too: the next batch is sent under these
179
+ * all the same.
180
+ */
181
+ export interface ImportModeLine {
182
+ type: 'mode';
183
+ after: number;
184
+ noBackslashEscapes: boolean;
185
+ ansiQuotes: boolean;
186
+ /** The character set the session reads statements in now, by its name in small letters. */
187
+ characterSet: string;
188
+ /** The database selected now; null with none. */
189
+ database: string | null;
190
+ }
191
+ /** The batch ended with every statement it ran done; the next batch starts at `done` + 1. */
192
+ export interface ImportDoneLine {
193
+ type: 'done';
194
+ /**
195
+ * The number of the last statement done or left out: the batch's last, or after a `mode`
196
+ * line, that line's `after`.
197
+ */
198
+ done: number;
199
+ /** The file has a transaction open. */
200
+ inTransaction: boolean;
201
+ /** With `inTransaction`: where it began, as ImportStatus's `since` says. */
202
+ since?: number;
203
+ }
204
+ /**
205
+ * Statement `n` was refused, and the batch stopped there. Unless `ended`, the import waits for a
206
+ * choice, for as long as `pauseMs`: run again from `done` + 1, do the same and leave `n` out
207
+ * (`skip`), or end the import.
208
+ */
209
+ export interface ImportFailedLine {
210
+ type: 'failed';
211
+ n: number;
212
+ error: ImportError;
213
+ state: ImportFailureState;
214
+ /**
215
+ * The number of the last statement that still counts as done: `n` − 1, or with `rolled-back`
216
+ * and `unknown`, `since` − 1.
217
+ */
218
+ done: number;
219
+ /**
220
+ * With `uncommitted`, `rolled-back` and `unknown`: where the file's transaction began.
221
+ * - `uncommitted`: where the one open now began, as ImportStatus's `since` says: `n` itself
222
+ * when none was open before it. When one was, `since` stays where it was, whatever kind of
223
+ * statement `n` is (see ImportFailureState).
224
+ * - `rolled-back`: the first statement of the one the server undid, which held every
225
+ * statement from there to `n`.
226
+ * - `unknown`: the first of the statements of which it is not known whether they were kept.
227
+ */
228
+ since?: number;
229
+ /**
230
+ * The import is over, closed by the server: there is no choice to make. With `rolled-back`,
231
+ * when what was undone can't be run again as it first ran, and always with `unknown` (see
232
+ * ImportFailureState).
233
+ */
234
+ ended?: true;
235
+ }
236
+ /**
237
+ * What became of the statements before a failed one, as the server said when asked after it.
238
+ * A commit is never called a rollback, nor a rollback kept: where it can't be told, it is
239
+ * `unknown`.
240
+ * - `uncommitted`: a transaction is open. Either none was open before the failed statement, or
241
+ * that statement can't have rolled it back: it is a plain one (see below), a SET, or of a
242
+ * kind that commits first (but for an error a failed commit is known to give: see `unknown`);
243
+ * or it was never sent (`unsent`). What ran in it from `since` isn't committed yet, or that
244
+ * statement committed it.
245
+ * One of a kind that commits first was only seen to fail and leave a transaction open when
246
+ * the server refused it before its commit (a syntax error, a collation it doesn't know), on
247
+ * MySQL and on MariaDB: it committed nothing, and the transaction is the one from `since`.
248
+ * Nothing in the answer says so, though. So from then on nothing from `since` is called
249
+ * undone: the import's end leaves out ImportEnded's `since`, and a later failure that would
250
+ * be `rolled-back` is `unknown`.
251
+ * - `rolled-back`: the transaction is gone after a plain statement failed (see below), and no
252
+ * statement from its first on may have committed on its way (see below). A plain statement
253
+ * can't commit, so the server rolled back the one transaction that held every statement from
254
+ * `since` to `n`: on a deadlock, on a lock wait that ran out where it is set to, when its lock
255
+ * table is full. The error's number doesn't decide it. What every statement from `since` on
256
+ * did to rows is undone, but for rows in tables without transactions, and what it changed in
257
+ * the session is not (both below). They must be sent again to be run.
258
+ * - `committed`: no transaction is open, and what ran before is kept. Either none was open
259
+ * before the failed statement, and each one was committed as it ran; or the failed statement
260
+ * is of a kind that commits what is open before it does anything else, as CREATE, ALTER, DROP,
261
+ * RENAME, TRUNCATE, GRANT, REVOKE and LOCK TABLES do, and its error is one known to come from
262
+ * its own work after that commit: a table that exists or doesn't, a definition the server
263
+ * refuses, a privilege the login lacks, data an ALTER can't convert, and the like, each seen
264
+ * so on MySQL 8.4 or MariaDB 11.4. After any other error, see `unknown`.
265
+ * - `unknown`: the failed statement is one that may have ended the file's transaction either
266
+ * way: a CALL, an EXECUTE, a SET STATEMENT … FOR or a compound statement can commit inside, or
267
+ * roll back in a handler and pass the error on. Either the transaction is gone after it, or
268
+ * one is open that may be another: a procedure can roll back and begin anew, and the server
269
+ * only says that one is open, not which. Whether the statements from `since` to `n` − 1 were
270
+ * kept is not known, so they are neither counted as done nor offered again: the import is
271
+ * always `ended`, which rolls back what is open, and `done` is `since` − 1. Any statement that
272
+ * isn't known to be unable to roll back is taken for one that may have: one behind a comment
273
+ * too, and a COMMIT or a ROLLBACK that failed. No error's number makes such a failure
274
+ * `rolled-back`: a procedure can commit and then fail with a deadlock's.
275
+ *
276
+ * It is also the answer when the transaction is gone after a plain statement, and a statement
277
+ * from its first on may have committed on its way and begun another: a second START
278
+ * TRANSACTION, COMMIT AND CHAIN, ROLLBACK AND CHAIN, a CALL, an EXECUTE, whether it opened the
279
+ * transaction or ran inside it; or one of a kind that commits first that failed inside it, and
280
+ * left it open (see `uncommitted`). The server only undid the one open last, and doesn't say
281
+ * where that began: some of the statements from `since` may be committed, and what is
282
+ * committed must not run twice. Only a plain statement, a SET, and as the transaction's first
283
+ * BEGIN, START TRANSACTION or a statement of a kind that commits first (which commits what was
284
+ * before it) are known to have committed nothing in it. A SET can commit, but then no
285
+ * transaction is open after it.
286
+ *
287
+ * And when a statement of a kind that commits first fails, the transaction is gone after it,
288
+ * and its error is not one known to come only after that commit (see `committed`). That
289
+ * commit may be what failed, and the server undid the transaction; or the commit held, and
290
+ * the statement's own work failed after it. The server doesn't say which, and what a failed
291
+ * commit is answered with has no end: the engine's 1180, a replication hook's 3100, the waits
292
+ * for another session's FLUSH TABLES WITH READ LOCK (1205, and 1317 or MariaDB's 1969 when
293
+ * that wait is cut short), a server gone read-only (1290), 1105 from MySQL's commit and from
294
+ * MariaDB's Galera past its write-set limit, Galera's 1235 and 1047, the binary log's 1026
295
+ * and 1024, a class-40 SQLSTATE (a server in a cluster refuses a commit that another
296
+ * server's conflicts with), and whatever a plugin or a cluster adds. With a transaction open
297
+ * after such a statement, the answer is `unknown` too when its error is one of these known
298
+ * ones: class 40, 1180, 3100, 1205, 1317, 1969 and 1290.
299
+ *
300
+ * A statement the import refused itself and never sent (its error says `unsent`) changed
301
+ * nothing: its failure is `uncommitted` with the same `since` while a transaction is open, and
302
+ * `committed` while none is, whatever kind of statement it is. Never `rolled-back` or `unknown`,
303
+ * and the import waits for a choice.
304
+ *
305
+ * A rollback undoes rows and nothing of the session. When a statement it undid had changed how
306
+ * the session reads strings, or which database is selected, the statements from `since` would
307
+ * run a second time under what a later one set: split differently than they first ran, or into
308
+ * another database. The server then ends the import with the failure (`ended`) instead of
309
+ * waiting for a choice.
310
+ *
311
+ * It does the same when what the transaction held can't be sent again to do what it first did.
312
+ * Only a transaction that BEGIN, START TRANSACTION or a plain statement opened, and that held
313
+ * plain statements alone (INSERT, REPLACE, UPDATE, DELETE, SELECT, DO, USE, SAVEPOINT,
314
+ * RELEASE), is offered again. A SET is not plain: what it changed in the session (the character
315
+ * set, the time zone, a variable) stays changed after the rollback. Nor is a statement of a
316
+ * kind that commits first that opened it, nor a transaction that was open before the import's
317
+ * first statement, which nothing of the file opened.
318
+ *
319
+ * What a rerun still can't put back, for the dialog to say: rows the undone statements put
320
+ * into tables that have no transactions (MyISAM, Aria), which the server kept, and a user
321
+ * variable a statement other than SET assigned (`@n := @n + 1`, SELECT … INTO @n).
322
+ */
323
+ export type ImportFailureState = 'uncommitted' | 'rolled-back' | 'committed' | 'unknown';
324
+ /** The connection went while the batch ran: the import is over, and what was open is undone. */
325
+ export interface ImportLostLine {
326
+ type: 'lost';
327
+ done: number;
328
+ /** The number of the statement that was running: whether it finished is not known. */
329
+ doubt?: number;
330
+ }
331
+ /** The import was ended while the batch ran: its statement was stopped, and no other started. */
332
+ export interface ImportStoppedLine {
333
+ type: 'stopped';
334
+ done: number;
335
+ }
336
+ /**
337
+ * Where an import is, from the server's memory alone: asking runs nothing on the database.
338
+ * - `idle`: open, waiting for the next batch.
339
+ * - `running`: a batch is running, whether or not anyone still reads its answer. With `ending`,
340
+ * the server is closing the import, with or without a batch: ask again for how it ended.
341
+ * - `failed`: a statement was refused, and the import waits for a choice for `pauseLeftMs`.
342
+ * Never said of an import the server has begun to end: a choice would be refused.
343
+ * - `ended`: closed, by import-end, by a limit, or on a failure it could not go on after; a
344
+ * transaction left open was rolled back.
345
+ * - `lost`: the connection went.
346
+ */
347
+ export interface ImportStatus {
348
+ state: 'idle' | 'running' | 'failed' | 'ended' | 'lost';
349
+ /**
350
+ * The number of the last statement done or left out, 0 before the first: the next batch
351
+ * starts after it.
352
+ */
353
+ done: number;
354
+ /** With `running`: the statement the server is waiting on, and for how long, in milliseconds. */
355
+ running?: {
356
+ n: number;
357
+ ms: number;
358
+ };
359
+ /**
360
+ * The statement that was refused, as its `failed` line said: with `failed`; with `running`
361
+ * while the batch it stopped still writes its last line, and while the import closes
362
+ * (`ending`); and with `ended` or `lost` when the import closed before a choice was made for
363
+ * it. Once the import has closed, the error is its numbers alone, with `ERROR 1062 (23000)`
364
+ * as its message: the server's words quote the file, and nothing of the file is kept. Its
365
+ * `unsent` stays.
366
+ *
367
+ * With `failed`, `ended` and `lost` it leaves the import where its `failed` line did, and the
368
+ * browser holds a status to that (with `running` the batch that met it may still be ending,
369
+ * and nothing is taken from it):
370
+ * - `since`, when there is one, is `n` at the most;
371
+ * - with `rolled-back` and `unknown`, `since` is there and `done` is `since` − 1;
372
+ * - otherwise `done` is `n` − 1;
373
+ * - `unknown` is never said with `failed`: the server has ended the import on it;
374
+ * - a statement that was never sent (`error.unsent`) is never `unknown` or `rolled-back`.
375
+ */
376
+ failure?: {
377
+ n: number;
378
+ error: ImportError;
379
+ state: ImportFailureState;
380
+ };
381
+ /** The file has a transaction open. */
382
+ inTransaction: boolean;
383
+ /**
384
+ * Where the file's transaction began: with `inTransaction`; with a failure that is
385
+ * `rolled-back` or `unknown`, also once the import has `ended` on it; and with `rolledBack`
386
+ * when that is known.
387
+ *
388
+ * With `inTransaction` it is the statement that opened the one open now, or, when a statement
389
+ * since may have committed on its way and begun another (a second START TRANSACTION, a CALL,
390
+ * a CREATE that failed), the earliest that one can have begun at: some statements from there
391
+ * may be committed. After a `rolled-back` failure it is the first of those the server undid.
392
+ * After an `unknown` one it is where the statements begin of which it is not known whether
393
+ * they were kept. With `rolledBack` and no such failure, it is where the transaction the end
394
+ * rolled back began, and it is left out where that isn't known (see ImportEnded).
395
+ */
396
+ since?: number;
397
+ /**
398
+ * sql_mode has NO_BACKSLASH_ESCAPES, as the server last heard from the session: what the next
399
+ * batch is split under, and says in `nbe`. The browser takes it from here after a refusal or
400
+ * a cut stream, where a `mode` line may never have arrived.
401
+ */
402
+ noBackslashEscapes: boolean;
403
+ /** The same for ANSI_QUOTES, and `aq`. */
404
+ ansiQuotes: boolean;
405
+ /** The character set the session reads statements in, as the server last heard: see ImportModeLine. */
406
+ characterSet: string;
407
+ /** The database selected, as the server last heard; null with none. */
408
+ database: string | null;
409
+ /**
410
+ * With `failed`: how long the choice still has, in milliseconds, as the server answers. The
411
+ * browser counts down from when the answer arrives: its clock is not the server's.
412
+ */
413
+ pauseLeftMs?: number;
414
+ /**
415
+ * With `running`: the server is ending the import, by import-end, by one of its limits, or on
416
+ * a failure it could not go on after. What runs is being stopped and what is open rolled
417
+ * back. No batch is taken from here on (409 `import-ended`) and no choice is waited for, so
418
+ * neither `idle` nor `failed` is said any more. The next answer that isn't this one is
419
+ * `ended` or `lost`, with how far the import got.
420
+ */
421
+ ending?: true;
422
+ /** With `ended`: a transaction was open at the end, and was rolled back (see `since`). */
423
+ rolledBack?: boolean;
424
+ /** With `lost` and `ended`: the statement that was running, which may not have finished. */
425
+ doubt?: number;
426
+ }
427
+ /**
428
+ * What import-end is sent: the import's id, from the dialog that started it; or `current`, from
429
+ * any other window of the same viewer, for their open import on the connection. An import whose
430
+ * dialog is gone (a tab that crashed, a page that was reloaded) is ended that way, and not left
431
+ * to hold its connection's locks until `idleMs` is over.
432
+ *
433
+ * Both end it the same, and are answered the same. With `current` and no import open, the
434
+ * answer is the 404 of an import nobody knows, also when one of the viewer's ended a moment
435
+ * ago: how that one ended is import-options' `current` to say.
436
+ */
437
+ export type ImportEndRequest = {
438
+ connection: string;
439
+ import: string;
440
+ current?: never;
441
+ } | {
442
+ connection: string;
443
+ /** `true`; 1 is taken for it too. */
444
+ current: true | 1;
445
+ import?: never;
446
+ };
447
+ /** How an import ended: what ran is kept up to `done`, less a transaction left open. */
448
+ export interface ImportEnded {
449
+ /** The number of the last statement done. */
450
+ done: number;
451
+ /** A transaction the file left open was rolled back, as the `mysql` client does at its end. */
452
+ rolledBack: boolean;
453
+ /**
454
+ * With `rolledBack`: the number of that transaction's first statement, which with all after
455
+ * it up to `done` was undone. Left out when that isn't known: a statement in it may have
456
+ * committed and begun the one rolled back (a second START TRANSACTION, a CALL, a CREATE that
457
+ * failed), and then statements from where it seemed to begin may be kept.
458
+ */
459
+ since?: number;
460
+ /** The statement that was running and would not stop: whether it finished is not known. */
461
+ doubt?: number;
462
+ }
@@ -0,0 +1,72 @@
1
+ /*
2
+ * What the import endpoints and the browser say to each other, in one place so neither can drift
3
+ * from the other. Constants and types only, nothing from Node: the browser app takes it as it
4
+ * is, with frames.ts, as `@otwdesign/db-core/import`.
5
+ *
6
+ * The browser reads and splits the file; the server holds one connection for the whole import and
7
+ * keeps the position. Statements are numbered from 1, in the file's order.
8
+ *
9
+ * GET import-options?connection= ImportOptions
10
+ * GET import-status?connection=&import= ImportStatus
11
+ * POST import-start ImportStartRequest, as JSON ImportStarted
12
+ * POST import-end ImportEndRequest, as JSON ImportEnded
13
+ * POST import-run?… ImportRunQuery in the URL, and a batch of frames as the body (see
14
+ * frames.ts); answers lines of JSON, an ImportLine each
15
+ *
16
+ * A refusal is the API's usual `{ error: { code, message } }`. import-run refuses before its
17
+ * first line, and nothing of a refused batch runs. Before it reads the body, from what the
18
+ * server holds in memory:
19
+ *
20
+ * 404 an import it doesn't know (another viewer's is unknown too)
21
+ * 400 a URL that isn't a batch's (see `batchNumbers` in frames.ts)
22
+ * 409 the import has ended, or the server is ending it; or another batch of the import is
23
+ * running; or `start` isn't the statement after the last done; or `nbe` and `aq` aren't
24
+ * the session's sql_mode as it is now; or `skip` isn't a statement the server refused
25
+ *
26
+ * Then, of the body:
27
+ *
28
+ * 408 it didn't arrive whole within IMPORT_BODY_MS
29
+ * 413 it is past IMPORT_BATCH_BYTES
30
+ * 400 it isn't the batch the URL says (see `decodeFrames`)
31
+ *
32
+ * The browser never sends a batch again on its own. After a refusal, or a stream that stopped
33
+ * without a last line, it asks import-status and goes on from what the server says there: its
34
+ * position, and how the session reads strings.
35
+ *
36
+ * Where a statement ends depends on two things in the session's sql_mode (NO_BACKSLASH_ESCAPES
37
+ * and ANSI_QUOTES), and a file can change them. Text split under one and run under the other is
38
+ * other statements: the end of a string becomes SQL. So a batch says what it was split under
39
+ * (`nbe`, `aq`), and the server compares that with the session before every statement, the
40
+ * batch's first included. Before the first, a difference is the 409 above. After a statement
41
+ * that changed it, the batch ends there with a `mode` line.
42
+ *
43
+ * What the browser's check showed the person depends on more of the session: those two, how its
44
+ * character set reads the file's text (see `readsAs`), which decides what the import sends, and
45
+ * the database selected, where statements that name none go. That is the session's reading. A
46
+ * batch ends after a statement that changed any of it, with a `mode` line that says it whole;
47
+ * import-start and import-status say it too. A `failed` line doesn't: a statement refused can
48
+ * have changed it first, so the browser asks import-status before it goes on. The browser sends
49
+ * a batch only while the reading the server says is the one the check took for each of its
50
+ * statements: where they differ, it has the person read the rest of the file checked again
51
+ * before anything more is sent.
52
+ */
53
+ /** A batch's body at most, in bytes: 4 MiB, lengths included. */
54
+ export const IMPORT_BATCH_BYTES = 4_194_304;
55
+ /** A batch's statements at most. */
56
+ export const IMPORT_BATCH_STATEMENTS = 1_000;
57
+ /** What a frame's length takes, before its statement: four bytes, the big end first. */
58
+ export const IMPORT_LENGTH_BYTES = 4;
59
+ /** One statement at most, in bytes of UTF-8: a batch to itself. A longer one can't be sent. */
60
+ export const IMPORT_STATEMENT_BYTES = IMPORT_BATCH_BYTES - IMPORT_LENGTH_BYTES;
61
+ /**
62
+ * How often import-run writes a line while one statement runs, in milliseconds. A proxy or a
63
+ * browser that sees nothing for longer has lost the stream, not a slow statement.
64
+ */
65
+ export const IMPORT_HEARTBEAT_MS = 10_000;
66
+ /**
67
+ * How long a batch's body has to arrive whole, in milliseconds: 2 minutes. The import is marked
68
+ * busy while it is read, and its connection may hold locks, so a body that stops is let go. A
69
+ * line too slow for 4 MiB in that time is too slow for the file.
70
+ */
71
+ export const IMPORT_BODY_MS = 120_000;
72
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/import/protocol.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AAEH,iEAAiE;AACjE,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAA;AAE3C,oCAAoC;AACpC,MAAM,CAAC,MAAM,uBAAuB,GAAG,KAAK,CAAA;AAE5C,wFAAwF;AACxF,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAA;AAEpC,+FAA+F;AAC/F,MAAM,CAAC,MAAM,sBAAsB,GAAG,kBAAkB,GAAG,mBAAmB,CAAA;AAE9E;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAA;AAEzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,OAAO,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './charsets.ts';
2
+ export * from './frames.ts';
3
+ export * from './protocol.ts';
@@ -0,0 +1,9 @@
1
+ /*
2
+ * What the browser app takes of an import, as `@otwdesign/db-core/import`: the batch's format,
3
+ * the protocol's limits and its types. An entry of its own, as `format` is, so the app's build
4
+ * finds it in the core's source and never in the server's entry. Nothing here is from Node.
5
+ */
6
+ export * from "./charsets.js";
7
+ export * from "./frames.js";
8
+ export * from "./protocol.js";
9
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/import/shared.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA"}
package/dist/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export type { SchemaChoice } from './backup/dump.ts';
16
16
  export type { BackupHeader, BackupSummary } from './backup/format.ts';
17
17
  export type { PgTool } from './backup/pg-tools.ts';
18
18
  export type { RestorePlan, RestoreResult } from './backup/restore.ts';
19
+ export type { ImportAcceptedLine, ImportBatch, ImportBeatLine, ImportDatabase, ImportDoneLine, ImportEnded, ImportEndRequest, ImportFailedLine, ImportFailureState, ImportLimits, ImportLine, ImportLostLine, ImportModeLine, ImportOptions, ImportRunQuery, ImportStarted, ImportStartRequest, ImportStatementLine, ImportStatus, ImportStoppedLine, } from './import/protocol.ts';