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