Skip to content

Commit

Permalink
refactor: make components and logger optional
Browse files Browse the repository at this point in the history
In previous releases of yamux the following was supported:

import { yamux } from '@chainsafe/libp2p-yamux'
const muxer = yamux()()

Now the following is required:

import { yamux } from '@chainsafe/libp2p-yamux'
const muxer = yamux()({logger: ...})

Adjust the code to support passing an empty logger and an empty Components.

Fixes #69

Signed-off-by: Christian Stewart <christian@aperture.us>
  • Loading branch information
paralin committed Dec 1, 2023
1 parent 1f9173b commit 039ff81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export { GoAwayCode, type FrameHeader, type FrameType } from './frame.js'
export type { YamuxMuxerInit }

export interface YamuxMuxerComponents {
logger: ComponentLogger
logger?: ComponentLogger
}

export function yamux (init: YamuxMuxerInit = {}): (components: YamuxMuxerComponents) => StreamMuxerFactory {
return (components) => new Yamux(components, init)
export function yamux (init: YamuxMuxerInit = {}): (components?: YamuxMuxerComponents) => StreamMuxerFactory {
return (components) => new Yamux(components ?? {}, init)
}
6 changes: 3 additions & 3 deletions src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class YamuxMuxer implements StreamMuxer {

private readonly config: Config
private readonly log?: Logger
private readonly logger: ComponentLogger
private readonly logger?: ComponentLogger

/** Used to close the muxer from either the sink or source */
private readonly closeController: AbortController
Expand Down Expand Up @@ -82,7 +82,7 @@ export class YamuxMuxer implements StreamMuxer {
this.client = init.direction === 'outbound'
this.config = { ...defaultConfig, ...init }
this.logger = components.logger
this.log = this.logger.forComponent('libp2p:yamux')
this.log = this.logger?.forComponent('libp2p:yamux')
verifyConfig(this.config)

this.closeController = new AbortController()
Expand Down Expand Up @@ -364,7 +364,7 @@ export class YamuxMuxer implements StreamMuxer {
this.closeStream(id)
this.onStreamEnd?.(stream)
},
log: this.logger.forComponent(`libp2p:yamux:${direction}:${id}`),
log: this.logger?.forComponent(`libp2p:yamux:${direction}:${id}`),
config: this.config,
getRTT: this.getRTT.bind(this)
})
Expand Down
24 changes: 22 additions & 2 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeError } from '@libp2p/interface'
import { CodeError, type Logger } from '@libp2p/interface'
import { AbstractStream, type AbstractStreamInit } from '@libp2p/utils/abstract-stream'
import each from 'it-foreach'
import { ERR_RECV_WINDOW_EXCEEDED, ERR_STREAM_ABORT, INITIAL_STREAM_WINDOW } from './constants.js'
Expand All @@ -15,12 +15,31 @@ export enum StreamState {
Finished,
}

export interface YamuxStreamInit extends AbstractStreamInit {
export interface YamuxStreamInit extends Omit<AbstractStreamInit, 'log'> {
name?: string
sendFrame(header: FrameHeader, body?: Uint8ArrayList): void
getRTT(): number
config: Config
state: StreamState
log?: Logger
}

// https://github.com/libp2p/js-libp2p/issues/2276
// https://github.com/libp2p/js-libp2p/blob/bca8d6e689b47d85dda74082ed72e671139391de/packages/logger/src/index.ts#L86
function createDisabledLogger(namespace: string): Logger {
const logger = (): void => {}
logger.enabled = false
logger.color = ''
logger.diff = 0
logger.log = (): void => {}
logger.namespace = namespace
logger.destroy = () => true
logger.extend = () => logger
logger.debug = logger
logger.error = logger
logger.trace = logger

return logger
}

/** YamuxStream is used to represent a logical stream within a session */
Expand Down Expand Up @@ -54,6 +73,7 @@ export class YamuxStream extends AbstractStream {
constructor (init: YamuxStreamInit) {
super({
...init,
log: init.log ?? createDisabledLogger('yamux'),
onEnd: (err?: Error) => {
this.state = StreamState.Finished
init.onEnd?.(err)
Expand Down

0 comments on commit 039ff81

Please sign in to comment.