Skip to content

Commit 4071de4

Browse files
authoredJul 28, 2018
Merge pull request #6771 from Automattic/gh6750
refactor: use `global.$MongooseDriver` to store current driver to remove webpack warnings
2 parents 12e0d09 + 836eb53 commit 4071de4

28 files changed

+99
-79
lines changed
 

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ test/model.discriminator.test.js
55
tools/
66
test/es6/
77
test/files/
8+
dist/

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ docs/*.html
3838

3939
# Webpack output
4040
dist
41+
test/files/main.js
4142

4243
package-lock.json

‎lib/browser.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-env browser */
22

3+
require('./driver').set(require('./drivers/browser'));
4+
35
var DocumentProvider = require('./document_provider.js');
46
var PromiseProvider = require('./promise_provider');
57

‎lib/collection.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
const EventEmitter = require('events').EventEmitter;
88
const STATES = require('./connectionstate');
9-
const utils = require('./utils');
9+
const immediate = require('./helpers/immediate');
1010

1111
/**
1212
* Abstract Collection constructor
@@ -83,10 +83,7 @@ Collection.prototype.conn;
8383

8484
Collection.prototype.onOpen = function() {
8585
this.buffer = false;
86-
var _this = this;
87-
utils.immediate(function() {
88-
_this.doQueue();
89-
});
86+
immediate(() => this.doQueue());
9087
};
9188

9289
/**

‎lib/connection.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
*/
66

77
const EventEmitter = require('events').EventEmitter;
8-
const driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native';
98
const Schema = require('./schema');
10-
const Collection = require(driver + '/collection');
9+
const Collection = require('./driver').get().Collection;
1110
const STATES = require('./connectionstate');
1211
const MongooseError = require('./error');
1312
const PromiseProvider = require('./promise_provider');

‎lib/driver.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
/*!
4+
* ignore
5+
*/
6+
7+
let driver = null;
8+
9+
module.exports.get = function() {
10+
return driver;
11+
};
12+
13+
module.exports.set = function(v) {
14+
driver = v;
15+
};

‎lib/drivers/browser/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
*/
44

55
exports.Binary = require('./binary');
6+
exports.Collection = function() {
7+
throw new Error('Cannot create a collection from browser library');
8+
};
69
exports.Decimal128 = require('./decimal128');
710
exports.ObjectId = require('./objectid');
811
exports.ReadPreference = require('./ReadPreference');

‎lib/drivers/index.js

-20
This file was deleted.

‎lib/drivers/index.web.js

-5
This file was deleted.

‎lib/drivers/node-mongodb-native/collection.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const MongooseCollection = require('../../collection');
88
const Collection = require('mongodb').Collection;
99
const get = require('lodash.get');
10-
const utils = require('../../utils');
10+
const sliced = require('sliced');
1111

