bare-ws 1.1.0 → 1.2.1

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/README.md CHANGED
@@ -6,6 +6,24 @@ WebSocket library for JavaScript.
6
6
  npm i bare-ws
7
7
  ```
8
8
 
9
+ ## Usage
10
+
11
+ ```js
12
+ const ws = require('bare-ws')
13
+
14
+ const server = new ws.Server({ port: 8080 }, (socket) => {
15
+ socket.on('data', (data) => {
16
+ console.log(data.toString())
17
+ })
18
+ })
19
+
20
+ server.on('listening', () => {
21
+ const socket = new ws.Socket({ port: 8080 })
22
+
23
+ socket.write('Hello WebSocket')
24
+ })
25
+ ```
26
+
9
27
  ## License
10
28
 
11
29
  Apache-2.0
package/lib/errors.js CHANGED
@@ -23,6 +23,18 @@ module.exports = class WebSocketError extends Error {
23
23
  return new WebSocketError(msg, 'UNEXPECTED_MASK', status.PROTOCOL_ERROR, WebSocketError.UNEXPECTED_MASK)
24
24
  }
25
25
 
26
+ static EXPECTED_CONTINUATION (msg = 'Expected a continuation frame') {
27
+ return new WebSocketError(msg, 'EXPECTED_CONTINUATION', status.PROTOCOL_ERROR, WebSocketError.EXPECTED_CONTINUATION)
28
+ }
29
+
30
+ static UNEXPECTED_CONTINUATION (msg = 'Unexpected continuation frame') {
31
+ return new WebSocketError(msg, 'UNEXPECTED_CONTINUATION', status.PROTOCOL_ERROR, WebSocketError.UNEXPECTED_CONTINUATION)
32
+ }
33
+
34
+ static UNEXPECTED_CONTROL (msg = 'Unexpected control frame') {
35
+ return new WebSocketError(msg, 'UNEXPECTED_CONTROL', status.PROTOCOL_ERROR, WebSocketError.UNEXPECTED_CONTROL)
36
+ }
37
+
26
38
  static INVALID_ENCODING (msg = 'Invalid encoding') {
27
39
  return new WebSocketError(msg, 'INVALID_ENCODING', status.PROTOCOL_ERROR, WebSocketError.INVALID_ENCODING)
28
40
  }
@@ -43,6 +55,10 @@ module.exports = class WebSocketError extends Error {
43
55
  return new WebSocketError(msg, 'INVALID_ACCEPT_HEADER', status.PROTOCOL_ERROR, WebSocketError.INVALID_ACCEPT_HEADER)
44
56
  }
45
57
 
58
+ static INVALID_OPCODE (msg = 'Invalid opcode') {
59
+ return new WebSocketError(msg, 'INVALID_OPCODE', status.PROTOCOL_ERROR, WebSocketError.INVALID_OPCODE)
60
+ }
61
+
46
62
  static INVALID_PAYLOAD_LENGTH (msg = 'Invalid payload length') {
47
63
  return new WebSocketError(msg, 'INVALID_PAYLOAD_LENGTH', status.MESSAGE_TOO_LARGE, WebSocketError.INVALID_PAYLOAD_LENGTH)
48
64
  }
package/lib/socket.js CHANGED
@@ -85,7 +85,7 @@ module.exports = exports = class WebSocket extends Duplex {
85
85
  while (this._buffer !== null) {
86
86
  const state = { start: 0, end: this._buffer.length, buffer: this._buffer }
87
87
 
88
- const frame = Frame.decode(state, { isServer: this._isServer })
88
+ const frame = Frame.decode(state)
89
89
 
90
90
  if (frame === null) return
91
91
 
@@ -97,24 +97,47 @@ module.exports = exports = class WebSocket extends Duplex {
97
97
 
98
98
  _onframe (frame) {
99
99
  if (frame.payload.length > 0 && !frame.mask === this._isServer) {
100
- throw this._isServer ? errors.EXPECTED_MASK() : errors.UNEXPECTED_MASK()
100
+ return this.destroy(this._isServer ? errors.EXPECTED_MASK() : errors.UNEXPECTED_MASK())
101
101
  }
102
102
 
103
- if (frame.fin === 0) {
104
- this._fragments.push(frame)
103
+ if (frame.fin === false) {
104
+ if (this._fragments.push(frame) === 1) { // First frame
105
+ if (frame.opcode === opcode.CONTINUATION) return this.destroy(errors.UNEXPECTED_CONTINUATION())
106
+
107
+ if (frame.opcode >= opcode.CLOSE) return this.destroy(errors.UNEXPECTED_CONTROL())
108
+
109
+ return
110
+ }
111
+
112
+ if (frame.opcode !== opcode.CONTINUATION) return this.destroy(errors.EXPECTED_CONTINUATION())
113
+
105
114
  return
106
115
  }
107
116
 
108
- if (this._fragments.length > 0) {
109
- frame.opcode = this._fragments[0].opcode
117
+ switch (frame.opcode) {
118
+ case opcode.CLOSE:
119
+ this.push(null)
120
+ this.end()
121
+ return
122
+
123
+ case opcode.CONTINUATION: {
124
+ if (this._fragments.length === 0) return this.destroy(errors.UNEXPECTED_CONTINUATION())
125
+
126
+ frame.opcode = this._fragments[0].opcode
127
+
128
+ const payloads = this._fragments.map(frame => frame.payload)
129
+
130
+ payloads.push(frame.payload)
110
131
 
111
- const payloads = this._fragments.map(frame => frame.payload)
132
+ frame.payload = Buffer.concat(payloads)
112
133
 
113
- payloads.push(frame.payload)
134
+ this._fragments = []
114
135
 
115
- frame.payload = Buffer.concat(payloads)
136
+ break
137
+ }
116
138
 
117
- this._fragments = []
139
+ default:
140
+ if (this._fragments.length > 0) return this.destroy(errors.EXPECTED_CONTINUATION())
118
141
  }
119
142
 
120
143
  switch (frame.opcode) {
@@ -123,9 +146,8 @@ module.exports = exports = class WebSocket extends Duplex {
123
146
  this.push(frame.payload)
124
147
  break
125
148
 
126
- case opcode.CLOSE:
127
- this.push(null)
128
- this.end()
149
+ default:
150
+ this.destroy(errors.INVALID_OPCODE())
129
151
  }
130
152
  }
131
153
 
@@ -200,10 +222,10 @@ const handshake = exports.handshake = function handshake (req, cb) {
200
222
  function defaultPort (url) {
201
223
  switch (url.protocol) {
202
224
  case 'ftp:': return 21
203
- case 'http':
204
- case 'ws': return 80
205
- case 'https':
206
- case 'wss': return 443
225
+ case 'http:':
226
+ case 'ws:': return 80
227
+ case 'https:':
228
+ case 'wss:': return 443
207
229
  }
208
230
 
209
231
  return null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-ws",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "WebSocket library for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",