@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.
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,467 @@
1
+ import { createHash } from 'node:crypto'
2
+
3
+ /*
4
+ * How an import's statements are written to the audit log. Every statement is logged whole,
5
+ * with one exception that keeps a dump's data out of the log: a long INSERT or REPLACE of
6
+ * nothing but rows of plain values is logged as its two ends, its length and its SHA-256, which
7
+ * is enough to find it in the file.
8
+ *
9
+ * What is left out is only ever such rows. The statement is read as the server reads it, from
10
+ * its first word to its last character: the words up to the table's name, the columns' names,
11
+ * VALUES, and rows of numbers, strings, NULL and the like. Anything else in it (a SELECT, a
12
+ * function, a variable, ON DUPLICATE KEY UPDATE, a comment) is something it reads or does
13
+ * beyond storing what the file holds, and a statement with any of that is logged whole.
14
+ */
15
+
16
+ /**
17
+ * About how long one event's statements are, in bytes of UTF-8, before the rest go in the next:
18
+ * 32 KiB. A statement is never cut, so one longer than that is an event as long as it is.
19
+ */
20
+ export const AUDIT_EVENT_BYTES = 32_768
21
+
22
+ /** How much of each end of a shortened statement is logged, in characters. */
23
+ const END_CHARS = 256
24
+
25
+ /** A statement no longer than this is logged whole, whatever it is: its ends would be most of it. */
26
+ const WHOLE_CHARS = 4 * END_CHARS
27
+
28
+ /** The statements that are shortened, by their first word. */
29
+ const VERB = /^(?:INSERT|REPLACE)$/i
30
+
31
+ /** The words the server takes between that word and the table's name. None can be a table's name. */
32
+ const BEFORE_NAME = /^(?:LOW_PRIORITY|DELAYED|HIGH_PRIORITY|IGNORE|INTO)$/i
33
+
34
+ /** The word the rows follow. */
35
+ const BEFORE_ROWS = /^VALUES?$/i
36
+
37
+ /** The words that are a value by themselves. None reads anything. */
38
+ const VALUE_WORD = /^(?:NULL|TRUE|FALSE|DEFAULT)$/i
39
+
40
+ /** `_binary`, `_utf8mb4`: before a string or a hex value, the character set it is written in. */
41
+ const INTRODUCER = /^_[A-Za-z0-9]+$/
42
+
43
+ const BACKTICK = 0x60
44
+ const DOUBLE_QUOTE = 0x22
45
+ const QUOTE = 0x27
46
+ const BACKSLASH = 0x5c
47
+ const DOT = 0x2e
48
+ const OPEN = 0x28
49
+ const CLOSE = 0x29
50
+ const COMMA = 0x2c
51
+ const PLUS = 0x2b
52
+ const MINUS = 0x2d
53
+ const ZERO = 0x30
54
+ const LOWER_B = 0x62
55
+ const LOWER_E = 0x65
56
+ const LOWER_X = 0x78
57
+ /** Added to a capital letter's code, it gives the small one's. */
58
+ const SMALL = 0x20
59
+ /** The first code beyond ASCII. */
60
+ const BEYOND_ASCII = 0x80
61
+
62
+ /** Some of a batch's statements, as one audit event holds them. */
63
+ export interface AuditPart {
64
+ /** The place in the batch of its first statement, from 0. */
65
+ from: number
66
+ /** How many statements it holds. */
67
+ count: number
68
+ /** Each as `logged` writes it, ended with `;` and a new line. */
69
+ statement: string
70
+ }
71
+
72
+ /**
73
+ * How the session reads strings when the statements run. It is the batch's own word for how it
74
+ * was split, which the import holds against the server's before each statement.
75
+ */
76
+ export interface AuditReading {
77
+ /** A backslash in a string is a character like any other (sql_mode NO_BACKSLASH_ESCAPES). */
78
+ noBackslashEscapes: boolean
79
+ }
80
+
81
+ /**
82
+ * A statement as the audit log keeps it: whole, unless it is a long INSERT or REPLACE of plain
83
+ * rows, which is its start, its length and its SHA-256 as the UTF-8 it was sent as, and its end.
84
+ *
85
+ * Its start is 256 characters from its first word, with its words up to the table's name one
86
+ * space apart, however much white space the file has before and between them: the server skips
87
+ * that, and it must not take the place of the name. Its end is the last 256 characters before
88
+ * any white space the statement ends on.
89
+ *
90
+ * It is whole all the same:
91
+ * - when that is no more than 1,024 characters of it;
92
+ * - when its start doesn't read as INSERT or REPLACE, the words that may follow, and a table's
93
+ * name that ends within what is logged, with a character to show what comes after it;
94
+ * - when anything but the columns' names, VALUES and rows of plain values follows that name
95
+ * (see `onlyRows`): what is left out of the log is rows of the file's own data, never
96
+ * something the statement reads or does.
97
+ */
98
+ export function logged(statement: string, reading: AuditReading): string {
99
+ if (statement.length <= WHOLE_CHARS) return statement
100
+ const start = opening(statement)
101
+ if (!start || start.words.length >= END_CHARS) return statement
102
+ const end = gapBefore(statement, statement.length)
103
+ // White space around it and between its first words is not length that hides anything.
104
+ if (start.words.length + end - start.rest <= WHOLE_CHARS) return statement
105
+ if (!onlyRows(statement, start.rest, !reading.noBackslashEscapes)) return statement
106
+ const cut = start.rest + END_CHARS - start.words.length
107
+ const bytes = Buffer.from(statement, 'utf8')
108
+ const sha256 = createHash('sha256').update(bytes).digest('hex')
109
+ const head = start.words + statement.slice(start.rest, cut)
110
+ const tail = statement.slice(end - END_CHARS, end)
111
+ return `${wholeEnd(head)} … [${bytes.byteLength} bytes, sha256 ${sha256}] … ${wholeStart(tail)}`
112
+ }
113
+
114
+ /** How an INSERT or REPLACE begins. */
115
+ interface Opening {
116
+ /**
117
+ * Its words up to the table's name and that name, one space apart, as they were written; then
118
+ * a space, when white space follows the name.
119
+ */
120
+ words: string
121
+ /** Where the statement goes on after them. */
122
+ rest: number
123
+ }
124
+
125
+ /**
126
+ * The start of a statement the server would read as INSERT or REPLACE into a table, up to the
127
+ * end of the table's name: null for anything else, a comment before the end of that name
128
+ * included. White space is what the server skips between words, and a name is read as the
129
+ * server reads one: bare, or in backticks or double quotes with the quote doubled inside, and
130
+ * with its database before a dot.
131
+ *
132
+ * Whether double quotes hold a name is the session's sql_mode to say, which this does not know.
133
+ * Where they hold a string instead, no reading of it makes a statement the server runs: a table
134
+ * is never named by a string.
135
+ */
136
+ function opening(statement: string): Opening | null {
137
+ const from = gapAfter(statement, 0)
138
+ let at = wordEnd(statement, from)
139
+ if (!VERB.test(statement.slice(from, at))) return null
140
+ const words = [statement.slice(from, at)]
141
+ for (;;) {
142
+ const next = gapAfter(statement, at)
143
+ const end = wordEnd(statement, next)
144
+ // A word needs white space before it. A quoted name needs none, and is no such word.
145
+ if (next === at || !BEFORE_NAME.test(statement.slice(next, end))) break
146
+ words.push(statement.slice(next, end))
147
+ at = end
148
+ }
149
+ const name = nameAt(statement, at)
150
+ if (!name) return null
151
+ const rest = gapAfter(statement, name.end)
152
+ return {
153
+ words: `${words.join(' ')} ${name.text}${rest > name.end ? ' ' : ''}`,
154
+ rest,
155
+ }
156
+ }
157
+
158
+ /**
159
+ * The table's name that follows the white space at `at`, with a database's before it when it
160
+ * has one: its parts as they were written, a dot between them. Null when no name is there, or
161
+ * when no white space parts a bare name from the word before it.
162
+ */
163
+ function nameAt(statement: string, at: number): { text: string; end: number } | null {
164
+ const parts: string[] = []
165
+ let end = at
166
+ for (;;) {
167
+ const from = gapAfter(statement, end)
168
+ const partEnd = quoted(statement, from) ?? wordEnd(statement, from)
169
+ if (partEnd === from) return null
170
+ if (!parts.length && from === at && !isQuote(statement.charCodeAt(from))) return null
171
+ parts.push(statement.slice(from, partEnd))
172
+ end = partEnd
173
+ const dot = gapAfter(statement, end)
174
+ if (statement.charCodeAt(dot) !== DOT) return { text: parts.join('.'), end }
175
+ end = dot + 1
176
+ }
177
+ }
178
+
179
+ function isQuote(code: number): boolean {
180
+ return code === BACKTICK || code === DOUBLE_QUOTE
181
+ }
182
+
183
+ /**
184
+ * Where the quoted name that starts at `at` ends, its last quote included: undefined when none
185
+ * starts there, and `at` itself when it never ends. Two of its quotes inside it are one, in the
186
+ * name.
187
+ *
188
+ * A name in backticks is taken for never ending, too, when one of its backticks follows a
189
+ * character beyond ASCII. A file can have the session read its statements in a character set
190
+ * whose characters of two bytes may end on the byte of a backtick or a backslash (SET NAMES
191
+ * gbk, big5, sjis). The server then takes the last byte of the character before and that byte
192
+ * for one character, and the name goes on where it ends here: what follows would be read as
193
+ * other than it is. No such character ends on the byte of a double quote, or of a single one.
194
+ * The import sends no statement with text beyond ASCII under those sets (errno 1235): this
195
+ * holds the log to what a statement read should one ever go, at the cost of a longer log.
196
+ */
197
+ function quoted(statement: string, at: number): number | undefined {
198
+ const quote = statement.charCodeAt(at)
199
+ if (!isQuote(quote)) return undefined
200
+ for (let i = at + 1; i < statement.length; i += 1) {
201
+ if (statement.charCodeAt(i) !== quote) continue
202
+ if (quote === BACKTICK && statement.charCodeAt(i - 1) >= BEYOND_ASCII) return at
203
+ if (statement.charCodeAt(i + 1) !== quote) return i + 1
204
+ i += 1
205
+ }
206
+ return at
207
+ }
208
+
209
+ /**
210
+ * Whether what follows the table's name, from `at` to the statement's end, is nothing but the
211
+ * columns' names in brackets, if any, then VALUES and rows of plain values:
212
+ *
213
+ * (`id`, `name`) VALUES (1,'a',NULL),(2,_binary 'b',0x00)
214
+ *
215
+ * It is read as the server reads it, and everything has to be accounted for: a row is values
216
+ * with a comma between them, in brackets, and a value is a number, a string in single quotes,
217
+ * NULL, TRUE, FALSE, DEFAULT, or a hex or bit value. Nothing else is: no name, no word, no
218
+ * operator, no bracket inside a row, no string in double quotes (a column's name, under
219
+ * ANSI_QUOTES), no comment anywhere, and nothing after the last row. So a statement that
220
+ * passes names no function, no variable and no other table, in any place.
221
+ *
222
+ * `escapes` says whether a backslash in a string takes the next character with it.
223
+ */
224
+ function onlyRows(statement: string, at: number, escapes: boolean): boolean {
225
+ let i = at
226
+ if (statement.charCodeAt(i) === OPEN) {
227
+ i = listEnd(statement, i + 1, columnEnd)
228
+ if (i < 0) return false
229
+ i = gapAfter(statement, i)
230
+ }
231
+ const word = wordEnd(statement, i)
232
+ if (!BEFORE_ROWS.test(statement.slice(i, word))) return false
233
+ i = word
234
+ for (;;) {
235
+ i = gapAfter(statement, i)
236
+ if (statement.charCodeAt(i) !== OPEN) return false
237
+ i = listEnd(statement, i + 1, (text, from) => valueEnd(text, from, escapes))
238
+ if (i < 0) return false
239
+ i = gapAfter(statement, i)
240
+ if (i === statement.length) return true
241
+ if (statement.charCodeAt(i) !== COMMA) return false
242
+ i += 1
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Where the list ends whose `(` is just before `at`, its `)` included: items with a comma
248
+ * between them, or none, each ending where `itemEnd` says. -1 when it isn't that.
249
+ */
250
+ function listEnd(
251
+ statement: string,
252
+ at: number,
253
+ itemEnd: (statement: string, at: number) => number,
254
+ ): number {
255
+ let i = gapAfter(statement, at)
256
+ if (statement.charCodeAt(i) === CLOSE) return i + 1
257
+ for (;;) {
258
+ i = itemEnd(statement, i)
259
+ if (i < 0) return -1
260
+ i = gapAfter(statement, i)
261
+ const code = statement.charCodeAt(i)
262
+ if (code === CLOSE) return i + 1
263
+ if (code !== COMMA) return -1
264
+ i = gapAfter(statement, i + 1)
265
+ }
266
+ }
267
+
268
+ /** Where the column's name at `at` ends, bare or quoted: -1 when none is there. */
269
+ function columnEnd(statement: string, at: number): number {
270
+ const end = quoted(statement, at) ?? wordEnd(statement, at)
271
+ return end === at ? -1 : end
272
+ }
273
+
274
+ /** Where the plain value at `at` ends: -1 when what is there is anything else. */
275
+ function valueEnd(statement: string, at: number, escapes: boolean): number {
276
+ const text = textEnd(statement, at, escapes)
277
+ if (text !== undefined) return text
278
+ const code = statement.charCodeAt(at)
279
+ if (isDigit(code) || code === DOT || code === PLUS || code === MINUS) {
280
+ return numberEnd(statement, at)
281
+ }
282
+ const end = wordEnd(statement, at)
283
+ const word = statement.slice(at, end)
284
+ if (VALUE_WORD.test(word)) return end
285
+ if (INTRODUCER.test(word)) return textEnd(statement, gapAfter(statement, end), escapes) ?? -1
286
+ return -1
287
+ }
288
+
289
+ /**
290
+ * Where the string, or the hex or bit value, at `at` ends: `'…'`, `X'…'`, `0x…`, `b'…'`, `0b…`.
291
+ * Undefined when none starts there, -1 when one starts and isn't one.
292
+ */
293
+ function textEnd(statement: string, at: number, escapes: boolean): number | undefined {
294
+ const code = statement.charCodeAt(at)
295
+ if (code === QUOTE) return stringEnd(statement, at, escapes)
296
+ const next = statement.charCodeAt(at + 1)
297
+ const quotedDigit = next === QUOTE ? digitAfter(code | SMALL) : undefined
298
+ if (quotedDigit) {
299
+ const end = runEnd(statement, at + 2, quotedDigit)
300
+ return statement.charCodeAt(end) === QUOTE ? end + 1 : -1
301
+ }
302
+ // After a 0 only the small letter makes a value: the server reads `0XFF` as a name.
303
+ const digit = code === ZERO ? digitAfter(next) : undefined
304
+ if (!digit) return undefined
305
+ const end = runEnd(statement, at + 2, digit)
306
+ // With no digit, or with more of a word behind its digits, the server reads a name too.
307
+ return end > at + 2 && !isWordPart(statement.charCodeAt(end)) ? end : -1
308
+ }
309
+
310
+ /** What the digits are of the value that a small x or b begins: undefined for any other. */
311
+ function digitAfter(letter: number): ((code: number) => boolean) | undefined {
312
+ return letter === LOWER_X ? isHexDigit : letter === LOWER_B ? isBit : undefined
313
+ }
314
+
315
+ /**
316
+ * Where the string in single quotes that starts at `at` ends, its last quote included: -1 when
317
+ * it never ends. Two quotes inside it are one, and with `escapes` a backslash takes the next
318
+ * character with it.
319
+ *
320
+ * -1 too for backslashes that follow a character beyond ASCII and come before a quote. In a
321
+ * character set whose characters of two bytes may end on a backslash's byte (see `quoted`), the
322
+ * server takes the first of them for the end of the character before, and whether the quote
323
+ * ends the string turns around. Before any other character that changes nothing: the string
324
+ * ends where it ends here.
325
+ */
326
+ function stringEnd(statement: string, at: number, escapes: boolean): number {
327
+ let i = at + 1
328
+ while (i < statement.length) {
329
+ const code = statement.charCodeAt(i)
330
+ if (code === QUOTE) {
331
+ if (statement.charCodeAt(i + 1) !== QUOTE) return i + 1
332
+ i += 2
333
+ } else if (code === BACKSLASH && escapes) {
334
+ const end = runEnd(statement, i, isBackslash)
335
+ if (statement.charCodeAt(end) === QUOTE && statement.charCodeAt(i - 1) >= BEYOND_ASCII) {
336
+ return -1
337
+ }
338
+ // Two by two they are backslashes: one left over takes the character after them.
339
+ i = end + ((end - i) % 2)
340
+ } else i += 1
341
+ }
342
+ return -1
343
+ }
344
+
345
+ /**
346
+ * Where the number at `at` ends: a sign, digits with a point among them, an exponent. -1 when
347
+ * it isn't one, or when more of a word follows it: the server reads `1st` as a name.
348
+ */
349
+ function numberEnd(statement: string, at: number): number {
350
+ const first = signEnd(statement, at)
351
+ const whole = runEnd(statement, first, isDigit)
352
+ const point = statement.charCodeAt(whole) === DOT
353
+ let end = point ? runEnd(statement, whole + 1, isDigit) : whole
354
+ if (end - first === (point ? 1 : 0)) return -1
355
+ if ((statement.charCodeAt(end) | SMALL) === LOWER_E) {
356
+ const exponent = signEnd(statement, end + 1)
357
+ end = runEnd(statement, exponent, isDigit)
358
+ if (end === exponent) return -1
359
+ }
360
+ return isWordPart(statement.charCodeAt(end)) ? -1 : end
361
+ }
362
+
363
+ /** Where a number's digits start: after the sign at `at`, when one is there. */
364
+ function signEnd(statement: string, at: number): number {
365
+ const code = statement.charCodeAt(at)
366
+ return code === PLUS || code === MINUS ? at + 1 : at
367
+ }
368
+
369
+ /** Where the characters that `is` one of, from `at` on, end. */
370
+ function runEnd(statement: string, at: number, is: (code: number) => boolean): number {
371
+ let i = at
372
+ while (i < statement.length && is(statement.charCodeAt(i))) i += 1
373
+ return i
374
+ }
375
+
376
+ function isDigit(code: number): boolean {
377
+ return code >= 0x30 && code <= 0x39
378
+ }
379
+
380
+ function isHexDigit(code: number): boolean {
381
+ const small = code | SMALL
382
+ return isDigit(code) || (small >= 0x61 && small <= 0x66)
383
+ }
384
+
385
+ function isBit(code: number): boolean {
386
+ return code === ZERO || code === ZERO + 1
387
+ }
388
+
389
+ function isBackslash(code: number): boolean {
390
+ return code === BACKSLASH
391
+ }
392
+
393
+ /** White space as the server skips it between words: a space, and the five from tab to return. */
394
+ function isGap(code: number): boolean {
395
+ return code === 0x20 || (code >= 0x09 && code <= 0x0d)
396
+ }
397
+
398
+ /** What a bare word is made of, to the server: letters, digits, _ and $, and all beyond ASCII. */
399
+ function isWordPart(code: number): boolean {
400
+ return (
401
+ (code >= 0x30 && code <= 0x39) ||
402
+ (code >= 0x41 && code <= 0x5a) ||
403
+ (code >= 0x61 && code <= 0x7a) ||
404
+ code === 0x5f ||
405
+ code === 0x24 ||
406
+ code >= 0x80
407
+ )
408
+ }
409
+
410
+ /** Where the white space that starts at `at` ends. */
411
+ function gapAfter(statement: string, at: number): number {
412
+ let i = at
413
+ while (i < statement.length && isGap(statement.charCodeAt(i))) i += 1
414
+ return i
415
+ }
416
+
417
+ /** Where the white space that ends at `at` starts. */
418
+ function gapBefore(statement: string, at: number): number {
419
+ let i = at
420
+ while (i > 0 && isGap(statement.charCodeAt(i - 1))) i -= 1
421
+ return i
422
+ }
423
+
424
+ /** Where the bare word that starts at `at` ends: `at` itself when none starts there. */
425
+ function wordEnd(statement: string, at: number): number {
426
+ let i = at
427
+ while (i < statement.length && isWordPart(statement.charCodeAt(i))) i += 1
428
+ return i
429
+ }
430
+
431
+ /** `text` without the first half of a character cut in two at its end. */
432
+ function wholeEnd(text: string): string {
433
+ const last = text.charCodeAt(text.length - 1)
434
+ return last >= 0xd800 && last <= 0xdbff ? text.slice(0, -1) : text
435
+ }
436
+
437
+ /** `text` without the second half of a character cut in two at its start. */
438
+ function wholeStart(text: string): string {
439
+ const first = text.charCodeAt(0)
440
+ return first >= 0xdc00 && first <= 0xdfff ? text.slice(1) : text
441
+ }
442
+
443
+ /**
444
+ * A batch's statements spread over audit events of about 32 KiB each, in their order, measured
445
+ * as the UTF-8 they are written as. A statement is never cut, so one longer than that has an
446
+ * event to itself: whole, as every statement but an INSERT or REPLACE of plain rows is logged, and
447
+ * so as long as the statement. A batch of no statements is one part with none.
448
+ */
449
+ export function auditParts(statements: readonly string[], reading: AuditReading): AuditPart[] {
450
+ const parts: AuditPart[] = []
451
+ let part: AuditPart = { from: 0, count: 0, statement: '' }
452
+ let bytes = 0
453
+ statements.forEach((statement, i) => {
454
+ const text = `${logged(statement, reading)};\n`
455
+ const more = Buffer.byteLength(text, 'utf8')
456
+ if (part.count && bytes + more > AUDIT_EVENT_BYTES) {
457
+ parts.push(part)
458
+ part = { from: i, count: 0, statement: '' }
459
+ bytes = 0
460
+ }
461
+ part.statement += text
462
+ part.count += 1
463
+ bytes += more
464
+ })
465
+ parts.push(part)
466
+ return parts
467
+ }
@@ -0,0 +1,46 @@
1
+ /*
2
+ * How a session's character set reads an import file's text, which the browser cuts and checks
3
+ * as UTF-8. One place for the importer, which refuses statements by it, the server, which ends a
4
+ * batch where it changes, and the browser's check, which warns by it and records it. Nothing
5
+ * here is from Node.
6
+ */
7
+
8
+ /**
9
+ * The character sets a session reads every text in as the browser does: they read ASCII as
10
+ * ASCII, and take no byte of it into a character of more than one. So the server ends each
11
+ * string, name and comment where the browser did. UTF-8's own sets, and those of one byte a
12
+ * character but swe7, which reads @, [, \, ], ^, `, {, |, } and ~ as letters. Every one was
13
+ * tried on MySQL 8.4 and MariaDB 11.4.
14
+ */
15
+ export const READ_ALIKE: ReadonlySet<string> = new Set([
16
+ ...['utf8mb4', 'utf8mb3', 'utf8', 'binary', 'ascii', 'armscii8', 'dec8', 'geostd8', 'greek'],
17
+ ...['hebrew', 'hp8', 'keybcs2', 'koi8r', 'koi8u', 'macce', 'macroman', 'tis620'],
18
+ ...['latin1', 'latin2', 'latin5', 'latin7', 'cp1250', 'cp1251', 'cp1256', 'cp1257'],
19
+ ...['cp850', 'cp852', 'cp866'],
20
+ ])
21
+
22
+ /**
23
+ * The sets of several bytes a character that read ASCII as ASCII, and read other text otherwise.
24
+ * Under gbk, big5, sjis, cp932 and gb18030 the bytes of the file's UTF-8 make other characters,
25
+ * and one can end on a backslash or a backtick that follows: a string or a name then ends where
26
+ * the browser did not see it end, and what the browser read as its text is SQL. Under these, a
27
+ * statement goes only while it is ASCII. The rest of the list is not known to do that, and not
28
+ * known to read beyond ASCII as UTF-8 does either: ASCII is what goes under them too. Under any
29
+ * set in neither list, swe7 or one a server adds, nothing does.
30
+ */
31
+ export const READ_ALIKE_IN_ASCII: ReadonlySet<string> = new Set([
32
+ ...['big5', 'cp932', 'gb18030', 'gbk', 'sjis'],
33
+ ...['eucjpms', 'euckr', 'gb2312', 'ujis'],
34
+ ])
35
+
36
+ /**
37
+ * How a set reads the file's text: `alike`, all of it as it was cut (READ_ALIKE); `ascii`, a
38
+ * statement only while it is ASCII (READ_ALIKE_IN_ASCII); `otherwise`, not even that.
39
+ */
40
+ export type CharacterSetReading = 'alike' | 'ascii' | 'otherwise'
41
+
42
+ /** How the character set of this name, in small letters, reads the file's text. */
43
+ export function readsAs(set: string): CharacterSetReading {
44
+ if (READ_ALIKE.has(set)) return 'alike'
45
+ return READ_ALIKE_IN_ASCII.has(set) ? 'ascii' : 'otherwise'
46
+ }