@libp2p/interfaces
Advanced tools
Comparing version 1.3.30 to 1.3.31
import type { ConnectionGater, ConnectionProtector } from './connection/index.js'; | ||
import type { ContentRouting } from './content-routing/index.js'; | ||
import { AddressManager, Startable } from './index.js'; | ||
import type { AddressManager } from './address-manager/index.js'; | ||
import { Startable } from './startable.js'; | ||
import type { Metrics } from './metrics/index.js'; | ||
@@ -5,0 +6,0 @@ import type { PeerId } from './peer-id/index.js'; |
import errCode from 'err-code'; | ||
import { isStartable } from './index.js'; | ||
import { isStartable } from './startable.js'; | ||
export function isInitializable(obj) { | ||
@@ -4,0 +4,0 @@ return obj != null && typeof obj.init === 'function'; |
@@ -1,2 +0,3 @@ | ||
import type { AbortOptions, EventEmitter } from '../index.js'; | ||
import type { AbortOptions } from '../index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
import type { Connection } from '../connection/index.js'; | ||
@@ -3,0 +4,0 @@ import type { PeerId } from '../peer-id/index.js'; |
@@ -1,101 +0,4 @@ | ||
import type { Multiaddr } from '@multiformats/multiaddr'; | ||
export interface AbortOptions { | ||
signal?: AbortSignal; | ||
} | ||
/** | ||
* Implemented by components that have a lifecycle | ||
*/ | ||
export interface Startable { | ||
isStarted: () => boolean; | ||
/** | ||
* If implemented, this method will be invoked before the start method. | ||
* | ||
* It should not assume any other components have been started. | ||
*/ | ||
beforeStart?: () => void | Promise<void>; | ||
/** | ||
* This method will be invoked to start the component. | ||
* | ||
* It should not assume that any other components have been started. | ||
*/ | ||
start: () => void | Promise<void>; | ||
/** | ||
* If implemented, this method will be invoked after the start method. | ||
* | ||
* All other components will have had their start method invoked before this method is called. | ||
*/ | ||
afterStart?: () => void | Promise<void>; | ||
/** | ||
* If implemented, this method will be invoked before the stop method. | ||
* | ||
* Any other components will still be running when this method is called. | ||
*/ | ||
beforeStop?: () => void | Promise<void>; | ||
/** | ||
* This method will be invoked to stop the component. | ||
* | ||
* It should not assume any other components are running when it is called. | ||
*/ | ||
stop: () => void | Promise<void>; | ||
/** | ||
* If implemented, this method will be invoked after the stop method. | ||
* | ||
* All other components will have had their stop method invoked before this method is called. | ||
*/ | ||
afterStop?: () => void | Promise<void>; | ||
} | ||
export declare function isStartable(obj: any): obj is Startable; | ||
export interface EventCallback<EventType> { | ||
(evt: EventType): void; | ||
} | ||
export declare type EventHandler<EventType> = EventCallback<EventType> | ({ | ||
handleEvent: EventCallback<EventType>; | ||
}) | null; | ||
/** | ||
* Adds types to the EventTarget class. Hopefully this won't be necessary forever. | ||
* | ||
* https://github.com/microsoft/TypeScript/issues/28357 | ||
* https://github.com/microsoft/TypeScript/issues/43477 | ||
* https://github.com/microsoft/TypeScript/issues/299 | ||
* etc | ||
*/ | ||
export declare class EventEmitter<EventMap> extends EventTarget { | ||
#private; | ||
listenerCount(type: string): number; | ||
addEventListener<U extends keyof EventMap>(type: U, callback: EventHandler<EventMap[U]>, options?: AddEventListenerOptions | boolean): void; | ||
removeEventListener<U extends keyof EventMap>(type: U, callback?: EventHandler<EventMap[U]> | undefined, options?: EventListenerOptions | boolean): void; | ||
dispatchEvent(event: Event): boolean; | ||
} | ||
export declare const CustomEvent: { | ||
new <T>(type: string, eventInitDict?: CustomEventInit<T> | undefined): CustomEvent<T>; | ||
prototype: CustomEvent<any>; | ||
}; | ||
export interface AddressManagerEvents { | ||
/** | ||
* Emitted when the current node's addresses change | ||
*/ | ||
'change:addresses': CustomEvent; | ||
} | ||
export interface AddressManager extends EventEmitter<AddressManagerEvents> { | ||
/** | ||
* Get peer listen multiaddrs | ||
*/ | ||
getListenAddrs: () => Multiaddr[]; | ||
/** | ||
* Get peer announcing multiaddrs | ||
*/ | ||
getAnnounceAddrs: () => Multiaddr[]; | ||
/** | ||
* Get observed multiaddrs | ||
*/ | ||
getObservedAddrs: () => Multiaddr[]; | ||
/** | ||
* Add peer observed addresses | ||
*/ | ||
addObservedAddr: (addr: Multiaddr) => void; | ||
/** | ||
* Get the current node's addresses | ||
*/ | ||
getAddresses: () => Multiaddr[]; | ||
} | ||
export declare type RecursivePartial<T> = { | ||
@@ -102,0 +5,0 @@ [P in keyof T]?: T[P] extends Array<infer I> ? Array<RecursivePartial<I>> : T[P] extends (...args: any[]) => any ? T[P] : RecursivePartial<T[P]>; |
@@ -1,82 +0,2 @@ | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var _EventEmitter_listeners; | ||
export function isStartable(obj) { | ||
return obj != null && typeof obj.start === 'function' && typeof obj.stop === 'function'; | ||
} | ||
/** | ||
* Adds types to the EventTarget class. Hopefully this won't be necessary forever. | ||
* | ||
* https://github.com/microsoft/TypeScript/issues/28357 | ||
* https://github.com/microsoft/TypeScript/issues/43477 | ||
* https://github.com/microsoft/TypeScript/issues/299 | ||
* etc | ||
*/ | ||
export class EventEmitter extends EventTarget { | ||
constructor() { | ||
super(...arguments); | ||
_EventEmitter_listeners.set(this, new Map()); | ||
} | ||
listenerCount(type) { | ||
const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(type); | ||
if (listeners == null) { | ||
return 0; | ||
} | ||
return listeners.length; | ||
} | ||
// @ts-expect-error EventTarget is not typed | ||
addEventListener(type, callback, options) { | ||
// @ts-expect-error EventTarget is not typed | ||
super.addEventListener(type, callback, options); | ||
let list = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(type); | ||
if (list == null) { | ||
list = []; | ||
__classPrivateFieldGet(this, _EventEmitter_listeners, "f").set(type, list); | ||
} | ||
list.push({ | ||
callback, | ||
once: (options !== true && options !== false && options?.once) ?? false | ||
}); | ||
} | ||
// @ts-expect-error EventTarget is not typed | ||
removeEventListener(type, callback, options) { | ||
// @ts-expect-error EventTarget is not typed | ||
super.removeEventListener(type, callback, options); | ||
let list = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(type); | ||
if (list == null) { | ||
return; | ||
} | ||
list = list.filter(({ callback: cb }) => cb !== callback); | ||
__classPrivateFieldGet(this, _EventEmitter_listeners, "f").set(type, list); | ||
} | ||
dispatchEvent(event) { | ||
const result = super.dispatchEvent(event); | ||
let list = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(event.type); | ||
if (list == null) { | ||
return result; | ||
} | ||
list = list.filter(({ once }) => !once); | ||
__classPrivateFieldGet(this, _EventEmitter_listeners, "f").set(event.type, list); | ||
return result; | ||
} | ||
} | ||
_EventEmitter_listeners = new WeakMap(); | ||
/** | ||
* CustomEvent is a standard event but it's not supported by node. | ||
* | ||
* Remove this when https://github.com/nodejs/node/issues/40678 is closed. | ||
* | ||
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent | ||
*/ | ||
class CustomEventPolyfill extends Event { | ||
constructor(message, data) { | ||
super(message, data); | ||
// @ts-expect-error could be undefined | ||
this.detail = data?.detail; | ||
} | ||
} | ||
export const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill; | ||
export {}; | ||
//# sourceMappingURL=index.js.map |
import type { PeerInfo } from '../peer-info/index.js'; | ||
import type { EventEmitter } from '../index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
export interface PeerDiscoveryEvents { | ||
@@ -4,0 +4,0 @@ 'peer': CustomEvent<PeerInfo>; |
import type { PeerId } from '../peer-id/index.js'; | ||
import type { Multiaddr } from '@multiformats/multiaddr'; | ||
import type { EventEmitter } from '../index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
import type { Envelope } from '../record/index.js'; | ||
@@ -5,0 +5,0 @@ import type { PeerInfo } from '../peer-info/index.js'; |
import type { PeerId } from '../peer-id/index.js'; | ||
import type { Pushable } from 'it-pushable'; | ||
import type { EventEmitter } from '../index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
import type { Stream } from '../connection/index.js'; | ||
@@ -5,0 +5,0 @@ /** |
@@ -1,2 +0,3 @@ | ||
import type { EventEmitter, AbortOptions } from '../index.js'; | ||
import type { AbortOptions } from '../index.js'; | ||
import type { EventEmitter } from '../events.js'; | ||
import type { Multiaddr } from '@multiformats/multiaddr'; | ||
@@ -3,0 +4,0 @@ import type { Connection } from '../connection/index.js'; |
{ | ||
"name": "@libp2p/interfaces", | ||
"version": "1.3.30", | ||
"version": "1.3.31", | ||
"description": "Interfaces for JS Libp2p", | ||
@@ -51,2 +51,6 @@ "license": "Apache-2.0 OR MIT", | ||
}, | ||
"./address-manager": { | ||
"import": "./dist/src/address-manager.js", | ||
"types": "./dist/src/address-manager.d.ts" | ||
}, | ||
"./components": { | ||
@@ -88,2 +92,6 @@ "import": "./dist/src/components.js", | ||
}, | ||
"./events": { | ||
"import": "./dist/src/events.js", | ||
"types": "./dist/src/events.d.ts" | ||
}, | ||
"./keys": { | ||
@@ -129,2 +137,6 @@ "import": "./dist/src/keys/index.js", | ||
}, | ||
"./startable": { | ||
"import": "./dist/src/startable.js", | ||
"types": "./dist/src/startable.d.ts" | ||
}, | ||
"./stream-muxer": { | ||
@@ -131,0 +143,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 { AddressManager, isStartable, Startable } from './index.js' | ||
import type { AddressManager } from './address-manager/index.js' | ||
import { isStartable, Startable } from './startable.js' | ||
import type { Metrics } from './metrics/index.js' | ||
@@ -6,0 +7,0 @@ import type { PeerId } from './peer-id/index.js' |
@@ -1,2 +0,3 @@ | ||
import type { AbortOptions, EventEmitter } from '../index.js' | ||
import type { AbortOptions } from '../index.js' | ||
import type { EventEmitter } from '../events.js' | ||
import type { Connection } from '../connection/index.js' | ||
@@ -3,0 +4,0 @@ import type { PeerId } from '../peer-id/index.js' |
186
src/index.ts
@@ -1,2 +0,1 @@ | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
@@ -7,187 +6,2 @@ export interface AbortOptions { | ||
/** | ||
* Implemented by components that have a lifecycle | ||
*/ | ||
export interface Startable { | ||
isStarted: () => boolean | ||
/** | ||
* If implemented, this method will be invoked before the start method. | ||
* | ||
* It should not assume any other components have been started. | ||
*/ | ||
beforeStart?: () => void | Promise<void> | ||
/** | ||
* This method will be invoked to start the component. | ||
* | ||
* It should not assume that any other components have been started. | ||
*/ | ||
start: () => void | Promise<void> | ||
/** | ||
* If implemented, this method will be invoked after the start method. | ||
* | ||
* All other components will have had their start method invoked before this method is called. | ||
*/ | ||
afterStart?: () => void | Promise<void> | ||
/** | ||
* If implemented, this method will be invoked before the stop method. | ||
* | ||
* Any other components will still be running when this method is called. | ||
*/ | ||
beforeStop?: () => void | Promise<void> | ||
/** | ||
* This method will be invoked to stop the component. | ||
* | ||
* It should not assume any other components are running when it is called. | ||
*/ | ||
stop: () => void | Promise<void> | ||
/** | ||
* If implemented, this method will be invoked after the stop method. | ||
* | ||
* All other components will have had their stop method invoked before this method is called. | ||
*/ | ||
afterStop?: () => void | Promise<void> | ||
} | ||
export function isStartable (obj: any): obj is Startable { | ||
return obj != null && typeof obj.start === 'function' && typeof obj.stop === 'function' | ||
} | ||
export interface EventCallback<EventType> { (evt: EventType): void } | ||
export type EventHandler<EventType> = EventCallback<EventType> | ({ handleEvent: EventCallback<EventType> }) | null | ||
interface Listener { | ||
once: boolean | ||
callback: any | ||
} | ||
/** | ||
* Adds types to the EventTarget class. Hopefully this won't be necessary forever. | ||
* | ||
* https://github.com/microsoft/TypeScript/issues/28357 | ||
* https://github.com/microsoft/TypeScript/issues/43477 | ||
* https://github.com/microsoft/TypeScript/issues/299 | ||
* etc | ||
*/ | ||
export class EventEmitter<EventMap> extends EventTarget { | ||
#listeners: Map<any, Listener[]> = new Map() | ||
listenerCount (type: string) { | ||
const listeners = this.#listeners.get(type) | ||
if (listeners == null) { | ||
return 0 | ||
} | ||
return listeners.length | ||
} | ||
// @ts-expect-error EventTarget is not typed | ||
addEventListener<U extends keyof EventMap> (type: U, callback: EventHandler<EventMap[U]>, options?: AddEventListenerOptions | boolean) { | ||
// @ts-expect-error EventTarget is not typed | ||
super.addEventListener(type, callback, options) | ||
let list = this.#listeners.get(type) | ||
if (list == null) { | ||
list = [] | ||
this.#listeners.set(type, list) | ||
} | ||
list.push({ | ||
callback, | ||
once: (options !== true && options !== false && options?.once) ?? false | ||
}) | ||
} | ||
// @ts-expect-error EventTarget is not typed | ||
removeEventListener<U extends keyof EventMap> (type: U, callback?: EventHandler<EventMap[U]> | undefined, options?: EventListenerOptions | boolean) { | ||
// @ts-expect-error EventTarget is not typed | ||
super.removeEventListener(type, callback, options) | ||
let list = this.#listeners.get(type) | ||
if (list == null) { | ||
return | ||
} | ||
list = list.filter(({ callback: cb }) => cb !== callback) | ||
this.#listeners.set(type, list) | ||
} | ||
dispatchEvent (event: Event): boolean { | ||
const result = super.dispatchEvent(event) | ||
let list = this.#listeners.get(event.type) | ||
if (list == null) { | ||
return result | ||
} | ||
list = list.filter(({ once }) => !once) | ||
this.#listeners.set(event.type, list) | ||
return result | ||
} | ||
} | ||
/** | ||
* CustomEvent is a standard event but it's not supported by node. | ||
* | ||
* Remove this when https://github.com/nodejs/node/issues/40678 is closed. | ||
* | ||
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent | ||
*/ | ||
class CustomEventPolyfill<T = any> extends Event { | ||
/** Returns any custom data event was created with. Typically used for synthetic events. */ | ||
public detail: T | ||
constructor (message: string, data?: EventInit & { detail: T }) { | ||
super(message, data) | ||
// @ts-expect-error could be undefined | ||
this.detail = data?.detail | ||
} | ||
} | ||
export const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill | ||
export interface AddressManagerEvents { | ||
/** | ||
* Emitted when the current node's addresses change | ||
*/ | ||
'change:addresses': CustomEvent | ||
} | ||
export interface AddressManager extends EventEmitter<AddressManagerEvents> { | ||
/** | ||
* Get peer listen multiaddrs | ||
*/ | ||
getListenAddrs: () => Multiaddr[] | ||
/** | ||
* Get peer announcing multiaddrs | ||
*/ | ||
getAnnounceAddrs: () => Multiaddr[] | ||
/** | ||
* Get observed multiaddrs | ||
*/ | ||
getObservedAddrs: () => Multiaddr[] | ||
/** | ||
* Add peer observed addresses | ||
*/ | ||
addObservedAddr: (addr: Multiaddr) => void | ||
/** | ||
* Get the current node's addresses | ||
*/ | ||
getAddresses: () => Multiaddr[] | ||
} | ||
// Borrowed from the tsdef module | ||
@@ -194,0 +8,0 @@ export type RecursivePartial<T> = { |
import type { PeerInfo } from '../peer-info/index.js' | ||
import type { EventEmitter } from '../index.js' | ||
import type { EventEmitter } from '../events.js' | ||
@@ -4,0 +4,0 @@ export interface PeerDiscoveryEvents { |
import type { PeerId } from '../peer-id/index.js' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
import type { EventEmitter } from '../index.js' | ||
import type { EventEmitter } from '../events.js' | ||
import type { Envelope } from '../record/index.js' | ||
@@ -5,0 +5,0 @@ import type { PeerInfo } from '../peer-info/index.js' |
import type { PeerId } from '../peer-id/index.js' | ||
import type { Pushable } from 'it-pushable' | ||
import type { EventEmitter } from '../index.js' | ||
import type { EventEmitter } from '../events.js' | ||
import type { Stream } from '../connection/index.js' | ||
@@ -5,0 +5,0 @@ |
@@ -1,2 +0,3 @@ | ||
import type { EventEmitter, AbortOptions } from '../index.js' | ||
import type { AbortOptions } from '../index.js' | ||
import type { EventEmitter } from '../events.js' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
@@ -3,0 +4,0 @@ import type { Connection } from '../connection/index.js' |
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
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
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
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
1381322
174
3664