1 | 'use strict';
|
---|
2 |
|
---|
3 | var Buffer = require('safe-buffer').Buffer,
|
---|
4 | Emitter = require('events').EventEmitter,
|
---|
5 | util = require('util'),
|
---|
6 | streams = require('../streams'),
|
---|
7 | Headers = require('./headers'),
|
---|
8 | Reader = require('./stream_reader');
|
---|
9 |
|
---|
10 | var Base = function(request, url, options) {
|
---|
11 | Emitter.call(this);
|
---|
12 | Base.validateOptions(options || {}, ['maxLength', 'masking', 'requireMasking', 'protocols']);
|
---|
13 |
|
---|
14 | this._request = request;
|
---|
15 | this._reader = new Reader();
|
---|
16 | this._options = options || {};
|
---|
17 | this._maxLength = this._options.maxLength || this.MAX_LENGTH;
|
---|
18 | this._headers = new Headers();
|
---|
19 | this.__queue = [];
|
---|
20 | this.readyState = 0;
|
---|
21 | this.url = url;
|
---|
22 |
|
---|
23 | this.io = new streams.IO(this);
|
---|
24 | this.messages = new streams.Messages(this);
|
---|
25 | this._bindEventListeners();
|
---|
26 | };
|
---|
27 | util.inherits(Base, Emitter);
|
---|
28 |
|
---|
29 | Base.isWebSocket = function(request) {
|
---|
30 | var connection = request.headers.connection || '',
|
---|
31 | upgrade = request.headers.upgrade || '';
|
---|
32 |
|
---|
33 | return request.method === 'GET' &&
|
---|
34 | connection.toLowerCase().split(/ *, */).indexOf('upgrade') >= 0 &&
|
---|
35 | upgrade.toLowerCase() === 'websocket';
|
---|
36 | };
|
---|
37 |
|
---|
38 | Base.validateOptions = function(options, validKeys) {
|
---|
39 | for (var key in options) {
|
---|
40 | if (validKeys.indexOf(key) < 0)
|
---|
41 | throw new Error('Unrecognized option: ' + key);
|
---|
42 | }
|
---|
43 | };
|
---|
44 |
|
---|
45 | var instance = {
|
---|
46 | // This is 64MB, small enough for an average VPS to handle without
|
---|
47 | // crashing from process out of memory
|
---|
48 | MAX_LENGTH: 0x3ffffff,
|
---|
49 |
|
---|
50 | STATES: ['connecting', 'open', 'closing', 'closed'],
|
---|
51 |
|
---|
52 | _bindEventListeners: function() {
|
---|
53 | var self = this;
|
---|
54 |
|
---|
55 | // Protocol errors are informational and do not have to be handled
|
---|
56 | this.messages.on('error', function() {});
|
---|
57 |
|
---|
58 | this.on('message', function(event) {
|
---|
59 | var messages = self.messages;
|
---|
60 | if (messages.readable) messages.emit('data', event.data);
|
---|
61 | });
|
---|
62 |
|
---|
63 | this.on('error', function(error) {
|
---|
64 | var messages = self.messages;
|
---|
65 | if (messages.readable) messages.emit('error', error);
|
---|
66 | });
|
---|
67 |
|
---|
68 | this.on('close', function() {
|
---|
69 | var messages = self.messages;
|
---|
70 | if (!messages.readable) return;
|
---|
71 | messages.readable = messages.writable = false;
|
---|
72 | messages.emit('end');
|
---|
73 | });
|
---|
74 | },
|
---|
75 |
|
---|
76 | getState: function() {
|
---|
77 | return this.STATES[this.readyState] || null;
|
---|
78 | },
|
---|
79 |
|
---|
80 | addExtension: function(extension) {
|
---|
81 | return false;
|
---|
82 | },
|
---|
83 |
|
---|
84 | setHeader: function(name, value) {
|
---|
85 | if (this.readyState > 0) return false;
|
---|
86 | this._headers.set(name, value);
|
---|
87 | return true;
|
---|
88 | },
|
---|
89 |
|
---|
90 | start: function() {
|
---|
91 | if (this.readyState !== 0) return false;
|
---|
92 |
|
---|
93 | if (!Base.isWebSocket(this._request))
|
---|
94 | return this._failHandshake(new Error('Not a WebSocket request'));
|
---|
95 |
|
---|
96 | var response;
|
---|
97 |
|
---|
98 | try {
|
---|
99 | response = this._handshakeResponse();
|
---|
100 | } catch (error) {
|
---|
101 | return this._failHandshake(error);
|
---|
102 | }
|
---|
103 |
|
---|
104 | this._write(response);
|
---|
105 | if (this._stage !== -1) this._open();
|
---|
106 | return true;
|
---|
107 | },
|
---|
108 |
|
---|
109 | _failHandshake: function(error) {
|
---|
110 | var headers = new Headers();
|
---|
111 | headers.set('Content-Type', 'text/plain');
|
---|
112 | headers.set('Content-Length', Buffer.byteLength(error.message, 'utf8'));
|
---|
113 |
|
---|
114 | headers = ['HTTP/1.1 400 Bad Request', headers.toString(), error.message];
|
---|
115 | this._write(Buffer.from(headers.join('\r\n'), 'utf8'));
|
---|
116 | this._fail('protocol_error', error.message);
|
---|
117 |
|
---|
118 | return false;
|
---|
119 | },
|
---|
120 |
|
---|
121 | text: function(message) {
|
---|
122 | return this.frame(message);
|
---|
123 | },
|
---|
124 |
|
---|
125 | binary: function(message) {
|
---|
126 | return false;
|
---|
127 | },
|
---|
128 |
|
---|
129 | ping: function() {
|
---|
130 | return false;
|
---|
131 | },
|
---|
132 |
|
---|
133 | pong: function() {
|
---|
134 | return false;
|
---|
135 | },
|
---|
136 |
|
---|
137 | close: function(reason, code) {
|
---|
138 | if (this.readyState !== 1) return false;
|
---|
139 | this.readyState = 3;
|
---|
140 | this.emit('close', new Base.CloseEvent(null, null));
|
---|
141 | return true;
|
---|
142 | },
|
---|
143 |
|
---|
144 | _open: function() {
|
---|
145 | this.readyState = 1;
|
---|
146 | this.__queue.forEach(function(args) { this.frame.apply(this, args) }, this);
|
---|
147 | this.__queue = [];
|
---|
148 | this.emit('open', new Base.OpenEvent());
|
---|
149 | },
|
---|
150 |
|
---|
151 | _queue: function(message) {
|
---|
152 | this.__queue.push(message);
|
---|
153 | return true;
|
---|
154 | },
|
---|
155 |
|
---|
156 | _write: function(chunk) {
|
---|
157 | var io = this.io;
|
---|
158 | if (io.readable) io.emit('data', chunk);
|
---|
159 | },
|
---|
160 |
|
---|
161 | _fail: function(type, message) {
|
---|
162 | this.readyState = 2;
|
---|
163 | this.emit('error', new Error(message));
|
---|
164 | this.close();
|
---|
165 | }
|
---|
166 | };
|
---|
167 |
|
---|
168 | for (var key in instance)
|
---|
169 | Base.prototype[key] = instance[key];
|
---|
170 |
|
---|
171 |
|
---|
172 | Base.ConnectEvent = function() {};
|
---|
173 |
|
---|
174 | Base.OpenEvent = function() {};
|
---|
175 |
|
---|
176 | Base.CloseEvent = function(code, reason) {
|
---|
177 | this.code = code;
|
---|
178 | this.reason = reason;
|
---|
179 | };
|
---|
180 |
|
---|
181 | Base.MessageEvent = function(data) {
|
---|
182 | this.data = data;
|
---|
183 | };
|
---|
184 |
|
---|
185 | Base.PingEvent = function(data) {
|
---|
186 | this.data = data;
|
---|
187 | };
|
---|
188 |
|
---|
189 | Base.PongEvent = function(data) {
|
---|
190 | this.data = data;
|
---|
191 | };
|
---|
192 |
|
---|
193 | module.exports = Base;
|
---|