diff --git a/src/config.ts b/src/config.ts index 887e461..5c682d5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,16 +1,8 @@ import { CodeError } from '@libp2p/interface/errors' -import { logger, type Logger } from '@libp2p/logger' import { ERR_INVALID_CONFIG, INITIAL_STREAM_WINDOW, MAX_STREAM_WINDOW } from './constants.js' // TOOD use config items or delete them export interface Config { - /** - * Used to control the log destination - * - * It can be disabled by explicitly setting to `undefined` - */ - log?: Logger - /** * Used to do periodic keep alive messages using a ping. */ @@ -55,7 +47,6 @@ export interface Config { } export const defaultConfig: Config = { - log: logger('libp2p:yamux'), enableKeepAlive: true, keepAliveInterval: 30_000, maxInboundStreams: 1_000, diff --git a/src/index.ts b/src/index.ts index ade3ad8..3718926 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,13 @@ import { Yamux } from './muxer.js' import type { YamuxMuxerInit } from './muxer.js' import type { StreamMuxerFactory } from '@libp2p/interface/stream-muxer' +import type { ComponentLogger } from '@libp2p/logger' export { GoAwayCode } from './frame.js' -export function yamux (init: YamuxMuxerInit = {}): () => StreamMuxerFactory { - return () => new Yamux(init) +export interface YamuxComponents { + logger: ComponentLogger +} + +export function yamux (init: YamuxMuxerInit = {}): (components: YamuxComponents) => StreamMuxerFactory { + return (components) => new Yamux(components, init) } diff --git a/src/muxer.ts b/src/muxer.ts index 2fa8f0c..4031332 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -1,6 +1,5 @@ import { CodeError } from '@libp2p/interface/errors' import { setMaxListeners } from '@libp2p/interface/events' -import { logger, type Logger } from '@libp2p/logger' import { getIterator } from 'get-iterator' import { pushable, type Pushable } from 'it-pushable' import { Uint8ArrayList } from 'uint8arraylist' @@ -10,9 +9,11 @@ import { Decoder } from './decode.js' import { encodeHeader } from './encode.js' import { Flag, type FrameHeader, FrameType, GoAwayCode } from './frame.js' import { StreamState, YamuxStream } from './stream.js' +import type { YamuxComponents } from './index.js' import type { AbortOptions } from '@libp2p/interface' import type { Stream } from '@libp2p/interface/connection' import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface/stream-muxer' +import type { ComponentLogger, Logger } from '@libp2p/logger' import type { Sink, Source } from 'it-stream-types' const YAMUX_PROTOCOL_ID = '/yamux/1.0.0' @@ -24,13 +25,15 @@ export interface YamuxMuxerInit extends StreamMuxerInit, Partial { export class Yamux implements StreamMuxerFactory { protocol = YAMUX_PROTOCOL_ID private readonly _init: YamuxMuxerInit + private readonly components: YamuxComponents - constructor (init: YamuxMuxerInit = {}) { + constructor (components: YamuxComponents, init: YamuxMuxerInit = {}) { this._init = init + this.components = components } createStreamMuxer (init?: YamuxMuxerInit): YamuxMuxer { - return new YamuxMuxer({ + return new YamuxMuxer(this.components, { ...this._init, ...init }) @@ -47,7 +50,8 @@ export class YamuxMuxer implements StreamMuxer { sink: Sink, Promise> private readonly config: Config - private readonly log?: Logger + private readonly log: Logger + private readonly logger: ComponentLogger /** Used to close the muxer from either the sink or source */ private readonly closeController: AbortController @@ -78,10 +82,11 @@ export class YamuxMuxer implements StreamMuxer { private readonly onIncomingStream?: (stream: Stream) => void private readonly onStreamEnd?: (stream: Stream) => void - constructor (init: YamuxMuxerInit) { + constructor (components: YamuxComponents, init: YamuxMuxerInit) { this.client = init.direction === 'outbound' this.config = { ...defaultConfig, ...init } - this.log = this.config.log + this.log = components.logger.forComponent('libp2p:yamux') + this.logger = components.logger verifyConfig(this.config) this.closeController = new AbortController() @@ -164,14 +169,18 @@ export class YamuxMuxer implements StreamMuxer { this.nextPingID = 0 this.rtt = -1 - this.log?.trace('muxer created') + this.log.trace('muxer created') if (this.config.enableKeepAlive) { - this.keepAliveLoop().catch(e => this.log?.error('keepalive error: %s', e)) + this.keepAliveLoop().catch(e => { + this.log.error('keepalive error: %s', e) + }) } // send an initial ping to establish RTT - this.ping().catch(e => this.log?.error('ping error: %s', e)) + this.ping().catch(e => { + this.log.error('ping error: %s', e) + }) } get streams (): YamuxStream[] { @@ -363,7 +372,7 @@ export class YamuxMuxer implements StreamMuxer { this.closeStream(id) this.onStreamEnd?.(stream) }, - log: logger(`libp2p:yamux:${direction}:${id}`), + log: this.logger.forComponent(`libp2p:yamux:${direction}:${id}`), config: this.config, getRTT: this.getRTT.bind(this) }) @@ -396,7 +405,9 @@ export class YamuxMuxer implements StreamMuxer { timeoutId = setTimeout(resolve, this.config.keepAliveInterval) }) ]) - this.ping().catch(e => this.log?.error('ping error: %s', e)) + this.ping().catch(e => { + this.log.error('ping error: %s', e) + }) } catch (e) { // closed clearInterval(timeoutId) diff --git a/test/compliance.spec.ts b/test/compliance.spec.ts index 4ec47dc..d4db0b1 100644 --- a/test/compliance.spec.ts +++ b/test/compliance.spec.ts @@ -1,12 +1,13 @@ /* eslint-env mocha */ import tests from '@libp2p/interface-compliance-tests/stream-muxer' +import { defaultLogger } from '@libp2p/logger' import { TestYamux } from './util.js' describe('compliance', () => { tests({ async setup () { - return new TestYamux({}) + return new TestYamux({ logger: defaultLogger() }) }, async teardown () {} }) diff --git a/test/util.ts b/test/util.ts index 9259213..bb7d3f1 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,4 +1,4 @@ -import { logger } from '@libp2p/logger' +import { prefixLogger } from '@libp2p/logger' import { duplexPair } from 'it-pair/duplex' import { pipe } from 'it-pipe' import { type Uint8ArrayList } from 'uint8arraylist' @@ -29,16 +29,15 @@ export const testConf: Partial = { export class TestYamux extends Yamux { createStreamMuxer (init?: YamuxMuxerInit): YamuxMuxer { const client = isClient() - return super.createStreamMuxer({ ...testConf, ...init, direction: client ? 'outbound' : 'inbound', log: logger(`libp2p:yamux${client ? 1 : 2}`) }) + return super.createStreamMuxer({ ...testConf, ...init, direction: client ? 'outbound' : 'inbound' }) } } export function testYamuxMuxer (name: string, client: boolean, conf: YamuxMuxerInit = {}): YamuxMuxer { - return new YamuxMuxer({ + return new YamuxMuxer({ logger: prefixLogger(name) }, { ...testConf, ...conf, - direction: client ? 'outbound' : 'inbound', - log: logger(name) + direction: client ? 'outbound' : 'inbound' }) }