@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,191 @@
|
|
|
1
|
+
import { DbAdminError } from '../errors.ts'
|
|
2
|
+
import {
|
|
3
|
+
IMPORT_BATCH_BYTES,
|
|
4
|
+
IMPORT_BATCH_STATEMENTS,
|
|
5
|
+
IMPORT_LENGTH_BYTES,
|
|
6
|
+
type ImportBatch,
|
|
7
|
+
} from './protocol.ts'
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* A batch of an import's statements as one request body: for each statement its length in bytes,
|
|
11
|
+
* as four bytes with the big end first, then that many bytes of UTF-8. The browser writes it and
|
|
12
|
+
* the server reads it, so the server never looks for where a statement ends. And which
|
|
13
|
+
* statements of the file those are, which both count the same way. Plain functions, nothing
|
|
14
|
+
* from Node.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** The last statement a batch may start at: every number it reaches is one that can be counted. */
|
|
18
|
+
const LAST_START = Number.MAX_SAFE_INTEGER - IMPORT_BATCH_STATEMENTS
|
|
19
|
+
|
|
20
|
+
/** Half of a surrogate pair on its own: with the `u` flag a whole pair is one character. */
|
|
21
|
+
const HALF_PAIR = /[\ud800-\udfff]/u
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The statements as a batch's body. It refuses what `decodeFrames` would: no statements or more
|
|
25
|
+
* than 1,000, an empty one, more than 4 MiB in all. And a statement with half a surrogate pair,
|
|
26
|
+
* which would be sent as another character than the one written.
|
|
27
|
+
*/
|
|
28
|
+
export function encodeFrames(statements: readonly string[]): Uint8Array {
|
|
29
|
+
mayHold(statements.length)
|
|
30
|
+
const encoder = new TextEncoder()
|
|
31
|
+
const texts: Uint8Array[] = []
|
|
32
|
+
let size = 0
|
|
33
|
+
statements.forEach((statement, i) => {
|
|
34
|
+
if (!statement) throw empty(i + 1)
|
|
35
|
+
if (HALF_PAIR.test(statement)) {
|
|
36
|
+
throw bad(`${nth(i + 1)} isn't whole Unicode: it has half of a surrogate pair.`)
|
|
37
|
+
}
|
|
38
|
+
const text = encoder.encode(statement)
|
|
39
|
+
size += IMPORT_LENGTH_BYTES + text.byteLength
|
|
40
|
+
if (size > IMPORT_BATCH_BYTES) throw tooLarge()
|
|
41
|
+
texts.push(text)
|
|
42
|
+
})
|
|
43
|
+
const body = new Uint8Array(size)
|
|
44
|
+
const lengths = new DataView(body.buffer)
|
|
45
|
+
let at = 0
|
|
46
|
+
for (const text of texts) {
|
|
47
|
+
lengths.setUint32(at, text.byteLength)
|
|
48
|
+
body.set(text, at + IMPORT_LENGTH_BYTES)
|
|
49
|
+
at += IMPORT_LENGTH_BYTES + text.byteLength
|
|
50
|
+
}
|
|
51
|
+
return body
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The statements of a batch's body, which the request says holds `expected` of them. Everything
|
|
56
|
+
* is checked before anything is answered, so nothing of a wrong batch runs: `expected` is 1 to
|
|
57
|
+
* 1,000, each length fits in what is left, no statement is empty, the body ends with the last of
|
|
58
|
+
* them, and each is UTF-8, read strictly.
|
|
59
|
+
*/
|
|
60
|
+
export function decodeFrames(body: Uint8Array, expected: number): string[] {
|
|
61
|
+
mayHold(expected)
|
|
62
|
+
if (body.byteLength > IMPORT_BATCH_BYTES) throw tooLarge()
|
|
63
|
+
// A byte order mark is the statement's own text, kept; anything that isn't UTF-8 throws.
|
|
64
|
+
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
|
|
65
|
+
return framesOf(body, expected).map((frame, i) => {
|
|
66
|
+
try {
|
|
67
|
+
return decoder.decode(frame)
|
|
68
|
+
} catch {
|
|
69
|
+
throw bad(`${nth(i + 1)} isn't UTF-8.`)
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Each statement's bytes, once the lengths have been found to add up to the body exactly. */
|
|
75
|
+
function framesOf(body: Uint8Array, expected: number): Uint8Array[] {
|
|
76
|
+
const lengths = new DataView(body.buffer, body.byteOffset, body.byteLength)
|
|
77
|
+
const frames: Uint8Array[] = []
|
|
78
|
+
let at = 0
|
|
79
|
+
while (at < body.byteLength) {
|
|
80
|
+
const n = frames.length + 1
|
|
81
|
+
if (n > expected) {
|
|
82
|
+
throw bad(
|
|
83
|
+
`The batch goes on after statement ${number(expected)}, which the request says is its last.`,
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
if (body.byteLength - at < IMPORT_LENGTH_BYTES) {
|
|
87
|
+
throw bad(`The batch ends inside the length of statement ${number(n)}.`)
|
|
88
|
+
}
|
|
89
|
+
// Unsigned: a length with its high bit set is 2 GiB or more, never a negative that fits.
|
|
90
|
+
const size = lengths.getUint32(at)
|
|
91
|
+
at += IMPORT_LENGTH_BYTES
|
|
92
|
+
const left = body.byteLength - at
|
|
93
|
+
if (!size) throw empty(n)
|
|
94
|
+
if (size > left) {
|
|
95
|
+
throw bad(
|
|
96
|
+
`${nth(n)} says it is ${counted(size, 'byte')} long, with ${counted(left, 'byte')} left.`,
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
frames.push(body.subarray(at, at + size))
|
|
100
|
+
at += size
|
|
101
|
+
}
|
|
102
|
+
if (frames.length !== expected) {
|
|
103
|
+
throw bad(
|
|
104
|
+
`The batch holds ${counted(frames.length, 'statement')}, and the request says ${number(expected)}.`,
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
return frames
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The number in the file of each of a batch's frames, in their order: from `start`, stepping
|
|
112
|
+
* over a statement left out. The server numbers its `statement` lines with it and the browser
|
|
113
|
+
* reads them with it. It refuses (400) what no batch is: a `start` that isn't a statement's
|
|
114
|
+
* number, a count past 1,000 or of none with nothing left out, a `skip` the batch doesn't reach.
|
|
115
|
+
*/
|
|
116
|
+
export function batchNumbers(batch: ImportBatch): number[] {
|
|
117
|
+
const { start, count, skip } = checked(batch)
|
|
118
|
+
return Array.from({ length: count }, (_, i) =>
|
|
119
|
+
skip === undefined || start + i < skip ? start + i : start + i + 1,
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The number of the last statement a batch covers, run or left out: the server's position once
|
|
125
|
+
* all of it is done. A statement left out counts, also when it comes after the last frame.
|
|
126
|
+
*/
|
|
127
|
+
export function batchLast(batch: ImportBatch): number {
|
|
128
|
+
const { start, count, skip } = checked(batch)
|
|
129
|
+
return skip === undefined ? start + count - 1 : start + count
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The batch, once its numbers are found to be ones a batch has. */
|
|
133
|
+
function checked(batch: ImportBatch): ImportBatch {
|
|
134
|
+
const { start, count, skip } = batch
|
|
135
|
+
if (!Number.isInteger(start) || start < 1 || start > LAST_START) {
|
|
136
|
+
throw bad(`A batch starts at a statement's number, 1 or more, not ${number(start)}.`)
|
|
137
|
+
}
|
|
138
|
+
if (skip === undefined) {
|
|
139
|
+
mayHold(count)
|
|
140
|
+
return batch
|
|
141
|
+
}
|
|
142
|
+
if (!Number.isInteger(count) || count < 0 || count > IMPORT_BATCH_STATEMENTS) {
|
|
143
|
+
throw bad(
|
|
144
|
+
`A batch that leaves a statement out holds 0 to ${number(IMPORT_BATCH_STATEMENTS)} statements, not ${number(count)}.`,
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
if (!Number.isInteger(skip) || skip < start || skip > start + count) {
|
|
148
|
+
throw bad(
|
|
149
|
+
`A batch of statements ${number(start)} to ${number(start + count)} can't leave out statement ${number(skip)}.`,
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
return batch
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Refuses a count of statements no batch has. */
|
|
156
|
+
function mayHold(count: number): void {
|
|
157
|
+
if (!Number.isInteger(count) || count < 1 || count > IMPORT_BATCH_STATEMENTS) {
|
|
158
|
+
throw bad(
|
|
159
|
+
`A batch holds 1 to ${number(IMPORT_BATCH_STATEMENTS)} statements, not ${number(count)}.`,
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function empty(n: number): DbAdminError {
|
|
165
|
+
return bad(`${nth(n)} is empty.`)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function tooLarge(): DbAdminError {
|
|
169
|
+
return new DbAdminError(
|
|
170
|
+
`A batch of statements is ${number(IMPORT_BATCH_BYTES / 1_048_576)} MB at most.`,
|
|
171
|
+
413,
|
|
172
|
+
'too-large',
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function bad(message: string): DbAdminError {
|
|
177
|
+
return new DbAdminError(message, 400, 'bad-request')
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A statement by its place in the batch, from 1: the batch doesn't know its number in the file. */
|
|
181
|
+
function nth(n: number): string {
|
|
182
|
+
return `Statement ${number(n)} of the batch`
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function counted(count: number, noun: string): string {
|
|
186
|
+
return `${number(count)} ${noun}${count === 1 ? '' : 's'}`
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function number(value: number): string {
|
|
190
|
+
return value.toLocaleString('en')
|
|
191
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* What a MySQL or MariaDB login may do in a database, read from the lines of its SHOW GRANTS.
|
|
3
|
+
* For the import dialog's check, which is a help and not a guard: the server decides at each
|
|
4
|
+
* statement. Nothing of a line is passed on but the privileges' names: MariaDB writes the
|
|
5
|
+
* login's password hash into the same line.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A line of SHOW GRANTS that is about everything (`*.*`) or about databases by name. */
|
|
9
|
+
export interface Grant {
|
|
10
|
+
/** A REVOKE: MySQL lists what was taken back, for one database, from a grant on everything. */
|
|
11
|
+
revoke: boolean
|
|
12
|
+
/** In the server's own words and capitals, such as `CREATE` and `LOCK TABLES`. */
|
|
13
|
+
privileges: string[]
|
|
14
|
+
/** The database as granted, which is a pattern (`_`, `%`, and `\` before either); null for `*.*`. */
|
|
15
|
+
database: string | null
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A privilege's name: words in capitals, one space between them. */
|
|
19
|
+
const PRIVILEGE = /^[A-Z_]+(?: [A-Z_]+)*$/
|
|
20
|
+
|
|
21
|
+
/** The quotes a server writes a name in: backticks, or double quotes under ANSI_QUOTES. */
|
|
22
|
+
const QUOTES: ReadonlySet<string> = new Set(['`', '"'])
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* What one line of SHOW GRANTS grants or takes back, when it is about everything or a whole
|
|
26
|
+
* database; null for any other line: a table's, a column's, a routine's, a role's, a proxy's.
|
|
27
|
+
*/
|
|
28
|
+
export function parseGrant(line: string): Grant | null {
|
|
29
|
+
const revoke = line.startsWith('REVOKE ')
|
|
30
|
+
if (!revoke && !line.startsWith('GRANT ')) return null
|
|
31
|
+
const from = revoke ? 'REVOKE '.length : 'GRANT '.length
|
|
32
|
+
const on = line.indexOf(' ON ', from)
|
|
33
|
+
if (on === -1) return null
|
|
34
|
+
// Names only. A list with a bracket or a quote in it is about columns, and what looks like
|
|
35
|
+
// its ` ON ` may be inside a column's name: such a line is about one table, and not counted.
|
|
36
|
+
const privileges = line
|
|
37
|
+
.slice(from, on)
|
|
38
|
+
.split(',')
|
|
39
|
+
.map((name) => name.trim())
|
|
40
|
+
if (!privileges.every((name) => PRIVILEGE.test(name))) return null
|
|
41
|
+
const object = line.slice(on + ' ON '.length)
|
|
42
|
+
if (object.startsWith('*.* ')) return { revoke, privileges, database: null }
|
|
43
|
+
const named = quotedName(object)
|
|
44
|
+
if (!named || !object.startsWith('.* ', named.end)) return null
|
|
45
|
+
return { revoke, privileges, database: named.name }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The quoted name `text` starts with, and where it ends; null when it starts with none. */
|
|
49
|
+
function quotedName(text: string): { name: string; end: number } | null {
|
|
50
|
+
const quote = text[0]
|
|
51
|
+
if (quote === undefined || !QUOTES.has(quote)) return null
|
|
52
|
+
let name = ''
|
|
53
|
+
let at = 1
|
|
54
|
+
for (;;) {
|
|
55
|
+
const next = text.indexOf(quote, at)
|
|
56
|
+
if (next === -1) return null
|
|
57
|
+
name += text.slice(at, next)
|
|
58
|
+
// A quote written twice is one quote of the name.
|
|
59
|
+
if (text[next + 1] !== quote) return { name, end: next + 1 }
|
|
60
|
+
name += quote
|
|
61
|
+
at = next + 2
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Whether a database's name fits the pattern a grant names: `_` is any one character, `%` any
|
|
67
|
+
* run of them, and a backslash makes the next character itself. Names are compared as written.
|
|
68
|
+
*/
|
|
69
|
+
export function grantCovers(pattern: string, database: string): boolean {
|
|
70
|
+
const parts: { any?: 'one' | 'run'; char?: string }[] = []
|
|
71
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
72
|
+
const char = pattern[i]!
|
|
73
|
+
if (char === '\\' && i + 1 < pattern.length) parts.push({ char: pattern[++i]! })
|
|
74
|
+
else if (char === '_') parts.push({ any: 'one' })
|
|
75
|
+
else if (char === '%') parts.push({ any: 'run' })
|
|
76
|
+
else parts.push({ char })
|
|
77
|
+
}
|
|
78
|
+
let p = 0
|
|
79
|
+
let d = 0
|
|
80
|
+
/** Where to go back to when the match fails after a `%`: it then takes one character more. */
|
|
81
|
+
let run: { p: number; d: number } | null = null
|
|
82
|
+
while (d < database.length) {
|
|
83
|
+
const part = parts[p]
|
|
84
|
+
if (part?.any === 'run') {
|
|
85
|
+
run = { p: p + 1, d }
|
|
86
|
+
p += 1
|
|
87
|
+
} else if (part && (part.any === 'one' || part.char === database[d])) {
|
|
88
|
+
p += 1
|
|
89
|
+
d += 1
|
|
90
|
+
} else if (run) {
|
|
91
|
+
run = { p: run.p, d: run.d + 1 }
|
|
92
|
+
p = run.p
|
|
93
|
+
d = run.d
|
|
94
|
+
} else return false
|
|
95
|
+
}
|
|
96
|
+
while (parts[p]?.any === 'run') p += 1
|
|
97
|
+
return p === parts.length
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* What the login may do in `database`, from the lines of its SHOW GRANTS: its privileges on
|
|
102
|
+
* everything and on every pattern the name fits, less what a REVOKE for the database takes back.
|
|
103
|
+
* `ALL PRIVILEGES` stands for them all, and stays whatever was taken back from it; `USAGE` is
|
|
104
|
+
* no privilege, and is left out. Sorted.
|
|
105
|
+
*/
|
|
106
|
+
export function privilegesIn(grants: readonly string[], database: string): string[] {
|
|
107
|
+
const may = new Set<string>()
|
|
108
|
+
const taken = new Set<string>()
|
|
109
|
+
for (const line of grants) {
|
|
110
|
+
const grant = parseGrant(line)
|
|
111
|
+
if (!grant) continue
|
|
112
|
+
if (grant.database !== null && !grantCovers(grant.database, database)) continue
|
|
113
|
+
for (const privilege of grant.privileges) (grant.revoke ? taken : may).add(privilege)
|
|
114
|
+
}
|
|
115
|
+
may.delete('USAGE')
|
|
116
|
+
return [...may].filter((privilege) => !taken.has(privilege)).sort()
|
|
117
|
+
}
|