@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
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
import type { Connection as CoreConnection } from 'mysql2'
|
|
2
|
+
import type { ConnectionOptions } from 'mysql2/promise'
|
|
3
|
+
import { ConnectionError, DbAdminError, QueryError } from '../errors.ts'
|
|
4
|
+
import type {
|
|
5
|
+
ImportClosed,
|
|
6
|
+
Importer,
|
|
7
|
+
ImportError,
|
|
8
|
+
ImportResult,
|
|
9
|
+
ImportSession,
|
|
10
|
+
ImportTarget,
|
|
11
|
+
} from '../types.ts'
|
|
12
|
+
import { READ_ALIKE, READ_ALIKE_IN_ASCII } from '../import/charsets.ts'
|
|
13
|
+
import { backtick, serverOf } from './mysql.ts'
|
|
14
|
+
import {
|
|
15
|
+
connectionOptions,
|
|
16
|
+
killQuery,
|
|
17
|
+
watchStatement,
|
|
18
|
+
type KillOutcome,
|
|
19
|
+
type MySql,
|
|
20
|
+
type StatementWatch,
|
|
21
|
+
} from './mysql-connection.ts'
|
|
22
|
+
import { whole } from './sql.ts'
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* One import's connection to MySQL or MariaDB: the file's statements, one a round trip, on a
|
|
26
|
+
* session that is the file's alone from its first statement to its last. Loaded when the first
|
|
27
|
+
* import opens.
|
|
28
|
+
*
|
|
29
|
+
* What the session is after a statement is never read from the statement's text. The server
|
|
30
|
+
* says it: whether a transaction is open with every answer, and the rest (the sql_mode, with
|
|
31
|
+
* NO_BACKSLASH_ESCAPES and ANSI_QUOTES or without, the character set statements are read in, and
|
|
32
|
+
* the database selected) when it is asked, which is whenever an answer says the session changed,
|
|
33
|
+
* or could not say. An answer that says nothing is taken for "nothing changed" only on a
|
|
34
|
+
* connection that has shown it does say, for each way a file can set a setting: on any other,
|
|
35
|
+
* MariaDB's among them, the server is asked after every statement.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/** The URL's options an import's connection takes. Where and who are the URL's own parts. */
|
|
39
|
+
const URL_OPTIONS: ReadonlySet<string> = new Set(['ssl', 'socketPath', 'connectTimeout'])
|
|
40
|
+
|
|
41
|
+
/** The answer to two statements sent as one, from a connection that runs one at a time. */
|
|
42
|
+
const SYNTAX_ERROR = 1064
|
|
43
|
+
/** "Query was empty": the statement is nothing on this server. */
|
|
44
|
+
const EMPTY_QUERY = 1065
|
|
45
|
+
/** "Got a packet bigger than 'max_allowed_packet' bytes", after which the server hangs up. */
|
|
46
|
+
const PACKET_TOO_LARGE = 1153
|
|
47
|
+
/**
|
|
48
|
+
* "This version of MySQL doesn't yet support …": the server's number for what it does not take,
|
|
49
|
+
* and the nearest to a statement an import does not send.
|
|
50
|
+
*/
|
|
51
|
+
const NOT_SUPPORTED = 1235
|
|
52
|
+
/** What a statement's packet holds beside its text, at most: the command, and two counts. */
|
|
53
|
+
const PACKET_HEAD = 3
|
|
54
|
+
/**
|
|
55
|
+
* The most a statement's packet holds here, in bytes, whatever the server takes: four short of
|
|
56
|
+
* what one packet of the protocol does (16 MiB less one). mysql2 (3.24.6) sends an empty packet
|
|
57
|
+
* after one that comes closer than that, which the server reads as the next statement, and it
|
|
58
|
+
* answers "Got packets out of order" to everything after. Anything longer goes as several
|
|
59
|
+
* packets, which no import needs: the API takes 4 MiB of statements at most in one request.
|
|
60
|
+
*/
|
|
61
|
+
const PACKET_MOST = 0xffffff - 4
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The server's status with every OK: a transaction is open. The status has a bit for
|
|
65
|
+
* NO_BACKSLASH_ESCAPES too (0x0200), which is not read: the server sets it where a SET sql_mode
|
|
66
|
+
* runs and leaves it when a routine that ran one returns and its caller's mode is put back, so
|
|
67
|
+
* from then on it is wrong with every answer.
|
|
68
|
+
*/
|
|
69
|
+
const IN_TRANSACTION = 0x0001
|
|
70
|
+
/** Something of the session changed with this statement, as far as the server reports it. */
|
|
71
|
+
const SESSION_CHANGED = 0x4000
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* How long a statement that was told to stop has to answer, from when its kill was answered or
|
|
75
|
+
* given up on, before its connection is closed under it; and how long the rollback at the end
|
|
76
|
+
* has.
|
|
77
|
+
*/
|
|
78
|
+
const REPLY_MS = 120_000
|
|
79
|
+
/** The time limit of the connection's own statements, which ask and change nothing. */
|
|
80
|
+
const OWN_MS = 30_000
|
|
81
|
+
/** How long the server has to hang up after the goodbye. */
|
|
82
|
+
const GOODBYE_MS = 5_000
|
|
83
|
+
/** How long an import may sit unused, unless whoever opens it says: 5 minutes. */
|
|
84
|
+
const IDLE_MS = 300_000
|
|
85
|
+
/** What the server keeps an unused connection for beyond that, in seconds. */
|
|
86
|
+
const IDLE_MARGIN_S = 60
|
|
87
|
+
/** The longest wait_timeout a server takes, in seconds: a year. */
|
|
88
|
+
const WAIT_MOST_S = 31_536_000
|
|
89
|
+
/** The longest wait a timer keeps, in milliseconds: past it, it fires at once. */
|
|
90
|
+
const TIMER_MOST_MS = 2 ** 31 - 1
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* What the server reports of the session with each answer: that something of it changed, which
|
|
94
|
+
* database is selected, and the settings in the list. Each of the two tells when the other is
|
|
95
|
+
* turned off: the list names the switch of the report of changes, and the report of changes
|
|
96
|
+
* tells of any change to the list, or to the report of the database. So a file can't turn one
|
|
97
|
+
* off unseen. The rest of the list is what a file's statements are read by: sql_mode,
|
|
98
|
+
* autocommit, and the character set, which mysql2 follows (see `#answer`) and which decides
|
|
99
|
+
* what is sent (see `#misread`).
|
|
100
|
+
*/
|
|
101
|
+
const REPORTS = [
|
|
102
|
+
'session_track_schema = ON',
|
|
103
|
+
'session_track_state_change = ON',
|
|
104
|
+
"session_track_system_variables = 'sql_mode,character_set_client,autocommit,session_track_state_change'",
|
|
105
|
+
].join(', ')
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Sets a setting of the list to what it is, in each way a file can name one: nothing of the
|
|
109
|
+
* session changes, and the server reports the setting as changed all the same, wherever it
|
|
110
|
+
* reports that way at all.
|
|
111
|
+
*
|
|
112
|
+
* The settings above say what the server would report, not whether it does. It reports only to a
|
|
113
|
+
* client it granted that at the handshake (CLIENT_SESSION_TRACK), which a proxy in between may
|
|
114
|
+
* not pass on: every SET above still succeeds then and reads back as set, and no answer ever
|
|
115
|
+
* says the session changed. And it may report some ways and not others: MariaDB (11.4) does not
|
|
116
|
+
* report a SET that names each of its settings as @@name, whether plain, in a comment for
|
|
117
|
+
* versions, prepared or in an EXECUTE IMMEDIATE. So a report of each way has to be seen to arrive.
|
|
118
|
+
*/
|
|
119
|
+
const PROOFS = [
|
|
120
|
+
'SET SESSION sql_mode = @@SESSION.sql_mode',
|
|
121
|
+
'SET sql_mode = @@SESSION.sql_mode',
|
|
122
|
+
'SET @@SESSION.sql_mode = @@SESSION.sql_mode',
|
|
123
|
+
'SET @@sql_mode = @@SESSION.sql_mode',
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
/** A character beyond ASCII. */
|
|
127
|
+
const BEYOND_ASCII = /\P{ASCII}/u
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The connection's character set once more, by name: the server then gives the session its own
|
|
131
|
+
* collation for utf8mb4, as the `mysql` client's has. The handshake names a collation by number,
|
|
132
|
+
* and no number is every server's own; mysql2's is utf8mb4_unicode_ci. Under that one, a file
|
|
133
|
+
* with no SET NAMES of its own has its variables and routines made in another collation than
|
|
134
|
+
* its columns, and a `WHERE name = @name` is refused as an illegal mix of the two (1267).
|
|
135
|
+
*/
|
|
136
|
+
const NAMES = 'SET NAMES utf8mb4'
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A question of the connection's own, answered with one row whatever the session has set for
|
|
140
|
+
* its SELECTs. Each value as bytes: a file may ask for its results in any character set, and
|
|
141
|
+
* bytes are sent as they are. Its own LIMIT comes before the session's sql_select_limit, which
|
|
142
|
+
* at 0 would leave it no row.
|
|
143
|
+
*/
|
|
144
|
+
const question = (values: string[]) =>
|
|
145
|
+
`SELECT ${values.map((value) => `CAST(${value} AS BINARY)`).join(', ')} LIMIT 1`
|
|
146
|
+
|
|
147
|
+
/** The session as only asking tells it, and the reports as they are now. */
|
|
148
|
+
const ASK = question([
|
|
149
|
+
'@@SESSION.sql_mode',
|
|
150
|
+
'DATABASE()',
|
|
151
|
+
'@@SESSION.character_set_client',
|
|
152
|
+
'@@SESSION.session_track_system_variables',
|
|
153
|
+
'@@SESSION.session_track_state_change',
|
|
154
|
+
'@@SESSION.session_track_schema',
|
|
155
|
+
])
|
|
156
|
+
|
|
157
|
+
/** The connection as it opened. */
|
|
158
|
+
const OPENED = question([
|
|
159
|
+
'CONNECTION_ID()',
|
|
160
|
+
'VERSION()',
|
|
161
|
+
'@@SESSION.max_allowed_packet',
|
|
162
|
+
'@@SESSION.wait_timeout',
|
|
163
|
+
])
|
|
164
|
+
|
|
165
|
+
const STUCK =
|
|
166
|
+
'The statement did not stop when it was asked to, so the import’s connection was closed under it. Whether it finished is not known.'
|
|
167
|
+
const KILL_UNANSWERED =
|
|
168
|
+
'A request to stop a statement got no answer and could still stop a later one, so the import’s connection was closed.'
|
|
169
|
+
|
|
170
|
+
/** What mysql2 says of a statement that failed. */
|
|
171
|
+
interface Failure {
|
|
172
|
+
errno?: number
|
|
173
|
+
sqlState?: string
|
|
174
|
+
sqlMessage?: string
|
|
175
|
+
code?: string
|
|
176
|
+
message?: string
|
|
177
|
+
fatal?: boolean
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A statement's answer, as far as an import reads it. */
|
|
181
|
+
interface Reply {
|
|
182
|
+
/** The server's no. */
|
|
183
|
+
error?: ImportError
|
|
184
|
+
/** The status of the OK the answer ended with. None when it ended with rows. */
|
|
185
|
+
status?: number
|
|
186
|
+
affected: number
|
|
187
|
+
/** Of the OK the answer ended with: mysql2 doesn't tell those of an answer that ended with rows. */
|
|
188
|
+
warnings: number
|
|
189
|
+
/** Rows returned, counted. */
|
|
190
|
+
rows: number
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A row as it arrived: each value's bytes. */
|
|
194
|
+
type Row = (Buffer | null)[]
|
|
195
|
+
|
|
196
|
+
/** Sends one statement of the connection's own and answers for it; `row` is given each row. */
|
|
197
|
+
type Send = (sql: string, row: (values: Row) => void) => Promise<Reply>
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* One connection's settings for an import of the server at `url`. Not the driver's own (see
|
|
201
|
+
* `connectionOptions`): a file must run as it would in the `mysql` client, so nothing of the URL
|
|
202
|
+
* that changes how statements are read or sent gets through, its character set, flags and time
|
|
203
|
+
* zone among them.
|
|
204
|
+
*/
|
|
205
|
+
export function importOptions(url: string): ConnectionOptions {
|
|
206
|
+
// mysql2 reads mysql:// addresses; mariadb:// is the same thing.
|
|
207
|
+
const address = new URL(url.replace(/^mariadb:/i, 'mysql:'))
|
|
208
|
+
for (const key of [...address.searchParams.keys()]) {
|
|
209
|
+
if (!URL_OPTIONS.has(key)) address.searchParams.delete(key)
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
uri: address.href,
|
|
213
|
+
// One statement a query, whoever asked otherwise: mysql2 drops a flag taken away. And as the
|
|
214
|
+
// `mysql` client connects: a function's name stays a name before a space, rows changed are
|
|
215
|
+
// counted and not rows matched, and the server can't ask this process for a file.
|
|
216
|
+
flags: ['-MULTI_STATEMENTS', '-IGNORE_SPACE', '-FOUND_ROWS', '-LOCAL_FILES'],
|
|
217
|
+
// For its character set, the wire's. The session's collation is set as it opens (see NAMES).
|
|
218
|
+
charset: 'UTF8MB4_UNICODE_CI',
|
|
219
|
+
connectAttributes: { program_name: 'otw-db' },
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Opens one import's connection to the server at `url`, with `mysql` as mysql2. It answers once
|
|
225
|
+
* the connection has proved it runs one statement a query, and the server reports its session.
|
|
226
|
+
*
|
|
227
|
+
* `idleMs` is how long the import may sit unused between statements: the server is asked to keep
|
|
228
|
+
* the connection a minute longer than that, where it would drop it sooner. `closed` is called
|
|
229
|
+
* once, when the connection has gone, however it went.
|
|
230
|
+
*/
|
|
231
|
+
export async function openImporter(
|
|
232
|
+
mysql: MySql,
|
|
233
|
+
url: string,
|
|
234
|
+
{ idleMs = IDLE_MS, closed = () => {} }: { idleMs?: number; closed?: () => void } = {},
|
|
235
|
+
): Promise<Importer> {
|
|
236
|
+
const keepS = Math.min(Math.ceil(idleMs / 1_000) + IDLE_MARGIN_S, WAIT_MOST_S)
|
|
237
|
+
if (!(idleMs > 0) || !Number.isSafeInteger(keepS)) {
|
|
238
|
+
throw new DbAdminError(`${idleMs} isn't a time in milliseconds.`, 400, 'bad-request')
|
|
239
|
+
}
|
|
240
|
+
let core: CoreConnection
|
|
241
|
+
try {
|
|
242
|
+
const connection = await mysql.createConnection(importOptions(url))
|
|
243
|
+
// The connection underneath takes a statement without a callback; the promise one doesn't.
|
|
244
|
+
core = (connection as unknown as { connection: CoreConnection }).connection
|
|
245
|
+
} catch (error) {
|
|
246
|
+
throw new ConnectionError(`Could not connect to MySQL: ${(error as Error).message}`)
|
|
247
|
+
}
|
|
248
|
+
const kill = (thread: number) => killQuery(mysql, connectionOptions(url), thread)
|
|
249
|
+
const importer = new MySqlImporter(core, kill, closed)
|
|
250
|
+
await importer.open(keepS)
|
|
251
|
+
return importer
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
class MySqlImporter implements Importer {
|
|
255
|
+
#target: ImportTarget | undefined
|
|
256
|
+
readonly #core: CoreConnection
|
|
257
|
+
/** KILL QUERY for this connection, on one of its own. */
|
|
258
|
+
readonly #kill: (thread: number) => Promise<KillOutcome>
|
|
259
|
+
/** Told once, when the connection has gone, however it went. */
|
|
260
|
+
readonly #closed: () => void
|
|
261
|
+
/** The character set its text goes over the wire in, as the connection opened: utf8mb4. */
|
|
262
|
+
readonly #wire: number
|
|
263
|
+
/** The session's max_allowed_packet, in bytes. */
|
|
264
|
+
#packet = 0
|
|
265
|
+
/** The session as last asked; its character set none known, under which none is sent. */
|
|
266
|
+
#session: ImportSession = {
|
|
267
|
+
inTransaction: false,
|
|
268
|
+
noBackslashEscapes: false,
|
|
269
|
+
ansiQuotes: false,
|
|
270
|
+
database: null,
|
|
271
|
+
characterSet: '',
|
|
272
|
+
}
|
|
273
|
+
/** The server's reports of the session as they were set. */
|
|
274
|
+
#reports = ''
|
|
275
|
+
/**
|
|
276
|
+
* An answer that reports no change means none: the reports were seen to arrive on this
|
|
277
|
+
* connection for each way of setting a setting, and are still set as they were. Otherwise
|
|
278
|
+
* every statement is asked after.
|
|
279
|
+
*/
|
|
280
|
+
#reported = false
|
|
281
|
+
/** The watch on the file's last statement: what `stop()` stops, while that one runs. */
|
|
282
|
+
#stoppable: StatementWatch | null = null
|
|
283
|
+
/** A statement of the file's was sent and has not answered. */
|
|
284
|
+
#unanswered = false
|
|
285
|
+
/** What runs now: a statement and all that is asked after it. */
|
|
286
|
+
#running: Promise<unknown> | null = null
|
|
287
|
+
/** `end()` was called: from then on nothing of the file's starts. */
|
|
288
|
+
#ended = false
|
|
289
|
+
#ending: Promise<ImportClosed> | null = null
|
|
290
|
+
/** Why the connection is of no more use, once it is. */
|
|
291
|
+
#gone: Error | null = null
|
|
292
|
+
/** The same, when it went other than by `end()`. */
|
|
293
|
+
#lost: Error | null = null
|
|
294
|
+
/** The connection went under a statement of the file's: whether that one finished is not known. */
|
|
295
|
+
#doubt = false
|
|
296
|
+
readonly #listeners: ((error: Error) => void)[] = []
|
|
297
|
+
/**
|
|
298
|
+
* Whoever waits on a statement's answer, to be told when the connection goes and no answer
|
|
299
|
+
* can come. Each is here only while it waits: an import has statements by the million, and
|
|
300
|
+
* what one of them left behind would be kept for all of them.
|
|
301
|
+
*/
|
|
302
|
+
readonly #waiting = new Set<(error: Error) => void>()
|
|
303
|
+
|
|
304
|
+
constructor(
|
|
305
|
+
core: CoreConnection,
|
|
306
|
+
kill: (thread: number) => Promise<KillOutcome>,
|
|
307
|
+
closed: () => void,
|
|
308
|
+
) {
|
|
309
|
+
this.#core = core
|
|
310
|
+
this.#kill = kill
|
|
311
|
+
this.#closed = closed
|
|
312
|
+
this.#wire = core.config.charsetNumber!
|
|
313
|
+
// mysql2 tells the connection, and not the statement, of a connection that went under a
|
|
314
|
+
// statement sent without a callback; and with no listener, that error is the process's end.
|
|
315
|
+
core.on('error', (error: Error) => {
|
|
316
|
+
this.#lose(new ConnectionError(`The import’s connection was lost: ${error.message}`))
|
|
317
|
+
})
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
get target(): ImportTarget {
|
|
321
|
+
return this.#target!
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Proves the connection runs one statement a query, gives its session the server's own
|
|
326
|
+
* collation, has the server keep it for `keepS` seconds unused and report its session, sees
|
|
327
|
+
* whether those reports arrive for each way of setting a setting, and reads where it is. A
|
|
328
|
+
* connection that fails any of it is closed; one whose reports do not all arrive is asked
|
|
329
|
+
* after every statement instead.
|
|
330
|
+
*/
|
|
331
|
+
async open(keepS: number): Promise<void> {
|
|
332
|
+
// No kill can be sent for these: the connection's id isn't known before they answer.
|
|
333
|
+
const late = setTimeout(
|
|
334
|
+
() => this.#lose(new ConnectionError(`MySQL did not answer within ${OWN_MS} ms.`)),
|
|
335
|
+
OWN_MS,
|
|
336
|
+
)
|
|
337
|
+
try {
|
|
338
|
+
const two = await this.#answer('DO 0; DO 0')
|
|
339
|
+
if (two.error?.errno !== SYNTAX_ERROR) {
|
|
340
|
+
throw new DbAdminError(
|
|
341
|
+
'This connection would run two statements sent as one, so no import can use it. Nothing of the file was sent.',
|
|
342
|
+
500,
|
|
343
|
+
'unsafe-connection',
|
|
344
|
+
)
|
|
345
|
+
}
|
|
346
|
+
const send: Send = (sql, row) => this.#answer(sql, row)
|
|
347
|
+
const [thread, version, packet, wait] = await this.#one(OPENED, 4, send)
|
|
348
|
+
const names = await this.#answer(NAMES)
|
|
349
|
+
if (names.error) throw refused(names.error)
|
|
350
|
+
const longer = Number(wait) < keepS ? `, wait_timeout = ${whole(keepS)}` : ''
|
|
351
|
+
const set = await this.#answer(`SET SESSION ${REPORTS}${longer}`)
|
|
352
|
+
if (set.error) throw refused(set.error)
|
|
353
|
+
const proofs: Reply[] = []
|
|
354
|
+
for (const proof of PROOFS) proofs.push(await this.#answer(proof))
|
|
355
|
+
const { sqlMode, reports, ...session } = await this.#ask(send)
|
|
356
|
+
this.#reports = reports
|
|
357
|
+
this.#reported = proofs.every(sessionChanged)
|
|
358
|
+
this.#packet = whole(Number(packet))
|
|
359
|
+
this.#session = { inTransaction: inTransaction(set.status!), ...session }
|
|
360
|
+
this.#target = { ...serverOf(version ?? ''), thread: whole(Number(thread)), sqlMode }
|
|
361
|
+
} catch (error) {
|
|
362
|
+
this.#lose(error as Error)
|
|
363
|
+
throw error
|
|
364
|
+
} finally {
|
|
365
|
+
clearTimeout(late)
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
use(database: string): Promise<ImportSession> {
|
|
370
|
+
return this.#alone(async () => {
|
|
371
|
+
const reply = await this.#statement(`USE ${backtick(database)}`, OWN_MS)
|
|
372
|
+
if (reply.error) throw refused(reply.error)
|
|
373
|
+
return this.#after(reply)
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
run(sql: string, { timeoutMs }: { timeoutMs: number }): Promise<ImportResult> {
|
|
378
|
+
return this.#alone(async () => {
|
|
379
|
+
const limit = timeLimit(timeoutMs)
|
|
380
|
+
const unsent = this.#tooLong(sql) ?? this.#misread(sql)
|
|
381
|
+
if (unsent) return { error: unsent }
|
|
382
|
+
const reply = await this.#statement(sql, limit, { ofFile: true })
|
|
383
|
+
const nothing = reply.error?.errno === EMPTY_QUERY
|
|
384
|
+
if (reply.error && !nothing) return { error: reply.error }
|
|
385
|
+
const done = { affected: reply.affected, warnings: reply.warnings, rows: reply.rows }
|
|
386
|
+
// The server has answered for it. That stands whatever comes of the connection after: a
|
|
387
|
+
// statement that ran and is not counted would be run again.
|
|
388
|
+
const session = await this.#sessionAfter(reply, nothing)
|
|
389
|
+
return session ? { ...done, ...session } : { ...done, lost: this.#gone! }
|
|
390
|
+
})
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* The session after a statement of the file's that ran: null when the connection went before
|
|
395
|
+
* it could say. `nothing` is a statement with nothing to run, so nothing changed: the session
|
|
396
|
+
* is as its last statement left it, while there is one.
|
|
397
|
+
*/
|
|
398
|
+
async #sessionAfter(reply: Reply, nothing: boolean): Promise<ImportSession | null> {
|
|
399
|
+
if (nothing) return this.#gone ? null : this.#session
|
|
400
|
+
try {
|
|
401
|
+
return await this.#after(reply)
|
|
402
|
+
} catch (error) {
|
|
403
|
+
if (error !== this.#gone) throw error
|
|
404
|
+
return null
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
state(): Promise<ImportSession> {
|
|
409
|
+
// An error carries no status, and on MariaDB what the statement changed before it failed is
|
|
410
|
+
// reported with no later answer either: so the server is asked.
|
|
411
|
+
return this.#alone(async () => this.#after(await this.#nothing(), true))
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
async stop(): Promise<void> {
|
|
415
|
+
await this.#stoppable?.stop('cancel')
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
end(): Promise<ImportClosed> {
|
|
419
|
+
this.#ended = true
|
|
420
|
+
this.#ending ??= this.#close()
|
|
421
|
+
return this.#ending
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
onLost(listener: (error: Error) => void): void {
|
|
425
|
+
if (this.#lost) tell(listener, this.#lost)
|
|
426
|
+
else if (!this.#gone) this.#listeners.push(listener)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* One thing at a time on the connection, a statement with all that is asked after it. While it
|
|
431
|
+
* lasts a kill may be on its way, which would land on whatever else was sent.
|
|
432
|
+
*/
|
|
433
|
+
#alone<T>(work: () => Promise<T>): Promise<T> {
|
|
434
|
+
if (this.#ended) {
|
|
435
|
+
return Promise.reject(new DbAdminError('The import has ended.', 409, 'conflict'))
|
|
436
|
+
}
|
|
437
|
+
if (this.#gone) return Promise.reject(this.#gone)
|
|
438
|
+
if (this.#running) {
|
|
439
|
+
return Promise.reject(new DbAdminError('The import’s connection is busy.', 409, 'conflict'))
|
|
440
|
+
}
|
|
441
|
+
const running = work().finally(() => (this.#running = null))
|
|
442
|
+
this.#running = running
|
|
443
|
+
return running
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* The server's error for a statement that would cost the connection, with nothing sent: one
|
|
448
|
+
* the server would hang up on, or one whose packet mysql2 would not send as it is.
|
|
449
|
+
*/
|
|
450
|
+
#tooLong(sql: string): ImportError | null {
|
|
451
|
+
// The server takes a packet shorter than max_allowed_packet, the statement's head included.
|
|
452
|
+
const taken = this.#packet - PACKET_HEAD - 1
|
|
453
|
+
const sent = PACKET_MOST - PACKET_HEAD
|
|
454
|
+
const bytes = Buffer.byteLength(sql, 'utf8')
|
|
455
|
+
if (bytes <= Math.min(taken, sent)) return null
|
|
456
|
+
const most =
|
|
457
|
+
taken <= sent
|
|
458
|
+
? `this server takes ${taken} at most in one statement (its max_allowed_packet is ${this.#packet})`
|
|
459
|
+
: `an import sends ${sent} at most in one statement`
|
|
460
|
+
return {
|
|
461
|
+
errno: PACKET_TOO_LARGE,
|
|
462
|
+
code: '08S01',
|
|
463
|
+
message: `The statement is ${bytes} bytes, and ${most}.`,
|
|
464
|
+
unsent: true,
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The import's no to a statement the server would read otherwise than the browser cut and
|
|
470
|
+
* checked it, with nothing sent: the person would have agreed to one statement, and the server
|
|
471
|
+
* run another. The character set the session reads it in is the one the server last said.
|
|
472
|
+
*/
|
|
473
|
+
#misread(sql: string): ImportError | null {
|
|
474
|
+
const set = this.#session.characterSet
|
|
475
|
+
if (READ_ALIKE.has(set)) return null
|
|
476
|
+
const ascii = READ_ALIKE_IN_ASCII.has(set)
|
|
477
|
+
if (ascii && !BEYOND_ASCII.test(sql)) return null
|
|
478
|
+
const why = ascii
|
|
479
|
+
? `The statement holds text beyond ASCII, and the session reads statements as ${set}, as the file set it. Read as ${set}, the bytes of that text are other characters than this import read, and the server could end a string or a name where this import did not.`
|
|
480
|
+
: `The session reads statements as ${set}, as the file set it, and in that set the server may not read even ASCII as this import does: it could end a string or a name where this import did not.`
|
|
481
|
+
return {
|
|
482
|
+
errno: NOT_SUPPORTED,
|
|
483
|
+
code: '42000',
|
|
484
|
+
message: `${why} So the statement was not sent: have the file set utf8mb4 first (SET NAMES utf8mb4).`,
|
|
485
|
+
unsent: true,
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* One statement, sent and watched. Past `timeoutMs`, or at a `stop()` when it is the file's, it
|
|
491
|
+
* is sent a KILL QUERY; once that kill has been answered or given up on, the statement has two
|
|
492
|
+
* minutes for an answer of its own, and then the connection is closed under it. It answers
|
|
493
|
+
* only once the kill has been answered too, so none is left on its way to whatever is sent
|
|
494
|
+
* next; a kill that got no answer may still come, and costs the connection.
|
|
495
|
+
*
|
|
496
|
+
* It rejects only when the statement got no answer. The answer of one that did is given even
|
|
497
|
+
* when the connection has gone since, for that kill or for anything else: `#after` then has
|
|
498
|
+
* no session to tell.
|
|
499
|
+
*/
|
|
500
|
+
async #statement(
|
|
501
|
+
sql: string,
|
|
502
|
+
timeoutMs: number,
|
|
503
|
+
{ ofFile = false, row }: { ofFile?: boolean; row?: (values: Row) => void } = {},
|
|
504
|
+
): Promise<Reply> {
|
|
505
|
+
const watch = watchStatement(
|
|
506
|
+
{ threadId: this.target.thread, destroy: () => this.#lose(new ConnectionError(STUCK)) },
|
|
507
|
+
{ timeoutMs, graceMs: REPLY_MS, graceFrom: 'answer', kill: this.#kill },
|
|
508
|
+
)
|
|
509
|
+
if (ofFile) this.#stoppable = watch
|
|
510
|
+
this.#unanswered = ofFile
|
|
511
|
+
try {
|
|
512
|
+
const reply = await this.#answer(sql, row)
|
|
513
|
+
watch.end()
|
|
514
|
+
if ((await watch.settled()) === 'unanswered') this.#lose(new ConnectionError(KILL_UNANSWERED))
|
|
515
|
+
return reply
|
|
516
|
+
} finally {
|
|
517
|
+
watch.end()
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* One statement's answer, or the reason the connection can give none: whichever came first.
|
|
523
|
+
* It settles where the answer arrives, so an answer and the end of the connection read from
|
|
524
|
+
* the network in one go are heard in that order.
|
|
525
|
+
*
|
|
526
|
+
* `sql` is sent as it is, as one query: no values, so a `?` or a `:name` is the file's own.
|
|
527
|
+
* Rows are counted as they arrive and let go: only a question of the connection's own has a
|
|
528
|
+
* `row` to hand them to.
|
|
529
|
+
*/
|
|
530
|
+
#answer(sql: string, row?: (values: Row) => void): Promise<Reply> {
|
|
531
|
+
if (this.#gone) return Promise.reject(this.#gone)
|
|
532
|
+
// mysql2 takes up the character set the server reports for the session, and would send the
|
|
533
|
+
// text after a SET NAMES latin1 as latin1. The file's bytes are UTF-8 whatever it has set,
|
|
534
|
+
// as they are when the `mysql` client reads it: the server is to make of them what it would.
|
|
535
|
+
this.#core.config.charsetNumber = this.#wire
|
|
536
|
+
return new Promise<Reply>((resolve, reject) => {
|
|
537
|
+
const reply: Reply = { affected: 0, warnings: 0, rows: 0 }
|
|
538
|
+
let failure: Failure | undefined
|
|
539
|
+
// Told by `#lose` while it waits. Not through a promise that lasts as long as the
|
|
540
|
+
// connection: each wait on one of those is kept until it settles, which is at the end.
|
|
541
|
+
this.#waiting.add(reject)
|
|
542
|
+
// Said here, where it arrives, and not by whoever waits for it: the next packet of the
|
|
543
|
+
// same read may be the connection's end, and that leaves no statement in doubt.
|
|
544
|
+
const answered = (answer: Reply) => {
|
|
545
|
+
this.#unanswered = false
|
|
546
|
+
this.#waiting.delete(reject)
|
|
547
|
+
resolve(answer)
|
|
548
|
+
}
|
|
549
|
+
const failed = (error: unknown) => reject(this.#lose(error as Error))
|
|
550
|
+
try {
|
|
551
|
+
// No callback: with one, mysql2 keeps every row until the statement has ended.
|
|
552
|
+
const query = this.#core.query({
|
|
553
|
+
sql,
|
|
554
|
+
rowsAsArray: true,
|
|
555
|
+
typeCast: false,
|
|
556
|
+
namedPlaceholders: false,
|
|
557
|
+
})
|
|
558
|
+
query.on('fields', (fields: unknown) => {
|
|
559
|
+
if (fields) delete reply.status
|
|
560
|
+
})
|
|
561
|
+
query.on('result', (result: unknown) => {
|
|
562
|
+
if (Array.isArray(result)) {
|
|
563
|
+
reply.rows++
|
|
564
|
+
return row?.(result as Row)
|
|
565
|
+
}
|
|
566
|
+
const ok = result as { serverStatus: number; affectedRows: number; warningStatus: number }
|
|
567
|
+
reply.status = ok.serverStatus
|
|
568
|
+
reply.affected = ok.affectedRows
|
|
569
|
+
reply.warnings = ok.warningStatus
|
|
570
|
+
})
|
|
571
|
+
query.on('error', (error: Failure) => (failure = error))
|
|
572
|
+
query.on('end', () => {
|
|
573
|
+
if (!failure) return answered(reply)
|
|
574
|
+
const error = serverError(failure)
|
|
575
|
+
if (error) return answered({ ...reply, error })
|
|
576
|
+
// Not the server's answer, so nothing says the connection can still be used.
|
|
577
|
+
failed(new ConnectionError(`The import’s connection failed: ${failure.message}`))
|
|
578
|
+
})
|
|
579
|
+
} catch (error) {
|
|
580
|
+
failed(error)
|
|
581
|
+
}
|
|
582
|
+
})
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* The session after a statement that ran, as the server says it. What is asked for it runs on
|
|
587
|
+
* the file's session, between two of the file's statements, and is not nothing there. The
|
|
588
|
+
* file's next statement then reads of the question, and not of the file's own last one:
|
|
589
|
+
* ROW_COUNT() is -1 and FOUND_ROWS() is 1, on both servers, and on MySQL the list SHOW
|
|
590
|
+
* WARNINGS reads is empty. MariaDB keeps that list, through a statement that reads no table.
|
|
591
|
+
* The `DO 0` sent first, where an answer came without its status, changes none of this.
|
|
592
|
+
*
|
|
593
|
+
* No way of asking leaves the three as they were (MySQL 8.4, MariaDB 11.4). What the session
|
|
594
|
+
* is comes only as rows, and an answer with rows puts ROW_COUNT() at -1, as one without puts
|
|
595
|
+
* it at what its own statement changed; a SELECT sets FOUND_ROWS(); and on MySQL every
|
|
596
|
+
* statement empties the list but those that read it. Of those, GET DIAGNOSTICS leaves all
|
|
597
|
+
* three, and it tells nothing of the session.
|
|
598
|
+
*
|
|
599
|
+
* So these read as in the `mysql` client only where nothing is asked: after a statement of the
|
|
600
|
+
* file's that ran, returned no rows and changed nothing of the session, on a connection whose
|
|
601
|
+
* reports are trusted (see PROOFS), which MariaDB's are not. One the server refused is asked
|
|
602
|
+
* after as well, by `state()`, before the file's next statement is sent.
|
|
603
|
+
*/
|
|
604
|
+
async #after(reply: Reply, ask = false): Promise<ImportSession> {
|
|
605
|
+
// The statement was answered and the connection went all the same, for its kill.
|
|
606
|
+
if (this.#gone) throw this.#gone
|
|
607
|
+
// Rows end without the status an OK carries, and without a word on what changed.
|
|
608
|
+
const hidden = reply.status === undefined
|
|
609
|
+
const status = reply.status ?? (await this.#nothing()).status!
|
|
610
|
+
const open = inTransaction(status)
|
|
611
|
+
const changed = ask || hidden || !this.#reported || sessionChanged(reply)
|
|
612
|
+
// The sql_mode is as it was last asked: nothing an answer carries says it better.
|
|
613
|
+
if (!changed) return (this.#session = { ...this.#session, inTransaction: open })
|
|
614
|
+
const { reports, ...asked } = await this.#ask((sql, row) =>
|
|
615
|
+
this.#statement(sql, OWN_MS, { row }),
|
|
616
|
+
)
|
|
617
|
+
// Once a file has changed what the server reports, every statement is asked after.
|
|
618
|
+
this.#reported &&= reports === this.#reports
|
|
619
|
+
const { noBackslashEscapes, ansiQuotes, database, characterSet } = asked
|
|
620
|
+
return (this.#session = {
|
|
621
|
+
inTransaction: open,
|
|
622
|
+
noBackslashEscapes,
|
|
623
|
+
ansiQuotes,
|
|
624
|
+
database,
|
|
625
|
+
characterSet,
|
|
626
|
+
})
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** A statement that does nothing, for the status its OK carries. */
|
|
630
|
+
async #nothing(): Promise<Reply> {
|
|
631
|
+
const reply = await this.#statement('DO 0', OWN_MS)
|
|
632
|
+
if (reply.status === undefined) throw this.#unknown(reply.error?.message)
|
|
633
|
+
return reply
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* What only asking tells: the sql_mode whole, with NO_BACKSLASH_ESCAPES and ANSI_QUOTES or
|
|
638
|
+
* without; the database selected; the character set statements are read in; and what the
|
|
639
|
+
* server reports of the session now.
|
|
640
|
+
*/
|
|
641
|
+
async #ask(send: Send) {
|
|
642
|
+
const [sqlMode, database, client, ...reports] = await this.#one(ASK, 6, send)
|
|
643
|
+
const modes = (sqlMode ?? '').split(',')
|
|
644
|
+
return {
|
|
645
|
+
sqlMode: sqlMode ?? '',
|
|
646
|
+
noBackslashEscapes: modes.includes('NO_BACKSLASH_ESCAPES'),
|
|
647
|
+
ansiQuotes: modes.includes('ANSI_QUOTES'),
|
|
648
|
+
database: database ?? null,
|
|
649
|
+
characterSet: client ?? '',
|
|
650
|
+
reports: reports.join('\n'),
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/** The one row of `count` values a question of the connection's own is answered with, as text. */
|
|
655
|
+
async #one(sql: string, count: number, send: Send): Promise<(string | null)[]> {
|
|
656
|
+
const rows: Row[] = []
|
|
657
|
+
const reply = await send(sql, (values) => rows.push(values))
|
|
658
|
+
if (rows.length !== 1 || rows[0]!.length !== count) throw this.#unknown(reply.error?.message)
|
|
659
|
+
return rows[0]!.map((value) => (value === null ? null : value.toString('utf8')))
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** The session's state can't be had, so nothing more can be split for it or sent to it. */
|
|
663
|
+
#unknown(why = 'it gave no answer'): Error {
|
|
664
|
+
return this.#lose(
|
|
665
|
+
new ConnectionError(
|
|
666
|
+
`The import’s connection could not say what state its session is in (${why}), so it was closed.`,
|
|
667
|
+
),
|
|
668
|
+
)
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* The connection is of no more use: it is closed, whatever waits on it is told, and the
|
|
673
|
+
* listeners are, unless the import was ending anyway. The first reason is the one kept.
|
|
674
|
+
*/
|
|
675
|
+
#lose(error: Error): Error {
|
|
676
|
+
if (this.#gone) return this.#gone
|
|
677
|
+
this.#gone = error
|
|
678
|
+
this.#doubt = this.#unanswered
|
|
679
|
+
this.#core.destroy()
|
|
680
|
+
for (const told of this.#waiting) told(error)
|
|
681
|
+
this.#waiting.clear()
|
|
682
|
+
this.#closed()
|
|
683
|
+
if (this.#ended) return error
|
|
684
|
+
this.#lost = error
|
|
685
|
+
for (const listener of this.#listeners.splice(0)) tell(listener, error)
|
|
686
|
+
return error
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Stops what runs and waits for it, kill and all; then rolls back what the file left open, as
|
|
691
|
+
* the server says is open then, and says goodbye.
|
|
692
|
+
*
|
|
693
|
+
* Only a rollback the server took is called one. A connection that went first, or one closed
|
|
694
|
+
* on a rollback the server refused, has its transaction `abandoned`: nothing here says
|
|
695
|
+
* whether one was still open by then, and the server keeps one kind after its connection, an
|
|
696
|
+
* XA transaction already prepared.
|
|
697
|
+
*/
|
|
698
|
+
async #close(): Promise<ImportClosed> {
|
|
699
|
+
const running = this.#running
|
|
700
|
+
await this.#stoppable?.stop('cancel')
|
|
701
|
+
await running?.catch(() => {})
|
|
702
|
+
try {
|
|
703
|
+
const open = inTransaction((await this.#nothing()).status!)
|
|
704
|
+
if (open) {
|
|
705
|
+
const undone = await this.#statement('ROLLBACK', REPLY_MS)
|
|
706
|
+
// The server's no, as inside an XA transaction, which only its own statements end.
|
|
707
|
+
if (undone.error) throw this.#lose(new ConnectionError(undone.error.message))
|
|
708
|
+
}
|
|
709
|
+
await this.#goodbye()
|
|
710
|
+
return { transaction: open ? 'rolled-back' : 'none', inDoubt: false }
|
|
711
|
+
} catch (error) {
|
|
712
|
+
if (error !== this.#gone) throw error
|
|
713
|
+
return { transaction: 'abandoned', inDoubt: this.#doubt }
|
|
714
|
+
} finally {
|
|
715
|
+
this.#lose(new ConnectionError('The import has ended.'))
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/** QUIT, and the server's own hanging up heard: its thread is going by then. */
|
|
720
|
+
#goodbye(): Promise<void> {
|
|
721
|
+
return new Promise((resolve) => {
|
|
722
|
+
const late = setTimeout(resolve, GOODBYE_MS)
|
|
723
|
+
this.#core.once('end', () => {
|
|
724
|
+
clearTimeout(late)
|
|
725
|
+
resolve()
|
|
726
|
+
})
|
|
727
|
+
this.#core.end()
|
|
728
|
+
})
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/** Whether a statement's answer came with the server's word that the session changed. */
|
|
733
|
+
function sessionChanged({ status = 0 }: Reply): boolean {
|
|
734
|
+
return (status & SESSION_CHANGED) !== 0
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/** Whether the status with an OK says a transaction is open. */
|
|
738
|
+
function inTransaction(status: number): boolean {
|
|
739
|
+
return (status & IN_TRANSACTION) !== 0
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** The server's no to a statement, which leaves the connection as it was; null for anything else. */
|
|
743
|
+
function serverError({ errno, sqlState, sqlMessage, message, code, fatal }: Failure) {
|
|
744
|
+
if (!Number.isInteger(errno) || fatal) return null
|
|
745
|
+
// The message comes in the character set the file asked its results in, and may not read.
|
|
746
|
+
return { errno: errno!, code: sqlState ?? 'HY000', message: sqlMessage || message || code || '' }
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/** The server's no, for a statement of the importer's own that whoever asked must hear of. */
|
|
750
|
+
function refused({ errno, code, message }: ImportError): QueryError {
|
|
751
|
+
return new QueryError(message, code, { errno })
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/** A statement's time limit as a timer can keep it: a whole number of milliseconds, 24 days at most. */
|
|
755
|
+
function timeLimit(timeoutMs: number): number {
|
|
756
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > TIMER_MOST_MS) {
|
|
757
|
+
throw new DbAdminError(`${timeoutMs} isn't a time limit in milliseconds.`, 400, 'bad-request')
|
|
758
|
+
}
|
|
759
|
+
return timeoutMs
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** A listener of someone else's, called so that nothing it throws gets out. */
|
|
763
|
+
function tell(listener: (error: Error) => void, error: Error): void {
|
|
764
|
+
try {
|
|
765
|
+
listener(error)
|
|
766
|
+
} catch (failure) {
|
|
767
|
+
console.error('otw-db: an import’s listener failed', failure)
|
|
768
|
+
}
|
|
769
|
+
}
|