-
Notifications
You must be signed in to change notification settings - Fork 12
/
wrapper.js
312 lines (293 loc) · 8.59 KB
/
wrapper.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"use strict";
const WebSocketChannel = require("./channel");
class WebSocketWrapper extends WebSocketChannel {
constructor(socket, options) {
// Make `this` a WebSocketChannel
super();
this._wrapper = this;
options = options || {};
if(typeof options.debug === "function") {
this._debug = options.debug;
} else if(options.debug === true) {
this._debug = console.log.bind(console);
} else {
this._debug = () => {}; // no-op
}
if(typeof options.errorToJSON !== "function") {
// Provide default error serialization
this._errorToJSON = (err) => {
if(typeof window === "undefined") {
// Node.js environment only serializes `message`
return JSON.stringify({"message": err.message});
} else {
// Browser serializes all error properties
return JSON.stringify(err,
Object.getOwnPropertyNames(err) );
}
};
} else {
this._errorToJSON = options.errorToJSON;
}
if(options.requestTimeout > 0)
this._requestTimeout = options.requestTimeout | 0;
// Flag set once the socket is opened
this._opened = false;
// Array of data to be sent once the connection is opened
this._pendingSend = [];
// Incrementing request ID counter for this WebSocket
this._lastRequestId = 0;
/* Object of pending requests; keys are the request ID, values are
Objects containing `resolve` and `reject` functions used to
resolve the request's Promise. */
this._pendingRequests = {};
/* Object of WebSocketChannels (except `this` associated with this
WebSocket); keys are the channel name. */
this._channels = {};
// Object containing user-assigned socket data
this._data = {};
// Bind this wrapper to the `socket` passed to the constructor
this._socket = null;
if(socket && socket.constructor) {
this.bind(socket);
}
}
bind(socket) {
// Clean up any event handlers on `this._socket`
if(this._socket) {
var s = this._socket;
s.onopen = s.onmessage = s.onerror = s.onclose = null;
}
// Save the `socket` and add event listeners
this._socket = socket;
socket.onopen = (event) => {
this._opened = true;
this._debug("socket: onopen");
// Send all pending messages
for(var i = 0; i < this._pendingSend.length; i++) {
if(this.isConnected) {
this._debug("wrapper: Sending pending message:",
this._pendingSend[i]);
try {
this._socket.send(this._pendingSend[i]);
} catch(e) {
this._pendingSend = this._pendingSend.slice(i - 1);
throw e;
}
} else {
break;
}
}
this._pendingSend = this._pendingSend.slice(i);
this.emit("open", event);
this.emit("connect", event);
};
socket.onmessage = (event) => {
this._debug("socket: onmessage", event.data);
this.emit("message", event, event.data);
this._onMessage(event.data);
};
socket.onerror = (event) => {
this._debug("socket: onerror", event);
this.emit("error", event);
};
socket.onclose = (event) => {
var opened = this._opened;
this._opened = false;
this._debug("socket: onclose", event);
this.emit("close", event, opened);
this.emit("disconnect", event, opened);
};
// If the socket is already open, send all pending messages now
if(this.isConnected) {
socket.onopen();
}
return this;
}
get socket() {
return this._socket;
}
set socket(socket) {
this.bind(socket);
}
// Rejects all pending requests and then clears the send queue
abort() {
for(var id in this._pendingRequests) {
this._pendingRequests[id].reject(new Error("Request was aborted") );
}
this._pendingRequests = {};
this._pendingSend = [];
return this;
}
// Returns a channel with the specified `namespace`
of(namespace) {
if(namespace == null) {
return this;
}
if(!this._channels[namespace]) {
this._channels[namespace] = new WebSocketChannel(namespace, this);
}
return this._channels[namespace];
}
get isConnecting() {
return this._socket && this._socket.readyState ===
this._socket.constructor.CONNECTING;
}
get isConnected() {
return this._socket && this._socket.readyState ===
this._socket.constructor.OPEN;
}
send(data, ignoreMaxQueueSize) {
if(this.isConnected) {
this._debug("wrapper: Sending message:", data);
this._socket.send(data);
} else if(ignoreMaxQueueSize ||
this._pendingSend.length < WebSocketWrapper.MAX_SEND_QUEUE_SIZE)
{
this._debug("wrapper: Queuing message:", data);
this._pendingSend.push(data);
} else {
throw new Error("WebSocket is not connected and send queue is full");
}
return this;
}
disconnect() {
if(this._socket)
this._socket.close.apply(this._socket, arguments);
return this;
}
// Called whenever the bound Socket receives a message
_onMessage(msg) {
try {
msg = JSON.parse(msg);
// If `msg` contains special ignore property, we'll ignore it
if(msg["ws-wrapper"] === false)
return;
if(msg.a) {
var argsArray = [];
for(var i in msg.a) {
argsArray[i] = msg.a[i];
}
msg.a = argsArray;
}
/* If `msg` does not have an `a` Array with at least 1 element,
ignore the message because it is not a valid event/request */
if(msg.a instanceof Array && msg.a.length >= 1 &&
(msg.c || WebSocketChannel.NO_WRAP_EVENTS.indexOf(msg.a[0]) < 0) )
{
// Process inbound event/request
var event = {
"name": msg.a.shift(),
"args": msg.a,
"requestId": msg.i
};
var channel = msg.c == null ? this : this._channels[msg.c];
if(!channel) {
if(msg.i >= 0) {
this._sendReject(msg.i, new Error(
`Channel '${msg.c}' does not exist`
) );
}
this._debug(`wrapper: Event '${event.name}' ignored ` +
`because channel '${msg.c}' does not exist.`);
} else if(channel._emitter.emit(event.name, event) ) {
this._debug(`wrapper: Event '${event.name}' sent to ` +
"event listener");
} else {
if(msg.i >= 0) {
this._sendReject(msg.i, new Error(
"No event listener for '" + event.name + "'" +
(msg.c ? " on channel '" + msg.c + "'" : "")
) );
}
this._debug(`wrapper: Event '${event.name}' had no ` +
"event listener");
}
} else if(this._pendingRequests[msg.i]) {
this._debug("wrapper: Processing response for request", msg.i);
// Process response to prior request
if(msg.e !== undefined) {
var err = msg.e;
// `msg._` indicates that `msg.e` is an Error
if(msg._ && err) {
err = new Error(err.message);
// Copy other properties to Error
for(var key in msg.e) {
err[key] = msg.e[key];
}
}
this._pendingRequests[msg.i].reject(err);
} else {
this._pendingRequests[msg.i].resolve(msg.d);
}
clearTimeout(this._pendingRequests[msg.i].timer);
delete this._pendingRequests[msg.i];
}
// else ignore the message because it's not valid
} catch(e) {
// Non-JSON messages are ignored
/* Note: It's also possible for uncaught exceptions from event
handlers to end up here. */
}
}
/* The following methods are called by a WebSocketChannel to send data
to the Socket. */
_sendEvent(channel, eventName, args, isRequest) {
// Serialize data for sending over the socket
var data = {"a": Array.prototype.slice.call(args)};
if(channel != null) {
data.c = channel;
}
var request;
if(isRequest) {
/* Unless we send petabytes of data using the same socket,
we won't worry about `_lastRequestId` getting too big. */
data.i = ++this._lastRequestId;
// Return a Promise to the caller to be resolved later
request = new Promise((resolve, reject) => {
var pendReq = this._pendingRequests[data.i] = {
"resolve": resolve,
"reject": reject
};
if(this._requestTimeout > 0) {
pendReq.timer = setTimeout(() => {
reject(new Error("Request timed out") );
delete this._pendingRequests[data.i];
}, this._requestTimeout);
}
});
}
// Send the message
this.send(JSON.stringify(data) );
// Return the request, if needed
return request;
}
_sendResolve(id, data) {
this.send(JSON.stringify({
"i": id,
"d": data
}), true /* ignore max queue length */);
}
_sendReject(id, err) {
var isError = err instanceof Error;
if(isError) {
err = JSON.parse(this._errorToJSON(err) );
}
this.send(JSON.stringify({
"i": id,
"e": err,
"_": isError ? 1 : undefined
}), true /* ignore max queue length */);
}
get(key) {
return this._data[key];
}
set(key, value) {
this._data[key] = value;
return this;
}
}
/* Maximum number of items in the send queue. If a user tries to send more
messages than this number while a WebSocket is not connected, errors will
be thrown. */
WebSocketWrapper.MAX_SEND_QUEUE_SIZE = 10;
module.exports = WebSocketWrapper;