1 |
|
2 |
|
3 |
|
4 |
|
5 | var EventEmitter = require('events').EventEmitter;
|
6 | var Request = require('./request');
|
7 | var Socket = require('./connection');
|
8 | var url = require('url');
|
9 |
|
10 | var HAS_WINDOW = typeof window !== 'undefined';
|
11 |
|
12 | exports.request = function(params, cb) {
|
13 | params = format(params);
|
14 | var req = new Request(params, exports._WS, exports._cache);
|
15 | if (cb) req.on('response', cb);
|
16 | return req;
|
17 | };
|
18 |
|
19 | exports.onpush = function(cb) {
|
20 | Socket.on('push', cb);
|
21 | return function() {
|
22 | Socket.removeListener('push', cb);
|
23 | };
|
24 | };
|
25 |
|
26 | function format(params) {
|
27 | if (typeof params === 'string') params = url.parse(params);
|
28 | if (!params) params = {};
|
29 | if (!params.host && !params.port && HAS_WINDOW) params.port = parseInt(window.location.port, 10);
|
30 | if (!params.host && params.hostname) params.host = params.hostname;
|
31 |
|
32 | if (!params.protocol) {
|
33 | if (params.scheme) {
|
34 | params.protocol = params.scheme + ':';
|
35 | } else if (HAS_WINDOW) {
|
36 | params.protocol = window.location.protocol;
|
37 | } else {
|
38 | params.protocol = 'ws:';
|
39 | }
|
40 | }
|
41 |
|
42 | if (!params.host && HAS_WINDOW) params.host = window.location.hostname || window.location.host;
|
43 | if (/:/.test(params.host)) {
|
44 | if (!params.port) params.port = params.host.split(':')[1];
|
45 | params.host = params.host.split(':')[0];
|
46 | }
|
47 |
|
48 | if (!params.port) params.port = params.protocol === 'https:' || params.protocol === 'wss:' ? 443 : 80;
|
49 |
|
50 | return params;
|
51 | }
|
52 |
|
53 | exports.STATUS_CODES = {
|
54 | 100 : 'Continue',
|
55 | 101 : 'Switching Protocols',
|
56 | 102 : 'Processing',
|
57 | 200 : 'OK',
|
58 | 201 : 'Created',
|
59 | 202 : 'Accepted',
|
60 | 203 : 'Non-Authoritative Information',
|
61 | 204 : 'No Content',
|
62 | 205 : 'Reset Content',
|
63 | 206 : 'Partial Content',
|
64 | 207 : 'Multi-Status',
|
65 | 300 : 'Multiple Choices',
|
66 | 301 : 'Moved Permanently',
|
67 | 302 : 'Moved Temporarily',
|
68 | 303 : 'See Other',
|
69 | 304 : 'Not Modified',
|
70 | 305 : 'Use Proxy',
|
71 | 307 : 'Temporary Redirect',
|
72 | 400 : 'Bad Request',
|
73 | 401 : 'Unauthorized',
|
74 | 402 : 'Payment Required',
|
75 | 403 : 'Forbidden',
|
76 | 404 : 'Not Found',
|
77 | 405 : 'Method Not Allowed',
|
78 | 406 : 'Not Acceptable',
|
79 | 407 : 'Proxy Authentication Required',
|
80 | 408 : 'Request Time-out',
|
81 | 409 : 'Conflict',
|
82 | 410 : 'Gone',
|
83 | 411 : 'Length Required',
|
84 | 412 : 'Precondition Failed',
|
85 | 413 : 'Request Entity Too Large',
|
86 | 414 : 'Request-URI Too Large',
|
87 | 415 : 'Unsupported Media Type',
|
88 | 416 : 'Requested Range Not Satisfiable',
|
89 | 417 : 'Expectation Failed',
|
90 | 418 : 'I\'m a teapot',
|
91 | 422 : 'Unprocessable Entity',
|
92 | 423 : 'Locked',
|
93 | 424 : 'Failed Dependency',
|
94 | 425 : 'Unordered Collection',
|
95 | 426 : 'Upgrade Required',
|
96 | 428 : 'Precondition Required',
|
97 | 429 : 'Too Many Requests',
|
98 | 431 : 'Request Header Fields Too Large',
|
99 | 500 : 'Internal Server Error',
|
100 | 501 : 'Not Implemented',
|
101 | 502 : 'Bad Gateway',
|
102 | 503 : 'Service Unavailable',
|
103 | 504 : 'Gateway Time-out',
|
104 | 505 : 'HTTP Version Not Supported',
|
105 | 506 : 'Variant Also Negotiates',
|
106 | 507 : 'Insufficient Storage',
|
107 | 509 : 'Bandwidth Limit Exceeded',
|
108 | 510 : 'Not Extended',
|
109 | 511 : 'Network Authentication Required'
|
110 | };
|