@tachybase/module-multi-app 0.23.8

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 (117) hide show
  1. package/.turbo/turbo-build.log +14 -0
  2. package/README.md +34 -0
  3. package/README.zh-CN.md +34 -0
  4. package/client.d.ts +1 -0
  5. package/client.js +1 -0
  6. package/dist/client/AppManager.d.ts +2 -0
  7. package/dist/client/AppNameInput.d.ts +2 -0
  8. package/dist/client/MultiAppBlockInitializer.d.ts +2 -0
  9. package/dist/client/MultiAppManagerProvider.d.ts +2 -0
  10. package/dist/client/MultiAppManagerProvider.style.d.ts +5 -0
  11. package/dist/client/Settings.d.ts +2 -0
  12. package/dist/client/index.d.ts +6 -0
  13. package/dist/client/index.js +1 -0
  14. package/dist/client/settings/schemas/applications.d.ts +13 -0
  15. package/dist/client/utils.d.ts +4 -0
  16. package/dist/constants.d.ts +1 -0
  17. package/dist/constants.js +27 -0
  18. package/dist/externalVersion.js +16 -0
  19. package/dist/index.d.ts +2 -0
  20. package/dist/index.js +39 -0
  21. package/dist/locale/en-US.json +27 -0
  22. package/dist/locale/es-ES.json +9 -0
  23. package/dist/locale/ko_KR.json +11 -0
  24. package/dist/locale/pt-BR.json +9 -0
  25. package/dist/locale/zh-CN.json +27 -0
  26. package/dist/node_modules/mariadb/LICENSE +502 -0
  27. package/dist/node_modules/mariadb/callback.js +41 -0
  28. package/dist/node_modules/mariadb/lib/cmd/batch-bulk.js +278 -0
  29. package/dist/node_modules/mariadb/lib/cmd/batch-rewrite.js +372 -0
  30. package/dist/node_modules/mariadb/lib/cmd/change-user.js +149 -0
  31. package/dist/node_modules/mariadb/lib/cmd/class/ok-packet.js +17 -0
  32. package/dist/node_modules/mariadb/lib/cmd/column-definition.js +102 -0
  33. package/dist/node_modules/mariadb/lib/cmd/command.js +168 -0
  34. package/dist/node_modules/mariadb/lib/cmd/common-binary-cmd.js +327 -0
  35. package/dist/node_modules/mariadb/lib/cmd/common-text-cmd.js +427 -0
  36. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/caching-sha2-password-auth.js +168 -0
  37. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/clear-password-auth.js +23 -0
  38. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/ed25519-password-auth.js +761 -0
  39. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/native-password-auth.js +55 -0
  40. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/pam-password-auth.js +58 -0
  41. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/plugin-auth.js +19 -0
  42. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/sha256-password-auth.js +142 -0
  43. package/dist/node_modules/mariadb/lib/cmd/handshake/client-capabilities.js +74 -0
  44. package/dist/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js +126 -0
  45. package/dist/node_modules/mariadb/lib/cmd/handshake/handshake.js +292 -0
  46. package/dist/node_modules/mariadb/lib/cmd/handshake/initial-handshake.js +74 -0
  47. package/dist/node_modules/mariadb/lib/cmd/handshake/ssl-request.js +29 -0
  48. package/dist/node_modules/mariadb/lib/cmd/ping.js +52 -0
  49. package/dist/node_modules/mariadb/lib/cmd/query.js +255 -0
  50. package/dist/node_modules/mariadb/lib/cmd/quit.js +28 -0
  51. package/dist/node_modules/mariadb/lib/cmd/reset.js +54 -0
  52. package/dist/node_modules/mariadb/lib/cmd/resultset.js +607 -0
  53. package/dist/node_modules/mariadb/lib/cmd/stream.js +45 -0
  54. package/dist/node_modules/mariadb/lib/config/connection-options.js +258 -0
  55. package/dist/node_modules/mariadb/lib/config/pool-cluster-options.js +19 -0
  56. package/dist/node_modules/mariadb/lib/config/pool-options.js +47 -0
  57. package/dist/node_modules/mariadb/lib/connection-callback.js +160 -0
  58. package/dist/node_modules/mariadb/lib/connection.js +1460 -0
  59. package/dist/node_modules/mariadb/lib/const/capabilities.js +64 -0
  60. package/dist/node_modules/mariadb/lib/const/collations.js +473 -0
  61. package/dist/node_modules/mariadb/lib/const/connection_status.js +13 -0
  62. package/dist/node_modules/mariadb/lib/const/error-code.js +1282 -0
  63. package/dist/node_modules/mariadb/lib/const/field-detail.js +35 -0
  64. package/dist/node_modules/mariadb/lib/const/field-type.js +71 -0
  65. package/dist/node_modules/mariadb/lib/const/server-status.js +30 -0
  66. package/dist/node_modules/mariadb/lib/const/state-change.js +12 -0
  67. package/dist/node_modules/mariadb/lib/filtered-pool-cluster.js +81 -0
  68. package/dist/node_modules/mariadb/lib/io/bulk-packet.js +590 -0
  69. package/dist/node_modules/mariadb/lib/io/compression-input-stream.js +141 -0
  70. package/dist/node_modules/mariadb/lib/io/compression-output-stream.js +171 -0
  71. package/dist/node_modules/mariadb/lib/io/packet-input-stream.js +193 -0
  72. package/dist/node_modules/mariadb/lib/io/packet-node-encoded.js +36 -0
  73. package/dist/node_modules/mariadb/lib/io/packet-node-iconv.js +37 -0
  74. package/dist/node_modules/mariadb/lib/io/packet-output-stream.js +502 -0
  75. package/dist/node_modules/mariadb/lib/io/packet.js +515 -0
  76. package/dist/node_modules/mariadb/lib/io/rewrite-packet.js +481 -0
  77. package/dist/node_modules/mariadb/lib/misc/connection-information.js +96 -0
  78. package/dist/node_modules/mariadb/lib/misc/errors.js +123 -0
  79. package/dist/node_modules/mariadb/lib/misc/parse.js +1033 -0
  80. package/dist/node_modules/mariadb/lib/misc/utils.js +298 -0
  81. package/dist/node_modules/mariadb/lib/pool-base.js +611 -0
  82. package/dist/node_modules/mariadb/lib/pool-callback.js +202 -0
  83. package/dist/node_modules/mariadb/lib/pool-cluster-callback.js +66 -0
  84. package/dist/node_modules/mariadb/lib/pool-cluster.js +407 -0
  85. package/dist/node_modules/mariadb/lib/pool-promise.js +108 -0
  86. package/dist/node_modules/mariadb/package.json +1 -0
  87. package/dist/node_modules/mariadb/promise.js +34 -0
  88. package/dist/node_modules/mariadb/types/index.d.ts +870 -0
  89. package/dist/server/actions/apps.d.ts +5 -0
  90. package/dist/server/actions/apps.js +117 -0
  91. package/dist/server/app-lifecycle.d.ts +8 -0
  92. package/dist/server/app-lifecycle.js +99 -0
  93. package/dist/server/app-start-env.d.ts +2 -0
  94. package/dist/server/app-start-env.js +105 -0
  95. package/dist/server/collections/applications.d.ts +2 -0
  96. package/dist/server/collections/applications.js +82 -0
  97. package/dist/server/index.d.ts +4 -0
  98. package/dist/server/index.js +29 -0
  99. package/dist/server/middlewares/app-selector.d.ts +1 -0
  100. package/dist/server/middlewares/app-selector.js +47 -0
  101. package/dist/server/middlewares/index.d.ts +2 -0
  102. package/dist/server/middlewares/index.js +23 -0
  103. package/dist/server/middlewares/inject-app-list.d.ts +1 -0
  104. package/dist/server/middlewares/inject-app-list.js +48 -0
  105. package/dist/server/migrations/20240820153000-add-apps-tmpl.d.ts +6 -0
  106. package/dist/server/migrations/20240820153000-add-apps-tmpl.js +47 -0
  107. package/dist/server/migrations/20241126124904-add-createdBy.d.ts +6 -0
  108. package/dist/server/migrations/20241126124904-add-createdBy.js +41 -0
  109. package/dist/server/models/application.d.ts +10 -0
  110. package/dist/server/models/application.js +57 -0
  111. package/dist/server/server.d.ts +19 -0
  112. package/dist/server/server.js +246 -0
  113. package/dist/swagger/index.d.ts +197 -0
  114. package/dist/swagger/index.js +227 -0
  115. package/package.json +38 -0
  116. package/server.d.ts +2 -0
  117. package/server.js +1 -0
