@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.
- package/.turbo/turbo-build.log +14 -0
- package/README.md +34 -0
- package/README.zh-CN.md +34 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/dist/client/AppManager.d.ts +2 -0
- package/dist/client/AppNameInput.d.ts +2 -0
- package/dist/client/MultiAppBlockInitializer.d.ts +2 -0
- package/dist/client/MultiAppManagerProvider.d.ts +2 -0
- package/dist/client/MultiAppManagerProvider.style.d.ts +5 -0
- package/dist/client/Settings.d.ts +2 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +1 -0
- package/dist/client/settings/schemas/applications.d.ts +13 -0
- package/dist/client/utils.d.ts +4 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +27 -0
- package/dist/externalVersion.js +16 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/locale/en-US.json +27 -0
- package/dist/locale/es-ES.json +9 -0
- package/dist/locale/ko_KR.json +11 -0
- package/dist/locale/pt-BR.json +9 -0
- package/dist/locale/zh-CN.json +27 -0
- package/dist/node_modules/mariadb/LICENSE +502 -0
- package/dist/node_modules/mariadb/callback.js +41 -0
- package/dist/node_modules/mariadb/lib/cmd/batch-bulk.js +278 -0
- package/dist/node_modules/mariadb/lib/cmd/batch-rewrite.js +372 -0
- package/dist/node_modules/mariadb/lib/cmd/change-user.js +149 -0
- package/dist/node_modules/mariadb/lib/cmd/class/ok-packet.js +17 -0
- package/dist/node_modules/mariadb/lib/cmd/column-definition.js +102 -0
- package/dist/node_modules/mariadb/lib/cmd/command.js +168 -0
- package/dist/node_modules/mariadb/lib/cmd/common-binary-cmd.js +327 -0
- package/dist/node_modules/mariadb/lib/cmd/common-text-cmd.js +427 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/caching-sha2-password-auth.js +168 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/clear-password-auth.js +23 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/ed25519-password-auth.js +761 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/native-password-auth.js +55 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/pam-password-auth.js +58 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/plugin-auth.js +19 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/auth/sha256-password-auth.js +142 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/client-capabilities.js +74 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/client-handshake-response.js +126 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/handshake.js +292 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/initial-handshake.js +74 -0
- package/dist/node_modules/mariadb/lib/cmd/handshake/ssl-request.js +29 -0
- package/dist/node_modules/mariadb/lib/cmd/ping.js +52 -0
- package/dist/node_modules/mariadb/lib/cmd/query.js +255 -0
- package/dist/node_modules/mariadb/lib/cmd/quit.js +28 -0
- package/dist/node_modules/mariadb/lib/cmd/reset.js +54 -0
- package/dist/node_modules/mariadb/lib/cmd/resultset.js +607 -0
- package/dist/node_modules/mariadb/lib/cmd/stream.js +45 -0
- package/dist/node_modules/mariadb/lib/config/connection-options.js +258 -0
- package/dist/node_modules/mariadb/lib/config/pool-cluster-options.js +19 -0
- package/dist/node_modules/mariadb/lib/config/pool-options.js +47 -0
- package/dist/node_modules/mariadb/lib/connection-callback.js +160 -0
- package/dist/node_modules/mariadb/lib/connection.js +1460 -0
- package/dist/node_modules/mariadb/lib/const/capabilities.js +64 -0
- package/dist/node_modules/mariadb/lib/const/collations.js +473 -0
- package/dist/node_modules/mariadb/lib/const/connection_status.js +13 -0
- package/dist/node_modules/mariadb/lib/const/error-code.js +1282 -0
- package/dist/node_modules/mariadb/lib/const/field-detail.js +35 -0
- package/dist/node_modules/mariadb/lib/const/field-type.js +71 -0
- package/dist/node_modules/mariadb/lib/const/server-status.js +30 -0
- package/dist/node_modules/mariadb/lib/const/state-change.js +12 -0
- package/dist/node_modules/mariadb/lib/filtered-pool-cluster.js +81 -0
- package/dist/node_modules/mariadb/lib/io/bulk-packet.js +590 -0
- package/dist/node_modules/mariadb/lib/io/compression-input-stream.js +141 -0
- package/dist/node_modules/mariadb/lib/io/compression-output-stream.js +171 -0
- package/dist/node_modules/mariadb/lib/io/packet-input-stream.js +193 -0
- package/dist/node_modules/mariadb/lib/io/packet-node-encoded.js +36 -0
- package/dist/node_modules/mariadb/lib/io/packet-node-iconv.js +37 -0
- package/dist/node_modules/mariadb/lib/io/packet-output-stream.js +502 -0
- package/dist/node_modules/mariadb/lib/io/packet.js +515 -0
- package/dist/node_modules/mariadb/lib/io/rewrite-packet.js +481 -0
- package/dist/node_modules/mariadb/lib/misc/connection-information.js +96 -0
- package/dist/node_modules/mariadb/lib/misc/errors.js +123 -0
- package/dist/node_modules/mariadb/lib/misc/parse.js +1033 -0
- package/dist/node_modules/mariadb/lib/misc/utils.js +298 -0
- package/dist/node_modules/mariadb/lib/pool-base.js +611 -0
- package/dist/node_modules/mariadb/lib/pool-callback.js +202 -0
- package/dist/node_modules/mariadb/lib/pool-cluster-callback.js +66 -0
- package/dist/node_modules/mariadb/lib/pool-cluster.js +407 -0
- package/dist/node_modules/mariadb/lib/pool-promise.js +108 -0
- package/dist/node_modules/mariadb/package.json +1 -0
- package/dist/node_modules/mariadb/promise.js +34 -0
- package/dist/node_modules/mariadb/types/index.d.ts +870 -0
- package/dist/server/actions/apps.d.ts +5 -0
- package/dist/server/actions/apps.js +117 -0
- package/dist/server/app-lifecycle.d.ts +8 -0
- package/dist/server/app-lifecycle.js +99 -0
- package/dist/server/app-start-env.d.ts +2 -0
- package/dist/server/app-start-env.js +105 -0
- package/dist/server/collections/applications.d.ts +2 -0
- package/dist/server/collections/applications.js +82 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +29 -0
- package/dist/server/middlewares/app-selector.d.ts +1 -0
- package/dist/server/middlewares/app-selector.js +47 -0
- package/dist/server/middlewares/index.d.ts +2 -0
- package/dist/server/middlewares/index.js +23 -0
- package/dist/server/middlewares/inject-app-list.d.ts +1 -0
- package/dist/server/middlewares/inject-app-list.js +48 -0
- package/dist/server/migrations/20240820153000-add-apps-tmpl.d.ts +6 -0
- package/dist/server/migrations/20240820153000-add-apps-tmpl.js +47 -0
- package/dist/server/migrations/20241126124904-add-createdBy.d.ts +6 -0
- package/dist/server/migrations/20241126124904-add-createdBy.js +41 -0
- package/dist/server/models/application.d.ts +10 -0
- package/dist/server/models/application.js +57 -0
- package/dist/server/server.d.ts +19 -0
- package/dist/server/server.js +246 -0
- package/dist/swagger/index.d.ts +197 -0
- package/dist/swagger/index.js +227 -0
- package/package.json +38 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ZLib = require('zlib');
|
|
4
|
+
const Utils = require('../misc/utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* MySQL packet parser
|
|
8
|
+
* see : https://mariadb.com/kb/en/library/0-packet/
|
|
9
|
+
*/
|
|
10
|
+
class CompressionInputStream {
|
|
11
|
+
constructor(reader, receiveQueue, opts, info) {
|
|
12
|
+
this.reader = reader;
|
|
13
|
+
this.receiveQueue = receiveQueue;
|
|
14
|
+
this.info = info;
|
|
15
|
+
this.opts = opts;
|
|
16
|
+
this.header = Buffer.allocUnsafe(7);
|
|
17
|
+
this.headerLen = 0;
|
|
18
|
+
this.compressPacketLen = null;
|
|
19
|
+
this.packetLen = null;
|
|
20
|
+
this.remainingLen = null;
|
|
21
|
+
|
|
22
|
+
this.parts = null;
|
|
23
|
+
this.partsTotalLen = 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
receivePacket(chunk) {
|
|
27
|
+
let cmd = this.currentCmd();
|
|
28
|
+
if (this.opts.debugCompress) {
|
|
29
|
+
console.log(
|
|
30
|
+
'<== conn:%d %s (compress)\n%s',
|
|
31
|
+
this.info.threadId ? this.info.threadId : -1,
|
|
32
|
+
cmd
|
|
33
|
+
? cmd.onPacketReceive
|
|
34
|
+
? cmd.constructor.name + '.' + cmd.onPacketReceive.name
|
|
35
|
+
: cmd.constructor.name
|
|
36
|
+
: 'no command',
|
|
37
|
+
Utils.log(this.opts, chunk, 0, chunk.length, this.header)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
if (cmd) cmd.compressSequenceNo = this.header[3];
|
|
41
|
+
const unCompressLen = this.header[4] | (this.header[5] << 8) | (this.header[6] << 16);
|
|
42
|
+
if (unCompressLen === 0) {
|
|
43
|
+
this.reader.onData(chunk);
|
|
44
|
+
} else {
|
|
45
|
+
//use synchronous inflating, to ensure FIFO packet order
|
|
46
|
+
const unCompressChunk = ZLib.inflateSync(chunk);
|
|
47
|
+
this.reader.onData(unCompressChunk);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
currentCmd() {
|
|
52
|
+
let cmd;
|
|
53
|
+
while ((cmd = this.receiveQueue.peek())) {
|
|
54
|
+
if (cmd.onPacketReceive) return cmd;
|
|
55
|
+
this.receiveQueue.shift();
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
resetHeader() {
|
|
61
|
+
this.remainingLen = null;
|
|
62
|
+
this.headerLen = 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
onData(chunk) {
|
|
66
|
+
let pos = 0;
|
|
67
|
+
let length;
|
|
68
|
+
const chunkLen = chunk.length;
|
|
69
|
+
|
|
70
|
+
do {
|
|
71
|
+
if (this.remainingLen) {
|
|
72
|
+
length = this.remainingLen;
|
|
73
|
+
} else if (this.headerLen === 0 && chunkLen - pos >= 7) {
|
|
74
|
+
this.header[0] = chunk[pos];
|
|
75
|
+
this.header[1] = chunk[pos + 1];
|
|
76
|
+
this.header[2] = chunk[pos + 2];
|
|
77
|
+
this.header[3] = chunk[pos + 3];
|
|
78
|
+
this.header[4] = chunk[pos + 4];
|
|
79
|
+
this.header[5] = chunk[pos + 5];
|
|
80
|
+
this.header[6] = chunk[pos + 6];
|
|
81
|
+
this.headerLen = 7;
|
|
82
|
+
pos += 7;
|
|
83
|
+
this.compressPacketLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
|
84
|
+
this.packetLen = this.header[4] | (this.header[5] << 8) | (this.header[6] << 16);
|
|
85
|
+
if (this.packetLen === 0) this.packetLen = this.compressPacketLen;
|
|
86
|
+
length = this.compressPacketLen;
|
|
87
|
+
} else {
|
|
88
|
+
length = null;
|
|
89
|
+
while (chunkLen - pos > 0) {
|
|
90
|
+
this.header[this.headerLen++] = chunk[pos++];
|
|
91
|
+
if (this.headerLen === 7) {
|
|
92
|
+
this.compressPacketLen =
|
|
93
|
+
this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
|
94
|
+
this.packetLen = this.header[4] | (this.header[5] << 8) | (this.header[6] << 16);
|
|
95
|
+
if (this.packetLen === 0) this.packetLen = this.compressPacketLen;
|
|
96
|
+
length = this.compressPacketLen;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (length) {
|
|
103
|
+
if (chunkLen - pos >= length) {
|
|
104
|
+
const buf = chunk.slice(pos, pos + length);
|
|
105
|
+
pos += length;
|
|
106
|
+
if (this.parts) {
|
|
107
|
+
this.parts.push(buf);
|
|
108
|
+
this.partsTotalLen += length;
|
|
109
|
+
|
|
110
|
+
if (this.compressPacketLen < 0xffffff) {
|
|
111
|
+
let buf = Buffer.concat(this.parts, this.partsTotalLen);
|
|
112
|
+
this.parts = null;
|
|
113
|
+
this.receivePacket(buf);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
if (this.compressPacketLen < 0xffffff) {
|
|
117
|
+
this.receivePacket(buf);
|
|
118
|
+
} else {
|
|
119
|
+
this.parts = [buf];
|
|
120
|
+
this.partsTotalLen = length;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.resetHeader();
|
|
124
|
+
} else {
|
|
125
|
+
const buf = chunk.slice(pos, chunkLen);
|
|
126
|
+
if (!this.parts) {
|
|
127
|
+
this.parts = [buf];
|
|
128
|
+
this.partsTotalLen = chunkLen - pos;
|
|
129
|
+
} else {
|
|
130
|
+
this.parts.push(buf);
|
|
131
|
+
this.partsTotalLen += chunkLen - pos;
|
|
132
|
+
}
|
|
133
|
+
this.remainingLen = length - (chunkLen - pos);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} while (pos < chunkLen);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = CompressionInputStream;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Utils = require('../misc/utils');
|
|
4
|
+
const ZLib = require('zlib');
|
|
5
|
+
|
|
6
|
+
//increase by level to avoid buffer copy.
|
|
7
|
+
const SMALL_BUFFER_SIZE = 2048;
|
|
8
|
+
const MEDIUM_BUFFER_SIZE = 131072; //128k
|
|
9
|
+
const LARGE_BUFFER_SIZE = 1048576; //1M
|
|
10
|
+
const MAX_BUFFER_SIZE = 16777222; //16M + 7
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
/**
|
|
14
|
+
* MySQL compression filter.
|
|
15
|
+
* see https://mariadb.com/kb/en/library/0-packet/#compressed-packet
|
|
16
|
+
*/
|
|
17
|
+
class CompressionOutputStream {
|
|
18
|
+
/**
|
|
19
|
+
* Constructor
|
|
20
|
+
*
|
|
21
|
+
* @param socket current socket
|
|
22
|
+
* @param opts current connection options
|
|
23
|
+
* @param info current connection information
|
|
24
|
+
* @constructor
|
|
25
|
+
*/
|
|
26
|
+
constructor(socket, opts, info) {
|
|
27
|
+
this.info = info;
|
|
28
|
+
this.opts = opts;
|
|
29
|
+
this.pos = 7;
|
|
30
|
+
this.header = Buffer.allocUnsafe(7);
|
|
31
|
+
this.buf = Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
|
32
|
+
this.writer = (buffer) => {
|
|
33
|
+
socket.write(buffer);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
growBuffer(len) {
|
|
38
|
+
let newCapacity;
|
|
39
|
+
if (len + this.pos < MEDIUM_BUFFER_SIZE) {
|
|
40
|
+
newCapacity = MEDIUM_BUFFER_SIZE;
|
|
41
|
+
} else if (len + this.pos < LARGE_BUFFER_SIZE) {
|
|
42
|
+
newCapacity = LARGE_BUFFER_SIZE;
|
|
43
|
+
} else newCapacity = MAX_BUFFER_SIZE;
|
|
44
|
+
|
|
45
|
+
let newBuf = Buffer.allocUnsafe(newCapacity);
|
|
46
|
+
this.buf.copy(newBuf, 0, 0, this.pos);
|
|
47
|
+
this.buf = newBuf;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
writeBuf(arr, cmd) {
|
|
51
|
+
let off = 0,
|
|
52
|
+
len = arr.length;
|
|
53
|
+
if (len > this.buf.length - this.pos) {
|
|
54
|
+
if (this.buf.length !== MAX_BUFFER_SIZE) {
|
|
55
|
+
this.growBuffer(len);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//max buffer size
|
|
59
|
+
if (len > this.buf.length - this.pos) {
|
|
60
|
+
//not enough space in buffer, will stream :
|
|
61
|
+
// fill buffer and flush until all data are snd
|
|
62
|
+
let remainingLen = len;
|
|
63
|
+
|
|
64
|
+
while (true) {
|
|
65
|
+
//filling buffer
|
|
66
|
+
let lenToFillBuffer = Math.min(MAX_BUFFER_SIZE - this.pos, remainingLen);
|
|
67
|
+
arr.copy(this.buf, this.pos, off, off + lenToFillBuffer);
|
|
68
|
+
remainingLen -= lenToFillBuffer;
|
|
69
|
+
off += lenToFillBuffer;
|
|
70
|
+
this.pos += lenToFillBuffer;
|
|
71
|
+
|
|
72
|
+
if (remainingLen === 0) return;
|
|
73
|
+
this.flush(false, cmd, remainingLen);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
arr.copy(this.buf, this.pos, off, off + len);
|
|
78
|
+
this.pos += len;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Flush the internal buffer.
|
|
83
|
+
*/
|
|
84
|
+
flush(cmdEnd, cmd, remainingLen) {
|
|
85
|
+
if (this.pos < 1536) {
|
|
86
|
+
//*******************************************************************************
|
|
87
|
+
// small packet, no compression
|
|
88
|
+
//*******************************************************************************
|
|
89
|
+
|
|
90
|
+
this.buf[0] = this.pos - 7;
|
|
91
|
+
this.buf[1] = (this.pos - 7) >>> 8;
|
|
92
|
+
this.buf[2] = (this.pos - 7) >>> 16;
|
|
93
|
+
this.buf[3] = ++cmd.compressSequenceNo;
|
|
94
|
+
this.buf[4] = 0;
|
|
95
|
+
this.buf[5] = 0;
|
|
96
|
+
this.buf[6] = 0;
|
|
97
|
+
|
|
98
|
+
if (this.opts.debugCompress) {
|
|
99
|
+
console.log(
|
|
100
|
+
'==> conn:%d %s (compress)\n%s',
|
|
101
|
+
this.info.threadId ? this.info.threadId : -1,
|
|
102
|
+
cmd ? cmd.constructor.name + '(0,' + this.pos + ')' : 'unknown',
|
|
103
|
+
Utils.log(this.opts, this.buf, 0, this.pos)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.writer(this.buf.slice(0, this.pos));
|
|
108
|
+
} else {
|
|
109
|
+
//*******************************************************************************
|
|
110
|
+
// compressing packet
|
|
111
|
+
//*******************************************************************************
|
|
112
|
+
//use synchronous inflating, to ensure FIFO packet order
|
|
113
|
+
const compressChunk = ZLib.deflateSync(this.buf.slice(7, this.pos));
|
|
114
|
+
const compressChunkLen = compressChunk.length;
|
|
115
|
+
|
|
116
|
+
this.header[0] = compressChunkLen;
|
|
117
|
+
this.header[1] = compressChunkLen >>> 8;
|
|
118
|
+
this.header[2] = compressChunkLen >>> 16;
|
|
119
|
+
this.header[3] = ++cmd.compressSequenceNo;
|
|
120
|
+
this.header[4] = this.pos - 7;
|
|
121
|
+
this.header[5] = (this.pos - 7) >>> 8;
|
|
122
|
+
this.header[6] = (this.pos - 7) >>> 16;
|
|
123
|
+
|
|
124
|
+
if (this.opts.debugCompress) {
|
|
125
|
+
console.log(
|
|
126
|
+
'==> conn:%d %s (compress)\n%s',
|
|
127
|
+
this.info.threadId ? this.info.threadId : -1,
|
|
128
|
+
cmd ? cmd.constructor.name + '(0,' + this.pos + '=>' + compressChunkLen + ')' : 'unknown',
|
|
129
|
+
Utils.log(this.opts, compressChunk, 0, compressChunkLen, this.header)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.writer(this.header);
|
|
134
|
+
this.writer(compressChunk);
|
|
135
|
+
if (cmdEnd && this.pos === MAX_BUFFER_SIZE) this.writeEmptyPacket(cmd);
|
|
136
|
+
this.header = Buffer.allocUnsafe(7);
|
|
137
|
+
}
|
|
138
|
+
this.buf = remainingLen
|
|
139
|
+
? CompressionOutputStream.allocateBuffer(remainingLen)
|
|
140
|
+
: Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
|
141
|
+
this.pos = 7;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static allocateBuffer(len) {
|
|
145
|
+
if (len + 4 < SMALL_BUFFER_SIZE) {
|
|
146
|
+
return Buffer.allocUnsafe(SMALL_BUFFER_SIZE);
|
|
147
|
+
} else if (len + 4 < MEDIUM_BUFFER_SIZE) {
|
|
148
|
+
return Buffer.allocUnsafe(MEDIUM_BUFFER_SIZE);
|
|
149
|
+
} else if (len + 4 < LARGE_BUFFER_SIZE) {
|
|
150
|
+
return Buffer.allocUnsafe(LARGE_BUFFER_SIZE);
|
|
151
|
+
}
|
|
152
|
+
return Buffer.allocUnsafe(MAX_BUFFER_SIZE);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
writeEmptyPacket(cmd) {
|
|
156
|
+
const emptyBuf = Buffer.from([0x00, 0x00, 0x00, cmd.compressSequenceNo, 0x00, 0x00, 0x00]);
|
|
157
|
+
|
|
158
|
+
if (this.opts.debugCompress) {
|
|
159
|
+
console.log(
|
|
160
|
+
'==> conn:%d %s (compress)\n%s',
|
|
161
|
+
this.info.threadId ? this.info.threadId : -1,
|
|
162
|
+
cmd ? cmd.constructor.name + '(0,' + this.pos + ')' : 'unknown',
|
|
163
|
+
Utils.log(this.opts, emptyBuf, 0, 7)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this.writer(emptyBuf);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = CompressionOutputStream;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const PacketNodeEncoded = require('./packet-node-encoded');
|
|
4
|
+
const PacketIconvEncoded = require('./packet-node-iconv');
|
|
5
|
+
|
|
6
|
+
const Utils = require('../misc/utils');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* MySQL packet parser
|
|
10
|
+
* see : https://mariadb.com/kb/en/library/0-packet/
|
|
11
|
+
*/
|
|
12
|
+
class PacketInputStream {
|
|
13
|
+
constructor(unexpectedPacket, receiveQueue, out, opts, info) {
|
|
14
|
+
this.unexpectedPacket = unexpectedPacket;
|
|
15
|
+
this.opts = opts;
|
|
16
|
+
this.receiveQueue = receiveQueue;
|
|
17
|
+
this.info = info;
|
|
18
|
+
this.out = out;
|
|
19
|
+
|
|
20
|
+
//in case packet is not complete
|
|
21
|
+
this.header = Buffer.allocUnsafe(4);
|
|
22
|
+
this.headerLen = 0;
|
|
23
|
+
this.packetLen = null;
|
|
24
|
+
this.remainingLen = null;
|
|
25
|
+
|
|
26
|
+
this.parts = null;
|
|
27
|
+
this.partsTotalLen = 0;
|
|
28
|
+
this.changeEncoding(this.opts.collation);
|
|
29
|
+
this.changeDebug(this.opts.logPackets, this.opts.debug);
|
|
30
|
+
this.opts.on('collation', this.changeEncoding.bind(this));
|
|
31
|
+
this.opts.on('debug', this.changeDebug.bind(this));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
changeEncoding(collation) {
|
|
35
|
+
this.encoding = collation.charset;
|
|
36
|
+
this.packetConstructor = Buffer.isEncoding(this.encoding)
|
|
37
|
+
? PacketNodeEncoded
|
|
38
|
+
: PacketIconvEncoded;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
changeDebug(logPackets, debug) {
|
|
42
|
+
this.logPackets = logPackets;
|
|
43
|
+
this.debug = debug;
|
|
44
|
+
this.receivePacket =
|
|
45
|
+
this.logPackets || this.debug ? this.receivePacketDebug : this.receivePacketBasic;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
receivePacketDebug(packet) {
|
|
49
|
+
let cmd = this.currentCmd();
|
|
50
|
+
|
|
51
|
+
if (packet) {
|
|
52
|
+
const packetStr = Utils.log(this.opts, packet.buf, packet.pos, packet.end, this.header);
|
|
53
|
+
if (this.opts.logPackets) {
|
|
54
|
+
this.info.addPacket(
|
|
55
|
+
'<== conn:' +
|
|
56
|
+
(this.info.threadId ? this.info.threadId : -1) +
|
|
57
|
+
' ' +
|
|
58
|
+
(cmd
|
|
59
|
+
? cmd.onPacketReceive
|
|
60
|
+
? cmd.constructor.name + '.' + cmd.onPacketReceive.name
|
|
61
|
+
: cmd.constructor.name
|
|
62
|
+
: 'no command') +
|
|
63
|
+
' (' +
|
|
64
|
+
packet.pos +
|
|
65
|
+
',' +
|
|
66
|
+
packet.end +
|
|
67
|
+
'))\n' +
|
|
68
|
+
packetStr
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (this.opts.debug) {
|
|
72
|
+
console.log(
|
|
73
|
+
'<== conn:%d %s (%d,%d)\n%s',
|
|
74
|
+
this.info.threadId ? this.info.threadId : -1,
|
|
75
|
+
cmd
|
|
76
|
+
? cmd.onPacketReceive
|
|
77
|
+
? cmd.constructor.name + '.' + cmd.onPacketReceive.name
|
|
78
|
+
: cmd.constructor.name
|
|
79
|
+
: 'no command',
|
|
80
|
+
packet.pos,
|
|
81
|
+
packet.end,
|
|
82
|
+
packetStr
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!cmd) {
|
|
88
|
+
this.unexpectedPacket(packet);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
cmd.sequenceNo = this.header[3];
|
|
93
|
+
cmd.onPacketReceive(packet, this.out, this.opts, this.info);
|
|
94
|
+
if (!cmd.onPacketReceive) this.receiveQueue.shift();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
receivePacketBasic(packet) {
|
|
98
|
+
let cmd = this.currentCmd();
|
|
99
|
+
if (!cmd) {
|
|
100
|
+
this.unexpectedPacket(packet);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
cmd.sequenceNo = this.header[3];
|
|
104
|
+
cmd.onPacketReceive(packet, this.out, this.opts, this.info);
|
|
105
|
+
if (!cmd.onPacketReceive) this.receiveQueue.shift();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
resetHeader() {
|
|
109
|
+
this.remainingLen = null;
|
|
110
|
+
this.headerLen = 0;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
currentCmd() {
|
|
114
|
+
let cmd;
|
|
115
|
+
while ((cmd = this.receiveQueue.peek())) {
|
|
116
|
+
if (cmd.onPacketReceive) return cmd;
|
|
117
|
+
this.receiveQueue.shift();
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
onData(chunk) {
|
|
123
|
+
let pos = 0;
|
|
124
|
+
let length;
|
|
125
|
+
const chunkLen = chunk.length;
|
|
126
|
+
|
|
127
|
+
do {
|
|
128
|
+
//read header
|
|
129
|
+
if (this.remainingLen) {
|
|
130
|
+
length = this.remainingLen;
|
|
131
|
+
} else if (this.headerLen === 0 && chunkLen - pos >= 4) {
|
|
132
|
+
this.header[0] = chunk[pos];
|
|
133
|
+
this.header[1] = chunk[pos + 1];
|
|
134
|
+
this.header[2] = chunk[pos + 2];
|
|
135
|
+
this.header[3] = chunk[pos + 3];
|
|
136
|
+
pos += 4;
|
|
137
|
+
this.headerLen = 4;
|
|
138
|
+
this.packetLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
|
139
|
+
length = this.packetLen;
|
|
140
|
+
} else {
|
|
141
|
+
length = null;
|
|
142
|
+
while (chunkLen - pos > 0) {
|
|
143
|
+
this.header[this.headerLen++] = chunk[pos++];
|
|
144
|
+
if (this.headerLen === 4) {
|
|
145
|
+
this.packetLen = this.header[0] + (this.header[1] << 8) + (this.header[2] << 16);
|
|
146
|
+
length = this.packetLen;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (length) {
|
|
153
|
+
if (chunkLen - pos >= length) {
|
|
154
|
+
const buf = chunk.slice(pos, pos + length);
|
|
155
|
+
pos += length;
|
|
156
|
+
if (this.parts) {
|
|
157
|
+
this.parts.push(buf);
|
|
158
|
+
this.partsTotalLen += length;
|
|
159
|
+
|
|
160
|
+
if (this.packetLen < 0xffffff) {
|
|
161
|
+
let buf = Buffer.concat(this.parts, this.partsTotalLen);
|
|
162
|
+
this.parts = null;
|
|
163
|
+
const packet = new this.packetConstructor(buf, 0, this.partsTotalLen, this.encoding);
|
|
164
|
+
this.receivePacket(packet);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
if (this.packetLen < 0xffffff) {
|
|
168
|
+
const packet = new this.packetConstructor(buf, 0, length, this.encoding);
|
|
169
|
+
this.receivePacket(packet);
|
|
170
|
+
} else {
|
|
171
|
+
this.parts = [buf];
|
|
172
|
+
this.partsTotalLen = length;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
this.resetHeader();
|
|
176
|
+
} else {
|
|
177
|
+
const buf = chunk.slice(pos, chunkLen);
|
|
178
|
+
if (!this.parts) {
|
|
179
|
+
this.parts = [buf];
|
|
180
|
+
this.partsTotalLen = chunkLen - pos;
|
|
181
|
+
} else {
|
|
182
|
+
this.parts.push(buf);
|
|
183
|
+
this.partsTotalLen += chunkLen - pos;
|
|
184
|
+
}
|
|
185
|
+
this.remainingLen = length - (chunkLen - pos);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
} while (pos < chunkLen);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
module.exports = PacketInputStream;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Packet = require('./packet');
|
|
4
|
+
|
|
5
|
+
class PacketNodeEncoded extends Packet {
|
|
6
|
+
constructor(buf, pos, end, encoding) {
|
|
7
|
+
super(buf, pos, end);
|
|
8
|
+
this.encoding = encoding;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
readStringLength() {
|
|
12
|
+
const len = this.readUnsignedLength();
|
|
13
|
+
if (len === null) return null;
|
|
14
|
+
|
|
15
|
+
this.pos += len;
|
|
16
|
+
return this.buf.toString(this.encoding, this.pos - len, this.pos);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
readString(beg, len) {
|
|
20
|
+
return this.buf.toString(this.encoding, beg, beg + len);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
subPacketLengthEncoded() {
|
|
24
|
+
const len = this.readUnsignedLength();
|
|
25
|
+
this.skip(len);
|
|
26
|
+
return new PacketNodeEncoded(this.buf, this.pos - len, this.pos, this.encoding);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
readStringRemaining() {
|
|
30
|
+
const str = this.buf.toString(this.encoding, this.pos, this.end);
|
|
31
|
+
this.pos = this.end;
|
|
32
|
+
return str;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = PacketNodeEncoded;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Packet = require('./packet');
|
|
4
|
+
const Iconv = require('iconv-lite');
|
|
5
|
+
|
|
6
|
+
class PacketIconvEncoded extends Packet {
|
|
7
|
+
constructor(buf, pos, end, encoding) {
|
|
8
|
+
super(buf, pos, end);
|
|
9
|
+
this.encoding = encoding;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
readStringLength() {
|
|
13
|
+
const len = this.readUnsignedLength();
|
|
14
|
+
if (len === null) return null;
|
|
15
|
+
|
|
16
|
+
this.pos += len;
|
|
17
|
+
return Iconv.decode(this.buf.slice(this.pos - len, this.pos), this.encoding);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
readString(beg, len) {
|
|
21
|
+
return Iconv.decode(this.buf.slice(beg, beg + len), this.encoding);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
subPacketLengthEncoded() {
|
|
25
|
+
const len = this.readUnsignedLength();
|
|
26
|
+
this.skip(len);
|
|
27
|
+
return new PacketIconvEncoded(this.buf, this.pos - len, this.pos, this.encoding);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
readStringRemaining() {
|
|
31
|
+
const str = Iconv.decode(this.buf.slice(this.pos, this.end), this.encoding);
|
|
32
|
+
this.pos = this.end;
|
|
33
|
+
return str;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = PacketIconvEncoded;
|