mongodb 2.2.36 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ 'use strict';
2
+
1
3
  var stream = require('stream'),
2
4
  util = require('util');
3
5
 
@@ -161,8 +163,7 @@ GridFSBucketReadStream.prototype.abort = function(callback) {
161
163
 
162
164
  function throwIfInitialized(self) {
163
165
  if (self.s.init) {
164
- throw new Error('You cannot change options after the stream has entered' +
165
- 'flowing mode!');
166
+ throw new Error('You cannot change options after the stream has entered' + 'flowing mode!');
166
167
  }
167
168
  }
168
169
 
@@ -194,18 +195,15 @@ function doRead(_this) {
194
195
 
195
196
  var bytesRemaining = _this.s.file.length - _this.s.bytesRead;
196
197
  var expectedN = _this.s.expected++;
197
- var expectedLength = Math.min(_this.s.file.chunkSize,
198
- bytesRemaining);
198
+ var expectedLength = Math.min(_this.s.file.chunkSize, bytesRemaining);
199
199
 
200
200
  if (doc.n > expectedN) {
201
- var errmsg = 'ChunkIsMissing: Got unexpected n: ' + doc.n +
202
- ', expected: ' + expectedN;
201
+ var errmsg = 'ChunkIsMissing: Got unexpected n: ' + doc.n + ', expected: ' + expectedN;
203
202
  return __handleError(_this, new Error(errmsg));
204
203
  }
205
204
 
206
205
  if (doc.n < expectedN) {
207
- errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n +
208
- ', expected: ' + expectedN;
206
+ errmsg = 'ExtraChunk: Got unexpected n: ' + doc.n + ', expected: ' + expectedN;
209
207
  return __handleError(_this, new Error(errmsg));
210
208
  }
211
209
 
@@ -217,8 +215,8 @@ function doRead(_this) {
217
215
  return __handleError(_this, new Error(errmsg));
218
216
  }
219
217
 
220
- errmsg = 'ChunkIsWrongSize: Got unexpected length: ' +
221
- buf.length + ', expected: ' + expectedLength;
218
+ errmsg =
219
+ 'ChunkIsWrongSize: Got unexpected length: ' + buf.length + ', expected: ' + expectedLength;
222
220
  return __handleError(_this, new Error(errmsg));
223
221
  }
224
222
 
@@ -241,10 +239,8 @@ function doRead(_this) {
241
239
  }
242
240
 
243
241
  // If the remaining amount of data left is < chunkSize read the right amount of data
244
- if (_this.s.options.end && (
245
- (_this.s.options.end - _this.s.bytesToSkip) < buf.length
246
- )) {
247
- sliceEnd = (_this.s.options.end - _this.s.bytesToSkip);
242
+ if (_this.s.options.end && _this.s.options.end - _this.s.bytesToSkip < buf.length) {
243
+ sliceEnd = _this.s.options.end - _this.s.bytesToSkip;
248
244
  }
249
245
 
250
246
  if (sliceStart != null || sliceEnd != null) {
@@ -252,7 +248,7 @@ function doRead(_this) {
252
248
  }
253
249
 
254
250
  _this.push(buf);
255
- })
251
+ });
256
252
  }
257
253
 
