@databricks/sql 1.0.0 → 1.1.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/CHANGELOG.md +16 -1
- package/dist/DBSQLClient.d.ts +1 -1
- package/dist/DBSQLClient.js +18 -4
- package/dist/DBSQLClient.js.map +1 -1
- package/dist/DBSQLOperation/SchemaHelper.d.ts +4 -1
- package/dist/DBSQLOperation/SchemaHelper.js +25 -4
- package/dist/DBSQLOperation/SchemaHelper.js.map +1 -1
- package/dist/DBSQLOperation/index.js +2 -3
- package/dist/DBSQLOperation/index.js.map +1 -1
- package/dist/connection/connections/HttpConnection.js +2 -2
- package/dist/connection/connections/HttpConnection.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/result/IOperationResult.d.ts +2 -1
- package/dist/result/JsonResult.d.ts +3 -5
- package/dist/result/JsonResult.js +6 -11
- package/dist/result/JsonResult.js.map +1 -1
- package/dist/utils/buildUserAgentString.js +2 -7
- package/dist/utils/buildUserAgentString.js.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.js +4 -0
- package/dist/version.js.map +1 -0
- package/node_modules/async-limiter/.eslintignore +2 -0
- package/node_modules/async-limiter/.nycrc +10 -0
- package/node_modules/async-limiter/.travis.yml +9 -0
- package/node_modules/async-limiter/LICENSE +8 -0
- package/node_modules/async-limiter/index.js +67 -0
- package/node_modules/async-limiter/package.json +39 -0
- package/node_modules/async-limiter/readme.md +132 -0
- package/node_modules/browser-or-node/LICENSE +21 -0
- package/node_modules/browser-or-node/README.md +63 -0
- package/node_modules/browser-or-node/lib/index.js +31 -0
- package/node_modules/browser-or-node/package.json +64 -0
- package/node_modules/browser-or-node/src/index.js +26 -0
- package/node_modules/isomorphic-ws/LICENSE +21 -0
- package/node_modules/isomorphic-ws/README.md +58 -0
- package/node_modules/isomorphic-ws/browser.js +17 -0
- package/node_modules/isomorphic-ws/index.d.ts +8 -0
- package/node_modules/isomorphic-ws/node.js +3 -0
- package/node_modules/isomorphic-ws/package.json +38 -0
- package/node_modules/node-int64/.npmignore +3 -0
- package/node_modules/node-int64/Int64.js +268 -0
- package/node_modules/node-int64/LICENSE +19 -0
- package/node_modules/node-int64/README.md +78 -0
- package/node_modules/node-int64/package.json +31 -0
- package/node_modules/node-int64/test.js +120 -0
- package/node_modules/q/CHANGES.md +800 -0
- package/node_modules/q/LICENSE +18 -0
- package/node_modules/q/README.md +874 -0
- package/node_modules/q/package.json +80 -0
- package/node_modules/q/q.js +2076 -0
- package/node_modules/q/queue.js +35 -0
- package/node_modules/thrift/LICENSE +306 -0
- package/node_modules/thrift/README.md +210 -0
- package/node_modules/thrift/lib/nodejs/README.md +142 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/binary.js +168 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/binary_protocol.js +367 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/browser.js +49 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/buffered_transport.js +186 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/compact_protocol.js +915 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/connection.js +396 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/create_client.js +54 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/framed_transport.js +185 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/header_protocol.js +258 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/header_transport.js +341 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/http_connection.js +282 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/index.js +76 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/input_buffer_underrun_error.js +32 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/int64_util.js +91 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/json_parse.js +299 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/json_protocol.js +799 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/log.js +87 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/multiplexed_processor.js +63 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/multiplexed_protocol.js +73 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/protocol.js +22 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/server.js +123 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/thrift.js +241 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/transport.js +22 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/web_server.js +567 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/ws_connection.js +286 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/ws_transport.js +206 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/xhr_connection.js +280 -0
- package/node_modules/thrift/package.json +76 -0
- package/node_modules/ws/LICENSE +21 -0
- package/node_modules/ws/README.md +417 -0
- package/node_modules/ws/index.js +9 -0
- package/node_modules/ws/lib/buffer-util.js +72 -0
- package/node_modules/ws/lib/constants.js +10 -0
- package/node_modules/ws/lib/event-target.js +170 -0
- package/node_modules/ws/lib/extension.js +211 -0
- package/node_modules/ws/lib/permessage-deflate.js +516 -0
- package/node_modules/ws/lib/receiver.js +513 -0
- package/node_modules/ws/lib/sender.js +401 -0
- package/node_modules/ws/lib/validation.js +29 -0
- package/node_modules/ws/lib/websocket-server.js +369 -0
- package/node_modules/ws/lib/websocket.js +828 -0
- package/node_modules/ws/package.json +48 -0
- package/package.json +9 -3
- package/dist/DBSQLOperation/getResult.d.ts +0 -2
- package/dist/DBSQLOperation/getResult.js +0 -19
- package/dist/DBSQLOperation/getResult.js.map +0 -1
- package/dist/result/NullResult.d.ts +0 -4
- package/dist/result/NullResult.js +0 -9
- package/dist/result/NullResult.js.map +0 -1
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
# ws: a Node.js WebSocket library
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/ws)
|
|
4
|
+
[](https://travis-ci.org/websockets/ws)
|
|
5
|
+
[](https://ci.appveyor.com/project/lpinca/ws)
|
|
6
|
+
[](https://coveralls.io/r/websockets/ws?branch=master)
|
|
7
|
+
|
|
8
|
+
ws is a simple to use, blazing fast, and thoroughly tested WebSocket client
|
|
9
|
+
and server implementation.
|
|
10
|
+
|
|
11
|
+
Passes the quite extensive Autobahn test suite: [server][server-report],
|
|
12
|
+
[client][client-report].
|
|
13
|
+
|
|
14
|
+
**Note**: This module does not work in the browser. The client in the docs is a
|
|
15
|
+
reference to a back end with the role of a client in the WebSocket
|
|
16
|
+
communication. Browser clients must use the native
|
|
17
|
+
[`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) object.
|
|
18
|
+
To make the same code work seamlessly on Node.js and the browser, you can use
|
|
19
|
+
one of the many wrappers available on npm, like
|
|
20
|
+
[isomorphic-ws](https://github.com/heineiuo/isomorphic-ws).
|
|
21
|
+
|
|
22
|
+
## Table of Contents
|
|
23
|
+
|
|
24
|
+
* [Protocol support](#protocol-support)
|
|
25
|
+
* [Installing](#installing)
|
|
26
|
+
+ [Opt-in for performance and spec compliance](#opt-in-for-performance-and-spec-compliance)
|
|
27
|
+
* [API docs](#api-docs)
|
|
28
|
+
* [WebSocket compression](#websocket-compression)
|
|
29
|
+
* [Usage examples](#usage-examples)
|
|
30
|
+
+ [Sending and receiving text data](#sending-and-receiving-text-data)
|
|
31
|
+
+ [Sending binary data](#sending-binary-data)
|
|
32
|
+
+ [Simple server](#simple-server)
|
|
33
|
+
+ [External HTTP/S server](#external-https-server)
|
|
34
|
+
+ [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
|
|
35
|
+
+ [Server broadcast](#server-broadcast)
|
|
36
|
+
+ [echo.websocket.org demo](#echowebsocketorg-demo)
|
|
37
|
+
+ [Other examples](#other-examples)
|
|
38
|
+
* [Error handling best practices](#error-handling-best-practices)
|
|
39
|
+
* [FAQ](#faq)
|
|
40
|
+
+ [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
|
|
41
|
+
+ [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
|
|
42
|
+
+ [How to connect via a proxy?](#how-to-connect-via-a-proxy)
|
|
43
|
+
* [Changelog](#changelog)
|
|
44
|
+
* [License](#license)
|
|
45
|
+
|
|
46
|
+
## Protocol support
|
|
47
|
+
|
|
48
|
+
* **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
|
|
49
|
+
* **HyBi drafts 13-17** (Current default, alternatively option `protocolVersion: 13`)
|
|
50
|
+
|
|
51
|
+
## Installing
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
npm install --save ws
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Opt-in for performance and spec compliance
|
|
58
|
+
|
|
59
|
+
There are 2 optional modules that can be installed along side with the ws
|
|
60
|
+
module. These modules are binary addons which improve certain operations.
|
|
61
|
+
Prebuilt binaries are available for the most popular platforms so you don't
|
|
62
|
+
necessarily need to have a C++ compiler installed on your machine.
|
|
63
|
+
|
|
64
|
+
- `npm install --save-optional bufferutil`: Allows to efficiently perform
|
|
65
|
+
operations such as masking and unmasking the data payload of the WebSocket
|
|
66
|
+
frames.
|
|
67
|
+
- `npm install --save-optional utf-8-validate`: Allows to efficiently check
|
|
68
|
+
if a message contains valid UTF-8 as required by the spec.
|
|
69
|
+
|
|
70
|
+
## API docs
|
|
71
|
+
|
|
72
|
+
See [`/doc/ws.md`](./doc/ws.md) for Node.js-like docs for the ws classes.
|
|
73
|
+
|
|
74
|
+
## WebSocket compression
|
|
75
|
+
|
|
76
|
+
ws supports the [permessage-deflate extension][permessage-deflate] which
|
|
77
|
+
enables the client and server to negotiate a compression algorithm and its
|
|
78
|
+
parameters, and then selectively apply it to the data payloads of each
|
|
79
|
+
WebSocket message.
|
|
80
|
+
|
|
81
|
+
The extension is disabled by default on the server and enabled by default on
|
|
82
|
+
the client. It adds a significant overhead in terms of performance and memory
|
|
83
|
+
consumption so we suggest to enable it only if it is really needed.
|
|
84
|
+
|
|
85
|
+
Note that Node.js has a variety of issues with high-performance compression,
|
|
86
|
+
where increased concurrency, especially on Linux, can lead to
|
|
87
|
+
[catastrophic memory fragmentation][node-zlib-bug] and slow performance.
|
|
88
|
+
If you intend to use permessage-deflate in production, it is worthwhile to set
|
|
89
|
+
up a test representative of your workload and ensure Node.js/zlib will handle
|
|
90
|
+
it with acceptable performance and memory usage.
|
|
91
|
+
|
|
92
|
+
Tuning of permessage-deflate can be done via the options defined below. You can
|
|
93
|
+
also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
|
|
94
|
+
into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
|
|
95
|
+
|
|
96
|
+
See [the docs][ws-server-options] for more options.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
const WebSocket = require('ws');
|
|
100
|
+
|
|
101
|
+
const wss = new WebSocket.Server({
|
|
102
|
+
port: 8080,
|
|
103
|
+
perMessageDeflate: {
|
|
104
|
+
zlibDeflateOptions: { // See zlib defaults.
|
|
105
|
+
chunkSize: 1024,
|
|
106
|
+
memLevel: 7,
|
|
107
|
+
level: 3,
|
|
108
|
+
},
|
|
109
|
+
zlibInflateOptions: {
|
|
110
|
+
chunkSize: 10 * 1024
|
|
111
|
+
},
|
|
112
|
+
// Other options settable:
|
|
113
|
+
clientNoContextTakeover: true, // Defaults to negotiated value.
|
|
114
|
+
serverNoContextTakeover: true, // Defaults to negotiated value.
|
|
115
|
+
clientMaxWindowBits: 10, // Defaults to negotiated value.
|
|
116
|
+
serverMaxWindowBits: 10, // Defaults to negotiated value.
|
|
117
|
+
// Below options specified as default values.
|
|
118
|
+
concurrencyLimit: 10, // Limits zlib concurrency for perf.
|
|
119
|
+
threshold: 1024, // Size (in bytes) below which messages
|
|
120
|
+
// should not be compressed.
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The client will only use the extension if it is supported and enabled on the
|
|
126
|
+
server. To always disable the extension on the client set the
|
|
127
|
+
`perMessageDeflate` option to `false`.
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
const WebSocket = require('ws');
|
|
131
|
+
|
|
132
|
+
const ws = new WebSocket('ws://www.host.com/path', {
|
|
133
|
+
perMessageDeflate: false
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Usage examples
|
|
138
|
+
|
|
139
|
+
### Sending and receiving text data
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
const WebSocket = require('ws');
|
|
143
|
+
|
|
144
|
+
const ws = new WebSocket('ws://www.host.com/path');
|
|
145
|
+
|
|
146
|
+
ws.on('open', function open() {
|
|
147
|
+
ws.send('something');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
ws.on('message', function incoming(data) {
|
|
151
|
+
console.log(data);
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Sending binary data
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
const WebSocket = require('ws');
|
|
159
|
+
|
|
160
|
+
const ws = new WebSocket('ws://www.host.com/path');
|
|
161
|
+
|
|
162
|
+
ws.on('open', function open() {
|
|
163
|
+
const array = new Float32Array(5);
|
|
164
|
+
|
|
165
|
+
for (var i = 0; i < array.length; ++i) {
|
|
166
|
+
array[i] = i / 2;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
ws.send(array);
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Simple server
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
const WebSocket = require('ws');
|
|
177
|
+
|
|
178
|
+
const wss = new WebSocket.Server({ port: 8080 });
|
|
179
|
+
|
|
180
|
+
wss.on('connection', function connection(ws) {
|
|
181
|
+
ws.on('message', function incoming(message) {
|
|
182
|
+
console.log('received: %s', message);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
ws.send('something');
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### External HTTP/S server
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
const fs = require('fs');
|
|
193
|
+
const https = require('https');
|
|
194
|
+
const WebSocket = require('ws');
|
|
195
|
+
|
|
196
|
+
const server = new https.createServer({
|
|
197
|
+
cert: fs.readFileSync('/path/to/cert.pem'),
|
|
198
|
+
key: fs.readFileSync('/path/to/key.pem')
|
|
199
|
+
});
|
|
200
|
+
const wss = new WebSocket.Server({ server });
|
|
201
|
+
|
|
202
|
+
wss.on('connection', function connection(ws) {
|
|
203
|
+
ws.on('message', function incoming(message) {
|
|
204
|
+
console.log('received: %s', message);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
ws.send('something');
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
server.listen(8080);
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Multiple servers sharing a single HTTP/S server
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
const http = require('http');
|
|
217
|
+
const WebSocket = require('ws');
|
|
218
|
+
|
|
219
|
+
const server = http.createServer();
|
|
220
|
+
const wss1 = new WebSocket.Server({ noServer: true });
|
|
221
|
+
const wss2 = new WebSocket.Server({ noServer: true });
|
|
222
|
+
|
|
223
|
+
wss1.on('connection', function connection(ws) {
|
|
224
|
+
// ...
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
wss2.on('connection', function connection(ws) {
|
|
228
|
+
// ...
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
server.on('upgrade', function upgrade(request, socket, head) {
|
|
232
|
+
const pathname = url.parse(request.url).pathname;
|
|
233
|
+
|
|
234
|
+
if (pathname === '/foo') {
|
|
235
|
+
wss1.handleUpgrade(request, socket, head, function done(ws) {
|
|
236
|
+
wss1.emit('connection', ws, request);
|
|
237
|
+
});
|
|
238
|
+
} else if (pathname === '/bar') {
|
|
239
|
+
wss2.handleUpgrade(request, socket, head, function done(ws) {
|
|
240
|
+
wss2.emit('connection', ws, request);
|
|
241
|
+
});
|
|
242
|
+
} else {
|
|
243
|
+
socket.destroy();
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
server.listen(8080);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Server broadcast
|
|
251
|
+
|
|
252
|
+
```js
|
|
253
|
+
const WebSocket = require('ws');
|
|
254
|
+
|
|
255
|
+
const wss = new WebSocket.Server({ port: 8080 });
|
|
256
|
+
|
|
257
|
+
// Broadcast to all.
|
|
258
|
+
wss.broadcast = function broadcast(data) {
|
|
259
|
+
wss.clients.forEach(function each(client) {
|
|
260
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
261
|
+
client.send(data);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
wss.on('connection', function connection(ws) {
|
|
267
|
+
ws.on('message', function incoming(data) {
|
|
268
|
+
// Broadcast to everyone else.
|
|
269
|
+
wss.clients.forEach(function each(client) {
|
|
270
|
+
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
|
271
|
+
client.send(data);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### echo.websocket.org demo
|
|
279
|
+
|
|
280
|
+
```js
|
|
281
|
+
const WebSocket = require('ws');
|
|
282
|
+
|
|
283
|
+
const ws = new WebSocket('wss://echo.websocket.org/', {
|
|
284
|
+
origin: 'https://websocket.org'
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
ws.on('open', function open() {
|
|
288
|
+
console.log('connected');
|
|
289
|
+
ws.send(Date.now());
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
ws.on('close', function close() {
|
|
293
|
+
console.log('disconnected');
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
ws.on('message', function incoming(data) {
|
|
297
|
+
console.log(`Roundtrip time: ${Date.now() - data} ms`);
|
|
298
|
+
|
|
299
|
+
setTimeout(function timeout() {
|
|
300
|
+
ws.send(Date.now());
|
|
301
|
+
}, 500);
|
|
302
|
+
});
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Other examples
|
|
306
|
+
|
|
307
|
+
For a full example with a browser client communicating with a ws server, see the
|
|
308
|
+
examples folder.
|
|
309
|
+
|
|
310
|
+
Otherwise, see the test cases.
|
|
311
|
+
|
|
312
|
+
## Error handling best practices
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
// If the WebSocket is closed before the following send is attempted
|
|
316
|
+
ws.send('something');
|
|
317
|
+
|
|
318
|
+
// Errors (both immediate and async write errors) can be detected in an optional
|
|
319
|
+
// callback. The callback is also the only way of being notified that data has
|
|
320
|
+
// actually been sent.
|
|
321
|
+
ws.send('something', function ack(error) {
|
|
322
|
+
// If error is not defined, the send has been completed, otherwise the error
|
|
323
|
+
// object will indicate what failed.
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// Immediate errors can also be handled with `try...catch`, but **note** that
|
|
327
|
+
// since sends are inherently asynchronous, socket write failures will *not* be
|
|
328
|
+
// captured when this technique is used.
|
|
329
|
+
try { ws.send('something'); }
|
|
330
|
+
catch (e) { /* handle error */ }
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## FAQ
|
|
334
|
+
|
|
335
|
+
### How to get the IP address of the client?
|
|
336
|
+
|
|
337
|
+
The remote IP address can be obtained from the raw socket.
|
|
338
|
+
|
|
339
|
+
```js
|
|
340
|
+
const WebSocket = require('ws');
|
|
341
|
+
|
|
342
|
+
const wss = new WebSocket.Server({ port: 8080 });
|
|
343
|
+
|
|
344
|
+
wss.on('connection', function connection(ws, req) {
|
|
345
|
+
const ip = req.connection.remoteAddress;
|
|
346
|
+
});
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
When the server runs behind a proxy like NGINX, the de-facto standard is to use
|
|
350
|
+
the `X-Forwarded-For` header.
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
wss.on('connection', function connection(ws, req) {
|
|
354
|
+
const ip = req.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
|
|
355
|
+
});
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
### How to detect and close broken connections?
|
|
359
|
+
|
|
360
|
+
Sometimes the link between the server and the client can be interrupted in a
|
|
361
|
+
way that keeps both the server and the client unaware of the broken state of the
|
|
362
|
+
connection (e.g. when pulling the cord).
|
|
363
|
+
|
|
364
|
+
In these cases ping messages can be used as a means to verify that the remote
|
|
365
|
+
endpoint is still responsive.
|
|
366
|
+
|
|
367
|
+
```js
|
|
368
|
+
const WebSocket = require('ws');
|
|
369
|
+
|
|
370
|
+
const wss = new WebSocket.Server({ port: 8080 });
|
|
371
|
+
|
|
372
|
+
function noop() {}
|
|
373
|
+
|
|
374
|
+
function heartbeat() {
|
|
375
|
+
this.isAlive = true;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
wss.on('connection', function connection(ws) {
|
|
379
|
+
ws.isAlive = true;
|
|
380
|
+
ws.on('pong', heartbeat);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
const interval = setInterval(function ping() {
|
|
384
|
+
wss.clients.forEach(function each(ws) {
|
|
385
|
+
if (ws.isAlive === false) return ws.terminate();
|
|
386
|
+
|
|
387
|
+
ws.isAlive = false;
|
|
388
|
+
ws.ping(noop);
|
|
389
|
+
});
|
|
390
|
+
}, 30000);
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
Pong messages are automatically sent in response to ping messages as required
|
|
394
|
+
by the spec.
|
|
395
|
+
|
|
396
|
+
### How to connect via a proxy?
|
|
397
|
+
|
|
398
|
+
Use a custom `http.Agent` implementation like [https-proxy-agent][] or
|
|
399
|
+
[socks-proxy-agent][].
|
|
400
|
+
|
|
401
|
+
## Changelog
|
|
402
|
+
|
|
403
|
+
We're using the GitHub [releases][changelog] for changelog entries.
|
|
404
|
+
|
|
405
|
+
## License
|
|
406
|
+
|
|
407
|
+
[MIT](LICENSE)
|
|
408
|
+
|
|
409
|
+
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
|
|
410
|
+
[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
|
|
411
|
+
[client-report]: http://websockets.github.io/ws/autobahn/clients/
|
|
412
|
+
[server-report]: http://websockets.github.io/ws/autobahn/servers/
|
|
413
|
+
[permessage-deflate]: https://tools.ietf.org/html/rfc7692
|
|
414
|
+
[changelog]: https://github.com/websockets/ws/releases
|
|
415
|
+
[node-zlib-bug]: https://github.com/nodejs/node/issues/8871
|
|
416
|
+
[node-zlib-deflaterawdocs]: https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
|
|
417
|
+
[ws-server-options]: https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Merges an array of buffers into a new buffer.
|
|
5
|
+
*
|
|
6
|
+
* @param {Buffer[]} list The array of buffers to concat
|
|
7
|
+
* @param {Number} totalLength The total length of buffers in the list
|
|
8
|
+
* @return {Buffer} The resulting buffer
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
function concat (list, totalLength) {
|
|
12
|
+
const target = Buffer.allocUnsafe(totalLength);
|
|
13
|
+
var offset = 0;
|
|
14
|
+
|
|
15
|
+
for (var i = 0; i < list.length; i++) {
|
|
16
|
+
const buf = list[i];
|
|
17
|
+
buf.copy(target, offset);
|
|
18
|
+
offset += buf.length;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return target;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Masks a buffer using the given mask.
|
|
26
|
+
*
|
|
27
|
+
* @param {Buffer} source The buffer to mask
|
|
28
|
+
* @param {Buffer} mask The mask to use
|
|
29
|
+
* @param {Buffer} output The buffer where to store the result
|
|
30
|
+
* @param {Number} offset The offset at which to start writing
|
|
31
|
+
* @param {Number} length The number of bytes to mask.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
function _mask (source, mask, output, offset, length) {
|
|
35
|
+
for (var i = 0; i < length; i++) {
|
|
36
|
+
output[offset + i] = source[i] ^ mask[i & 3];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Unmasks a buffer using the given mask.
|
|
42
|
+
*
|
|
43
|
+
* @param {Buffer} buffer The buffer to unmask
|
|
44
|
+
* @param {Buffer} mask The mask to use
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
function _unmask (buffer, mask) {
|
|
48
|
+
// Required until https://github.com/nodejs/node/issues/9006 is resolved.
|
|
49
|
+
const length = buffer.length;
|
|
50
|
+
for (var i = 0; i < length; i++) {
|
|
51
|
+
buffer[i] ^= mask[i & 3];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const bufferUtil = require('bufferutil');
|
|
57
|
+
const bu = bufferUtil.BufferUtil || bufferUtil;
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
mask (source, mask, output, offset, length) {
|
|
61
|
+
if (length < 48) _mask(source, mask, output, offset, length);
|
|
62
|
+
else bu.mask(source, mask, output, offset, length);
|
|
63
|
+
},
|
|
64
|
+
unmask (buffer, mask) {
|
|
65
|
+
if (buffer.length < 32) _unmask(buffer, mask);
|
|
66
|
+
else bu.unmask(buffer, mask);
|
|
67
|
+
},
|
|
68
|
+
concat
|
|
69
|
+
};
|
|
70
|
+
} catch (e) /* istanbul ignore next */ {
|
|
71
|
+
module.exports = { concat, mask: _mask, unmask: _unmask };
|
|
72
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
|
|
5
|
+
GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
|
|
6
|
+
kStatusCode: Symbol('status-code'),
|
|
7
|
+
kWebSocket: Symbol('websocket'),
|
|
8
|
+
EMPTY_BUFFER: Buffer.alloc(0),
|
|
9
|
+
NOOP: () => {}
|
|
10
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Class representing an event.
|
|
5
|
+
*
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
8
|
+
class Event {
|
|
9
|
+
/**
|
|
10
|
+
* Create a new `Event`.
|
|
11
|
+
*
|
|
12
|
+
* @param {String} type The name of the event
|
|
13
|
+
* @param {Object} target A reference to the target to which the event was dispatched
|
|
14
|
+
*/
|
|
15
|
+
constructor (type, target) {
|
|
16
|
+
this.target = target;
|
|
17
|
+
this.type = type;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Class representing a message event.
|
|
23
|
+
*
|
|
24
|
+
* @extends Event
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
class MessageEvent extends Event {
|
|
28
|
+
/**
|
|
29
|
+
* Create a new `MessageEvent`.
|
|
30
|
+
*
|
|
31
|
+
* @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
|
|
32
|
+
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
|
33
|
+
*/
|
|
34
|
+
constructor (data, target) {
|
|
35
|
+
super('message', target);
|
|
36
|
+
|
|
37
|
+
this.data = data;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Class representing a close event.
|
|
43
|
+
*
|
|
44
|
+
* @extends Event
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
class CloseEvent extends Event {
|
|
48
|
+
/**
|
|
49
|
+
* Create a new `CloseEvent`.
|
|
50
|
+
*
|
|
51
|
+
* @param {Number} code The status code explaining why the connection is being closed
|
|
52
|
+
* @param {String} reason A human-readable string explaining why the connection is closing
|
|
53
|
+
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
|
54
|
+
*/
|
|
55
|
+
constructor (code, reason, target) {
|
|
56
|
+
super('close', target);
|
|
57
|
+
|
|
58
|
+
this.wasClean = target._closeFrameReceived && target._closeFrameSent;
|
|
59
|
+
this.reason = reason;
|
|
60
|
+
this.code = code;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Class representing an open event.
|
|
66
|
+
*
|
|
67
|
+
* @extends Event
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
class OpenEvent extends Event {
|
|
71
|
+
/**
|
|
72
|
+
* Create a new `OpenEvent`.
|
|
73
|
+
*
|
|
74
|
+
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
|
75
|
+
*/
|
|
76
|
+
constructor (target) {
|
|
77
|
+
super('open', target);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Class representing an error event.
|
|
83
|
+
*
|
|
84
|
+
* @extends Event
|
|
85
|
+
* @private
|
|
86
|
+
*/
|
|
87
|
+
class ErrorEvent extends Event {
|
|
88
|
+
/**
|
|
89
|
+
* Create a new `ErrorEvent`.
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} error The error that generated this event
|
|
92
|
+
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
|
93
|
+
*/
|
|
94
|
+
constructor (error, target) {
|
|
95
|
+
super('error', target);
|
|
96
|
+
|
|
97
|
+
this.message = error.message;
|
|
98
|
+
this.error = error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* This provides methods for emulating the `EventTarget` interface. It's not
|
|
104
|
+
* meant to be used directly.
|
|
105
|
+
*
|
|
106
|
+
* @mixin
|
|
107
|
+
*/
|
|
108
|
+
const EventTarget = {
|
|
109
|
+
/**
|
|
110
|
+
* Register an event listener.
|
|
111
|
+
*
|
|
112
|
+
* @param {String} method A string representing the event type to listen for
|
|
113
|
+
* @param {Function} listener The listener to add
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
addEventListener (method, listener) {
|
|
117
|
+
if (typeof listener !== 'function') return;
|
|
118
|
+
|
|
119
|
+
function onMessage (data) {
|
|
120
|
+
listener.call(this, new MessageEvent(data, this));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function onClose (code, message) {
|
|
124
|
+
listener.call(this, new CloseEvent(code, message, this));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function onError (error) {
|
|
128
|
+
listener.call(this, new ErrorEvent(error, this));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function onOpen () {
|
|
132
|
+
listener.call(this, new OpenEvent(this));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (method === 'message') {
|
|
136
|
+
onMessage._listener = listener;
|
|
137
|
+
this.on(method, onMessage);
|
|
138
|
+
} else if (method === 'close') {
|
|
139
|
+
onClose._listener = listener;
|
|
140
|
+
this.on(method, onClose);
|
|
141
|
+
} else if (method === 'error') {
|
|
142
|
+
onError._listener = listener;
|
|
143
|
+
this.on(method, onError);
|
|
144
|
+
} else if (method === 'open') {
|
|
145
|
+
onOpen._listener = listener;
|
|
146
|
+
this.on(method, onOpen);
|
|
147
|
+
} else {
|
|
148
|
+
this.on(method, listener);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Remove an event listener.
|
|
154
|
+
*
|
|
155
|
+
* @param {String} method A string representing the event type to remove
|
|
156
|
+
* @param {Function} listener The listener to remove
|
|
157
|
+
* @public
|
|
158
|
+
*/
|
|
159
|
+
removeEventListener (method, listener) {
|
|
160
|
+
const listeners = this.listeners(method);
|
|
161
|
+
|
|
162
|
+
for (var i = 0; i < listeners.length; i++) {
|
|
163
|
+
if (listeners[i] === listener || listeners[i]._listener === listener) {
|
|
164
|
+
this.removeListener(method, listeners[i]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
module.exports = EventTarget;
|