@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,39 @@
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
+ * The character sets a session reads every text in as the browser does: they read ASCII as
9
+ * ASCII, and take no byte of it into a character of more than one. So the server ends each
10
+ * string, name and comment where the browser did. UTF-8's own sets, and those of one byte a
11
+ * character but swe7, which reads @, [, \, ], ^, `, {, |, } and ~ as letters. Every one was
12
+ * tried on MySQL 8.4 and MariaDB 11.4.
13
+ */
14
+ export const READ_ALIKE = new Set([
15
+ ...['utf8mb4', 'utf8mb3', 'utf8', 'binary', 'ascii', 'armscii8', 'dec8', 'geostd8', 'greek'],
16
+ ...['hebrew', 'hp8', 'keybcs2', 'koi8r', 'koi8u', 'macce', 'macroman', 'tis620'],
17
+ ...['latin1', 'latin2', 'latin5', 'latin7', 'cp1250', 'cp1251', 'cp1256', 'cp1257'],
18
+ ...['cp850', 'cp852', 'cp866'],
19
+ ]);
20
+ /**
21
+ * The sets of several bytes a character that read ASCII as ASCII, and read other text otherwise.
22
+ * Under gbk, big5, sjis, cp932 and gb18030 the bytes of the file's UTF-8 make other characters,
23
+ * and one can end on a backslash or a backtick that follows: a string or a name then ends where
24
+ * the browser did not see it end, and what the browser read as its text is SQL. Under these, a
25
+ * statement goes only while it is ASCII. The rest of the list is not known to do that, and not
26
+ * known to read beyond ASCII as UTF-8 does either: ASCII is what goes under them too. Under any
27
+ * set in neither list, swe7 or one a server adds, nothing does.
28
+ */
29
+ export const READ_ALIKE_IN_ASCII = new Set([
30
+ ...['big5', 'cp932', 'gb18030', 'gbk', 'sjis'],
31
+ ...['eucjpms', 'euckr', 'gb2312', 'ujis'],
32
+ ]);
33
+ /** How the character set of this name, in small letters, reads the file's text. */
34
+ export function readsAs(set) {
35
+ if (READ_ALIKE.has(set))
36
+ return 'alike';
37
+ return READ_ALIKE_IN_ASCII.has(set) ? 'ascii' : 'otherwise';
38
+ }
39
+ //# sourceMappingURL=charsets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"charsets.js","sourceRoot":"","sources":["../../src/import/charsets.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAwB,IAAI,GAAG,CAAC;IACrD,GAAG,CAAC,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC;IAC5F,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC;IAChF,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC;IACnF,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC;CAC/B,CAAC,CAAA;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAwB,IAAI,GAAG,CAAC;IAC9D,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC;IAC9C,GAAG,CAAC,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC;CAC1C,CAAC,CAAA;AAQF,mFAAmF;AACnF,MAAM,UAAU,OAAO,CAAC,GAAW;IACjC,IAAI,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC;QAAE,OAAO,OAAO,CAAA;IACvC,OAAO,mBAAmB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAA;AAC7D,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { type ImportBatch } from './protocol.ts';
2
+ /**
3
+ * The statements as a batch's body. It refuses what `decodeFrames` would: no statements or more
4
+ * than 1,000, an empty one, more than 4 MiB in all. And a statement with half a surrogate pair,
5
+ * which would be sent as another character than the one written.
6
+ */
7
+ export declare function encodeFrames(statements: readonly string[]): Uint8Array;
8
+ /**
9
+ * The statements of a batch's body, which the request says holds `expected` of them. Everything
10
+ * is checked before anything is answered, so nothing of a wrong batch runs: `expected` is 1 to
11
+ * 1,000, each length fits in what is left, no statement is empty, the body ends with the last of
12
+ * them, and each is UTF-8, read strictly.
13
+ */
14
+ export declare function decodeFrames(body: Uint8Array, expected: number): string[];
15
+ /**
16
+ * The number in the file of each of a batch's frames, in their order: from `start`, stepping
17
+ * over a statement left out. The server numbers its `statement` lines with it and the browser
18
+ * reads them with it. It refuses (400) what no batch is: a `start` that isn't a statement's
19
+ * number, a count past 1,000 or of none with nothing left out, a `skip` the batch doesn't reach.
20
+ */
21
+ export declare function batchNumbers(batch: ImportBatch): number[];
22
+ /**
23
+ * The number of the last statement a batch covers, run or left out: the server's position once
24
+ * all of it is done. A statement left out counts, also when it comes after the last frame.
25
+ */
26
+ export declare function batchLast(batch: ImportBatch): number;
@@ -0,0 +1,158 @@
1
+ import { DbAdminError } from "../errors.js";
2
+ import { IMPORT_BATCH_BYTES, IMPORT_BATCH_STATEMENTS, IMPORT_LENGTH_BYTES, } from "./protocol.js";
3
+ /*
4
+ * A batch of an import's statements as one request body: for each statement its length in bytes,
5
+ * as four bytes with the big end first, then that many bytes of UTF-8. The browser writes it and
6
+ * the server reads it, so the server never looks for where a statement ends. And which
7
+ * statements of the file those are, which both count the same way. Plain functions, nothing
8
+ * from Node.
9
+ */
10
+ /** The last statement a batch may start at: every number it reaches is one that can be counted. */
11
+ const LAST_START = Number.MAX_SAFE_INTEGER - IMPORT_BATCH_STATEMENTS;
12
+ /** Half of a surrogate pair on its own: with the `u` flag a whole pair is one character. */
13
+ const HALF_PAIR = /[\ud800-\udfff]/u;
14
+ /**
15
+ * The statements as a batch's body. It refuses what `decodeFrames` would: no statements or more
16
+ * than 1,000, an empty one, more than 4 MiB in all. And a statement with half a surrogate pair,
17
+ * which would be sent as another character than the one written.
18
+ */
19
+ export function encodeFrames(statements) {
20
+ mayHold(statements.length);
21
+ const encoder = new TextEncoder();
22
+ const texts = [];
23
+ let size = 0;
24
+ statements.forEach((statement, i) => {
25
+ if (!statement)
26
+ throw empty(i + 1);
27
+ if (HALF_PAIR.test(statement)) {
28
+ throw bad(`${nth(i + 1)} isn't whole Unicode: it has half of a surrogate pair.`);
29
+ }
30
+ const text = encoder.encode(statement);
31
+ size += IMPORT_LENGTH_BYTES + text.byteLength;
32
+ if (size > IMPORT_BATCH_BYTES)
33
+ throw tooLarge();
34
+ texts.push(text);
35
+ });
36
+ const body = new Uint8Array(size);
37
+ const lengths = new DataView(body.buffer);
38
+ let at = 0;
39
+ for (const text of texts) {
40
+ lengths.setUint32(at, text.byteLength);
41
+ body.set(text, at + IMPORT_LENGTH_BYTES);
42
+ at += IMPORT_LENGTH_BYTES + text.byteLength;
43
+ }
44
+ return body;
45
+ }
46
+ /**
47
+ * The statements of a batch's body, which the request says holds `expected` of them. Everything
48
+ * is checked before anything is answered, so nothing of a wrong batch runs: `expected` is 1 to
49
+ * 1,000, each length fits in what is left, no statement is empty, the body ends with the last of
50
+ * them, and each is UTF-8, read strictly.
51
+ */
52
+ export function decodeFrames(body, expected) {
53
+ mayHold(expected);
54
+ if (body.byteLength > IMPORT_BATCH_BYTES)
55
+ throw tooLarge();
56
+ // A byte order mark is the statement's own text, kept; anything that isn't UTF-8 throws.
57
+ const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
58
+ return framesOf(body, expected).map((frame, i) => {
59
+ try {
60
+ return decoder.decode(frame);
61
+ }
62
+ catch {
63
+ throw bad(`${nth(i + 1)} isn't UTF-8.`);
64
+ }
65
+ });
66
+ }
67
+ /** Each statement's bytes, once the lengths have been found to add up to the body exactly. */
68
+ function framesOf(body, expected) {
69
+ const lengths = new DataView(body.buffer, body.byteOffset, body.byteLength);
70
+ const frames = [];
71
+ let at = 0;
72
+ while (at < body.byteLength) {
73
+ const n = frames.length + 1;
74
+ if (n > expected) {
75
+ throw bad(`The batch goes on after statement ${number(expected)}, which the request says is its last.`);
76
+ }
77
+ if (body.byteLength - at < IMPORT_LENGTH_BYTES) {
78
+ throw bad(`The batch ends inside the length of statement ${number(n)}.`);
79
+ }
80
+ // Unsigned: a length with its high bit set is 2 GiB or more, never a negative that fits.
81
+ const size = lengths.getUint32(at);
82
+ at += IMPORT_LENGTH_BYTES;
83
+ const left = body.byteLength - at;
84
+ if (!size)
85
+ throw empty(n);
86
+ if (size > left) {
87
+ throw bad(`${nth(n)} says it is ${counted(size, 'byte')} long, with ${counted(left, 'byte')} left.`);
88
+ }
89
+ frames.push(body.subarray(at, at + size));
90
+ at += size;
91
+ }
92
+ if (frames.length !== expected) {
93
+ throw bad(`The batch holds ${counted(frames.length, 'statement')}, and the request says ${number(expected)}.`);
94
+ }
95
+ return frames;
96
+ }
97
+ /**
98
+ * The number in the file of each of a batch's frames, in their order: from `start`, stepping
99
+ * over a statement left out. The server numbers its `statement` lines with it and the browser
100
+ * reads them with it. It refuses (400) what no batch is: a `start` that isn't a statement's
101
+ * number, a count past 1,000 or of none with nothing left out, a `skip` the batch doesn't reach.
102
+ */
103
+ export function batchNumbers(batch) {
104
+ const { start, count, skip } = checked(batch);
105
+ return Array.from({ length: count }, (_, i) => skip === undefined || start + i < skip ? start + i : start + i + 1);
106
+ }
107
+ /**
108
+ * The number of the last statement a batch covers, run or left out: the server's position once
109
+ * all of it is done. A statement left out counts, also when it comes after the last frame.
110
+ */
111
+ export function batchLast(batch) {
112
+ const { start, count, skip } = checked(batch);
113
+ return skip === undefined ? start + count - 1 : start + count;
114
+ }
115
+ /** The batch, once its numbers are found to be ones a batch has. */
116
+ function checked(batch) {
117
+ const { start, count, skip } = batch;
118
+ if (!Number.isInteger(start) || start < 1 || start > LAST_START) {
119
+ throw bad(`A batch starts at a statement's number, 1 or more, not ${number(start)}.`);
120
+ }
121
+ if (skip === undefined) {
122
+ mayHold(count);
123
+ return batch;
124
+ }
125
+ if (!Number.isInteger(count) || count < 0 || count > IMPORT_BATCH_STATEMENTS) {
126
+ throw bad(`A batch that leaves a statement out holds 0 to ${number(IMPORT_BATCH_STATEMENTS)} statements, not ${number(count)}.`);
127
+ }
128
+ if (!Number.isInteger(skip) || skip < start || skip > start + count) {
129
+ throw bad(`A batch of statements ${number(start)} to ${number(start + count)} can't leave out statement ${number(skip)}.`);
130
+ }
131
+ return batch;
132
+ }
133
+ /** Refuses a count of statements no batch has. */
134
+ function mayHold(count) {
135
+ if (!Number.isInteger(count) || count < 1 || count > IMPORT_BATCH_STATEMENTS) {
136
+ throw bad(`A batch holds 1 to ${number(IMPORT_BATCH_STATEMENTS)} statements, not ${number(count)}.`);
137
+ }
138
+ }
139
+ function empty(n) {
140
+ return bad(`${nth(n)} is empty.`);
141
+ }
142
+ function tooLarge() {
143
+ return new DbAdminError(`A batch of statements is ${number(IMPORT_BATCH_BYTES / 1_048_576)} MB at most.`, 413, 'too-large');
144
+ }
145
+ function bad(message) {
146
+ return new DbAdminError(message, 400, 'bad-request');
147
+ }
148
+ /** A statement by its place in the batch, from 1: the batch doesn't know its number in the file. */
149
+ function nth(n) {
150
+ return `Statement ${number(n)} of the batch`;
151
+ }
152
+ function counted(count, noun) {
153
+ return `${number(count)} ${noun}${count === 1 ? '' : 's'}`;
154
+ }
155
+ function number(value) {
156
+ return value.toLocaleString('en');
157
+ }
158
+ //# sourceMappingURL=frames.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frames.js","sourceRoot":"","sources":["../../src/import/frames.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,mBAAmB,GAEpB,MAAM,eAAe,CAAA;AAEtB;;;;;;GAMG;AAEH,mGAAmG;AACnG,MAAM,UAAU,GAAG,MAAM,CAAC,gBAAgB,GAAG,uBAAuB,CAAA;AAEpE,4FAA4F;AAC5F,MAAM,SAAS,GAAG,kBAAkB,CAAA;AAEpC;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,UAA6B;IACxD,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;IAC1B,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IACjC,MAAM,KAAK,GAAiB,EAAE,CAAA;IAC9B,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,UAAU,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE;QAClC,IAAI,CAAC,SAAS;YAAE,MAAM,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;QAClC,IAAI,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;YAC9B,MAAM,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,wDAAwD,CAAC,CAAA;QAClF,CAAC;QACD,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QACtC,IAAI,IAAI,mBAAmB,GAAG,IAAI,CAA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AS,CAAC,cAAc,EAChF,GAAG,EACH,WAAW,CACZ,CAAA;AACH,CAAC;AAED,SAAS,GAAG,CAAC,OAAe;IAC1B,OAAO,IAAI,YAAY,CAAC,OAAO,EAAE,GAAG,EAAE,aAAa,CAAC,CAAA;AACtD,CAAC;AAED,oGAAoG;AACpG,SAAS,GAAG,CAAC,CAAS;IACpB,OAAO,aAAa,MAAM,CAAC,CAAC,CAAC,eAAe,CAAA;AAC9C,CAAC;AAED,SAAS,OAAO,CAAC,KAAa,EAAE,IAAY;IAC1C,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;AAC5D,CAAC;AAED,SAAS,MAAM,CAAC,KAAa;IAC3B,OAAO,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;AACnC,CAAC"}
@@ -0,0 +1,26 @@
1
+ /** A line of SHOW GRANTS that is about everything (`*.*`) or about databases by name. */
2
+ export interface Grant {
3
+ /** A REVOKE: MySQL lists what was taken back, for one database, from a grant on everything. */
4
+ revoke: boolean;
5
+ /** In the server's own words and capitals, such as `CREATE` and `LOCK TABLES`. */
6
+ privileges: string[];
7
+ /** The database as granted, which is a pattern (`_`, `%`, and `\` before either); null for `*.*`. */
8
+ database: string | null;
9
+ }
10
+ /**
11
+ * What one line of SHOW GRANTS grants or takes back, when it is about everything or a whole
12
+ * database; null for any other line: a table's, a column's, a routine's, a role's, a proxy's.
13
+ */
14
+ export declare function parseGrant(line: string): Grant | null;
15
+ /**
16
+ * Whether a database's name fits the pattern a grant names: `_` is any one character, `%` any
17
+ * run of them, and a backslash makes the next character itself. Names are compared as written.
18
+ */
19
+ export declare function grantCovers(pattern: string, database: string): boolean;
20
+ /**
21
+ * What the login may do in `database`, from the lines of its SHOW GRANTS: its privileges on
22
+ * everything and on every pattern the name fits, less what a REVOKE for the database takes back.
23
+ * `ALL PRIVILEGES` stands for them all, and stays whatever was taken back from it; `USAGE` is
24
+ * no privilege, and is left out. Sorted.
25
+ */
26
+ export declare function privilegesIn(grants: readonly string[], database: string): string[];
@@ -0,0 +1,122 @@
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
+ /** A privilege's name: words in capitals, one space between them. */
8
+ const PRIVILEGE = /^[A-Z_]+(?: [A-Z_]+)*$/;
9
+ /** The quotes a server writes a name in: backticks, or double quotes under ANSI_QUOTES. */
10
+ const QUOTES = new Set(['`', '"']);
11
+ /**
12
+ * What one line of SHOW GRANTS grants or takes back, when it is about everything or a whole
13
+ * database; null for any other line: a table's, a column's, a routine's, a role's, a proxy's.
14
+ */
15
+ export function parseGrant(line) {
16
+ const revoke = line.startsWith('REVOKE ');
17
+ if (!revoke && !line.startsWith('GRANT '))
18
+ return null;
19
+ const from = revoke ? 'REVOKE '.length : 'GRANT '.length;
20
+ const on = line.indexOf(' ON ', from);
21
+ if (on === -1)
22
+ return null;
23
+ // Names only. A list with a bracket or a quote in it is about columns, and what looks like
24
+ // its ` ON ` may be inside a column's name: such a line is about one table, and not counted.
25
+ const privileges = line
26
+ .slice(from, on)
27
+ .split(',')
28
+ .map((name) => name.trim());
29
+ if (!privileges.every((name) => PRIVILEGE.test(name)))
30
+ return null;
31
+ const object = line.slice(on + ' ON '.length);
32
+ if (object.startsWith('*.* '))
33
+ return { revoke, privileges, database: null };
34
+ const named = quotedName(object);
35
+ if (!named || !object.startsWith('.* ', named.end))
36
+ return null;
37
+ return { revoke, privileges, database: named.name };
38
+ }
39
+ /** The quoted name `text` starts with, and where it ends; null when it starts with none. */
40
+ function quotedName(text) {
41
+ const quote = text[0];
42
+ if (quote === undefined || !QUOTES.has(quote))
43
+ return null;
44
+ let name = '';
45
+ let at = 1;
46
+ for (;;) {
47
+ const next = text.indexOf(quote, at);
48
+ if (next === -1)
49
+ return null;
50
+ name += text.slice(at, next);
51
+ // A quote written twice is one quote of the name.
52
+ if (text[next + 1] !== quote)
53
+ return { name, end: next + 1 };
54
+ name += quote;
55
+ at = next + 2;
56
+ }
57
+ }
58
+ /**
59
+ * Whether a database's name fits the pattern a grant names: `_` is any one character, `%` any
60
+ * run of them, and a backslash makes the next character itself. Names are compared as written.
61
+ */
62
+ export function grantCovers(pattern, database) {
63
+ const parts = [];
64
+ for (let i = 0; i < pattern.length; i++) {
65
+ const char = pattern[i];
66
+ if (char === '\\' && i + 1 < pattern.length)
67
+ parts.push({ char: pattern[++i] });
68
+ else if (char === '_')
69
+ parts.push({ any: 'one' });
70
+ else if (char === '%')
71
+ parts.push({ any: 'run' });
72
+ else
73
+ parts.push({ char });
74
+ }
75
+ let p = 0;
76
+ let d = 0;
77
+ /** Where to go back to when the match fails after a `%`: it then takes one character more. */
78
+ let run = null;
79
+ while (d < database.length) {
80
+ const part = parts[p];
81
+ if (part?.any === 'run') {
82
+ run = { p: p + 1, d };
83
+ p += 1;
84
+ }
85
+ else if (part && (part.any === 'one' || part.char === database[d])) {
86
+ p += 1;
87
+ d += 1;
88
+ }
89
+ else if (run) {
90
+ run = { p: run.p, d: run.d + 1 };
91
+ p = run.p;
92
+ d = run.d;
93
+ }
94
+ else
95
+ return false;
96
+ }
97
+ while (parts[p]?.any === 'run')
98
+ p += 1;
99
+ return p === parts.length;
100
+ }
101
+ /**
102
+ * What the login may do in `database`, from the lines of its SHOW GRANTS: its privileges on
103
+ * everything and on every pattern the name fits, less what a REVOKE for the database takes back.
104
+ * `ALL PRIVILEGES` stands for them all, and stays whatever was taken back from it; `USAGE` is
105
+ * no privilege, and is left out. Sorted.
106
+ */
107
+ export function privilegesIn(grants, database) {
108
+ const may = new Set();
109
+ const taken = new Set();
110
+ for (const line of grants) {
111
+ const grant = parseGrant(line);
112
+ if (!grant)
113
+ continue;
114
+ if (grant.database !== null && !grantCovers(grant.database, database))
115
+ continue;
116
+ for (const privilege of grant.privileges)
117
+ (grant.revoke ? taken : may).add(privilege);
118
+ }
119
+ may.delete('USAGE');
120
+ return [...may].filter((privilege) => !taken.has(privilege)).sort();
121
+ }
122
+ //# sourceMappingURL=grants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grants.js","sourceRoot":"","sources":["../../src/import/grants.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH,qEAAqE;AACrE,MAAM,SAAS,GAAG,wBAAwB,CAAA;AAE1C,2FAA2F;AAC3F,MAAM,MAAM,GAAwB,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;AAEvD;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAA;IACzC,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,CAAA;IACtD,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAA;IACxD,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IACrC,IAAI,EAAE,KAAK,CAAC,CAAC;QAAE,OAAO,IAAI,CAAA;IAC1B,2FAA2F;IAC3F,6FAA6F;IAC7F,MAAM,UAAU,GAAG,IAAI;SACpB,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;SACf,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;IAC7B,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;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