@joystick.js/db-canary 0.0.0-canary.2251 → 0.0.0-canary.2252
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/dist/client/database.js +1 -1
- package/dist/client/index.js +1 -1
- package/dist/server/cluster/master.js +4 -4
- package/dist/server/cluster/worker.js +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/lib/auto_index_manager.js +1 -1
- package/dist/server/lib/backup_manager.js +1 -1
- package/dist/server/lib/index_manager.js +1 -1
- package/dist/server/lib/operation_dispatcher.js +1 -1
- package/dist/server/lib/operations/admin.js +1 -1
- package/dist/server/lib/operations/bulk_write.js +1 -1
- package/dist/server/lib/operations/create_index.js +1 -1
- package/dist/server/lib/operations/delete_many.js +1 -1
- package/dist/server/lib/operations/delete_one.js +1 -1
- package/dist/server/lib/operations/find.js +1 -1
- package/dist/server/lib/operations/find_one.js +1 -1
- package/dist/server/lib/operations/insert_one.js +1 -1
- package/dist/server/lib/operations/update_one.js +1 -1
- package/dist/server/lib/send_response.js +1 -1
- package/dist/server/lib/tcp_protocol.js +1 -1
- package/package.json +2 -2
- package/src/client/database.js +92 -119
- package/src/client/index.js +279 -345
- package/src/server/cluster/master.js +265 -156
- package/src/server/cluster/worker.js +26 -18
- package/src/server/index.js +553 -330
- package/src/server/lib/auto_index_manager.js +85 -23
- package/src/server/lib/backup_manager.js +117 -70
- package/src/server/lib/index_manager.js +63 -25
- package/src/server/lib/operation_dispatcher.js +339 -168
- package/src/server/lib/operations/admin.js +343 -205
- package/src/server/lib/operations/bulk_write.js +458 -194
- package/src/server/lib/operations/create_index.js +127 -34
- package/src/server/lib/operations/delete_many.js +204 -67
- package/src/server/lib/operations/delete_one.js +164 -52
- package/src/server/lib/operations/find.js +552 -319
- package/src/server/lib/operations/find_one.js +530 -304
- package/src/server/lib/operations/insert_one.js +147 -52
- package/src/server/lib/operations/update_one.js +334 -93
- package/src/server/lib/send_response.js +37 -17
- package/src/server/lib/tcp_protocol.js +158 -53
- package/test_data_api_key_1758233848259_cglfjzhou/data.mdb +0 -0
- package/test_data_api_key_1758233848259_cglfjzhou/lock.mdb +0 -0
- package/test_data_api_key_1758233848502_urlje2utd/data.mdb +0 -0
- package/test_data_api_key_1758233848502_urlje2utd/lock.mdb +0 -0
- package/test_data_api_key_1758233848738_mtcpfe5ns/data.mdb +0 -0
- package/test_data_api_key_1758233848738_mtcpfe5ns/lock.mdb +0 -0
- package/test_data_api_key_1758233848856_9g97p6gag/data.mdb +0 -0
- package/test_data_api_key_1758233848856_9g97p6gag/lock.mdb +0 -0
- package/test_data_api_key_1758233857008_0tl9zzhj8/data.mdb +0 -0
- package/test_data_api_key_1758233857008_0tl9zzhj8/lock.mdb +0 -0
- package/test_data_api_key_1758233857120_60c2f2uhu/data.mdb +0 -0
- package/test_data_api_key_1758233857120_60c2f2uhu/lock.mdb +0 -0
- package/test_data_api_key_1758233857232_aw7fkqgd9/data.mdb +0 -0
- package/test_data_api_key_1758233857232_aw7fkqgd9/lock.mdb +0 -0
- package/test_data_api_key_1758234881285_4aeflubjb/data.mdb +0 -0
- package/test_data_api_key_1758234881285_4aeflubjb/lock.mdb +0 -0
- package/test_data_api_key_1758234881520_kb0amvtqb/data.mdb +0 -0
- package/test_data_api_key_1758234881520_kb0amvtqb/lock.mdb +0 -0
- package/test_data_api_key_1758234881756_k04gfv2va/data.mdb +0 -0
- package/test_data_api_key_1758234881756_k04gfv2va/lock.mdb +0 -0
- package/test_data_api_key_1758234881876_wn90dpo1z/data.mdb +0 -0
- package/test_data_api_key_1758234881876_wn90dpo1z/lock.mdb +0 -0
- package/test_data_api_key_1758234889461_26xz3dmbr/data.mdb +0 -0
- package/test_data_api_key_1758234889461_26xz3dmbr/lock.mdb +0 -0
- package/test_data_api_key_1758234889572_uziz7e0p5/data.mdb +0 -0
- package/test_data_api_key_1758234889572_uziz7e0p5/lock.mdb +0 -0
- package/test_data_api_key_1758234889684_5f9wmposh/data.mdb +0 -0
- package/test_data_api_key_1758234889684_5f9wmposh/lock.mdb +0 -0
- package/test_data_api_key_1758235657729_prwgm6mxr/data.mdb +0 -0
- package/test_data_api_key_1758235657729_prwgm6mxr/lock.mdb +0 -0
- package/test_data_api_key_1758235657961_rc2da0dc2/data.mdb +0 -0
- package/test_data_api_key_1758235657961_rc2da0dc2/lock.mdb +0 -0
- package/test_data_api_key_1758235658193_oqqxm0sny/data.mdb +0 -0
- package/test_data_api_key_1758235658193_oqqxm0sny/lock.mdb +0 -0
- package/test_data_api_key_1758235658309_vggac1pj6/data.mdb +0 -0
- package/test_data_api_key_1758235658309_vggac1pj6/lock.mdb +0 -0
- package/test_data_api_key_1758235665968_61ko07dd1/data.mdb +0 -0
- package/test_data_api_key_1758235665968_61ko07dd1/lock.mdb +0 -0
- package/test_data_api_key_1758235666082_50lrt6sq8/data.mdb +0 -0
- package/test_data_api_key_1758235666082_50lrt6sq8/lock.mdb +0 -0
- package/test_data_api_key_1758235666194_ykvauwlzh/data.mdb +0 -0
- package/test_data_api_key_1758235666194_ykvauwlzh/lock.mdb +0 -0
- package/test_data_api_key_1758236187207_9c4paeh09/data.mdb +0 -0
- package/test_data_api_key_1758236187207_9c4paeh09/lock.mdb +0 -0
- package/test_data_api_key_1758236187441_4n3o3gkkl/data.mdb +0 -0
- package/test_data_api_key_1758236187441_4n3o3gkkl/lock.mdb +0 -0
- package/test_data_api_key_1758236187672_jt6b21ye0/data.mdb +0 -0
- package/test_data_api_key_1758236187672_jt6b21ye0/lock.mdb +0 -0
- package/test_data_api_key_1758236187788_oo84fz9u6/data.mdb +0 -0
- package/test_data_api_key_1758236187788_oo84fz9u6/lock.mdb +0 -0
- package/test_data_api_key_1758236195507_o9zeznwlm/data.mdb +0 -0
- package/test_data_api_key_1758236195507_o9zeznwlm/lock.mdb +0 -0
- package/test_data_api_key_1758236195619_qsqd60y41/data.mdb +0 -0
- package/test_data_api_key_1758236195619_qsqd60y41/lock.mdb +0 -0
- package/test_data_api_key_1758236195731_im13iq284/data.mdb +0 -0
- package/test_data_api_key_1758236195731_im13iq284/lock.mdb +0 -0
|
@@ -11,6 +11,49 @@ import create_logger from './logger.js';
|
|
|
11
11
|
|
|
12
12
|
const { create_context_logger } = create_logger('tcp_protocol');
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Creates MessagePack encoding options for consistent serialization.
|
|
16
|
+
* @returns {Object} MessagePack encoding options
|
|
17
|
+
*/
|
|
18
|
+
const create_messagepack_options = () => ({
|
|
19
|
+
useFloat32: false,
|
|
20
|
+
int64AsType: 'number',
|
|
21
|
+
mapsAsObjects: true
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a 4-byte length header buffer.
|
|
26
|
+
* @param {number} data_length - Length of data to encode
|
|
27
|
+
* @returns {Buffer} Length header buffer
|
|
28
|
+
*/
|
|
29
|
+
const create_length_header = (data_length) => {
|
|
30
|
+
const length_buffer = Buffer.allocUnsafe(4);
|
|
31
|
+
length_buffer.writeUInt32BE(data_length, 0);
|
|
32
|
+
return length_buffer;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Logs message encoding details.
|
|
37
|
+
* @param {Function} log - Logger function
|
|
38
|
+
* @param {number} payload_size - Size of MessagePack payload
|
|
39
|
+
* @param {number} total_size - Total message size with header
|
|
40
|
+
*/
|
|
41
|
+
const log_message_encoding = (log, payload_size, total_size) => {
|
|
42
|
+
log.debug('Message encoded', {
|
|
43
|
+
payload_size,
|
|
44
|
+
total_size
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Logs message encoding error.
|
|
50
|
+
* @param {Function} log - Logger function
|
|
51
|
+
* @param {Error} error - Encoding error
|
|
52
|
+
*/
|
|
53
|
+
const log_encoding_error = (log, error) => {
|
|
54
|
+
log.error('Failed to encode message', { error: error.message });
|
|
55
|
+
};
|
|
56
|
+
|
|
14
57
|
/**
|
|
15
58
|
* Encodes a message with MessagePack and prepends a 4-byte length header.
|
|
16
59
|
* @param {any} data - The data to encode
|
|
@@ -21,29 +64,120 @@ const encode_message = (data) => {
|
|
|
21
64
|
const log = create_context_logger();
|
|
22
65
|
|
|
23
66
|
try {
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
useFloat32: false,
|
|
27
|
-
int64AsType: 'number',
|
|
28
|
-
mapsAsObjects: true
|
|
29
|
-
});
|
|
30
|
-
const length_buffer = Buffer.allocUnsafe(4);
|
|
31
|
-
length_buffer.writeUInt32BE(messagepack_data.length, 0);
|
|
32
|
-
|
|
67
|
+
const messagepack_data = encode_messagepack(data, create_messagepack_options());
|
|
68
|
+
const length_buffer = create_length_header(messagepack_data.length);
|
|
33
69
|
const message = Buffer.concat([length_buffer, messagepack_data]);
|
|
34
70
|
|
|
35
|
-
log.
|
|
36
|
-
payload_size: messagepack_data.length,
|
|
37
|
-
total_size: message.length
|
|
38
|
-
});
|
|
71
|
+
log_message_encoding(log, messagepack_data.length, message.length);
|
|
39
72
|
|
|
40
73
|
return message;
|
|
41
74
|
} catch (error) {
|
|
42
|
-
log
|
|
75
|
+
log_encoding_error(log, error);
|
|
43
76
|
throw error;
|
|
44
77
|
}
|
|
45
78
|
};
|
|
46
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Reads length prefix from buffer.
|
|
82
|
+
* @param {Buffer} buffer - Current buffer
|
|
83
|
+
* @returns {Object} Result with expected length and remaining buffer
|
|
84
|
+
*/
|
|
85
|
+
const read_length_prefix = (buffer) => {
|
|
86
|
+
if (buffer.length < 4) {
|
|
87
|
+
return { expected_length: null, remaining_buffer: buffer };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const expected_length = buffer.readUInt32BE(0);
|
|
91
|
+
const remaining_buffer = buffer.slice(4);
|
|
92
|
+
|
|
93
|
+
return { expected_length, remaining_buffer };
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Logs length prefix reading.
|
|
98
|
+
* @param {Function} log - Logger function
|
|
99
|
+
* @param {number} expected_length - Expected message length
|
|
100
|
+
*/
|
|
101
|
+
const log_length_prefix_read = (log, expected_length) => {
|
|
102
|
+
log.debug('Length prefix read', { expected_length });
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Extracts message data from buffer.
|
|
107
|
+
* @param {Buffer} buffer - Current buffer
|
|
108
|
+
* @param {number} expected_length - Expected message length
|
|
109
|
+
* @returns {Object} Result with message data and remaining buffer
|
|
110
|
+
*/
|
|
111
|
+
const extract_message_data = (buffer, expected_length) => {
|
|
112
|
+
const message_data = buffer.slice(0, expected_length);
|
|
113
|
+
const remaining_buffer = buffer.slice(expected_length);
|
|
114
|
+
|
|
115
|
+
return { message_data, remaining_buffer };
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Decodes MessagePack message data.
|
|
120
|
+
* @param {Buffer} message_data - Raw message data
|
|
121
|
+
* @returns {Object} Decoded message
|
|
122
|
+
* @throws {Error} When decoding fails
|
|
123
|
+
*/
|
|
124
|
+
const decode_message_data = (message_data) => {
|
|
125
|
+
return decode_messagepack(message_data, create_messagepack_options());
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Logs successful message decoding.
|
|
130
|
+
* @param {Function} log - Logger function
|
|
131
|
+
* @param {number} message_size - Size of decoded message
|
|
132
|
+
*/
|
|
133
|
+
const log_message_decoding = (log, message_size) => {
|
|
134
|
+
log.debug('Message decoded', { message_size });
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Logs message decoding error.
|
|
139
|
+
* @param {Function} log - Logger function
|
|
140
|
+
* @param {Buffer} message_data - Raw message data that failed to decode
|
|
141
|
+
* @param {Error} error - Decoding error
|
|
142
|
+
*/
|
|
143
|
+
const log_decoding_error = (log, message_data, error) => {
|
|
144
|
+
log.error('Failed to decode message', {
|
|
145
|
+
message_size: message_data.length,
|
|
146
|
+
error: error.message,
|
|
147
|
+
hex_data: message_data.toString('hex')
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Processes complete message from buffer.
|
|
153
|
+
* @param {Buffer} buffer - Current buffer
|
|
154
|
+
* @param {number} expected_length - Expected message length
|
|
155
|
+
* @param {Function} log - Logger function
|
|
156
|
+
* @returns {Object} Result with decoded message and remaining buffer
|
|
157
|
+
* @throws {Error} When message decoding fails
|
|
158
|
+
*/
|
|
159
|
+
const process_complete_message = (buffer, expected_length, log) => {
|
|
160
|
+
const { message_data, remaining_buffer } = extract_message_data(buffer, expected_length);
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
const decoded_message = decode_message_data(message_data);
|
|
164
|
+
log_message_decoding(log, message_data.length);
|
|
165
|
+
|
|
166
|
+
return { decoded_message, remaining_buffer };
|
|
167
|
+
} catch (error) {
|
|
168
|
+
log_decoding_error(log, message_data, error);
|
|
169
|
+
throw new Error(`Invalid message format: ${error.message}`);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Logs parser reset operation.
|
|
175
|
+
* @param {Function} log - Logger function
|
|
176
|
+
*/
|
|
177
|
+
const log_parser_reset = (log) => {
|
|
178
|
+
log.debug('Parser reset');
|
|
179
|
+
};
|
|
180
|
+
|
|
47
181
|
/**
|
|
48
182
|
* Creates a message parser for handling TCP stream data with length-prefixed MessagePack messages.
|
|
49
183
|
* @returns {Object} Parser object with parse_messages and reset methods
|
|
@@ -53,69 +187,40 @@ const create_message_parser = () => {
|
|
|
53
187
|
let expected_length = null;
|
|
54
188
|
const log = create_context_logger();
|
|
55
189
|
|
|
56
|
-
/**
|
|
57
|
-
* Parses incoming data and extracts complete messages.
|
|
58
|
-
* @param {Buffer} data - Raw TCP data
|
|
59
|
-
* @returns {Array} Array of decoded messages
|
|
60
|
-
* @throws {Error} When message format is invalid
|
|
61
|
-
*/
|
|
62
190
|
const parse_messages = (data) => {
|
|
63
191
|
buffer = Buffer.concat([buffer, data]);
|
|
64
192
|
const messages = [];
|
|
65
193
|
|
|
66
194
|
while (buffer.length > 0) {
|
|
67
195
|
if (expected_length === null) {
|
|
68
|
-
|
|
196
|
+
const length_result = read_length_prefix(buffer);
|
|
197
|
+
expected_length = length_result.expected_length;
|
|
198
|
+
buffer = length_result.remaining_buffer;
|
|
199
|
+
|
|
200
|
+
if (expected_length === null) {
|
|
69
201
|
break;
|
|
70
202
|
}
|
|
71
203
|
|
|
72
|
-
expected_length
|
|
73
|
-
buffer = buffer.slice(4);
|
|
74
|
-
|
|
75
|
-
log.debug('Length prefix read', { expected_length });
|
|
204
|
+
log_length_prefix_read(log, expected_length);
|
|
76
205
|
}
|
|
77
206
|
|
|
78
207
|
if (buffer.length < expected_length) {
|
|
79
208
|
break;
|
|
80
209
|
}
|
|
81
210
|
|
|
82
|
-
const
|
|
83
|
-
|
|
211
|
+
const message_result = process_complete_message(buffer, expected_length, log);
|
|
212
|
+
messages.push(message_result.decoded_message);
|
|
213
|
+
buffer = message_result.remaining_buffer;
|
|
84
214
|
expected_length = null;
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
// NOTE: Use compatible MessagePack options to avoid parsing issues.
|
|
88
|
-
const decoded_message = decode_messagepack(message_data, {
|
|
89
|
-
useFloat32: false,
|
|
90
|
-
int64AsType: 'number',
|
|
91
|
-
mapsAsObjects: true
|
|
92
|
-
});
|
|
93
|
-
messages.push(decoded_message);
|
|
94
|
-
|
|
95
|
-
log.debug('Message decoded', {
|
|
96
|
-
message_size: message_data.length
|
|
97
|
-
});
|
|
98
|
-
} catch (error) {
|
|
99
|
-
log.error('Failed to decode message', {
|
|
100
|
-
message_size: message_data.length,
|
|
101
|
-
error: error.message,
|
|
102
|
-
hex_data: message_data.toString('hex')
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
throw new Error(`Invalid message format: ${error.message}`);
|
|
106
|
-
}
|
|
107
215
|
}
|
|
108
216
|
|
|
109
217
|
return messages;
|
|
110
218
|
};
|
|
111
219
|
|
|
112
|
-
/**
|
|
113
|
-
* Resets the parser state, clearing buffers and expected length.
|
|
114
|
-
*/
|
|
115
220
|
const reset = () => {
|
|
116
221
|
buffer = Buffer.alloc(0);
|
|
117
222
|
expected_length = null;
|
|
118
|
-
log
|
|
223
|
+
log_parser_reset(log);
|
|
119
224
|
};
|
|
120
225
|
|
|
121
226
|
return {
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|