1212
/**
1313
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
@@ -72,7 +72,7 @@ NativeCollection.prototype.onOpen = function() {
7272
}
7373
} else {
7474
// create
75-
var opts = utils.clone(_this.opts.capped);
75+
const opts = Object.assign({}, _this.opts.capped);
7676
opts.capped = true;
7777
_this.conn.db.createCollection(_this.name, opts, callback);
7878
}
@@ -132,7 +132,7 @@ function iter(i) {
132132
if (debug) {
133133
if (typeof debug === 'function') {
134134
debug.apply(_this,
135-
[_this.name, i].concat(utils.args(args, 0, args.length - 1)));
135+
[_this.name, i].concat(sliced(args, 0, args.length - 1)));
136136
} else {
137137
this.$print(_this.name, i, args);
138138
}
@@ -224,7 +224,7 @@ function format(obj, sub) {
224224
return obj;
225225
}
226226

227-
var x = utils.clone(obj, {transform: false});
227+
var x = require('../../utils').clone(obj, {transform: false});
228228
var representation;
229229

230230
if (x.constructor.name === 'Binary') {
@@ -282,10 +282,10 @@ function format(obj, sub) {
282282
return x;
283283
}
284284

285-
return require('util')
286-
.inspect(x, false, 10, true)
287-
.replace(/\n/g, '')
288-
.replace(/\s{2,}/g, ' ');
285+
return require('util').
286+
inspect(x, false, 10, true).
287+
replace(/\n/g, '').
288+
replace(/\s{2,}/g, ' ');
289289
}
290290

291291
/**

‎lib/drivers/node-mongodb-native/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
exports.Binary = require('./binary');
6+
exports.Collection = require('./collection');
67
exports.Decimal128 = require('./decimal128');
78
exports.ObjectId = require('./objectid');
89
exports.ReadPreference = require('./ReadPreference');

‎lib/helpers/immediate.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*!
2+
* Centralize this so we can more easily work around issues with people
3+
* stubbing out `process.nextTick()` in tests using sinon:
4+
* https://github.com/sinonjs/lolex#automatically-incrementing-mocked-time
5+
* See gh-6074
6+
*/
7+
8+
module.exports = function immediate(cb) {
9+
return process.nextTick(cb);
10+
};

‎lib/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* Module dependencies.
55
*/
66

7+
const driverPath = global.MONGOOSE_DRIVER_PATH ||
8+
'./drivers/node-mongodb-native';
9+
require('./driver').set(require(driverPath));
10+
711
const Schema = require('./schema');
812
const SchemaType = require('./schematype');
913
const VirtualType = require('./virtualtype');

