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 = / (?: m o n g o d b \: \/ \/ ) ? (?: ( .* ) \: ( .* ) \@ ) ? ( .* ) \: ( \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,106 @@ 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+
39+ // ensure uri has mongodb scheme
40+ if ( uri . indexOf ( 'mongodb://' ) !== 0 ) {
41+ uri = 'mongodb://' + uri ;
42+ }
43+
44+ // Parse to uri into an object
45+ var uriObj = mongodbUri . parse ( uri ) ;
46+ if ( uriObj . username && uriObj . password ) {
47+ opts . username = uriObj . username ;
48+ opts . password = uriObj . password ;
49+ }
50+ opts . host = uriObj . hosts [ 0 ] . host ;
51+ opts . port = uriObj . hosts [ 0 ] . port ;
52+ opts . db = uriObj . database ;
53+ }
54+
55+ // opts
56+ var socket = opts . socket ;
57+ var creds = ( opts . username && opts . password ) ? opts . username + ':' + opts . password + '@' : '' ;
58+ var host = opts . host || '127.0.0.1' ;
59+ var port = Number ( opts . port || 27017 ) ;
60+ var db = opts . db || 'mubsub' ;
61+
62+ var client = opts . client ;
63+ var key = opts . key || 'socket.io' ;
64+
65+ // init clients if needed
66+ if ( ! client ) client = socket ? mubsub ( socket ) : mubsub ( 'mongodb://' + creds + host + ':' + port + '/' + db ) ;
67+
68+ // this server's key
69+ var uid = uid2 ( 6 ) ;
70+
71+ channel = client . channel ( key ) ;
72+
73+ /**
74+ * Adapter constructor.
75+ *
76+ * @param {String } namespace name
77+ * @api public
78+ */
79+
80+ function Mongo ( nsp ) {
81+ Adapter . call ( this , nsp ) ;
82+
83+ channel . subscribe ( key , this . onmessage . bind ( this ) ) ;
84+ }
85+
86+ /**
87+ * Inherits from `Adapter`.
88+ */
89+
90+ Mongo . prototype . __proto__ = Adapter . prototype ;
91+
92+ /**
93+ * Called with a subscription message
94+ *
95+ * @api private
96+ */
97+
98+ Mongo . prototype . onmessage = function ( msg ) {
99+ if ( uid == msg . uid || ! msg . uid ) return debug ( 'ignore same uid' ) ;
100+
101+ var args = msgpack . decode ( msg . data . buffer ) ;
102+ if ( args [ 0 ] && args [ 0 ] . nsp === undefined )
103+ args [ 0 ] . nsp = '/' ;
104+
105+ if ( ! args [ 0 ] || args [ 0 ] . nsp != this . nsp . name ) return debug ( 'ignore different namespace' ) ;
106+ args . push ( true ) ;
107+ this . broadcast . apply ( this , args ) ;
108+ } ;
109+
110+ /**
111+ * Broadcasts a packet.
112+ *
113+ * @param {Object } packet to emit
114+ * @param {Object } options
115+ * @param {Boolean } whether the packet came from another node
116+ * @api public
117+ */
118+
119+ Mongo . prototype . broadcast = function ( packet , opts , remote ) {
120+ Adapter . prototype . broadcast . call ( this , packet , opts ) ;
121+
122+ if ( ! remote ) {
123+ channel . publish ( key , { uid : uid , data : msgpack . encode ( [ packet , opts ] ) } ) ;
124+ }
125+ } ;
126+
127+ return Mongo ;
121128
122129}
0 commit comments