Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

grid_store.js 64KB

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