@@ -0,0 +1,481 @@
1
+ 'use strict';
2
+
3
+ const Iconv = require('iconv-lite');
4
+
5
+ const QUOTE = 0x27;
6
+ const DBL_QUOTE = 0x22;
7
+ const ZERO_BYTE = 0x00;
8
+ const SLASH = 0x5c;
9
+
10
+ const SMALL_BUFFER_SIZE = 1024;
11
+ const MEDIUM_BUFFER_SIZE = 16384; //16k
12
+ const LARGE_BUFFER_SIZE = 131072; //128k
13
+ const BIG_BUFFER_SIZE = 1048576; //1M
14
+ const MAX_BUFFER_SIZE = 16777219; //16M + 4
15
+
16
+ const CHARS_GLOBAL_REGEXP = /[\0\"\'\\]/g; // eslint-disable-line no-control-regex
17
+
18
+ /**
19
+ * Packet splitter.
20
+ *
21
+ * The servers have a limit max_allowed_packet which limits the size of the data sent, to avoid saturating the server in memory.
22
+ *
23
+ * The following implementation has a workaround that will rewrite the command and separate the send according to this value.
24
+ * This implies that this command can send multiple commands, with some tricks for sequencing packets.
25
+ *
26
+ */
27
+ class ReWritePacket {
28
+ constructor(maxAllowedPacket, out, initString, endString) {
29
+ this.out = out;
30
+ this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
31
+ this.pos = 4;
32
+ this.initStr = initString;
33
+ this.endStr = endString;
34
+ this.encoding = out.encoding;
35
+ this.endStrLength = Buffer.byteLength(this.endStr, this.encoding);
36
+ this.waitingResponseNo = 0;
37
+ this.singleQuery = false;
38
+ this.haveErrorResponse = false;
39
+
40
+ if (this.encoding === 'utf8') {
41
+ this.writeString = this.writeDefaultBufferString;
42
+ this.writeStringEscapeQuote = this.writeUtf8StringEscapeQuote;
43
+ } else if (Buffer.isEncoding(this.encoding)) {
44
+ this.writeString = this.writeDefaultBufferString;
45
+ this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
46
+ } else {
47
+ this.writeString = this.writeDefaultIconvString;
48
+ this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
49
+ }
50
+ this.maxAllowedPacket = maxAllowedPacket;
51
+ if (maxAllowedPacket) {
52
+ this.maxPacketSize = Math.min(MAX_BUFFER_SIZE, maxAllowedPacket) - this.endStrLength;
53
+ } else this.maxPacketSize = 4194304 - this.endStrLength;
54
+
55
+ this.buf[this.pos++] = 0x03;
56
+ this.writeString(this.initStr);
57
+ }
58
+
59
+ growBuffer(len) {
60
+ let newCapacity;
61
+ if (len + this.pos < MEDIUM_BUFFER_SIZE) {
62
+ newCapacity = MEDIUM_BUFFER_SIZE;
63
+ } else if (len + this.pos < LARGE_BUFFER_SIZE) {
64
+ newCapacity = LARGE_BUFFER_SIZE;
65
+ } else if (len + this.pos < BIG_BUFFER_SIZE) {
66
+ newCapacity = BIG_BUFFER_SIZE;
67
+ } else newCapacity = MAX_BUFFER_SIZE;
68
+
69
+ if (newCapacity > this.maxPacketSize && this.markPos) {
70
+ this.flush(false, len);
71
+ return true;
72
+ }
73
+ let newBuf = Buffer.allocUnsafe(Math.min(newCapacity));
74
+ this.buf.copy(newBuf, 0, 0, this.pos);
75
+ this.buf = newBuf;
76
+ return false;
77
+ }
78
+
79
+ writeInt8(value) {
80
+ let flushed = false;
81
+ if (this.pos + 1 + this.endStrLength >= this.buf.length) {
82
+ if (this.buf.length < MAX_BUFFER_SIZE) {
83
+ flushed = this.growBuffer(1);
84
+ } else {
85
+ this.flush(false, 1);
86
+ this.buf[this.pos++] = value;
87
+ return true;
88
+ }
89
+ }
90
+ this.buf[this.pos++] = value;
91
+ return flushed;
92
+ }
93
+
94
+ /**
95
+ * Write ascii string to socket (no escaping)
96
+ *
97
+ * @param str string
98
+ */
99
+ writeStringAscii(str) {
100
+ let len = str.length;
101
+
102
+ //not enough space remaining
103
+ if (len >= this.buf.length - (this.pos + this.endStrLength)) {
104
+ let strBuf = Buffer.from(str, 'ascii');
105
+ return this.writeBuffer(strBuf, 0, strBuf.length);
106
+ }
107
+
108
+ for (let off = 0; off < len; ) {
109
+ this.buf[this.pos++] = str.charCodeAt(off++);
110
+ }
111
+ return false;
112
+ }
113
+
114
+ writeUtf8StringEscapeQuote(str) {
115
+ const charsLength = str.length;
116
+
117
+ //not enough space remaining
118
+ if (charsLength * 3 + 2 >= this.buf.length - (this.pos + this.endStrLength)) {
119
+ let flushed;
120
+ const arr = Buffer.from(str, 'utf8');
121
+ flushed = this.writeInt8(QUOTE);
122
+ flushed = this.writeBufferEscape(arr) || flushed;
123
+ flushed = this.writeInt8(QUOTE) || flushed;
124
+ return flushed;
125
+ }
126
+
127
+ //create UTF-8 byte array
128
+ //since javascript char are internally using UTF-16 using surrogate's pattern, 4 bytes unicode characters will
129
+ //represent 2 characters : example "\uD83C\uDFA4" = 🎤 unicode 8 "no microphones"
130
+ //so max size is 3 * charLength
131
+ //(escape characters are 1 byte encoded, so length might only be 2 when escaped)
132
+ // + 2 for the quotes for text protocol
133
+ let charsOffset = 0;
134
+ let currChar;
135
+ this.buf[this.pos++] = QUOTE;
136
+ //quick loop if only ASCII chars for faster escape
137
+ for (
138
+ ;
139
+ charsOffset < charsLength && (currChar = str.charCodeAt(charsOffset)) < 0x80;
140
+ charsOffset++
141
+ ) {
142
+ if (
143
+ currChar === SLASH ||
144
+ currChar === QUOTE ||
145
+ currChar === ZERO_BYTE ||
146
+ currChar === DBL_QUOTE
147
+ ) {
148
+ this.buf[this.pos++] = SLASH;
149
+ }
150
+ this.buf[this.pos++] = currChar;
151
+ }
152
+
153
+ //if quick loop not finished
154
+ while (charsOffset < charsLength) {
155
+ currChar = str.charCodeAt(charsOffset++);
156
+ if (currChar < 0x80) {
157
+ if (
158
+ currChar === SLASH ||
159
+ currChar === QUOTE ||
160
+ currChar === ZERO_BYTE ||
161
+ currChar === DBL_QUOTE
162
+ ) {
163
+ this.buf[this.pos++] = SLASH;
164
+ }
165
+ this.buf[this.pos++] = currChar;
166
+ } else if (currChar < 0x800) {
167
+ this.buf[this.pos++] = 0xc0 | (currChar >> 6);
168
+ this.buf[this.pos++] = 0x80 | (currChar & 0x3f);
169
+ } else if (currChar >= 0xd800 && currChar < 0xe000) {
170
+ //reserved for surrogate - see https://en.wikipedia.org/wiki/UTF-16
171
+ if (currChar < 0xdc00) {
172
+ //is high surrogate
173
+ if (charsOffset + 1 > charsLength) {
174
+ this.buf[this.pos++] = 0x3f;
175
+ } else {
176
+ const nextChar = str.charCodeAt(charsOffset);
177
+ if (nextChar >= 0xdc00 && nextChar < 0xe000) {
178
+ //is low surrogate
179
+ const surrogatePairs =
180
+ (currChar << 10) + nextChar + (0x010000 - (0xd800 << 10) - 0xdc00);
181
+ this.buf[this.pos++] = 0xf0 | (surrogatePairs >> 18);
182
+ this.buf[this.pos++] = 0x80 | ((surrogatePairs >> 12) & 0x3f);
183
+ this.buf[this.pos++] = 0x80 | ((surrogatePairs >> 6) & 0x3f);
184
+ this.buf[this.pos++] = 0x80 | (surrogatePairs & 0x3f);
185
+ charsOffset++;
186
+ } else {
187
+ //must have low surrogate
188
+ this.buf[this.pos++] = 0x3f;
189
+ }
190
+ }
191
+ } else {
192
+ //low surrogate without high surrogate before
193
+ this.buf[this.pos++] = 0x3f;
194
+ }
195
+ } else {
196
+ this.buf[this.pos++] = 0xe0 | (currChar >> 12);
197
+ this.buf[this.pos++] = 0x80 | ((currChar >> 6) & 0x3f);
198
+ this.buf[this.pos++] = 0x80 | (currChar & 0x3f);
199
+ }
200
+ }
201
+ this.buf[this.pos++] = QUOTE;
202
+ return false;
203
+ }
204
+
205
+ writeDefaultIconvString(str) {
206
+ let buf = Iconv.encode(str, this.encoding);
207
+ return this.writeBuffer(buf, 0, buf.length);
208
+ }
209
+
210
+ writeDefaultBufferString(str) {
211
+ //javascript use UCS-2 or UTF-16 string internal representation
212
+ //that means that string to byte will be a maximum of * 3
213
+ // (4 bytes utf-8 are represented on 2 UTF-16 characters)
214
+ if (str.length * 3 < this.buf.length - (this.pos + this.endStrLength)) {
215
+ this.pos += this.buf.write(str, this.pos, this.encoding);
216
+ return false;
217
+ }
218
+
219
+ //checking real length
220
+ let flushed = false;
221
+ let byteLength = Buffer.byteLength(str, this.encoding);
222
+ if (byteLength > this.buf.length - (this.pos + this.endStrLength)) {
223
+ if (this.buf.length < MAX_BUFFER_SIZE) {
224
+ flushed = this.growBuffer(byteLength);
225
+ }
226
+ if (byteLength > this.buf.length - (this.pos + this.endStrLength)) {
227
+ //not enough space in buffer, will stream :
228
+ let strBuf = Buffer.from(str, this.encoding);
229
+ flushed = this.writeBuffer(strBuf, 0, strBuf.length) || flushed;
230
+ return flushed;
231
+ }
232
+ }
233
+ this.pos += this.buf.write(str, this.pos, this.encoding);
234
+ return flushed;
235
+ }
236
+
237
+ /**
238
+ * Parameters need to be properly escaped :
239
+ * following characters are to be escaped by "\" :
240
+ * - \0
241
+ * - \\
242
+ * - \'
243
+ * - \"
244
+ * regex split part of string writing part, and escaping special char.
245
+ * Those chars are <= 7f meaning that this will work even with multi-byte encoding
246
+ *
247
+ * @param str string to escape.
248
+ */
249
+ writeDefaultStringEscapeQuote(str) {
250
+ let flushed = this.writeInt8(QUOTE);
251
+ let match;
252
+ let lastIndex = 0;
253
+ while ((match = CHARS_GLOBAL_REGEXP.exec(str)) !== null) {
254
+ flushed = this.writeString(str.slice(lastIndex, match.index)) || flushed;
255
+ flushed = this.writeInt8(SLASH) || flushed;
256
+ flushed = this.writeInt8(match[0].charCodeAt(0)) || flushed;
257
+ lastIndex = CHARS_GLOBAL_REGEXP.lastIndex;
258
+ }
259
+
260
+ if (lastIndex === 0) {
261
+ // Nothing was escaped
262
+ flushed = this.writeString(str) || flushed;
263
+ flushed = this.writeInt8(QUOTE) || flushed;
264
+ return flushed;
265
+ }
266
+
267
+ if (lastIndex < str.length) {
268
+ flushed = this.writeString(str.slice(lastIndex)) || flushed;
269
+ }
270
+ flushed = this.writeInt8(QUOTE) || flushed;
271
+ return flushed;
272
+ }
273
+
274
+ writeBufferEscape(val) {
275
+ let flushed = false;
276
+ let valLen = val.length;
277
+ if (valLen * 2 > this.buf.length - (this.pos + this.endStrLength)) {
278
+ //makes buffer bigger (up to 16M)
279
+ if (this.buf.length < MAX_BUFFER_SIZE) flushed = this.growBuffer(valLen * 2);
280
+
281
+ //data may still be bigger than buffer.
282
+ //must flush buffer when full (and reset position to 4)
283
+ if (valLen * 2 > this.buf.length - (this.pos + this.endStrLength)) {
284
+ //not enough space in buffer, will fill buffer
285
+ for (let i = 0; i < valLen; i++) {
286
+ switch (val[i]) {
287
+ case QUOTE:
288
+ case SLASH:
289
+ case DBL_QUOTE:
290
+ case ZERO_BYTE:
291
+ if (this.pos >= this.buf.length) this.flush(false, (valLen - i) * 2);
292
+ this.buf[this.pos++] = SLASH; //add escape slash
293
+ }
294
+ if (this.pos >= this.buf.length) this.flush(false, (valLen - i) * 2);
295
+ this.buf[this.pos++] = val[i];
296
+ }
297
+ return true;
298
+ }
299
+ }
300
+
301
+ //sure to have enough place to use buffer directly
302
+ for (let i = 0; i < valLen; i++) {
303
+ switch (val[i]) {
304
+ case QUOTE:
305
+ case SLASH:
306
+ case DBL_QUOTE:
307
+ case ZERO_BYTE:
308
+ this.buf[this.pos++] = SLASH; //add escape slash
309
+ }
310
+ this.buf[this.pos++] = val[i];
311
+ }
312
+ return flushed;
313
+ }
314
+
315
+ writeBuffer(arr, off, len) {
316
+ let flushed = false;
317
+ if (len > this.buf.length - (this.pos + this.endStrLength)) {
318
+ if (this.buf.length < MAX_BUFFER_SIZE) flushed = this.growBuffer(len);
319
+
320
+ //max buffer size
321
+ if (len > this.buf.length - (this.pos + this.endStrLength)) {
322
+ //not enough space in buffer, will stream :
323
+ // fill buffer and flush until all data are snd
324
+ let remainingLen = len;
325
+
326
+ while (true) {
327
+ //filling buffer
328
+ let lenToFillBuffer = Math.min(MAX_BUFFER_SIZE - this.pos, remainingLen);
329
+ arr.copy(this.buf, this.pos, off, off + lenToFillBuffer);
330
+ remainingLen -= lenToFillBuffer;
331
+ off += lenToFillBuffer;
332
+ this.pos += lenToFillBuffer;
333
+
334
+ if (remainingLen === 0) return flushed;
335
+ this.flush(false, remainingLen);
336
+ flushed = true;
337
+ }
338
+ }
339
+ }
340
+ arr.copy(this.buf, this.pos, off, off + len);
341
+ this.pos += len;
342
+ return flushed;
343
+ }
344
+
345
+ mark(isLast) {
346
+ let flushed = false;
347
+ if (this.singleQuery) {
348
+ //end of big query that is more than 16M
349
+ //write single one
350
+ flushed = this.writeString(this.endStr);
351
+
352
+ if (!this.haveErrorResponse) {
353
+ const packetSendSize =
354
+ this.pos +
355
+ (this.singleQuerySequenceNo != undefined
356
+ ? (this.singleQuerySequenceNo + 1) * MAX_BUFFER_SIZE
357
+ : 0);
358
+ if (this.maxAllowedPacket && packetSendSize > this.maxAllowedPacket) {
359
+ console.log(
360
+ "will send a packet to db server with size > connection option 'maxAllowedPacket' (size send is " +
361
+ packetSendSize +
362
+ ') connection might be reset by server'
363
+ );
364
+ }
365
+ this.copyAndFlush(true);
366
+ flushed = true;
367
+ this.markPos = undefined;
368
+ }
369
+
370
+ this.singleQuerySequenceNo = undefined;
371
+ this.singleQueryCompressSequenceNo = undefined;
372
+ this.singleQuery = false;
373
+ this.buf[this.pos++] = 0x03;
374
+ this.writeString(this.initStr);
375
+ this.markPos = undefined;
376
+ } else {
377
+ if (this.markPos && this.pos + this.endStrLength > this.maxPacketSize) {
378
+ //not enough room for current query, flush mark.
379
+ this.flushMark();
380
+ flushed = true;
381
+ }
382
+ //just mark ending query
383
+ this.markPos = this.pos;
384
+ if (isLast) {
385
+ this.flushMark();
386
+ flushed = true;
387
+ }
388
+ if (!isLast) flushed = this.writeStringAscii(',') || flushed;
389
+ }
390
+ return flushed;
391
+ }
392
+
393
+ flush(end, remainingLen) {
394
+ if (this.markPos && !this.singleQuery) {
395
+ this.flushMark();
396
+ } else {
397
+ //one insert is more than 16M, will continue to mono insert, hoping
398
+ //that max_allowed_packet is sized accordingly to query.
399
+ if (this.buf.length < MAX_BUFFER_SIZE) {
400
+ //in this case, connector has default to 4M packet, and a single query size
401
+ //is > to 4mb. growing buffer to 16M
402
+ let newBuf = Buffer.allocUnsafe(MAX_BUFFER_SIZE);
403
+ this.buf.copy(newBuf, 0, 0, this.pos);
404
+ this.buf = newBuf;
405
+ } else {
406
+ if (!this.haveErrorResponse) {
407
+ if (this.maxAllowedPacket && this.buf.length > this.maxAllowedPacket) {
408
+ console.log(
409
+ "will send a packet to server with size > connection option 'maxAllowedPacket' (size send is " +
410
+ this.pos +
411
+ ') connection might be reset by server'
412
+ );
413
+ }
414
+ this.copyAndFlush(false);
415
+
416
+ this.markPos = undefined;
417
+ if (!this.singleQuery) this.waitingResponseNo++;
418
+ this.singleQuery = true;
419
+ this.singleQuerySequenceNo = this.out.cmd.sequenceNo;
420
+ this.singleQueryCompressSequenceNo = this.out.cmd.compressSequenceNo;
421
+ }
422
+ }
423
+ }
424
+ }
425
+
426
+ flushMark() {
427
+ let afterMark;
428
+ if (this.pos !== this.markPos) {
429
+ //remove "," character
430
+ afterMark = Buffer.allocUnsafe(this.pos - this.markPos - 1);
431
+ this.buf.copy(afterMark, 0, this.markPos + 1, this.pos);
432
+ }
433
+
434
+ this.pos = this.markPos;
435
+ this.writeString(this.endStr);
436
+
437
+ if (!this.haveErrorResponse) {
438
+ this.copyAndFlush(true);
439
+ this.waitingResponseNo++;
440
+ }
441
+
442
+ this.pos = 4;
443
+ this.buf[this.pos++] = 0x03;
444
+ this.writeString(this.initStr);
445
+ this.markPos = undefined;
446
+ if (afterMark) {
447
+ if (this.buf.length - this.pos < afterMark.length)
448
+ this.growBuffer(afterMark.length - (this.buf.length - this.pos));
449
+ afterMark.copy(this.buf, this.pos, 0, afterMark.length);
450
+ this.pos += afterMark.length;
451
+ }
452
+ this.singleQuery = false;
453
+ this.singleQuerySequenceNo = undefined;
454
+ this.singleQueryCompressSequenceNo = undefined;
455
+ }
456
+
457
+ copyAndFlush(ended) {
458
+ this.out.buf = this.buf;
459
+ this.out.pos = this.pos;
460
+ if (this.singleQuerySequenceNo != undefined) {
461
+ this.out.cmd.sequenceNo = this.singleQuerySequenceNo;
462
+ this.out.cmd.compressSequenceNo = this.singleQueryCompressSequenceNo;
463
+ } else {
464
+ this.out.cmd.sequenceNo = -1;
465
+ this.out.cmd.compressSequenceNo = -1;
466
+ }
467
+ this.out.flushBuffer(ended);
468
+ if (this.singleQuerySequenceNo != undefined) {
469
+ this.singleQuerySequenceNo = this.out.cmd.sequenceNo;
470
+ this.singleQueryCompressSequenceNo = this.out.cmd.compressSequenceNo;
471
+ }
472
+ this.pos = 4;
473
+ this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
474
+ }
475
+
476
+ endedWithError() {
477
+ this.haveErrorResponse = true;
478
+ }
479
+ }
480
+
481
+ module.exports = ReWritePacket;
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+ const Queue = require('denque');
3
+
4
+ const _addPacket = function (msg) {
5
+ this.lastPackets.push(msg);
6
+ while (this.lastPackets.size() > 32) this.lastPackets.shift();
7
+ };
8
+
9
+ const _getLastPackets = function () {
10
+ let output = '';
11
+ let packet;
12
+ while ((packet = this.lastPackets.shift())) {
13
+ output += '\n' + packet;
14
+ }
15
+ return output;
16
+ };
17
+
18
+ class ConnectionInformation {
19
+ constructor() {
20
+ this.threadId = -1;
21
+ this.status = null;
22
+ this.serverVersion = null;
23
+ this.serverCapabilities = null;
24
+ }
25
+
26
+ addPacket(msg) {}
27
+
28
+ getLastPackets() {
29
+ return '';
30
+ }
31
+
32
+ enableLogPacket() {
33
+ this.lastPackets = new Queue();
34
+ this.addPacket = _addPacket.bind(this);
35
+ this.getLastPackets = _getLastPackets.bind(this);
36
+ }
37
+
38
+ hasMinVersion(major, minor, patch) {
39
+ if (!this.serverVersion)
40
+ throw new Error('cannot know if server version until connection is established');
41
+
42
+ if (!major) throw new Error('a major version must be set');
43
+
44
+ if (!minor) minor = 0;
45
+ if (!patch) patch = 0;
46
+
47
+ let ver = this.serverVersion;
48
+ return (
49
+ ver.major > major ||
50
+ (ver.major === major && ver.minor > minor) ||
51
+ (ver.major === major && ver.minor === minor && ver.patch >= patch)
52
+ );
53
+ }
54
+
55
+ isMariaDB() {
56
+ if (!this.serverVersion)
57
+ throw new Error('cannot know if server is MariaDB until connection is established');
58
+ return this.serverVersion.mariaDb;
59
+ }
60
+
61
+ /**
62
+ * Parse raw info to set server major/minor/patch values
63
+ * @param info
64
+ */
65
+ static parseVersionString(info) {
66
+ let car;
67
+ let offset = 0;
68
+ let type = 0;
69
+ let val = 0;
70
+
71
+ for (; offset < info.serverVersion.raw.length; offset++) {
72
+ car = info.serverVersion.raw.charCodeAt(offset);
73
+ if (car < 48 || car > 57) {
74
+ switch (type) {
75
+ case 0:
76
+ info.serverVersion.major = val;
77
+ break;
78
+ case 1:
79
+ info.serverVersion.minor = val;
80
+ break;
81
+ case 2:
82
+ info.serverVersion.patch = val;
83
+ return;
84
+ }
85
+ type++;
86
+ val = 0;
87
+ } else {
88
+ val = val * 10 + car - 48;
89
+ }
90
+ }
91
+ //serverVersion finished by number like "5.5.57", assign patchVersion
92
+ if (type === 2) info.serverVersion.patch = val;
93
+ }
94
+ }
95
+
96
+ module.exports = ConnectionInformation;
@@ -0,0 +1,123 @@
1
+ 'use strict';
2
+ const ErrorCodes = require('../const/error-code');
3
+
4
+ class SqlError extends Error {
5
+ constructor(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader) {
6
+ super(
7
+ (addHeader === undefined || addHeader
8
+ ? '(conn=' +
9
+ (info ? (info.threadId ? info.threadId : -1) : -1) +
10
+ ', no: ' +
11
+ (errno ? errno : -1) +
12
+ ', SQLState: ' +
13
+ (sqlState ? sqlState : 'HY000') +
14
+ ') '
15
+ : '') +
16
+ msg +
17
+ (sql ? '\nsql: ' + sql : '')
18
+ );
19
+ this.text = msg;
20
+ this.sql = sql;
21
+ this.fatal = fatal;
22
+ this.errno = errno;
23
+ this.sqlState = sqlState;
24
+ if (errno > 45000 && errno < 46000) {
25
+ //driver error
26
+ this.code = errByNo[errno] || 'UNKNOWN';
27
+ } else {
28
+ this.code = ErrorCodes.codes[this.errno] || 'UNKNOWN';
29
+ }
30
+ if (additionalStack) {
31
+ //adding caller stack, removing initial "Error:\n"
32
+ this.stack +=
33
+ '\n From event:\n' + additionalStack.substring(additionalStack.indexOf('\n') + 1);
34
+ }
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Error factory, so error get connection information.
40
+ *
41
+ * @param msg current error message
42
+ * @param sql sql command
43
+ * @param fatal is error fatal
44
+ * @param info connection information
45
+ * @param sqlState sql state
46
+ * @param errno error number
47
+ * @param additionalStack additional stack trace to see
48
+ * @param addHeader add connection information
49
+ * @returns {Error} the error
50
+ */
51
+ module.exports.createError = function (
52
+ msg,
53
+ sql,
54
+ fatal,
55
+ info,
56
+ sqlState,
57
+ errno,
58
+ additionalStack,
59
+ addHeader
60
+ ) {
61
+ return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
62
+ };
63
+
64
+ /********************************************************************************
65
+ * Driver specific errors
66
+ ********************************************************************************/
67
+
68
+ module.exports.ER_CONNECTION_ALREADY_CLOSED = 45001;
69
+ module.exports.ER_ALREADY_CONNECTING = 45002;
70
+ module.exports.ER_MYSQL_CHANGE_USER_BUG = 45003;
71
+ module.exports.ER_CMD_NOT_EXECUTED_DESTROYED = 45004;
72
+ module.exports.ER_NULL_CHAR_ESCAPEID = 45005;
73
+ module.exports.ER_NULL_ESCAPEID = 45006;
74
+ module.exports.ER_NOT_IMPLEMENTED_FORMAT = 45007;
75
+ module.exports.ER_NODE_NOT_SUPPORTED_TLS = 45008;
76
+ module.exports.ER_SOCKET_UNEXPECTED_CLOSE = 45009;
77
+ module.exports.ER_UNEXPECTED_PACKET = 45011;
78
+ module.exports.ER_CONNECTION_TIMEOUT = 45012;
79
+ module.exports.ER_CMD_CONNECTION_CLOSED = 45013;
80
+ module.exports.ER_CHANGE_USER_BAD_PACKET = 45014;
81
+ module.exports.ER_PING_BAD_PACKET = 45015;
82
+ module.exports.ER_MISSING_PARAMETER = 45016;
83
+ module.exports.ER_PARAMETER_UNDEFINED = 45017;
84
+ module.exports.ER_PLACEHOLDER_UNDEFINED = 45018;
85
+ module.exports.ER_SOCKET = 45019;
86
+ module.exports.ER_EOF_EXPECTED = 45020;
87
+ module.exports.ER_LOCAL_INFILE_DISABLED = 45021;
88
+ module.exports.ER_LOCAL_INFILE_NOT_READABLE = 45022;
89
+ module.exports.ER_SERVER_SSL_DISABLED = 45023;
90
+ module.exports.ER_AUTHENTICATION_BAD_PACKET = 45024;
91
+ module.exports.ER_AUTHENTICATION_PLUGIN_NOT_SUPPORTED = 45025;
92
+ module.exports.ER_SOCKET_TIMEOUT = 45026;
93
+ module.exports.ER_POOL_ALREADY_CLOSED = 45027;
94
+ module.exports.ER_GET_CONNECTION_TIMEOUT = 45028;
95
+ module.exports.ER_SETTING_SESSION_ERROR = 45029;
96
+ module.exports.ER_INITIAL_SQL_ERROR = 45030;
97
+ module.exports.ER_BATCH_WITH_NO_VALUES = 45031;
98
+ module.exports.ER_RESET_BAD_PACKET = 45032;
99
+ module.exports.ER_WRONG_IANA_TIMEZONE = 45033;
100
+ module.exports.ER_LOCAL_INFILE_WRONG_FILENAME = 45034;
101
+ module.exports.ER_ADD_CONNECTION_CLOSED_POOL = 45035;
102
+ module.exports.ER_WRONG_AUTO_TIMEZONE = 45036;
103
+ module.exports.ER_CLOSING_POOL = 45037;
104
+ module.exports.ER_TIMEOUT_NOT_SUPPORTED = 45038;
105
+ module.exports.ER_INITIAL_TIMEOUT_ERROR = 45039;
106
+ module.exports.ER_DUPLICATE_FIELD = 45040;
107
+ module.exports.ER_CLIENT_OPTION_INCOMPATIBILITY = 45041;
108
+ module.exports.ER_PING_TIMEOUT = 45042;
109
+ module.exports.ER_BAD_PARAMETER_VALUE = 45043;
110
+ module.exports.ER_CANNOT_RETRIEVE_RSA_KEY = 45044;
111
+ module.exports.ER_MINIMUM_NODE_VERSION_REQUIRED = 45045;
112
+ module.exports.ER_POOL_UNDEFINED_SQL = 45049;
113
+
114
+ const keys = Object.keys(module.exports);
115
+ const errByNo = {};
116
+ for (let i = 0; i < keys.length; i++) {
117
+ const keyName = keys[i];
118
+ if (keyName !== 'createError') {
119
+ errByNo[module.exports[keyName]] = keyName;
120
+ }
121
+ }
122
+
123
+ module.exports.SqlError = SqlError;