1 | 'use strict';
|
2 |
|
3 | const crypto = require('crypto');
|
4 |
|
5 | const PerMessageDeflate = require('./permessage-deflate');
|
6 | const bufferUtil = require('./buffer-util');
|
7 | const validation = require('./validation');
|
8 | const constants = require('./constants');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class Sender {
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | constructor (socket, extensions) {
|
21 | this._extensions = extensions || {};
|
22 | this._socket = socket;
|
23 |
|
24 | this._firstFragment = true;
|
25 | this._compress = false;
|
26 |
|
27 | this._bufferedBytes = 0;
|
28 | this._deflating = false;
|
29 | this._queue = [];
|
30 | }
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | static frame (data, options) {
|
46 | const merge = data.length < 1024 || (options.mask && options.readOnly);
|
47 | var offset = options.mask ? 6 : 2;
|
48 | var payloadLength = data.length;
|
49 |
|
50 | if (data.length >= 65536) {
|
51 | offset += 8;
|
52 | payloadLength = 127;
|
53 | } else if (data.length > 125) {
|
54 | offset += 2;
|
55 | payloadLength = 126;
|
56 | }
|
57 |
|
58 | const target = Buffer.allocUnsafe(merge ? data.length + offset : offset);
|
59 |
|
60 | target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
|
61 | if (options.rsv1) target[0] |= 0x40;
|
62 |
|
63 | if (payloadLength === 126) {
|
64 | target.writeUInt16BE(data.length, 2);
|
65 | } else if (payloadLength === 127) {
|
66 | target.writeUInt32BE(0, 2);
|
67 | target.writeUInt32BE(data.length, 6);
|
68 | }
|
69 |
|
70 | if (!options.mask) {
|
71 | target[1] = payloadLength;
|
72 | if (merge) {
|
73 | data.copy(target, offset);
|
74 | return [target];
|
75 | }
|
76 |
|
77 | return [target, data];
|
78 | }
|
79 |
|
80 | const mask = crypto.randomBytes(4);
|
81 |
|
82 | target[1] = payloadLength | 0x80;
|
83 | target[offset - 4] = mask[0];
|
84 | target[offset - 3] = mask[1];
|
85 | target[offset - 2] = mask[2];
|
86 | target[offset - 1] = mask[3];
|
87 |
|
88 | if (merge) {
|
89 | bufferUtil.mask(data, mask, target, offset, data.length);
|
90 | return [target];
|
91 | }
|
92 |
|
93 | bufferUtil.mask(data, mask, data, 0, data.length);
|
94 | return [target, data];
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | close (code, data, mask, cb) {
|
107 | var buf;
|
108 |
|
109 | if (code === undefined) {
|
110 | buf = constants.EMPTY_BUFFER;
|
111 | } else if (typeof code !== 'number' || !validation.isValidStatusCode(code)) {
|
112 | throw new TypeError('First argument must be a valid error code number');
|
113 | } else if (data === undefined || data === '') {
|
114 | buf = Buffer.allocUnsafe(2);
|
115 | buf.writeUInt16BE(code, 0);
|
116 | } else {
|
117 | buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
|
118 | buf.writeUInt16BE(code, 0);
|
119 | buf.write(data, 2);
|
120 | }
|
121 |
|
122 | if (this._deflating) {
|
123 | this.enqueue([this.doClose, buf, mask, cb]);
|
124 | } else {
|
125 | this.doClose(buf, mask, cb);
|
126 | }
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | doClose (data, mask, cb) {
|
138 | this.sendFrame(Sender.frame(data, {
|
139 | fin: true,
|
140 | rsv1: false,
|
141 | opcode: 0x08,
|
142 | mask,
|
143 | readOnly: false
|
144 | }), cb);
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | ping (data, mask, cb) {
|
156 | var readOnly = true;
|
157 |
|
158 | if (!Buffer.isBuffer(data)) {
|
159 | if (data instanceof ArrayBuffer) {
|
160 | data = Buffer.from(data);
|
161 | } else if (ArrayBuffer.isView(data)) {
|
162 | data = viewToBuffer(data);
|
163 | } else {
|
164 | data = Buffer.from(data);
|
165 | readOnly = false;
|
166 | }
|
167 | }
|
168 |
|
169 | if (this._deflating) {
|
170 | this.enqueue([this.doPing, data, mask, readOnly, cb]);
|
171 | } else {
|
172 | this.doPing(data, mask, readOnly, cb);
|
173 | }
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | doPing (data, mask, readOnly, cb) {
|
186 | this.sendFrame(Sender.frame(data, {
|
187 | fin: true,
|
188 | rsv1: false,
|
189 | opcode: 0x09,
|
190 | mask,
|
191 | readOnly
|
192 | }), cb);
|
193 | }
|
194 |
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | pong (data, mask, cb) {
|
204 | var readOnly = true;
|
205 |
|
206 | if (!Buffer.isBuffer(data)) {
|
207 | if (data instanceof ArrayBuffer) {
|
208 | data = Buffer.from(data);
|
209 | } else if (ArrayBuffer.isView(data)) {
|
210 | data = viewToBuffer(data);
|
211 | } else {
|
212 | data = Buffer.from(data);
|
213 | readOnly = false;
|
214 | }
|
215 | }
|
216 |
|
217 | if (this._deflating) {
|
218 | this.enqueue([this.doPong, data, mask, readOnly, cb]);
|
219 | } else {
|
220 | this.doPong(data, mask, readOnly, cb);
|
221 | }
|
222 | }
|
223 |
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | doPong (data, mask, readOnly, cb) {
|
234 | this.sendFrame(Sender.frame(data, {
|
235 | fin: true,
|
236 | rsv1: false,
|
237 | opcode: 0x0a,
|
238 | mask,
|
239 | readOnly
|
240 | }), cb);
|
241 | }
|
242 |
|
243 | |
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | send (data, options, cb) {
|
256 | var opcode = options.binary ? 2 : 1;
|
257 | var rsv1 = options.compress;
|
258 | var readOnly = true;
|
259 |
|
260 | if (!Buffer.isBuffer(data)) {
|
261 | if (data instanceof ArrayBuffer) {
|
262 | data = Buffer.from(data);
|
263 | } else if (ArrayBuffer.isView(data)) {
|
264 | data = viewToBuffer(data);
|
265 | } else {
|
266 | data = Buffer.from(data);
|
267 | readOnly = false;
|
268 | }
|
269 | }
|
270 |
|
271 | const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
272 |
|
273 | if (this._firstFragment) {
|
274 | this._firstFragment = false;
|
275 | if (rsv1 && perMessageDeflate) {
|
276 | rsv1 = data.length >= perMessageDeflate._threshold;
|
277 | }
|
278 | this._compress = rsv1;
|
279 | } else {
|
280 | rsv1 = false;
|
281 | opcode = 0;
|
282 | }
|
283 |
|
284 | if (options.fin) this._firstFragment = true;
|
285 |
|
286 | if (perMessageDeflate) {
|
287 | const opts = {
|
288 | fin: options.fin,
|
289 | rsv1,
|
290 | opcode,
|
291 | mask: options.mask,
|
292 | readOnly
|
293 | };
|
294 |
|
295 | if (this._deflating) {
|
296 | this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
297 | } else {
|
298 | this.dispatch(data, this._compress, opts, cb);
|
299 | }
|
300 | } else {
|
301 | this.sendFrame(Sender.frame(data, {
|
302 | fin: options.fin,
|
303 | rsv1: false,
|
304 | opcode,
|
305 | mask: options.mask,
|
306 | readOnly
|
307 | }), cb);
|
308 | }
|
309 | }
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | dispatch (data, compress, options, cb) {
|
326 | if (!compress) {
|
327 | this.sendFrame(Sender.frame(data, options), cb);
|
328 | return;
|
329 | }
|
330 |
|
331 | const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
332 |
|
333 | this._deflating = true;
|
334 | perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
335 | this._deflating = false;
|
336 |
|
337 | if (!this._socket.readable && !this._socket.writable) {
|
338 |
|
339 |
|
340 |
|
341 | this._bufferedBytes = 0;
|
342 | this._queue.length = 0;
|
343 | return;
|
344 | }
|
345 |
|
346 | options.readOnly = false;
|
347 | this.sendFrame(Sender.frame(buf, options), cb);
|
348 | this.dequeue();
|
349 | });
|
350 | }
|
351 |
|
352 | |
353 |
|
354 |
|
355 |
|
356 |
|
357 | dequeue () {
|
358 | while (!this._deflating && this._queue.length) {
|
359 | const params = this._queue.shift();
|
360 |
|
361 | this._bufferedBytes -= params[1].length;
|
362 | params[0].apply(this, params.slice(1));
|
363 | }
|
364 | }
|
365 |
|
366 | |
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | enqueue (params) {
|
373 | this._bufferedBytes += params[1].length;
|
374 | this._queue.push(params);
|
375 | }
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | sendFrame (list, cb) {
|
385 | if (list.length === 2) {
|
386 | this._socket.write(list[0]);
|
387 | this._socket.write(list[1], cb);
|
388 | } else {
|
389 | this._socket.write(list[0], cb);
|
390 | }
|
391 | }
|
392 | }
|
393 |
|
394 | module.exports = Sender;
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | function viewToBuffer (view) {
|
404 | const buf = Buffer.from(view.buffer);
|
405 |
|
406 | if (view.byteLength !== view.buffer.byteLength) {
|
407 | return buf.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
408 | }
|
409 |
|
410 | return buf;
|
411 | }
|