@libp2p/interface 0.0.1-8b0e6bef → 0.0.1-e9cafd3d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,31 +10,9 @@ export interface ConnectionTimeline {
10
10
  close?: number;
11
11
  }
12
12
  /**
13
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
13
+ * Outbound connections are opened by the local node, inbound streams are opened by the remote
14
14
  */
15
15
  export type Direction = 'inbound' | 'outbound';
16
- export interface ConnectionStat {
17
- /**
18
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
19
- */
20
- direction: Direction;
21
- /**
22
- * Lifecycle times for the connection
23
- */
24
- timeline: ConnectionTimeline;
25
- /**
26
- * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
27
- */
28
- multiplexer?: string;
29
- /**
30
- * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
31
- */
32
- encryption?: string;
33
- /**
34
- * The current status of the connection
35
- */
36
- status: keyof typeof Status;
37
- }
38
16
  export interface StreamTimeline {
39
17
  /**
40
18
  * A timestamp of when the stream was opened
@@ -57,20 +35,6 @@ export interface StreamTimeline {
57
35
  */
58
36
  reset?: number;
59
37
  }
60
- export interface StreamStat {
61
- /**
62
- * Outbound streams are opened by the local node, inbound streams are opened by the remote
63
- */
64
- direction: Direction;
65
- /**
66
- * Lifecycle times for the stream
67
- */
68
- timeline: StreamTimeline;
69
- /**
70
- * Once a protocol has been negotiated for this stream, it will be set on the stat object
71
- */
72
- protocol?: string;
73
- }
74
38
  /**
75
39
  * A Stream is a data channel between two peers that
76
40
  * can be written to and read from at both ends.
@@ -126,9 +90,17 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
126
90
  */
127
91
  id: string;
128
92
  /**
129
- * Stats about this stream
93
+ * Outbound streams are opened by the local node, inbound streams are opened by the remote
94
+ */
95
+ direction: Direction;
96
+ /**
97
+ * Lifecycle times for the stream
98
+ */
99
+ timeline: StreamTimeline;
100
+ /**
101
+ * Once a protocol has been negotiated for this stream, it will be set on the stat object
130
102
  */
131
- stat: StreamStat;
103
+ protocol?: string;
132
104
  /**
133
105
  * User defined stream metadata
134
106
  */
