@tachybase/module-multi-app 1.5.1 → 1.6.1

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 (94) hide show
  1. package/dist/externalVersion.js +5 -5
  2. package/dist/node_modules/mariadb/callback.js +43 -8
  3. package/dist/node_modules/mariadb/check-node.js +30 -0
  4. package/dist/node_modules/mariadb/lib/cluster-callback.js +84 -0
  5. package/dist/node_modules/mariadb/lib/cluster.js +446 -0
  6. package/dist/node_modules/mariadb/lib/cmd/batch-bulk.js +576 -177
  7. package/dist/node_modules/mariadb/lib/cmd/change-user.js +54 -44
  8. package/dist/node_modules/mariadb/lib/cmd/class/ok-packet.js +3 -2
  9. package/dist/node_modules/mariadb/lib/cmd/class/prepare-cache-wrapper.js +46 -0
  10. package/dist/node_modules/mariadb/lib/cmd/class/prepare-result-packet.js +141 -0
  11. package/dist/node_modules/mariadb/lib/cmd/class/prepare-wrapper.js +70 -0
  12. package/dist/node_modules/mariadb/lib/cmd/close-prepare.js +38 -0
  13. package/dist/node_modules/mariadb/lib/cmd/column-definition.js +145 -47
  14. package/dist/node_modules/mariadb/lib/cmd/command.js +41 -75
  15. package/dist/node_modules/mariadb/lib/cmd/decoder/binary-decoder.js +282 -0
  16. package/dist/node_modules/mariadb/lib/cmd/decoder/text-decoder.js +210 -0
  17. package/dist/node_modules/mariadb/lib/cmd/{common-binary-cmd.js → encoder/binary-encoder.js} +34 -77
  18. package/dist/node_modules/mariadb/lib/cmd/encoder/text-encoder.js +311 -0
  19. package/dist/node_modules/mariadb/lib/cmd/execute-stream.js +61 -0
  20. package/dist/node_modules/mariadb/lib/cmd/execute.js +338 -0
  21. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/caching-sha2-password-auth.js +25 -62
  22. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/clear-password-auth.js +39 -6
  23. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/ed25519-password-auth.js +48 -16
  24. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/handshake.js +198 -0
  25. package/dist/node_modules/mariadb/lib/cmd/handshake/{initial-handshake.js → auth/initial-handshake.js} +10 -8
  26. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/native-password-auth.js +22 -9
  27. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/pam-password-auth.js +9 -4
  28. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/parsec-auth.js +115 -0
  29. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/plugin-auth.js +12 -5
  30. package/dist/node_modules/mariadb/lib/cmd/handshake/auth/sha256-password-auth.js +44 -33
  31. package/dist/node_modules/mariadb/lib/cmd/handshake/authentication.js +335 -0
  32. package/dist/node_modules/mariadb/lib/cmd/handshake/client-capabilities.js +20 -19
  33. package/dist/node_modules/mariadb/lib/cmd/handshake/ssl-request.js +6 -3
  34. package/dist/node_modules/mariadb/lib/cmd/parser.js +861 -0
  35. package/dist/node_modules/mariadb/lib/cmd/ping.js +17 -18
  36. package/dist/node_modules/mariadb/lib/cmd/prepare.js +170 -0
  37. package/dist/node_modules/mariadb/lib/cmd/query.js +281 -144
  38. package/dist/node_modules/mariadb/lib/cmd/quit.js +9 -6
  39. package/dist/node_modules/mariadb/lib/cmd/reset.js +15 -19
  40. package/dist/node_modules/mariadb/lib/cmd/stream.js +21 -6
  41. package/dist/node_modules/mariadb/lib/config/cluster-options.js +23 -0
  42. package/dist/node_modules/mariadb/lib/config/connection-options.js +196 -132
  43. package/dist/node_modules/mariadb/lib/config/pool-options.js +27 -19
  44. package/dist/node_modules/mariadb/lib/connection-callback.js +492 -120
  45. package/dist/node_modules/mariadb/lib/connection-promise.js +372 -0
  46. package/dist/node_modules/mariadb/lib/connection.js +1739 -1016
  47. package/dist/node_modules/mariadb/lib/const/capabilities.js +36 -30
  48. package/dist/node_modules/mariadb/lib/const/collations.js +972 -36
  49. package/dist/node_modules/mariadb/lib/const/connection_status.js +3 -0
  50. package/dist/node_modules/mariadb/lib/const/error-code.js +35 -11
  51. package/dist/node_modules/mariadb/lib/const/field-detail.js +3 -0
  52. package/dist/node_modules/mariadb/lib/const/field-type.js +7 -4
  53. package/dist/node_modules/mariadb/lib/const/server-status.js +4 -1
  54. package/dist/node_modules/mariadb/lib/const/state-change.js +3 -0
  55. package/dist/node_modules/mariadb/lib/filtered-cluster-callback.js +136 -0
  56. package/dist/node_modules/mariadb/lib/filtered-cluster.js +118 -0
  57. package/dist/node_modules/mariadb/lib/io/compression-input-stream.js +14 -13
  58. package/dist/node_modules/mariadb/lib/io/compression-output-stream.js +21 -18
  59. package/dist/node_modules/mariadb/lib/io/packet-input-stream.js +75 -64
  60. package/dist/node_modules/mariadb/lib/io/packet-node-encoded.js +13 -9
  61. package/dist/node_modules/mariadb/lib/io/packet-node-iconv.js +12 -10
  62. package/dist/node_modules/mariadb/lib/io/packet-output-stream.js +402 -134
  63. package/dist/node_modules/mariadb/lib/io/packet.js +287 -202
  64. package/dist/node_modules/mariadb/lib/lru-prepare-cache.js +84 -0
  65. package/dist/node_modules/mariadb/lib/misc/connection-information.js +15 -32
  66. package/dist/node_modules/mariadb/lib/misc/errors.js +68 -25
  67. package/dist/node_modules/mariadb/lib/misc/parse.js +207 -711
  68. package/dist/node_modules/mariadb/lib/misc/utils.js +34 -62
  69. package/dist/node_modules/mariadb/lib/pool-callback.js +213 -174
  70. package/dist/node_modules/mariadb/lib/pool-promise.js +228 -94
  71. package/dist/node_modules/mariadb/lib/pool.js +951 -0
  72. package/dist/node_modules/mariadb/package.json +1 -1
  73. package/dist/node_modules/mariadb/promise.js +1 -34
  74. package/dist/node_modules/mariadb/types/callback.d.ts +207 -0
  75. package/dist/node_modules/mariadb/types/index.d.ts +94 -674
  76. package/dist/node_modules/mariadb/types/share.d.ts +804 -0
  77. package/dist/node_modules/qs/package.json +1 -1
  78. package/dist/server/actions/apps.js +2 -2
  79. package/dist/server/app-lifecycle.d.ts +1 -1
  80. package/dist/server/app-lifecycle.js +4 -4
  81. package/dist/server/models/application.d.ts +1 -1
  82. package/package.json +7 -7
  83. package/dist/node_modules/mariadb/lib/cmd/batch-rewrite.js +0 -372
  84. package/dist/node_modules/mariadb/lib/cmd/common-text-cmd.js +0 -427
  85. package/dist/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js +0 -126
  86. package/dist/node_modules/mariadb/lib/cmd/handshake/handshake.js +0 -292
  87. package/dist/node_modules/mariadb/lib/cmd/resultset.js +0 -607
  88. package/dist/node_modules/mariadb/lib/config/pool-cluster-options.js +0 -19
  89. package/dist/node_modules/mariadb/lib/filtered-pool-cluster.js +0 -81
  90. package/dist/node_modules/mariadb/lib/io/bulk-packet.js +0 -590
  91. package/dist/node_modules/mariadb/lib/io/rewrite-packet.js +0 -481
  92. package/dist/node_modules/mariadb/lib/pool-base.js +0 -611
  93. package/dist/node_modules/mariadb/lib/pool-cluster-callback.js +0 -66
  94. package/dist/node_modules/mariadb/lib/pool-cluster.js +0 -407
