From 6e3e4ddea3ef512f6ae0d8e330ad90458c437e4c Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Thu, 13 Jul 2017 10:41:48 -0300 Subject: [PATCH] Typescript: remove @class/@extends/@property declarations --- sockets-workers.js | 25 ++++++++++++----- sockets.js | 67 +++++++++++++++++++++++++++++++++------------- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/sockets-workers.js b/sockets-workers.js index fcfc7af029577..b3c77b217ddd2 100644 --- a/sockets-workers.js +++ b/sockets-workers.js @@ -39,18 +39,16 @@ const SUBCHANNEL_MESSAGE_REGEX = /\|split\n([^\n]*)\n([^\n]*)\n([^\n]*)\n[^\n]*/ * Manages the worker's state for sockets, channels, and * subchannels. This is responsible for parsing all outgoing and incoming * messages. - * - * @class Multiplexer - * @property {number} socketCounter - * @property {Map} sockets - * @property {Map>} channels - * @property {?NodeJS.Timer} cleanupInterval */ class Multiplexer { constructor() { + /** @type {number} */ this.socketCounter = 0; + /** @type {Map} */ this.sockets = new Map(); + /** @type {Map>} */ this.channels = new Map(); + /** @type {?NodeJS.Timer} */ this.cleanupInterval = setInterval(() => this.sweepClosedSockets(), 10 * 60 * 1000); } @@ -85,6 +83,7 @@ class Multiplexer { /** * Sends an IPC message to the parent process. + * * @param {string} token * @param {string[]} params */ @@ -96,6 +95,7 @@ class Multiplexer { /** * Parses the params in a downstream message sent as a * command. + * * @param {string} params * @param {number} count * @return {string[]} @@ -130,6 +130,7 @@ class Multiplexer { /** * Parses downstream messages. + * * @param {string} data * @return {boolean} */ @@ -168,6 +169,7 @@ class Multiplexer { /** * Safely tries to destroy a socket's connection. + * * @param {any} socket */ tryDestroySocket(socket) { @@ -179,6 +181,7 @@ class Multiplexer { /** * Eval handler for downstream messages. + * * @param {string} expr * @return {boolean} */ @@ -192,6 +195,7 @@ class Multiplexer { /** * Sockets.socketConnect message handler. + * * @param {any} socket * @return {boolean} */ @@ -240,6 +244,7 @@ class Multiplexer { /** * Sockets.socketSend message handler. + * * @param {string} socketid * @param {string} message * @return {boolean} @@ -255,6 +260,7 @@ class Multiplexer { /** * onmessage event handler for sockets. Passes the message * upstream. + * * @param {string} socketid * @param {string} message * @return {boolean} @@ -282,6 +288,7 @@ class Multiplexer { /** * Sockets.channelAdd message handler. + * * @param {string} channelid * @param {string} socketid * @return {boolean} @@ -303,6 +310,7 @@ class Multiplexer { /** * Sockets.channelRemove message handler. + * * @param {string} channelid * @param {string} socketid * @return {boolean} @@ -319,6 +327,7 @@ class Multiplexer { /** * Sockets.channelSend and Sockets.channelBroadcast message * handler. + * * @param {string} channelid * @param {string} message * @return {boolean} @@ -343,6 +352,7 @@ class Multiplexer { /** * Sockets.subchannelMove message handler. + * * @param {string} channelid * @param {string} subchannelid * @param {string} socketid @@ -364,6 +374,7 @@ class Multiplexer { /** * Sockets.subchannelBroadcast message handler. + * * @param {string} channelid * @param {string} message * @return {boolean} @@ -410,7 +421,9 @@ class Multiplexer { * is already disconnected. */ destroy() { + // @ts-ignore clearInterval(this.cleanupInterval); + this.cleanupInterval = null; this.sockets.forEach(socket => this.tryDestroySocket(socket)); this.sockets.clear(); this.channels.clear(); diff --git a/sockets.js b/sockets.js index 870d234338370..8139bd5943b3c 100644 --- a/sockets.js +++ b/sockets.js @@ -27,6 +27,7 @@ if (cluster.isMaster) { * IPC delimiter byte. This byte must stringify as a hexadeimal * escape code when stringified as JSON to prevent messages from being able to * contain the byte itself. + * * @type {string} */ const DELIM = '\x03'; @@ -37,12 +38,6 @@ const DELIM = '\x03'; * cleans up when workers crash or get killed before respawning them. In other * words this listens for events emitted from both types of workers and handles * them accordingly. - * - * @class WorkerWrapper - * @property {any} worker - * @property {number} id - * @property {(ip: string) => boolean} isTrustedProxyIp - * @property {?Error} error */ class WorkerWrapper { /** @@ -50,9 +45,13 @@ class WorkerWrapper { * @param {number} id */ constructor(worker, id) { + /** @type {any} */ this.worker = worker; + /** @type {number} */ this.id = id; + /** @type {function(string): boolean} */ this.isTrustedProxyIp = Dnsbl.checker(Config.proxyip); + /** @type {?Error} */ this.error = null; worker.once('listening', () => this.onListen()); @@ -70,6 +69,7 @@ class WorkerWrapper { /** * Worker process getter + * * @return {any} */ get process() { @@ -78,6 +78,7 @@ class WorkerWrapper { /** * Worker exitedAfterDisconnect getter + * * @return {boolean | void} */ get exitedAfterDisconnect() { @@ -86,6 +87,7 @@ class WorkerWrapper { /** * Worker suicide getter + * * @return {boolean | void} */ get suicide() { @@ -94,6 +96,7 @@ class WorkerWrapper { /** * Worker#disconnect wrapper + * */ disconnect() { return this.worker.disconnect(); @@ -101,6 +104,7 @@ class WorkerWrapper { /** * Worker#kill wrapper + * * @param {string=} signal */ kill(signal) { @@ -109,6 +113,7 @@ class WorkerWrapper { /** * Worker#destroy wrapper + * * @param {string=} signal */ destroy(signal) { @@ -117,6 +122,7 @@ class WorkerWrapper { /** * Worker#send wrapper + * * @param {string} message * @return {boolean} */ @@ -126,6 +132,7 @@ class WorkerWrapper { /** * Worker#isConnected wrapper + * * @return {boolean} */ isConnected() { @@ -134,6 +141,7 @@ class WorkerWrapper { /** * Worker#isDead wrapper + * * @return {boolean} */ isDead() { @@ -154,6 +162,7 @@ class WorkerWrapper { * 'message' event handler for the worker. Parses which type * of command the incoming IPC message is calling, then passes its * parametres to the appropriate method to handle. + * * @param {string} data */ onMessage(data) { @@ -178,6 +187,7 @@ class WorkerWrapper { /** * Socket connection message handler. + * * @param {string} params */ onSocketConnect(params) { @@ -199,6 +209,7 @@ class WorkerWrapper { /** * Socket disconnect handler. + * * @param {string} socketid */ onSocketDisconnect(socketid) { @@ -207,6 +218,7 @@ class WorkerWrapper { /** * Socket message receive handler. + * * @param {string} params */ onSocketReceive(params) { @@ -218,6 +230,7 @@ class WorkerWrapper { /** * Worker 'error' event handler. + * * @param {?Error} err */ onError(err) { @@ -226,6 +239,7 @@ class WorkerWrapper { /** * Worker 'exit' event handler. + * * @param {any} worker * @param {?number} code * @param {?string} signal @@ -256,16 +270,6 @@ class WorkerWrapper { * Node.js workers, it uses a TCP net server to perform IPC. After launching * the server, it will spawn the Go child process and wait for it to make a * connection to the worker's server before performing IPC with it. - * - * @class GoWorker - * @extends NodeJS.EventEmitter - * @property {number} id - * @property {boolean | void} exitedAfterDisconnect - * @property {string[]} buffer - * @property {?Error} error - * @property {any} process - * @property {any} connection - * @property {any} server */ class GoWorker extends EventEmitter { /** @@ -274,18 +278,25 @@ class GoWorker extends EventEmitter { constructor(id) { super(); + /** @type {number} */ this.id = id; + /** @type {boolean | void} */ this.exitedAfterDisconnect = undefined; /** @type {string[]} */ this.obuf = []; + /** @type {string} */ this.ibuf = ''; + /** @type {?Error} */ this.error = null; + /** @type {any} */ this.process = null; + /** @type {any} */ this.connection = null; + /** @type {any} */ this.server = require('net').createServer(); - this.server.once('connection', connection => this.onChildConnect(connection)); + this.server.once('connection', /** @param {any} connection */ connection => this.onChildConnect(connection)); this.server.on('error', () => {}); this.server.listen(() => process.nextTick(() => this.spawnChild())); } @@ -299,6 +310,7 @@ class GoWorker extends EventEmitter { /** * Worker#kill mock + * * @param {string} [signal = 'SIGTERM'] */ kill(signal = 'SIGTERM') { @@ -307,6 +319,7 @@ class GoWorker extends EventEmitter { /** * Worker#destroy mock + * * @param {string=} signal */ destroy(signal) { @@ -315,6 +328,7 @@ class GoWorker extends EventEmitter { /** * Worker#send mock + * * @param {string} message * @return {boolean} */ @@ -335,6 +349,7 @@ class GoWorker extends EventEmitter { /** * Worker#isConnected mock + * * @return {boolean} */ isConnected() { @@ -343,6 +358,7 @@ class GoWorker extends EventEmitter { /** * Worker#isDead mock + * * @return {boolean} */ isDead() { @@ -425,7 +441,8 @@ class GoWorker extends EventEmitter { } /** - * Map of worker IDs to workers. + * Map of worker IDs to worker wrappers. + * * @type {Map} */ const workers = new Map(); @@ -433,6 +450,7 @@ const workers = new Map(); /** * Worker ID counter. We don't use cluster's internal counter so * Config.golang can be freely changed while the server is still running. + * * @type {number} */ let nextWorkerid = 1; @@ -440,12 +458,14 @@ let nextWorkerid = 1; /** * Config.golang cache. Checked when spawning new workers to * ensure that Node and Go workers will not try to run at the same time. + * * @type {boolean} */ let golangCache = !!Config.golang; /** * Spawns a new worker. + * * @return {WorkerWrapper} */ function spawnWorker() { @@ -491,6 +511,7 @@ function spawnWorker() { /** * Initializes the configured number of worker processes. + * * @param {any} port * @param {any} bindAddress * @param {any} workerCount @@ -531,6 +552,7 @@ function listen(port, bindAddress, workerCount) { /** * Kills a worker process using the given worker object. + * * @param {WorkerWrapper} worker * @return {number} */ @@ -547,6 +569,7 @@ function killWorker(worker) { /** * Kills a worker process using the given worker PID. + * * @param {number} pid * @return {number | false} */ @@ -561,6 +584,7 @@ function killPid(pid) { /** * Sends a message to a socket in a given worker by ID. + * * @param {WorkerWrapper} worker * @param {string} socketid * @param {string} message @@ -571,6 +595,7 @@ function socketSend(worker, socketid, message) { /** * Forcefully disconnects a socket in a given worker by ID. + * * @param {WorkerWrapper} worker * @param {string} socketid */ @@ -581,6 +606,7 @@ function socketDisconnect(worker, socketid) { /** * Broadcasts a message to all sockets in a given channel across * all workers. + * * @param {string} channelid * @param {string} message */ @@ -593,6 +619,7 @@ function channelBroadcast(channelid, message) { /** * Broadcasts a message to all sockets in a given channel and a * given worker. + * * @param {WorkerWrapper} worker * @param {string} channelid * @param {string} message @@ -603,6 +630,7 @@ function channelSend(worker, channelid, message) { /** * Adds a socket to a given channel in a given worker by ID. + * * @param {WorkerWrapper} worker * @param {string} channelid * @param {string} socketid @@ -613,6 +641,7 @@ function channelAdd(worker, channelid, socketid) { /** * Removes a socket from a given channel in a given worker by ID. + * * @param {WorkerWrapper} worker * @param {string} channelid * @param {string} socketid @@ -624,6 +653,7 @@ function channelRemove(worker, channelid, socketid) { /** * Broadcasts a message to be demuxed into three separate messages * across three subchannels in a given channel across all workers. + * * @param {string} channelid * @param {string} message */ @@ -636,6 +666,7 @@ function subchannelBroadcast(channelid, message) { /** * Moves a given socket to a different subchannel in a channel by * ID in the given worker. + * * @param {WorkerWrapper} worker * @param {string} channelid * @param {string} subchannelid