2020-09-30 11:21:11 +02:00
|
|
|
export = PeerStreams;
|
|
|
|
/**
|
|
|
|
* @callback Sink
|
|
|
|
* @param {Uint8Array} source
|
|
|
|
* @returns {Promise<Uint8Array>}
|
|
|
|
*
|
|
|
|
* @typedef {object} DuplexIterableStream
|
|
|
|
* @property {Sink} sink
|
|
|
|
* @property {() AsyncIterator<Uint8Array>} source
|
|
|
|
*
|
|
|
|
* @typedef PeerId
|
|
|
|
* @type import('peer-id')
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Thin wrapper around a peer's inbound / outbound pubsub streams
|
|
|
|
*/
|
|
|
|
declare class PeerStreams {
|
|
|
|
/**
|
2020-11-30 16:43:51 +01:00
|
|
|
* @param {object} properties - properties of the PeerStreams.
|
2020-09-30 11:21:11 +02:00
|
|
|
* @param {PeerId} properties.id
|
|
|
|
* @param {string} properties.protocol
|
|
|
|
*/
|
|
|
|
constructor({ id, protocol }: {
|
2020-11-30 16:43:51 +01:00
|
|
|
id: PeerId;
|
2020-09-30 11:21:11 +02:00
|
|
|
protocol: string;
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* @type {import('peer-id')}
|
|
|
|
*/
|
|
|
|
id: import('peer-id');
|
|
|
|
/**
|
|
|
|
* Established protocol
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
protocol: string;
|
|
|
|
/**
|
|
|
|
* The raw outbound stream, as retrieved from conn.newStream
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @private
|
|
|
|
* @type {DuplexIterableStream}
|
|
|
|
*/
|
2020-11-30 16:43:51 +01:00
|
|
|
private _rawOutboundStream;
|
2020-09-30 11:21:11 +02:00
|
|
|
/**
|
|
|
|
* The raw inbound stream, as retrieved from the callback from libp2p.handle
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @private
|
|
|
|
* @type {DuplexIterableStream}
|
|
|
|
*/
|
2020-11-30 16:43:51 +01:00
|
|
|
private _rawInboundStream;
|
2020-09-30 11:21:11 +02:00
|
|
|
/**
|
|
|
|
* An AbortController for controlled shutdown of the inbound stream
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @private
|
|
|
|
* @type {typeof AbortController}
|
|
|
|
*/
|
2020-11-30 16:43:51 +01:00
|
|
|
private _inboundAbortController;
|
2020-09-30 11:21:11 +02:00
|
|
|
/**
|
|
|
|
* Write stream -- its preferable to use the write method
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @type {import('it-pushable').Pushable<Uint8Array>>}
|
|
|
|
*/
|
|
|
|
outboundStream: import('it-pushable').Pushable<Uint8Array>;
|
|
|
|
/**
|
|
|
|
* Read stream
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @type {DuplexIterableStream}
|
|
|
|
*/
|
|
|
|
inboundStream: DuplexIterableStream;
|
|
|
|
/**
|
|
|
|
* Do we have a connection to read from?
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
get isReadable(): boolean;
|
|
|
|
/**
|
|
|
|
* Do we have a connection to write on?
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
get isWritable(): boolean;
|
|
|
|
/**
|
|
|
|
* Send a message to this peer.
|
|
|
|
* Throws if there is no `stream` to write to available.
|
|
|
|
*
|
|
|
|
* @param {Uint8Array} data
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
write(data: Uint8Array): void;
|
|
|
|
/**
|
|
|
|
* Attach a raw inbound stream and setup a read stream
|
|
|
|
*
|
|
|
|
* @param {DuplexIterableStream} stream
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
attachInboundStream(stream: DuplexIterableStream): void;
|
|
|
|
/**
|
|
|
|
* Attach a raw outbound stream and setup a write stream
|
|
|
|
*
|
|
|
|
* @param {Stream} stream
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
attachOutboundStream(stream: any): Promise<void>;
|
|
|
|
/**
|
|
|
|
* Closes the open connection to peer
|
2020-11-30 16:43:51 +01:00
|
|
|
*
|
2020-09-30 11:21:11 +02:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
close(): void;
|
|
|
|
}
|
|
|
|
declare namespace PeerStreams {
|
|
|
|
export { Sink, DuplexIterableStream, PeerId };
|
|
|
|
}
|
|
|
|
type DuplexIterableStream = {
|
|
|
|
sink: Sink;
|
2020-11-30 16:43:51 +01:00
|
|
|
source: () => AsyncIterator<Uint8Array>;
|
2020-09-30 11:21:11 +02:00
|
|
|
};
|
|
|
|
type PeerId = import("peer-id");
|
2020-11-30 16:43:51 +01:00
|
|
|
type Sink = (source: Uint8Array) => Promise<Uint8Array>;
|