1 | 'use strict';
|
2 |
|
3 | const { randomBytes } = 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 |
|
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 = 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 | target[1] = payloadLength;
|
64 |
|
65 | if (payloadLength === 126) {
|
66 | target.writeUInt16BE(data.length, 2);
|
67 | } else if (payloadLength === 127) {
|
68 | target.writeUInt32BE(0, 2);
|
69 | target.writeUInt32BE(data.length, 6);
|
70 | }
|
71 |
|
72 | if (!options.mask) return [target, data];
|
73 |
|
74 | const mask = randomBytes(4);
|
75 |
|
76 | target[1] |= 0x80;
|
77 | target[offset - 4] = mask[0];
|
78 | target[offset - 3] = mask[1];
|
79 | target[offset - 2] = mask[2];
|
80 | target[offset - 1] = mask[3];
|
81 |
|
82 | if (merge) {
|
83 | applyMask(data, mask, target, offset, data.length);
|
84 | return [target];
|
85 | }
|
86 |
|
87 | applyMask(data, mask, data, 0, data.length);
|
88 | return [target, data];
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | close(code, data, mask, cb) {
|
101 | var buf;
|
102 |
|
103 | if (code === undefined) {
|
104 | buf = EMPTY_BUFFER;
|
105 | } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
|
106 | throw new TypeError('First argument must be a valid error code number');
|
107 | } else if (data === undefined || data === '') {
|
108 | buf = Buffer.allocUnsafe(2);
|
109 | buf.writeUInt16BE(code, 0);
|
110 | } else {
|
111 | buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
|
112 | buf.writeUInt16BE(code, 0);
|
113 | buf.write(data, 2);
|
114 | }
|
115 |
|
116 | if (this._deflating) {
|
117 | this.enqueue([this.doClose, buf, mask, cb]);
|
118 | } else {
|
119 | this.doClose(buf, mask, cb);
|
120 | }
|
121 | }
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | doClose(data, mask, cb) {
|
132 | this.sendFrame(
|
133 | Sender.frame(data, {
|
134 | fin: true,
|
135 | rsv1: false,
|
136 | opcode: 0x08,
|
137 | mask,
|
138 | readOnly: false
|
139 | }),
|
140 | cb
|
141 | );
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | ping(data, mask, cb) {
|
153 | const buf = toBuffer(data);
|
154 |
|
155 | if (this._deflating) {
|
156 | this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
|
157 | } else {
|
158 | this.doPing(buf, mask, toBuffer.readOnly, cb);
|
159 | }
|
160 | }
|
161 |
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | doPing(data, mask, readOnly, cb) {
|
172 | this.sendFrame(
|
173 | Sender.frame(data, {
|
174 | fin: true,
|
175 | rsv1: false,
|
176 | opcode: 0x09,
|
177 | mask,
|
178 | readOnly
|
179 | }),
|
180 | cb
|
181 | );
|
182 | }
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | pong(data, mask, cb) {
|
193 | const buf = toBuffer(data);
|
194 |
|
195 | if (this._deflating) {
|
196 | this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
|
197 | } else {
|
198 | this.doPong(buf, mask, toBuffer.readOnly, cb);
|
199 | }
|
200 | }
|
201 |
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | doPong(data, mask, readOnly, cb) {
|
212 | this.sendFrame(
|
213 | Sender.frame(data, {
|
214 | fin: true,
|
215 | rsv1: false,
|
216 | opcode: 0x0a,
|
217 | mask,
|
218 | readOnly
|
219 | }),
|
220 | cb
|
221 | );
|
222 | }
|
223 |
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | send(data, options, cb) {
|
237 | const buf = toBuffer(data);
|
238 | const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
239 | var opcode = options.binary ? 2 : 1;
|
240 | var rsv1 = options.compress;
|
241 |
|
242 | if (this._firstFragment) {
|
243 | this._firstFragment = false;
|
244 | if (rsv1 && perMessageDeflate) {
|
245 | rsv1 = buf.length >= perMessageDeflate._threshold;
|
246 | }
|
247 | this._compress = rsv1;
|
248 | } else {
|
249 | rsv1 = false;
|
250 | opcode = 0;
|
251 | }
|
252 |
|
253 | if (options.fin) this._firstFragment = true;
|
254 |
|
255 | if (perMessageDeflate) {
|
256 | const opts = {
|
257 | fin: options.fin,
|
258 | rsv1,
|
259 | opcode,
|
260 | mask: options.mask,
|
261 | readOnly: toBuffer.readOnly
|
262 | };
|
263 |
|
264 | if (this._deflating) {
|
265 | this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
|
266 | } else {
|
267 | this.dispatch(buf, this._compress, opts, cb);
|
268 | }
|
269 | } else {
|
270 | this.sendFrame(
|
271 | Sender.frame(buf, {
|
272 | fin: options.fin,
|
273 | rsv1: false,
|
274 | opcode,
|
275 | mask: options.mask,
|
276 | readOnly: toBuffer.readOnly
|
277 | }),
|
278 | cb
|
279 | );
|
280 | }
|
281 | }
|
282 |
|
283 | |
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | dispatch(data, compress, options, cb) {
|
298 | if (!compress) {
|
299 | this.sendFrame(Sender.frame(data, options), cb);
|
300 | return;
|
301 | }
|
302 |
|
303 | const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
304 |
|
305 | this._deflating = true;
|
306 | perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
307 | this._deflating = false;
|
308 | options.readOnly = false;
|
309 | this.sendFrame(Sender.frame(buf, options), cb);
|
310 | this.dequeue();
|
311 | });
|
312 | }
|
313 |
|
314 | |
315 |
|
316 |
|
317 |
|
318 |
|
319 | dequeue() {
|
320 | while (!this._deflating && this._queue.length) {
|
321 | const params = this._queue.shift();
|
322 |
|
323 | this._bufferedBytes -= params[1].length;
|
324 | params[0].apply(this, params.slice(1));
|
325 | }
|
326 | }
|
327 |
|
328 | |
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | enqueue(params) {
|
335 | this._bufferedBytes += params[1].length;
|
336 | this._queue.push(params);
|
337 | }
|
338 |
|
339 | |
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | sendFrame(list, cb) {
|
347 | if (list.length === 2) {
|
348 | this._socket.cork();
|
349 | this._socket.write(list[0]);
|
350 | this._socket.write(list[1], cb);
|
351 | this._socket.uncork();
|
352 | } else {
|
353 | this._socket.write(list[0], cb);
|
354 | }
|
355 | }
|
356 | }
|
357 |
|
358 | module.exports = Sender;
|