-
Notifications
You must be signed in to change notification settings - Fork 155
/
frame.js
152 lines (130 loc) · 3.77 KB
/
frame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @file Utility functions for creating frames
*/
'use strict'
/**
* Creates a text frame
* @param {string} data
* @param {boolean} [masked=false] if the frame should be masked
* @returns {Buffer}
* @private
*/
exports.createTextFrame = function (data, masked) {
var payload, meta
payload = Buffer.from(data)
meta = generateMetaData(true, 1, masked === undefined ? false : masked, payload)
return Buffer.concat([meta, payload], meta.length + payload.length)
}
/**
* Create a binary frame
* @param {Buffer} data
* @param {boolean} [masked=false] if the frame should be masked
* @param {boolean} [first=true] if this is the first frame in a sequence
* @param {boolean} [fin=true] if this is the final frame in a sequence
* @returns {Buffer}
* @private
*/
exports.createBinaryFrame = function (data, masked, first, fin) {
var payload, meta
first = first === undefined ? true : first
masked = masked === undefined ? false : masked
if (masked) {
payload = Buffer.alloc(data.length)
data.copy(payload)
} else {
payload = data
}
meta = generateMetaData(fin === undefined ? true : fin, first ? 2 : 0, masked, payload)
return Buffer.concat([meta, payload], meta.length + payload.length)
}
/**
* Create a close frame
* @param {number} code
* @param {string} [reason='']
* @param {boolean} [masked=false] if the frame should be masked
* @returns {Buffer}
* @private
*/
exports.createCloseFrame = function (code, reason, masked) {
var payload, meta
if (code !== undefined && code !== 1005) {
payload = Buffer.from(reason === undefined ? '--' : '--' + reason)
payload.writeUInt16BE(code, 0)
} else {
payload = Buffer.alloc(0)
}
meta = generateMetaData(true, 8, masked === undefined ? false : masked, payload)
return Buffer.concat([meta, payload], meta.length + payload.length)
}
/**
* Create a ping frame
* @param {string} data
* @param {boolean} [masked=false] if the frame should be masked
* @returns {Buffer}
* @private
*/
exports.createPingFrame = function (data, masked) {
var payload, meta
payload = Buffer.from(data)
meta = generateMetaData(true, 9, masked === undefined ? false : masked, payload)
return Buffer.concat([meta, payload], meta.length + payload.length)
}
/**
* Create a pong frame
* @param {string} data
* @param {boolean} [masked=false] if the frame should be masked
* @returns {Buffer}
* @private
*/
exports.createPongFrame = function (data, masked) {
var payload, meta
payload = Buffer.from(data)
meta = generateMetaData(true, 10, masked === undefined ? false : masked, payload)
return Buffer.concat([meta, payload], meta.length + payload.length)
}
/**
* Creates the meta-data portion of the frame
* If the frame is masked, the payload is altered accordingly
* @param {boolean} fin
* @param {number} opcode
* @param {boolean} masked
* @param {Buffer} payload
* @returns {Buffer}
* @private
*/
function generateMetaData(fin, opcode, masked, payload) {
var len, meta, start, mask, i
len = payload.length
// Creates the buffer for meta-data
meta = Buffer.alloc(2 + (len < 126 ? 0 : (len < 65536 ? 2 : 8)) + (masked ? 4 : 0))
// Sets fin and opcode
meta[0] = (fin ? 128 : 0) + opcode
// Sets the mask and length
meta[1] = masked ? 128 : 0
start = 2
if (len < 126) {
meta[1] += len
} else if (len < 65536) {
meta[1] += 126
meta.writeUInt16BE(len, 2)
start += 2
} else {
// Warning: JS doesn't support integers greater than 2^53
meta[1] += 127
meta.writeUInt32BE(Math.floor(len / Math.pow(2, 32)), 2)
meta.writeUInt32BE(len % Math.pow(2, 32), 6)
start += 8
}
// Set the mask-key
if (masked) {
mask = Buffer.alloc(4)
for (i = 0; i < 4; i++) {
meta[start + i] = mask[i] = Math.floor(Math.random() * 256)
}
for (i = 0; i < payload.length; i++) {
payload[i] ^= mask[i % 4]
}
start += 4
}
return meta
}