From f0a24b8f6508015f1ad4acdcb52b25f8742977b9 Mon Sep 17 00:00:00 2001 From: mmorrison Date: Tue, 19 Feb 2019 21:15:34 -0600 Subject: [PATCH] Use @types/node@8.0.0 to validate (and fix) small node 8 api issues --- lib/GameResolver.js | 2 ++ lib/QueryRunner.js | 3 +-- package-lock.json | 23 +++++++++++++++------- package.json | 8 ++++++-- protocols/buildandshoot.js | 2 +- protocols/core.js | 7 ++++--- protocols/doom3.js | 4 ---- protocols/gamespy3.js | 3 +-- protocols/nadeo.js | 40 +++++++++++++++++--------------------- 9 files changed, 49 insertions(+), 43 deletions(-) diff --git a/lib/GameResolver.js b/lib/GameResolver.js index 219cb6e..b75ad33 100644 --- a/lib/GameResolver.js +++ b/lib/GameResolver.js @@ -85,6 +85,8 @@ class GameResolver { for (const one of str.split(',')) { const equals = one.indexOf('='); const key = equals === -1 ? one : one.substr(0,equals); + + /** @type {string|number|boolean} */ let value = equals === -1 ? '' : one.substr(equals+1); if(value === 'true' || value === '') value = true; diff --git a/lib/QueryRunner.js b/lib/QueryRunner.js index 54d9a4c..5919dee 100644 --- a/lib/QueryRunner.js +++ b/lib/QueryRunner.js @@ -1,7 +1,6 @@ const GameResolver = require('./GameResolver'), ProtocolResolver = require('./ProtocolResolver'), - GlobalUdpSocket = require('./GlobalUdpSocket'), - Logger = require('./Logger'); + GlobalUdpSocket = require('./GlobalUdpSocket'); const defaultOptions = { socketTimeout: 2000, diff --git a/package-lock.json b/package-lock.json index 9a53515..d352bb0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,19 @@ { "name": "gamedig", - "version": "2.0.5", + "version": "2.0.13", "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/cheerio": { + "version": "0.22.10", + "resolved": "https://registry.npmjs.org/@types/cheerio/-/cheerio-0.22.10.tgz", + "integrity": "sha512-fOM/Jhv51iyugY7KOBZz2ThfT1gwvsGCfWxpLpZDgkGjpEO4Le9cld07OdskikLjDUQJ43dzDaVRSFwQlpdqVg==", + "dev": true + }, "@types/node": { - "version": "10.12.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.18.tgz", - "integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==" + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-8.0.0.tgz", + "integrity": "sha512-j2tekvJCO7j22cs+LO6i0kRPhmQ9MXaPZ55TzOc1lzkN5b6BWqq4AFjl04s1oRRQ1v5rSe+KEvnLUSTonuls/A==" }, "ajv": { "version": "6.6.2", @@ -324,9 +330,12 @@ } }, "iconv-lite": { - "version": "0.4.18", - "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.18.tgz", - "integrity": "sha512-sr1ZQph3UwHTR0XftSbK85OvBbxe/abLGzEnPENCQwmHf7sck8Oyu4ob3LgBxWWxRoM+QszeUyl7jbqapu2TqA==" + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } }, "inherits": { "version": "2.0.3", diff --git a/package.json b/package.json index 727394c..5a547e6 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "cheerio": "^1.0.0-rc.2", "compressjs": "^1.0.2", "gbxremote": "^0.1.4", - "iconv-lite": "^0.4.18", + "iconv-lite": "^0.4.24", "long": "^2.4.0", "minimist": "^1.2.0", "moment": "^2.21.0", @@ -46,5 +46,9 @@ "games.txt", "LICENSE", "README.md" - ] + ], + "devDependencies": { + "@types/cheerio": "^0.22.10", + "@types/node": "^8.0.0" + } } diff --git a/protocols/buildandshoot.js b/protocols/buildandshoot.js index f52ed2e..7598ae5 100644 --- a/protocols/buildandshoot.js +++ b/protocols/buildandshoot.js @@ -9,7 +9,7 @@ class BuildAndShoot extends Core { let m; - m = body.match(/status server for (.*?)\.?(\r|\n)/); + m = body.match(/status server for (.*?)\.?[\r\n]/); if(m) state.name = m[1]; m = body.match(/Current uptime: (\d+)/); diff --git a/protocols/core.js b/protocols/core.js index 2dc536e..a21a331 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -182,7 +182,8 @@ class Core extends EventEmitter { /** * @template T - * @param {function(Socket):Promise} fn + * @param {function(NodeJS.Socket):Promise} fn + * @param {number=} port * @returns {Promise} */ async withTcp(fn, port) { @@ -236,7 +237,7 @@ class Core extends EventEmitter { /** * @template T - * @param {Socket} socket + * @param {NodeJS.Socket} socket * @param {Buffer|string} buffer * @param {function(Buffer):T} ondata * @returns Promise @@ -250,7 +251,7 @@ class Core extends EventEmitter { received = Buffer.concat([received, data]); const result = ondata(received); if (result !== undefined) { - socket.off('data', onData); + socket.removeListener('data', onData); resolve(result); } }; diff --git a/protocols/doom3.js b/protocols/doom3.js index 3e23e35..ffb2678 100644 --- a/protocols/doom3.js +++ b/protocols/doom3.js @@ -4,10 +4,6 @@ class Doom3 extends Core { constructor() { super(); this.encoding = 'latin1'; - this.isEtqw = false; - this.hasSpaceBeforeClanTag = false; - this.hasClanTag = false; - this.hasTypeFlag = false; } async run(state) { const body = await this.udpSend('\xff\xffgetInfo\x00PiNGPoNg\x00', packet => { diff --git a/protocols/gamespy3.js b/protocols/gamespy3.js index e006399..853c900 100644 --- a/protocols/gamespy3.js +++ b/protocols/gamespy3.js @@ -1,5 +1,4 @@ -const Core = require('./core'), - HexUtil = require('../lib/HexUtil'); +const Core = require('./core'); class Gamespy3 extends Core { constructor() { diff --git a/protocols/nadeo.js b/protocols/nadeo.js index 2280bd7..7b46788 100644 --- a/protocols/nadeo.js +++ b/protocols/nadeo.js @@ -1,5 +1,6 @@ const gbxremote = require('gbxremote'), - Core = require('./core'); + Core = require('./core'), + Promises = require('../lib/Promises'); class Nadeo extends Core { async run(state) { @@ -59,20 +60,16 @@ class Nadeo extends Core { async withClient(fn) { const socket = gbxremote.createClient(this.options.port, this.options.host); - const cancelAsyncLeak = this.addCleanup(() => socket.terminate()); try { - await this.timedPromise( - new Promise((resolve,reject) => { - socket.on('connect', resolve); - socket.on('error', e => reject(new Error('GBX Remote Connection Error: ' + e))); - socket.on('close', () => reject(new Error('GBX Remote Connection Refused'))); - }), - this.options.socketTimeout, - 'GBX Remote Opening' - ); + const connectPromise = new Promise((resolve,reject) => { + socket.on('connect', resolve); + socket.on('error', e => reject(new Error('GBX Remote Connection Error: ' + e))); + socket.on('close', () => reject(new Error('GBX Remote Connection Refused'))); + }); + const timeoutPromise = Promises.createTimeout(this.options.socketTimeout, 'GBX Remote Opening'); + const socket = await Promise.race([connectPromise, timeoutPromise, this.abortedPromise]); return await fn(socket); } finally { - cancelAsyncLeak(); socket.terminate(); } } @@ -80,16 +77,15 @@ class Nadeo extends Core { async methodCall(client, ...cmdset) { const cmd = cmdset[0]; const params = cmdset.slice(1); - return await this.timedPromise( - new Promise(async (resolve,reject) => { - client.methodCall(cmd, params, (err, value) => { - if (err) reject('XMLRPC error ' + JSON.stringify(err)); - resolve(value); - }); - }), - this.options.socketTimeout, - 'GBX Method Call' - ); + + const sentPromise = new Promise(async (resolve,reject) => { + client.methodCall(cmd, params, (err, value) => { + if (err) reject('XMLRPC error ' + JSON.stringify(err)); + resolve(value); + }); + }); + const timeoutPromise = Promises.createTimeout(this.options.socketTimeout, 'GBX Method Call'); + return await Promise.race([sentPromise, timeoutPromise, this.abortedPromise]); } stripColors(str) {