UNPKG

3.37 kBJavaScriptView Raw
1(function() {
2 var MongoUri, RE_PULL_DATABASE, RE_PULL_HOSTNAME_PORTION, RE_PULL_MONGODB_AUTH, RE_PULL_MONGODB_SCHEME, RE_PULL_OPTIONS, querystring;
3
4 querystring = require("querystring");
5
6 RE_PULL_MONGODB_SCHEME = /^mongodb:\/\/(.*)$/i;
7
8 RE_PULL_MONGODB_AUTH = /^([^:\/\?,]+):([^@\/\?,]+)@(.*)$/;
9
10 RE_PULL_HOSTNAME_PORTION = /^([^\/\?]+)(.*)$/;
11
12 RE_PULL_DATABASE = /^\/([^?]*)($|\?.*$)/;
13
14 RE_PULL_OPTIONS = /^\?(.*)$/;
15
16 exports.MongoUri = MongoUri = (function() {
17
18 function MongoUri() {
19 this.username = null;
20 this.password = null;
21 this.hosts = null;
22 this.ports = null;
23 this.database = null;
24 this.options = null;
25 }
26
27 return MongoUri;
28
29 })();
30
31 exports.parse = function(uri) {
32 var mongoUri, _ref, _ref1, _ref2;
33 if (typeof uri !== "string") {
34 throw new TypeError("Parameter 'uri' must be a string, not " + (typeof uri));
35 }
36 mongoUri = new exports.MongoUri();
37 uri = uri.trim();
38 uri = exports._pullMongodbScheme(uri);
39 _ref = exports._pullAuth(uri), mongoUri.username = _ref[0], mongoUri.password = _ref[1], uri = _ref[2];
40 _ref1 = exports._pullHostnames(uri), mongoUri.hosts = _ref1[0], mongoUri.ports = _ref1[1], uri = _ref1[2];
41 _ref2 = exports._pullDatabase(uri), mongoUri.database = _ref2[0], uri = _ref2[1];
42 mongoUri.options = exports._pullOptions(uri)[0];
43 return mongoUri;
44 };
45
46 exports._pullMongodbScheme = function(uri) {
47 var matches;
48 matches = uri.match(RE_PULL_MONGODB_SCHEME);
49 if ((matches != null ? matches.length : void 0) !== 2) {
50 throw new TypeError("uri must be mongodb scheme");
51 }
52 return matches[1];
53 };
54
55 exports._pullAuth = function(uri) {
56 var matches, password, username;
57 matches = uri.match(RE_PULL_MONGODB_AUTH);
58 if (matches === null) {
59 return [null, null, uri];
60 }
61 username = decodeURIComponent(matches[1]);
62 password = decodeURIComponent(matches[2]);
63 uri = matches[3];
64 return [username, password, uri];
65 };
66
67 exports._pullHostnames = function(uri) {
68 var host, hostname, hostnames, hosts, matches, port, ports, _i, _len, _ref;
69 matches = uri.match(RE_PULL_HOSTNAME_PORTION);
70 if ((matches != null ? matches.length : void 0) !== 3) {
71 throw new TypeError("uri must specify hostname");
72 }
73 hostnames = matches[1].split(",");
74 uri = matches[2];
75 hosts = [];
76 ports = [];
77 for (_i = 0, _len = hostnames.length; _i < _len; _i++) {
78 hostname = hostnames[_i];
79 if (hostname.indexOf(":") === -1) {
80 port = null;
81 host = hostname;
82 } else {
83 _ref = hostname.split(":"), host = _ref[0], port = _ref[1];
84 port = parseInt(port, 10);
85 }
86 hosts.push(host);
87 ports.push(port);
88 }
89 return [hosts, ports, uri];
90 };
91
92 exports._pullDatabase = function(uri) {
93 var database, matches;
94 matches = uri.match(RE_PULL_DATABASE);
95 if (matches === null) {
96 return [null, uri];
97 }
98 database = matches[1];
99 uri = matches[2];
100 database = database.length ? database : null;
101 return [database, uri];
102 };
103
104 exports._pullOptions = function(uri) {
105 var matches, options;
106 matches = uri.match(RE_PULL_OPTIONS);
107 if (matches === null) {
108 return [Object.create(null)];
109 }
110 options = querystring.parse(matches[1]);
111 return [options];
112 };
113
114}).call(this);