UNPKG

3.48 kBJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4
5var EventEmitter = require('events').EventEmitter;
6var Request = require('./request');
7var Socket = require('./connection');
8var url = require('url');
9
10var HAS_WINDOW = typeof window !== 'undefined';
11
12exports.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
19exports.onpush = function(cb) {
20 Socket.on('push', cb);
21 return function() {
22 Socket.removeListener('push', cb);
23 };
24};
25
26function 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
53exports.STATUS_CODES = {
54 100 : 'Continue',
55 101 : 'Switching Protocols',
56 102 : 'Processing', // RFC 2518, obsoleted by RFC 4918
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', // RFC 4918
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', // RFC 2324
91 422 : 'Unprocessable Entity', // RFC 4918
92 423 : 'Locked', // RFC 4918
93 424 : 'Failed Dependency', // RFC 4918
94 425 : 'Unordered Collection', // RFC 4918
95 426 : 'Upgrade Required', // RFC 2817
96 428 : 'Precondition Required', // RFC 6585
97 429 : 'Too Many Requests', // RFC 6585
98 431 : 'Request Header Fields Too Large',// RFC 6585
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', // RFC 2295
106 507 : 'Insufficient Storage', // RFC 4918
107 509 : 'Bandwidth Limit Exceeded',
108 510 : 'Not Extended', // RFC 2774
109 511 : 'Network Authentication Required' // RFC 6585
110};