@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,282 @@
|
|
|
1
|
+
import type * as MySqlModule from 'mysql2/promise'
|
|
2
|
+
import type { Connection, ConnectionOptions } from 'mysql2/promise'
|
|
3
|
+
import { DbAdminError, QueryError } from '../errors.ts'
|
|
4
|
+
import { whole } from './sql.ts'
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* What the driver and an import's connection share that is written in mysql2's own types: the
|
|
8
|
+
* package itself, a connection's settings, the kill and the watch on a running statement.
|
|
9
|
+
*
|
|
10
|
+
* A file of its own, which the package's entries never name. The driver's file is one of the
|
|
11
|
+
* package's exports, so a host's type checker reads its declarations; with these in it they
|
|
12
|
+
* named mysql2, and a host without mysql2 (PostgreSQL or SQLite alone) no longer compiled.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export type MySql = typeof MySqlModule
|
|
16
|
+
|
|
17
|
+
let module: Promise<MySql> | undefined
|
|
18
|
+
|
|
19
|
+
/** mysql2, imported on the first MySQL connection, so other hosts never load it. */
|
|
20
|
+
export function loadMySql(): Promise<MySql> {
|
|
21
|
+
// Imported where the host runs, not bundled (see postgres.ts).
|
|
22
|
+
module ??= import(/* webpackIgnore: true */ /* turbopackIgnore: true */ 'mysql2/promise').then(
|
|
23
|
+
// CommonJS: its exports arrive as the default.
|
|
24
|
+
(m) => (m as unknown as { default?: MySql }).default ?? (m as unknown as MySql),
|
|
25
|
+
(error: unknown) => {
|
|
26
|
+
module = undefined
|
|
27
|
+
throw new DbAdminError(
|
|
28
|
+
`MySQL and MariaDB connections need the mysql2 package: npm install mysql2 (${(error as Error).message})`,
|
|
29
|
+
500,
|
|
30
|
+
'driver-missing',
|
|
31
|
+
)
|
|
32
|
+
},
|
|
33
|
+
)
|
|
34
|
+
return module
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** How long a KILL QUERY gets to connect, be answered and say goodbye, unless its caller says. */
|
|
38
|
+
const KILL_ANSWER_MS = 10_000
|
|
39
|
+
const KILL_FAILED = 'otw-db: could not stop a MySQL query'
|
|
40
|
+
/** "Unknown thread id": the server has no connection with the id a kill names. */
|
|
41
|
+
const NO_SUCH_THREAD = 1094
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* One connection's settings for `url`, as the driver's own connections open: the pool's, and the
|
|
45
|
+
* one that carries KILL QUERY, an import's kill included. New each time, because mysql2 writes
|
|
46
|
+
* the URL's options into the object it is given.
|
|
47
|
+
*
|
|
48
|
+
* An import's own connection is not opened with these. The URL's options all pass through here,
|
|
49
|
+
* its character set and time zone among them, and a file's text must reach the server as the
|
|
50
|
+
* bytes it holds: that connection takes an allow-list of the URL's options and flags of its own.
|
|
51
|
+
*/
|
|
52
|
+
export function connectionOptions(url: string): ConnectionOptions {
|
|
53
|
+
return {
|
|
54
|
+
// mysql2 reads mysql:// addresses; mariadb:// is the same thing.
|
|
55
|
+
uri: url.replace(/^mariadb:/i, 'mysql:'),
|
|
56
|
+
// One statement a query: `COMMIT; DROP …` is a syntax error. Said as a flag taken away,
|
|
57
|
+
// which mysql2 drops whoever asked for it: `multipleStatements: false` here would lose to
|
|
58
|
+
// the URL's ?multipleStatements=true, while the URL's own `flags` lose to these.
|
|
59
|
+
// FOUND_ROWS is rows matched, not rows changed: an update to the same value still counts
|
|
60
|
+
// its row.
|
|
61
|
+
flags: ['-MULTI_STATEMENTS', 'FOUND_ROWS', '-LOCAL_FILES'],
|
|
62
|
+
connectAttributes: { program_name: 'otw-db' },
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* What became of a KILL QUERY:
|
|
68
|
+
* - `taken`: the server answered that it took it.
|
|
69
|
+
* - `failed`: it could not be sent, or the server refused it, as it does for a connection it no
|
|
70
|
+
* longer has. Nothing is on its way.
|
|
71
|
+
* - `unanswered`: it was sent and no answer came back in its time. It may still land, on
|
|
72
|
+
* whatever its connection runs then.
|
|
73
|
+
*/
|
|
74
|
+
export type KillOutcome = 'taken' | 'failed' | 'unanswered'
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* KILL QUERY on a connection of its own: the busy one can't take it. You may kill your own.
|
|
78
|
+
*
|
|
79
|
+
* It never rejects and never waits longer than `withinMs`: a proxy or a network that stalls
|
|
80
|
+
* answers nothing, for as long as the system lets a silent connection live, and whoever waits
|
|
81
|
+
* on the kill may hold locks meanwhile. Past that the kill's connection is cut. What goes wrong
|
|
82
|
+
* is reported here, but for the server's answer that it has no connection of that id (1094).
|
|
83
|
+
* Mostly that connection has gone, and its statement with it, which is what the kill was for.
|
|
84
|
+
* Behind a proxy that spreads connections over several servers, the kill can reach another
|
|
85
|
+
* server than the statement's, which answers the same: the statement runs on, and is ended by
|
|
86
|
+
* the grace its watch gives it (see `watchStatement`), which closes its connection.
|
|
87
|
+
*/
|
|
88
|
+
export async function killQuery(
|
|
89
|
+
mysql: MySql,
|
|
90
|
+
options: ConnectionOptions,
|
|
91
|
+
threadId: number,
|
|
92
|
+
withinMs = KILL_ANSWER_MS,
|
|
93
|
+
): Promise<KillOutcome> {
|
|
94
|
+
let killer: Connection | undefined
|
|
95
|
+
let outcome: KillOutcome = 'failed'
|
|
96
|
+
/** The server has said yes or no: only the goodbye is left. */
|
|
97
|
+
let answered = false
|
|
98
|
+
let over = false
|
|
99
|
+
const send = async (): Promise<void> => {
|
|
100
|
+
const sql = `KILL QUERY ${whole(threadId)}`
|
|
101
|
+
// A copy: mysql2 would write the URL's options into the caller's.
|
|
102
|
+
killer = await mysql.createConnection({ ...options })
|
|
103
|
+
// Its time was up while it connected: nothing is sent now, for nobody waits to hear of it.
|
|
104
|
+
if (over) return killer.destroy()
|
|
105
|
+
outcome = 'unanswered'
|
|
106
|
+
try {
|
|
107
|
+
await killer.query(sql)
|
|
108
|
+
outcome = 'taken'
|
|
109
|
+
} catch (error) {
|
|
110
|
+
// Only the server's own no, such as for a thread that has gone, says nothing will land.
|
|
111
|
+
if (!isRefusal(error)) throw error
|
|
112
|
+
outcome = 'failed'
|
|
113
|
+
if (error.errno !== NO_SUCH_THREAD) console.error(KILL_FAILED, error)
|
|
114
|
+
}
|
|
115
|
+
answered = true
|
|
116
|
+
await killer.end()
|
|
117
|
+
}
|
|
118
|
+
let timer: NodeJS.Timeout | undefined
|
|
119
|
+
const late = new Promise<never>((_, reject) => {
|
|
120
|
+
timer = setTimeout(() => reject(new Error(`It had no answer within ${withinMs} ms.`)), withinMs)
|
|
121
|
+
})
|
|
122
|
+
try {
|
|
123
|
+
// A `send` the time limit leaves behind ends later with nobody waiting on it: the race
|
|
124
|
+
// has heard it by then, so its failure goes nowhere.
|
|
125
|
+
await Promise.race([send(), late])
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (!answered) console.error(KILL_FAILED, error)
|
|
128
|
+
killer?.destroy()
|
|
129
|
+
} finally {
|
|
130
|
+
over = true
|
|
131
|
+
clearTimeout(timer)
|
|
132
|
+
}
|
|
133
|
+
return outcome
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Why a statement was stopped: it ran past its time limit, or whoever ran it asked. */
|
|
137
|
+
export type StopReason = 'timeout' | 'cancel'
|
|
138
|
+
|
|
139
|
+
/** The watch kept on one running statement, from `watchStatement`. */
|
|
140
|
+
export interface StatementWatch {
|
|
141
|
+
/** Why the statement was stopped; null while nothing has stopped it. */
|
|
142
|
+
readonly stopped: StopReason | null
|
|
143
|
+
/**
|
|
144
|
+
* Answers with why, once the statement's connection has been closed under it: its kill's grace
|
|
145
|
+
* passed and the statement still hadn't answered. Never, when the statement answers first.
|
|
146
|
+
*/
|
|
147
|
+
readonly abandoned: Promise<StopReason>
|
|
148
|
+
/**
|
|
149
|
+
* Stops the statement now, unless something already has, and answers with what became of the
|
|
150
|
+
* kill: null when none was sent, which is so once the statement has answered. It waits for
|
|
151
|
+
* the kill's answer no longer than the grace, and then answers `unanswered`.
|
|
152
|
+
*/
|
|
153
|
+
stop(why: StopReason): Promise<KillOutcome | null>
|
|
154
|
+
/**
|
|
155
|
+
* Answers as `stop()` does, without stopping anything: at once, with null, if no kill was sent.
|
|
156
|
+
* After `taken`, `failed` or null no kill is on its way.
|
|
157
|
+
*/
|
|
158
|
+
settled(): Promise<KillOutcome | null>
|
|
159
|
+
/**
|
|
160
|
+
* The statement has answered: no listener is left, no kill will be sent, and no timer is left
|
|
161
|
+
* but the one that ends the wait for a kill still unanswered.
|
|
162
|
+
*/
|
|
163
|
+
end(): void
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Watches the statement about to run on `connection`, the pool's or a caller's own: past
|
|
168
|
+
* `timeoutMs`, on `signal`'s abort or at a `stop()`, it sends one KILL QUERY with `kill`.
|
|
169
|
+
* Whoever runs the statement calls `end()` when it answers. A `signal` that has already aborted
|
|
170
|
+
* is refused as canceled, with nothing left behind: the statement must not be sent.
|
|
171
|
+
*
|
|
172
|
+
* KILL QUERY needs a connection of its own, which a server at its limit refuses, and some
|
|
173
|
+
* proxies never pass it on: the statement would run on, and its caller wait with it. So
|
|
174
|
+
* `graceMs` after the kill was sent, the statement's connection is closed under it, which ends
|
|
175
|
+
* the wait whatever the server does; and the wait for the kill's own answer ends then too,
|
|
176
|
+
* whatever `kill` does.
|
|
177
|
+
*
|
|
178
|
+
* With `graceFrom: 'answer'` the grace is counted from the kill's answer instead, whatever that
|
|
179
|
+
* answer is: a statement that was told to stop then has all of it to stop in. Nothing here ends
|
|
180
|
+
* the wait for the kill itself in that case, so `kill` must end its own, as `killQuery` does.
|
|
181
|
+
*
|
|
182
|
+
* A kill names the connection, not a statement. One still on its way when the statement answers
|
|
183
|
+
* lands on whatever runs there next: await `settled()` before the connection is used again, and
|
|
184
|
+
* close it instead if the kill was left `unanswered`.
|
|
185
|
+
*/
|
|
186
|
+
export function watchStatement(
|
|
187
|
+
connection: Pick<Connection, 'threadId' | 'destroy'>,
|
|
188
|
+
{
|
|
189
|
+
timeoutMs,
|
|
190
|
+
graceMs,
|
|
191
|
+
graceFrom = 'kill',
|
|
192
|
+
signal,
|
|
193
|
+
kill,
|
|
194
|
+
}: {
|
|
195
|
+
timeoutMs: number
|
|
196
|
+
graceMs: number
|
|
197
|
+
graceFrom?: 'kill' | 'answer'
|
|
198
|
+
signal?: AbortSignal
|
|
199
|
+
kill(threadId: number): Promise<KillOutcome>
|
|
200
|
+
},
|
|
201
|
+
): StatementWatch {
|
|
202
|
+
// A listener added now would never be called.
|
|
203
|
+
if (signal?.aborted) throw interrupted('cancel', timeoutMs)
|
|
204
|
+
let stopped: StopReason | null = null
|
|
205
|
+
let ended = false
|
|
206
|
+
let answered = false
|
|
207
|
+
let killed: Promise<KillOutcome | null> = Promise.resolve(null)
|
|
208
|
+
let giveUp: NodeJS.Timeout | undefined
|
|
209
|
+
let abandon!: (why: StopReason) => void
|
|
210
|
+
const abandoned = new Promise<StopReason>((resolve) => (abandon = resolve))
|
|
211
|
+
const stop = (why: StopReason): Promise<KillOutcome | null> => {
|
|
212
|
+
if (stopped || ended) return killed
|
|
213
|
+
stopped = why
|
|
214
|
+
killed = new Promise((resolve) => {
|
|
215
|
+
const allow = () => {
|
|
216
|
+
giveUp = setTimeout(() => {
|
|
217
|
+
resolve('unanswered')
|
|
218
|
+
if (ended) return
|
|
219
|
+
connection.destroy()
|
|
220
|
+
abandon(why)
|
|
221
|
+
}, graceMs)
|
|
222
|
+
}
|
|
223
|
+
void heard(kill, connection.threadId).then((outcome) => {
|
|
224
|
+
answered = true
|
|
225
|
+
if (ended) clearTimeout(giveUp)
|
|
226
|
+
resolve(outcome)
|
|
227
|
+
// A statement that has answered by now needs no more time.
|
|
228
|
+
if (graceFrom === 'answer' && !ended) allow()
|
|
229
|
+
})
|
|
230
|
+
if (graceFrom === 'kill') allow()
|
|
231
|
+
})
|
|
232
|
+
return killed
|
|
233
|
+
}
|
|
234
|
+
const timer = setTimeout(() => void stop('timeout'), timeoutMs)
|
|
235
|
+
const abort = () => void stop('cancel')
|
|
236
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
237
|
+
return {
|
|
238
|
+
get stopped() {
|
|
239
|
+
return stopped
|
|
240
|
+
},
|
|
241
|
+
abandoned,
|
|
242
|
+
stop,
|
|
243
|
+
settled: () => killed,
|
|
244
|
+
end() {
|
|
245
|
+
ended = true
|
|
246
|
+
clearTimeout(timer)
|
|
247
|
+
signal?.removeEventListener('abort', abort)
|
|
248
|
+
// While its kill is unanswered, the grace still ends the wait for that answer.
|
|
249
|
+
if (answered) clearTimeout(giveUp)
|
|
250
|
+
},
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* A caller's kill, sent and heard so that nothing it does gets out: it is called from a timer
|
|
256
|
+
* and from a signal, where an error let out would take the process down. One that throws or
|
|
257
|
+
* rejects is reported, and may have sent its kill all the same.
|
|
258
|
+
*/
|
|
259
|
+
function heard(
|
|
260
|
+
kill: (threadId: number) => Promise<KillOutcome>,
|
|
261
|
+
threadId: number,
|
|
262
|
+
): Promise<KillOutcome> {
|
|
263
|
+
return new Promise<KillOutcome>((resolve) => resolve(kill(threadId))).catch((error: unknown) => {
|
|
264
|
+
console.error(KILL_FAILED, error)
|
|
265
|
+
return 'unanswered'
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** What whoever ran a stopped statement is told, by why it was stopped. */
|
|
270
|
+
export function interrupted(why: StopReason, timeoutMs: number): DbAdminError {
|
|
271
|
+
return why === 'cancel'
|
|
272
|
+
? new DbAdminError('The query was canceled.', 409, 'canceled')
|
|
273
|
+
: new QueryError(`The statement ran past its ${timeoutMs} ms limit and was stopped.`, '70100')
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** The server's own no to a statement: an answer, which leaves the connection as it was. */
|
|
277
|
+
export function isRefusal(
|
|
278
|
+
error: unknown,
|
|
279
|
+
): error is { sqlMessage: string; sqlState?: string; errno?: number } {
|
|
280
|
+
const e = error as { sqlMessage?: string; fatal?: boolean } | null
|
|
281
|
+
return Boolean(e?.sqlMessage) && !e?.fatal
|
|
282
|
+
}
|