From ad9adff06c7b1563ec22b001c136a4e7c61b8ad7 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 14 Sep 2023 23:28:31 +0300 Subject: [PATCH] Move to ES6 module (#357) * Redo imports and exports for lib * Redo imports and exports for bim * Redo imports and exports for games * Remove remaining module.exports * Use export default in lib * Use export default in protocols * Fix import in genreadme.js * Make package module and solve __dirname * Fix minecraft protocol imports * Fix imports on games and make binary runnable * Renamed protocol class exports to lowercase * Export promises class as default * Update README.md to use imports instead of require * Update CHANGELOG to mention the changes. * Remove Valve unused imports * Fix iconv import --- CHANGELOG.md | 2 ++ README.md | 7 +++-- bin/gamedig.js | 4 +-- bin/genreadme.js | 7 +++-- lib/DnsResolver.js | 16 +++++------ lib/GameResolver.js | 13 +++++---- lib/GlobalUdpSocket.js | 23 ++++++++-------- lib/HexUtil.js | 43 ++++++++++++++--------------- lib/Logger.js | 8 ++---- lib/Promises.js | 4 +-- lib/ProtocolResolver.js | 24 ++++------------ lib/QueryRunner.js | 13 ++++----- lib/Results.js | 9 +++--- lib/index.js | 6 ++-- lib/reader.js | 13 ++++----- package.json | 1 + protocols/armagetron.js | 6 ++-- protocols/ase.js | 6 ++-- protocols/assettocorsa.js | 8 ++---- protocols/battlefield.js | 6 ++-- protocols/buildandshoot.js | 8 ++---- protocols/core.js | 22 +++++++-------- protocols/cs2d.js | 6 ++-- protocols/discord.js | 6 ++-- protocols/doom3.js | 6 ++-- protocols/ffow.js | 6 ++-- protocols/fivem.js | 6 ++-- protocols/gamespy1.js | 6 ++-- protocols/gamespy2.js | 6 ++-- protocols/gamespy3.js | 6 ++-- protocols/geneshift.js | 6 ++-- protocols/goldsrc.js | 6 ++-- protocols/hexen2.js | 6 ++-- protocols/index.js | 52 +++++++++++++++++++++++++++++++++++ protocols/jc2mp.js | 6 ++-- protocols/kspdmp.js | 6 ++-- protocols/mafia2mp.js | 6 ++-- protocols/mafia2online.js | 6 ++-- protocols/minecraft.js | 18 ++++++------ protocols/minecraftbedrock.js | 8 ++---- protocols/minecraftvanilla.js | 8 ++---- protocols/mumble.js | 6 ++-- protocols/mumbleping.js | 6 ++-- protocols/nadeo.js | 11 +++----- protocols/openttd.js | 6 ++-- protocols/quake1.js | 6 ++-- protocols/quake2.js | 6 ++-- protocols/quake3.js | 6 ++-- protocols/rfactor.js | 6 ++-- protocols/samp.js | 6 ++-- protocols/savage2.js | 6 ++-- protocols/starmade.js | 6 ++-- protocols/starsiege.js | 6 ++-- protocols/teamspeak2.js | 6 ++-- protocols/teamspeak3.js | 6 ++-- protocols/terraria.js | 6 ++-- protocols/tribes1.js | 6 ++-- protocols/tribes1master.js | 6 ++-- protocols/unreal2.js | 6 ++-- protocols/ut3.js | 6 ++-- protocols/valve.js | 10 ++----- protocols/vcmp.js | 6 ++-- protocols/ventrilo.js | 6 ++-- protocols/warsow.js | 6 ++-- 64 files changed, 249 insertions(+), 323 deletions(-) create mode 100644 protocols/index.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 54df512..4f6302e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ### To Be Released... * Replaced usage of deprecated `substr` with `substring`. +* Moved the library a `module`. +* CLI: Resolved incorrect error message when querying with a non-existent protocol name. ### 4.1.0 * Replace `compressjs` dependency by `seek-bzip` to solve some possible import issues. diff --git a/README.md b/README.md index c79618e..d710085 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,14 @@ Support is available on the [GameDig Discord](https://discord.gg/NVCMn3tnxH) (fo See the [GAMES_LIST.md](GAMES_LIST.md) file for the currently supported titles, not yet supported ones and notes about some of them. ## Usage from Node.js - +Install... ```shell npm install gamedig ``` +... then use! +```js +import GameDig from 'gamedig'; -```javascript -const GameDig = require('gamedig'); GameDig.query({ type: 'minecraft', host: 'mc.example.com' diff --git a/bin/gamedig.js b/bin/gamedig.js index 0b0d0ee..c01d875 100755 --- a/bin/gamedig.js +++ b/bin/gamedig.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -const Minimist = require('minimist'); -const GameDig = require('..'); +import Minimist from 'minimist'; +import GameDig from './../lib/index.js'; const argv = Minimist(process.argv.slice(2), { boolean: ['pretty','debug','givenPortOnly','requestRules'], diff --git a/bin/genreadme.js b/bin/genreadme.js index 56d66bc..c44d51c 100644 --- a/bin/genreadme.js +++ b/bin/genreadme.js @@ -1,8 +1,9 @@ #!/usr/bin/env node -const fs = require('fs'), - GameResolver = require('../lib/GameResolver'), - gameResolver = new GameResolver(); +import * as fs from 'fs'; +import GameResolver from "../lib/GameResolver"; + +const gameResolver = new GameResolver(); const generated = gameResolver.printReadme(); diff --git a/lib/DnsResolver.js b/lib/DnsResolver.js index 5d9e0a6..6d94aa5 100644 --- a/lib/DnsResolver.js +++ b/lib/DnsResolver.js @@ -1,11 +1,11 @@ -const dns = require('dns'), - Logger = require('./Logger'), - util = require('util'), - dnsLookupAsync = util.promisify(dns.lookup), - dnsResolveAsync = util.promisify(dns.resolve), - punycode = require('punycode'); +import * as dns from 'dns'; +import * as punycode from "punycode"; +import { promisify } from "util"; -class DnsResolver { +const dnsLookupAsync = promisify(dns.lookup); +const dnsResolveAsync = promisify(dns.resolve); + +export default class DnsResolver { /** * @param {Logger} logger */ @@ -76,5 +76,3 @@ class DnsResolver { return {address: address}; } } - -module.exports = DnsResolver; diff --git a/lib/GameResolver.js b/lib/GameResolver.js index 2d19fbd..2c5ec42 100644 --- a/lib/GameResolver.js +++ b/lib/GameResolver.js @@ -1,7 +1,8 @@ -const Path = require('path'), - fs = require('fs'); +import * as path from 'path'; +import { fileURLToPath } from "url"; +import * as fs from 'fs'; -class GameResolver { +export default class GameResolver { constructor() { const loaded = this._readGames(); this.gamesByKey = loaded.gamesByKey; @@ -56,7 +57,9 @@ class GameResolver { } _readGames() { - const gamesFile = Path.normalize(__dirname+'/../games.txt'); + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const gamesFile = path.normalize(__dirname+'/../games.txt'); const lines = fs.readFileSync(gamesFile,'utf8').split('\n'); const gamesByKey = new Map(); @@ -117,5 +120,3 @@ class GameResolver { return out; } } - -module.exports = GameResolver; diff --git a/lib/GlobalUdpSocket.js b/lib/GlobalUdpSocket.js index 16d403d..c1938ac 100644 --- a/lib/GlobalUdpSocket.js +++ b/lib/GlobalUdpSocket.js @@ -1,9 +1,10 @@ -const dgram = require('dgram'); -const HexUtil = require('./HexUtil'); -const Logger = require('./Logger'); -const util = require('util'); -class GlobalUdpSocket { +import { createSocket } from "dgram"; +import { debugDump } from "./HexUtil.js"; +import { promisify } from "util"; +import Logger from "./Logger.js"; + +export default class GlobalUdpSocket { constructor({port}) { this.socket = null; this.callbacks = new Set(); @@ -14,7 +15,7 @@ class GlobalUdpSocket { async _getSocket() { if (!this.socket) { - const udpSocket = dgram.createSocket({ + const udpSocket = createSocket({ type: 'udp4', reuseAddr: true }); @@ -24,7 +25,7 @@ class GlobalUdpSocket { const fromPort = rinfo.port; this.logger.debug(log => { log(fromAddress + ':' + fromPort + " <--UDP(" + this.port + ")"); - log(HexUtil.debugDump(buffer)); + log(debugDump(buffer)); }); for (const callback of this.callbacks) { callback(fromAddress, fromPort, buffer); @@ -33,7 +34,7 @@ class GlobalUdpSocket { udpSocket.on('error', e => { this.logger.debug("UDP ERROR:", e); }); - await util.promisify(udpSocket.bind).bind(udpSocket)(this.port); + await promisify(udpSocket.bind).bind(udpSocket)(this.port); this.port = udpSocket.address().port; this.socket = udpSocket; } @@ -46,11 +47,11 @@ class GlobalUdpSocket { if (debug) { this.logger._print(log => { log(address + ':' + port + " UDP(" + this.port + ")-->"); - log(HexUtil.debugDump(buffer)); + log(debugDump(buffer)); }); } - await util.promisify(socket.send).bind(socket)(buffer,0,buffer.length,port,address); + await promisify(socket.send).bind(socket)(buffer,0,buffer.length,port,address); } addCallback(callback, debug) { @@ -66,5 +67,3 @@ class GlobalUdpSocket { this.logger.debugEnabled = this.debuggingCallbacks.size > 0; } } - -module.exports = GlobalUdpSocket; diff --git a/lib/HexUtil.js b/lib/HexUtil.js index eda0540..aa555f1 100644 --- a/lib/HexUtil.js +++ b/lib/HexUtil.js @@ -1,24 +1,21 @@ -class HexUtil { - /** @param {Buffer} buffer */ - static debugDump(buffer) { - let hexLine = ''; - let chrLine = ''; - let out = ''; - out += "Buffer length: " + buffer.length + " bytes\n"; - for(let i = 0; i < buffer.length; i++) { - const sliced = buffer.slice(i,i+1); - hexLine += sliced.toString('hex')+' '; - let chr = sliced.toString(); - if(chr < ' ' || chr > '~') chr = ' '; - chrLine += chr+' '; - if(hexLine.length > 60 || i === buffer.length - 1) { - out += hexLine + '\n'; - out += chrLine + '\n'; - hexLine = chrLine = ''; - } - } - return out; - } -} -module.exports = HexUtil; +/** @param {Buffer} buffer */ +export const debugDump = (buffer) => { + let hexLine = ''; + let chrLine = ''; + let out = ''; + out += "Buffer length: " + buffer.length + " bytes\n"; + for(let i = 0; i < buffer.length; i++) { + const sliced = buffer.slice(i,i+1); + hexLine += sliced.toString('hex')+' '; + let chr = sliced.toString(); + if(chr < ' ' || chr > '~') chr = ' '; + chrLine += chr+' '; + if(hexLine.length > 60 || i === buffer.length - 1) { + out += hexLine + '\n'; + out += chrLine + '\n'; + hexLine = chrLine = ''; + } + } + return out; +} diff --git a/lib/Logger.js b/lib/Logger.js index 2627e46..f9a584d 100644 --- a/lib/Logger.js +++ b/lib/Logger.js @@ -1,6 +1,6 @@ -const HexUtil = require('./HexUtil'); +import {debugDump} from './HexUtil.js'; -class Logger { +export default class Logger { constructor() { this.debugEnabled = false; this.prefix = ''; @@ -31,7 +31,7 @@ class Logger { if (arg instanceof Error) { out.push(arg.stack); } else if (arg instanceof Buffer) { - out.push(HexUtil.debugDump(arg)); + out.push(debugDump(arg)); } else if (typeof arg == 'function') { const result = arg.call(undefined, (...args) => this._print(...args)); if (result !== undefined) out.push(...this._convertArgsToStrings(result)); @@ -42,5 +42,3 @@ class Logger { return out; } } - -module.exports = Logger; diff --git a/lib/Promises.js b/lib/Promises.js index b807672..7d4f0d6 100644 --- a/lib/Promises.js +++ b/lib/Promises.js @@ -1,4 +1,4 @@ -class Promises { +export default class Promises { static createTimeout(timeoutMs, timeoutMsg) { let cancel = null; const wrapped = new Promise((res, rej) => { @@ -16,5 +16,3 @@ class Promises { return wrapped; } } - -module.exports = Promises; diff --git a/lib/ProtocolResolver.js b/lib/ProtocolResolver.js index ef99e9e..8e796c9 100644 --- a/lib/ProtocolResolver.js +++ b/lib/ProtocolResolver.js @@ -1,22 +1,8 @@ -const Path = require('path'), - fs = require('fs'), - Core = require('../protocols/core'); +import * as Protocols from '../protocols/index.js' -class ProtocolResolver { - constructor() { - this.protocolDir = Path.normalize(__dirname+'/../protocols'); - } +export const getProtocol = (protocolId) => { + if(!(protocolId in Protocols)) + throw Error('Protocol definition file missing: ' + protocolId); - /** - * @returns Core - */ - create(protocolId) { - protocolId = Path.basename(protocolId); - const path = this.protocolDir+'/'+protocolId; - if(!fs.existsSync(path+'.js')) throw Error('Protocol definition file missing: '+type); - const protocol = require(path); - return new protocol(); - } + return new Protocols[protocolId]; } - -module.exports = ProtocolResolver; diff --git a/lib/QueryRunner.js b/lib/QueryRunner.js index 8f64d59..6e4187b 100644 --- a/lib/QueryRunner.js +++ b/lib/QueryRunner.js @@ -1,6 +1,6 @@ -const GameResolver = require('./GameResolver'), - ProtocolResolver = require('./ProtocolResolver'), - GlobalUdpSocket = require('./GlobalUdpSocket'); +import GameResolver from "./GameResolver.js"; +import {getProtocol} from './ProtocolResolver.js'; +import GlobalUdpSocket from "./GlobalUdpSocket.js"; const defaultOptions = { socketTimeout: 2000, @@ -9,13 +9,12 @@ const defaultOptions = { ipFamily: 0 }; -class QueryRunner { +export default class QueryRunner { constructor(runnerOpts = {}) { this.udpSocket = new GlobalUdpSocket({ port: runnerOpts.listenUdpPort }); this.gameResolver = new GameResolver(); - this.protocolResolver = new ProtocolResolver(); } async run(userOptions) { @@ -90,11 +89,9 @@ class QueryRunner { } async _attempt(options) { - const core = this.protocolResolver.create(options.protocol); + const core = getProtocol(options.protocol); core.options = options; core.udpSocket = this.udpSocket; return await core.runOnceSafe(); } } - -module.exports = QueryRunner; diff --git a/lib/Results.js b/lib/Results.js index b2a9ae3..71908d0 100644 --- a/lib/Results.js +++ b/lib/Results.js @@ -1,4 +1,5 @@ -class Player { + +export class Player { name = ''; raw = {}; @@ -13,7 +14,7 @@ class Player { } } -class Players extends Array { +export class Players extends Array { setNum(num) { // If the server specified some ridiculous number of players (billions), we don't want to // run out of ram allocating these objects. @@ -29,7 +30,7 @@ class Players extends Array { } } -class Results { +export class Results { name = ''; map = ''; password = false; @@ -40,5 +41,3 @@ class Results { players = new Players(); bots = new Players(); } - -module.exports = Results; diff --git a/lib/index.js b/lib/index.js index 0bab991..71da89d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,8 +1,8 @@ -const QueryRunner = require('./QueryRunner'); +import QueryRunner from './QueryRunner.js'; let singleton = null; -class Gamedig { +export default class Gamedig { constructor(runnerOpts) { this.queryRunner = new QueryRunner(runnerOpts); } @@ -22,5 +22,3 @@ class Gamedig { return await Gamedig.getInstance().query(...args); } } - -module.exports = Gamedig; diff --git a/lib/reader.js b/lib/reader.js index 309ea6b..17801b9 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -1,8 +1,7 @@ -const Iconv = require('iconv-lite'), - Long = require('long'), - Core = require('../protocols/core'), - Buffer = require('buffer'), - Varint = require('varint'); +import Iconv from "iconv-lite"; +import Long from 'long'; +import {Buffer} from "buffer"; +import Varint from 'varint'; function readUInt64BE(buffer,offset) { const high = buffer.readUInt32BE(offset); @@ -15,7 +14,7 @@ function readUInt64LE(buffer,offset) { return new Long(low,high,true); } -class Reader { +export default class Reader { /** * @param {Core} query * @param {Buffer} buffer @@ -171,5 +170,3 @@ class Reader { return this.i >= this.buffer.length; } } - -module.exports = Reader; diff --git a/package.json b/package.json index 7ff19ac..97d084a 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "csgo", "minecraft" ], + "type": "module", "main": "lib/index.js", "author": "GameDig Contributors", "version": "4.1.0", diff --git a/protocols/armagetron.js b/protocols/armagetron.js index 1e52e8e..626c42e 100644 --- a/protocols/armagetron.js +++ b/protocols/armagetron.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Armagetron extends Core { +export default class armagetron extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -61,5 +61,3 @@ class Armagetron extends Core { return str.replace(/0x[0-9a-f]{6}/g,''); } } - -module.exports = Armagetron; diff --git a/protocols/ase.js b/protocols/ase.js index beb8eb4..81f925e 100644 --- a/protocols/ase.js +++ b/protocols/ase.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Ase extends Core { +export default class ase extends Core { async run(state) { const buffer = await this.udpSend('s',(buffer) => { const reader = this.reader(buffer); @@ -43,5 +43,3 @@ class Ase extends Core { return reader.pascalString(1, -1); } } - -module.exports = Ase; diff --git a/protocols/assettocorsa.js b/protocols/assettocorsa.js index 98d9fac..480a5fa 100644 --- a/protocols/assettocorsa.js +++ b/protocols/assettocorsa.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class AssettoCorsa extends Core { +export default class assettocorsa extends Core { async run(state) { const serverInfo = await this.request({ url: `http://${this.options.address}:${this.options.port}/INFO`, @@ -14,7 +14,7 @@ class AssettoCorsa extends Core { if (!serverInfo || !carInfo || !carInfo.Cars) { throw new Error('Query not successful'); } - + state.maxplayers = serverInfo.maxclients; state.name = serverInfo.name; state.map = serverInfo.track; @@ -36,5 +36,3 @@ class AssettoCorsa extends Core { } } } - -module.exports = AssettoCorsa; diff --git a/protocols/battlefield.js b/protocols/battlefield.js index 898e6ea..08118bd 100644 --- a/protocols/battlefield.js +++ b/protocols/battlefield.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Battlefield extends Core { +export default class battlefield extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -159,5 +159,3 @@ class Battlefield extends Core { return params; } } - -module.exports = Battlefield; \ No newline at end of file diff --git a/protocols/buildandshoot.js b/protocols/buildandshoot.js index 824eedc..ba94f67 100644 --- a/protocols/buildandshoot.js +++ b/protocols/buildandshoot.js @@ -1,7 +1,7 @@ -const Core = require('./core'), - cheerio = require('cheerio'); +import Core from './core.js'; +import * as cheerio from "cheerio"; -class BuildAndShoot extends Core { +export default class buildandshoot extends Core { async run(state) { const body = await this.request({ url: 'http://'+this.options.address+':'+this.options.port+'/', @@ -53,5 +53,3 @@ class BuildAndShoot extends Core { */ } } - -module.exports = BuildAndShoot; diff --git a/protocols/core.js b/protocols/core.js index fe846d8..5a1ba96 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -1,15 +1,15 @@ -const EventEmitter = require('events').EventEmitter; -const net = require('net'); -const Reader = require('../lib/reader'); -const HexUtil = require('../lib/HexUtil'); -const Promises = require('../lib/Promises'); -const Logger = require('../lib/Logger'); -const DnsResolver = require('../lib/DnsResolver'); -const Results = require('../lib/Results'); +import {EventEmitter} from "events"; +import * as net from "net"; +import Reader from "../lib/reader.js"; +import {debugDump} from '../lib/HexUtil.js'; +import Logger from "../lib/Logger.js"; +import DnsResolver from "../lib/DnsResolver.js"; +import {Results} from "../lib/Results.js"; +import Promises from "../lib/Promises.js"; let uid = 0; -class Core extends EventEmitter { +export default class Core extends EventEmitter { constructor() { super(); this.encoding = 'utf8'; @@ -178,7 +178,7 @@ class Core extends EventEmitter { const writeHook = socket.write; socket.write = (...args) => { log(address+':'+port+" TCP-->"); - log(HexUtil.debugDump(args[0])); + log(debugDump(args[0])); writeHook.apply(socket,args); }; socket.on('error', e => log('TCP Error:', e)); @@ -352,5 +352,3 @@ class Core extends EventEmitter { this.logger.debug(...args); } } - -module.exports = Core; diff --git a/protocols/cs2d.js b/protocols/cs2d.js index 6e98836..22545ae 100644 --- a/protocols/cs2d.js +++ b/protocols/cs2d.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Cs2d extends Core { +export default class cs2d extends Core { async run(state) { { const reader = await this.sendQuery( @@ -69,5 +69,3 @@ class Cs2d extends Core { return reader.pascalString(1); } } - -module.exports = Cs2d; diff --git a/protocols/discord.js b/protocols/discord.js index 739086e..9d48d85 100644 --- a/protocols/discord.js +++ b/protocols/discord.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Discord extends Core { +export default class discord extends Core { async run(state) { const guildId = this.options.guildId; if (typeof guildId !== 'string') { @@ -27,5 +27,3 @@ class Discord extends Core { state.raw = json; } } - -module.exports = Discord; diff --git a/protocols/doom3.js b/protocols/doom3.js index ffb2678..a89e6b3 100644 --- a/protocols/doom3.js +++ b/protocols/doom3.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Doom3 extends Core { +export default class doom3 extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -147,5 +147,3 @@ class Doom3 extends Core { return str.replace(/\^(X.{6}|.)/g,''); } } - -module.exports = Doom3; diff --git a/protocols/ffow.js b/protocols/ffow.js index 5960beb..8c1f577 100644 --- a/protocols/ffow.js +++ b/protocols/ffow.js @@ -1,6 +1,6 @@ -const Valve = require('./valve'); +import valve from './valve.js'; -class Ffow extends Valve { +export default class ffow extends valve { constructor() { super(); this.byteorder = 'be'; @@ -35,5 +35,3 @@ class Ffow extends Valve { state.raw.timeleft = reader.uint(2); } } - -module.exports = Ffow; diff --git a/protocols/fivem.js b/protocols/fivem.js index 8fc0288..b86391a 100644 --- a/protocols/fivem.js +++ b/protocols/fivem.js @@ -1,6 +1,6 @@ -const Quake2 = require('./quake2'); +import quake2 from './quake2.js'; -class FiveM extends Quake2 { +export default class fivem extends quake2 { constructor() { super(); this.sendHeader = 'getinfo xxx'; @@ -31,5 +31,3 @@ class FiveM extends Quake2 { } } } - -module.exports = FiveM; diff --git a/protocols/gamespy1.js b/protocols/gamespy1.js index dbaacfd..546cc96 100644 --- a/protocols/gamespy1.js +++ b/protocols/gamespy1.js @@ -1,4 +1,4 @@ -const Core = require('./core'); +import Core from './core.js'; const stringKeys = new Set([ 'website', @@ -29,7 +29,7 @@ function normalizeEntry([key,value]) { return [key,value]; } -class Gamespy1 extends Core { +export default class gamespy1 extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -177,5 +177,3 @@ class Gamespy1 extends Core { }); } } - -module.exports = Gamespy1; diff --git a/protocols/gamespy2.js b/protocols/gamespy2.js index b913859..98b3b74 100644 --- a/protocols/gamespy2.js +++ b/protocols/gamespy2.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Gamespy2 extends Core { +export default class gamespy2 extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -138,5 +138,3 @@ class Gamespy2 extends Core { return units; } } - -module.exports = Gamespy2; diff --git a/protocols/gamespy3.js b/protocols/gamespy3.js index 8372265..49c6bfd 100644 --- a/protocols/gamespy3.js +++ b/protocols/gamespy3.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Gamespy3 extends Core { +export default class gamespy3 extends Core { constructor() { super(); this.sessionId = 1; @@ -194,5 +194,3 @@ class Gamespy3 extends Core { }); } } - -module.exports = Gamespy3; diff --git a/protocols/geneshift.js b/protocols/geneshift.js index 3c9c45c..92ef780 100644 --- a/protocols/geneshift.js +++ b/protocols/geneshift.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class GeneShift extends Core { +export default class geneshift extends Core { async run(state) { await this.tcpPing(); @@ -44,5 +44,3 @@ class GeneShift extends Core { state.raw.version = found[19]; } } - -module.exports = GeneShift; diff --git a/protocols/goldsrc.js b/protocols/goldsrc.js index bc9605f..ab4a526 100644 --- a/protocols/goldsrc.js +++ b/protocols/goldsrc.js @@ -1,10 +1,8 @@ -const Valve = require('./valve'); +import valve from './valve.js'; -class GoldSrc extends Valve { +export default class goldsrc extends valve { constructor() { super(); this.goldsrcInfo = true; } } - -module.exports = GoldSrc; diff --git a/protocols/hexen2.js b/protocols/hexen2.js index 4773b0a..45c8bf7 100644 --- a/protocols/hexen2.js +++ b/protocols/hexen2.js @@ -1,6 +1,6 @@ -const Quake1 = require('./quake1'); +import quake1 from './quake1.js'; -class Hexen2 extends Quake1 { +export default class hexen2 extends quake1 { constructor() { super(); this.sendHeader = '\xFFstatus\x0a'; @@ -11,5 +11,3 @@ class Hexen2 extends Quake1 { state.gamePort = this.options.port - 50; } } - -module.exports = Hexen2; diff --git a/protocols/index.js b/protocols/index.js new file mode 100644 index 0000000..134f677 --- /dev/null +++ b/protocols/index.js @@ -0,0 +1,52 @@ +import armagetron from "./armagetron.js"; +import ase from "./ase.js"; +import assettocorsa from "./assettocorsa.js"; +import battlefield from "./battlefield.js"; +import buildandshoot from "./buildandshoot.js"; +import cs2d from "./cs2d.js"; +import discord from "./discord.js"; +import doom3 from "./doom3.js"; +import ffow from "./ffow.js"; +import fivem from "./fivem.js"; +import gamespy1 from "./gamespy1.js"; +import gamespy2 from "./gamespy2.js"; +import gamespy3 from "./gamespy3.js"; +import geneshift from "./geneshift.js"; +import goldsrc from "./goldsrc.js"; +import hexen2 from "./hexen2.js"; +import jc2mp from "./jc2mp.js"; +import kspdmp from "./kspdmp.js"; +import mafia2mp from "./mafia2mp.js"; +import mafia2online from "./mafia2online.js"; +import minecraft from "./minecraft.js"; +import minecraftbedrock from "./minecraftbedrock.js"; +import minecraftvanilla from "./minecraftvanilla.js"; +import mumble from "./mumble.js"; +import mumbleping from "./mumbleping.js"; +import nadeo from "./nadeo.js"; +import openttd from "./openttd.js"; +import quake1 from "./quake1.js"; +import quake2 from "./quake2.js"; +import quake3 from "./quake3.js"; +import rfactor from "./rfactor.js"; +import samp from "./samp.js"; +import savage2 from "./savage2.js"; +import starmade from "./starmade.js"; +import starsiege from "./starsiege.js"; +import teamspeak2 from "./teamspeak2.js"; +import teamspeak3 from "./teamspeak3.js"; +import terraria from "./terraria.js"; +import tribes1 from "./tribes1.js"; +import tribes1master from "./tribes1master.js"; +import unreal2 from "./unreal2.js"; +import ut3 from "./ut3.js"; +import valve from "./valve.js"; +import vcmp from "./vcmp.js"; +import ventrilo from "./ventrilo.js"; +import warsow from "./warsow.js"; + +export { armagetron, ase, assettocorsa, battlefield, buildandshoot, cs2d, discord, doom3, ffow, fivem, gamespy1, + gamespy2, gamespy3, geneshift, goldsrc, hexen2, jc2mp, kspdmp, mafia2mp, mafia2online, minecraft, + minecraftbedrock, minecraftvanilla, mumble, mumbleping, nadeo, openttd, quake1, quake2, quake3, rfactor, samp, + savage2, starmade, starsiege, teamspeak2, teamspeak3, terraria, tribes1, tribes1master, unreal2, ut3, valve, + vcmp, ventrilo, warsow } diff --git a/protocols/jc2mp.js b/protocols/jc2mp.js index 05f060f..8e69f49 100644 --- a/protocols/jc2mp.js +++ b/protocols/jc2mp.js @@ -1,8 +1,8 @@ -const Gamespy3 = require('./gamespy3'); +import gamespy3 from './gamespy3.js'; // supposedly, gamespy3 is the "official" query protocol for jcmp, // but it's broken (requires useOnlySingleSplit), and may not include some player names -class Jc2mp extends Gamespy3 { +export default class jc2mp extends gamespy3 { constructor() { super(); this.useOnlySingleSplit = true; @@ -16,5 +16,3 @@ class Jc2mp extends Gamespy3 { } } } - -module.exports = Jc2mp; diff --git a/protocols/kspdmp.js b/protocols/kspdmp.js index 9d107f8..b5af402 100644 --- a/protocols/kspdmp.js +++ b/protocols/kspdmp.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Kspdmp extends Core { +export default class kspdmp extends Core { async run(state) { const json = await this.request({ url: 'http://'+this.options.address+':'+this.options.port, @@ -25,5 +25,3 @@ class Kspdmp extends Core { } } } - -module.exports = Kspdmp; diff --git a/protocols/mafia2mp.js b/protocols/mafia2mp.js index 21992d6..5c63cc7 100644 --- a/protocols/mafia2mp.js +++ b/protocols/mafia2mp.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Mafia2Multiplayer extends Core { +export default class mafia2mp extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -39,5 +39,3 @@ class Mafia2Multiplayer extends Core { return reader.pascalString(1,-1); } } - -module.exports = Mafia2Multiplayer; diff --git a/protocols/mafia2online.js b/protocols/mafia2online.js index 0523554..31c49f9 100644 --- a/protocols/mafia2online.js +++ b/protocols/mafia2online.js @@ -1,11 +1,9 @@ -const Mafia2Multiplayer = require('./mafia2mp'); +import mafia2mp from './mafia2mp.js'; -class Mafia2Online extends Mafia2Multiplayer { +export default class mafia2online extends mafia2mp { constructor() { super(); this.header = 'M2Online'; this.isMafia2Online = true; } } - -module.exports = Mafia2Online; diff --git a/protocols/minecraft.js b/protocols/minecraft.js index 96b0783..5e1b3e8 100644 --- a/protocols/minecraft.js +++ b/protocols/minecraft.js @@ -1,8 +1,8 @@ -const Core = require('./core'); -const MinecraftVanilla = require('./minecraftvanilla'); -const MinecraftBedrock = require('./minecraftbedrock'); -const Gamespy3 = require('./gamespy3'); -const Results = require('../lib/Results'); +import Core from './core.js'; +import minecraftbedrock from "./minecraftbedrock.js"; +import minecraftvanilla from "./minecraftvanilla.js"; +import Gamespy3 from "./gamespy3.js"; +import {Results} from "../lib/Results.js"; /* Vanilla servers respond to minecraftvanilla only @@ -12,7 +12,7 @@ Some bedrock servers respond to minecraftbedrock only Unsure if any bedrock servers respond to gamespy3 and minecraftbedrock */ -class Minecraft extends Core { +export default class minecraft extends Core { constructor() { super(); this.srvRecord = "_minecraft._tcp"; @@ -21,7 +21,7 @@ class Minecraft extends Core { /** @type {Promise[]} */ const promises = []; - const vanillaResolver = new MinecraftVanilla(); + const vanillaResolver = new minecraftvanilla(); vanillaResolver.options = this.options; vanillaResolver.udpSocket = this.udpSocket; promises.push((async () => { @@ -38,7 +38,7 @@ class Minecraft extends Core { try { return await gamespyResolver.runOnceSafe(); } catch(e) {} })()); - const bedrockResolver = new MinecraftBedrock(); + const bedrockResolver = new minecraftbedrock(); bedrockResolver.options = this.options; bedrockResolver.udpSocket = this.udpSocket; promises.push((async () => { @@ -97,5 +97,3 @@ class Minecraft extends Core { state.name = state.name.replace(/\u00A7./g, ''); } } - -module.exports = Minecraft; diff --git a/protocols/minecraftbedrock.js b/protocols/minecraftbedrock.js index 3aa658e..f85ee57 100644 --- a/protocols/minecraftbedrock.js +++ b/protocols/minecraftbedrock.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class MinecraftBedrock extends Core { +export default class minecraftbedrock extends Core { constructor() { super(); this.byteorder = 'be'; @@ -68,9 +68,5 @@ class MinecraftBedrock extends Core { return true; }); - } - } - -module.exports = MinecraftBedrock; diff --git a/protocols/minecraftvanilla.js b/protocols/minecraftvanilla.js index 45dd6c5..5f09ebe 100644 --- a/protocols/minecraftvanilla.js +++ b/protocols/minecraftvanilla.js @@ -1,7 +1,7 @@ -const Core = require('./core'), - Varint = require('varint'); +import Core from './core.js'; +import Varint from "varint"; -class MinecraftVanilla extends Core { +export default class minecraftvanilla extends Core { async run(state) { const portBuf = Buffer.alloc(2); portBuf.writeUInt16BE(this.options.port,0); @@ -78,5 +78,3 @@ class MinecraftVanilla extends Core { ]); } } - -module.exports = MinecraftVanilla; diff --git a/protocols/mumble.js b/protocols/mumble.js index f60f3c4..312a615 100644 --- a/protocols/mumble.js +++ b/protocols/mumble.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Mumble extends Core { +export default class mumble extends Core { async run(state) { const json = await this.withTcp(async socket => { return await this.tcpSend(socket, 'json', (buffer) => { @@ -37,5 +37,3 @@ class Mumble extends Core { return str.replace(/<.*>/g,''); } } - -module.exports = Mumble; diff --git a/protocols/mumbleping.js b/protocols/mumbleping.js index b666178..ab1fccb 100644 --- a/protocols/mumbleping.js +++ b/protocols/mumbleping.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class MumblePing extends Core { +export default class mumbleping extends Core { constructor() { super(); this.byteorder = 'be'; @@ -22,5 +22,3 @@ class MumblePing extends Core { state.raw.allowedbandwidth = reader.uint(4); } } - -module.exports = MumblePing; diff --git a/protocols/nadeo.js b/protocols/nadeo.js index c02b08e..e9c9ca6 100644 --- a/protocols/nadeo.js +++ b/protocols/nadeo.js @@ -1,8 +1,8 @@ -const gbxremote = require('gbxremote'), - Core = require('./core'), - Promises = require('../lib/Promises'); +import Core from './core.js'; +import Promises from "../lib/Promises.js"; +import * as gbxremote from 'gbxremote'; -class Nadeo extends Core { +export default class nadeo extends Core { async run(state) { await this.withClient(async client => { const start = Date.now(); @@ -82,7 +82,4 @@ class Nadeo extends Core { stripColors(str) { return str.replace(/\$([0-9a-f]{3}|[a-z])/gi,''); } - } - -module.exports = Nadeo; diff --git a/protocols/openttd.js b/protocols/openttd.js index 5705022..718be92 100644 --- a/protocols/openttd.js +++ b/protocols/openttd.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class OpenTtd extends Core { +export default class openttd extends Core { async run(state) { { const [reader, version] = await this.query(0, 1, 1, 4); @@ -125,5 +125,3 @@ class OpenTtd extends Core { return arr[num]; } } - -module.exports = OpenTtd; diff --git a/protocols/quake1.js b/protocols/quake1.js index be1dca2..e1c5137 100644 --- a/protocols/quake1.js +++ b/protocols/quake1.js @@ -1,11 +1,9 @@ -const Quake2 = require('./quake2'); +import quake2 from './quake2.js'; -class Quake1 extends Quake2 { +export default class quake1 extends quake2 { constructor() { super(); this.responseHeader = 'n'; this.isQuake1 = true; } } - -module.exports = Quake1; diff --git a/protocols/quake2.js b/protocols/quake2.js index 3290ac2..8f01b98 100644 --- a/protocols/quake2.js +++ b/protocols/quake2.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Quake2 extends Core { +export default class quake2 extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -84,5 +84,3 @@ class Quake2 extends Core { if('hostname' in state.raw) state.name = state.raw.hostname; } } - -module.exports = Quake2; diff --git a/protocols/quake3.js b/protocols/quake3.js index 70a7b2c..866b5b0 100644 --- a/protocols/quake3.js +++ b/protocols/quake3.js @@ -1,6 +1,6 @@ -const Quake2 = require('./quake2'); +import quake2 from './quake2.js'; -class Quake3 extends Quake2 { +export default class quake3 extends quake2 { constructor() { super(); this.sendHeader = 'getstatus'; @@ -20,5 +20,3 @@ class Quake3 extends Quake2 { return str.replace(/\^(X.{6}|.)/g,''); } } - -module.exports = Quake3; diff --git a/protocols/rfactor.js b/protocols/rfactor.js index d48a559..80bbc3e 100644 --- a/protocols/rfactor.js +++ b/protocols/rfactor.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Rfactor extends Core { +export default class rfactor extends Core { constructor() { super(); //this.byteorder = 'be'; @@ -72,5 +72,3 @@ class Rfactor extends Core { return this.reader(consumed).string(); } } - -module.exports = Rfactor; diff --git a/protocols/samp.js b/protocols/samp.js index 28ad970..96f6937 100644 --- a/protocols/samp.js +++ b/protocols/samp.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Samp extends Core { +export default class samp extends Core { constructor() { super(); this.encoding = 'win1252'; @@ -105,5 +105,3 @@ class Samp extends Core { ); } } - -module.exports = Samp; diff --git a/protocols/savage2.js b/protocols/savage2.js index dfa7a5e..b53688c 100644 --- a/protocols/savage2.js +++ b/protocols/savage2.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Savage2 extends Core { +export default class savage2 extends Core { constructor() { super(); } @@ -27,5 +27,3 @@ class Savage2 extends Core { return str.replace(/\^./g,''); } } - -module.exports = Savage2; diff --git a/protocols/starmade.js b/protocols/starmade.js index 86567d3..b8c054b 100644 --- a/protocols/starmade.js +++ b/protocols/starmade.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Starmade extends Core { +export default class starmade extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -65,5 +65,3 @@ class Starmade extends Core { if(typeof data[6] === 'number') state.maxplayers = data[6]; } } - -module.exports = Starmade; diff --git a/protocols/starsiege.js b/protocols/starsiege.js index 680b83a..dcb0d61 100644 --- a/protocols/starsiege.js +++ b/protocols/starsiege.js @@ -1,6 +1,6 @@ -const Tribes1 = require('./tribes1'); +import tribes1 from "./tribes1.js"; -class Starsiege extends Tribes1 { +export default class starsiege extends tribes1 { constructor() { super(); this.encoding = 'latin1'; @@ -8,5 +8,3 @@ class Starsiege extends Tribes1 { this.responseByte = 0x73; } } - -module.exports = Starsiege; diff --git a/protocols/teamspeak2.js b/protocols/teamspeak2.js index e563b78..153d781 100644 --- a/protocols/teamspeak2.js +++ b/protocols/teamspeak2.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Teamspeak2 extends Core { +export default class teamspeak2 extends Core { async run(state) { const queryPort = this.options.teamspeakQueryPort || 51234; @@ -68,5 +68,3 @@ class Teamspeak2 extends Core { }); } } - -module.exports = Teamspeak2; diff --git a/protocols/teamspeak3.js b/protocols/teamspeak3.js index 0d91335..a132e26 100644 --- a/protocols/teamspeak3.js +++ b/protocols/teamspeak3.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Teamspeak3 extends Core { +export default class teamspeak3 extends Core { async run(state) { const queryPort = this.options.teamspeakQueryPort || 10011; @@ -64,5 +64,3 @@ class Teamspeak3 extends Core { } } } - -module.exports = Teamspeak3; diff --git a/protocols/terraria.js b/protocols/terraria.js index f9afaa0..5ea8486 100644 --- a/protocols/terraria.js +++ b/protocols/terraria.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Terraria extends Core { +export default class terraria extends Core { async run(state) { const json = await this.request({ url: 'http://'+this.options.address+':'+this.options.port+'/v2/server/status', @@ -22,5 +22,3 @@ class Terraria extends Core { state.raw.numplayers = json.playercount; } } - -module.exports = Terraria; diff --git a/protocols/tribes1.js b/protocols/tribes1.js index d50ceee..25bff2a 100644 --- a/protocols/tribes1.js +++ b/protocols/tribes1.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Tribes1 extends Core { +export default class tribes1 extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -148,5 +148,3 @@ class Tribes1 extends Core { return reader.pascalString(1); } } - -module.exports = Tribes1; diff --git a/protocols/tribes1master.js b/protocols/tribes1master.js index 9939242..bb60989 100644 --- a/protocols/tribes1master.js +++ b/protocols/tribes1master.js @@ -1,8 +1,8 @@ -const Core = require('./core'); +import Core from './core.js'; /** Unsupported -- use at your own risk!! */ -class Tribes1Master extends Core { +export default class tribes1master extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -77,5 +77,3 @@ class Tribes1Master extends Core { return reader.pascalString(1); } } - -module.exports = Tribes1Master; diff --git a/protocols/unreal2.js b/protocols/unreal2.js index b08c147..8ed3363 100644 --- a/protocols/unreal2.js +++ b/protocols/unreal2.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Unreal2 extends Core { +export default class unreal2 extends Core { constructor() { super(); this.encoding = 'latin1'; @@ -148,5 +148,3 @@ class Unreal2 extends Core { }); } } - -module.exports = Unreal2; diff --git a/protocols/ut3.js b/protocols/ut3.js index 9b96b41..aed697e 100644 --- a/protocols/ut3.js +++ b/protocols/ut3.js @@ -1,6 +1,6 @@ -const Gamespy3 = require('./gamespy3'); +import gamespy3 from './gamespy3.js'; -class Ut3 extends Gamespy3 { +export default class ut3 extends gamespy3 { async run(state) { await super.run(state); @@ -43,5 +43,3 @@ class Ut3 extends Gamespy3 { if('map' in state.raw) state.map = state.raw.map; } } - -module.exports = Ut3; diff --git a/protocols/valve.js b/protocols/valve.js index 3aa27d1..e12b0b7 100644 --- a/protocols/valve.js +++ b/protocols/valve.js @@ -1,7 +1,5 @@ -const Bzip2 = require('seek-bzip'); -const Core = require('./core'); -const Results = require('../lib/Results'); -const Reader = require('../lib/reader'); +import Bzip2 from 'seek-bzip'; +import Core from './core.js'; const AppId = { Squad: 393380, @@ -16,7 +14,7 @@ const AppId = { Source_SDK_Base_2006: 215 }; -class Valve extends Core { +export default class valve extends Core { constructor() { super(); @@ -606,5 +604,3 @@ class Valve extends Core { ); } } - -module.exports = Valve; diff --git a/protocols/vcmp.js b/protocols/vcmp.js index 8f64fa2..1ccc810 100644 --- a/protocols/vcmp.js +++ b/protocols/vcmp.js @@ -1,6 +1,6 @@ -const Samp = require('./samp'); +import samp from './samp.js'; -class Vcmp extends Samp { +export default class vcmp extends samp { constructor() { super(); this.magicHeader = 'VCMP'; @@ -8,5 +8,3 @@ class Vcmp extends Samp { this.isVcmp = true; } } - -module.exports = Vcmp; diff --git a/protocols/ventrilo.js b/protocols/ventrilo.js index 7ee875a..fc1f8b7 100644 --- a/protocols/ventrilo.js +++ b/protocols/ventrilo.js @@ -1,6 +1,6 @@ -const Core = require('./core'); +import Core from './core.js'; -class Ventrilo extends Core { +export default class ventrilo extends Core { constructor() { super(); this.byteorder = 'be'; @@ -233,5 +233,3 @@ const crc_table = [ 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 ]; - -module.exports = Ventrilo; diff --git a/protocols/warsow.js b/protocols/warsow.js index c970be5..eee0902 100644 --- a/protocols/warsow.js +++ b/protocols/warsow.js @@ -1,6 +1,6 @@ -const Quake3 = require('./quake3'); +import quake3 from './quake3.js'; -class Warsow extends Quake3 { +export default class warsow extends quake3 { async run(state) { await super.run(state); if(state.players) { @@ -11,5 +11,3 @@ class Warsow extends Quake3 { } } } - -module.exports = Warsow;