Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Request.prototype.reply = function(documents, options) {
options = options || {};
documents = Array.isArray(documents) ? documents : [documents];
// Unpack any variables we need
var cursorId = options.cursorId || Long.ZERO;
var responseFlags = typeof options.responseFlags === 'number' ? options.responseFlags : 0;
var startingFrom = typeof options.startingFrom === 'number' ? options.startingFrom : 0;
var numberReturned = documents.length;
// Additional response Options
var killConnectionAfterNBytes =
typeof options.killConnectionAfterNBytes === 'number'
? options.killConnectionAfterNBytes
: null;
// Create the Response document
var response;
if (options.compression) {
response = new CompressedResponse(
this.bson,
{
// && Long.ZERO.equals(self.cursorState.cursorId) && !self.cmd.tailable)) {
// self.cursorState.dead = true;
// // Finished iterating over the cursor
// return setCursorDeadAndNotified(self, callback);
// }
//
// console.log("======================================== VALIDATE")
// Save the returned connection to ensure all getMore's fire over the same connection
self.connection = connection;
// Tailable cursor getMore result, notify owner about it
// No attempt is made here to retry, this is left to the user of the
// core module to handle to keep core simple
if(self.cursorState.documents.length == 0
&& self.cmd.tailable && Long.ZERO.equals(self.cursorState.cursorId)) {
// No more documents in the tailed cursor
return handleCallback(callback, MongoError.create({
message: "No more documents in tailed cursor"
, tailable: self.cmd.tailable
, awaitData: self.cmd.awaitData
}));
} else if(self.cursorState.documents.length == 0
&& self.cmd.tailable && !Long.ZERO.equals(self.cursorState.cursorId)) {
return nextFunction(self, callback);
}
if(self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
return setCursorDeadAndNotified(self, callback);
}
nextFunction(self, callback);
WireProtocol.prototype.killCursor = function(bson, ns, cursorId, connection, callbacks, callback) {
// Create a kill cursor command
var killCursor = new KillCursor(bson, [cursorId]);
// Execute the kill cursor command
if(connection && connection.isConnected()) connection.write(killCursor.toBin());
// Set cursor to 0
cursorId = Long.ZERO;
// Return to caller
if(callback) callback(null, null);
}
if(self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
return setCursorDeadAndNotified(self, callback);
}
nextFunction(self, callback);
});
} else if(self.cursorState.documents.length == self.cursorState.cursorIndex
&& self.cmd.tailable) {
return handleCallback(callback, MongoError.create({
message: "No more documents in tailed cursor"
, tailable: self.cmd.tailable
, awaitData: self.cmd.awaitData
}));
} else if(self.cursorState.documents.length == self.cursorState.cursorIndex
&& Long.ZERO.equals(self.cursorState.cursorId)) {
setCursorDeadAndNotified(self, callback);
} else {
if(self.cursorState.limit > 0 && self.cursorState.currentLimit >= self.cursorState.limit) {
// Ensure we kill the cursor on the server
self.kill();
// Set cursor in dead and notified state
return setCursorDeadAndNotified(self, callback);
}
// Increment the current cursor limit
self.cursorState.currentLimit += 1;
// Get the document
var doc = self.cursorState.documents[self.cursorState.cursorIndex++];
// Doc overflow
if(self.cursorState.cursorId == null) {
self.cursorState.cursorId = result.cursorId;
self.cursorState.lastCursorId = result.cursorId;
nextFunction(self, callback);
}
}
// If we have exhaust
if(self.cmd.exhaust && self.cursorState.cursorId == null) {
// Handle all the exhaust responses
self.callbacks.register(self.query.requestId, processExhaustMessages);
// Write the initial command out
return self.connection.write(self.query.toBin());
} else if(self.cmd.exhaust && self.cursorState.cursorIndex < self.cursorState.documents.length) {
return handleCallback(callback, null, self.cursorState.documents[self.cursorState.cursorIndex++]);
} else if(self.cmd.exhaust && Long.ZERO.equals(self.cursorState.cursorId)) {
self.callbacks.unregister(self.query.requestId);
return setCursorNotified(self, callback);
} else if(self.cmd.exhaust) {
return setTimeout(function() {
if(Long.ZERO.equals(self.cursorState.cursorId)) return;
nextFunction(self, callback);
}, 1);
}
// If we don't have a cursorId execute the first query
if(self.cursorState.cursorId == null) {
// Check if connection is dead and return if not possible to
// execute the query against the db
if(isConnectionDead(self, callback)) return;
// Check if topology is destroyed
// Promote id to long if needed
cursorState.cursorId = typeof id == 'number' ? Long.fromNumber(id) : id;
cursorState.lastCursorId = cursorState.cursorId;
// If we have a firstBatch set it
if(Array.isArray(result.documents[0].cursor.firstBatch)) {
cursorState.documents = result.documents[0].cursor.firstBatch;//.reverse();
}
// Return after processing command cursor
return callback(null, null);
}
if(Array.isArray(result.documents[0].result)) {
cursorState.documents = result.documents[0].result;
cursorState.cursorId = Long.ZERO;
return callback(null, null);
}
}
// Otherwise fall back to regular find path
cursorState.cursorId = result.cursorId;
cursorState.lastCursorId = result.cursorId;
cursorState.documents = result.documents;
// Transform the results with passed in transformation method if provided
if(cursorState.transforms && typeof cursorState.transforms.query == 'function') {
cursorState.documents = cursorState.transforms.query(result);
}
// Return callback
callback(null, null);
// Promote id to long if needed
cursorState.cursorId = typeof id == 'number' ? Long.fromNumber(id) : id;
cursorState.lastCursorId = cursorState.cursorId;
// If we have a firstBatch set it
if(Array.isArray(result.documents[0].cursor.firstBatch)) {
cursorState.documents = result.documents[0].cursor.firstBatch;//.reverse();
}
// Return after processing command cursor
return callback(null, null);
}
if(Array.isArray(result.documents[0].result)) {
cursorState.documents = result.documents[0].result;
cursorState.cursorId = Long.ZERO;
return callback(null, null);
}
}
// Otherwise fall back to regular find path
cursorState.cursorId = result.cursorId;
cursorState.lastCursorId = result.cursorId;
cursorState.documents = result.documents;
// Transform the results with passed in transformation method if provided
if(cursorState.transforms && typeof cursorState.transforms.query == 'function') {
cursorState.documents = cursorState.transforms.query(result);
}
// Return callback
callback(null, null);
self.ns = result.documents[0].cursor.ns;
}
// Promote id to long if needed
cursorState.cursorId = typeof id == 'number' ? Long.fromNumber(id) : id;
// If we have a firstBatch set it
if(Array.isArray(result.documents[0].cursor.firstBatch)) {
cursorState.documents = result.documents[0].cursor.firstBatch;//.reverse();
}
// Return after processing command cursor
return callback(null, null);
}
if(Array.isArray(result.documents[0].result)) {
cursorState.documents = result.documents[0].result;
cursorState.cursorId = Long.ZERO;
return callback(null, null);
}
}
// Otherwise fall back to regular find path
cursorState.cursorId = result.cursorId;
cursorState.documents = result.documents;
// Transform the results with passed in transformation method if provided
if(cursorState.transforms && typeof cursorState.transforms.query == 'function') {
cursorState.documents = cursorState.transforms.query(result);
}
// Return callback
callback(null, null);
}
return setTimeout(function() {
if(Long.ZERO.equals(self.cursorState.cursorId)) return;
nextFunction(self, callback);
}, 1);
}