UNPKG

@chainsafe/libp2p-quic

Version:
57 lines 1.89 kB
import { TypedEventEmitter } from '@libp2p/interface'; import { multiaddr } from '@multiformats/multiaddr'; export class QuicConnection extends TypedEventEmitter { #connection; log; remoteAddr; metrics; metricsPrefix; remoteClosed; timeline = { open: Date.now() }; source = (async function* () { })(); sink = async function* () { }; constructor(init) { super(); this.#connection = init.connection; this.log = init.logger.forComponent(`libp2p:quic:connection:${this.#connection.id()}:${init.direction}`); this.remoteAddr = multiaddr(this.#connection.remoteMultiaddr()); this.metrics = init.metrics; this.metricsPrefix = init.metricsPrefix ?? ''; // close maconn when connection is closed by remote this.#connection.closed().then(() => { this.remoteClosed = true; this.metrics?.increment({ [`${this.metricsPrefix}end`]: true }); this.close() .catch(err => { this.abort(err); }); }, (err) => { this.abort(err); }); } async close(options) { if (this.timeline.close != null) { return; } this.#connection.abort(); this.timeline.close = Date.now(); this.log('closed'); if (this.remoteClosed !== true) { this.metrics?.increment({ [`${this.metricsPrefix}close`]: true }); } this.safeDispatchEvent('close'); } abort(err) { if (this.timeline.close != null) { return; } this.#connection.abort(); this.timeline.close = Date.now(); this.log('aborted - %e', err); this.metrics?.increment({ [`${this.metricsPrefix}abort`]: true }); this.safeDispatchEvent('close'); } } //# sourceMappingURL=connection.js.map