258
254
  /**
@@ -276,8 +272,7 @@ function init(self) {
276
272
  return __handleError(self, error);
277
273
  }
278
274
  if (!doc) {
279
- var identifier = self.s.filter._id ?
280
- self.s.filter._id.toString() : self.s.filter.filename;
275
+ var identifier = self.s.filter._id ? self.s.filter._id.toString() : self.s.filter.filename;
281
276
  var errmsg = 'FileNotFound: file ' + identifier + ' was not found';
282
277
  var err = new Error(errmsg);
283
278
  err.code = 'ENOENT';
@@ -306,10 +301,10 @@ function init(self) {
306
301
  // Currently (MongoDB 3.4.4) skip function does not support the index,
307
302
  // it needs to retrieve all the documents first and then skip them. (CS-25811)
308
303
  // As work around we use $gte on the "n" field.
309
- if (self.s.options && self.s.options.start != null){
304
+ if (self.s.options && self.s.options.start != null) {
310
305
  var skip = Math.floor(self.s.options.start / doc.chunkSize);
311
- if (skip > 0){
312
- filter["n"] = {"$gte": skip};
306
+ if (skip > 0) {
307
+ filter['n'] = { $gte: skip };
313
308
  }
314
309
  }
315
310
  self.s.cursor = self.s.chunks.find(filter).sort({ n: 1 });
@@ -320,8 +315,7 @@ function init(self) {
320
315
 
321
316
  self.s.expectedEnd = Math.ceil(doc.length / doc.chunkSize);
322
317
  self.s.file = doc;
323
- self.s.bytesToTrim = handleEndOption(self, doc, self.s.cursor,
324
- self.s.options);
318
+ self.s.bytesToTrim = handleEndOption(self, doc, self.s.cursor, self.s.options);
325
319
  self.emit('file', doc);
326
320
  });
327
321
  }
@@ -342,7 +336,7 @@ function waitForFile(_this, callback) {
342
336
 
343
337
  _this.once('file', function() {
344
338
  callback();
345
- })
339
+ });
346
340
  }
347
341
 
348
342
  /**
@@ -352,20 +346,30 @@ function waitForFile(_this, callback) {
352
346
  function handleStartOption(stream, doc, options) {
353
347
  if (options && options.start != null) {
354
348
  if (options.start > doc.length) {
355
- throw new Error('Stream start (' + options.start + ') must not be ' +
356
- 'more than the length of the file (' + doc.length +')');
349
+ throw new Error(
350
+ 'Stream start (' +
351
+ options.start +
352
+ ') must not be ' +
353
+ 'more than the length of the file (' +
354
+ doc.length +
355
+ ')'
356
+ );
357
357
  }
358
358
  if (options.start < 0) {
359
- throw new Error('Stream start (' + options.start + ') must not be ' +
360
- 'negative');
359
+ throw new Error('Stream start (' + options.start + ') must not be ' + 'negative');
361
360
  }
362
361
  if (options.end != null && options.end < options.start) {
363
- throw new Error('Stream start (' + options.start + ') must not be ' +
364
- 'greater than stream end (' + options.end + ')');
362
+ throw new Error(
363
+ 'Stream start (' +
364
+ options.start +
365
+ ') must not be ' +
366
+ 'greater than stream end (' +
367
+ options.end +
368
+ ')'
369
+ );
365
370
  }
366
371
 
367
- stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) *
368
- doc.chunkSize;
372
+ stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize;
369
373
  stream.s.expected = Math.floor(options.start / doc.chunkSize);
370
374
 
371
375
  return options.start - stream.s.bytesRead;
@@ -379,24 +383,26 @@ function handleStartOption(stream, doc, options) {
379
383
  function handleEndOption(stream, doc, cursor, options) {
380
384
  if (options && options.end != null) {
381
385
  if (options.end > doc.length) {
382
- throw new Error('Stream end (' + options.end + ') must not be ' +
383
- 'more than the length of the file (' + doc.length +')')
386
+ throw new Error(
387
+ 'Stream end (' +
388
+ options.end +
389
+ ') must not be ' +
390
+ 'more than the length of the file (' +
391
+ doc.length +
392
+ ')'
393
+ );
384
394
  }
385
395
  if (options.start < 0) {
386
- throw new Error('Stream end (' + options.end + ') must not be ' +
387
- 'negative');
396
+ throw new Error('Stream end (' + options.end + ') must not be ' + 'negative');
388
397
  }
389
398
 
390
- var start = options.start != null ?
391
- Math.floor(options.start / doc.chunkSize) :
392
- 0;
399
+ var start = options.start != null ? Math.floor(options.start / doc.chunkSize) : 0;
393
400
 
394
401
  cursor.limit(Math.ceil(options.end / doc.chunkSize) - start);
395
402
 
396
403
  stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize);
397
404
 
398
- return (Math.ceil(options.end / doc.chunkSize) * doc.chunkSize) -
399
- options.end;
405
+ return Math.ceil(options.end / doc.chunkSize) * doc.chunkSize - options.end;
400
406
  }
401
407
  }
402
408
 
@@ -1,9 +1,12 @@
1
+ 'use strict';
2
+
1
3
  var Emitter = require('events').EventEmitter;
2
4
  var GridFSBucketReadStream = require('./download');
3
5
  var GridFSBucketWriteStream = require('./upload');
4
6
  var shallowClone = require('../utils').shallowClone;
5
7
  var toError = require('../utils').toError;
6
8
  var util = require('util');
9
+ var executeOperation = require('../utils').executeOperation;
7
10
 
8
11
  var DEFAULT_GRIDFS_BUCKET_OPTIONS = {
9
12
  bucketName: 'fs',
@@ -48,8 +51,7 @@ function GridFSBucket(db, options) {
48
51
  _filesCollection: db.collection(options.bucketName + '.files'),
49
52
  checkedIndexes: false,
50
53
  calledOpenUploadStream: false,
51
- promiseLibrary: db.s.promiseLibrary ||
52
- (typeof global.Promise == 'function' ? global.Promise : require('es6-promise').Promise)
54
+ promiseLibrary: db.s.promiseLibrary || Promise
53
55
  };
54
56
  }
55
57
 
@@ -141,8 +143,13 @@ GridFSBucket.prototype.openDownloadStream = function(id, options) {
141
143
  end: options && options.end
142
144
  };
143
145
 
144
- return new GridFSBucketReadStream(this.s._chunksCollection,
145
- this.s._filesCollection, this.s.options.readPreference, filter, options);
146
+ return new GridFSBucketReadStream(
147
+ this.s._chunksCollection,
148
+ this.s._filesCollection,
149
+ this.s.options.readPreference,
150
+ filter,
151
+ options
152
+ );
146
153
  };
147
154
 
148
155
  /**
@@ -153,19 +160,8 @@ GridFSBucket.prototype.openDownloadStream = function(id, options) {
153
160
  */
