Skip to content

Commit bf14dfa

Browse files
committed
Merge pull request #235 from TooTallNate/add/component-client-side
Add component(1) support
2 parents e33a685 + 0cb9fe7 commit bf14dfa

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/browser.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1-
/// shim for browser packaging
21

3-
module.exports = global.WebSocket || global.MozWebSocket;
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var global = (function() { return this; })();
7+
8+
/**
9+
* WebSocket constructor.
10+
*/
11+
12+
var WebSocket = global.WebSocket || global.MozWebSocket;
13+
14+
/**
15+
* Module exports.
16+
*/
17+
18+
module.exports = WebSocket ? ws : null;
19+
20+
/**
21+
* WebSocket constructor.
22+
*
23+
* The third `opts` options object gets ignored in web browsers, since it's
24+
* non-standard, and throws a TypeError if passed to the constructor.
25+
* See: https://github.com/einaros/ws/issues/227
26+
*
27+
* @param {String} uri
28+
* @param {Array} protocols (optional)
29+
* @param {Object) opts (optional)
30+
* @api public
31+
*/
32+
33+
function ws(uri, protocols, opts) {
34+
var instance;
35+
if (protocols) {
36+
instance = new WebSocket(uri, protocols);
37+
} else {
38+
instance = new WebSocket(uri);
39+
}
40+
return instance;
41+
}
42+
43+
if (WebSocket) ws.prototype = WebSocket.prototype;

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,10 @@
4040
"ansi": "latest"
4141
},
4242
"browser": "./lib/browser.js",
43+
"component": {
44+
"scripts": {
45+
"ws/index.js": "./lib/browser.js"
46+
}
47+
},
4348
"gypfile": true
4449
}

0 commit comments

Comments
 (0)