Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
currentInterval = setInterval(() => {
// if current word hasn't changed, don't send another request to image search api
if (currentWord != nextWord) {
currentWord = nextWord;
findImage(currentWord);
}
try {
if (ws.readyState != WebSocket.CLOSED) {
// send image url to client
ws.send(currentImageURL);
}
} catch (e) {
console.log(e);
}
}, 1000);
}
for (var i = 0; i < l; i++) {
buffer[i] = array[o + i];
}
return buffer;
}
//if (this.readyState == WebSocket.OPEN && (this._socket.bufferSize == 0) && packet.build) {
if (this.readyState == WebSocket.OPEN && packet.build) {
var buf = packet.build();
this.send(buf, { binary: true });
} else if (!packet.build) {
// Do nothing
} else {
this.readyState = WebSocket.CLOSED;
this.emit('close');
this.removeAllListeners();
}
};
return new Promise((resolve, reject) => {
const each = 10;
const runs = 3000 / each; // time in ms divided by above
if (this.ws.readyState === WebSocket.CLOSED) {
this._resetWebSocket();
}
const interval = setInterval(() => {
if (this.ws.readyState === WebSocket.OPEN && this.sessionRegistered) {
this._getTicker(tickerName, resolve, reject);
clearInterval(interval);
} else if (!runs) {
reject("WebSocket connection is closed.");
clearInterval(interval);
}
}, each);
});
}
function _writeResponsePreamble(callback) {
const conn = this.connection;
if (conn && conn.readyState === WS.CLOSED) {
// The socket was destroyed. If we're still trying to write to it,
// then we haven't gotten the 'close' event yet.
return false;
}
var hdrs = {};
for( var k in this._headers) {
//if ( k === "set-cookie" || k === "etag") continue;
if ( this._headers[k] instanceof Array ) {
hdrs[k] = this._headers[k][0];
} else {
hdrs[k] = this._headers[k];
}
}
var response = { response : {
connect() {
if (this.state === Connection.State.CONNECTING) {
return Promise.reject(new Error('Already connecting!'))
} else if (this.state === Connection.State.CONNECTED) {
return Promise.reject(new Error('Already connected!'))
}
if (!this.socket || this.socket.readyState === WebSocket.CLOSED) {
try {
this.socket = new WebSocket(this.options.url)
} catch (err) {
this.emit('error', err)
return Promise.reject(err)
}
}
this.socket.binaryType = 'arraybuffer'
this.socket.events = new EventEmitter()
this.socket.onopen = () => this.socket.events.emit('open')
this.socket.onclose = () => this.socket.events.emit('close')
this.updateState(Connection.State.CONNECTING)
this.socket.events.on('open', () => {
debug('Connected to ', this.options.url)
var future = new Future();
_futures[id] = future;
if (context.timeout > 0) {
future = future.timeout(context.timeout).catchError(function(e) {
delete _futures[id];
--_count;
close();
throw e;
},
function(e) {
return e instanceof TimeoutError;
});
}
if (ws === null ||
ws.readyState === WebSocket.CLOSING ||
ws.readyState === WebSocket.CLOSED) {
connect();
}
if (_count < 100) {
++_count;
_ready.then(function() { send(id, request); });
}
else {
_requests.push([id, request]);
}
if (context.oneway) { future.resolve(); }
return future;
}
function close() {
async send(method, params = [], retry = false) {
if (!Array.isArray(params)) {
params = [params];
}
if (
!this.ws ||
!this.ws.readyState ||
this.ws.readyState === WebSocket.CLOSED
) {
ethereumNodeRemoteLog.warn(
`Remote websocket connection not open, attempting to reconnect and retry ${method}...`
);
return new Promise(resolve => {
this.start().then(() => {
resolve(this.send(method, params, retry));
});
});
}
if (this.ws.readyState !== WebSocket.OPEN) {
if (this.ws.readyState === WebSocket.CONNECTING) {
ethereumNodeRemoteLog.error(
`Can't send method ${method} because remote WebSocket is connecting`
);
return new Promise((resolve, reject) => {
if (this.ws) {
this.ws.onclose = (message) => {
this.ws = null;
resolve(message);
};
if (this.ws.readyState !== WebSocket.CLOSED) {
this.ws.close();
} else {
this.ws.onclose();
}
} else {
reject(new Error(`websocket is closed, init the by calling 'open()'`));
}
});
}