From 49fd2b69ee5ad0087ba1211dab5942cc8aac2222 Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 31 Dec 2021 15:16:18 +0000 Subject: [PATCH] client: make console connection logging optional --- docs/API.md | 3 ++- src/client.js | 11 ++++++++--- src/createClient.js | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/API.md b/docs/API.md index 677f12f..d6e23ed 100644 --- a/docs/API.md +++ b/docs/API.md @@ -13,12 +13,12 @@ Returns a `Client` instance and connects to the server. | version | *optional* | Version to connect as. If not specified, automatically match server version. | | offline | *optional* | default to **false**. Set this to true to disable Microsoft/Xbox auth. | | username | Conditional | Required if `offline` set to true : Username to connect to server as. | -| authTitle | *optional* | The title ID to connect as, see the README for usage. | | connectTimeout | *optional* | default to **9000ms**. How long to wait in milliseconds while trying to connect to server. | | onMsaCode | *optional* | Callback called when signing in with a microsoft account with device code auth, `data` is an object documented [here](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code#device-authorization-response) | | profilesFolder | *optional* | Where to store cached authentication tokens. Defaults to .minecraft, or the node_modules folder if not found. | | autoInitPlayer | *optional* | default to true, If we should send SetPlayerInitialized to the server after getting play_status spawn. | | skipPing | *optional* | Whether pinging the server to check its version should be skipped. | +| conLog | *optional* | Where to log connection information (server join, kick messages to). Defaults to console.log, set to `null` to not log anywhere. | | useNativeRaknet | *optional* | Whether to use the C++ version of RakNet. Set to false to use JS. | | authTitle | *optional* | The client ID to sign in as, defaults to Minecraft for Nintendo Switch. Set false to sign in through Azure. See prismarine-auth | | deviceType | *optional* | The device type to sign in as, defaults to "Nintendo". See prismarine-auth | @@ -48,6 +48,7 @@ authenticated unless offline is set to true. | kickTimeout | *[Future][1]* | How long to wait before kicking a unresponsive client. | | motd | *optional* | The "message of the day" for the server, the message shown to players in the server list. See usage below. | | advertisementFn | *optional* | optional. Custom function to call that should return a ServerAdvertisement, used for setting the RakNet server PONG data. Overrides `motd`. | +| conLog | *optional* | Where to log connection information (server join, kick messages to). Default to log only in DEBUG mode. | ## be.ping({ host, port }) : ServerAdvertisement diff --git a/src/client.js b/src/client.js index 5909667..76e4bcb 100644 --- a/src/client.js +++ b/src/client.js @@ -41,6 +41,7 @@ class Client extends Connection { this.inLog = (...args) => debug('C ->', ...args) this.outLog = (...args) => debug('C <-', ...args) } + this.conLog = this.options.conLog === undefined ? console.log : this.options.conLog } connect () { @@ -82,7 +83,9 @@ class Client extends Connection { try { return await this.connection.ping(this.options.connectTimeout) } catch (e) { - console.warn(`Unable to connect to [${this.options.host}]/${this.options.port}. Is the server running?`) + // TODO: workaround bug in standardjs, waiting for https://github.com/standard/eslint-config-standard/pull/193 + const t = `Unable to connect to [${this.options.host}]/${this.options.port}. Is the server running?` + this.conLog?.(t) throw e } } @@ -126,7 +129,9 @@ class Client extends Connection { } onDisconnectRequest (packet) { - console.warn(`Server requested ${packet.hide_disconnect_reason ? 'silent disconnect' : 'disconnect'}: ${packet.message}`) + // TODO: workaround bug in standardjs, waiting for https://github.com/standard/eslint-config-standard/pull/193 + const t = `Server requested ${packet.hide_disconnect_reason ? 'silent disconnect' : 'disconnect'}: ${packet.message}` + this.conLog?.(t) this.emit('kick', packet) this.close() } @@ -172,7 +177,7 @@ class Client extends Connection { return } const pakData = { name: des.data.name, params: des.data.params } - this.inLog?.('-> C', pakData.name, this.options.loggging ? serialize(pakData.params) : '') + this.inLog?.('-> C', pakData.name, this.options.logging ? serialize(pakData.params) : '') this.emit('packet', des) if (debugging) { diff --git a/src/createClient.js b/src/createClient.js index a7e9fd4..59b6ceb 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -16,7 +16,7 @@ function createClient (options) { client.ping().then(data => { const ad = advertisement.fromServerName(data) client.options.version = options.version ?? (Versions[ad.version] ? ad.version : CURRENT_VERSION) - console.log(`Connecting to server ${ad.motd} (${ad.name}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : undefined) + if (client.conLog) client.conLog(`Connecting to server ${ad.motd} (${ad.name}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : '') connect(client) }, client) }