@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,502 @@
1
+ 'use strict';
2
+
3
+ const Iconv = require('iconv-lite');
4
+ const Utils = require('../misc/utils');
5
+
6
+ const QUOTE = 0x27;
7
+ const DBL_QUOTE = 0x22;
8
+ const ZERO_BYTE = 0x00;
9
+ const SLASH = 0x5c;
10
+
11
+ //increase by level to avoid buffer copy.
12
+ const SMALL_BUFFER_SIZE = 1024;
13
+ const MEDIUM_BUFFER_SIZE = 16384; //16k
14
+ const LARGE_BUFFER_SIZE = 131072; //128k
15
+ const BIG_BUFFER_SIZE = 1048576; //1M
16
+ const MAX_BUFFER_SIZE = 16777219; //16M + 4
17
+ const CHARS_GLOBAL_REGEXP = /[\0\"\'\\\b\n\r\t\u001A]/g; // eslint-disable-line no-control-regex
18
+
19
+ /**
20
+ * MySQL packet builder.
21
+ *
22
+ * @param opts options
23
+ * @param info connection info
24
+ * @constructor
25
+ */
26
+ class PacketOutputStream {
27
+ constructor(opts, info) {
28
+ this.opts = opts;
29
+ this.info = info;
30
+ this.pos = 4;
31
+ this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
32
+ this.changeEncoding(this.opts.collation);
33
+ this.changeDebug(this.opts.logPackets, this.opts.debug);
34
+
35
+ this.opts.on('collation', this.changeEncoding.bind(this));
36
+ this.opts.on('debug', this.changeDebug.bind(this));
37
+ }
38
+
39
+ changeEncoding(collation) {
40
+ this.encoding = collation.charset;
41
+ if (this.encoding === 'utf8') {
42
+ this.writeString = this.writeDefaultBufferString;
43
+ this.writeStringEscapeQuote = this.writeUtf8StringEscapeQuote;
44
+ } else if (Buffer.isEncoding(this.encoding)) {
45
+ this.writeString = this.writeDefaultBufferString;
46
+ this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
47
+ } else {
48
+ this.writeString = this.writeDefaultIconvString;
49
+ this.writeStringEscapeQuote = this.writeDefaultStringEscapeQuote;
50
+ }
51
+ }
52
+
53
+ changeDebug(logPackets, debug) {
54
+ this.logPackets = logPackets;
55
+ this.debug = debug;
56
+ this.flushBuffer =
57
+ this.logPackets || this.debug ? this.flushBufferDebug : this.flushBufferBasic;
58
+ }
59
+
60
+ setStream(stream) {
61
+ this.stream = stream;
62
+ }
63
+
64
+ growBuffer(len) {
65
+ let newCapacity;
66
+ if (len + this.pos < MEDIUM_BUFFER_SIZE) {
67
+ newCapacity = MEDIUM_BUFFER_SIZE;
68
+ } else if (len + this.pos < LARGE_BUFFER_SIZE) {
69
+ newCapacity = LARGE_BUFFER_SIZE;
70
+ } else if (len + this.pos < BIG_BUFFER_SIZE) {
71
+ newCapacity = BIG_BUFFER_SIZE;
72
+ } else newCapacity = MAX_BUFFER_SIZE;
73
+
74
+ let newBuf = Buffer.allocUnsafe(newCapacity);
75
+ this.buf.copy(newBuf, 0, 0, this.pos);
76
+ this.buf = newBuf;
77
+ }
78
+
79
+ startPacket(cmd) {
80
+ this.cmd = cmd;
81
+ this.pos = 4;
82
+ }
83
+
84
+ writeInt8(value) {
85
+ if (this.pos + 1 >= this.buf.length) {
86
+ if (this.pos >= MAX_BUFFER_SIZE) {
87
+ //buffer is more than a Packet, must flushBuffer()
88
+ this.flushBuffer(false, 1);
89
+ } else this.growBuffer(1);
90
+ }
91
+ this.buf[this.pos++] = value;
92
+ }
93
+
94
+ writeInt16(value) {
95
+ if (this.pos + 2 >= this.buf.length) {
96
+ let b = Buffer.allocUnsafe(2);
97
+ b.writeUInt16LE(value, 0);
98
+ this.writeBuffer(b, 0, 2);
99
+ return;
100
+ }
101
+ this.buf[this.pos] = value;
102
+ this.buf[this.pos + 1] = value >> 8;
103
+ this.pos += 2;
104
+ }
105
+
106
+ writeInt16AtPos(initPos) {
107
+ this.buf[initPos] = this.pos - initPos - 2;
108
+ this.buf[initPos + 1] = (this.pos - initPos - 2) >> 8;
109
+ }
110
+
111
+ writeInt32(value) {
112
+ if (this.pos + 4 >= this.buf.length) {
113
+ //not enough space remaining
114
+ let arr = Buffer.allocUnsafe(4);
115
+ arr.writeInt32LE(value, 0);
116
+ this.writeBuffer(arr, 0, 4);
117
+ return;
118
+ }
119
+
120
+ this.buf[this.pos] = value;
121
+ this.buf[this.pos + 1] = value >> 8;
122
+ this.buf[this.pos + 2] = value >> 16;
123
+ this.buf[this.pos + 3] = value >> 24;
124
+ this.pos += 4;
125
+ }
126
+
127
+ writeLengthCoded(len) {
128
+ //length encoded can be null(0xfb) or bigger than 65k, but only if using binary protocol
129
+ //so not implemented for now
130
+ if (len < 0xfb) {
131
+ this.writeInt8(len);
132
+ return;
133
+ }
134
+
135
+ //max length is len < 0xffff
136
+ this.writeInt8(0xfc);
137
+ this.writeInt16(len);
138
+ }
139
+
140
+ writeBuffer(arr, off, len) {
141
+ if (len > this.buf.length - this.pos) {
142
+ if (this.buf.length !== MAX_BUFFER_SIZE) {
143
+ this.growBuffer(len);
144
+ }
145
+
146
+ //max buffer size
147
+ if (len > this.buf.length - this.pos) {
148
+ //not enough space in buffer, will stream :
149
+ // fill buffer and flush until all data are snd
150
+ let remainingLen = len;
151
+
152
+ while (true) {
153
+ //filling buffer
154
+ let lenToFillBuffer = Math.min(MAX_BUFFER_SIZE - this.pos, remainingLen);
155
+ arr.copy(this.buf, this.pos, off, off + lenToFillBuffer);
156
+ remainingLen -= lenToFillBuffer;
157
+ off += lenToFillBuffer;
158
+ this.pos += lenToFillBuffer;
159
+
160
+ if (remainingLen === 0) return;
161
+ this.flushBuffer(false, remainingLen);
162
+ }
163
+ }
164
+ }
165
+ arr.copy(this.buf, this.pos, off, off + len);
166
+ this.pos += len;
167
+ }
168
+
169
+ /**
170
+ * Write ascii string to socket (no escaping)
171
+ *
172
+ * @param str string
173
+ */
174
+ writeStringAscii(str) {
175
+ let len = str.length;
176
+
177
+ //not enough space remaining
178
+ if (len >= this.buf.length - this.pos) {
179
+ let strBuf = Buffer.from(str, 'ascii');
180
+ this.writeBuffer(strBuf, 0, strBuf.length);
181
+ return;
182
+ }
183
+
184
+ for (let off = 0; off < len; ) {
185
+ this.buf[this.pos++] = str.charCodeAt(off++);
186
+ }
187
+ }
188
+
189
+ writeUtf8StringEscapeQuote(str) {
190
+ const charsLength = str.length;
191
+
192
+ //not enough space remaining
193
+ if (charsLength * 3 + 2 >= this.buf.length - this.pos) {
194
+ const arr = Buffer.from(str, 'utf8');
195
+ this.writeInt8(QUOTE);
196
+ this.writeBufferEscape(arr);
197
+ this.writeInt8(QUOTE);
198
+ return;
199
+ }
200
+
201
+ //create UTF-8 byte array
202
+ //since javascript char are internally using UTF-16 using surrogate's pattern, 4 bytes unicode characters will
203
+ //represent 2 characters : example "\uD83C\uDFA4" = 🎤 unicode 8 "no microphones"
204
+ //so max size is 3 * charLength
205
+ //(escape characters are 1 byte encoded, so length might only be 2 when escaped)
206
+ // + 2 for the quotes for text protocol
207
+ let charsOffset = 0;
208
+ let currChar;
209
+ this.buf[this.pos++] = QUOTE;
210
+ //quick loop if only ASCII chars for faster escape
211
+ for (
212
+ ;
213
+ charsOffset < charsLength && (currChar = str.charCodeAt(charsOffset)) < 0x80;
214
+ charsOffset++
215
+ ) {
216
+ if (
217
+ currChar === SLASH ||
218
+ currChar === QUOTE ||
219
+ currChar === ZERO_BYTE ||
220
+ currChar === DBL_QUOTE
221
+ ) {
222
+ this.buf[this.pos++] = SLASH;
223
+ }
224
+ this.buf[this.pos++] = currChar;
225
+ }
226
+
227
+ //if quick loop not finished
228
+ while (charsOffset < charsLength) {
229
+ currChar = str.charCodeAt(charsOffset++);
230
+ if (currChar < 0x80) {
231
+ if (
232
+ currChar === SLASH ||
233
+ currChar === QUOTE ||
234
+ currChar === ZERO_BYTE ||
235
+ currChar === DBL_QUOTE
236
+ ) {
237
+ this.buf[this.pos++] = SLASH;
238
+ }
239
+ this.buf[this.pos++] = currChar;
240
+ } else if (currChar < 0x800) {
241
+ this.buf[this.pos++] = 0xc0 | (currChar >> 6);
242
+ this.buf[this.pos++] = 0x80 | (currChar & 0x3f);
243
+ } else if (currChar >= 0xd800 && currChar < 0xe000) {
244
+ //reserved for surrogate - see https://en.wikipedia.org/wiki/UTF-16
245
+ if (currChar < 0xdc00) {
246
+ //is high surrogate
247
+ if (charsOffset + 1 > charsLength) {
248
+ this.buf[this.pos++] = 0x3f;
249
+ } else {
250
+ const nextChar = str.charCodeAt(charsOffset);
251
+ if (nextChar >= 0xdc00 && nextChar < 0xe000) {
252
+ //is low surrogate
253
+ const surrogatePairs =
254
+ (currChar << 10) + nextChar + (0x010000 - (0xd800 << 10) - 0xdc00);
255
+ this.buf[this.pos++] = 0xf0 | (surrogatePairs >> 18);
256
+ this.buf[this.pos++] = 0x80 | ((surrogatePairs >> 12) & 0x3f);
257
+ this.buf[this.pos++] = 0x80 | ((surrogatePairs >> 6) & 0x3f);
258
+ this.buf[this.pos++] = 0x80 | (surrogatePairs & 0x3f);
259
+ charsOffset++;
260
+ } else {
261
+ //must have low surrogate
262
+ this.buf[this.pos++] = 0x3f;
263
+ }
264
+ }
265
+ } else {
266
+ //low surrogate without high surrogate before
267
+ this.buf[this.pos++] = 0x3f;
268
+ }
269
+ } else {
270
+ this.buf[this.pos++] = 0xe0 | (currChar >> 12);
271
+ this.buf[this.pos++] = 0x80 | ((currChar >> 6) & 0x3f);
272
+ this.buf[this.pos++] = 0x80 | (currChar & 0x3f);
273
+ }
274
+ }
275
+ this.buf[this.pos++] = QUOTE;
276
+ }
277
+
278
+ writeDefaultBufferString(str) {
279
+ //javascript use UCS-2 or UTF-16 string internal representation
280
+ //that means that string to byte will be a maximum of * 3
281
+ // (4 bytes utf-8 are represented on 2 UTF-16 characters)
282
+ if (str.length * 3 < this.buf.length - this.pos) {
283
+ this.pos += this.buf.write(str, this.pos, this.encoding);
284
+ return;
285
+ }
286
+
287
+ //checking real length
288
+ let byteLength = Buffer.byteLength(str, this.encoding);
289
+ if (byteLength > this.buf.length - this.pos) {
290
+ if (this.buf.length < MAX_BUFFER_SIZE) {
291
+ this.growBuffer(byteLength);
292
+ }
293
+ if (byteLength > this.buf.length - this.pos) {
294
+ //not enough space in buffer, will stream :
295
+ let strBuf = Buffer.from(str, this.encoding);
296
+ this.writeBuffer(strBuf, 0, strBuf.length);
297
+ return;
298
+ }
299
+ }
300
+ this.pos += this.buf.write(str, this.pos, this.encoding);
301
+ }
302
+
303
+ writeDefaultIconvString(str) {
304
+ let buf = Iconv.encode(str, this.encoding);
305
+ this.writeBuffer(buf, 0, buf.length);
306
+ }
307
+
308
+ /**
309
+ * Parameters need to be properly escaped :
310
+ * following characters are to be escaped by "\" :
311
+ * - \0
312
+ * - \\
313
+ * - \'
314
+ * - \"
315
+ * regex split part of string writing part, and escaping special char.
316
+ * Those chars are <= 7f meaning that this will work even with multi-byte encoding
317
+ *
318
+ * @param str string to escape.
319
+ */
320
+ writeDefaultStringEscapeQuote(str) {
321
+ this.writeInt8(QUOTE);
322
+ let match;
323
+ let lastIndex = 0;
324
+ while ((match = CHARS_GLOBAL_REGEXP.exec(str)) !== null) {
325
+ this.writeString(str.slice(lastIndex, match.index));
326
+ this.writeInt8(SLASH);
327
+ this.writeInt8(match[0].charCodeAt(0));
328
+ lastIndex = CHARS_GLOBAL_REGEXP.lastIndex;
329
+ }
330
+
331
+ if (lastIndex === 0) {
332
+ // Nothing was escaped
333
+ this.writeString(str);
334
+ this.writeInt8(QUOTE);
335
+ return;
336
+ }
337
+
338
+ if (lastIndex < str.length) {
339
+ this.writeString(str.slice(lastIndex));
340
+ }
341
+ this.writeInt8(QUOTE);
342
+ }
343
+
344
+ writeBufferEscape(val) {
345
+ let valLen = val.length;
346
+ if (valLen * 2 > this.buf.length - this.pos) {
347
+ //makes buffer bigger (up to 16M)
348
+ if (this.buf.length !== MAX_BUFFER_SIZE) this.growBuffer(valLen * 2);
349
+
350
+ //data may still be bigger than buffer.
351
+ //must flush buffer when full (and reset position to 4)
352
+ if (valLen * 2 > this.buf.length - this.pos) {
353
+ //not enough space in buffer, will fill buffer
354
+ for (let i = 0; i < valLen; i++) {
355
+ switch (val[i]) {
356
+ case QUOTE:
357
+ case SLASH:
358
+ case DBL_QUOTE:
359
+ case ZERO_BYTE:
360
+ if (this.pos >= this.buf.length) this.flushBuffer(false, (valLen - i) * 2);
361
+ this.buf[this.pos++] = SLASH; //add escape slash
362
+ }
363
+ if (this.pos >= this.buf.length) this.flushBuffer(false, (valLen - i) * 2);
364
+ this.buf[this.pos++] = val[i];
365
+ }
366
+ return;
367
+ }
368
+ }
369
+
370
+ //sure to have enough place to use buffer directly
371
+ for (let i = 0; i < valLen; i++) {
372
+ switch (val[i]) {
373
+ case QUOTE:
374
+ case SLASH:
375
+ case DBL_QUOTE:
376
+ case ZERO_BYTE:
377
+ this.buf[this.pos++] = SLASH; //add escape slash
378
+ }
379
+ this.buf[this.pos++] = val[i];
380
+ }
381
+ }
382
+
383
+ /**
384
+ * Indicate if buffer contain any data.
385
+ * @returns {boolean}
386
+ */
387
+ isEmpty() {
388
+ return this.pos <= 4;
389
+ }
390
+
391
+ /**
392
+ * Flush the internal buffer.
393
+ */
394
+ flushBufferDebug(commandEnd, remainingLen) {
395
+ this.buf[0] = this.pos - 4;
396
+ this.buf[1] = (this.pos - 4) >>> 8;
397
+ this.buf[2] = (this.pos - 4) >>> 16;
398
+ this.buf[3] = ++this.cmd.sequenceNo;
399
+
400
+ this.stream.writeBuf(this.buf.slice(0, this.pos), this.cmd);
401
+
402
+ const packet = Utils.log(this.opts, this.buf, 0, this.pos);
403
+ if (this.opts.logPackets) {
404
+ this.info.addPacket(
405
+ '==> conn:' +
406
+ (this.info.threadId ? this.info.threadId : -1) +
407
+ ' ' +
408
+ this.cmd.constructor.name +
409
+ '(0,' +
410
+ this.pos +
411
+ ')\n' +
412
+ packet
413
+ );
414
+ }
415
+
416
+ if (this.opts.debug) {
417
+ console.log(
418
+ '==> conn:%d %s\n%s',
419
+ this.info.threadId ? this.info.threadId : -1,
420
+ this.cmd.constructor.name + '(0,' + this.pos + ')',
421
+ Utils.log(this.opts, this.buf, 0, this.pos)
422
+ );
423
+ }
424
+
425
+ if (commandEnd) {
426
+ //if last packet fill the max size, must send an empty com to indicate that command end.
427
+ if (this.pos === MAX_BUFFER_SIZE) {
428
+ this.writeEmptyPacket();
429
+ } else {
430
+ this.stream.flush(true, this.cmd);
431
+ this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
432
+ }
433
+ } else {
434
+ this.buf = allocateBuffer(remainingLen + 4);
435
+ this.pos = 4;
436
+ }
437
+ }
438
+
439
+ flushBufferBasic(commandEnd, remainingLen) {
440
+ this.buf[0] = this.pos - 4;
441
+ this.buf[1] = (this.pos - 4) >>> 8;
442
+ this.buf[2] = (this.pos - 4) >>> 16;
443
+ this.buf[3] = ++this.cmd.sequenceNo;
444
+ this.stream.writeBuf(this.buf.slice(0, this.pos), this.cmd);
445
+ if (commandEnd) {
446
+ //if last packet fill the max size, must send an empty com to indicate that command end.
447
+ if (this.pos === MAX_BUFFER_SIZE) {
448
+ this.writeEmptyPacket();
449
+ } else {
450
+ this.stream.flush(true, this.cmd);
451
+ this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
452
+ }
453
+ } else {
454
+ this.buf = allocateBuffer(remainingLen + 4);
455
+ this.pos = 4;
456
+ }
457
+ }
458
+
459
+ writeEmptyPacket() {
460
+ const emptyBuf = Buffer.from([0x00, 0x00, 0x00, ++this.cmd.sequenceNo]);
461
+
462
+ if (this.opts.logPackets || this.opts.debug) {
463
+ const packet = Utils.log(this.opts, emptyBuf, 0, 4);
464
+ if (this.opts.logPackets) {
465
+ this.info.addPacket(
466
+ '==> conn:' +
467
+ (this.info.threadId ? this.info.threadId : -1) +
468
+ ' ' +
469
+ this.cmd.constructor.name +
470
+ '(0,4)\n' +
471
+ packet
472
+ );
473
+ }
474
+ if (this.opts.debug) {
475
+ console.log(
476
+ '==> conn:%d %s\n%s',
477
+ this.info.threadId ? this.info.threadId : -1,
478
+ this.cmd.constructor.name + '(0,4)',
479
+ packet
480
+ );
481
+ }
482
+ }
483
+
484
+ this.stream.writeBuf(emptyBuf, this.cmd);
485
+ this.stream.flush(true, this.cmd);
486
+ }
487
+ }
488
+
489
+ function allocateBuffer(len) {
490
+ if (len < SMALL_BUFFER_SIZE) {
491
+ return Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
492
+ } else if (len < MEDIUM_BUFFER_SIZE) {
493
+ return Buffer.allocUnsafe(MEDIUM_BUFFER_SIZE);
494
+ } else if (len < LARGE_BUFFER_SIZE) {
495
+ return Buffer.allocUnsafe(LARGE_BUFFER_SIZE);
496
+ } else if (len < BIG_BUFFER_SIZE) {
497
+ return Buffer.allocUnsafe(BIG_BUFFER_SIZE);
498
+ }
499
+ return Buffer.allocUnsafe(MAX_BUFFER_SIZE);
500
+ }
501
+
502
+ module.exports = PacketOutputStream;