‎lib/model.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const cast = require('./cast');
2727
const castUpdate = require('./helpers/query/castUpdate');
2828
const discriminator = require('./helpers/model/discriminator');
2929
const getDiscriminatorByValue = require('./queryhelpers').getDiscriminatorByValue;
30+
const immediate = require('./helpers/immediate');
3031
const internalToObjectOptions = require('./options').internalToObjectOptions;
3132
const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
3233
const get = require('lodash.get');
@@ -824,7 +825,7 @@ Model.prototype.remove = function remove(options, fn) {
824825

825826
Model.prototype.$__remove = function $__remove(options, cb) {
826827
if (this.$__.isDeleted) {
827-
return utils.immediate(() => cb(null, this));
828+
return immediate(() => cb(null, this));
828829
}
829830

830831
var where = this.$__where();
@@ -1225,7 +1226,7 @@ function _ensureIndexes(model, options, callback) {
12251226
}
12261227

12271228
if (!indexes.length) {
1228-
utils.immediate(function() {
1229+
immediate(function() {
12291230
done();
12301231
});
12311232
return;
@@ -1271,7 +1272,7 @@ function _ensureIndexes(model, options, callback) {
12711272
}));
12721273
};
12731274

1274-
utils.immediate(function() {
1275+
immediate(function() {
12751276
// If buffering is off, do this manually.
12761277
if (options._automatic && !model.collection.collection) {
12771278
model.collection.addQueue(create, []);
@@ -3487,7 +3488,7 @@ function populate(model, docs, options, callback) {
34873488
const modelsMap = getModelsMapForPopulate(model, docs, options);
34883489

34893490
if (modelsMap instanceof Error) {
3490-
return utils.immediate(function() {
3491+
return immediate(function() {
34913492
callback(modelsMap);
34923493
});
34933494
}

‎lib/query.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const CastError = require('./error/cast');
88
const ObjectParameterError = require('./error/objectParameter');
99
const QueryCursor = require('./cursor/QueryCursor');
10-
const ReadPreference = require('./drivers').ReadPreference;
10+
const ReadPreference = require('./driver').get().ReadPreference;
1111
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
1212
const cast = require('./cast');
1313
const castUpdate = require('./helpers/query/castUpdate');

‎lib/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const VirtualType = require('./virtualtype');
1111
const get = require('lodash.get');
1212
const getIndexes = require('./helpers/schema/getIndexes');
1313
const mpath = require('mpath');
14-
const readPref = require('./drivers').ReadPreference;
14+
const readPref = require('./driver').get().ReadPreference;
1515
const utils = require('./utils');
1616

1717
let MongooseTypes;

‎lib/schematype.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* Module dependencies.
55
*/
66

7+
var MongooseError = require('./error');
78
var $exists = require('./schema/operators/exists');
89
var $type = require('./schema/operators/type');
10+
var immediate = require('./helpers/immediate');
911
var utils = require('./utils');
10-
var MongooseError = require('./error');
12+
1113
var CastError = MongooseError.CastError;
1214
var ValidatorError = MongooseError.ValidatorError;
1315

@@ -796,15 +798,15 @@ SchemaType.prototype.doValidate = function(value, fn, scope) {
796798
}
797799
if (ok === undefined || ok) {
798800
if (--count <= 0) {
799-
utils.immediate(function() {
801+
immediate(function() {
800802
fn(null);
801803
});
802804
}
803805
} else {
804806
var ErrorConstructor = validatorProperties.ErrorConstructor || ValidatorError;
805807
err = new ErrorConstructor(validatorProperties);
806808
err.$isValidatorError = true;
807-
utils.immediate(function() {
809+
immediate(function() {
808810
fn(err);
809811
});
810812
}

‎lib/types/buffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Module dependencies.
33
*/
44

5-
var Binary = require('../drivers').Binary,
6-
utils = require('../utils');
5+
var Binary = require('../driver').get().Binary;
6+
var utils = require('../utils');
77

88
/**
99
* Mongoose Buffer constructor.

‎lib/types/decimal128.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
* @constructor ObjectId
99
*/
1010

11-
module.exports = require('../drivers').Decimal128;
11+
module.exports = require('../driver').get().Decimal128;

‎lib/types/embedded.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const Document = require('../document_provider')();
88
const EventEmitter = require('events').EventEmitter;
9+
const immediate = require('../helpers/immediate');
910
const internalToObjectOptions = require('../options').internalToObjectOptions;
1011
const utils = require('../utils');
1112

@@ -139,7 +140,7 @@ EmbeddedDocument.prototype.save = function(options, fn) {
139140
*/
140141

141142
EmbeddedDocument.prototype.$__save = function(fn) {
142-
return utils.immediate(() => fn(null, this));
143+
return immediate(() => fn(null, this));
143144
};
144145

145146
/*!

‎lib/types/objectid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @constructor ObjectId
99
*/
1010

11-
var ObjectId = require('../drivers').ObjectId;
11+
var ObjectId = require('../driver').get().ObjectId;
1212

1313
/*!
1414
* Getter for convenience with populate, see gh-6115

‎lib/types/subdocument.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var Document = require('../document');
4+
var immediate = require('../helpers/immediate');
45
var internalToObjectOptions = require('../options').internalToObjectOptions;
56
var utils = require('../utils');
67

@@ -68,7 +69,7 @@ Subdocument.prototype.save = function(options, fn) {
6869
*/
6970

7071
Subdocument.prototype.$__save = function(fn) {
71-
return utils.immediate(() => fn(null, this));
72+
return immediate(() => fn(null, this));
7273
};
7374

7475
Subdocument.prototype.$isValid = function(path) {

‎lib/utils.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ exports.deepEqual = function deepEqual(a, b) {
6060
return a.getTime() === b.getTime();
6161
}
6262

63-
if ((a instanceof ObjectId && b instanceof ObjectId) ||
64-
(a instanceof Decimal && b instanceof Decimal)) {
63+
if ((isBsonType(a, 'ObjectID') && isBsonType(b, 'ObjectID')) ||
64+
(isBsonType(a, 'Decimal128') && isBsonType(b, 'Decimal128'))) {
6565
return a.toString() === b.toString();
6666
}
6767

@@ -138,6 +138,14 @@ exports.deepEqual = function deepEqual(a, b) {
138138
return true;
139139
};
140140

141+
/*!
142+
* Get the bson type, if it exists
143+
*/
144+
145+
function isBsonType(obj, typename) {
146+
return get(obj, '_bsontype', void 0) === typename;
147+
}
148+
141149
/*!
142150
* Get the last element of an array
143151
*/
@@ -195,7 +203,7 @@ exports.clone = function clone(obj, options) {
195203
if (obj instanceof ObjectId) {
196204
return new ObjectId(obj.id);
197205
}
198-
if (obj instanceof Decimal) {
206+
if (isBsonType(obj, 'Decimal128')) {
199207
if (options && options.flattenDecimals) {
200208
return obj.toJSON();
201209
}
@@ -882,17 +890,6 @@ exports.each = function(arr, fn) {
882890
}
883891
};
884892

885-
/*!
886-
* Centralize this so we can more easily work around issues with people
887-
* stubbing out `process.nextTick()` in tests using sinon:
888-
* https://github.com/sinonjs/lolex#automatically-incrementing-mocked-time
889-
* See gh-6074
890-
*/
891-
892-
exports.immediate = function immediate(cb) {
893-
return process.nextTick(cb);
894-
};
895-
896893
/*!
897894
* ignore
898895
*/

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"nyc": "11.8.0",
5555
"power-assert": "1.4.1",
5656
"q": "1.5.1",
57-
"rimraf": "2.6.2",
5857
"semver": "5.5.0",
5958
"tbd": "0.6.4",
6059
"uuid": "2.0.3",

‎test/browser.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const exec = require('child_process').exec;
1515
*/
1616
describe('browser', function() {
1717
it('require() works with no other require calls (gh-5842)', function(done) {
18-
exec('node --eval "require(\'./lib/browserDocument\')"', done);
18+
exec('node --eval "require(\'./lib/browser\')"', done);
1919
});
2020

2121
it('document works (gh-4987)', function(done) {

‎test/common.js

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* Module dependencies.
55
*/
66

7-
Error.stackTraceLimit = 10;
8-
97
const Server = require('mongodb-topology-manager').Server;
108
const mongoose = require('../');
119
const Collection = mongoose.Collection;

‎test/files/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<script type="text/javascript" src="main.js"></script>
4+
</head>
5+
6+
<body>
7+
Test
8+
</body>
9+
</html>

‎test/webpack.test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const assert = require('assert');
4-
const rimraf = require('rimraf');
54
const utils = require('../lib/utils');
65
const semver = require('semver');
76

@@ -19,6 +18,11 @@ describe('webpack', function() {
1918
// acquit:ignore:end
2019
const config = {
2120
entry: ['./test/files/sample.js'],
21+
// acquit:ignore:start
22+
output: {
23+
path: `${__dirname}/files`
24+
},
25+
// acquit:ignore:end
2226
module: {
2327
rules: [
2428
{
@@ -38,18 +42,18 @@ describe('webpack', function() {
3842
net: 'empty',
3943
tls: 'empty'
4044
},
41-
target: 'web'
45+
target: 'web',
46+
mode: 'production'
4247
};
4348
// acquit:ignore:start
4449
webpack(config, utils.tick(function(error, stats) {
4550
assert.ifError(error);
4651
assert.deepEqual(stats.compilation.errors, []);
52+
// Avoid expressions in `require()` because that scares webpack (gh-6705)
53+
assert.ok(!stats.compilation.warnings.
54+
find(msg => msg.toString().startsWith('ModuleDependencyWarning:')));
4755
done();
4856
}));
4957
// acquit:ignore:end
5058
});
51-
52-
after(function(done) {
53-
rimraf('./dist', done);
54-
});
5559
});

0 commit comments

Comments
 (0)
Please sign in to comment.