Skip to content

Commit 15edb0c

Browse files
committed
Merge pull request #148 from taylorstine/encode_uri
Added encoding to user name and password
2 parents 8fda77c + 97cf00b commit 15edb0c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

ExportAdapter.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ ExportAdapter.prototype.connect = function() {
3434
return this.connectionPromise;
3535
}
3636

37+
//http://regexr.com/3cn6m
38+
if (!this.mongoURI.match(/^mongodb:\/\/((.+):(.+)@)?([^:@]+):([^:]+)\/(.+?)$/gm)) {
39+
throw new Error("Invalid mongoURI: " + this.mongoURI)
40+
}
41+
var usernameStart = this.mongoURI.indexOf('://') + 3;
42+
var lastAtIndex = this.mongoURI.lastIndexOf('@');
43+
var encodedMongoURI = this.mongoURI;
44+
var split = null;
45+
if (lastAtIndex > 0) {
46+
split = this.mongoURI.slice(usernameStart, lastAtIndex).split(':');
47+
encodedMongoURI = this.mongoURI.slice(0, usernameStart) + encodeURIComponent(split[0]) + ':' + encodeURIComponent(split[1]) + this.mongoURI.slice(lastAtIndex);
48+
}
49+
3750
this.connectionPromise = Promise.resolve().then(() => {
38-
return MongoClient.connect(this.mongoURI);
51+
return MongoClient.connect(encodedMongoURI, {uri_decode_auth:true});
3952
}).then((db) => {
4053
this.db = db;
4154
});

0 commit comments

Comments
 (0)