Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make components and logger optional #70

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading