Skip to content

Commit 36adaed

Browse files
committed
use mongodb-uri to parse uri param
1 parent 49506a1 commit 36adaed

File tree

2 files changed

+106
-104
lines changed

2 files changed

+106
-104
lines changed

index.js

Lines changed: 102 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
21
/**
32
* Module dependencies.
43
*/
54

6-
var uid2 = require('uid2')
7-
, mubsub = require('mubsub')
8-
, msgpack = require('msgpack-js')
9-
, Adapter = require('socket.io-adapter')
10-
, debug = require('debug')('socket.io-mongo')
11-
;
12-
13-
var URI_MATCH = /(?:mongodb\:\/\/)?(?:(.*)\:(.*)\@)?(.*)\:(\d+)(?:\/(.*))?/i;
5+
var uid2 = require('uid2')
6+
, mubsub = require('mubsub')
7+
, msgpack = require('msgpack-js')
8+
, Adapter = require('socket.io-adapter')
9+
, debug = require('debug')('socket.io-mongo')
10+
, mongodbUri = require('mongodb-uri');
11+
;
1412

1513
/**
1614
* Module exports.
@@ -26,97 +24,100 @@ module.exports = adapter;
2624
* @api public
2725
*/
2826

29-
function adapter(uri, opts){
30-
opts = opts || {};
31-
32-
// handle options only
33-
if ('object' == typeof uri) {
34-
opts = uri;
35-
uri = null;
36-
}
37-
38-
// handle uri string
39-
uri = (uri || '').match(URI_MATCH);
40-
if (uri) {
41-
opts.username = uri[1];
42-
opts.password = uri[2];
43-
opts.host = uri[3];
44-
opts.port = uri[4];
45-
opts.db = uri[5];
46-
}
47-
48-
// opts
49-
var socket = opts.socket;
50-
var creds = (opts.username && opts.password) ? opts.username + ':' + opts.password + '@' : '';
51-
var host = opts.host || '127.0.0.1';
52-
var port = Number(opts.port || 27017);
53-
var db = opts.db || 'mubsub';
54-
55-
var client = opts.client;
56-
var key = opts.key || 'socket.io';
57-
58-
// init clients if needed
59-
if (!client) client = socket ? mubsub(socket) : mubsub('mongodb://' + creds + host + ':' + port + '/' + db);
60-
61-
// this server's key
62-
var uid = uid2(6);
63-
64-
channel = client.channel(key);
65-
66-
/**
67-
* Adapter constructor.
68-
*
69-
* @param {String} namespace name
70-
* @api public
71-
*/
72-
73-
function Mongo(nsp){
74-
Adapter.call(this, nsp);
75-
76-
channel.subscribe(key, this.onmessage.bind(this));
77-
}
78-
79-
/**
80-
* Inherits from `Adapter`.
81-
*/
82-
83-
Mongo.prototype.__proto__ = Adapter.prototype;
84-
85-
/**
86-
* Called with a subscription message
87-
*
88-
* @api private
89-
*/
90-
91-
Mongo.prototype.onmessage = function(msg){
92-
if (uid == msg.uid || !msg.uid) return debug('ignore same uid');
93-
94-
var args = msgpack.decode(msg.data.buffer);
95-
if (args[0] && args[0].nsp === undefined)
96-
args[0].nsp = '/';
97-
98-
if (!args[0] || args[0].nsp != this.nsp.name) return debug('ignore different namespace');
99-
args.push(true);
100-
this.broadcast.apply(this, args);
101-
};
102-
103-
/**
104-
* Broadcasts a packet.
105-
*
106-
* @param {Object} packet to emit
107-
* @param {Object} options
108-
* @param {Boolean} whether the packet came from another node
109-
* @api public
110-
*/
111-
112-
Mongo.prototype.broadcast = function(packet, opts, remote){
113-
Adapter.prototype.broadcast.call(this, packet, opts);
114-
115-
if (!remote) {
116-
channel.publish(key, { uid: uid, data: msgpack.encode([packet, opts]) });
117-
}
118-
};
119-
120-
return Mongo;
27+
function adapter(uri, opts) {
28+
opts = opts || {};
29+
30+
// handle options only
31+
if ('object' == typeof uri) {
32+
opts = uri;
33+
uri = null;
34+
}
35+
36+
// handle uri string
37+
if (uri) {
38+
// Parse to url object
39+
var uriObj = mongodbUri.parse(uri);
40+
if (uriObj.username && uriObj.password) {
41+
opts.username = uriObj.username;
42+
opts.password = uriObj.password;
43+
}
44+
opts.host = uriObj.hosts[0].host;
45+
opts.port = uriObj.hosts[0].port;
46+
opts.db = uriObj.database;
47+
}
48+
49+
// opts
50+
var socket = opts.socket;
51+
var creds = (opts.username && opts.password) ? opts.username + ':' + opts.password + '@' : '';
52+
var host = opts.host || '127.0.0.1';
53+
var port = Number(opts.port || 27017);
54+
var db = opts.db || 'mubsub';
55+
56+
var client = opts.client;
57+
var key = opts.key || 'socket.io';
58+
59+
// init clients if needed
60+
if (!client) client = socket ? mubsub(socket) : mubsub('mongodb://' + creds + host + ':' + port + '/' + db);
61+
62+
// this server's key
63+
var uid = uid2(6);
64+
65+
channel = client.channel(key);
66+
67+
/**
68+
* Adapter constructor.
69+
*
70+
* @param {String} namespace name
71+
* @api public
72+
*/
73+
74+
function Mongo(nsp) {
75+
Adapter.call(this, nsp);
76+
77+
channel.subscribe(key, this.onmessage.bind(this));
78+
}
79+
80+
/**
81+
* Inherits from `Adapter`.
82+
*/
83+
84+
Mongo.prototype.__proto__ = Adapter.prototype;
85+
86+
/**
87+
* Called with a subscription message
88+
*
89+
* @api private
90+
*/
91+
92+
Mongo.prototype.onmessage = function (msg) {
93+
if (uid == msg.uid || !msg.uid) return debug('ignore same uid');
94+
95+
var args = msgpack.decode(msg.data.buffer);
96+
if (args[0] && args[0].nsp === undefined)
97+
args[0].nsp = '/';
98+
99+
if (!args[0] || args[0].nsp != this.nsp.name) return debug('ignore different namespace');
100+
args.push(true);
101+
this.broadcast.apply(this, args);
102+
};
103+
104+
/**
105+
* Broadcasts a packet.
106+
*
107+
* @param {Object} packet to emit
108+
* @param {Object} options
109+
* @param {Boolean} whether the packet came from another node
110+
* @api public
111+
*/
112+
113+
Mongo.prototype.broadcast = function (packet, opts, remote) {
114+
Adapter.prototype.broadcast.call(this, packet, opts);
115+
116+
if (!remote) {
117+
channel.publish(key, { uid: uid, data: msgpack.encode([packet, opts]) });
118+
}
119+
};
120+
121+
return Mongo;
121122

122123
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
},
88
"dependencies": {
99
"debug": "0.7.4",
10-
"uid2": "0.0.3",
11-
"mubsub": "1.0.4",
10+
"mongodb-uri": "^0.9.7",
1211
"msgpack-js": "0.3.0",
13-
"socket.io-adapter": "0.3.0"
12+
"mubsub": "1.0.4",
13+
"socket.io-adapter": "0.3.0",
14+
"uid2": "0.0.3"
1415
},
1516
"devDependencies": {
1617
"socket.io": "1.0.2",

0 commit comments

Comments
 (0)