@@ -1,47 +1,32 @@
1
+ // SPDX-License-Identifier: LGPL-2.1-or-later
2
+ // Copyright (c) 2015-2024 MariaDB Corporation Ab
3
+
1
4
  'use strict';
2
5
 
3
6
  const Errors = require('../misc/errors');
4
- const Iconv = require('iconv-lite');
5
- const Long = require('long');
6
- const moment = require('moment-timezone');
7
7
 
8
8
  /**
9
9
  * Object to easily parse buffer.
10
+ * Packet are MUTABLE (buffer are changed, to avoid massive packet object creation).
11
+ * Use clone() in case immutability is required
10
12
  *
11
13
  */
12
14
  class Packet {
13
- constructor(buf, pos, end) {
15
+ update(buf, pos, end) {
14
16
  this.buf = buf;
15
17
  this.pos = pos;
16
18
  this.end = end;
19
+ return this;
17
20
  }
18
21
 
19
22
  skip(n) {
20
23
  this.pos += n;
21
24
  }
22
25
 
23
- readGeometry(dataTypeName) {
26
+ readGeometry(defaultVal) {
24
27
  const geoBuf = this.readBufferLengthEncoded();
25
28
  if (geoBuf === null || geoBuf.length === 0) {
26
- if (dataTypeName) {
27
- switch (dataTypeName) {
28
- case 'point':
29
- return { type: 'Point' };
30
- case 'linestring':
31
- return { type: 'LineString' };
32
- case 'polygon':
33
- return { type: 'Polygon' };
34
- case 'multipoint':
35
- return { type: 'MultiPoint' };
36
- case 'multilinestring':
37
- return { type: 'MultiLineString' };
38
- case 'multipolygon':
39
- return { type: 'MultiPolygon' };
40
- default:
41
- return { type: dataTypeName };
42
- }
43
- }
44
- return null;
29
+ return defaultVal;
45
30
  }
46
31
  let geoPos = 4;
47
32
  return readGeometryObject(false);
@@ -144,88 +129,110 @@ class Packet {
144
129
  return this.end - this.pos > 0;
145
130
  }
146
131
 
132
+ readInt8() {
133
+ const val = this.buf[this.pos++];
134
+ return val | ((val & (2 ** 7)) * 0x1fffffe);
135
+ }
136
+
147
137
  readUInt8() {
148
138
  return this.buf[this.pos++];
149
139
  }
150
140
 
141
+ readInt16() {
142
+ this.pos += 2;
143
+ const first = this.buf[this.pos - 2];
144
+ const last = this.buf[this.pos - 1];
145
+ const val = first + last * 2 ** 8;
146
+ return val | ((val & (2 ** 15)) * 0x1fffe);
147
+ }
148
+
151
149
  readUInt16() {
152
- return this.buf[this.pos++] + (this.buf[this.pos++] << 8);
150
+ this.pos += 2;
151
+ return this.buf[this.pos - 2] + this.buf[this.pos - 1] * 2 ** 8;
152
+ }
153
+
154
+ readInt24() {
155
+ const first = this.buf[this.pos];
156
+ const last = this.buf[this.pos + 2];
157
+ const val = first + this.buf[this.pos + 1] * 2 ** 8 + last * 2 ** 16;
158
+ this.pos += 3;
159
+ return val | ((val & (2 ** 23)) * 0x1fe);
153
160
  }
154
161
 
155
162
  readUInt24() {
156
- return this.buf[this.pos++] + (this.buf[this.pos++] << 8) + (this.buf[this.pos++] << 16);
163
+ this.pos += 3;
164
+ return this.buf[this.pos - 3] + this.buf[this.pos - 2] * 2 ** 8 + this.buf[this.pos - 1] * 2 ** 16;
157
165
  }
158
166
 
159
167
  readUInt32() {
168
+ this.pos += 4;
160
169
  return (
161
- this.buf[this.pos++] +
162
- (this.buf[this.pos++] << 8) +
163
- (this.buf[this.pos++] << 16) +
164
- this.buf[this.pos++] * 0x1000000
170
+ this.buf[this.pos - 4] +
171
+ this.buf[this.pos - 3] * 2 ** 8 +
172
+ this.buf[this.pos - 2] * 2 ** 16 +
173
+ this.buf[this.pos - 1] * 2 ** 24
165
174
  );
166
175
  }
167
176
 
168
177
  readInt32() {
178
+ this.pos += 4;
169
179
  return (
170
- this.buf[this.pos++] +
171
- (this.buf[this.pos++] << 8) +
172
- (this.buf[this.pos++] << 16) +
173
- (this.buf[this.pos++] << 24)
180
+ this.buf[this.pos - 4] +
181
+ this.buf[this.pos - 3] * 2 ** 8 +
182
+ this.buf[this.pos - 2] * 2 ** 16 +
183
+ (this.buf[this.pos - 1] << 24)
174
184
  );
175
185
  }
176
186
 
177
- readInt32LE() {
178
- return (
179
- (this.buf[this.pos++] << 24) +
180
- (this.buf[this.pos++] << 16) +
181
- (this.buf[this.pos++] << 8) +
182
- this.buf[this.pos++]
183
- );
187
+ readBigInt64() {
188
+ const val = this.buf.readBigInt64LE(this.pos);
189
+ this.pos += 8;
190
+ return val;
184
191
  }
185
192
 
186
- readInt64() {
187
- // could use readBigInt64LE when support would be 10.20+
188
- const val =
189
- this.buf[this.pos + 4] +
190
- this.buf[this.pos + 5] * 2 ** 8 +
191
- this.buf[this.pos + 6] * 2 ** 16 +
192
- (this.buf[this.pos + 7] << 24);
193
- const vv =
194
- (BigInt(val) << BigInt(32)) +
195
- BigInt(
196
- this.buf[this.pos] +
197
- this.buf[this.pos + 1] * 2 ** 8 +
198
- this.buf[this.pos + 2] * 2 ** 16 +
199
- this.buf[this.pos + 3] * 2 ** 24
200
- );
193
+ readBigUInt64() {
194
+ const val = this.buf.readBigUInt64LE(this.pos);
201
195
  this.pos += 8;
202
- return vv;
196
+ return val;
197
+ }
198
+
199
+ /**
200
+ * Metadata are length encoded, but cannot have length > 256, so simplified readUnsignedLength
201
+ * @returns {number}
202
+ */
203
+ readMetadataLength() {
204
+ const type = this.buf[this.pos++];
205
+ if (type < 0xfb) return type;
206
+ return this.readUInt16();
203
207
  }
204
208
 
205
209
  readUnsignedLength() {
206
- const type = this.buf[this.pos++] & 0xff;
210
+ const type = this.buf[this.pos++];
211
+ if (type < 0xfb) return type;
207
212
  switch (type) {
208
213
  case 0xfb:
209
214
  return null;
210
215
  case 0xfc:
211
- return this.readUInt16();
216
+ //readUInt16();
217
+ this.pos += 2;
218
+ return this.buf[this.pos - 2] + this.buf[this.pos - 1] * 2 ** 8;
212
219
  case 0xfd:
213
- return this.readUInt24();
220
+ //readUInt24();
221
+ this.pos += 3;
222
+ return this.buf[this.pos - 3] + this.buf[this.pos - 2] * 2 ** 8 + this.buf[this.pos - 1] * 2 ** 16;
214
223
  case 0xfe:
215
224
  // limitation to BigInt signed value
216
- return Number(this.readInt64());
217
- default:
218
- return type;
225
+ return Number(this.readBigInt64());
219
226
  }
220
227
  }
221
228
 
222
229
  readBuffer(len) {
223
230
  this.pos += len;
224
- return this.buf.slice(this.pos - len, this.pos);
231
+ return this.buf.subarray(this.pos - len, this.pos);
225
232
  }
226
233
 
227
234
  readBufferRemaining() {
228
- let b = this.buf.slice(this.pos, this.end);
235
+ let b = this.buf.subarray(this.pos, this.end);
229
236
  this.pos = this.end;
230
237
  return b;
231
238
  }
@@ -234,7 +241,7 @@ class Packet {
234
241
  const len = this.readUnsignedLength();
235
242
  if (len === null) return null;
236
243
  this.pos += len;
237
- return this.buf.slice(this.pos - len, this.pos);
244
+ return this.buf.subarray(this.pos - len, this.pos);
238
245
  }
239
246
 
240
247
  readStringNullEnded() {
@@ -243,38 +250,27 @@ class Packet {
243
250
  while (this.remaining() > 0 && this.buf[this.pos++] !== 0) {
244
251
  cnt++;
245
252
  }
246
- return this.buf.toString('utf8', initialPosition, initialPosition + cnt);
253
+ return this.buf.toString(undefined, initialPosition, initialPosition + cnt);
247
254
  }
248
255
 
249
- readSignedLength() {
250
- const type = this.buf[this.pos++];
251
- switch (type) {
252
- case 0xfb:
253
- return null;
254
- case 0xfc:
255
- return this.readUInt16();
256
- case 0xfd:
257
- return this.readUInt24();
258
- case 0xfe:
259
- return Number(this.readInt64());
260
- default:
261
- return type;
262
- }
263
- }
264
-
265
- readSignedLengthBigInt() {
256
+ /**
257
+ * Return unsigned Bigint.
258
+ *
259
+ * Could be used for reading other kinds of value than InsertId, if reading possible null value
260
+ * @returns {bigint}
261
+ */
262
+ readInsertId() {
266
263
  const type = this.buf[this.pos++];
264
+ if (type < 0xfb) return BigInt(type);
267
265
  switch (type) {
268
- case 0xfb:
269
- return null;
270
266
  case 0xfc:
271
- return BigInt(this.readUInt16());
267
+ this.pos += 2;
268
+ return BigInt(this.buf[this.pos - 2] + this.buf[this.pos - 1] * 2 ** 8);
272
269
  case 0xfd:
273
- return BigInt(this.readUInt24());
270
+ this.pos += 3;
271
+ return BigInt(this.buf[this.pos - 3] + this.buf[this.pos - 2] * 2 ** 8 + this.buf[this.pos - 1] * 2 ** 16);
274
272
  case 0xfe:
275
- return this.readInt64();
276
- default:
277
- return BigInt(type);
273
+ return this.readBigInt64();
278
274
  }
279
275
  }
280
276
 
@@ -285,72 +281,51 @@ class Packet {
285
281
  return this.buf.toString('ascii', this.pos - len, this.pos);
286
282
  }
287
283
 
288
- readStringLength() {
289
- throw new Error(
290
- 'code is normally superseded by Node encoder or Iconv depending on charset used'
291
- );
284
+ readStringLengthEncoded() {
285
+ throw new Error('code is normally superseded by Node encoder or Iconv depending on charset used');
292
286
  }
293
287
 
294
- readStringLengthEncoded(encoding) {
295
- const len = this.readUnsignedLength();
296
- if (len === null) return null;
288
+ readBigIntLengthEncoded() {
289
+ const len = this.buf[this.pos++];
297
290
 
298
- this.pos += len;
299
- if (Buffer.isEncoding(encoding)) {
300
- return this.buf.toString(encoding, this.pos - len, this.pos);
291
+ // fast-path: if length encoded is < to 16, value is in safe integer range, using atoi
292
+ if (len < 16) {
293
+ return BigInt(this._atoi(len));
301
294
  }
302
- return Iconv.decode(this.buf.slice(this.pos - len, this.pos), encoding);
303
- }
304
295
 
305
- readLongLengthEncoded(supportBigInt, supportBigNumbers, bigNumberStrings, unsigned) {
306
- const len = this.readUnsignedLength();
307
- if (len === null) return null;
296
+ if (len === 0xfb) return null;
308
297
 
309
- if (supportBigInt) {
310
- const str = this.buf.toString('ascii', this.pos, this.pos + len);
311
- this.pos += len;
312
- return BigInt(str);
313
- }
298
+ return this.readBigIntFromLen(len);
299
+ }
314
300
 
315
- let result = 0;
301
+ readBigIntFromLen(len) {
302
+ // atoll
303
+ let result = 0n;
316
304
  let negate = false;
317
305
  let begin = this.pos;
318
306
 
319
- //minus sign
320
307
  if (len > 0 && this.buf[begin] === 45) {
308
+ //minus sign
321
309
  negate = true;
322
310
  begin++;
323
311
  }
324
312
  for (; begin < this.pos + len; begin++) {
325
- result = result * 10 + (this.buf[begin] - 48);
313
+ result = result * 10n + BigInt(this.buf[begin] - 48);
326
314
  }
327
-
328
- let val = negate ? -1 * result : result;
329
315
  this.pos += len;
330
-
331
- if (!Number.isSafeInteger(val)) {
332
- const str = this.buf.toString('ascii', this.pos - len, this.pos);
333
- if (bigNumberStrings) return str;
334
- if (supportBigNumbers) {
335
- return Long.fromString(str, unsigned, 10);
336
- }
337
- }
338
- return val;
316
+ return negate ? -1n * result : result;
339
317
  }
340
318
 
341
- readDecimalLengthEncoded(bigNumberStrings) {
342
- const len = this.readUnsignedLength();
343
- if (len === null) return null;
344
-
319
+ readDecimalLengthEncoded() {
320
+ const len = this.buf[this.pos++];
321
+ if (len === 0xfb) return null;
345
322
  this.pos += len;
346
- let str = this.buf.toString('ascii', this.pos - len, this.pos);
347
- return bigNumberStrings ? str : +str;
323
+ return this.buf.toString('ascii', this.pos - len, this.pos);
348
324
  }
349
325
 
350
326
  readDate() {
351
- const len = this.readUnsignedLength();
352
- if (len === null) return null;
353
-
327
+ const len = this.buf[this.pos++];
328
+ if (len === 0xfb) return null;
354
329
  let res = [];
355
330
  let value = 0;
356
331
  let initPos = this.pos;
@@ -373,25 +348,168 @@ class Packet {
373
348
  return new Date(res[0], res[1] - 1, res[2]);
374
349
  }
375
350
 
376
- readDateTime(opts) {
377
- const len = this.readUnsignedLength();
378
- if (len === null) return null;
351
+ readBinaryDate(opts) {
352
+ const len = this.buf[this.pos++];
353
+ let year = 0;
354
+ let month = 0;
355
+ let day = 0;
356
+ if (len > 0) {
357
+ year = this.readInt16();
358
+ if (len > 2) {
359
+ month = this.readUInt8() - 1;
360
+ if (len > 3) {
361
+ day = this.readUInt8();
362
+ }
363
+ }
364
+ }
365
+ if (year === 0 && month === 0 && day === 0) return opts.dateStrings ? '0000-00-00' : null;
366
+ if (opts.dateStrings) {
367
+ return `${appendZero(year, 4)}-${appendZero(month + 1, 2)}-${appendZero(day, 2)}`;
368
+ }
369
+ //handle zero-date as null
370
+ return new Date(year, month, day);
371
+ }
372
+
373
+ readDateTime() {
374
+ const len = this.buf[this.pos++];
375
+ if (len === 0xfb) return null;
379
376
  this.pos += len;
380
377
  const str = this.buf.toString('ascii', this.pos - len, this.pos);
381
378
  if (str.startsWith('0000-00-00 00:00:00')) return null;
379
+ return new Date(str);
380
+ }
382
381
 
383
- if (opts.tz) {
384
- return new Date(
385
- moment.tz(str, opts.tz).clone().tz(opts._localTz).format('YYYY-MM-DD HH:mm:ss.SSSSSS')
386
- );
382
+ readBinaryDateTime() {
383
+ const len = this.buf[this.pos++];
384
+ let year = 0;
385
+ let month = 0;
386
+ let day = 0;
387
+ let hour = 0;
388
+ let min = 0;
389
+ let sec = 0;
390
+ let microSec = 0;
391
+
392
+ if (len > 0) {
393
+ year = this.readInt16();
394
+ if (len > 2) {
395
+ month = this.readUInt8();
396
+ if (len > 3) {
397
+ day = this.readUInt8();
398
+ if (len > 4) {
399
+ hour = this.readUInt8();
400
+ min = this.readUInt8();
401
+ sec = this.readUInt8();
402
+ if (len > 7) {
403
+ microSec = this.readUInt32();
404
+ }
405
+ }
406
+ }
407
+ }
387
408
  }
388
- return new Date(str);
409
+
410
+ //handle zero-date as null
411
+ if (year === 0 && month === 0 && day === 0 && hour === 0 && min === 0 && sec === 0 && microSec === 0) return null;
412
+ return new Date(year, month - 1, day, hour, min, sec, microSec / 1000);
413
+ }
414
+
415
+ readBinaryDateTimeAsString(scale) {
416
+ const len = this.buf[this.pos++];
417
+ let year = 0;
418
+ let month = 0;
419
+ let day = 0;
420
+ let hour = 0;
421
+ let min = 0;
422
+ let sec = 0;
423
+ let microSec = 0;
424
+
425
+ if (len > 0) {
426
+ year = this.readInt16();
427
+ if (len > 2) {
428
+ month = this.readUInt8();
429
+ if (len > 3) {
430
+ day = this.readUInt8();
431
+ if (len > 4) {
432
+ hour = this.readUInt8();
433
+ min = this.readUInt8();
434
+ sec = this.readUInt8();
435
+ if (len > 7) {
436
+ microSec = this.readUInt32();
437
+ }
438
+ }
439
+ }
440
+ }
441
+ }
442
+
443
+ //handle zero-date as null
444
+ if (year === 0 && month === 0 && day === 0 && hour === 0 && min === 0 && sec === 0 && microSec === 0)
445
+ return '0000-00-00 00:00:00' + (scale > 0 ? '.000000'.substring(0, scale + 1) : '');
446
+
447
+ return (
448
+ appendZero(year, 4) +
449
+ '-' +
450
+ appendZero(month, 2) +
451
+ '-' +
452
+ appendZero(day, 2) +
453
+ ' ' +
454
+ appendZero(hour, 2) +
455
+ ':' +
456
+ appendZero(min, 2) +
457
+ ':' +
458
+ appendZero(sec, 2) +
459
+ (microSec > 0
460
+ ? scale > 0
461
+ ? '.' + appendZero(microSec, 6).substring(0, scale)
462
+ : '.' + appendZero(microSec, 6)
463
+ : scale > 0
464
+ ? '.' + appendZero(microSec, 6).substring(0, scale)
465
+ : '')
466
+ );
467
+ }
468
+
469
+ readBinaryTime() {
470
+ const len = this.buf[this.pos++];
471
+ let negate = false;
472
+ let hour = 0;
473
+ let min = 0;
474
+ let sec = 0;
475
+ let microSec = 0;
476
+
477
+ if (len > 0) {
478
+ negate = this.buf[this.pos++] === 1;
479
+ hour = this.readUInt32() * 24 + this.readUInt8();
480
+ min = this.readUInt8();
481
+ sec = this.readUInt8();
482
+ if (len > 8) {
483
+ microSec = this.readUInt32();
484
+ }
485
+ }
486
+ let val = appendZero(hour, 2) + ':' + appendZero(min, 2) + ':' + appendZero(sec, 2);
487
+ if (microSec > 0) {
488
+ val += '.' + appendZero(microSec, 6);
489
+ }
490
+ if (negate) return '-' + val;
491
+ return val;
492
+ }
493
+
494
+ readFloat() {
495
+ const val = this.buf.readFloatLE(this.pos);
496
+ this.pos += 4;
497
+ return val;
498
+ }
499
+
500
+ readDouble() {
501
+ const val = this.buf.readDoubleLE(this.pos);
502
+ this.pos += 8;
503
+ return val;
389
504
  }
390
505
 
391
506
  readIntLengthEncoded() {
392
- const len = this.readUnsignedLength();
393
- if (len === null) return null;
507
+ const len = this.buf[this.pos++];
508
+ if (len === 0xfb) return null;
509
+ return this._atoi(len);
510
+ }
394
511
 
512
+ _atoi(len) {
395
513
  let result = 0;
396
514
  let negate = false;
397
515
  let begin = this.pos;
@@ -416,33 +534,19 @@ class Packet {
416
534
  }
417
535
 
418
536
  skipLengthCodedNumber() {
419
- const type = this.buf[this.pos++] & 0xff;
537
+ const type = this.buf[this.pos++];
420
538
  switch (type) {
421
539
  case 251:
422
540
  return;
423
541
  case 252:
424
- this.pos +=
425
- 2 + (0xffff & ((this.buf[this.pos] & 0xff) + ((this.buf[this.pos + 1] & 0xff) << 8)));
542
+ this.pos += 2 + (0xffff & (this.buf[this.pos] + (this.buf[this.pos + 1] << 8)));
426
543
  return;
427
544
  case 253:
428
545
  this.pos +=
429
- 3 +
430
- (0xffffff &
431
- ((this.buf[this.pos] & 0xff) +
432
- ((this.buf[this.pos + 1] & 0xff) << 8) +
433
- ((this.buf[this.pos + 2] & 0xff) << 16)));
546
+ 3 + (0xffffff & (this.buf[this.pos] + (this.buf[this.pos + 1] << 8) + (this.buf[this.pos + 2] << 16)));
434
547
  return;
435
548
  case 254:
436
- this.pos +=
437
- 8 +
438
- ((this.buf[this.pos] & 0xff) +
439
- ((this.buf[this.pos + 1] & 0xff) << 8) +
440
- ((this.buf[this.pos + 2] & 0xff) << 16) +
441
- ((this.buf[this.pos + 3] & 0xff) << 24) +
442
- ((this.buf[this.pos + 4] & 0xff) << 32) +
443
- ((this.buf[this.pos + 5] & 0xff) << 40) +
444
- ((this.buf[this.pos + 6] & 0xff) << 48) +
445
- ((this.buf[this.pos + 7] & 0xff) << 56));
549
+ this.pos += 8 + Number(this.buf.readBigUInt64LE(this.pos));
446
550
  return;
447
551
  default:
448
552
  this.pos += type;
@@ -450,30 +554,11 @@ class Packet {
450
554
  }
451
555
  }
452
556
 
453
- positionFromEnd(num) {
454
- this.pos = this.end - num;
455
- }
456
-
457
- /**
458
- * For testing purpose only
459
- */
460
- _toBuf() {
461
- return this.buf.slice(this.pos, this.end);
462
- }
463
-
464
- forceOffset(off) {
465
- this.pos = off;
466
- }
467
-
468
557
  length() {
469
558
  return this.end - this.pos;
470
559
  }
471
560
 
472
- subPacketLengthEncoded() {
473
- const len = this.readUnsignedLength();
474
- this.skip(len);
475
- return new Packet(this.buf, this.pos - len, this.pos);
476
- }
561
+ subPacketLengthEncoded(len) {}
477
562
 
478
563
  /**
479
564
  * Parse ERR_Packet : https://mariadb.com/kb/en/library/err_packet/
@@ -485,31 +570,31 @@ class Packet {
485
570
  */
486
571
  readError(info, sql, stack) {
487
572
  this.skip(1);
488
- let errorCode = this.readUInt16();
489
- let sqlState = '';
490
-
573
+ let errno = this.readUInt16();
574
+ let sqlState;
575
+ let msg;
576
+ // check '#'
491
577
  if (this.peek() === 0x23) {
578
+ // skip '#'
492
579
  this.skip(6);
493
- sqlState = this.buf.toString('utf8', this.pos - 5, this.pos);
580
+ sqlState = this.buf.toString(undefined, this.pos - 5, this.pos);
581
+ msg = this.readStringNullEnded();
582
+ } else {
583
+ // pre 4.1 format
584
+ sqlState = 'HY000';
585
+ msg = this.buf.toString(undefined, this.pos, this.end);
494
586
  }
495
-
496
- let msg = this.buf.toString('utf8', this.pos, this.end);
497
587
  let fatal = sqlState.startsWith('08') || sqlState === '70100';
498
- if (fatal) {
499
- const packetMsgs = info.getLastPackets();
500
- if (packetMsgs !== '')
501
- return Errors.createError(
502
- msg + '\nlast received packets:\n' + packetMsgs,
503
- sql,
504
- fatal,
505
- info,
506
- sqlState,
507
- errorCode,
508
- stack
509
- );
510
- }
511
- return Errors.createError(msg, sql, fatal, info, sqlState, errorCode, stack);
588
+ return Errors.createError(msg, errno, info, sqlState, sql, fatal, stack);
512
589
  }
513
590
  }
514
591
 
592
+ const appendZero = (val, len) => {
593
+ let st = val.toString();
594
+ while (st.length < len) {
595
+ st = '0' + st;
596
+ }
597
+ return st;
598
+ };
599
+
515
600
  module.exports = Packet;