@push.rocks/smartmongo 2.0.12 → 2.1.0
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_ts/00_commitinfo_data.js +2 -2
- package/dist_ts/congodb/congodb.plugins.d.ts +10 -0
- package/dist_ts/congodb/congodb.plugins.js +14 -0
- package/dist_ts/congodb/engine/AggregationEngine.d.ts +66 -0
- package/dist_ts/congodb/engine/AggregationEngine.js +189 -0
- package/dist_ts/congodb/engine/IndexEngine.d.ts +77 -0
- package/dist_ts/congodb/engine/IndexEngine.js +376 -0
- package/dist_ts/congodb/engine/QueryEngine.d.ts +54 -0
- package/dist_ts/congodb/engine/QueryEngine.js +271 -0
- package/dist_ts/congodb/engine/TransactionEngine.d.ts +85 -0
- package/dist_ts/congodb/engine/TransactionEngine.js +287 -0
- package/dist_ts/congodb/engine/UpdateEngine.d.ts +47 -0
- package/dist_ts/congodb/engine/UpdateEngine.js +461 -0
- package/dist_ts/congodb/errors/CongoErrors.d.ts +100 -0
- package/dist_ts/congodb/errors/CongoErrors.js +155 -0
- package/dist_ts/congodb/index.d.ts +19 -0
- package/dist_ts/congodb/index.js +26 -0
- package/dist_ts/congodb/server/CommandRouter.d.ts +51 -0
- package/dist_ts/congodb/server/CommandRouter.js +132 -0
- package/dist_ts/congodb/server/CongoServer.d.ts +95 -0
- package/dist_ts/congodb/server/CongoServer.js +227 -0
- package/dist_ts/congodb/server/WireProtocol.d.ts +117 -0
- package/dist_ts/congodb/server/WireProtocol.js +298 -0
- package/dist_ts/congodb/server/handlers/AdminHandler.d.ts +100 -0
- package/dist_ts/congodb/server/handlers/AdminHandler.js +568 -0
- package/dist_ts/congodb/server/handlers/AggregateHandler.d.ts +31 -0
- package/dist_ts/congodb/server/handlers/AggregateHandler.js +277 -0
- package/dist_ts/congodb/server/handlers/DeleteHandler.d.ts +8 -0
- package/dist_ts/congodb/server/handlers/DeleteHandler.js +83 -0
- package/dist_ts/congodb/server/handlers/FindHandler.d.ts +31 -0
- package/dist_ts/congodb/server/handlers/FindHandler.js +261 -0
- package/dist_ts/congodb/server/handlers/HelloHandler.d.ts +11 -0
- package/dist_ts/congodb/server/handlers/HelloHandler.js +62 -0
- package/dist_ts/congodb/server/handlers/IndexHandler.d.ts +20 -0
- package/dist_ts/congodb/server/handlers/IndexHandler.js +183 -0
- package/dist_ts/congodb/server/handlers/InsertHandler.d.ts +8 -0
- package/dist_ts/congodb/server/handlers/InsertHandler.js +76 -0
- package/dist_ts/congodb/server/handlers/UpdateHandler.d.ts +24 -0
- package/dist_ts/congodb/server/handlers/UpdateHandler.js +270 -0
- package/dist_ts/congodb/server/handlers/index.d.ts +8 -0
- package/dist_ts/congodb/server/handlers/index.js +10 -0
- package/dist_ts/congodb/server/index.d.ts +6 -0
- package/dist_ts/congodb/server/index.js +7 -0
- package/dist_ts/congodb/storage/FileStorageAdapter.d.ts +61 -0
- package/dist_ts/congodb/storage/FileStorageAdapter.js +396 -0
- package/dist_ts/congodb/storage/IStorageAdapter.d.ts +140 -0
- package/dist_ts/congodb/storage/IStorageAdapter.js +2 -0
- package/dist_ts/congodb/storage/MemoryStorageAdapter.d.ts +66 -0
- package/dist_ts/congodb/storage/MemoryStorageAdapter.js +367 -0
- package/dist_ts/congodb/storage/OpLog.d.ts +93 -0
- package/dist_ts/congodb/storage/OpLog.js +221 -0
- package/dist_ts/congodb/types/interfaces.d.ts +363 -0
- package/dist_ts/congodb/types/interfaces.js +2 -0
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +8 -6
- package/dist_ts/smartmongo.plugins.d.ts +1 -1
- package/dist_ts/smartmongo.plugins.js +2 -2
- package/npmextra.json +17 -7
- package/package.json +20 -12
- package/readme.hints.md +89 -1
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/congodb/congodb.plugins.ts +17 -0
- package/ts/congodb/engine/AggregationEngine.ts +283 -0
- package/ts/congodb/engine/IndexEngine.ts +479 -0
- package/ts/congodb/engine/QueryEngine.ts +301 -0
- package/ts/congodb/engine/TransactionEngine.ts +351 -0
- package/ts/congodb/engine/UpdateEngine.ts +506 -0
- package/ts/congodb/errors/CongoErrors.ts +181 -0
- package/ts/congodb/index.ts +37 -0
- package/ts/congodb/server/CommandRouter.ts +180 -0
- package/ts/congodb/server/CongoServer.ts +298 -0
- package/ts/congodb/server/WireProtocol.ts +416 -0
- package/ts/congodb/server/handlers/AdminHandler.ts +614 -0
- package/ts/congodb/server/handlers/AggregateHandler.ts +342 -0
- package/ts/congodb/server/handlers/DeleteHandler.ts +100 -0
- package/ts/congodb/server/handlers/FindHandler.ts +301 -0
- package/ts/congodb/server/handlers/HelloHandler.ts +78 -0
- package/ts/congodb/server/handlers/IndexHandler.ts +207 -0
- package/ts/congodb/server/handlers/InsertHandler.ts +91 -0
- package/ts/congodb/server/handlers/UpdateHandler.ts +315 -0
- package/ts/congodb/server/handlers/index.ts +10 -0
- package/ts/congodb/server/index.ts +10 -0
- package/ts/congodb/storage/FileStorageAdapter.ts +479 -0
- package/ts/congodb/storage/IStorageAdapter.ts +202 -0
- package/ts/congodb/storage/MemoryStorageAdapter.ts +443 -0
- package/ts/congodb/storage/OpLog.ts +282 -0
- package/ts/congodb/types/interfaces.ts +433 -0
- package/ts/index.ts +3 -0
- package/ts/smartmongo.plugins.ts +1 -1
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import * as plugins from '../congodb.plugins.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MongoDB Wire Protocol Implementation
|
|
5
|
+
* Handles parsing and encoding of MongoDB wire protocol messages (OP_MSG primarily)
|
|
6
|
+
*
|
|
7
|
+
* Wire Protocol Message Format:
|
|
8
|
+
* - Header (16 bytes): messageLength (4), requestID (4), responseTo (4), opCode (4)
|
|
9
|
+
* - OP_MSG: flagBits (4), sections[], optional checksum (4)
|
|
10
|
+
*
|
|
11
|
+
* References:
|
|
12
|
+
* - https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// OpCodes
|
|
16
|
+
export const OP_REPLY = 1; // Legacy reply
|
|
17
|
+
export const OP_UPDATE = 2001; // Legacy update
|
|
18
|
+
export const OP_INSERT = 2002; // Legacy insert
|
|
19
|
+
export const OP_QUERY = 2004; // Legacy query (still used for initial handshake)
|
|
20
|
+
export const OP_GET_MORE = 2005; // Legacy getMore
|
|
21
|
+
export const OP_DELETE = 2006; // Legacy delete
|
|
22
|
+
export const OP_KILL_CURSORS = 2007; // Legacy kill cursors
|
|
23
|
+
export const OP_COMPRESSED = 2012; // Compressed message
|
|
24
|
+
export const OP_MSG = 2013; // Modern protocol (MongoDB 3.6+)
|
|
25
|
+
|
|
26
|
+
// OP_MSG Section Types
|
|
27
|
+
export const SECTION_BODY = 0; // Single BSON document
|
|
28
|
+
export const SECTION_DOCUMENT_SEQUENCE = 1; // Document sequence for bulk operations
|
|
29
|
+
|
|
30
|
+
// OP_MSG Flag Bits
|
|
31
|
+
export const MSG_FLAG_CHECKSUM_PRESENT = 1 << 0;
|
|
32
|
+
export const MSG_FLAG_MORE_TO_COME = 1 << 1;
|
|
33
|
+
export const MSG_FLAG_EXHAUST_ALLOWED = 1 << 16;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parsed message header
|
|
37
|
+
*/
|
|
38
|
+
export interface IMessageHeader {
|
|
39
|
+
messageLength: number;
|
|
40
|
+
requestID: number;
|
|
41
|
+
responseTo: number;
|
|
42
|
+
opCode: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parsed OP_MSG message
|
|
47
|
+
*/
|
|
48
|
+
export interface IOpMsgMessage {
|
|
49
|
+
header: IMessageHeader;
|
|
50
|
+
flagBits: number;
|
|
51
|
+
sections: IOpMsgSection[];
|
|
52
|
+
checksum?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* OP_MSG section (either body or document sequence)
|
|
57
|
+
*/
|
|
58
|
+
export interface IOpMsgSection {
|
|
59
|
+
type: number;
|
|
60
|
+
payload: plugins.bson.Document;
|
|
61
|
+
sequenceIdentifier?: string;
|
|
62
|
+
documents?: plugins.bson.Document[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Parsed OP_QUERY message (legacy, but used for initial handshake)
|
|
67
|
+
*/
|
|
68
|
+
export interface IOpQueryMessage {
|
|
69
|
+
header: IMessageHeader;
|
|
70
|
+
flags: number;
|
|
71
|
+
fullCollectionName: string;
|
|
72
|
+
numberToSkip: number;
|
|
73
|
+
numberToReturn: number;
|
|
74
|
+
query: plugins.bson.Document;
|
|
75
|
+
returnFieldsSelector?: plugins.bson.Document;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Parsed command from any message type
|
|
80
|
+
*/
|
|
81
|
+
export interface IParsedCommand {
|
|
82
|
+
commandName: string;
|
|
83
|
+
command: plugins.bson.Document;
|
|
84
|
+
database: string;
|
|
85
|
+
requestID: number;
|
|
86
|
+
opCode: number;
|
|
87
|
+
documentSequences?: Map<string, plugins.bson.Document[]>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Wire Protocol parser and encoder
|
|
92
|
+
*/
|
|
93
|
+
export class WireProtocol {
|
|
94
|
+
/**
|
|
95
|
+
* Parse a complete message from a buffer
|
|
96
|
+
* Returns the parsed command and the number of bytes consumed
|
|
97
|
+
*/
|
|
98
|
+
static parseMessage(buffer: Buffer): { command: IParsedCommand; bytesConsumed: number } | null {
|
|
99
|
+
if (buffer.length < 16) {
|
|
100
|
+
return null; // Not enough data for header
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const header = this.parseHeader(buffer);
|
|
104
|
+
|
|
105
|
+
if (buffer.length < header.messageLength) {
|
|
106
|
+
return null; // Not enough data for complete message
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const messageBuffer = buffer.subarray(0, header.messageLength);
|
|
110
|
+
|
|
111
|
+
switch (header.opCode) {
|
|
112
|
+
case OP_MSG:
|
|
113
|
+
return this.parseOpMsg(messageBuffer, header);
|
|
114
|
+
case OP_QUERY:
|
|
115
|
+
return this.parseOpQuery(messageBuffer, header);
|
|
116
|
+
default:
|
|
117
|
+
throw new Error(`Unsupported opCode: ${header.opCode}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Parse message header (16 bytes)
|
|
123
|
+
*/
|
|
124
|
+
private static parseHeader(buffer: Buffer): IMessageHeader {
|
|
125
|
+
return {
|
|
126
|
+
messageLength: buffer.readInt32LE(0),
|
|
127
|
+
requestID: buffer.readInt32LE(4),
|
|
128
|
+
responseTo: buffer.readInt32LE(8),
|
|
129
|
+
opCode: buffer.readInt32LE(12),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Parse OP_MSG message
|
|
135
|
+
*/
|
|
136
|
+
private static parseOpMsg(buffer: Buffer, header: IMessageHeader): { command: IParsedCommand; bytesConsumed: number } {
|
|
137
|
+
let offset = 16; // Skip header
|
|
138
|
+
|
|
139
|
+
const flagBits = buffer.readUInt32LE(offset);
|
|
140
|
+
offset += 4;
|
|
141
|
+
|
|
142
|
+
const sections: IOpMsgSection[] = [];
|
|
143
|
+
const documentSequences = new Map<string, plugins.bson.Document[]>();
|
|
144
|
+
|
|
145
|
+
// Parse sections until we reach the end (or checksum)
|
|
146
|
+
const messageEnd = (flagBits & MSG_FLAG_CHECKSUM_PRESENT)
|
|
147
|
+
? header.messageLength - 4
|
|
148
|
+
: header.messageLength;
|
|
149
|
+
|
|
150
|
+
while (offset < messageEnd) {
|
|
151
|
+
const sectionType = buffer.readUInt8(offset);
|
|
152
|
+
offset += 1;
|
|
153
|
+
|
|
154
|
+
if (sectionType === SECTION_BODY) {
|
|
155
|
+
// Single BSON document
|
|
156
|
+
const docSize = buffer.readInt32LE(offset);
|
|
157
|
+
const docBuffer = buffer.subarray(offset, offset + docSize);
|
|
158
|
+
const doc = plugins.bson.deserialize(docBuffer);
|
|
159
|
+
sections.push({ type: SECTION_BODY, payload: doc });
|
|
160
|
+
offset += docSize;
|
|
161
|
+
} else if (sectionType === SECTION_DOCUMENT_SEQUENCE) {
|
|
162
|
+
// Document sequence
|
|
163
|
+
const sectionSize = buffer.readInt32LE(offset);
|
|
164
|
+
const sectionEnd = offset + sectionSize;
|
|
165
|
+
offset += 4;
|
|
166
|
+
|
|
167
|
+
// Read sequence identifier (C string)
|
|
168
|
+
let identifierEnd = offset;
|
|
169
|
+
while (buffer[identifierEnd] !== 0 && identifierEnd < sectionEnd) {
|
|
170
|
+
identifierEnd++;
|
|
171
|
+
}
|
|
172
|
+
const identifier = buffer.subarray(offset, identifierEnd).toString('utf8');
|
|
173
|
+
offset = identifierEnd + 1; // Skip null terminator
|
|
174
|
+
|
|
175
|
+
// Read documents
|
|
176
|
+
const documents: plugins.bson.Document[] = [];
|
|
177
|
+
while (offset < sectionEnd) {
|
|
178
|
+
const docSize = buffer.readInt32LE(offset);
|
|
179
|
+
const docBuffer = buffer.subarray(offset, offset + docSize);
|
|
180
|
+
documents.push(plugins.bson.deserialize(docBuffer));
|
|
181
|
+
offset += docSize;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
sections.push({
|
|
185
|
+
type: SECTION_DOCUMENT_SEQUENCE,
|
|
186
|
+
payload: {},
|
|
187
|
+
sequenceIdentifier: identifier,
|
|
188
|
+
documents
|
|
189
|
+
});
|
|
190
|
+
documentSequences.set(identifier, documents);
|
|
191
|
+
} else {
|
|
192
|
+
throw new Error(`Unknown section type: ${sectionType}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// The first section body contains the command
|
|
197
|
+
const commandSection = sections.find(s => s.type === SECTION_BODY);
|
|
198
|
+
if (!commandSection) {
|
|
199
|
+
throw new Error('OP_MSG missing command body section');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const command = commandSection.payload;
|
|
203
|
+
const commandName = Object.keys(command)[0];
|
|
204
|
+
const database = command.$db || 'admin';
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
command: {
|
|
208
|
+
commandName,
|
|
209
|
+
command,
|
|
210
|
+
database,
|
|
211
|
+
requestID: header.requestID,
|
|
212
|
+
opCode: header.opCode,
|
|
213
|
+
documentSequences: documentSequences.size > 0 ? documentSequences : undefined,
|
|
214
|
+
},
|
|
215
|
+
bytesConsumed: header.messageLength,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Parse OP_QUERY message (legacy, used for initial handshake)
|
|
221
|
+
*/
|
|
222
|
+
private static parseOpQuery(buffer: Buffer, header: IMessageHeader): { command: IParsedCommand; bytesConsumed: number } {
|
|
223
|
+
let offset = 16; // Skip header
|
|
224
|
+
|
|
225
|
+
const flags = buffer.readInt32LE(offset);
|
|
226
|
+
offset += 4;
|
|
227
|
+
|
|
228
|
+
// Read full collection name (C string)
|
|
229
|
+
let nameEnd = offset;
|
|
230
|
+
while (buffer[nameEnd] !== 0 && nameEnd < buffer.length) {
|
|
231
|
+
nameEnd++;
|
|
232
|
+
}
|
|
233
|
+
const fullCollectionName = buffer.subarray(offset, nameEnd).toString('utf8');
|
|
234
|
+
offset = nameEnd + 1;
|
|
235
|
+
|
|
236
|
+
const numberToSkip = buffer.readInt32LE(offset);
|
|
237
|
+
offset += 4;
|
|
238
|
+
|
|
239
|
+
const numberToReturn = buffer.readInt32LE(offset);
|
|
240
|
+
offset += 4;
|
|
241
|
+
|
|
242
|
+
// Read query document
|
|
243
|
+
const querySize = buffer.readInt32LE(offset);
|
|
244
|
+
const queryBuffer = buffer.subarray(offset, offset + querySize);
|
|
245
|
+
const query = plugins.bson.deserialize(queryBuffer);
|
|
246
|
+
offset += querySize;
|
|
247
|
+
|
|
248
|
+
// Extract database from collection name (format: "dbname.$cmd" or "dbname.collection")
|
|
249
|
+
const parts = fullCollectionName.split('.');
|
|
250
|
+
const database = parts[0];
|
|
251
|
+
|
|
252
|
+
// For OP_QUERY to .$cmd, the query IS the command
|
|
253
|
+
let commandName = 'find';
|
|
254
|
+
let command = query;
|
|
255
|
+
|
|
256
|
+
if (parts[1] === '$cmd') {
|
|
257
|
+
// This is a command
|
|
258
|
+
commandName = Object.keys(query)[0];
|
|
259
|
+
// Handle special commands like isMaster, hello
|
|
260
|
+
if (commandName === 'isMaster' || commandName === 'ismaster') {
|
|
261
|
+
commandName = 'hello';
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
command: {
|
|
267
|
+
commandName,
|
|
268
|
+
command,
|
|
269
|
+
database,
|
|
270
|
+
requestID: header.requestID,
|
|
271
|
+
opCode: header.opCode,
|
|
272
|
+
},
|
|
273
|
+
bytesConsumed: header.messageLength,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Encode a response as OP_MSG
|
|
279
|
+
*/
|
|
280
|
+
static encodeOpMsgResponse(
|
|
281
|
+
responseTo: number,
|
|
282
|
+
response: plugins.bson.Document,
|
|
283
|
+
requestID: number = Math.floor(Math.random() * 0x7FFFFFFF)
|
|
284
|
+
): Buffer {
|
|
285
|
+
// Add $db if not present (optional in response)
|
|
286
|
+
const responseDoc = { ...response };
|
|
287
|
+
|
|
288
|
+
// Serialize the response document
|
|
289
|
+
const bodyBson = plugins.bson.serialize(responseDoc);
|
|
290
|
+
|
|
291
|
+
// Calculate message length
|
|
292
|
+
// Header (16) + flagBits (4) + section type (1) + body BSON
|
|
293
|
+
const messageLength = 16 + 4 + 1 + bodyBson.length;
|
|
294
|
+
|
|
295
|
+
const buffer = Buffer.alloc(messageLength);
|
|
296
|
+
let offset = 0;
|
|
297
|
+
|
|
298
|
+
// Write header
|
|
299
|
+
buffer.writeInt32LE(messageLength, offset); // messageLength
|
|
300
|
+
offset += 4;
|
|
301
|
+
buffer.writeInt32LE(requestID, offset); // requestID
|
|
302
|
+
offset += 4;
|
|
303
|
+
buffer.writeInt32LE(responseTo, offset); // responseTo
|
|
304
|
+
offset += 4;
|
|
305
|
+
buffer.writeInt32LE(OP_MSG, offset); // opCode
|
|
306
|
+
offset += 4;
|
|
307
|
+
|
|
308
|
+
// Write flagBits (0 = no flags)
|
|
309
|
+
buffer.writeUInt32LE(0, offset);
|
|
310
|
+
offset += 4;
|
|
311
|
+
|
|
312
|
+
// Write section type 0 (body)
|
|
313
|
+
buffer.writeUInt8(SECTION_BODY, offset);
|
|
314
|
+
offset += 1;
|
|
315
|
+
|
|
316
|
+
// Write body BSON
|
|
317
|
+
Buffer.from(bodyBson).copy(buffer, offset);
|
|
318
|
+
|
|
319
|
+
return buffer;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Encode a response as OP_REPLY (legacy, for OP_QUERY responses)
|
|
324
|
+
*/
|
|
325
|
+
static encodeOpReplyResponse(
|
|
326
|
+
responseTo: number,
|
|
327
|
+
documents: plugins.bson.Document[],
|
|
328
|
+
requestID: number = Math.floor(Math.random() * 0x7FFFFFFF),
|
|
329
|
+
cursorId: bigint = BigInt(0)
|
|
330
|
+
): Buffer {
|
|
331
|
+
// Serialize all documents
|
|
332
|
+
const docBuffers = documents.map(doc => plugins.bson.serialize(doc));
|
|
333
|
+
const totalDocsSize = docBuffers.reduce((sum, buf) => sum + buf.length, 0);
|
|
334
|
+
|
|
335
|
+
// Message format:
|
|
336
|
+
// Header (16) + responseFlags (4) + cursorID (8) + startingFrom (4) + numberReturned (4) + documents
|
|
337
|
+
const messageLength = 16 + 4 + 8 + 4 + 4 + totalDocsSize;
|
|
338
|
+
|
|
339
|
+
const buffer = Buffer.alloc(messageLength);
|
|
340
|
+
let offset = 0;
|
|
341
|
+
|
|
342
|
+
// Write header
|
|
343
|
+
buffer.writeInt32LE(messageLength, offset); // messageLength
|
|
344
|
+
offset += 4;
|
|
345
|
+
buffer.writeInt32LE(requestID, offset); // requestID
|
|
346
|
+
offset += 4;
|
|
347
|
+
buffer.writeInt32LE(responseTo, offset); // responseTo
|
|
348
|
+
offset += 4;
|
|
349
|
+
buffer.writeInt32LE(OP_REPLY, offset); // opCode
|
|
350
|
+
offset += 4;
|
|
351
|
+
|
|
352
|
+
// Write OP_REPLY fields
|
|
353
|
+
buffer.writeInt32LE(0, offset); // responseFlags (0 = no errors)
|
|
354
|
+
offset += 4;
|
|
355
|
+
buffer.writeBigInt64LE(cursorId, offset); // cursorID
|
|
356
|
+
offset += 8;
|
|
357
|
+
buffer.writeInt32LE(0, offset); // startingFrom
|
|
358
|
+
offset += 4;
|
|
359
|
+
buffer.writeInt32LE(documents.length, offset); // numberReturned
|
|
360
|
+
offset += 4;
|
|
361
|
+
|
|
362
|
+
// Write documents
|
|
363
|
+
for (const docBuffer of docBuffers) {
|
|
364
|
+
Buffer.from(docBuffer).copy(buffer, offset);
|
|
365
|
+
offset += docBuffer.length;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return buffer;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Encode an error response
|
|
373
|
+
*/
|
|
374
|
+
static encodeErrorResponse(
|
|
375
|
+
responseTo: number,
|
|
376
|
+
errorCode: number,
|
|
377
|
+
errorMessage: string,
|
|
378
|
+
commandName?: string
|
|
379
|
+
): Buffer {
|
|
380
|
+
const response: plugins.bson.Document = {
|
|
381
|
+
ok: 0,
|
|
382
|
+
errmsg: errorMessage,
|
|
383
|
+
code: errorCode,
|
|
384
|
+
codeName: this.getErrorCodeName(errorCode),
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
return this.encodeOpMsgResponse(responseTo, response);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Get error code name from error code
|
|
392
|
+
*/
|
|
393
|
+
private static getErrorCodeName(code: number): string {
|
|
394
|
+
const errorNames: Record<number, string> = {
|
|
395
|
+
0: 'OK',
|
|
396
|
+
1: 'InternalError',
|
|
397
|
+
2: 'BadValue',
|
|
398
|
+
11000: 'DuplicateKey',
|
|
399
|
+
11001: 'DuplicateKeyValue',
|
|
400
|
+
13: 'Unauthorized',
|
|
401
|
+
26: 'NamespaceNotFound',
|
|
402
|
+
27: 'IndexNotFound',
|
|
403
|
+
48: 'NamespaceExists',
|
|
404
|
+
59: 'CommandNotFound',
|
|
405
|
+
66: 'ImmutableField',
|
|
406
|
+
73: 'InvalidNamespace',
|
|
407
|
+
85: 'IndexOptionsConflict',
|
|
408
|
+
112: 'WriteConflict',
|
|
409
|
+
121: 'DocumentValidationFailure',
|
|
410
|
+
211: 'KeyNotFound',
|
|
411
|
+
251: 'NoSuchTransaction',
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
return errorNames[code] || 'UnknownError';
|
|
415
|
+
}
|
|
416
|
+
}
|