Skip to content

Commit 8d9bc7d

Browse files
authored
Merge pull request #13850 from meteor/italo/npm-mongo-legacy
Handling MongoCompatibilityError when connect into mongodb driver 6.x
2 parents 28cd192 + 4b8aaa2 commit 8d9bc7d

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

packages/mongo/tests/mongo_livedata_tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,9 +4298,10 @@ Tinytest.addAsync(
42984298
await Collection.updateAsync({ _id: 'a' }, { $set: { num: 1 } });
42994299
await Collection.updateAsync({ _id: 'b' }, { $set: { num: 2 } });
43004300

4301+
if(Meteor.isClient) Meteor._sleepForMs(100); // wait for async operations to complete
43014302
items = await Collection.find().fetchAsync();
43024303
itemIds = items.map(_item => _item.num);
4303-
4304+
43044305
test.equal(itemIds, [1, 2]);
43054306

43064307
await Collection.removeAsync({ _id: 'a' });

packages/npm-mongo/wrapper.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
const useLegacyMongo = !!Package['npm-mongo-legacy'];
1+
const { MongoClient, MongoCompatibilityError } = Npm.require('mongodb');
22

3+
function connect(client) {
4+
return client.connect()
5+
.catch(error => {
6+
if (error.cause instanceof MongoCompatibilityError && error.message.includes('maximum wire version')) {
7+
console.warn(`[DEPRECATION] Legacy MongoDB version detected, using mongo-legacy package: ${error.message}
8+
Warning: MongoDB versions <= 3.6 are deprecated. Some Meteor features may not work properly with this version.
9+
It is recommended to use MongoDB >= 4.`);
10+
if (!Package['npm-mongo-legacy']) {
11+
throw new Error('Please, install npm-mongo-legacy package to use this version of MongoDB running "meteor add npm-mongo-legacy", then move the listed package inside .meteor/packages to the top.');
12+
}
13+
return false
14+
} else {
15+
throw new Error(`Failed to initialize Meteor's npm-mongo package: ${error}`);
16+
}
17+
})
18+
}
19+
20+
if (process.env.MONGO_URL && (/^mongodb(\+srv)?:\/\//.test(process.env.MONGO_URL))) {
21+
try {
22+
connect(new MongoClient(process.env.MONGO_URL)).then(client => {
23+
if (client) client.close();
24+
});
25+
} catch (e) {
26+
console.warn('Invalid MongoDB connection string in MONGO_URL:', process.env.MONGO_URL);
27+
}
28+
}
29+
30+
const useLegacyMongo = !!Package['npm-mongo-legacy']
331
const oldNoDeprecationValue = process.noDeprecation;
32+
33+
useLegacyMongo && console.log('WARN: npm-mongo-legacy package detected, using package for mongo <= 3.6');
34+
435
try {
536
// Silence deprecation warnings introduced in a patch update to mongodb:
637
// https://github.com/meteor/meteor/pull/9942#discussion_r218564879
@@ -14,4 +45,4 @@ try {
1445

1546
NpmModuleMongodbVersion = useLegacyMongo
1647
? Package['npm-mongo-legacy'].NpmModuleMongodbVersion
17-
: Npm.require('mongodb/package.json').version;
48+
: Npm.require('mongodb/package.json').version;

packages/tinytest/tinytest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class TestCaseResults {
7474
var frame = stack[i];
7575
// Heuristic: use the OUTERMOST line which is in a :tests.js
7676
// file (this is less likely to be a test helper function).
77-
const fileName = frame.getFileName();
77+
const fileName = frame?.getFileName ? frame.getFileName() : null;
7878
if (fileName && fileName.match(/:tests\.js/)) {
7979
doc.filename = fileName;
8080
doc.line = frame.getLineNumber();

tools/project-context.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,16 @@ Object.assign(exports.ProjectConstraintsFile.prototype, {
12171217
constraint: constraintToAdd,
12181218
trailingSpaceAndComment: ''
12191219
};
1220-
self._constraintLines.push(lineRecord);
1220+
if (constraintToAdd.package === 'npm-mongo-legacy') {
1221+
const mongoIdx = self._constraintLines.findIndex(lr => lr.constraint && lr.constraint.package === 'mongo');
1222+
if (mongoIdx > -1) {
1223+
self._constraintLines.splice(mongoIdx, 0, lineRecord);
1224+
} else {
1225+
self._constraintLines.push(lineRecord);
1226+
}
1227+
} else {
1228+
self._constraintLines.push(lineRecord);
1229+
}
12211230
self._constraintMap[constraintToAdd.package] = lineRecord;
12221231
self._modified = true;
12231232
return;

0 commit comments

Comments
 (0)