154
161
 
155
162
  GridFSBucket.prototype.delete = function(id, callback) {
156
- if (typeof callback === 'function') {
157
- return _delete(this, id, callback);
158
- }
159
-
160
- var _this = this;
161
- return new this.s.promiseLibrary(function(resolve, reject) {
162
- _delete(_this, id, function(error, res) {
163
- if (error) {
164
- reject(error);
165
- } else {
166
- resolve(res);
167
- }
168
- });
163
+ return executeOperation(this.s.db.s.topology, _delete, [this, id, callback], {
164
+ skipSessions: true
169
165
  });
170
166
  };
171
167
 
@@ -271,8 +267,13 @@ GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) {
271
267
  start: options && options.start,
272
268
  end: options && options.end
273
269
  };
274
- return new GridFSBucketReadStream(this.s._chunksCollection,
275
- this.s._filesCollection, this.s.options.readPreference, filter, options);
270
+ return new GridFSBucketReadStream(
271
+ this.s._chunksCollection,
272
+ this.s._filesCollection,
273
+ this.s.options.readPreference,
274
+ filter,
275
+ options
276
+ );
276
277
  };
277
278
 
278
279
  /**
@@ -284,19 +285,8 @@ GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) {
284
285
  */
285
286
 
286
287
  GridFSBucket.prototype.rename = function(id, filename, callback) {
287
- if (typeof callback === 'function') {
288
- return _rename(this, id, filename, callback);
289
- }
290
-
291
- var _this = this;
292
- return new this.s.promiseLibrary(function(resolve, reject) {
293
- _rename(_this, id, filename, function(error, res) {
294
- if (error) {
295
- reject(error);
296
- } else {
297
- resolve(res);
298
- }
299
- });
288
+ return executeOperation(this.s.db.s.topology, _rename, [this, id, filename, callback], {
289
+ skipSessions: true
300
290
  });
301
291
  };
302
292
 
@@ -325,19 +315,8 @@ function _rename(_this, id, filename, callback) {
325
315
  */
326
316
 
327
317
  GridFSBucket.prototype.drop = function(callback) {
328
- if (typeof callback === 'function') {
329
- return _drop(this, callback);
330
- }
331
-
332
- var _this = this;
333
- return new this.s.promiseLibrary(function(resolve, reject) {
334
- _drop(_this, function(error, res) {
335
- if (error) {
336
- reject(error);
337
- } else {
338
- resolve(res);
339
- }
340
- });
318
+ return executeOperation(this.s.db.s.topology, _drop, [this, callback], {
319
+ skipSessions: true
341
320
  });
342
321
  };
343
322
 
@@ -1,3 +1,5 @@
1
+ 'use strict';
2
+
1
3
  var core = require('mongodb-core');
2
4
  var crypto = require('crypto');
3
5
  var stream = require('stream');
@@ -108,21 +110,21 @@ GridFSBucketWriteStream.prototype.write = function(chunk, encoding, callback) {
108
110
  GridFSBucketWriteStream.prototype.abort = function(callback) {
109
111
  if (this.state.streamEnd) {
110
112
  var error = new Error('Cannot abort a stream that has already completed');
111
- if (typeof callback == 'function') {
113
+ if (typeof callback === 'function') {
112
114
  return callback(error);
113
115
  }
114
116
  return this.state.promiseLibrary.reject(error);
115
117
  }
116
118
  if (this.state.aborted) {
117
119
  error = new Error('Cannot call abort() on a stream twice');
118
- if (typeof callback == 'function') {
120
+ if (typeof callback === 'function') {
119
121
  return callback(error);
120
122
  }
121
123
  return this.state.promiseLibrary.reject(error);
122
124
  }
123
125
  this.state.aborted = true;
124
126
  this.chunks.deleteMany({ files_id: this.id }, function(error) {
125
- if(typeof callback == 'function') callback(error);
127
+ if (typeof callback === 'function') callback(error);
126
128
  });
127
129
  };
128
130
 
@@ -139,10 +141,10 @@ GridFSBucketWriteStream.prototype.abort = function(callback) {
139
141
 
140
142
  GridFSBucketWriteStream.prototype.end = function(chunk, encoding, callback) {
141
143
  var _this = this;
142
- if(typeof chunk == 'function') {
143
- callback = chunk, chunk = null, encoding = null;
144
- } else if(typeof encoding == 'function') {
145
- callback = encoding, encoding = null;
144
+ if (typeof chunk === 'function') {
145
+ (callback = chunk), (chunk = null), (encoding = null);
146
+ } else if (typeof encoding === 'function') {
147
+ (callback = encoding), (encoding = null);
146
148
  }
147
149
 
148
150
  if (checkAborted(this, callback)) {
@@ -222,8 +224,7 @@ function checkChunksIndex(_this, callback) {
222
224
  indexes.forEach(function(index) {
223
225
  if (index.key) {
224
226
  var keys = Object.keys(index.key);
225
- if (keys.length === 2 && index.key.files_id === 1 &&
226
- index.key.n === 1) {
227
+ if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) {
227
228
  hasChunksIndex = true;
228
229
  }
229
230
  }
@@ -254,16 +255,21 @@ function checkChunksIndex(_this, callback) {
254
255
  */
255
256
 
256
257
  function checkDone(_this, callback) {
257
- if(_this.done) return true;
258
- if (_this.state.streamEnd &&
259
- _this.state.outstandingRequests === 0 &&
260
- !_this.state.errored) {
258
+ if (_this.done) return true;
259
+ if (_this.state.streamEnd && _this.state.outstandingRequests === 0 && !_this.state.errored) {
261
260
  // Set done so we dont' trigger duplicate createFilesDoc
262
261
  _this.done = true;
263
262
  // Create a new files doc
264
- var filesDoc = createFilesDoc(_this.id, _this.length, _this.chunkSizeBytes,
265
- _this.md5.digest('hex'), _this.filename, _this.options.contentType,
266
- _this.options.aliases, _this.options.metadata);
263
+ var filesDoc = createFilesDoc(
264
+ _this.id,
265
+ _this.length,
266
+ _this.chunkSizeBytes,
267
+ _this.md5.digest('hex'),
268
+ _this.filename,
269
+ _this.options.contentType,
270
+ _this.options.aliases,
271
+ _this.options.metadata
272
+ );
267
273
 
268
274
  if (checkAborted(_this, callback)) {
269
275
  return false;
@@ -315,8 +321,7 @@ function checkIndexes(_this, callback) {
315
321
  var hasFileIndex = false;
316
322
  indexes.forEach(function(index) {
317
323
  var keys = Object.keys(index.key);
318
- if (keys.length === 2 && index.key.filename === 1 &&
319
- index.key.uploadDate === 1) {
324
+ if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) {
320
325
  hasFileIndex = true;
321
326
  }
322
327
  });
@@ -346,8 +351,7 @@ function checkIndexes(_this, callback) {
346
351
  * @ignore
347
352
  */
348
353
 
349
- function createFilesDoc(_id, length, chunkSize, md5, filename, contentType,
350
- aliases, metadata) {
354
+ function createFilesDoc(_id, length, chunkSize, md5, filename, contentType, aliases, metadata) {
351
355
  var ret = {
352
356
  _id: _id,
353
357
  length: length,
@@ -381,8 +385,7 @@ function doWrite(_this, chunk, encoding, callback) {
381
385
  return false;
382
386
  }
383
387
 
384
- var inputBuf = (Buffer.isBuffer(chunk)) ?
385
- chunk : new Buffer(chunk, encoding);
388
+ var inputBuf = Buffer.isBuffer(chunk) ? chunk : new Buffer(chunk, encoding);
386
389
 
387
390
  _this.length += inputBuf.length;
388
391
 
@@ -407,8 +410,7 @@ function doWrite(_this, chunk, encoding, callback) {
407
410
  var outstandingRequests = 0;
408
411
  while (inputBufRemaining > 0) {
409
412
  var inputBufPos = inputBuf.length - inputBufRemaining;
410
- inputBuf.copy(_this.bufToStore, _this.pos,
411
- inputBufPos, inputBufPos + numToCopy);
413
+ inputBuf.copy(_this.bufToStore, _this.pos, inputBufPos, inputBufPos + numToCopy);
412
414
  _this.pos += numToCopy;
413
415
  spaceRemaining -= numToCopy;
414
416
  if (spaceRemaining === 0) {
@@ -427,7 +429,7 @@ function doWrite(_this, chunk, encoding, callback) {
427
429
  }
428
430
  --_this.state.outstandingRequests;
429
431
  --outstandingRequests;
430
-
432
+
431
433
  if (!outstandingRequests) {
432
434
  _this.emit('drain', doc);
433
435
  callback && callback();
@@ -518,7 +520,7 @@ function writeRemnant(_this, callback) {
518
520
 
519
521
  function checkAborted(_this, callback) {
520
522
  if (_this.state.aborted) {
521
- if(typeof callback == 'function') {
523
+ if (typeof callback === 'function') {
522
524
  callback(new Error('this stream has been aborted'));
523
525
  }
524
526
  return true;
package/lib/metadata.js CHANGED
@@ -1,35 +1,38 @@
1
+ 'use strict';
2
+
1
3
  var f = require('util').format;
2
4
 
3
5
  var Define = function(name, object, stream) {
4
6
  this.name = name;
5
7
  this.object = object;
6
- this.stream = typeof stream == 'boolean' ? stream : false;
8
+ this.stream = typeof stream === 'boolean' ? stream : false;
7
9
  this.instrumentations = {};
8
- }
10
+ };
9
11
 
10
12
  Define.prototype.classMethod = function(name, options) {
11
13
  var keys = Object.keys(options).sort();
12
14
  var key = generateKey(keys, options);
13
15
 
14
16
  // Add a list of instrumentations
15
- if(this.instrumentations[key] == null) {
17
+ if (this.instrumentations[key] == null) {
16
18
  this.instrumentations[key] = {
17
- methods: [], options: options
18
- }
19
+ methods: [],
20
+ options: options
21
+ };
19
22
  }
20
23
 
21
24
  // Push to list of method for this instrumentation
22
25
  this.instrumentations[key].methods.push(name);
23
- }
26
+ };
24
27
 
25
28
  var generateKey = function(keys, options) {
26
29
  var parts = [];
27
- for(var i = 0; i < keys.length; i++) {
30
+ for (var i = 0; i < keys.length; i++) {
28
31
  parts.push(f('%s=%s', keys[i], options[keys[i]]));
29
32
  }
30
33
 
31
34
  return parts.join();
32
- }
35
+ };
33
36
 
34
37
  Define.prototype.staticMethod = function(name, options) {
35
38
  options.static = true;
@@ -37,28 +40,31 @@ Define.prototype.staticMethod = function(name, options) {
37
40
  var key = generateKey(keys, options);
38
41
 
39
42
  // Add a list of instrumentations
40
- if(this.instrumentations[key] == null) {
43
+ if (this.instrumentations[key] == null) {
41
44
  this.instrumentations[key] = {
42
- methods: [], options: options
43
- }
45
+ methods: [],
46
+ options: options
47
+ };
44
48
  }
45
49
 
46
50
  // Push to list of method for this instrumentation
47
51
  this.instrumentations[key].methods.push(name);
48
- }
52
+ };
49
53
 
50
54
  Define.prototype.generate = function() {
51
55
  // Generate the return object
52
56
  var object = {
53
- name: this.name, obj: this.object, stream: this.stream,
57
+ name: this.name,
58
+ obj: this.object,
59
+ stream: this.stream,
54
60
  instrumentations: []
55
- }
61
+ };
56
62
 
57
- for(var name in this.instrumentations) {
63
+ for (var name in this.instrumentations) {
58
64
  object.instrumentations.push(this.instrumentations[name]);
59
65
  }
60
66
 
61
67
  return object;
62
- }
68
+ };
63
69
 
64
70
  module.exports = Define;