From 631203168b729cf10b10d94691c6d422b5d7e16a Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:11:40 -0700 Subject: [PATCH] chore: cleanup console logging and eslint disabling comments --- src/content-type-parser.ts | 2 -- src/healthcheck.ts | 7 +++++-- src/heliaServer.ts | 12 +++++------- src/index.ts | 27 +++++++++++---------------- src/logger.ts | 3 +++ 5 files changed, 24 insertions(+), 27 deletions(-) create mode 100644 src/logger.ts diff --git a/src/content-type-parser.ts b/src/content-type-parser.ts index 2b1dcf5..76f2247 100644 --- a/src/content-type-parser.ts +++ b/src/content-type-parser.ts @@ -10,8 +10,6 @@ function checkForSvg (bytes: Uint8Array): string { } export async function contentTypeParser (bytes: Uint8Array, fileName?: string): Promise { - // eslint-disable-next-line no-console - const detectedType = (await fileTypeFromBuffer(bytes))?.mime if (detectedType != null) { return detectedType diff --git a/src/healthcheck.ts b/src/healthcheck.ts index c5804ae..2bc02a6 100644 --- a/src/healthcheck.ts +++ b/src/healthcheck.ts @@ -1,10 +1,13 @@ #!/usr/bin/env node import { HOST, PORT } from './constants.js' +import { logger } from './logger.js' + +const log = logger.forComponent('healthcheck') /** * This healthcheck script is used to check if the server is running and healthy. */ const rootReq = await fetch(`http://${HOST}:${PORT}`, { method: 'GET' }) const status = rootReq.status -// eslint-disable-next-line no-console -console.log(`Healthcheck status: ${status}`) + +log(`Healthcheck status: ${status}`) process.exit(status === 200 ? 0 : 1) diff --git a/src/heliaServer.ts b/src/heliaServer.ts index 349fda2..5391bf2 100644 --- a/src/heliaServer.ts +++ b/src/heliaServer.ts @@ -7,8 +7,7 @@ import { contentTypeParser } from './content-type-parser.js' import { dnsLinkLabelDecoder, isDnsLabel } from './dns-link-labels.js' import { getCustomHelia } from './getCustomHelia.js' import { getIpnsAddressDetails } from './ipns-address-utils.js' -import type { PeerId } from '@libp2p/interface' -import type debug from 'debug' +import type { ComponentLogger, Logger, PeerId } from '@libp2p/interface' import type { Helia } from 'helia' import type { CID } from 'multiformats/cid' @@ -47,13 +46,13 @@ export class HeliaServer { private heliaFetch!: Awaited> private heliaVersionInfo!: { Version: string, Commit: string } private readonly HOST_PART_REGEX = /^(?
.+)\.(?ip[fn]s)\..+$/ - private readonly log: debug.Debugger + private readonly log: Logger public isReady: Promise public routes: RouteEntry[] private heliaNode!: Helia - constructor (logger: debug.Debugger) { - this.log = logger.extend('server') + constructor (logger: ComponentLogger) { + this.log = logger.forComponent('server') this.isReady = this.init() .then(() => { this.log('Initialized') @@ -72,8 +71,7 @@ export class HeliaServer { this.heliaNode = await getCustomHelia() this.heliaFetch = await createVerifiedFetch(this.heliaNode, { contentTypeParser }) - // eslint-disable-next-line no-console - console.log('Helia Started!') + this.log('Helia Started!') this.routes = [ { // without this non-wildcard postfixed path, the '/*' route will match first. diff --git a/src/index.ts b/src/index.ts index de94d4a..524b26c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,12 @@ import compress from '@fastify/compress' import cors from '@fastify/cors' -import debug from 'debug' import Fastify from 'fastify' import metricsPlugin from 'fastify-metrics' import { HOST, PORT, METRICS, ECHO_HEADERS, FASTIFY_DEBUG } from './constants.js' import { HeliaServer, type RouteEntry } from './heliaServer.js' +import { logger } from './logger.js' -const logger = debug('helia-http-gateway') +const log = logger.forComponent('index') const heliaServer = new HeliaServer(logger) await heliaServer.isReady @@ -60,19 +60,19 @@ heliaServer.routes.forEach(({ path, type, handler }: RouteEntry) => { if ([ECHO_HEADERS].includes(true)) { app.addHook('onRequest', async (request, reply) => { if (ECHO_HEADERS) { - logger('fastify hook onRequest: echoing headers:') + log('fastify hook onRequest: echoing headers:') Object.keys(request.headers).forEach((headerName) => { - logger('\t %s: %s', headerName, request.headers[headerName]) + log('\t %s: %s', headerName, request.headers[headerName]) }) } }) app.addHook('onSend', async (request, reply, payload) => { if (ECHO_HEADERS) { - logger('fastify hook onSend: echoing headers:') + log('fastify hook onSend: echoing headers:') const responseHeaders = reply.getHeaders() Object.keys(responseHeaders).forEach((headerName) => { - logger('\t %s: %s', headerName, responseHeaders[headerName]) + log('\t %s: %s', headerName, responseHeaders[headerName]) }) } return payload @@ -85,28 +85,23 @@ const stopWebServer = async (): Promise => { try { await app.close() } catch (error) { - // eslint-disable-next-line no-console - console.error(error) + log.error(error) process.exit(1) } - // eslint-disable-next-line no-console - console.log('Closed out remaining webServer connections.') + log('Closed out remaining webServer connections.') } let shutdownRequested = false async function closeGracefully (signal: number): Promise { - // eslint-disable-next-line no-console - console.log(`Received signal to terminate: ${signal}`) + log(`Received signal to terminate: ${signal}`) if (shutdownRequested) { - // eslint-disable-next-line no-console - console.log('closeGracefully: shutdown already requested, exiting callback.') + log('closeGracefully: shutdown already requested, exiting callback.') return } shutdownRequested = true await Promise.all([heliaServer.stop().then(() => { - // eslint-disable-next-line no-console - console.log('Stopped Helia.') + log('Stopped Helia.') }), stopWebServer()]) process.kill(process.pid, signal) diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 0000000..42a6bb6 --- /dev/null +++ b/src/logger.ts @@ -0,0 +1,3 @@ +import { prefixLogger } from '@libp2p/logger' + +export const logger = prefixLogger('helia-http-gateway')