Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
GridStoreStream.prototype.write = function(chunk, encoding, callback) {
var self = this;
if(self.endCalled) return self.emit('error', MongoError.create({message: 'attempting to write to stream after end called', driver:true}))
// Do we have to open the gridstore
if(!self.gs.isOpen) {
self.gs.open(function() {
self.gs.isOpen = true;
self.gs.write(chunk, function() {
process.nextTick(function() {
self.emit('drain');
});
});
});
return false;
} else {
self.gs.write(chunk, function() {
self.emit('drain');
});
return true;
Collection.prototype.insertMany = function(docs, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = options || {ordered:true};
if(!Array.isArray(docs) && typeof callback == 'function') {
return callback(MongoError.create({message: 'docs parameter must be an array of documents', driver:true }));
} else if(!Array.isArray(docs)) {
return new this.s.promiseLibrary(function(resolve, reject) {
reject(MongoError.create({message: 'docs parameter must be an array of documents', driver:true }));
});
}
// Get the write concern options
if(typeof options.checkKeys != 'boolean') {
options.checkKeys = true;
}
// If keep going set unordered
options['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
// Set up the force server object id
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
finalOptions = _setWriteConcernHash(options);
} else if(options.safe != null && typeof options.safe == 'object') {
finalOptions = _setWriteConcernHash(options.safe);
} else if(typeof options.safe == "boolean") {
finalOptions = {w: (options.safe ? 1 : 0)};
} else if(self.options.w != null || typeof self.options.j == 'boolean' || typeof self.options.journal == 'boolean' || typeof self.options.fsync == 'boolean') {
finalOptions = _setWriteConcernHash(self.options);
} else if(self.safe && (self.safe.w != null || typeof self.safe.j == 'boolean' || typeof self.safe.journal == 'boolean' || typeof self.safe.fsync == 'boolean')) {
finalOptions = _setWriteConcernHash(self.safe);
} else if(typeof self.safe == "boolean") {
finalOptions = {w: (self.safe ? 1 : 0)};
}
// Ensure we don't have an invalid combination of write concerns
if(finalOptions.w < 1
&& (finalOptions.journal == true || finalOptions.j == true || finalOptions.fsync == true)) throw MongoError.create({message: "No acknowledgement using w < 1 cannot be combined with journal:true or fsync:true", driver:true});
// Return the options
return finalOptions;
}
var nextObject = function(self, callback) {
if(self.s.state == Cursor.CLOSED || self.isDead && self.isDead()) return handleCallback(callback, MongoError.create({message: "Cursor is closed", driver:true}));
if(self.s.state == Cursor.INIT && self.s.cmd.sort) {
try {
self.s.cmd.sort = formattedOrderClause(self.s.cmd.sort);
} catch(err) {
return handleCallback(callback, err);
}
}
// Get the next object
self._next(function(err, doc) {
self.s.state = Cursor.OPEN;
if(err) return handleCallback(callback, err);
handleCallback(callback, null, doc);
});
}
self.contentType = doc.contentType;
self.internalChunkSize = doc.chunkSize;
self.uploadDate = doc.uploadDate;
self.aliases = doc.aliases;
self.length = doc.length;
self.metadata = doc.metadata;
self.internalMd5 = doc.md5;
} else if (self.mode != 'r') {
self.fileId = self.fileId == null ? new ObjectID() : self.fileId;
self.contentType = GridStore.DEFAULT_CONTENT_TYPE;
self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
self.length = 0;
} else {
self.length = 0;
var txtId = self.fileId instanceof ObjectID ? self.fileId.toHexString() : self.fileId;
return error(MongoError.create({message: f("file with id %s not opened for writing", (self.referenceBy == REFERENCE_BY_ID ? txtId : self.filename)), driver:true}), self);
}
// Process the mode of the object
if(self.mode == "r") {
nthChunk(self, 0, options, function(err, chunk) {
if(err) return error(err);
self.currentChunk = chunk;
self.position = 0;
callback(null, self);
});
} else if(self.mode == "w") {
// Delete any existing chunks
deleteChunks(self, options, function(err, result) {
if(err) return error(err);
self.currentChunk = new Chunk(self, {'n':0}, self.writeConcern);
self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type'];
var seek = function(self, position, seekLocation, callback) {
// Seek only supports read mode
if(self.mode != 'r') {
return callback(MongoError.create({message: "seek is only supported for mode r", driver:true}))
}
var seekLocationFinal = seekLocation == null ? GridStore.IO_SEEK_SET : seekLocation;
var finalPosition = position;
var targetPosition = 0;
// Calculate the position
if(seekLocationFinal == GridStore.IO_SEEK_CUR) {
targetPosition = self.position + finalPosition;
} else if(seekLocationFinal == GridStore.IO_SEEK_END) {
targetPosition = self.length + finalPosition;
} else {
targetPosition = finalPosition;
}
// Get the chunk
self.s.topology.insert(f("%s.%s", self.s.databaseName, Db.SYSTEM_INDEX_COLLECTION), doc, finalOptions, function(err, result) {
if(callback == null) return;
if(err) return handleCallback(callback, err);
if(result == null) return handleCallback(callback, null, null);
if(result.result.writeErrors) return handleCallback(callback, MongoError.create(result.result.writeErrors[0]), null);
handleCallback(callback, null, doc.name);
});
});
self.s.db.listCollections({name: self.s.name}).toArray(function(err, collections) {
if(err) return handleCallback(callback, err);
if(collections.length == 0) {
return handleCallback(callback, MongoError.create({message: f("collection %s not found", self.s.namespace), driver:true }));
}
handleCallback(callback, err, collections[0].options || null);
});
}
CommandCursor.prototype.batchSize = function(value) {
if(this.s.state == CommandCursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true});
if(typeof value != 'number') throw MongoError.create({message: "batchSize requires an integer", driver:true});
if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
this.setCursorBatchSize(value);
return this;
}