bare-ws 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/errors.d.ts +16 -15
- package/lib/errors.js +9 -0
- package/lib/frame.js +10 -7
- package/lib/socket.js +20 -17
- package/package.json +1 -1
package/lib/errors.d.ts
CHANGED
|
@@ -8,21 +8,22 @@ declare class WebSocketError extends Error {
|
|
|
8
8
|
)
|
|
9
9
|
|
|
10
10
|
static NETWORK_ERROR(msg: string, cause?: unknown): WebSocketError
|
|
11
|
-
static NOT_CONNECTED(msg?: string
|
|
12
|
-
static UNEXPECTED_RSV1(msg?: string
|
|
13
|
-
static UNEXPECTED_RSV2(msg?: string
|
|
14
|
-
static UNEXPECTED_RSV3(msg?: string
|
|
15
|
-
static EXPECTED_MASK(msg?: string
|
|
16
|
-
static EXPECTED_CONTINUATION(msg?: string
|
|
17
|
-
static UNEXPECTED_CONTINUATION(msg?: string
|
|
18
|
-
static UNEXPECTED_CONTROL(msg?: string
|
|
19
|
-
static INVALID_ENCODING(msg?: string
|
|
20
|
-
static INVALID_UPGRADE_HEADER(msg?: string
|
|
21
|
-
static INVALID_VERSION_HEADER(msg?: string
|
|
22
|
-
static INVALID_KEY_HEADER(msg?: string
|
|
23
|
-
static INVALID_ACCEPT_HEADER(msg?: string
|
|
24
|
-
static INVALID_OPCODE(msg?: string
|
|
25
|
-
static INVALID_PAYLOAD_LENGTH(msg?: string
|
|
11
|
+
static NOT_CONNECTED(msg?: string): WebSocketError
|
|
12
|
+
static UNEXPECTED_RSV1(msg?: string): WebSocketError
|
|
13
|
+
static UNEXPECTED_RSV2(msg?: string): WebSocketError
|
|
14
|
+
static UNEXPECTED_RSV3(msg?: string): WebSocketError
|
|
15
|
+
static EXPECTED_MASK(msg?: string): WebSocketError
|
|
16
|
+
static EXPECTED_CONTINUATION(msg?: string): WebSocketError
|
|
17
|
+
static UNEXPECTED_CONTINUATION(msg?: string): WebSocketError
|
|
18
|
+
static UNEXPECTED_CONTROL(msg?: string): WebSocketError
|
|
19
|
+
static INVALID_ENCODING(msg?: string): WebSocketError
|
|
20
|
+
static INVALID_UPGRADE_HEADER(msg?: string): WebSocketError
|
|
21
|
+
static INVALID_VERSION_HEADER(msg?: string): WebSocketError
|
|
22
|
+
static INVALID_KEY_HEADER(msg?: string): WebSocketError
|
|
23
|
+
static INVALID_ACCEPT_HEADER(msg?: string): WebSocketError
|
|
24
|
+
static INVALID_OPCODE(msg?: string): WebSocketError
|
|
25
|
+
static INVALID_PAYLOAD_LENGTH(msg?: string): WebSocketError
|
|
26
|
+
static INCOMPLETE_FRAME(msg?: string, length?: number): WebSocketError
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
export = WebSocketError
|
package/lib/errors.js
CHANGED
|
@@ -168,4 +168,13 @@ module.exports = class WebSocketError extends Error {
|
|
|
168
168
|
WebSocketError.INVALID_PAYLOAD_LENGTH
|
|
169
169
|
)
|
|
170
170
|
}
|
|
171
|
+
|
|
172
|
+
static INCOMPLETE_FRAME(msg = 'Incomplete frame', length = -1) {
|
|
173
|
+
return new WebSocketError(
|
|
174
|
+
msg,
|
|
175
|
+
'INCOMPLETE_FRAME',
|
|
176
|
+
length,
|
|
177
|
+
WebSocketError.INCOMPLETE_FRAME
|
|
178
|
+
)
|
|
179
|
+
}
|
|
171
180
|
}
|
package/lib/frame.js
CHANGED
|
@@ -136,12 +136,13 @@ exports.encode = function encode(state, f) {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
exports.decode = function decode(state) {
|
|
139
|
+
const s = state.start
|
|
139
140
|
const b = state.buffer
|
|
140
141
|
|
|
141
|
-
let i =
|
|
142
|
-
let n = b.
|
|
142
|
+
let i = s
|
|
143
|
+
let n = b.byteLength
|
|
143
144
|
|
|
144
|
-
if (n < 2)
|
|
145
|
+
if (n < 2) throw errors.INCOMPLETE_FRAME()
|
|
145
146
|
|
|
146
147
|
const view = new DataView(b.buffer, b.byteOffset, b.byteLength)
|
|
147
148
|
|
|
@@ -164,14 +165,14 @@ exports.decode = function decode(state) {
|
|
|
164
165
|
n--
|
|
165
166
|
|
|
166
167
|
if (length === 0x7e) {
|
|
167
|
-
if (n < 2)
|
|
168
|
+
if (n < 2) throw errors.INCOMPLETE_FRAME()
|
|
168
169
|
|
|
169
170
|
length = view.getUint16(i, false)
|
|
170
171
|
|
|
171
172
|
i += 2
|
|
172
173
|
n -= 2
|
|
173
174
|
} else if (length === 0x7f) {
|
|
174
|
-
if (n < 8)
|
|
175
|
+
if (n < 8) throw errors.INCOMPLETE_FRAME()
|
|
175
176
|
|
|
176
177
|
const high = view.getUint32(i, false)
|
|
177
178
|
|
|
@@ -191,7 +192,7 @@ exports.decode = function decode(state) {
|
|
|
191
192
|
let mask = null
|
|
192
193
|
|
|
193
194
|
if (masked) {
|
|
194
|
-
if (n < 4)
|
|
195
|
+
if (n < 4) throw errors.INCOMPLETE_FRAME()
|
|
195
196
|
|
|
196
197
|
mask = b.subarray(i, i + 4)
|
|
197
198
|
|
|
@@ -199,7 +200,9 @@ exports.decode = function decode(state) {
|
|
|
199
200
|
n -= 4
|
|
200
201
|
}
|
|
201
202
|
|
|
202
|
-
if (n < length)
|
|
203
|
+
if (n < length) {
|
|
204
|
+
throw errors.INCOMPLETE_FRAME('Incomplete frame', i - s + length)
|
|
205
|
+
}
|
|
203
206
|
|
|
204
207
|
const payload = b.subarray(i, i + length)
|
|
205
208
|
|
package/lib/socket.js
CHANGED
|
@@ -40,7 +40,9 @@ module.exports = exports = class WebSocket extends Duplex {
|
|
|
40
40
|
this._pendingOpen = null
|
|
41
41
|
this._pendingWrite = null
|
|
42
42
|
|
|
43
|
-
this._buffer =
|
|
43
|
+
this._buffer = []
|
|
44
|
+
this._buffered = 0
|
|
45
|
+
this._frame = -1
|
|
44
46
|
|
|
45
47
|
if (socket !== null) this._attach(socket)
|
|
46
48
|
else this._connect(opts)
|
|
@@ -101,29 +103,30 @@ module.exports = exports = class WebSocket extends Duplex {
|
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
_ondata(data) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
+
this._buffer.push(data)
|
|
107
|
+
this._buffered += data.byteLength
|
|
106
108
|
|
|
107
|
-
while (this.
|
|
108
|
-
const
|
|
109
|
+
while (this._frame === -1 || this._frame <= this._buffered) {
|
|
110
|
+
const buffer =
|
|
111
|
+
this._buffer.length === 1
|
|
112
|
+
? this._buffer[0]
|
|
113
|
+
: Buffer.concat(this._buffer)
|
|
109
114
|
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
frame = Frame.decode(state)
|
|
113
|
-
} catch (err) {
|
|
114
|
-
return this.destroy(err)
|
|
115
|
-
}
|
|
115
|
+
this._buffer = [buffer]
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this._buffer =
|
|
120
|
-
state.start === state.end ? null : this._buffer.subarray(state.start)
|
|
117
|
+
const state = { start: 0, end: buffer.length, buffer }
|
|
121
118
|
|
|
122
119
|
try {
|
|
123
|
-
this._onframe(
|
|
120
|
+
this._onframe(Frame.decode(state))
|
|
124
121
|
} catch (err) {
|
|
125
|
-
|
|
122
|
+
if (err.code === 'INCOMPLETE_FRAME') this._frame = err.status
|
|
123
|
+
else this.destroy(err)
|
|
124
|
+
return
|
|
126
125
|
}
|
|
126
|
+
|
|
127
|
+
this._buffered -= state.start
|
|
128
|
+
this._buffer = this._buffered > 0 ? [buffer.subarray(state.start)] : []
|
|
129
|
+
this._frame = -1
|
|
127
130
|
}
|
|
128
131
|
}
|
|
129
132
|
|