@libp2p/interfaces
Advanced tools
Comparing version 1.3.26 to 1.3.27
import type { ConnectionGater, ConnectionProtector } from './connection/index.js'; | ||
import type { ContentRouting } from './content-routing/index.js'; | ||
import type { Dialer } from './dialer/index.js'; | ||
import type { AddressManager } from './index.js'; | ||
import { AddressManager, Startable } from './index.js'; | ||
import type { Metrics } from './metrics/index.js'; | ||
@@ -9,3 +8,3 @@ import type { PeerId } from './peer-id/index.js'; | ||
import type { PeerStore } from './peer-store/index.js'; | ||
import type { ConnectionManager, Registrar } from './registrar/index.js'; | ||
import type { Registrar } from './registrar/index.js'; | ||
import type { TransportManager, Upgrader } from './transport/index.js'; | ||
@@ -15,2 +14,3 @@ import type { Datastore } from 'interface-datastore'; | ||
import type { DualDHT } from './dht/index.js'; | ||
import type { ConnectionManager } from './connection-manager/index.js'; | ||
export interface Initializable { | ||
@@ -26,3 +26,2 @@ init: (components: Components) => void; | ||
metrics?: Metrics; | ||
dialer?: Dialer; | ||
registrar?: Registrar; | ||
@@ -39,3 +38,3 @@ connectionManager?: ConnectionManager; | ||
} | ||
export declare class Components { | ||
export declare class Components implements Startable { | ||
private peerId?; | ||
@@ -46,3 +45,2 @@ private addressManager?; | ||
private metrics?; | ||
private dialer?; | ||
private registrar?; | ||
@@ -58,3 +56,11 @@ private connectionManager?; | ||
private pubsub?; | ||
private started; | ||
constructor(init?: ComponentsInit); | ||
isStarted(): boolean; | ||
beforeStart(): Promise<void>; | ||
start(): Promise<void>; | ||
afterStart(): Promise<void>; | ||
beforeStop(): Promise<void>; | ||
stop(): Promise<void>; | ||
afterStop(): Promise<void>; | ||
setPeerId(peerId: PeerId): PeerId; | ||
@@ -70,4 +76,2 @@ getPeerId(): PeerId; | ||
getUpgrader(): Upgrader; | ||
setDialer(dialer: Dialer): Dialer; | ||
getDialer(): Dialer; | ||
setRegistrar(registrar: Registrar): Registrar; | ||
@@ -74,0 +78,0 @@ getRegistrar(): Registrar; |
import errCode from 'err-code'; | ||
import { isStartable } from './index.js'; | ||
export function isInitializable(obj) { | ||
@@ -7,19 +8,98 @@ return obj != null && typeof obj.init === 'function'; | ||
constructor(init = {}) { | ||
this.peerId = init.peerId; | ||
this.addressManager = init.addressManager; | ||
this.peerStore = init.peerStore; | ||
this.upgrader = init.upgrader; | ||
this.metrics = init.metrics; | ||
this.dialer = init.dialer; | ||
this.registrar = init.registrar; | ||
this.connectionManager = init.connectionManager; | ||
this.transportManager = init.transportManager; | ||
this.connectionGater = init.connectionGater; | ||
this.contentRouting = init.contentRouting; | ||
this.peerRouting = init.peerRouting; | ||
this.datastore = init.datastore; | ||
this.connectionProtector = init.connectionProtector; | ||
this.dht = init.dht; | ||
this.pubsub = init.pubsub; | ||
this.started = false; | ||
if (init.peerId != null) { | ||
this.setPeerId(init.peerId); | ||
} | ||
if (init.addressManager != null) { | ||
this.setAddressManager(init.addressManager); | ||
} | ||
if (init.peerStore != null) { | ||
this.setPeerStore(init.peerStore); | ||
} | ||
if (init.upgrader != null) { | ||
this.setUpgrader(init.upgrader); | ||
} | ||
if (init.metrics != null) { | ||
this.setMetrics(init.metrics); | ||
} | ||
if (init.registrar != null) { | ||
this.setRegistrar(init.registrar); | ||
} | ||
if (init.connectionManager != null) { | ||
this.setConnectionManager(init.connectionManager); | ||
} | ||
if (init.transportManager != null) { | ||
this.setTransportManager(init.transportManager); | ||
} | ||
if (init.connectionGater != null) { | ||
this.setConnectionGater(init.connectionGater); | ||
} | ||
if (init.contentRouting != null) { | ||
this.setContentRouting(init.contentRouting); | ||
} | ||
if (init.peerRouting != null) { | ||
this.setPeerRouting(init.peerRouting); | ||
} | ||
if (init.datastore != null) { | ||
this.setDatastore(init.datastore); | ||
} | ||
if (init.connectionProtector != null) { | ||
this.setConnectionProtector(init.connectionProtector); | ||
} | ||
if (init.dht != null) { | ||
this.setDHT(init.dht); | ||
} | ||
if (init.pubsub != null) { | ||
this.setPubSub(init.pubsub); | ||
} | ||
} | ||
isStarted() { | ||
return this.started; | ||
} | ||
async beforeStart() { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj; | ||
if (startable.beforeStart != null) { | ||
await startable.beforeStart(); | ||
} | ||
} | ||
} | ||
async start() { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj; | ||
await startable.start(); | ||
} | ||
this.started = true; | ||
} | ||
async afterStart() { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj; | ||
if (startable.afterStart != null) { | ||
await startable.afterStart(); | ||
} | ||
} | ||
} | ||
async beforeStop() { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj; | ||
if (startable.beforeStop != null) { | ||
await startable.beforeStop(); | ||
} | ||
} | ||
} | ||
async stop() { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj; | ||
await startable.stop(); | ||
} | ||
this.started = false; | ||
} | ||
async afterStop() { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj; | ||
if (startable.afterStop != null) { | ||
await startable.afterStop(); | ||
} | ||
} | ||
} | ||
setPeerId(peerId) { | ||
@@ -37,2 +117,5 @@ this.peerId = peerId; | ||
this.metrics = metrics; | ||
if (isInitializable(metrics)) { | ||
metrics.init(this); | ||
} | ||
return metrics; | ||
@@ -45,2 +128,5 @@ } | ||
this.addressManager = addressManager; | ||
if (isInitializable(addressManager)) { | ||
addressManager.init(this); | ||
} | ||
return addressManager; | ||
@@ -56,2 +142,5 @@ } | ||
this.peerStore = peerStore; | ||
if (isInitializable(peerStore)) { | ||
peerStore.init(this); | ||
} | ||
return peerStore; | ||
@@ -67,2 +156,5 @@ } | ||
this.upgrader = upgrader; | ||
if (isInitializable(upgrader)) { | ||
upgrader.init(this); | ||
} | ||
return upgrader; | ||
@@ -76,14 +168,7 @@ } | ||
} | ||
setDialer(dialer) { | ||
this.dialer = dialer; | ||
return dialer; | ||
} | ||
getDialer() { | ||
if (this.dialer == null) { | ||
throw errCode(new Error('dialer not set'), 'ERR_SERVICE_MISSING'); | ||
} | ||
return this.dialer; | ||
} | ||
setRegistrar(registrar) { | ||
this.registrar = registrar; | ||
if (isInitializable(registrar)) { | ||
registrar.init(this); | ||
} | ||
return registrar; | ||
@@ -99,2 +184,5 @@ } | ||
this.connectionManager = connectionManager; | ||
if (isInitializable(connectionManager)) { | ||
connectionManager.init(this); | ||
} | ||
return connectionManager; | ||
@@ -110,2 +198,5 @@ } | ||
this.transportManager = transportManager; | ||
if (isInitializable(transportManager)) { | ||
transportManager.init(this); | ||
} | ||
return transportManager; | ||
@@ -121,2 +212,5 @@ } | ||
this.connectionGater = connectionGater; | ||
if (isInitializable(connectionGater)) { | ||
connectionGater.init(this); | ||
} | ||
return connectionGater; | ||
@@ -132,2 +226,5 @@ } | ||
this.contentRouting = contentRouting; | ||
if (isInitializable(contentRouting)) { | ||
contentRouting.init(this); | ||
} | ||
return contentRouting; | ||
@@ -143,2 +240,5 @@ } | ||
this.peerRouting = peerRouting; | ||
if (isInitializable(peerRouting)) { | ||
peerRouting.init(this); | ||
} | ||
return peerRouting; | ||
@@ -154,2 +254,5 @@ } | ||
this.datastore = datastore; | ||
if (isInitializable(datastore)) { | ||
datastore.init(this); | ||
} | ||
return datastore; | ||
@@ -165,2 +268,5 @@ } | ||
this.connectionProtector = connectionProtector; | ||
if (isInitializable(connectionProtector)) { | ||
connectionProtector.init(this); | ||
} | ||
return connectionProtector; | ||
@@ -173,2 +279,5 @@ } | ||
this.dht = dht; | ||
if (isInitializable(dht)) { | ||
dht.init(this); | ||
} | ||
return dht; | ||
@@ -184,2 +293,5 @@ } | ||
this.pubsub = pubsub; | ||
if (isInitializable(pubsub)) { | ||
pubsub.init(this); | ||
} | ||
return pubsub; | ||
@@ -186,0 +298,0 @@ } |
@@ -1,4 +0,2 @@ | ||
import type { EventEmitter } from '../index.js'; | ||
import type { Connection, Stream } from '../connection/index.js'; | ||
import type { PeerId } from '../peer-id/index.js'; | ||
import type { Topology } from '../topology/index.js'; | ||
@@ -10,24 +8,2 @@ export interface IncomingStreamData { | ||
} | ||
export interface ConnectionManagerEvents { | ||
'peer:connect': CustomEvent<Connection>; | ||
'peer:disconnect': CustomEvent<Connection>; | ||
} | ||
export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents> { | ||
/** | ||
* Return all connections to the remote peer or an empty array | ||
*/ | ||
getConnections: (peerId: PeerId) => Connection[]; | ||
/** | ||
* Return the first connection to a remote peer, if any | ||
*/ | ||
getConnection: (peerId: PeerId) => Connection | undefined; | ||
/** | ||
* Returns all connections keyed by the stringified peer ID of the remote peer | ||
*/ | ||
getConnectionMap: () => Map<string, Connection[]>; | ||
/** | ||
* Returns all connections | ||
*/ | ||
getConnectionList: () => Connection[]; | ||
} | ||
export interface StreamHandler { | ||
@@ -34,0 +10,0 @@ (data: IncomingStreamData): void; |
{ | ||
"name": "@libp2p/interfaces", | ||
"version": "1.3.26", | ||
"version": "1.3.27", | ||
"description": "Interfaces for JS Libp2p", | ||
@@ -67,2 +67,6 @@ "license": "Apache-2.0 OR MIT", | ||
}, | ||
"./connection-manager": { | ||
"import": "./dist/src/connection-manager/index.js", | ||
"types": "./dist/src/connection-manager/index.d.ts" | ||
}, | ||
"./connection/status": { | ||
@@ -120,2 +124,6 @@ "import": "./dist/src/connection/status.js", | ||
}, | ||
"./registrar": { | ||
"import": "./dist/src/registrar/index.js", | ||
"types": "./dist/src/registrar/index.d.ts" | ||
}, | ||
"./stream-muxer": { | ||
@@ -122,0 +130,0 @@ "import": "./dist/src/stream-muxer/index.js", |
import errCode from 'err-code' | ||
import type { ConnectionGater, ConnectionProtector } from './connection/index.js' | ||
import type { ContentRouting } from './content-routing/index.js' | ||
import type { Dialer } from './dialer/index.js' | ||
import type { AddressManager } from './index.js' | ||
import { AddressManager, isStartable, Startable } from './index.js' | ||
import type { Metrics } from './metrics/index.js' | ||
@@ -10,3 +9,3 @@ import type { PeerId } from './peer-id/index.js' | ||
import type { PeerStore } from './peer-store/index.js' | ||
import type { ConnectionManager, Registrar } from './registrar/index.js' | ||
import type { Registrar } from './registrar/index.js' | ||
import type { TransportManager, Upgrader } from './transport/index.js' | ||
@@ -16,2 +15,3 @@ import type { Datastore } from 'interface-datastore' | ||
import type { DualDHT } from './dht/index.js' | ||
import type { ConnectionManager } from './connection-manager/index.js' | ||
@@ -32,3 +32,2 @@ export interface Initializable { | ||
metrics?: Metrics | ||
dialer?: Dialer | ||
registrar?: Registrar | ||
@@ -46,3 +45,3 @@ connectionManager?: ConnectionManager | ||
export class Components { | ||
export class Components implements Startable { | ||
private peerId?: PeerId | ||
@@ -53,3 +52,2 @@ private addressManager?: AddressManager | ||
private metrics?: Metrics | ||
private dialer?: Dialer | ||
private registrar?: Registrar | ||
@@ -65,22 +63,130 @@ private connectionManager?: ConnectionManager | ||
private pubsub?: PubSub | ||
private started = false | ||
constructor (init: ComponentsInit = {}) { | ||
this.peerId = init.peerId | ||
this.addressManager = init.addressManager | ||
this.peerStore = init.peerStore | ||
this.upgrader = init.upgrader | ||
this.metrics = init.metrics | ||
this.dialer = init.dialer | ||
this.registrar = init.registrar | ||
this.connectionManager = init.connectionManager | ||
this.transportManager = init.transportManager | ||
this.connectionGater = init.connectionGater | ||
this.contentRouting = init.contentRouting | ||
this.peerRouting = init.peerRouting | ||
this.datastore = init.datastore | ||
this.connectionProtector = init.connectionProtector | ||
this.dht = init.dht | ||
this.pubsub = init.pubsub | ||
if (init.peerId != null) { | ||
this.setPeerId(init.peerId) | ||
} | ||
if (init.addressManager != null) { | ||
this.setAddressManager(init.addressManager) | ||
} | ||
if (init.peerStore != null) { | ||
this.setPeerStore(init.peerStore) | ||
} | ||
if (init.upgrader != null) { | ||
this.setUpgrader(init.upgrader) | ||
} | ||
if (init.metrics != null) { | ||
this.setMetrics(init.metrics) | ||
} | ||
if (init.registrar != null) { | ||
this.setRegistrar(init.registrar) | ||
} | ||
if (init.connectionManager != null) { | ||
this.setConnectionManager(init.connectionManager) | ||
} | ||
if (init.transportManager != null) { | ||
this.setTransportManager(init.transportManager) | ||
} | ||
if (init.connectionGater != null) { | ||
this.setConnectionGater(init.connectionGater) | ||
} | ||
if (init.contentRouting != null) { | ||
this.setContentRouting(init.contentRouting) | ||
} | ||
if (init.peerRouting != null) { | ||
this.setPeerRouting(init.peerRouting) | ||
} | ||
if (init.datastore != null) { | ||
this.setDatastore(init.datastore) | ||
} | ||
if (init.connectionProtector != null) { | ||
this.setConnectionProtector(init.connectionProtector) | ||
} | ||
if (init.dht != null) { | ||
this.setDHT(init.dht) | ||
} | ||
if (init.pubsub != null) { | ||
this.setPubSub(init.pubsub) | ||
} | ||
} | ||
isStarted () { | ||
return this.started | ||
} | ||
async beforeStart () { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj as Startable | ||
if (startable.beforeStart != null) { | ||
await startable.beforeStart() | ||
} | ||
} | ||
} | ||
async start () { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj as Startable | ||
await startable.start() | ||
} | ||
this.started = true | ||
} | ||
async afterStart () { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj as Startable | ||
if (startable.afterStart != null) { | ||
await startable.afterStart() | ||
} | ||
} | ||
} | ||
async beforeStop () { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj as Startable | ||
if (startable.beforeStop != null) { | ||
await startable.beforeStop() | ||
} | ||
} | ||
} | ||
async stop () { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj as Startable | ||
await startable.stop() | ||
} | ||
this.started = false | ||
} | ||
async afterStop () { | ||
for (const obj of Object.values(this).filter(obj => isStartable(obj))) { | ||
const startable = obj as Startable | ||
if (startable.afterStop != null) { | ||
await startable.afterStop() | ||
} | ||
} | ||
} | ||
setPeerId (peerId: PeerId) { | ||
@@ -103,2 +209,6 @@ this.peerId = peerId | ||
if (isInitializable(metrics)) { | ||
metrics.init(this) | ||
} | ||
return metrics | ||
@@ -114,2 +224,6 @@ } | ||
if (isInitializable(addressManager)) { | ||
addressManager.init(this) | ||
} | ||
return addressManager | ||
@@ -129,2 +243,6 @@ } | ||
if (isInitializable(peerStore)) { | ||
peerStore.init(this) | ||
} | ||
return peerStore | ||
@@ -144,2 +262,6 @@ } | ||
if (isInitializable(upgrader)) { | ||
upgrader.init(this) | ||
} | ||
return upgrader | ||
@@ -156,19 +278,9 @@ } | ||
setDialer (dialer: Dialer) { | ||
this.dialer = dialer | ||
setRegistrar (registrar: Registrar) { | ||
this.registrar = registrar | ||
return dialer | ||
} | ||
getDialer (): Dialer { | ||
if (this.dialer == null) { | ||
throw errCode(new Error('dialer not set'), 'ERR_SERVICE_MISSING') | ||
if (isInitializable(registrar)) { | ||
registrar.init(this) | ||
} | ||
return this.dialer | ||
} | ||
setRegistrar (registrar: Registrar) { | ||
this.registrar = registrar | ||
return registrar | ||
@@ -188,2 +300,6 @@ } | ||
if (isInitializable(connectionManager)) { | ||
connectionManager.init(this) | ||
} | ||
return connectionManager | ||
@@ -203,2 +319,6 @@ } | ||
if (isInitializable(transportManager)) { | ||
transportManager.init(this) | ||
} | ||
return transportManager | ||
@@ -218,2 +338,6 @@ } | ||
if (isInitializable(connectionGater)) { | ||
connectionGater.init(this) | ||
} | ||
return connectionGater | ||
@@ -233,2 +357,6 @@ } | ||
if (isInitializable(contentRouting)) { | ||
contentRouting.init(this) | ||
} | ||
return contentRouting | ||
@@ -248,2 +376,6 @@ } | ||
if (isInitializable(peerRouting)) { | ||
peerRouting.init(this) | ||
} | ||
return peerRouting | ||
@@ -263,2 +395,6 @@ } | ||
if (isInitializable(datastore)) { | ||
datastore.init(this) | ||
} | ||
return datastore | ||
@@ -278,2 +414,6 @@ } | ||
if (isInitializable(connectionProtector)) { | ||
connectionProtector.init(this) | ||
} | ||
return connectionProtector | ||
@@ -289,2 +429,6 @@ } | ||
if (isInitializable(dht)) { | ||
dht.init(this) | ||
} | ||
return dht | ||
@@ -304,2 +448,6 @@ } | ||
if (isInitializable(pubsub)) { | ||
pubsub.init(this) | ||
} | ||
return pubsub | ||
@@ -306,0 +454,0 @@ } |
@@ -1,4 +0,2 @@ | ||
import type { EventEmitter } from '../index.js' | ||
import type { Connection, Stream } from '../connection/index.js' | ||
import type { PeerId } from '../peer-id/index.js' | ||
import type { Topology } from '../topology/index.js' | ||
@@ -12,29 +10,2 @@ | ||
export interface ConnectionManagerEvents { | ||
'peer:connect': CustomEvent<Connection> | ||
'peer:disconnect': CustomEvent<Connection> | ||
} | ||
export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents> { | ||
/** | ||
* Return all connections to the remote peer or an empty array | ||
*/ | ||
getConnections: (peerId: PeerId) => Connection[] | ||
/** | ||
* Return the first connection to a remote peer, if any | ||
*/ | ||
getConnection: (peerId: PeerId) => Connection | undefined | ||
/** | ||
* Returns all connections keyed by the stringified peer ID of the remote peer | ||
*/ | ||
getConnectionMap: () => Map<string, Connection[]> | ||
/** | ||
* Returns all connections | ||
*/ | ||
getConnectionList: () => Connection[] | ||
} | ||
export interface StreamHandler { | ||
@@ -41,0 +12,0 @@ (data: IncomingStreamData): void |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1378330
3646