Skip to content

Commit

Permalink
chore: cleanup console logging and eslint disabling comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 11, 2024
1 parent 6564215 commit 6312031
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/content-type-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function checkForSvg (bytes: Uint8Array): string {
}

export async function contentTypeParser (bytes: Uint8Array, fileName?: string): Promise<string> {
// eslint-disable-next-line no-console

const detectedType = (await fileTypeFromBuffer(bytes))?.mime
if (detectedType != null) {
return detectedType
Expand Down
7 changes: 5 additions & 2 deletions src/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 5 additions & 7 deletions src/heliaServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -47,13 +46,13 @@ export class HeliaServer {
private heliaFetch!: Awaited<ReturnType<typeof createVerifiedFetch>>
private heliaVersionInfo!: { Version: string, Commit: string }
private readonly HOST_PART_REGEX = /^(?<address>.+)\.(?<namespace>ip[fn]s)\..+$/
private readonly log: debug.Debugger
private readonly log: Logger
public isReady: Promise<void>
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')
Expand All @@ -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.
Expand Down
27 changes: 11 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -85,28 +85,23 @@ const stopWebServer = async (): Promise<void> => {
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<void> {
// 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)
Expand Down
3 changes: 3 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { prefixLogger } from '@libp2p/logger'

export const logger = prefixLogger('helia-http-gateway')

0 comments on commit 6312031

Please sign in to comment.