Eslintify all of libtextsecure
This commit is contained in:
parent
4b3f9e969a
commit
0774ba2903
36 changed files with 1960 additions and 2128 deletions
|
@ -1,6 +1,7 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
/* global window, dcodeIO, Event, textsecure, FileReader, WebSocketResource */
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
(function() {
|
||||
/*
|
||||
* WebSocket-Resources
|
||||
*
|
||||
|
@ -23,7 +24,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
var Request = function(options) {
|
||||
const Request = function Request(options) {
|
||||
this.verb = options.verb || options.type;
|
||||
this.path = options.path || options.url;
|
||||
this.body = options.body || options.data;
|
||||
|
@ -32,7 +33,7 @@
|
|||
this.id = options.id;
|
||||
|
||||
if (this.id === undefined) {
|
||||
var bits = new Uint32Array(2);
|
||||
const bits = new Uint32Array(2);
|
||||
window.crypto.getRandomValues(bits);
|
||||
this.id = dcodeIO.Long.fromBits(bits[0], bits[1], true);
|
||||
}
|
||||
|
@ -42,19 +43,19 @@
|
|||
}
|
||||
};
|
||||
|
||||
var IncomingWebSocketRequest = function(options) {
|
||||
var request = new Request(options);
|
||||
var socket = options.socket;
|
||||
const IncomingWebSocketRequest = function IncomingWebSocketRequest(options) {
|
||||
const request = new Request(options);
|
||||
const { socket } = options;
|
||||
|
||||
this.verb = request.verb;
|
||||
this.path = request.path;
|
||||
this.body = request.body;
|
||||
|
||||
this.respond = function(status, message) {
|
||||
this.respond = (status, message) => {
|
||||
socket.send(
|
||||
new textsecure.protobuf.WebSocketMessage({
|
||||
type: textsecure.protobuf.WebSocketMessage.Type.RESPONSE,
|
||||
response: { id: request.id, message: message, status: status },
|
||||
response: { id: request.id, message, status },
|
||||
})
|
||||
.encode()
|
||||
.toArrayBuffer()
|
||||
|
@ -62,9 +63,12 @@
|
|||
};
|
||||
};
|
||||
|
||||
var outgoing = {};
|
||||
var OutgoingWebSocketRequest = function(options, socket) {
|
||||
var request = new Request(options);
|
||||
const outgoing = {};
|
||||
const OutgoingWebSocketRequest = function OutgoingWebSocketRequest(
|
||||
options,
|
||||
socket
|
||||
) {
|
||||
const request = new Request(options);
|
||||
outgoing[request.id] = request;
|
||||
socket.send(
|
||||
new textsecure.protobuf.WebSocketMessage({
|
||||
|
@ -81,22 +85,18 @@
|
|||
);
|
||||
};
|
||||
|
||||
window.WebSocketResource = function(socket, opts) {
|
||||
opts = opts || {};
|
||||
var handleRequest = opts.handleRequest;
|
||||
window.WebSocketResource = function WebSocketResource(socket, opts = {}) {
|
||||
let { handleRequest } = opts;
|
||||
if (typeof handleRequest !== 'function') {
|
||||
handleRequest = function(request) {
|
||||
request.respond(404, 'Not found');
|
||||
};
|
||||
handleRequest = request => request.respond(404, 'Not found');
|
||||
}
|
||||
this.sendRequest = function(options) {
|
||||
return new OutgoingWebSocketRequest(options, socket);
|
||||
};
|
||||
this.sendRequest = options => new OutgoingWebSocketRequest(options, socket);
|
||||
|
||||
socket.onmessage = function(socketMessage) {
|
||||
var blob = socketMessage.data;
|
||||
var handleArrayBuffer = function(buffer) {
|
||||
var message = textsecure.protobuf.WebSocketMessage.decode(buffer);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
socket.onmessage = socketMessage => {
|
||||
const blob = socketMessage.data;
|
||||
const handleArrayBuffer = buffer => {
|
||||
const message = textsecure.protobuf.WebSocketMessage.decode(buffer);
|
||||
if (
|
||||
message.type === textsecure.protobuf.WebSocketMessage.Type.REQUEST
|
||||
) {
|
||||
|
@ -106,17 +106,17 @@
|
|||
path: message.request.path,
|
||||
body: message.request.body,
|
||||
id: message.request.id,
|
||||
socket: socket,
|
||||
socket,
|
||||
})
|
||||
);
|
||||
} else if (
|
||||
message.type === textsecure.protobuf.WebSocketMessage.Type.RESPONSE
|
||||
) {
|
||||
var response = message.response;
|
||||
var request = outgoing[response.id];
|
||||
const { response } = message;
|
||||
const request = outgoing[response.id];
|
||||
if (request) {
|
||||
request.response = response;
|
||||
var callback = request.error;
|
||||
let callback = request.error;
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
callback = request.success;
|
||||
}
|
||||
|
@ -125,8 +125,9 @@
|
|||
callback(response.message, response.status, request);
|
||||
}
|
||||
} else {
|
||||
throw 'Received response for unknown request ' +
|
||||
message.response.id;
|
||||
throw new Error(
|
||||
`Received response for unknown request ${message.response.id}`
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -134,10 +135,8 @@
|
|||
if (blob instanceof ArrayBuffer) {
|
||||
handleArrayBuffer(blob);
|
||||
} else {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function() {
|
||||
handleArrayBuffer(reader.result);
|
||||
};
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => handleArrayBuffer(reader.result);
|
||||
reader.readAsArrayBuffer(blob);
|
||||
}
|
||||
};
|
||||
|
@ -147,7 +146,7 @@
|
|||
path: opts.keepalive.path,
|
||||
disconnect: opts.keepalive.disconnect,
|
||||
});
|
||||
var resetKeepAliveTimer = this.keepalive.reset.bind(this.keepalive);
|
||||
const resetKeepAliveTimer = this.keepalive.reset.bind(this.keepalive);
|
||||
socket.addEventListener('open', resetKeepAliveTimer);
|
||||
socket.addEventListener('message', resetKeepAliveTimer);
|
||||
socket.addEventListener(
|
||||
|
@ -156,54 +155,45 @@
|
|||
);
|
||||
}
|
||||
|
||||
socket.addEventListener(
|
||||
'close',
|
||||
function() {
|
||||
this.closed = true;
|
||||
}.bind(this)
|
||||
);
|
||||
socket.addEventListener('close', () => {
|
||||
this.closed = true;
|
||||
});
|
||||
|
||||
this.close = function(code, reason) {
|
||||
this.close = (code = 3000, reason) => {
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.log.info('WebSocketResource.close()');
|
||||
if (!code) {
|
||||
code = 3000;
|
||||
}
|
||||
if (this.keepalive) {
|
||||
this.keepalive.stop();
|
||||
}
|
||||
|
||||
socket.close(code, reason);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
socket.onmessage = null;
|
||||
|
||||
// On linux the socket can wait a long time to emit its close event if we've
|
||||
// lost the internet connection. On the order of minutes. This speeds that
|
||||
// process up.
|
||||
setTimeout(
|
||||
function() {
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.closed = true;
|
||||
setTimeout(() => {
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.closed = true;
|
||||
|
||||
window.log.warn('Dispatching our own socket close event');
|
||||
var ev = new Event('close');
|
||||
ev.code = code;
|
||||
ev.reason = reason;
|
||||
this.dispatchEvent(ev);
|
||||
}.bind(this),
|
||||
1000
|
||||
);
|
||||
window.log.warn('Dispatching our own socket close event');
|
||||
const ev = new Event('close');
|
||||
ev.code = code;
|
||||
ev.reason = reason;
|
||||
this.dispatchEvent(ev);
|
||||
}, 1000);
|
||||
};
|
||||
};
|
||||
window.WebSocketResource.prototype = new textsecure.EventTarget();
|
||||
|
||||
function KeepAlive(websocketResource, opts) {
|
||||
function KeepAlive(websocketResource, opts = {}) {
|
||||
if (websocketResource instanceof WebSocketResource) {
|
||||
opts = opts || {};
|
||||
this.path = opts.path;
|
||||
if (this.path === undefined) {
|
||||
this.path = '/';
|
||||
|
@ -220,36 +210,30 @@
|
|||
|
||||
KeepAlive.prototype = {
|
||||
constructor: KeepAlive,
|
||||
stop: function() {
|
||||
stop() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
clearTimeout(this.disconnectTimer);
|
||||
},
|
||||
reset: function() {
|
||||
reset() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
clearTimeout(this.disconnectTimer);
|
||||
this.keepAliveTimer = setTimeout(
|
||||
function() {
|
||||
if (this.disconnect) {
|
||||
// automatically disconnect if server doesn't ack
|
||||
this.disconnectTimer = setTimeout(
|
||||
function() {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
this.wsr.close(3001, 'No response to keepalive request');
|
||||
}.bind(this),
|
||||
1000
|
||||
);
|
||||
} else {
|
||||
this.reset();
|
||||
}
|
||||
window.log.info('Sending a keepalive message');
|
||||
this.wsr.sendRequest({
|
||||
verb: 'GET',
|
||||
path: this.path,
|
||||
success: this.reset.bind(this),
|
||||
});
|
||||
}.bind(this),
|
||||
55000
|
||||
);
|
||||
this.keepAliveTimer = setTimeout(() => {
|
||||
if (this.disconnect) {
|
||||
// automatically disconnect if server doesn't ack
|
||||
this.disconnectTimer = setTimeout(() => {
|
||||
clearTimeout(this.keepAliveTimer);
|
||||
this.wsr.close(3001, 'No response to keepalive request');
|
||||
}, 1000);
|
||||
} else {
|
||||
this.reset();
|
||||
}
|
||||
window.log.info('Sending a keepalive message');
|
||||
this.wsr.sendRequest({
|
||||
verb: 'GET',
|
||||
path: this.path,
|
||||
success: this.reset.bind(this),
|
||||
});
|
||||
}, 55000);
|
||||
},
|
||||
};
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue