grid_store.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. "use strict";
  2. /**
  3. * @fileOverview GridFS is a tool for MongoDB to store files to the database.
  4. * Because of the restrictions of the object size the database can hold, a
  5. * facility to split a file into several chunks is needed. The {@link GridStore}
  6. * class offers a simplified api to interact with files while managing the
  7. * chunks of split files behind the scenes. More information about GridFS can be
  8. * found <a href="http://www.mongodb.org/display/DOCS/GridFS">here</a>.
  9. *
  10. * @example
  11. * var MongoClient = require('mongodb').MongoClient,
  12. * GridStore = require('mongodb').GridStore,
  13. * ObjectID = require('mongodb').ObjectID,
  14. * test = require('assert');
  15. *
  16. * // Connection url
  17. * var url = 'mongodb://localhost:27017/test';
  18. * // Connect using MongoClient
  19. * MongoClient.connect(url, function(err, db) {
  20. * var gridStore = new GridStore(db, null, "w");
  21. * gridStore.open(function(err, gridStore) {
  22. * gridStore.write("hello world!", function(err, gridStore) {
  23. * gridStore.close(function(err, result) {
  24. *
  25. * // Let's read the file using object Id
  26. * GridStore.read(db, result._id, function(err, data) {
  27. * test.equal('hello world!', data);
  28. * db.close();
  29. * test.done();
  30. * });
  31. * });
  32. * });
  33. * });
  34. * });
  35. */
  36. var Chunk = require('./chunk'),
  37. ObjectID = require('mongodb-core').BSON.ObjectID,
  38. ReadPreference = require('../read_preference'),
  39. Buffer = require('buffer').Buffer,
  40. Collection = require('../collection'),
  41. fs = require('fs'),
  42. f = require('util').format,
  43. util = require('util'),
  44. Define = require('../metadata'),
  45. MongoError = require('mongodb-core').MongoError,
  46. inherits = util.inherits,
  47. Duplex = require('stream').Duplex || require('readable-stream').Duplex,
  48. shallowClone = require('../utils').shallowClone;
  49. var REFERENCE_BY_FILENAME = 0,
  50. REFERENCE_BY_ID = 1;
  51. /**
  52. * Namespace provided by the mongodb-core and node.js
  53. * @external Duplex
  54. */
  55. /**
  56. * Create a new GridStore instance
  57. *
  58. * Modes
  59. * - **"r"** - read only. This is the default mode.
  60. * - **"w"** - write in truncate mode. Existing data will be overwriten.
  61. *
  62. * @class
  63. * @param {Db} db A database instance to interact with.
  64. * @param {object} [id] optional unique id for this file
  65. * @param {string} [filename] optional filename for this file, no unique constrain on the field
  66. * @param {string} mode set the mode for this file.
  67. * @param {object} [options=null] Optional settings.
  68. * @param {(number|string)} [options.w=null] The write concern.
  69. * @param {number} [options.wtimeout=null] The write concern timeout.
  70. * @param {boolean} [options.j=false] Specify a journal write concern.
  71. * @param {boolean} [options.fsync=false] Specify a file sync write concern.
  72. * @param {string} [options.root=null] Root collection to use. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
  73. * @param {string} [options.content_type=null] MIME type of the file. Defaults to **{GridStore.DEFAULT_CONTENT_TYPE}**.
  74. * @param {number} [options.chunk_size=261120] Size for the chunk. Defaults to **{Chunk.DEFAULT_CHUNK_SIZE}**.
  75. * @param {object} [options.metadata=null] Arbitrary data the user wants to store.
  76. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
  77. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  78. * @property {number} chunkSize Get the gridstore chunk size.
  79. * @property {number} md5 The md5 checksum for this file.
  80. * @property {number} chunkNumber The current chunk number the gridstore has materialized into memory
  81. * @return {GridStore} a GridStore instance.
  82. * @deprecated Use GridFSBucket API instead
  83. */
  84. var GridStore = function GridStore(db, id, filename, mode, options) {
  85. if(!(this instanceof GridStore)) return new GridStore(db, id, filename, mode, options);
  86. this.db = db;
  87. // Handle options
  88. if(typeof options === 'undefined') options = {};
  89. // Handle mode
  90. if(typeof mode === 'undefined') {
  91. mode = filename;
  92. filename = undefined;
  93. } else if(typeof mode == 'object') {
  94. options = mode;
  95. mode = filename;
  96. filename = undefined;
  97. }
  98. if(id && id._bsontype == 'ObjectID') {
  99. this.referenceBy = REFERENCE_BY_ID;
  100. this.fileId = id;
  101. this.filename = filename;
  102. } else if(typeof filename == 'undefined') {
  103. this.referenceBy = REFERENCE_BY_FILENAME;
  104. this.filename = id;
  105. if (mode.indexOf('w') != null) {
  106. this.fileId = new ObjectID();
  107. }
  108. } else {
  109. this.referenceBy = REFERENCE_BY_ID;
  110. this.fileId = id;
  111. this.filename = filename;
  112. }
  113. // Set up the rest
  114. this.mode = mode == null ? "r" : mode;
  115. this.options = options || {};
  116. // Opened
  117. this.isOpen = false;
  118. // Set the root if overridden
  119. this.root = this.options['root'] == null ? GridStore.DEFAULT_ROOT_COLLECTION : this.options['root'];
  120. this.position = 0;
  121. this.readPreference = this.options.readPreference || db.options.readPreference || ReadPreference.PRIMARY;
  122. this.writeConcern = _getWriteConcern(db, this.options);
  123. // Set default chunk size
  124. this.internalChunkSize = this.options['chunkSize'] == null ? Chunk.DEFAULT_CHUNK_SIZE : this.options['chunkSize'];
  125. // Get the promiseLibrary
  126. var promiseLibrary = this.options.promiseLibrary;
  127. // No promise library selected fall back
  128. if(!promiseLibrary) {
  129. promiseLibrary = typeof global.Promise == 'function' ?
  130. global.Promise : require('es6-promise').Promise;
  131. }
  132. // Set the promiseLibrary
  133. this.promiseLibrary = promiseLibrary;
  134. Object.defineProperty(this, "chunkSize", { enumerable: true
  135. , get: function () {
  136. return this.internalChunkSize;
  137. }
  138. , set: function(value) {
  139. if(!(this.mode[0] == "w" && this.position == 0 && this.uploadDate == null)) {
  140. this.internalChunkSize = this.internalChunkSize;
  141. } else {
  142. this.internalChunkSize = value;
  143. }
  144. }
  145. });
  146. Object.defineProperty(this, "md5", { enumerable: true
  147. , get: function () {
  148. return this.internalMd5;
  149. }
  150. });
  151. Object.defineProperty(this, "chunkNumber", { enumerable: true
  152. , get: function () {
  153. return this.currentChunk && this.currentChunk.chunkNumber ? this.currentChunk.chunkNumber : null;
  154. }
  155. });
  156. }
  157. var define = GridStore.define = new Define('Gridstore', GridStore, true);
  158. /**
  159. * The callback format for the Gridstore.open method
  160. * @callback GridStore~openCallback
  161. * @param {MongoError} error An error instance representing the error during the execution.
  162. * @param {GridStore} gridStore The GridStore instance if the open method was successful.
  163. */
  164. /**
  165. * Opens the file from the database and initialize this object. Also creates a
  166. * new one if file does not exist.
  167. *
  168. * @method
  169. * @param {GridStore~openCallback} [callback] this will be called after executing this method
  170. * @return {Promise} returns Promise if no callback passed
  171. * @deprecated Use GridFSBucket API instead
  172. */
  173. GridStore.prototype.open = function(callback) {
  174. var self = this;
  175. if( this.mode != "w" && this.mode != "w+" && this.mode != "r"){
  176. throw MongoError.create({message: "Illegal mode " + this.mode, driver:true});
  177. }
  178. // We provided a callback leg
  179. if(typeof callback == 'function') return open(self, callback);
  180. // Return promise
  181. return new self.promiseLibrary(function(resolve, reject) {
  182. open(self, function(err, store) {
  183. if(err) return reject(err);
  184. resolve(store);
  185. })
  186. });
  187. };
  188. var open = function(self, callback) {
  189. // Get the write concern
  190. var writeConcern = _getWriteConcern(self.db, self.options);
  191. // If we are writing we need to ensure we have the right indexes for md5's
  192. if((self.mode == "w" || self.mode == "w+")) {
  193. // Get files collection
  194. var collection = self.collection();
  195. // Put index on filename
  196. collection.ensureIndex([['filename', 1]], writeConcern, function() {
  197. // Get chunk collection
  198. var chunkCollection = self.chunkCollection();
  199. // Make an unique index for compatibility with mongo-cxx-driver:legacy
  200. var chunkIndexOptions = shallowClone(writeConcern);
  201. chunkIndexOptions.unique = true;
  202. // Ensure index on chunk collection
  203. chunkCollection.ensureIndex([['files_id', 1], ['n', 1]], chunkIndexOptions, function() {
  204. // Open the connection
  205. _open(self, writeConcern, function(err, r) {
  206. if(err) return callback(err);
  207. self.isOpen = true;
  208. callback(err, r);
  209. });
  210. });
  211. });
  212. } else {
  213. // Open the gridstore
  214. _open(self, writeConcern, function(err, r) {
  215. if(err) return callback(err);
  216. self.isOpen = true;
  217. callback(err, r);
  218. });
  219. }
  220. }
  221. // Push the definition for open
  222. define.classMethod('open', {callback: true, promise:true});
  223. /**
  224. * Verify if the file is at EOF.
  225. *
  226. * @method
  227. * @return {boolean} true if the read/write head is at the end of this file.
  228. * @deprecated Use GridFSBucket API instead
  229. */
  230. GridStore.prototype.eof = function() {
  231. return this.position == this.length ? true : false;
  232. }
  233. define.classMethod('eof', {callback: false, promise:false, returns: [Boolean]});
  234. /**
  235. * The callback result format.
  236. * @callback GridStore~resultCallback
  237. * @param {MongoError} error An error instance representing the error during the execution.
  238. * @param {object} result The result from the callback.
  239. */
  240. /**
  241. * Retrieves a single character from this file.
  242. *
  243. * @method
  244. * @param {GridStore~resultCallback} [callback] this gets called after this method is executed. Passes null to the first parameter and the character read to the second or null to the second if the read/write head is at the end of the file.
  245. * @return {Promise} returns Promise if no callback passed
  246. * @deprecated Use GridFSBucket API instead
  247. */
  248. GridStore.prototype.getc = function(callback) {
  249. var self = this;
  250. // We provided a callback leg
  251. if(typeof callback == 'function') return eof(self, callback);
  252. // Return promise
  253. return new self.promiseLibrary(function(resolve, reject) {
  254. eof(self, function(err, r) {
  255. if(err) return reject(err);
  256. resolve(r);
  257. })
  258. });
  259. }
  260. var eof = function(self, callback) {
  261. if(self.eof()) {
  262. callback(null, null);
  263. } else if(self.currentChunk.eof()) {
  264. nthChunk(self, self.currentChunk.chunkNumber + 1, function(err, chunk) {
  265. self.currentChunk = chunk;
  266. self.position = self.position + 1;
  267. callback(err, self.currentChunk.getc());
  268. });
  269. } else {
  270. self.position = self.position + 1;
  271. callback(null, self.currentChunk.getc());
  272. }
  273. }
  274. define.classMethod('getc', {callback: true, promise:true});
  275. /**
  276. * Writes a string to the file with a newline character appended at the end if
  277. * the given string does not have one.
  278. *
  279. * @method
  280. * @param {string} string the string to write.
  281. * @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
  282. * @return {Promise} returns Promise if no callback passed
  283. * @deprecated Use GridFSBucket API instead
  284. */
  285. GridStore.prototype.puts = function(string, callback) {
  286. var self = this;
  287. var finalString = string.match(/\n$/) == null ? string + "\n" : string;
  288. // We provided a callback leg
  289. if(typeof callback == 'function') return this.write(finalString, callback);
  290. // Return promise
  291. return new self.promiseLibrary(function(resolve, reject) {
  292. self.write(finalString, function(err, r) {
  293. if(err) return reject(err);
  294. resolve(r);
  295. })
  296. });
  297. }
  298. define.classMethod('puts', {callback: true, promise:true});
  299. /**
  300. * Return a modified Readable stream including a possible transform method.
  301. *
  302. * @method
  303. * @return {GridStoreStream}
  304. * @deprecated Use GridFSBucket API instead
  305. */
  306. GridStore.prototype.stream = function() {
  307. return new GridStoreStream(this);
  308. }
  309. define.classMethod('stream', {callback: false, promise:false, returns: [GridStoreStream]});
  310. /**
  311. * Writes some data. This method will work properly only if initialized with mode "w" or "w+".
  312. *
  313. * @method
  314. * @param {(string|Buffer)} data the data to write.
  315. * @param {boolean} [close] closes this file after writing if set to true.
  316. * @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
  317. * @return {Promise} returns Promise if no callback passed
  318. * @deprecated Use GridFSBucket API instead
  319. */
  320. GridStore.prototype.write = function write(data, close, callback) {
  321. var self = this;
  322. // We provided a callback leg
  323. if(typeof callback == 'function') return _writeNormal(this, data, close, callback);
  324. // Return promise
  325. return new self.promiseLibrary(function(resolve, reject) {
  326. _writeNormal(self, data, close, function(err, r) {
  327. if(err) return reject(err);
  328. resolve(r);
  329. })
  330. });
  331. }
  332. define.classMethod('write', {callback: true, promise:true});
  333. /**
  334. * Handles the destroy part of a stream
  335. *
  336. * @method
  337. * @result {null}
  338. * @deprecated Use GridFSBucket API instead
  339. */
  340. GridStore.prototype.destroy = function destroy() {
  341. // close and do not emit any more events. queued data is not sent.
  342. if(!this.writable) return;
  343. this.readable = false;
  344. if(this.writable) {
  345. this.writable = false;
  346. this._q.length = 0;
  347. this.emit('close');
  348. }
  349. }
  350. define.classMethod('destroy', {callback: false, promise:false});
  351. /**
  352. * Stores a file from the file system to the GridFS database.
  353. *
  354. * @method
  355. * @param {(string|Buffer|FileHandle)} file the file to store.
  356. * @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
  357. * @return {Promise} returns Promise if no callback passed
  358. * @deprecated Use GridFSBucket API instead
  359. */
  360. GridStore.prototype.writeFile = function (file, callback) {
  361. var self = this;
  362. // We provided a callback leg
  363. if(typeof callback == 'function') return writeFile(self, file, callback);
  364. // Return promise
  365. return new self.promiseLibrary(function(resolve, reject) {
  366. writeFile(self, file, function(err, r) {
  367. if(err) return reject(err);
  368. resolve(r);
  369. })
  370. });
  371. };
  372. var writeFile = function(self, file, callback) {
  373. if (typeof file === 'string') {
  374. fs.open(file, 'r', function (err, fd) {
  375. if(err) return callback(err);
  376. self.writeFile(fd, callback);
  377. });
  378. return;
  379. }
  380. self.open(function (err, self) {
  381. if(err) return callback(err, self);
  382. fs.fstat(file, function (err, stats) {
  383. if(err) return callback(err, self);
  384. var offset = 0;
  385. var index = 0;
  386. // Write a chunk
  387. var writeChunk = function() {
  388. fs.read(file, self.chunkSize, offset, 'binary', function(err, data, bytesRead) {
  389. if(err) return callback(err, self);
  390. offset = offset + bytesRead;
  391. // Create a new chunk for the data
  392. var chunk = new Chunk(self, {n:index++}, self.writeConcern);
  393. chunk.write(data, function(err, chunk) {
  394. if(err) return callback(err, self);
  395. chunk.save({}, function(err) {
  396. if(err) return callback(err, self);
  397. self.position = self.position + data.length;
  398. // Point to current chunk
  399. self.currentChunk = chunk;
  400. if(offset >= stats.size) {
  401. fs.close(file);
  402. self.close(function(err) {
  403. if(err) return callback(err, self);
  404. return callback(null, self);
  405. });
  406. } else {
  407. return process.nextTick(writeChunk);
  408. }
  409. });
  410. });
  411. });
  412. }
  413. // Process the first write
  414. process.nextTick(writeChunk);
  415. });
  416. });
  417. }
  418. define.classMethod('writeFile', {callback: true, promise:true});
  419. /**
  420. * Saves this file to the database. This will overwrite the old entry if it
  421. * already exists. This will work properly only if mode was initialized to
  422. * "w" or "w+".
  423. *
  424. * @method
  425. * @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
  426. * @return {Promise} returns Promise if no callback passed
  427. * @deprecated Use GridFSBucket API instead
  428. */
  429. GridStore.prototype.close = function(callback) {
  430. var self = this;
  431. // We provided a callback leg
  432. if(typeof callback == 'function') return close(self, callback);
  433. // Return promise
  434. return new self.promiseLibrary(function(resolve, reject) {
  435. close(self, function(err, r) {
  436. if(err) return reject(err);
  437. resolve(r);
  438. })
  439. });
  440. };
  441. var close = function(self, callback) {
  442. if(self.mode[0] == "w") {
  443. // Set up options
  444. var options = self.writeConcern;
  445. if(self.currentChunk != null && self.currentChunk.position > 0) {
  446. self.currentChunk.save({}, function(err) {
  447. if(err && typeof callback == 'function') return callback(err);
  448. self.collection(function(err, files) {
  449. if(err && typeof callback == 'function') return callback(err);
  450. // Build the mongo object
  451. if(self.uploadDate != null) {
  452. buildMongoObject(self, function(err, mongoObject) {
  453. if(err) {
  454. if(typeof callback == 'function') return callback(err); else throw err;
  455. }
  456. files.save(mongoObject, options, function(err) {
  457. if(typeof callback == 'function')
  458. callback(err, mongoObject);
  459. });
  460. });
  461. } else {
  462. self.uploadDate = new Date();
  463. buildMongoObject(self, function(err, mongoObject) {
  464. if(err) {
  465. if(typeof callback == 'function') return callback(err); else throw err;
  466. }
  467. files.save(mongoObject, options, function(err) {
  468. if(typeof callback == 'function')
  469. callback(err, mongoObject);
  470. });
  471. });
  472. }
  473. });
  474. });
  475. } else {
  476. self.collection(function(err, files) {
  477. if(err && typeof callback == 'function') return callback(err);
  478. self.uploadDate = new Date();
  479. buildMongoObject(self, function(err, mongoObject) {
  480. if(err) {
  481. if(typeof callback == 'function') return callback(err); else throw err;
  482. }
  483. files.save(mongoObject, options, function(err) {
  484. if(typeof callback == 'function')
  485. callback(err, mongoObject);
  486. });
  487. });
  488. });
  489. }
  490. } else if(self.mode[0] == "r") {
  491. if(typeof callback == 'function')
  492. callback(null, null);
  493. } else {
  494. if(typeof callback == 'function')
  495. callback(MongoError.create({message: f("Illegal mode %s", self.mode), driver:true}));
  496. }
  497. }
  498. define.classMethod('close', {callback: true, promise:true});
  499. /**
  500. * The collection callback format.
  501. * @callback GridStore~collectionCallback
  502. * @param {MongoError} error An error instance representing the error during the execution.
  503. * @param {Collection} collection The collection from the command execution.
  504. */
  505. /**
  506. * Retrieve this file's chunks collection.
  507. *
  508. * @method
  509. * @param {GridStore~collectionCallback} callback the command callback.
  510. * @return {Collection}
  511. * @deprecated Use GridFSBucket API instead
  512. */
  513. GridStore.prototype.chunkCollection = function(callback) {
  514. if(typeof callback == 'function')
  515. return this.db.collection((this.root + ".chunks"), callback);
  516. return this.db.collection((this.root + ".chunks"));
  517. };
  518. define.classMethod('chunkCollection', {callback: true, promise:false, returns: [Collection]});
  519. /**
  520. * Deletes all the chunks of this file in the database.
  521. *
  522. * @method
  523. * @param {GridStore~resultCallback} [callback] the command callback.
  524. * @return {Promise} returns Promise if no callback passed
  525. * @deprecated Use GridFSBucket API instead
  526. */
  527. GridStore.prototype.unlink = function(callback) {
  528. var self = this;
  529. // We provided a callback leg
  530. if(typeof callback == 'function') return unlink(self, callback);
  531. // Return promise
  532. return new self.promiseLibrary(function(resolve, reject) {
  533. unlink(self, function(err, r) {
  534. if(err) return reject(err);
  535. resolve(r);
  536. })
  537. });
  538. };
  539. var unlink = function(self, callback) {
  540. deleteChunks(self, function(err) {
  541. if(err!==null) {
  542. err.message = "at deleteChunks: " + err.message;
  543. return callback(err);
  544. }
  545. self.collection(function(err, collection) {
  546. if(err!==null) {
  547. err.message = "at collection: " + err.message;
  548. return callback(err);
  549. }
  550. collection.remove({'_id':self.fileId}, self.writeConcern, function(err) {
  551. callback(err, self);
  552. });
  553. });
  554. });
  555. }
  556. define.classMethod('unlink', {callback: true, promise:true});
  557. /**
  558. * Retrieves the file collection associated with this object.
  559. *
  560. * @method
  561. * @param {GridStore~collectionCallback} callback the command callback.
  562. * @return {Collection}
  563. * @deprecated Use GridFSBucket API instead
  564. */
  565. GridStore.prototype.collection = function(callback) {
  566. if(typeof callback == 'function')
  567. this.db.collection(this.root + ".files", callback);
  568. return this.db.collection(this.root + ".files");
  569. };
  570. define.classMethod('collection', {callback: true, promise:false, returns: [Collection]});
  571. /**
  572. * The readlines callback format.
  573. * @callback GridStore~readlinesCallback
  574. * @param {MongoError} error An error instance representing the error during the execution.
  575. * @param {string[]} strings The array of strings returned.
  576. */
  577. /**
  578. * Read the entire file as a list of strings splitting by the provided separator.
  579. *
  580. * @method
  581. * @param {string} [separator] The character to be recognized as the newline separator.
  582. * @param {GridStore~readlinesCallback} [callback] the command callback.
  583. * @return {Promise} returns Promise if no callback passed
  584. * @deprecated Use GridFSBucket API instead
  585. */
  586. GridStore.prototype.readlines = function(separator, callback) {
  587. var self = this;
  588. var args = Array.prototype.slice.call(arguments, 0);
  589. callback = args.pop();
  590. if(typeof callback != 'function') args.push(callback);
  591. separator = args.length ? args.shift() : "\n";
  592. separator = separator || "\n";
  593. // We provided a callback leg
  594. if(typeof callback == 'function') return readlines(self, separator, callback);
  595. // Return promise
  596. return new self.promiseLibrary(function(resolve, reject) {
  597. readlines(self, separator, function(err, r) {
  598. if(err) return reject(err);
  599. resolve(r);
  600. })
  601. });
  602. };
  603. var readlines = function(self, separator, callback) {
  604. self.read(function(err, data) {
  605. if(err) return callback(err);
  606. var items = data.toString().split(separator);
  607. items = items.length > 0 ? items.splice(0, items.length - 1) : [];
  608. for(var i = 0; i < items.length; i++) {
  609. items[i] = items[i] + separator;
  610. }
  611. callback(null, items);
  612. });
  613. }
  614. define.classMethod('readlines', {callback: true, promise:true});
  615. /**
  616. * Deletes all the chunks of this file in the database if mode was set to "w" or
  617. * "w+" and resets the read/write head to the initial position.
  618. *
  619. * @method
  620. * @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
  621. * @return {Promise} returns Promise if no callback passed
  622. * @deprecated Use GridFSBucket API instead
  623. */
  624. GridStore.prototype.rewind = function(callback) {
  625. var self = this;
  626. // We provided a callback leg
  627. if(typeof callback == 'function') return rewind(self, callback);
  628. // Return promise
  629. return new self.promiseLibrary(function(resolve, reject) {
  630. rewind(self, function(err, r) {
  631. if(err) return reject(err);
  632. resolve(r);
  633. })
  634. });
  635. };
  636. var rewind = function(self, callback) {
  637. if(self.currentChunk.chunkNumber != 0) {
  638. if(self.mode[0] == "w") {
  639. deleteChunks(self, function(err) {
  640. if(err) return callback(err);
  641. self.currentChunk = new Chunk(self, {'n': 0}, self.writeConcern);
  642. self.position = 0;
  643. callback(null, self);
  644. });
  645. } else {
  646. self.currentChunk(0, function(err, chunk) {
  647. if(err) return callback(err);
  648. self.currentChunk = chunk;
  649. self.currentChunk.rewind();
  650. self.position = 0;
  651. callback(null, self);
  652. });
  653. }
  654. } else {
  655. self.currentChunk.rewind();
  656. self.position = 0;
  657. callback(null, self);
  658. }
  659. }
  660. define.classMethod('rewind', {callback: true, promise:true});
  661. /**
  662. * The read callback format.
  663. * @callback GridStore~readCallback
  664. * @param {MongoError} error An error instance representing the error during the execution.
  665. * @param {Buffer} data The data read from the GridStore object
  666. */
  667. /**
  668. * Retrieves the contents of this file and advances the read/write head. Works with Buffers only.
  669. *
  670. * There are 3 signatures for this method:
  671. *
  672. * (callback)
  673. * (length, callback)
  674. * (length, buffer, callback)
  675. *
  676. * @method
  677. * @param {number} [length] the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.
  678. * @param {(string|Buffer)} [buffer] a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.
  679. * @param {GridStore~readCallback} [callback] the command callback.
  680. * @return {Promise} returns Promise if no callback passed
  681. * @deprecated Use GridFSBucket API instead
  682. */
  683. GridStore.prototype.read = function(length, buffer, callback) {
  684. var self = this;
  685. var args = Array.prototype.slice.call(arguments, 0);
  686. callback = args.pop();
  687. if(typeof callback != 'function') args.push(callback);
  688. length = args.length ? args.shift() : null;
  689. buffer = args.length ? args.shift() : null;
  690. // We provided a callback leg
  691. if(typeof callback == 'function') return read(self, length, buffer, callback);
  692. // Return promise
  693. return new self.promiseLibrary(function(resolve, reject) {
  694. read(self, length, buffer, function(err, r) {
  695. if(err) return reject(err);
  696. resolve(r);
  697. })
  698. });
  699. }
  700. var read = function(self, length, buffer, callback) {
  701. // The data is a c-terminated string and thus the length - 1
  702. var finalLength = length == null ? self.length - self.position : length;
  703. var finalBuffer = buffer == null ? new Buffer(finalLength) : buffer;
  704. // Add a index to buffer to keep track of writing position or apply current index
  705. finalBuffer._index = buffer != null && buffer._index != null ? buffer._index : 0;
  706. if((self.currentChunk.length() - self.currentChunk.position + finalBuffer._index) >= finalLength) {
  707. var slice = self.currentChunk.readSlice(finalLength - finalBuffer._index);
  708. // Copy content to final buffer
  709. slice.copy(finalBuffer, finalBuffer._index);
  710. // Update internal position
  711. self.position = self.position + finalBuffer.length;
  712. // Check if we don't have a file at all
  713. if(finalLength == 0 && finalBuffer.length == 0) return callback(MongoError.create({message: "File does not exist", driver:true}), null);
  714. // Else return data
  715. return callback(null, finalBuffer);
  716. }
  717. // Read the next chunk
  718. slice = self.currentChunk.readSlice(self.currentChunk.length() - self.currentChunk.position);
  719. // Copy content to final buffer
  720. slice.copy(finalBuffer, finalBuffer._index);
  721. // Update index position
  722. finalBuffer._index += slice.length;
  723. // Load next chunk and read more
  724. nthChunk(self, self.currentChunk.chunkNumber + 1, function(err, chunk) {
  725. if(err) return callback(err);
  726. if(chunk.length() > 0) {
  727. self.currentChunk = chunk;
  728. self.read(length, finalBuffer, callback);
  729. } else {
  730. if(finalBuffer._index > 0) {
  731. callback(null, finalBuffer)
  732. } else {
  733. callback(MongoError.create({message: "no chunks found for file, possibly corrupt", driver:true}), null);
  734. }
  735. }
  736. });
  737. }
  738. define.classMethod('read', {callback: true, promise:true});
  739. /**
  740. * The tell callback format.
  741. * @callback GridStore~tellCallback
  742. * @param {MongoError} error An error instance representing the error during the execution.
  743. * @param {number} position The current read position in the GridStore.
  744. */
  745. /**
  746. * Retrieves the position of the read/write head of this file.
  747. *
  748. * @method
  749. * @param {number} [length] the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.
  750. * @param {(string|Buffer)} [buffer] a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.
  751. * @param {GridStore~tellCallback} [callback] the command callback.
  752. * @return {Promise} returns Promise if no callback passed
  753. * @deprecated Use GridFSBucket API instead
  754. */
  755. GridStore.prototype.tell = function(callback) {
  756. var self = this;
  757. // We provided a callback leg
  758. if(typeof callback == 'function') return callback(null, this.position);
  759. // Return promise
  760. return new self.promiseLibrary(function(resolve) {
  761. resolve(self.position);
  762. });
  763. };
  764. define.classMethod('tell', {callback: true, promise:true});
  765. /**
  766. * The tell callback format.
  767. * @callback GridStore~gridStoreCallback
  768. * @param {MongoError} error An error instance representing the error during the execution.
  769. * @param {GridStore} gridStore The gridStore.
  770. */
  771. /**
  772. * Moves the read/write head to a new location.
  773. *
  774. * There are 3 signatures for this method
  775. *
  776. * Seek Location Modes
  777. * - **GridStore.IO_SEEK_SET**, **(default)** set the position from the start of the file.
  778. * - **GridStore.IO_SEEK_CUR**, set the position from the current position in the file.
  779. * - **GridStore.IO_SEEK_END**, set the position from the end of the file.
  780. *
  781. * @method
  782. * @param {number} [position] the position to seek to
  783. * @param {number} [seekLocation] seek mode. Use one of the Seek Location modes.
  784. * @param {GridStore~gridStoreCallback} [callback] the command callback.
  785. * @return {Promise} returns Promise if no callback passed
  786. * @deprecated Use GridFSBucket API instead
  787. */
  788. GridStore.prototype.seek = function(position, seekLocation, callback) {
  789. var self = this;
  790. var args = Array.prototype.slice.call(arguments, 1);
  791. callback = args.pop();
  792. if(typeof callback != 'function') args.push(callback);
  793. seekLocation = args.length ? args.shift() : null;
  794. // We provided a callback leg
  795. if(typeof callback == 'function') return seek(self, position, seekLocation, callback);
  796. // Return promise
  797. return new self.promiseLibrary(function(resolve, reject) {
  798. seek(self, position, seekLocation, function(err, r) {
  799. if(err) return reject(err);
  800. resolve(r);
  801. })
  802. });
  803. }
  804. var seek = function(self, position, seekLocation, callback) {
  805. // Seek only supports read mode
  806. if(self.mode != 'r') {
  807. return callback(MongoError.create({message: "seek is only supported for mode r", driver:true}))
  808. }
  809. var seekLocationFinal = seekLocation == null ? GridStore.IO_SEEK_SET : seekLocation;
  810. var finalPosition = position;
  811. var targetPosition = 0;
  812. // Calculate the position
  813. if(seekLocationFinal == GridStore.IO_SEEK_CUR) {
  814. targetPosition = self.position + finalPosition;
  815. } else if(seekLocationFinal == GridStore.IO_SEEK_END) {
  816. targetPosition = self.length + finalPosition;
  817. } else {
  818. targetPosition = finalPosition;
  819. }
  820. // Get the chunk
  821. var newChunkNumber = Math.floor(targetPosition/self.chunkSize);
  822. var seekChunk = function() {
  823. nthChunk(self, newChunkNumber, function(err, chunk) {
  824. if(err) return callback(err, null);
  825. if(chunk == null) return callback(new Error('no chunk found'));
  826. // Set the current chunk
  827. self.currentChunk = chunk;
  828. self.position = targetPosition;
  829. self.currentChunk.position = (self.position % self.chunkSize);
  830. callback(err, self);
  831. });
  832. };
  833. seekChunk();
  834. }
  835. define.classMethod('seek', {callback: true, promise:true});
  836. /**
  837. * @ignore
  838. */
  839. var _open = function(self, options, callback) {
  840. var collection = self.collection();
  841. // Create the query
  842. var query = self.referenceBy == REFERENCE_BY_ID ? {_id:self.fileId} : {filename:self.filename};
  843. query = null == self.fileId && self.filename == null ? null : query;
  844. options.readPreference = self.readPreference;
  845. // Fetch the chunks
  846. if(query != null) {
  847. collection.findOne(query, options, function(err, doc) {
  848. if(err) return error(err);
  849. // Check if the collection for the files exists otherwise prepare the new one
  850. if(doc != null) {
  851. self.fileId = doc._id;
  852. // Prefer a new filename over the existing one if this is a write
  853. self.filename = ((self.mode == 'r') || (self.filename == undefined)) ? doc.filename : self.filename;
  854. self.contentType = doc.contentType;
  855. self.internalChunkSize = doc.chunkSize;
  856. self.uploadDate = doc.uploadDate;
  857. self.aliases = doc.aliases;
  858. self.length = doc.length;
  859. self.metadata = doc.metadata;
  860. self.internalMd5 = doc.md5;
  861. } else if (self.mode != 'r') {
  862. self.fileId = self.fileId == null ? new ObjectID() : self.fileId;
  863. self.contentType = GridStore.DEFAULT_CONTENT_TYPE;
  864. self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
  865. self.length = 0;
  866. } else {
  867. self.length = 0;
  868. var txtId = self.fileId._bsontype == "ObjectID" ? self.fileId.toHexString() : self.fileId;
  869. 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);
  870. }
  871. // Process the mode of the object
  872. if(self.mode == "r") {
  873. nthChunk(self, 0, options, function(err, chunk) {
  874. if(err) return error(err);
  875. self.currentChunk = chunk;
  876. self.position = 0;
  877. callback(null, self);
  878. });
  879. } else if(self.mode == "w" && doc) {
  880. // Delete any existing chunks
  881. deleteChunks(self, options, function(err) {
  882. if(err) return error(err);
  883. self.currentChunk = new Chunk(self, {'n':0}, self.writeConcern);
  884. self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type'];
  885. self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size'];
  886. self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
  887. self.aliases = self.options['aliases'] == null ? self.aliases : self.options['aliases'];
  888. self.position = 0;
  889. callback(null, self);
  890. });
  891. } else if(self.mode == "w") {
  892. self.currentChunk = new Chunk(self, {'n':0}, self.writeConcern);
  893. self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type'];
  894. self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size'];
  895. self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
  896. self.aliases = self.options['aliases'] == null ? self.aliases : self.options['aliases'];
  897. self.position = 0;
  898. callback(null, self);
  899. } else if(self.mode == "w+") {
  900. nthChunk(self, lastChunkNumber(self), options, function(err, chunk) {
  901. if(err) return error(err);
  902. // Set the current chunk
  903. self.currentChunk = chunk == null ? new Chunk(self, {'n':0}, self.writeConcern) : chunk;
  904. self.currentChunk.position = self.currentChunk.data.length();
  905. self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
  906. self.aliases = self.options['aliases'] == null ? self.aliases : self.options['aliases'];
  907. self.position = self.length;
  908. callback(null, self);
  909. });
  910. }
  911. });
  912. } else {
  913. // Write only mode
  914. self.fileId = null == self.fileId ? new ObjectID() : self.fileId;
  915. self.contentType = GridStore.DEFAULT_CONTENT_TYPE;
  916. self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
  917. self.length = 0;
  918. // No file exists set up write mode
  919. if(self.mode == "w") {
  920. // Delete any existing chunks
  921. deleteChunks(self, options, function(err) {
  922. if(err) return error(err);
  923. self.currentChunk = new Chunk(self, {'n':0}, self.writeConcern);
  924. self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type'];
  925. self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size'];
  926. self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
  927. self.aliases = self.options['aliases'] == null ? self.aliases : self.options['aliases'];
  928. self.position = 0;
  929. callback(null, self);
  930. });
  931. } else if(self.mode == "w+") {
  932. nthChunk(self, lastChunkNumber(self), options, function(err, chunk) {
  933. if(err) return error(err);
  934. // Set the current chunk
  935. self.currentChunk = chunk == null ? new Chunk(self, {'n':0}, self.writeConcern) : chunk;
  936. self.currentChunk.position = self.currentChunk.data.length();
  937. self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
  938. self.aliases = self.options['aliases'] == null ? self.aliases : self.options['aliases'];
  939. self.position = self.length;
  940. callback(null, self);
  941. });
  942. }
  943. }
  944. // only pass error to callback once
  945. function error (err) {
  946. if(error.err) return;
  947. callback(error.err = err);
  948. }
  949. };
  950. /**
  951. * @ignore
  952. */
  953. var writeBuffer = function(self, buffer, close, callback) {
  954. if(typeof close === "function") { callback = close; close = null; }
  955. var finalClose = typeof close == 'boolean' ? close : false;
  956. if(self.mode != "w") {
  957. callback(MongoError.create({message: f("file with id %s not opened for writing", (self.referenceBy == REFERENCE_BY_ID ? self.referenceBy : self.filename)), driver:true}), null);
  958. } else {
  959. if(self.currentChunk.position + buffer.length >= self.chunkSize) {
  960. // Write out the current Chunk and then keep writing until we have less data left than a chunkSize left
  961. // to a new chunk (recursively)
  962. var previousChunkNumber = self.currentChunk.chunkNumber;
  963. var leftOverDataSize = self.chunkSize - self.currentChunk.position;
  964. var firstChunkData = buffer.slice(0, leftOverDataSize);
  965. var leftOverData = buffer.slice(leftOverDataSize);
  966. // A list of chunks to write out
  967. var chunksToWrite = [self.currentChunk.write(firstChunkData)];
  968. // If we have more data left than the chunk size let's keep writing new chunks
  969. while(leftOverData.length >= self.chunkSize) {
  970. // Create a new chunk and write to it
  971. var newChunk = new Chunk(self, {'n': (previousChunkNumber + 1)}, self.writeConcern);
  972. firstChunkData = leftOverData.slice(0, self.chunkSize);
  973. leftOverData = leftOverData.slice(self.chunkSize);
  974. // Update chunk number
  975. previousChunkNumber = previousChunkNumber + 1;
  976. // Write data
  977. newChunk.write(firstChunkData);
  978. // Push chunk to save list
  979. chunksToWrite.push(newChunk);
  980. }
  981. // Set current chunk with remaining data
  982. self.currentChunk = new Chunk(self, {'n': (previousChunkNumber + 1)}, self.writeConcern);
  983. // If we have left over data write it
  984. if(leftOverData.length > 0) self.currentChunk.write(leftOverData);
  985. // Update the position for the gridstore
  986. self.position = self.position + buffer.length;
  987. // Total number of chunks to write
  988. var numberOfChunksToWrite = chunksToWrite.length;
  989. for(var i = 0; i < chunksToWrite.length; i++) {
  990. chunksToWrite[i].save({}, function(err) {
  991. if(err) return callback(err);
  992. numberOfChunksToWrite = numberOfChunksToWrite - 1;
  993. if(numberOfChunksToWrite <= 0) {
  994. // We care closing the file before returning
  995. if(finalClose) {
  996. return self.close(function(err) {
  997. callback(err, self);
  998. });
  999. }
  1000. // Return normally
  1001. return callback(null, self);
  1002. }
  1003. });
  1004. }
  1005. } else {
  1006. // Update the position for the gridstore
  1007. self.position = self.position + buffer.length;
  1008. // We have less data than the chunk size just write it and callback
  1009. self.currentChunk.write(buffer);
  1010. // We care closing the file before returning
  1011. if(finalClose) {
  1012. return self.close(function(err) {
  1013. callback(err, self);
  1014. });
  1015. }
  1016. // Return normally
  1017. return callback(null, self);
  1018. }
  1019. }
  1020. };
  1021. /**
  1022. * Creates a mongoDB object representation of this object.
  1023. *
  1024. * <pre><code>
  1025. * {
  1026. * '_id' : , // {number} id for this file
  1027. * 'filename' : , // {string} name for this file
  1028. * 'contentType' : , // {string} mime type for this file
  1029. * 'length' : , // {number} size of this file?
  1030. * 'chunksize' : , // {number} chunk size used by this file
  1031. * 'uploadDate' : , // {Date}
  1032. * 'aliases' : , // {array of string}
  1033. * 'metadata' : , // {string}
  1034. * }
  1035. * </code></pre>
  1036. *
  1037. * @ignore
  1038. */
  1039. var buildMongoObject = function(self, callback) {
  1040. // Calcuate the length
  1041. var mongoObject = {
  1042. '_id': self.fileId,
  1043. 'filename': self.filename,
  1044. 'contentType': self.contentType,
  1045. 'length': self.position ? self.position : 0,
  1046. 'chunkSize': self.chunkSize,
  1047. 'uploadDate': self.uploadDate,
  1048. 'aliases': self.aliases,
  1049. 'metadata': self.metadata
  1050. };
  1051. var md5Command = {filemd5:self.fileId, root:self.root};
  1052. self.db.command(md5Command, function(err, results) {
  1053. if(err) return callback(err);
  1054. mongoObject.md5 = results.md5;
  1055. callback(null, mongoObject);
  1056. });
  1057. };
  1058. /**
  1059. * Gets the nth chunk of this file.
  1060. * @ignore
  1061. */
  1062. var nthChunk = function(self, chunkNumber, options, callback) {
  1063. if(typeof options == 'function') {
  1064. callback = options;
  1065. options = {};
  1066. }
  1067. options = options || self.writeConcern;
  1068. options.readPreference = self.readPreference;
  1069. // Get the nth chunk
  1070. self.chunkCollection().findOne({'files_id':self.fileId, 'n':chunkNumber}, options, function(err, chunk) {
  1071. if(err) return callback(err);
  1072. var finalChunk = chunk == null ? {} : chunk;
  1073. callback(null, new Chunk(self, finalChunk, self.writeConcern));
  1074. });
  1075. };
  1076. /**
  1077. * @ignore
  1078. */
  1079. var lastChunkNumber = function(self) {
  1080. return Math.floor((self.length ? self.length - 1 : 0)/self.chunkSize);
  1081. };
  1082. /**
  1083. * Deletes all the chunks of this file in the database.
  1084. *
  1085. * @ignore
  1086. */
  1087. var deleteChunks = function(self, options, callback) {
  1088. if(typeof options == 'function') {
  1089. callback = options;
  1090. options = {};
  1091. }
  1092. options = options || self.writeConcern;
  1093. if(self.fileId != null) {
  1094. self.chunkCollection().remove({'files_id':self.fileId}, options, function(err) {
  1095. if(err) return callback(err, false);
  1096. callback(null, true);
  1097. });
  1098. } else {
  1099. callback(null, true);
  1100. }
  1101. };
  1102. /**
  1103. * The collection to be used for holding the files and chunks collection.
  1104. *
  1105. * @classconstant DEFAULT_ROOT_COLLECTION
  1106. **/
  1107. GridStore.DEFAULT_ROOT_COLLECTION = 'fs';
  1108. /**
  1109. * Default file mime type
  1110. *
  1111. * @classconstant DEFAULT_CONTENT_TYPE
  1112. **/
  1113. GridStore.DEFAULT_CONTENT_TYPE = 'binary/octet-stream';
  1114. /**
  1115. * Seek mode where the given length is absolute.
  1116. *
  1117. * @classconstant IO_SEEK_SET
  1118. **/
  1119. GridStore.IO_SEEK_SET = 0;
  1120. /**
  1121. * Seek mode where the given length is an offset to the current read/write head.
  1122. *
  1123. * @classconstant IO_SEEK_CUR
  1124. **/
  1125. GridStore.IO_SEEK_CUR = 1;
  1126. /**
  1127. * Seek mode where the given length is an offset to the end of the file.
  1128. *
  1129. * @classconstant IO_SEEK_END
  1130. **/
  1131. GridStore.IO_SEEK_END = 2;
  1132. /**
  1133. * Checks if a file exists in the database.
  1134. *
  1135. * @method
  1136. * @static
  1137. * @param {Db} db the database to query.
  1138. * @param {string} name The name of the file to look for.
  1139. * @param {string} [rootCollection] The root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
  1140. * @param {object} [options=null] Optional settings.
  1141. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1142. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
  1143. * @param {GridStore~resultCallback} [callback] result from exists.
  1144. * @return {Promise} returns Promise if no callback passed
  1145. * @deprecated Use GridFSBucket API instead
  1146. */
  1147. GridStore.exist = function(db, fileIdObject, rootCollection, options, callback) {
  1148. var args = Array.prototype.slice.call(arguments, 2);
  1149. callback = args.pop();
  1150. if(typeof callback != 'function') args.push(callback);
  1151. rootCollection = args.length ? args.shift() : null;
  1152. options = args.length ? args.shift() : {};
  1153. options = options || {};
  1154. // Get the promiseLibrary
  1155. var promiseLibrary = options.promiseLibrary;
  1156. // No promise library selected fall back
  1157. if(!promiseLibrary) {
  1158. promiseLibrary = typeof global.Promise == 'function' ?
  1159. global.Promise : require('es6-promise').Promise;
  1160. }
  1161. // We provided a callback leg
  1162. if(typeof callback == 'function') return exists(db, fileIdObject, rootCollection, options, callback);
  1163. // Return promise
  1164. return new promiseLibrary(function(resolve, reject) {
  1165. exists(db, fileIdObject, rootCollection, options, function(err, r) {
  1166. if(err) return reject(err);
  1167. resolve(r);
  1168. })
  1169. });
  1170. };
  1171. var exists = function(db, fileIdObject, rootCollection, options, callback) {
  1172. // Establish read preference
  1173. var readPreference = options.readPreference || ReadPreference.PRIMARY;
  1174. // Fetch collection
  1175. var rootCollectionFinal = rootCollection != null ? rootCollection : GridStore.DEFAULT_ROOT_COLLECTION;
  1176. db.collection(rootCollectionFinal + ".files", function(err, collection) {
  1177. if(err) return callback(err);
  1178. // Build query
  1179. var query = (typeof fileIdObject == 'string' || Object.prototype.toString.call(fileIdObject) == '[object RegExp]' )
  1180. ? {'filename':fileIdObject}
  1181. : {'_id':fileIdObject}; // Attempt to locate file
  1182. // We have a specific query
  1183. if(fileIdObject != null
  1184. && typeof fileIdObject == 'object'
  1185. && Object.prototype.toString.call(fileIdObject) != '[object RegExp]') {
  1186. query = fileIdObject;
  1187. }
  1188. // Check if the entry exists
  1189. collection.findOne(query, {readPreference:readPreference}, function(err, item) {
  1190. if(err) return callback(err);
  1191. callback(null, item == null ? false : true);
  1192. });
  1193. });
  1194. }
  1195. define.staticMethod('exist', {callback: true, promise:true});
  1196. /**
  1197. * Gets the list of files stored in the GridFS.
  1198. *
  1199. * @method
  1200. * @static
  1201. * @param {Db} db the database to query.
  1202. * @param {string} [rootCollection] The root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
  1203. * @param {object} [options=null] Optional settings.
  1204. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1205. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
  1206. * @param {GridStore~resultCallback} [callback] result from exists.
  1207. * @return {Promise} returns Promise if no callback passed
  1208. * @deprecated Use GridFSBucket API instead
  1209. */
  1210. GridStore.list = function(db, rootCollection, options, callback) {
  1211. var args = Array.prototype.slice.call(arguments, 1);
  1212. callback = args.pop();
  1213. if(typeof callback != 'function') args.push(callback);
  1214. rootCollection = args.length ? args.shift() : null;
  1215. options = args.length ? args.shift() : {};
  1216. options = options || {};
  1217. // Get the promiseLibrary
  1218. var promiseLibrary = options.promiseLibrary;
  1219. // No promise library selected fall back
  1220. if(!promiseLibrary) {
  1221. promiseLibrary = typeof global.Promise == 'function' ?
  1222. global.Promise : require('es6-promise').Promise;
  1223. }
  1224. // We provided a callback leg
  1225. if(typeof callback == 'function') return list(db, rootCollection, options, callback);
  1226. // Return promise
  1227. return new promiseLibrary(function(resolve, reject) {
  1228. list(db, rootCollection, options, function(err, r) {
  1229. if(err) return reject(err);
  1230. resolve(r);
  1231. })
  1232. });
  1233. };
  1234. var list = function(db, rootCollection, options, callback) {
  1235. // Ensure we have correct values
  1236. if(rootCollection != null && typeof rootCollection == 'object') {
  1237. options = rootCollection;
  1238. rootCollection = null;
  1239. }
  1240. // Establish read preference
  1241. var readPreference = options.readPreference || ReadPreference.PRIMARY;
  1242. // Check if we are returning by id not filename
  1243. var byId = options['id'] != null ? options['id'] : false;
  1244. // Fetch item
  1245. var rootCollectionFinal = rootCollection != null ? rootCollection : GridStore.DEFAULT_ROOT_COLLECTION;
  1246. var items = [];
  1247. db.collection((rootCollectionFinal + ".files"), function(err, collection) {
  1248. if(err) return callback(err);
  1249. collection.find({}, {readPreference:readPreference}, function(err, cursor) {
  1250. if(err) return callback(err);
  1251. cursor.each(function(err, item) {
  1252. if(item != null) {
  1253. items.push(byId ? item._id : item.filename);
  1254. } else {
  1255. callback(err, items);
  1256. }
  1257. });
  1258. });
  1259. });
  1260. }
  1261. define.staticMethod('list', {callback: true, promise:true});
  1262. /**
  1263. * Reads the contents of a file.
  1264. *
  1265. * This method has the following signatures
  1266. *
  1267. * (db, name, callback)
  1268. * (db, name, length, callback)
  1269. * (db, name, length, offset, callback)
  1270. * (db, name, length, offset, options, callback)
  1271. *
  1272. * @method
  1273. * @static
  1274. * @param {Db} db the database to query.
  1275. * @param {string} name The name of the file.
  1276. * @param {number} [length] The size of data to read.
  1277. * @param {number} [offset] The offset from the head of the file of which to start reading from.
  1278. * @param {object} [options=null] Optional settings.
  1279. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1280. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
  1281. * @param {GridStore~readCallback} [callback] the command callback.
  1282. * @return {Promise} returns Promise if no callback passed
  1283. * @deprecated Use GridFSBucket API instead
  1284. */
  1285. GridStore.read = function(db, name, length, offset, options, callback) {
  1286. var args = Array.prototype.slice.call(arguments, 2);
  1287. callback = args.pop();
  1288. if(typeof callback != 'function') args.push(callback);
  1289. length = args.length ? args.shift() : null;
  1290. offset = args.length ? args.shift() : null;
  1291. options = args.length ? args.shift() : null;
  1292. options = options || {};
  1293. // Get the promiseLibrary
  1294. var promiseLibrary = options ? options.promiseLibrary : null;
  1295. // No promise library selected fall back
  1296. if(!promiseLibrary) {
  1297. promiseLibrary = typeof global.Promise == 'function' ?
  1298. global.Promise : require('es6-promise').Promise;
  1299. }
  1300. // We provided a callback leg
  1301. if(typeof callback == 'function') return readStatic(db, name, length, offset, options, callback);
  1302. // Return promise
  1303. return new promiseLibrary(function(resolve, reject) {
  1304. readStatic(db, name, length, offset, options, function(err, r) {
  1305. if(err) return reject(err);
  1306. resolve(r);
  1307. })
  1308. });
  1309. };
  1310. var readStatic = function(db, name, length, offset, options, callback) {
  1311. new GridStore(db, name, "r", options).open(function(err, gridStore) {
  1312. if(err) return callback(err);
  1313. // Make sure we are not reading out of bounds
  1314. if(offset && offset >= gridStore.length) return callback("offset larger than size of file", null);
  1315. if(length && length > gridStore.length) return callback("length is larger than the size of the file", null);
  1316. if(offset && length && (offset + length) > gridStore.length) return callback("offset and length is larger than the size of the file", null);
  1317. if(offset != null) {
  1318. gridStore.seek(offset, function(err, gridStore) {
  1319. if(err) return callback(err);
  1320. gridStore.read(length, callback);
  1321. });
  1322. } else {
  1323. gridStore.read(length, callback);
  1324. }
  1325. });
  1326. }
  1327. define.staticMethod('read', {callback: true, promise:true});
  1328. /**
  1329. * Read the entire file as a list of strings splitting by the provided separator.
  1330. *
  1331. * @method
  1332. * @static
  1333. * @param {Db} db the database to query.
  1334. * @param {(String|object)} name the name of the file.
  1335. * @param {string} [separator] The character to be recognized as the newline separator.
  1336. * @param {object} [options=null] Optional settings.
  1337. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1338. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
  1339. * @param {GridStore~readlinesCallback} [callback] the command callback.
  1340. * @return {Promise} returns Promise if no callback passed
  1341. * @deprecated Use GridFSBucket API instead
  1342. */
  1343. GridStore.readlines = function(db, name, separator, options, callback) {
  1344. var args = Array.prototype.slice.call(arguments, 2);
  1345. callback = args.pop();
  1346. if(typeof callback != 'function') args.push(callback);
  1347. separator = args.length ? args.shift() : null;
  1348. options = args.length ? args.shift() : null;
  1349. options = options || {};
  1350. // Get the promiseLibrary
  1351. var promiseLibrary = options ? options.promiseLibrary : null;
  1352. // No promise library selected fall back
  1353. if(!promiseLibrary) {
  1354. promiseLibrary = typeof global.Promise == 'function' ?
  1355. global.Promise : require('es6-promise').Promise;
  1356. }
  1357. // We provided a callback leg
  1358. if(typeof callback == 'function') return readlinesStatic(db, name, separator, options, callback);
  1359. // Return promise
  1360. return new promiseLibrary(function(resolve, reject) {
  1361. readlinesStatic(db, name, separator, options, function(err, r) {
  1362. if(err) return reject(err);
  1363. resolve(r);
  1364. })
  1365. });
  1366. };
  1367. var readlinesStatic = function(db, name, separator, options, callback) {
  1368. var finalSeperator = separator == null ? "\n" : separator;
  1369. new GridStore(db, name, "r", options).open(function(err, gridStore) {
  1370. if(err) return callback(err);
  1371. gridStore.readlines(finalSeperator, callback);
  1372. });
  1373. }
  1374. define.staticMethod('readlines', {callback: true, promise:true});
  1375. /**
  1376. * Deletes the chunks and metadata information of a file from GridFS.
  1377. *
  1378. * @method
  1379. * @static
  1380. * @param {Db} db The database to query.
  1381. * @param {(string|array)} names The name/names of the files to delete.
  1382. * @param {object} [options=null] Optional settings.
  1383. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
  1384. * @param {GridStore~resultCallback} [callback] the command callback.
  1385. * @return {Promise} returns Promise if no callback passed
  1386. * @deprecated Use GridFSBucket API instead
  1387. */
  1388. GridStore.unlink = function(db, names, options, callback) {
  1389. var self = this;
  1390. var args = Array.prototype.slice.call(arguments, 2);
  1391. callback = args.pop();
  1392. if(typeof callback != 'function') args.push(callback);
  1393. options = args.length ? args.shift() : {};
  1394. options = options || {};
  1395. // Get the promiseLibrary
  1396. var promiseLibrary = options.promiseLibrary;
  1397. // No promise library selected fall back
  1398. if(!promiseLibrary) {
  1399. promiseLibrary = typeof global.Promise == 'function' ?
  1400. global.Promise : require('es6-promise').Promise;
  1401. }
  1402. // We provided a callback leg
  1403. if(typeof callback == 'function') return unlinkStatic(self, db, names, options, callback);
  1404. // Return promise
  1405. return new promiseLibrary(function(resolve, reject) {
  1406. unlinkStatic(self, db, names, options, function(err, r) {
  1407. if(err) return reject(err);
  1408. resolve(r);
  1409. })
  1410. });
  1411. };
  1412. var unlinkStatic = function(self, db, names, options, callback) {
  1413. // Get the write concern
  1414. var writeConcern = _getWriteConcern(db, options);
  1415. // List of names
  1416. if(names.constructor == Array) {
  1417. var tc = 0;
  1418. for(var i = 0; i < names.length; i++) {
  1419. ++tc;
  1420. GridStore.unlink(db, names[i], options, function() {
  1421. if(--tc == 0) {
  1422. callback(null, self);
  1423. }
  1424. });
  1425. }
  1426. } else {
  1427. new GridStore(db, names, "w", options).open(function(err, gridStore) {
  1428. if(err) return callback(err);
  1429. deleteChunks(gridStore, function(err) {
  1430. if(err) return callback(err);
  1431. gridStore.collection(function(err, collection) {
  1432. if(err) return callback(err);
  1433. collection.remove({'_id':gridStore.fileId}, writeConcern, function(err) {
  1434. callback(err, self);
  1435. });
  1436. });
  1437. });
  1438. });
  1439. }
  1440. }
  1441. define.staticMethod('unlink', {callback: true, promise:true});
  1442. /**
  1443. * @ignore
  1444. */
  1445. var _writeNormal = function(self, data, close, callback) {
  1446. // If we have a buffer write it using the writeBuffer method
  1447. if(Buffer.isBuffer(data)) {
  1448. return writeBuffer(self, data, close, callback);
  1449. } else {
  1450. return writeBuffer(self, new Buffer(data, 'binary'), close, callback);
  1451. }
  1452. }
  1453. /**
  1454. * @ignore
  1455. */
  1456. var _setWriteConcernHash = function(options) {
  1457. var finalOptions = {};
  1458. if(options.w != null) finalOptions.w = options.w;
  1459. if(options.journal == true) finalOptions.j = options.journal;
  1460. if(options.j == true) finalOptions.j = options.j;
  1461. if(options.fsync == true) finalOptions.fsync = options.fsync;
  1462. if(options.wtimeout != null) finalOptions.wtimeout = options.wtimeout;
  1463. return finalOptions;
  1464. }
  1465. /**
  1466. * @ignore
  1467. */
  1468. var _getWriteConcern = function(self, options) {
  1469. // Final options
  1470. var finalOptions = {w:1};
  1471. options = options || {};
  1472. // Local options verification
  1473. if(options.w != null || typeof options.j == 'boolean' || typeof options.journal == 'boolean' || typeof options.fsync == 'boolean') {
  1474. finalOptions = _setWriteConcernHash(options);
  1475. } else if(options.safe != null && typeof options.safe == 'object') {
  1476. finalOptions = _setWriteConcernHash(options.safe);
  1477. } else if(typeof options.safe == "boolean") {
  1478. finalOptions = {w: (options.safe ? 1 : 0)};
  1479. } else if(self.options.w != null || typeof self.options.j == 'boolean' || typeof self.options.journal == 'boolean' || typeof self.options.fsync == 'boolean') {
  1480. finalOptions = _setWriteConcernHash(self.options);
  1481. } else if(self.safe && (self.safe.w != null || typeof self.safe.j == 'boolean' || typeof self.safe.journal == 'boolean' || typeof self.safe.fsync == 'boolean')) {
  1482. finalOptions = _setWriteConcernHash(self.safe);
  1483. } else if(typeof self.safe == "boolean") {
  1484. finalOptions = {w: (self.safe ? 1 : 0)};
  1485. }
  1486. // Ensure we don't have an invalid combination of write concerns
  1487. if(finalOptions.w < 1
  1488. && (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});
  1489. // Return the options
  1490. return finalOptions;
  1491. }
  1492. /**
  1493. * Create a new GridStoreStream instance (INTERNAL TYPE, do not instantiate directly)
  1494. *
  1495. * @class
  1496. * @extends external:Duplex
  1497. * @return {GridStoreStream} a GridStoreStream instance.
  1498. * @deprecated Use GridFSBucket API instead
  1499. */
  1500. var GridStoreStream = function(gs) {
  1501. // Initialize the duplex stream
  1502. Duplex.call(this);
  1503. // Get the gridstore
  1504. this.gs = gs;
  1505. // End called
  1506. this.endCalled = false;
  1507. // If we have a seek
  1508. this.totalBytesToRead = this.gs.length - this.gs.position;
  1509. this.seekPosition = this.gs.position;
  1510. }
  1511. //
  1512. // Inherit duplex
  1513. inherits(GridStoreStream, Duplex);
  1514. GridStoreStream.prototype._pipe = GridStoreStream.prototype.pipe;
  1515. // Set up override
  1516. GridStoreStream.prototype.pipe = function(destination) {
  1517. var self = this;
  1518. // Only open gridstore if not already open
  1519. if(!self.gs.isOpen) {
  1520. self.gs.open(function(err) {
  1521. if(err) return self.emit('error', err);
  1522. self.totalBytesToRead = self.gs.length - self.gs.position;
  1523. self._pipe.apply(self, [destination]);
  1524. });
  1525. } else {
  1526. self.totalBytesToRead = self.gs.length - self.gs.position;
  1527. self._pipe.apply(self, [destination]);
  1528. }
  1529. return destination;
  1530. }
  1531. // Called by stream
  1532. GridStoreStream.prototype._read = function() {
  1533. var self = this;
  1534. var read = function() {
  1535. // Read data
  1536. self.gs.read(length, function(err, buffer) {
  1537. if(err && !self.endCalled) return self.emit('error', err);
  1538. // Stream is closed
  1539. if(self.endCalled || buffer == null) return self.push(null);
  1540. // Remove bytes read
  1541. if(buffer.length <= self.totalBytesToRead) {
  1542. self.totalBytesToRead = self.totalBytesToRead - buffer.length;
  1543. self.push(buffer);
  1544. } else if(buffer.length > self.totalBytesToRead) {
  1545. self.totalBytesToRead = self.totalBytesToRead - buffer._index;
  1546. self.push(buffer.slice(0, buffer._index));
  1547. }
  1548. // Finished reading
  1549. if(self.totalBytesToRead <= 0) {
  1550. self.endCalled = true;
  1551. }
  1552. });
  1553. }
  1554. // Set read length
  1555. var length = self.gs.length < self.gs.chunkSize ? self.gs.length - self.seekPosition : self.gs.chunkSize;
  1556. if(!self.gs.isOpen) {
  1557. self.gs.open(function(err) {
  1558. self.totalBytesToRead = self.gs.length - self.gs.position;
  1559. if(err) return self.emit('error', err);
  1560. read();
  1561. });
  1562. } else {
  1563. read();
  1564. }
  1565. }
  1566. GridStoreStream.prototype.destroy = function() {
  1567. this.pause();
  1568. this.endCalled = true;
  1569. this.gs.close();
  1570. this.emit('end');
  1571. }
  1572. GridStoreStream.prototype.write = function(chunk) {
  1573. var self = this;
  1574. if(self.endCalled) return self.emit('error', MongoError.create({message: 'attempting to write to stream after end called', driver:true}))
  1575. // Do we have to open the gridstore
  1576. if(!self.gs.isOpen) {
  1577. self.gs.open(function() {
  1578. self.gs.isOpen = true;
  1579. self.gs.write(chunk, function() {
  1580. process.nextTick(function() {
  1581. self.emit('drain');
  1582. });
  1583. });
  1584. });
  1585. return false;
  1586. } else {
  1587. self.gs.write(chunk, function() {
  1588. self.emit('drain');
  1589. });
  1590. return true;
  1591. }
  1592. }
  1593. GridStoreStream.prototype.end = function(chunk, encoding, callback) {
  1594. var self = this;
  1595. var args = Array.prototype.slice.call(arguments, 0);
  1596. callback = args.pop();
  1597. if(typeof callback != 'function') args.push(callback);
  1598. chunk = args.length ? args.shift() : null;
  1599. encoding = args.length ? args.shift() : null;
  1600. self.endCalled = true;
  1601. if(chunk) {
  1602. self.gs.write(chunk, function() {
  1603. self.gs.close(function() {
  1604. if(typeof callback == 'function') callback();
  1605. self.emit('end')
  1606. });
  1607. });
  1608. }
  1609. self.gs.close(function() {
  1610. if(typeof callback == 'function') callback();
  1611. self.emit('end')
  1612. });
  1613. }
  1614. /**
  1615. * The read() method pulls some data out of the internal buffer and returns it. If there is no data available, then it will return null.
  1616. * @function external:Duplex#read
  1617. * @param {number} size Optional argument to specify how much data to read.
  1618. * @return {(String | Buffer | null)}
  1619. */
  1620. /**
  1621. * Call this function to cause the stream to return strings of the specified encoding instead of Buffer objects.
  1622. * @function external:Duplex#setEncoding
  1623. * @param {string} encoding The encoding to use.
  1624. * @return {null}
  1625. */
  1626. /**
  1627. * This method will cause the readable stream to resume emitting data events.
  1628. * @function external:Duplex#resume
  1629. * @return {null}
  1630. */
  1631. /**
  1632. * This method will cause a stream in flowing-mode to stop emitting data events. Any data that becomes available will remain in the internal buffer.
  1633. * @function external:Duplex#pause
  1634. * @return {null}
  1635. */
  1636. /**
  1637. * This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.
  1638. * @function external:Duplex#pipe
  1639. * @param {Writable} destination The destination for writing data
  1640. * @param {object} [options] Pipe options
  1641. * @return {null}
  1642. */
  1643. /**
  1644. * This method will remove the hooks set up for a previous pipe() call.
  1645. * @function external:Duplex#unpipe
  1646. * @param {Writable} [destination] The destination for writing data
  1647. * @return {null}
  1648. */
  1649. /**
  1650. * This is useful in certain cases where a stream is being consumed by a parser, which needs to "un-consume" some data that it has optimistically pulled out of the source, so that the stream can be passed on to some other party.
  1651. * @function external:Duplex#unshift
  1652. * @param {(Buffer|string)} chunk Chunk of data to unshift onto the read queue.
  1653. * @return {null}
  1654. */
  1655. /**
  1656. * Versions of Node prior to v0.10 had streams that did not implement the entire Streams API as it is today. (See "Compatibility" below for more information.)
  1657. * @function external:Duplex#wrap
  1658. * @param {Stream} stream An "old style" readable stream.
  1659. * @return {null}
  1660. */
  1661. /**
  1662. * This method writes some data to the underlying system, and calls the supplied callback once the data has been fully handled.
  1663. * @function external:Duplex#write
  1664. * @param {(string|Buffer)} chunk The data to write
  1665. * @param {string} encoding The encoding, if chunk is a String
  1666. * @param {function} callback Callback for when this chunk of data is flushed
  1667. * @return {boolean}
  1668. */
  1669. /**
  1670. * Call this method when no more data will be written to the stream. If supplied, the callback is attached as a listener on the finish event.
  1671. * @function external:Duplex#end
  1672. * @param {(string|Buffer)} chunk The data to write
  1673. * @param {string} encoding The encoding, if chunk is a String
  1674. * @param {function} callback Callback for when this chunk of data is flushed
  1675. * @return {null}
  1676. */
  1677. /**
  1678. * GridStoreStream stream data event, fired for each document in the cursor.
  1679. *
  1680. * @event GridStoreStream#data
  1681. * @type {object}
  1682. */
  1683. /**
  1684. * GridStoreStream stream end event
  1685. *
  1686. * @event GridStoreStream#end
  1687. * @type {null}
  1688. */
  1689. /**
  1690. * GridStoreStream stream close event
  1691. *
  1692. * @event GridStoreStream#close
  1693. * @type {null}
  1694. */
  1695. /**
  1696. * GridStoreStream stream readable event
  1697. *
  1698. * @event GridStoreStream#readable
  1699. * @type {null}
  1700. */
  1701. /**
  1702. * GridStoreStream stream drain event
  1703. *
  1704. * @event GridStoreStream#drain
  1705. * @type {null}
  1706. */
  1707. /**
  1708. * GridStoreStream stream finish event
  1709. *
  1710. * @event GridStoreStream#finish
  1711. * @type {null}
  1712. */
  1713. /**
  1714. * GridStoreStream stream pipe event
  1715. *
  1716. * @event GridStoreStream#pipe
  1717. * @type {null}
  1718. */
  1719. /**
  1720. * GridStoreStream stream unpipe event
  1721. *
  1722. * @event GridStoreStream#unpipe
  1723. * @type {null}
  1724. */
  1725. /**
  1726. * GridStoreStream stream error event
  1727. *
  1728. * @event GridStoreStream#error
  1729. * @type {null}
  1730. */
  1731. /**
  1732. * @ignore
  1733. */
  1734. module.exports = GridStore;