Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
helpers.dummyWebClients(wsServer, dummyClients, (err, sockets, messages) => {
if (err) throw err
assert.deepEqual(
_.pluck(Array.from(wsServer._wsServer.clients).slice(0, 5), 'readyState'),
_.range(5).map(() => WebSocket.OPEN)
)
// Check that the last socket received connection rejected
var lastMsg = messages.pop()
assert.equal(lastMsg.length, 2)
assert.equal(lastMsg[0], 1)
assert.ok(_.isString(lastMsg[1]))
assert.equal(_.last(Array.from(wsServer._wsServer.clients)).readyState, WebSocket.CLOSING)
// Check that all sockets before got connection accepted
messages.forEach((msg) => {
assert.equal(msg.length, 2)
assert.equal(msg[0], 0)
assert.ok(_.isString(msg[1]))
})
done()
})
})
client.onFlush = wireMsg => {
if (ws.readyState === WebSocket.CLOSING || ws.readyState === WebSocket.CLOSED) {
// Fail silently as we will soon receive the close event which will trigger the cleanup.
return
}
try {
ws.send(wireMsg, error => {
if (error !== undefined) {
console.error(error)
ws.close()
}
})
} catch (error) {
console.error(error)
ws.close()
}
}
const MessageEvent = require("../generated/MessageEvent");
const CONNECTING = 0;
const OPEN = 1;
const CLOSING = 2;
const CLOSED = 3;
const productions = {
// https://tools.ietf.org/html/rfc7230#section-3.2.6
token: /^[!#$%&'*+\-.^_`|~\dA-Za-z]+$/
};
const readyStateWSToDOM = [];
readyStateWSToDOM[WebSocket.CONNECTING] = CONNECTING;
readyStateWSToDOM[WebSocket.OPEN] = OPEN;
readyStateWSToDOM[WebSocket.CLOSING] = CLOSING;
readyStateWSToDOM[WebSocket.CLOSED] = CLOSED;
// https://tools.ietf.org/html/rfc6455#section-4.3
// See Sec-WebSocket-Protocol-Client, which is for the syntax of an entire header value. This function checks if a
// single header conforms to the rules.
function verifySecWebSocketProtocol(str) {
return productions.token.test(str);
}
class PromiseQueues extends WeakMap {
get(window) {
const cur = super.get(window);
return cur !== undefined ? cur : Promise.resolve();
}
}
, sendJSON = shared.sendJSON
, EventEmitter = require('events').EventEmitter
, WebSocket = require('ws') // polyfilling not required -> https://github.com/einaros/ws/blob/master/lib/browser.js
, isBrowser = typeof window !== 'undefined'
, Blob = isBrowser ? window.Blob : Buffer
_.extend(exports, new EventEmitter)
// Context variables
var socket, socketEvents, blobTransaction
// Maps socket state to client status
if (WebSocket) {
var wsStates = _.object([
[WebSocket.CONNECTING, 'stopped'],
[WebSocket.OPEN, 'started'],
[WebSocket.CLOSING, 'stopped'],
[WebSocket.CLOSED, 'stopped']
])
}
// ========================= PUBLIC API ========================= //
// Unique id of the client
exports.userId = null
// Configuration of the client
exports.config = {
// Sets the `time` to wait before attempting reconnection.
reconnect: function(time) {
if (_.isNumber(time)) config.reconnect = time
else throw new Error('`time` should be a number')
const check = (k) => {
if (k <= 0)
reject(new Error("failed to await WebSocket ready-state OPEN"))
else if (this._ws.readyState === WebSocket.CLOSED)
reject(new Error("failed to send to WebSocket, already in ready-state CLOSED"))
else if (this._ws.readyState === WebSocket.CLOSING)
reject(new Error("failed to send to WebSocket, already in ready-state CLOSING"))
else if (this._ws.readyState === WebSocket.CONNECTING)
setTimeout(() => check(k - 1), 100)
else
resolve()
}
check(100)
connect() {
if (
this.socket &&
(this.socket.readyState !== Websocket.CLOSED &&
this.socket.readyState !== Websocket.CLOSING)
) {
throw new Error('Could not connect (not disconnected)');
}
this.socket = new Websocket(this.websocketURI);
this.socket.on('message', this.onMessage.bind(this));
this.socket.on('open', this.onOpen.bind(this));
this.socket.on('close', this.onClose.bind(this));
this.socket.on('error', this.onError.bind(this));
}
ConnectionService.prototype.sendPing = function (self) {
if (self.ws.readyState == WebSocket.CLOSING || self.ws.readyState == WebSocket.CLOSED) {
if (!self.ws.reconnecting) {
clearTimeout(self.pingIntervalTimer);
clearTimeout(self.pingTimeoutTimer);
self.reconnect();
}
}
else {
self.lastPingId = uuid.v4();
self.ws.send(JSON.stringify({
type: 'ping',
id: self.lastPingId
}));
}
}