@@ -149,12 +121,46 @@ export interface NewStreamOptions extends AbortOptions {
149
121
  * between which the connection is made.
150
122
  */
151
123
  export interface Connection {
124
+ /**
125
+ * The unique identifier for this connection
126
+ */
152
127
  id: string;
153
- stat: ConnectionStat;
128
+ /**
129
+ * The address of the remote end of the connection
130
+ */
154
131
  remoteAddr: Multiaddr;
132
+ /**
133
+ * The id of the peer at the remote end of the connection
134
+ */
155
135
  remotePeer: PeerId;
136
+ /**
137
+ * A list of tags applied to this connection
138
+ */
156
139
  tags: string[];
140
+ /**
141
+ * A list of open streams on this connection
142
+ */
157
143
  streams: Stream[];
144
+ /**
145
+ * Outbound conections are opened by the local node, inbound streams are opened by the remote
146
+ */
147
+ direction: Direction;
148
+ /**
149
+ * Lifecycle times for the connection
150
+ */
151
+ timeline: ConnectionTimeline;
152
+ /**
153
+ * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
154
+ */
155
+ multiplexer?: string;
156
+ /**
157
+ * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
158
+ */
159
+ encryption?: string;
160
+ /**
161
+ * The current status of the connection
162
+ */
163
+ status: keyof typeof Status;
158
164
  newStream: (multicodecs: string | string[], options?: NewStreamOptions) => Promise<Stream>;
159
165
  addStream: (stream: Stream) => void;
160
166
  removeStream: (id: string) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,MAAM,aAAa,CAAA;AAC1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;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AA2B,CAAA;CACtC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connection/index.ts"],"names":[],"mappings":"AA6LA,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAA;AAEtD,MAAM,UAAU,YAAY,CAAE,KAAU;IACtC,OAAO,KAAK,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;AAChD,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connection/index.ts"],"names":[],"mappings":"AAsMA,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAA;AAEtD,MAAM,UAAU,YAAY,CAAE,KAAU;IACtC,OAAO,KAAK,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;AAChD,CAAC"}
@@ -0,0 +1,236 @@
1
+ import type { Stream } from '../connection/index.js';
2
+ import type { EventEmitter } from '../events.js';
3
+ import type { PeerId } from '../peer-id/index.js';
4
+ import type { Pushable } from 'it-pushable';
5
+ import type { Uint8ArrayList } from 'uint8arraylist';
6
+ /**
7
+ * On the producing side:
8
+ * * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
9
+ *
10
+ * On the consuming side:
11
+ * * Enforce the fields to be present, reject otherwise.
12
+ * * Propagate only if the fields are valid and signature can be verified, reject otherwise.
13
+ */
14
+ export declare const StrictSign = "StrictSign";
15
+ /**
16
+ * On the producing side:
17
+ * * Build messages without the signature, key, from and seqno fields.
18
+ * * The corresponding protobuf key-value pairs are absent from the marshalled message, not just empty.
19
+ *
20
+ * On the consuming side:
21
+ * * Enforce the fields to be absent, reject otherwise.
22
+ * * Propagate only if the fields are absent, reject otherwise.
23
+ * * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
24
+ */
25
+ export declare const StrictNoSign = "StrictNoSign";
26
+ export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign;
27
+ export interface SignedMessage {
28
+ type: 'signed';
29
+ from: PeerId;
30
+ topic: string;
31
+ data: Uint8Array;
32
+ sequenceNumber: bigint;
33
+ signature: Uint8Array;
34
+ key: Uint8Array;
35
+ }
36
+ export interface UnsignedMessage {
37
+ type: 'unsigned';
38
+ topic: string;
39
+ data: Uint8Array;
40
+ }
41
+ export type Message = SignedMessage | UnsignedMessage;
42
+ export interface PubSubRPCMessage {
43
+ from?: Uint8Array;
44
+ topic?: string;
45
+ data?: Uint8Array;
46
+ sequenceNumber?: Uint8Array;
47
+ signature?: Uint8Array;
48
+ key?: Uint8Array;
49
+ }
50
+ export interface PubSubRPCSubscription {
51
+ subscribe?: boolean;
52
+ topic?: string;
53
+ }
54
+ export interface PubSubRPC {
55
+ subscriptions: PubSubRPCSubscription[];
56
+ messages: PubSubRPCMessage[];
57
+ }
58
+ export interface PeerStreams extends EventEmitter<PeerStreamEvents> {
59
+ id: PeerId;
60
+ protocol: string;
61
+ outboundStream?: Pushable<Uint8ArrayList>;
62
+ inboundStream?: AsyncIterable<Uint8ArrayList>;
63
+ isWritable: boolean;
64
+ close: () => void;
65
+ write: (buf: Uint8Array | Uint8ArrayList) => void;
66
+ attachInboundStream: (stream: Stream) => AsyncIterable<Uint8ArrayList>;
67
+ attachOutboundStream: (stream: Stream) => Promise<Pushable<Uint8ArrayList>>;
68
+ }
69
+ export interface PubSubInit {
70
+ enabled?: boolean;
71
+ multicodecs?: string[];
72
+ /**
73
+ * defines how signatures should be handled
74
+ */
75
+ globalSignaturePolicy?: SignaturePolicy;
76
+ /**
77
+ * if can relay messages not subscribed
78
+ */
79
+ canRelayMessage?: boolean;
80
+ /**
81
+ * if publish should emit to self, if subscribed
82
+ */
83
+ emitSelf?: boolean;
84
+ /**
85
+ * handle this many incoming pubsub messages concurrently
86
+ */
87
+ messageProcessingConcurrency?: number;
88
+ /**
89
+ * How many parallel incoming streams to allow on the pubsub protocol per-connection
90
+ */
91
+ maxInboundStreams?: number;
92
+ /**
93
+ * How many parallel outgoing streams to allow on the pubsub protocol per-connection
94
+ */
95
+ maxOutboundStreams?: number;
96
+ }
97
+ interface Subscription {
98
+ topic: string;
99
+ subscribe: boolean;
100
+ }
101
+ export interface SubscriptionChangeData {
102
+ peerId: PeerId;
103
+ subscriptions: Subscription[];
104
+ }
105
+ export interface PubSubEvents {
106
+ 'subscription-change': CustomEvent<SubscriptionChangeData>;
107
+ 'message': CustomEvent<Message>;
108
+ }
109
+ export interface PublishResult {
110
+ recipients: PeerId[];
111
+ }
112
+ export declare enum TopicValidatorResult {
113
+ /**
114
+ * The message is considered valid, and it should be delivered and forwarded to the network
115
+ */
116
+ Accept = "accept",
117
+ /**
118
+ * The message is neither delivered nor forwarded to the network
119
+ */
120
+ Ignore = "ignore",
121
+ /**
122
+ * The message is considered invalid, and it should be rejected
123
+ */
124
+ Reject = "reject"
125
+ }
126
+ export interface TopicValidatorFn {
127
+ (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>;
128
+ }
129
+ export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> {
130
+ /**
131
+ * The global signature policy controls whether or not we sill send and receive
132
+ * signed or unsigned messages.
133
+ *
134
+ * Signed messages prevent spoofing message senders and should be preferred to
135
+ * using unsigned messages.
136
+ */
137
+ globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign;
138
+ /**
139
+ * A list of multicodecs that contain the pubsub protocol name.
140
+ */
141
+ multicodecs: string[];
142
+ /**
143
+ * Pubsub routers support message validators per topic, which will validate the message
144
+ * before its propagations. They are stored in a map where keys are the topic name and
145
+ * values are the validators.
146
+ *
147
+ * @example
148
+ *
149
+ * ```js
150
+ * const topic = 'topic'
151
+ * const validateMessage = (msgTopic, msg) => {
152
+ * const input = uint8ArrayToString(msg.data)
153
+ * const validInputs = ['a', 'b', 'c']
154
+ *
155
+ * if (!validInputs.includes(input)) {
156
+ * throw new Error('no valid input received')
157
+ * }
158
+ * }
159
+ * libp2p.pubsub.topicValidators.set(topic, validateMessage)
160
+ * ```
161
+ */
162
+ topicValidators: Map<string, TopicValidatorFn>;
163
+ getPeers: () => PeerId[];
164
+ /**
165
+ * Gets a list of topics the node is subscribed to.
166
+ *
167
+ * ```js
168
+ * const topics = libp2p.pubsub.getTopics()
169
+ * ```
170
+ */
171
+ getTopics: () => string[];
172
+ /**
173
+ * Subscribes to a pubsub topic.
174
+ *
175
+ * @example
176
+ *
177
+ * ```js
178
+ * const topic = 'topic'
179
+ * const handler = (msg) => {
180
+ * if (msg.topic === topic) {
181
+ * // msg.data - pubsub data received
182
+ * }
183
+ * }
184
+ *
185
+ * libp2p.pubsub.addEventListener('message', handler)
186
+ * libp2p.pubsub.subscribe(topic)
187
+ * ```
188
+ */
189
+ subscribe: (topic: string) => void;
190
+ /**
191
+ * Unsubscribes from a pubsub topic.
192
+ *
193
+ * @example
194
+ *
195
+ * ```js
196
+ * const topic = 'topic'
197
+ * const handler = (msg) => {
198
+ * // msg.data - pubsub data received
199
+ * }
200
+ *
201
+ * libp2p.pubsub.removeEventListener(topic handler)
202
+ * libp2p.pubsub.unsubscribe(topic)
203
+ * ```
204
+ */
205
+ unsubscribe: (topic: string) => void;
206
+ /**
207
+ * Gets a list of the PeerIds that are subscribed to one topic.
208
+ *
209
+ * @example
210
+ *
211
+ * ```js
212
+ * const peerIds = libp2p.pubsub.getSubscribers(topic)
213
+ * ```
214
+ */
215
+ getSubscribers: (topic: string) => PeerId[];
216
+ /**
217
+ * Publishes messages to the given topic.
218
+ *
219
+ * @example
220
+ *
221
+ * ```js
222
+ * const topic = 'topic'
223
+ * const data = uint8ArrayFromString('data')
224
+ *
225
+ * await libp2p.pubsub.publish(topic, data)
226
+ * ```
227
+ */
228
+ publish: (topic: string, data: Uint8Array) => Promise<PublishResult>;
229
+ }
230
+ export interface PeerStreamEvents {
231
+ 'stream:inbound': CustomEvent<never>;
232
+ 'stream:outbound': CustomEvent<never>;
233
+ 'close': CustomEvent<never>;
234
+ }
235
+ export {};
236
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/pubsub/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,eAAe,CAAA;AAEtC;;;;;;;;;GASG;AACH,eAAO,MAAM,YAAY,iBAAiB,CAAA;AAE1C,MAAM,MAAM,eAAe,GAAG,OAAO,UAAU,GAAG,OAAO,YAAY,CAAA;AAErE,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,QAAQ,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,UAAU,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,UAAU,CAAA;IACrB,GAAG,EAAE,UAAU,CAAA;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,UAAU,CAAA;IAChB,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,UAAU,CAAA;CACjB;AAED,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,eAAe,CAAA;AAErD,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,UAAU,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,UAAU,CAAA;IACjB,cAAc,CAAC,EAAE,UAAU,CAAA;IAC3B,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,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IAEpC;;;;;;;;OAQG;IACH,cAAc,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,EAAE,CAAA;IAE3C;;;;;;;;;;;OAWG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,OAAO,CAAC,aAAa,CAAC,CAAA;CACrE;AAED,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;IACpC,iBAAiB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;IACrC,OAAO,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;CAC5B"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * On the producing side:
3
+ * * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
4
+ *
5
+ * On the consuming side:
6
+ * * Enforce the fields to be present, reject otherwise.
7
+ * * Propagate only if the fields are valid and signature can be verified, reject otherwise.
8
+ */
9
+ export const StrictSign = 'StrictSign';
10
+ /**
11
+ * On the producing side:
12
+ * * Build messages without the signature, key, from and seqno fields.
13
+ * * The corresponding protobuf key-value pairs are absent from the marshalled message, not just empty.
14
+ *
15
+ * On the consuming side:
16
+ * * Enforce the fields to be absent, reject otherwise.
17
+ * * Propagate only if the fields are absent, reject otherwise.
18
+ * * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
19
+ */
20
+ export const StrictNoSign = 'StrictNoSign';
21
+ export var TopicValidatorResult;
22
+ (function (TopicValidatorResult) {
23
+ /**
24
+ * The message is considered valid, and it should be delivered and forwarded to the network
25
+ */
26
+ TopicValidatorResult["Accept"] = "accept";
27
+ /**
28
+ * The message is neither delivered nor forwarded to the network
29
+ */
30
+ TopicValidatorResult["Ignore"] = "ignore";
31
+ /**
32
+ * The message is considered invalid, and it should be rejected
33
+ */
34
+ TopicValidatorResult["Reject"] = "reject";
35
+ })(TopicValidatorResult || (TopicValidatorResult = {}));
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/pubsub/index.ts"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,CAAA;AAEtC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAA;AA6G1C,MAAM,CAAN,IAAY,oBAaX;AAbD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,yCAAiB,CAAA;IACjB;;OAEG;IACH,yCAAiB,CAAA;IACjB;;OAEG;IACH,yCAAiB,CAAA;AACnB,CAAC,EAbW,oBAAoB,KAApB,oBAAoB,QAa/B"}
@@ -1,5 +1,5 @@
1
1
  import { Uint8ArrayList } from 'uint8arraylist';
2
- import type { Direction, Stream, StreamStat } from '../connection/index.js';
2
+ import type { Direction, Stream, StreamTimeline } from '../connection/index.js';
3
3
  import type { Source } from 'it-stream-types';
4
4
  export interface AbstractStreamInit {
5
5
  /**
@@ -26,7 +26,9 @@ export interface AbstractStreamInit {
26
26
  }
27
27
  export declare abstract class AbstractStream implements Stream {
28
28
  id: string;
29
- stat: StreamStat;
29
+ direction: Direction;
30
+ timeline: StreamTimeline;
31
+ protocol?: string;
30
32
  metadata: Record<string, unknown>;
31
33
  source: AsyncGenerator<Uint8ArrayList, void, unknown>;
32
34
  private readonly abortController;
@@ -1 +1 @@
1
- {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AAC3E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAa7C,MAAM,WAAW,kBAAkB;IACjC;;OAEG;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AK,MAAM;IAI/B;;;OAGG;IACH,QAAQ,CAAC,aAAa,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE/C;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAE,GAAG,EAAE,cAAc,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE7D;;OAEG;IACH,QAAQ,CAAC,SAAS,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE3C;;;OAGG;IACH,QAAQ,CAAC,cAAc,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEhD;;;OAGG;IACH,QAAQ,CAAC,aAAa,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CAChD"}
@@ -17,7 +17,9 @@ function isPromise(res) {
17
17
  }
18
18
  export class AbstractStream {
19
19
  id;
20
- stat;
20
+ direction;
21
+ timeline;
22
+ protocol;
21
23
  metadata;
22
24
  source;
23
25
  abortController;
@@ -39,18 +41,16 @@ export class AbstractStream {
39
41
  this.sinkSunk = false;
40
42
  this.id = init.id;
41
43
  this.metadata = init.metadata ?? {};
42
- this.stat = {
43
- direction: init.direction,
44
- timeline: {
45
- open: Date.now()
46
- }
44
+ this.direction = init.direction;
45
+ this.timeline = {
46
+ open: Date.now()
47
47
  };
48
48
  this.maxDataSize = init.maxDataSize;
49
49
  this.onEnd = init.onEnd;
50
50
  this.source = this.streamSource = pushable({
51
51
  onEnd: () => {
52
52
  // already sent a reset message
53
- if (this.stat.timeline.reset !== null) {
53
+ if (this.timeline.reset !== null) {
54
54
  const res = this.sendCloseRead();
55
55
  if (isPromise(res)) {
56
56
  res.catch(err => {
@@ -68,14 +68,14 @@ export class AbstractStream {
68
68
  if (this.sourceEnded) {
69
69
  return;
70
70
  }
71
- this.stat.timeline.closeRead = Date.now();
71
+ this.timeline.closeRead = Date.now();
72
72
  this.sourceEnded = true;
73
- log.trace('%s stream %s source end - err: %o', this.stat.direction, this.id, err);
73
+ log.trace('%s stream %s source end - err: %o', this.direction, this.id, err);
74
74
  if (err != null && this.endErr == null) {
75
75
  this.endErr = err;
76
76
  }
77
77
  if (this.sinkEnded) {
78
- this.stat.timeline.close = Date.now();
78
+ this.timeline.close = Date.now();
79
79
  if (this.onEnd != null) {
80
80
  this.onEnd(this.endErr);
81
81
  }
@@ -85,14 +85,14 @@ export class AbstractStream {
85
85
  if (this.sinkEnded) {
86
86
  return;
87
87
  }
88
- this.stat.timeline.closeWrite = Date.now();
88
+ this.timeline.closeWrite = Date.now();
89
89
  this.sinkEnded = true;
90
- log.trace('%s stream %s sink end - err: %o', this.stat.direction, this.id, err);
90
+ log.trace('%s stream %s sink end - err: %o', this.direction, this.id, err);
91
91
  if (err != null && this.endErr == null) {
92
92
  this.endErr = err;
93
93
  }
94
94
  if (this.sourceEnded) {
95
- this.stat.timeline.close = Date.now();
95
+ this.timeline.close = Date.now();
96
96
  if (this.onEnd != null) {
97
97
  this.onEnd(this.endErr);
98
98
  }
@@ -100,13 +100,13 @@ export class AbstractStream {
100
100
  }
101
101
  // Close for both Reading and Writing
102
102
  close() {
103
- log.trace('%s stream %s close', this.stat.direction, this.id);
103
+ log.trace('%s stream %s close', this.direction, this.id);
104
104
  this.closeRead();
105
105
  this.closeWrite();
106
106
  }
107
107
  // Close for reading
108
108
  closeRead() {
109
- log.trace('%s stream %s closeRead', this.stat.direction, this.id);
109
+ log.trace('%s stream %s closeRead', this.direction, this.id);
110
110
  if (this.sourceEnded) {
111
111
  return;
112
112
  }
@@ -114,7 +114,7 @@ export class AbstractStream {
114
114
  }
115
115
  // Close for writing
116
116
  closeWrite() {
117
- log.trace('%s stream %s closeWrite', this.stat.direction, this.id);
117
+ log.trace('%s stream %s closeWrite', this.direction, this.id);
118
118
  if (this.sinkEnded) {
119
119
  return;
120
120
  }
@@ -130,13 +130,13 @@ export class AbstractStream {
130
130
  }
131
131
  }
132
132
  catch (err) {
133
- log.trace('%s stream %s error sending close', this.stat.direction, this.id, err);
133
+ log.trace('%s stream %s error sending close', this.direction, this.id, err);
134
134
  }
135
135
  this.onSinkEnd();
136
136
  }
137
137
  // Close for reading and writing (local error)
138
138
  abort(err) {
139
- log.trace('%s stream %s abort', this.stat.direction, this.id, err);
139
+ log.trace('%s stream %s abort', this.direction, this.id, err);
140
140
  // End the source with the passed error
141
141
  this.streamSource.end(err);
142
142
  this.abortController.abort();
@@ -164,7 +164,7 @@ export class AbstractStream {
164
164
  ]);
165
165
  try {
166
166
  source = abortableSource(source, signal);
167
- if (this.stat.direction === 'outbound') { // If initiator, open a new stream
167
+ if (this.direction === 'outbound') { // If initiator, open a new stream
168
168
  const res = this.sendNewStream();
169
169
  if (isPromise(res)) {
170
170
  await res;
@@ -204,19 +204,19 @@ export class AbstractStream {
204
204
  }
205
205
  // Send no more data if this stream was remotely reset
206
206
  if (err.code === ERR_STREAM_RESET) {
207
- log.trace('%s stream %s reset', this.stat.direction, this.id);
207
+ log.trace('%s stream %s reset', this.direction, this.id);
208
208
  }
209
209
  else {
210
- log.trace('%s stream %s error', this.stat.direction, this.id, err);
210
+ log.trace('%s stream %s error', this.direction, this.id, err);
211
211
  try {
212
212
  const res = this.sendReset();
213
213
  if (isPromise(res)) {
214
214
  await res;
215
215
  }
216
- this.stat.timeline.reset = Date.now();
216
+ this.timeline.reset = Date.now();
217
217
  }
218
218
  catch (err) {
219
- log.trace('%s stream %s error sending reset', this.stat.direction, this.id, err);
219
+ log.trace('%s stream %s error sending reset', this.direction, this.id, err);
220
220
  }
221
221
  }
222
222
  this.streamSource.end(err);
@@ -233,7 +233,7 @@ export class AbstractStream {
233
233
  }
234
234
  }
235
235
  catch (err) {
236
- log.trace('%s stream %s error sending close', this.stat.direction, this.id, err);
236
+ log.trace('%s stream %s error sending close', this.direction, this.id, err);
237
237
  }
238
238
  this.onSinkEnd();
239
239
  }
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,OAAO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;iBAC5E;aACF;YAED,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YAC1B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;YAEnB,MAAM,GAAG,CAAA;SACV;gBAAS;YACR,MAAM,CAAC,KAAK,EAAE,CAAA;SACf;QAED,IAAI;YACF,MAAM,GAAG,GAAG,IAAI,CAAC,cAAc,EAAE,CAAA;YAEjC,IAAI,SAAS,CAAC,GAAG,CAAC,EAAE;gBAClB,MAAM,GAAG,CAAA;aACV;SACF;QAAC,OAAO,GAAG,EAAE;YACZ,GAAG,CAAC,KAAK,CAAC,kCAAkC,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,CAAA;SAC5E;QAED,IAAI,CAAC,SAAS,EAAE,CAAA;IAClB,CAAC;IAED;;;OAGG;IACH,UAAU,CAAE,IAAoB;QAC9B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;IAED;;;OAGG;IACH,oBAAoB;QAClB,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,CAAA;IACzC,CAAC;CA6BF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/interface",
3
- "version": "0.0.1-8b0e6bef",
3
+ "version": "0.0.1-e9cafd3d",
4
4
  "description": "The interface implemented by a libp2p node",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/interface#readme",
@@ -112,6 +112,10 @@
112
112
  "types": "./dist/src/peer-store/tags.d.ts",
113
113
  "import": "./dist/src/peer-store/tags.js"
114
114
  },
115
+ "./pubsub": {
116
+ "types": "./dist/src/pubsub/index.d.ts",
117
+ "import": "./dist/src/pubsub/index.js"
118
+ },
115
119
  "./record": {
116
120
  "types": "./dist/src/record/index.d.ts",
117
121
  "import": "./dist/src/record/index.js"
@@ -167,7 +171,7 @@
167
171
  },
168
172
  "devDependencies": {
169
173
  "@types/sinon": "^10.0.15",
170
- "aegir": "^39.0.10",
174
+ "aegir": "^39.0.13",
171
175
  "sinon": "^15.1.2",
172
176
  "sinon-ts": "^1.0.0"
173
177
  },
@@ -12,37 +12,10 @@ export interface ConnectionTimeline {
12
12
  }
13
13
 
14
14
  /**
15
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
15
+ * Outbound connections are opened by the local node, inbound streams are opened by the remote
16
16
  */
17
17
  export type Direction = 'inbound' | 'outbound'
18
18
 
19
- export interface ConnectionStat {
20
- /**
21
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
22
- */
23
- direction: Direction
24
-
25
- /**
26
- * Lifecycle times for the connection
27
- */
28
- timeline: ConnectionTimeline
29
-
30
- /**
31
- * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
32
- */
33
- multiplexer?: string
34
-
35
- /**
36
- * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
37
- */
38
- encryption?: string
39
-
40
- /**
41
- * The current status of the connection
42
- */
43
- status: keyof typeof Status
44
- }
45
-
46
19
  export interface StreamTimeline {
47
20
  /**
48
21
  * A timestamp of when the stream was opened
@@ -70,23 +43,6 @@ export interface StreamTimeline {
70
43
  reset?: number
71
44
  }
72
45
 
73
- export interface StreamStat {
74
- /**
75
- * Outbound streams are opened by the local node, inbound streams are opened by the remote
76
- */
77
- direction: Direction
78
-
79
- /**
80
- * Lifecycle times for the stream
81
- */
82
- timeline: StreamTimeline
83
-
84
- /**
85
- * Once a protocol has been negotiated for this stream, it will be set on the stat object
86
- */
87
- protocol?: string
88
- }
89
-
90
46
  /**
91
47
  * A Stream is a data channel between two peers that
92
48
  * can be written to and read from at both ends.
@@ -148,9 +104,19 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
148
104
  id: string
149
105
 
150
106
  /**
151
- * Stats about this stream
107
+ * Outbound streams are opened by the local node, inbound streams are opened by the remote
152
108
  */
153
- stat: StreamStat
109
+ direction: Direction
110
+
111
+ /**
112
+ * Lifecycle times for the stream
113
+ */
114
+ timeline: StreamTimeline
115
+
116
+ /**
117
+ * Once a protocol has been negotiated for this stream, it will be set on the stat object
118
+ */
119
+ protocol?: string
154
120
 
155
121
  /**
156
122
  * User defined stream metadata
@@ -174,13 +140,56 @@ export interface NewStreamOptions extends AbortOptions {
174
140
  * between which the connection is made.
175
141
  */
176
142
  export interface Connection {
143
+ /**
144
+ * The unique identifier for this connection
145
+ */
177
146
  id: string
178
- stat: ConnectionStat
147
+
148
+ /**
149
+ * The address of the remote end of the connection
150
+ */
179
151
  remoteAddr: Multiaddr
152
+
153
+ /**
154
+ * The id of the peer at the remote end of the connection
155
+ */
180
156
  remotePeer: PeerId
157
+
158
+ /**
159
+ * A list of tags applied to this connection
160
+ */
181
161
  tags: string[]
162
+
163
+ /**
164
+ * A list of open streams on this connection
165
+ */
182
166
  streams: Stream[]
183
167
 
168
+ /**
169
+ * Outbound conections are opened by the local node, inbound streams are opened by the remote
170
+ */
171
+ direction: Direction
172
+
173
+ /**
174
+ * Lifecycle times for the connection
175
+ */
176
+ timeline: ConnectionTimeline
177
+
178
+ /**
179
+ * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
180
+ */
181
+ multiplexer?: string
182
+
183
+ /**
184
+ * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
185
+ */
186
+ encryption?: string
187
+
188
+ /**
189
+ * The current status of the connection
190
+ */
191
+ status: keyof typeof Status
192
+
184
193
  newStream: (multicodecs: string | string[], options?: NewStreamOptions) => Promise<Stream>
185
194
  addStream: (stream: Stream) => void
186
195
  removeStream: (id: string) => void
@@ -0,0 +1,269 @@
1
+ import type { Stream } from '../connection/index.js'
2
+ import type { EventEmitter } from '../events.js'
3
+ import type { PeerId } from '../peer-id/index.js'
4
+ import type { Pushable } from 'it-pushable'
5
+ import type { Uint8ArrayList } from 'uint8arraylist'
6
+
7
+ /**
8
+ * On the producing side:
9
+ * * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
10
+ *
11
+ * On the consuming side:
12
+ * * Enforce the fields to be present, reject otherwise.
13
+ * * Propagate only if the fields are valid and signature can be verified, reject otherwise.
14
+ */
15
+ export const StrictSign = 'StrictSign'
16
+
17
+ /**
18
+ * On the producing side:
19
+ * * Build messages without the signature, key, from and seqno fields.
20
+ * * The corresponding protobuf key-value pairs are absent from the marshalled message, not just empty.
21
+ *
22
+ * On the consuming side:
23
+ * * Enforce the fields to be absent, reject otherwise.
24
+ * * Propagate only if the fields are absent, reject otherwise.
25
+ * * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
26
+ */
27
+ export const StrictNoSign = 'StrictNoSign'
28
+
29
+ export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign
30
+
31
+ export interface SignedMessage {
32
+ type: 'signed'
33
+ from: PeerId
34
+ topic: string
35
+ data: Uint8Array
36
+ sequenceNumber: bigint
37
+ signature: Uint8Array
38
+ key: Uint8Array
39
+ }
40
+
41
+ export interface UnsignedMessage {
42
+ type: 'unsigned'
43
+ topic: string
44
+ data: Uint8Array
45
+ }
46
+
47
+ export type Message = SignedMessage | UnsignedMessage
48
+
49
+ export interface PubSubRPCMessage {
50
+ from?: Uint8Array
51
+ topic?: string
52
+ data?: Uint8Array
53
+ sequenceNumber?: Uint8Array
54
+ signature?: Uint8Array
55
+ key?: Uint8Array
56
+ }
57
+
58
+ export interface PubSubRPCSubscription {
59
+ subscribe?: boolean
60
+ topic?: string
61
+ }
62
+
63
+ export interface PubSubRPC {
64
+ subscriptions: PubSubRPCSubscription[]
65
+ messages: PubSubRPCMessage[]
66
+ }
67
+
68
+ export interface PeerStreams extends EventEmitter<PeerStreamEvents> {
69
+ id: PeerId
70
+ protocol: string
71
+ outboundStream?: Pushable<Uint8ArrayList>
72
+ inboundStream?: AsyncIterable<Uint8ArrayList>
73
+ isWritable: boolean
74
+
75
+ close: () => void
76
+ write: (buf: Uint8Array | Uint8ArrayList) => void
77
+ attachInboundStream: (stream: Stream) => AsyncIterable<Uint8ArrayList>
78
+ attachOutboundStream: (stream: Stream) => Promise<Pushable<Uint8ArrayList>>
79
+ }
80
+
81
+ export interface PubSubInit {
82
+ enabled?: boolean
83
+
84
+ multicodecs?: string[]
85
+
86
+ /**
87
+ * defines how signatures should be handled
88
+ */
89
+ globalSignaturePolicy?: SignaturePolicy
90
+
91
+ /**
92
+ * if can relay messages not subscribed
93
+ */
94
+ canRelayMessage?: boolean
95
+
96
+ /**
97
+ * if publish should emit to self, if subscribed
98
+ */
99
+ emitSelf?: boolean
100
+
101
+ /**
102
+ * handle this many incoming pubsub messages concurrently
103
+ */
104
+ messageProcessingConcurrency?: number
105
+
106
+ /**
107
+ * How many parallel incoming streams to allow on the pubsub protocol per-connection
108
+ */
109
+ maxInboundStreams?: number
110
+
111
+ /**
112
+ * How many parallel outgoing streams to allow on the pubsub protocol per-connection
113
+ */
114
+ maxOutboundStreams?: number
115
+ }
116
+
117
+ interface Subscription {
118
+ topic: string
119
+ subscribe: boolean
120
+ }
121
+
122
+ export interface SubscriptionChangeData {
123
+ peerId: PeerId
124
+ subscriptions: Subscription[]
125
+ }
126
+
127
+ export interface PubSubEvents {
128
+ 'subscription-change': CustomEvent<SubscriptionChangeData>
129
+ 'message': CustomEvent<Message>
130
+ }
131
+
132
+ export interface PublishResult {
133
+ recipients: PeerId[]
134
+ }
135
+
136
+ export enum TopicValidatorResult {
137
+ /**
138
+ * The message is considered valid, and it should be delivered and forwarded to the network
139
+ */
140
+ Accept = 'accept',
141
+ /**
142
+ * The message is neither delivered nor forwarded to the network
143
+ */
144
+ Ignore = 'ignore',
145
+ /**
146
+ * The message is considered invalid, and it should be rejected
147
+ */
148
+ Reject = 'reject'
149
+ }
150
+
151
+ export interface TopicValidatorFn {
152
+ (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
153
+ }
154
+
155
+ export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> {
156
+ /**
157
+ * The global signature policy controls whether or not we sill send and receive
158
+ * signed or unsigned messages.
159
+ *
160
+ * Signed messages prevent spoofing message senders and should be preferred to
161
+ * using unsigned messages.
162
+ */
163
+ globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
164
+
165
+ /**
166
+ * A list of multicodecs that contain the pubsub protocol name.
167
+ */
168
+ multicodecs: string[]
169
+
170
+ /**
171
+ * Pubsub routers support message validators per topic, which will validate the message
172
+ * before its propagations. They are stored in a map where keys are the topic name and
173
+ * values are the validators.
174
+ *
175
+ * @example
176
+ *
177
+ * ```js
178
+ * const topic = 'topic'
179
+ * const validateMessage = (msgTopic, msg) => {
180
+ * const input = uint8ArrayToString(msg.data)
181
+ * const validInputs = ['a', 'b', 'c']
182
+ *
183
+ * if (!validInputs.includes(input)) {
184
+ * throw new Error('no valid input received')
185
+ * }
186
+ * }
187
+ * libp2p.pubsub.topicValidators.set(topic, validateMessage)
188
+ * ```
189
+ */
190
+ topicValidators: Map<string, TopicValidatorFn>
191
+
192
+ getPeers: () => PeerId[]
193
+
194
+ /**
195
+ * Gets a list of topics the node is subscribed to.
196
+ *
197
+ * ```js
198
+ * const topics = libp2p.pubsub.getTopics()
199
+ * ```
200
+ */
201
+ getTopics: () => string[]
202
+
203
+ /**
204
+ * Subscribes to a pubsub topic.
205
+ *
206
+ * @example
207
+ *
208
+ * ```js
209
+ * const topic = 'topic'
210
+ * const handler = (msg) => {
211
+ * if (msg.topic === topic) {
212
+ * // msg.data - pubsub data received
213
+ * }
214
+ * }
215
+ *
216
+ * libp2p.pubsub.addEventListener('message', handler)
217
+ * libp2p.pubsub.subscribe(topic)
218
+ * ```
219
+ */
220
+ subscribe: (topic: string) => void
221
+
222
+ /**
223
+ * Unsubscribes from a pubsub topic.
224
+ *
225
+ * @example
226
+ *
227
+ * ```js
228
+ * const topic = 'topic'
229
+ * const handler = (msg) => {
230
+ * // msg.data - pubsub data received
231
+ * }
232
+ *
233
+ * libp2p.pubsub.removeEventListener(topic handler)
234
+ * libp2p.pubsub.unsubscribe(topic)
235
+ * ```
236
+ */
237
+ unsubscribe: (topic: string) => void
238
+
239
+ /**
240
+ * Gets a list of the PeerIds that are subscribed to one topic.
241
+ *
242
+ * @example
243
+ *
244
+ * ```js
245
+ * const peerIds = libp2p.pubsub.getSubscribers(topic)
246
+ * ```
247
+ */
248
+ getSubscribers: (topic: string) => PeerId[]
249
+
250
+ /**
251
+ * Publishes messages to the given topic.
252
+ *
253
+ * @example
254
+ *
255
+ * ```js
256
+ * const topic = 'topic'
257
+ * const data = uint8ArrayFromString('data')
258
+ *
259
+ * await libp2p.pubsub.publish(topic, data)
260
+ * ```
261
+ */
262
+ publish: (topic: string, data: Uint8Array) => Promise<PublishResult>
263
+ }
264
+
265
+ export interface PeerStreamEvents {
266
+ 'stream:inbound': CustomEvent<never>
267
+ 'stream:outbound': CustomEvent<never>
268
+ 'close': CustomEvent<never>
269
+ }
@@ -4,7 +4,7 @@ import { anySignal } from 'any-signal'
4
4
  import { type Pushable, pushable } from 'it-pushable'
5
5
  import { Uint8ArrayList } from 'uint8arraylist'
6
6
  import { CodeError } from '../errors.js'
7
- import type { Direction, Stream, StreamStat } from '../connection/index.js'
7
+ import type { Direction, Stream, StreamTimeline } from '../connection/index.js'
8
8
  import type { Source } from 'it-stream-types'
9
9
 
10
10
  // const log = logger('libp2p:stream')
@@ -52,7 +52,9 @@ function isPromise (res?: any): res is Promise<void> {
52
52
 
53
53
  export abstract class AbstractStream implements Stream {
54
54
  public id: string
55
- public stat: StreamStat
55
+ public direction: Direction
56
+ public timeline: StreamTimeline
57
+ public protocol?: string
56
58
  public metadata: Record<string, unknown>
57
59
  public source: AsyncGenerator<Uint8ArrayList, void, unknown>
58
60
 
@@ -77,11 +79,9 @@ export abstract class AbstractStream implements Stream {
77
79
 
78
80
  this.id = init.id
79
81
  this.metadata = init.metadata ?? {}
80
- this.stat = {
81
- direction: init.direction,
82
- timeline: {
83
- open: Date.now()
84
- }
82
+ this.direction = init.direction
83
+ this.timeline = {
84
+ open: Date.now()
85
85
  }
86
86
  this.maxDataSize = init.maxDataSize
87
87
  this.onEnd = init.onEnd
@@ -89,7 +89,7 @@ export abstract class AbstractStream implements Stream {
89
89
  this.source = this.streamSource = pushable<Uint8ArrayList>({
90
90
  onEnd: () => {
91
91
  // already sent a reset message
92
- if (this.stat.timeline.reset !== null) {
92
+ if (this.timeline.reset !== null) {
93
93
  const res = this.sendCloseRead()
94
94
 
95
95
  if (isPromise(res)) {
@@ -112,16 +112,16 @@ export abstract class AbstractStream implements Stream {
112
112
  return
113
113
  }
114
114
 
115
- this.stat.timeline.closeRead = Date.now()
115
+ this.timeline.closeRead = Date.now()
116
116
  this.sourceEnded = true
117
- log.trace('%s stream %s source end - err: %o', this.stat.direction, this.id, err)
117
+ log.trace('%s stream %s source end - err: %o', this.direction, this.id, err)
118
118
 
119
119
  if (err != null && this.endErr == null) {
120
120
  this.endErr = err
121
121
  }
122
122
 
123
123
  if (this.sinkEnded) {
124
- this.stat.timeline.close = Date.now()
124
+ this.timeline.close = Date.now()
125
125
 
126
126
  if (this.onEnd != null) {
127
127
  this.onEnd(this.endErr)
@@ -134,16 +134,16 @@ export abstract class AbstractStream implements Stream {
134
134
  return
135
135
  }
136
136
 
137
- this.stat.timeline.closeWrite = Date.now()
137
+ this.timeline.closeWrite = Date.now()
138
138
  this.sinkEnded = true
139
- log.trace('%s stream %s sink end - err: %o', this.stat.direction, this.id, err)
139
+ log.trace('%s stream %s sink end - err: %o', this.direction, this.id, err)
140
140
 
141
141
  if (err != null && this.endErr == null) {
142
142
  this.endErr = err
143
143
  }
144
144
 
145
145
  if (this.sourceEnded) {
146
- this.stat.timeline.close = Date.now()
146
+ this.timeline.close = Date.now()
147
147
 
148
148
  if (this.onEnd != null) {
149
149
  this.onEnd(this.endErr)
@@ -153,7 +153,7 @@ export abstract class AbstractStream implements Stream {
153
153
 
154
154
  // Close for both Reading and Writing
155
155
  close (): void {
156
- log.trace('%s stream %s close', this.stat.direction, this.id)
156
+ log.trace('%s stream %s close', this.direction, this.id)
157
157
 
158
158
  this.closeRead()
159
159
  this.closeWrite()
@@ -161,7 +161,7 @@ export abstract class AbstractStream implements Stream {
161
161
 
162
162
  // Close for reading
163
163
  closeRead (): void {
164
- log.trace('%s stream %s closeRead', this.stat.direction, this.id)
164
+ log.trace('%s stream %s closeRead', this.direction, this.id)
165
165
 
166
166
  if (this.sourceEnded) {
167
167
  return
@@ -172,7 +172,7 @@ export abstract class AbstractStream implements Stream {
172
172
 
173
173
  // Close for writing
174
174
  closeWrite (): void {
175
- log.trace('%s stream %s closeWrite', this.stat.direction, this.id)
175
+ log.trace('%s stream %s closeWrite', this.direction, this.id)
176
176
 
177
177
  if (this.sinkEnded) {
178
178
  return
@@ -191,7 +191,7 @@ export abstract class AbstractStream implements Stream {
191
191
  })
192
192
  }
193
193
  } catch (err) {
194
- log.trace('%s stream %s error sending close', this.stat.direction, this.id, err)
194
+ log.trace('%s stream %s error sending close', this.direction, this.id, err)
195
195
  }
196
196
 
197
197
  this.onSinkEnd()
@@ -199,7 +199,7 @@ export abstract class AbstractStream implements Stream {
199
199
 
200
200
  // Close for reading and writing (local error)
201
201
  abort (err: Error): void {
202
- log.trace('%s stream %s abort', this.stat.direction, this.id, err)
202
+ log.trace('%s stream %s abort', this.direction, this.id, err)
203
203
  // End the source with the passed error
204
204
  this.streamSource.end(err)
205
205
  this.abortController.abort()
@@ -234,7 +234,7 @@ export abstract class AbstractStream implements Stream {
234
234
  try {
235
235
  source = abortableSource(source, signal)
236
236
 
237
- if (this.stat.direction === 'outbound') { // If initiator, open a new stream
237
+ if (this.direction === 'outbound') { // If initiator, open a new stream
238
238
  const res = this.sendNewStream()
239
239
 
240
240
  if (isPromise(res)) {
@@ -282,9 +282,9 @@ export abstract class AbstractStream implements Stream {
282
282
 
283
283
  // Send no more data if this stream was remotely reset
284
284
  if (err.code === ERR_STREAM_RESET) {
285
- log.trace('%s stream %s reset', this.stat.direction, this.id)
285
+ log.trace('%s stream %s reset', this.direction, this.id)
286
286
  } else {
287
- log.trace('%s stream %s error', this.stat.direction, this.id, err)
287
+ log.trace('%s stream %s error', this.direction, this.id, err)
288
288
  try {
289
289
  const res = this.sendReset()
290
290
 
@@ -292,9 +292,9 @@ export abstract class AbstractStream implements Stream {
292
292
  await res
293
293
  }
294
294
 
295
- this.stat.timeline.reset = Date.now()
295
+ this.timeline.reset = Date.now()
296
296
  } catch (err) {
297
- log.trace('%s stream %s error sending reset', this.stat.direction, this.id, err)
297
+ log.trace('%s stream %s error sending reset', this.direction, this.id, err)
298
298
  }
299
299
  }
300
300
 
@@ -313,7 +313,7 @@ export abstract class AbstractStream implements Stream {
313
313
  await res
314
314
  }
315
315
  } catch (err) {
316
- log.trace('%s stream %s error sending close', this.stat.direction, this.id, err)
316
+ log.trace('%s stream %s error sending close', this.direction, this.id, err)
317
317
  }
318
318
 
319
319
  this.onSinkEnd()