@libp2p/interface 0.1.6 → 1.0.0-7682861f9
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.
- package/README.md +15 -1
- package/dist/index.min.js +1 -1
- package/dist/src/connection/index.d.ts +15 -3
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +2 -2
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-encrypter/index.d.ts +7 -5
- package/dist/src/connection-encrypter/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.d.ts +1 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/errors.d.ts +20 -5
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +20 -8
- package/dist/src/errors.js.map +1 -1
- package/dist/src/events.d.ts +0 -1
- package/dist/src/events.d.ts.map +1 -1
- package/dist/src/events.js +0 -2
- package/dist/src/events.js.map +1 -1
- package/dist/src/index.d.ts +34 -14
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +21 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/keys/index.d.ts +5 -4
- package/dist/src/keys/index.d.ts.map +1 -1
- package/dist/src/keys/index.js.map +1 -1
- package/dist/src/peer-discovery/index.d.ts +1 -1
- package/dist/src/peer-discovery/index.d.ts.map +1 -1
- package/dist/src/peer-discovery/index.js +1 -1
- package/dist/src/peer-discovery/index.js.map +1 -1
- package/dist/src/peer-id/index.d.ts +1 -1
- package/dist/src/peer-id/index.d.ts.map +1 -1
- package/dist/src/peer-id/index.js +2 -2
- package/dist/src/peer-id/index.js.map +1 -1
- package/dist/src/peer-info/index.d.ts +12 -1
- package/dist/src/peer-info/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.d.ts +1 -1
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +1 -1
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/startable.d.ts +8 -1
- package/dist/src/startable.d.ts.map +1 -1
- package/dist/src/startable.js +8 -0
- package/dist/src/startable.js.map +1 -1
- package/dist/src/stream-muxer/index.d.ts +2 -2
- package/dist/src/stream-muxer/index.d.ts.map +1 -1
- package/dist/src/stream-muxer/stream.d.ts +2 -149
- package/dist/src/stream-muxer/stream.d.ts.map +1 -1
- package/dist/src/stream-muxer/stream.js +8 -322
- package/dist/src/stream-muxer/stream.js.map +1 -1
- package/dist/src/topology/index.d.ts +0 -2
- package/dist/src/topology/index.d.ts.map +1 -1
- package/dist/src/transport/index.d.ts +2 -2
- package/dist/src/transport/index.d.ts.map +1 -1
- package/dist/src/transport/index.js +2 -2
- package/dist/src/transport/index.js.map +1 -1
- package/package.json +15 -27
- package/src/connection/index.ts +19 -4
- package/src/connection-encrypter/index.ts +7 -5
- package/src/content-routing/index.ts +1 -1
- package/src/errors.ts +21 -8
- package/src/events.ts +0 -3
- package/src/index.ts +37 -15
- package/src/keys/index.ts +6 -4
- package/src/peer-discovery/index.ts +1 -1
- package/src/peer-id/index.ts +2 -2
- package/src/peer-info/index.ts +13 -1
- package/src/peer-routing/index.ts +1 -1
- package/src/startable.ts +8 -2
- package/src/stream-muxer/index.ts +2 -2
- package/src/stream-muxer/stream.ts +9 -494
- package/src/topology/index.ts +0 -3
- package/src/transport/index.ts +3 -3
- package/dist/src/keychain/index.d.ts +0 -154
- package/dist/src/keychain/index.d.ts.map +0 -1
- package/dist/src/keychain/index.js +0 -23
- package/dist/src/keychain/index.js.map +0 -1
- package/dist/src/metrics/tracked-map.d.ts +0 -17
- package/dist/src/metrics/tracked-map.d.ts.map +0 -1
- package/dist/src/metrics/tracked-map.js +0 -38
- package/dist/src/metrics/tracked-map.js.map +0 -1
- package/dist/typedoc-urls.json +0 -268
- package/src/keychain/index.ts +0 -167
- package/src/metrics/tracked-map.ts +0 -65
|
@@ -1,496 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import type { AbortOptions } from '../index.js'
|
|
9
|
-
import type { Source } from 'it-stream-types'
|
|
1
|
+
// TODO: remove this file and the dep-check override in package.json for
|
|
2
|
+
// @libp2p/utils when yamux is updated.
|
|
3
|
+
// This is a hack to defeat TypeScript trying to inspect the types for
|
|
4
|
+
// @libp2p/utils/abstract-stream which depends on this module - making the
|
|
5
|
+
// import path only resolvable at runtime breaks the transpile time circular
|
|
6
|
+
// dependency
|
|
7
|
+
const s = await import('@libp2p/utils' + '/abstract-stream')
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
interface Logger {
|
|
13
|
-
(formatter: any, ...args: any[]): void
|
|
14
|
-
error(formatter: any, ...args: any[]): void
|
|
15
|
-
trace(formatter: any, ...args: any[]): void
|
|
16
|
-
enabled: boolean
|
|
17
|
-
}
|
|
9
|
+
export type AbstractStreamInit = any
|
|
18
10
|
|
|
19
|
-
const
|
|
20
|
-
const ERR_SINK_INVALID_STATE = 'ERR_SINK_INVALID_STATE'
|
|
21
|
-
const DEFAULT_SEND_CLOSE_WRITE_TIMEOUT = 5000
|
|
22
|
-
|
|
23
|
-
export interface AbstractStreamInit {
|
|
24
|
-
/**
|
|
25
|
-
* A unique identifier for this stream
|
|
26
|
-
*/
|
|
27
|
-
id: string
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The stream direction
|
|
31
|
-
*/
|
|
32
|
-
direction: Direction
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* A Logger implementation used to log stream-specific information
|
|
36
|
-
*/
|
|
37
|
-
log: Logger
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* User specific stream metadata
|
|
41
|
-
*/
|
|
42
|
-
metadata?: Record<string, unknown>
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Invoked when the stream ends
|
|
46
|
-
*/
|
|
47
|
-
onEnd?(err?: Error | undefined): void
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Invoked when the readable end of the stream is closed
|
|
51
|
-
*/
|
|
52
|
-
onCloseRead?(): void
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Invoked when the writable end of the stream is closed
|
|
56
|
-
*/
|
|
57
|
-
onCloseWrite?(): void
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Invoked when the the stream has been reset by the remote
|
|
61
|
-
*/
|
|
62
|
-
onReset?(): void
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Invoked when the the stream has errored
|
|
66
|
-
*/
|
|
67
|
-
onAbort?(err: Error): void
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* How long to wait in ms for stream data to be written to the underlying
|
|
71
|
-
* connection when closing the writable end of the stream. (default: 500)
|
|
72
|
-
*/
|
|
73
|
-
closeTimeout?: number
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* After the stream sink has closed, a limit on how long it takes to send
|
|
77
|
-
* a close-write message to the remote peer.
|
|
78
|
-
*/
|
|
79
|
-
sendCloseWriteTimeout?: number
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function isPromise (res?: any): res is Promise<void> {
|
|
83
|
-
return res != null && typeof res.then === 'function'
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export abstract class AbstractStream implements Stream {
|
|
87
|
-
public id: string
|
|
88
|
-
public direction: Direction
|
|
89
|
-
public timeline: StreamTimeline
|
|
90
|
-
public protocol?: string
|
|
91
|
-
public metadata: Record<string, unknown>
|
|
92
|
-
public source: AsyncGenerator<Uint8ArrayList, void, unknown>
|
|
93
|
-
public status: StreamStatus
|
|
94
|
-
public readStatus: ReadStatus
|
|
95
|
-
public writeStatus: WriteStatus
|
|
96
|
-
|
|
97
|
-
private readonly sinkController: AbortController
|
|
98
|
-
private readonly sinkEnd: DeferredPromise<void>
|
|
99
|
-
private endErr: Error | undefined
|
|
100
|
-
private readonly streamSource: Pushable<Uint8ArrayList>
|
|
101
|
-
private readonly onEnd?: (err?: Error | undefined) => void
|
|
102
|
-
private readonly onCloseRead?: () => void
|
|
103
|
-
private readonly onCloseWrite?: () => void
|
|
104
|
-
private readonly onReset?: () => void
|
|
105
|
-
private readonly onAbort?: (err: Error) => void
|
|
106
|
-
private readonly sendCloseWriteTimeout: number
|
|
107
|
-
|
|
108
|
-
protected readonly log: Logger
|
|
109
|
-
|
|
110
|
-
constructor (init: AbstractStreamInit) {
|
|
111
|
-
this.sinkController = new AbortController()
|
|
112
|
-
this.sinkEnd = defer()
|
|
113
|
-
this.log = init.log
|
|
114
|
-
|
|
115
|
-
// stream status
|
|
116
|
-
this.status = 'open'
|
|
117
|
-
this.readStatus = 'ready'
|
|
118
|
-
this.writeStatus = 'ready'
|
|
119
|
-
|
|
120
|
-
this.id = init.id
|
|
121
|
-
this.metadata = init.metadata ?? {}
|
|
122
|
-
this.direction = init.direction
|
|
123
|
-
this.timeline = {
|
|
124
|
-
open: Date.now()
|
|
125
|
-
}
|
|
126
|
-
this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT
|
|
127
|
-
|
|
128
|
-
this.onEnd = init.onEnd
|
|
129
|
-
this.onCloseRead = init?.onCloseRead
|
|
130
|
-
this.onCloseWrite = init?.onCloseWrite
|
|
131
|
-
this.onReset = init?.onReset
|
|
132
|
-
this.onAbort = init?.onAbort
|
|
133
|
-
|
|
134
|
-
this.source = this.streamSource = pushable<Uint8ArrayList>({
|
|
135
|
-
onEnd: (err) => {
|
|
136
|
-
if (err != null) {
|
|
137
|
-
this.log.trace('source ended with error', err)
|
|
138
|
-
} else {
|
|
139
|
-
this.log.trace('source ended')
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
this.onSourceEnd(err)
|
|
143
|
-
}
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
// necessary because the libp2p upgrader wraps the sink function
|
|
147
|
-
this.sink = this.sink.bind(this)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async sink (source: Source<Uint8ArrayList | Uint8Array>): Promise<void> {
|
|
151
|
-
if (this.writeStatus !== 'ready') {
|
|
152
|
-
throw new CodeError(`writable end state is "${this.writeStatus}" not "ready"`, ERR_SINK_INVALID_STATE)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
this.writeStatus = 'writing'
|
|
157
|
-
|
|
158
|
-
const options: AbortOptions = {
|
|
159
|
-
signal: this.sinkController.signal
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (this.direction === 'outbound') { // If initiator, open a new stream
|
|
163
|
-
const res = this.sendNewStream(options)
|
|
164
|
-
|
|
165
|
-
if (isPromise(res)) {
|
|
166
|
-
await res
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
source = abortableSource(source, this.sinkController.signal, {
|
|
171
|
-
returnOnAbort: true
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
this.log.trace('sink reading from source')
|
|
175
|
-
|
|
176
|
-
for await (let data of source) {
|
|
177
|
-
data = data instanceof Uint8Array ? new Uint8ArrayList(data) : data
|
|
178
|
-
|
|
179
|
-
const res = this.sendData(data, options)
|
|
180
|
-
|
|
181
|
-
if (isPromise(res)) { // eslint-disable-line max-depth
|
|
182
|
-
await res
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
this.log.trace('sink finished reading from source, write status is "%s"', this.writeStatus)
|
|
187
|
-
|
|
188
|
-
if (this.writeStatus === 'writing') {
|
|
189
|
-
this.writeStatus = 'closing'
|
|
190
|
-
|
|
191
|
-
this.log.trace('send close write to remote')
|
|
192
|
-
await this.sendCloseWrite({
|
|
193
|
-
signal: AbortSignal.timeout(this.sendCloseWriteTimeout)
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
this.writeStatus = 'closed'
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
this.onSinkEnd()
|
|
200
|
-
} catch (err: any) {
|
|
201
|
-
this.log.trace('sink ended with error, calling abort with error', err)
|
|
202
|
-
this.abort(err)
|
|
203
|
-
|
|
204
|
-
throw err
|
|
205
|
-
} finally {
|
|
206
|
-
this.log.trace('resolve sink end')
|
|
207
|
-
this.sinkEnd.resolve()
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
protected onSourceEnd (err?: Error): void {
|
|
212
|
-
if (this.timeline.closeRead != null) {
|
|
213
|
-
return
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
this.timeline.closeRead = Date.now()
|
|
217
|
-
this.readStatus = 'closed'
|
|
218
|
-
|
|
219
|
-
if (err != null && this.endErr == null) {
|
|
220
|
-
this.endErr = err
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
this.onCloseRead?.()
|
|
224
|
-
|
|
225
|
-
if (this.timeline.closeWrite != null) {
|
|
226
|
-
this.log.trace('source and sink ended')
|
|
227
|
-
this.timeline.close = Date.now()
|
|
228
|
-
|
|
229
|
-
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
230
|
-
this.status = 'closed'
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (this.onEnd != null) {
|
|
234
|
-
this.onEnd(this.endErr)
|
|
235
|
-
}
|
|
236
|
-
} else {
|
|
237
|
-
this.log.trace('source ended, waiting for sink to end')
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
protected onSinkEnd (err?: Error): void {
|
|
242
|
-
if (this.timeline.closeWrite != null) {
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.timeline.closeWrite = Date.now()
|
|
247
|
-
this.writeStatus = 'closed'
|
|
248
|
-
|
|
249
|
-
if (err != null && this.endErr == null) {
|
|
250
|
-
this.endErr = err
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
this.onCloseWrite?.()
|
|
254
|
-
|
|
255
|
-
if (this.timeline.closeRead != null) {
|
|
256
|
-
this.log.trace('sink and source ended')
|
|
257
|
-
this.timeline.close = Date.now()
|
|
258
|
-
|
|
259
|
-
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
260
|
-
this.status = 'closed'
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (this.onEnd != null) {
|
|
264
|
-
this.onEnd(this.endErr)
|
|
265
|
-
}
|
|
266
|
-
} else {
|
|
267
|
-
this.log.trace('sink ended, waiting for source to end')
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// Close for both Reading and Writing
|
|
272
|
-
async close (options?: AbortOptions): Promise<void> {
|
|
273
|
-
this.log.trace('closing gracefully')
|
|
274
|
-
|
|
275
|
-
this.status = 'closing'
|
|
276
|
-
|
|
277
|
-
await Promise.all([
|
|
278
|
-
this.closeRead(options),
|
|
279
|
-
this.closeWrite(options)
|
|
280
|
-
])
|
|
281
|
-
|
|
282
|
-
this.status = 'closed'
|
|
283
|
-
|
|
284
|
-
this.log.trace('closed gracefully')
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async closeRead (options: AbortOptions = {}): Promise<void> {
|
|
288
|
-
if (this.readStatus === 'closing' || this.readStatus === 'closed') {
|
|
289
|
-
return
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
this.log.trace('closing readable end of stream with starting read status "%s"', this.readStatus)
|
|
293
|
-
|
|
294
|
-
const readStatus = this.readStatus
|
|
295
|
-
this.readStatus = 'closing'
|
|
296
|
-
|
|
297
|
-
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
|
|
298
|
-
this.log.trace('send close read to remote')
|
|
299
|
-
await this.sendCloseRead(options)
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
if (readStatus === 'ready') {
|
|
303
|
-
this.log.trace('ending internal source queue')
|
|
304
|
-
this.streamSource.end()
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
this.log.trace('closed readable end of stream')
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
async closeWrite (options: AbortOptions = {}): Promise<void> {
|
|
311
|
-
if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
|
|
312
|
-
return
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus)
|
|
316
|
-
|
|
317
|
-
if (this.writeStatus === 'ready') {
|
|
318
|
-
this.log.trace('sink was never sunk, sink an empty array')
|
|
319
|
-
|
|
320
|
-
await raceSignal(this.sink([]), options.signal)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (this.writeStatus === 'writing') {
|
|
324
|
-
// stop reading from the source passed to `.sink` in the microtask queue
|
|
325
|
-
// - this lets any data queued by the user in the current tick get read
|
|
326
|
-
// before we exit
|
|
327
|
-
await new Promise((resolve, reject) => {
|
|
328
|
-
queueMicrotask(() => {
|
|
329
|
-
this.log.trace('aborting source passed to .sink')
|
|
330
|
-
this.sinkController.abort()
|
|
331
|
-
raceSignal(this.sinkEnd.promise, options.signal)
|
|
332
|
-
.then(resolve, reject)
|
|
333
|
-
})
|
|
334
|
-
})
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
this.writeStatus = 'closed'
|
|
338
|
-
|
|
339
|
-
this.log.trace('closed writable end of stream')
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Close immediately for reading and writing and send a reset message (local
|
|
344
|
-
* error)
|
|
345
|
-
*/
|
|
346
|
-
abort (err: Error): void {
|
|
347
|
-
if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
|
|
348
|
-
return
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
this.log('abort with error', err)
|
|
352
|
-
|
|
353
|
-
// try to send a reset message
|
|
354
|
-
this.log('try to send reset to remote')
|
|
355
|
-
const res = this.sendReset()
|
|
356
|
-
|
|
357
|
-
if (isPromise(res)) {
|
|
358
|
-
res.catch((err) => {
|
|
359
|
-
this.log.error('error sending reset message', err)
|
|
360
|
-
})
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
this.status = 'aborted'
|
|
364
|
-
this.timeline.abort = Date.now()
|
|
365
|
-
this._closeSinkAndSource(err)
|
|
366
|
-
this.onAbort?.(err)
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Receive a reset message - close immediately for reading and writing (remote
|
|
371
|
-
* error)
|
|
372
|
-
*/
|
|
373
|
-
reset (): void {
|
|
374
|
-
if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
|
|
375
|
-
return
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const err = new CodeError('stream reset', ERR_STREAM_RESET)
|
|
379
|
-
|
|
380
|
-
this.status = 'reset'
|
|
381
|
-
this.timeline.reset = Date.now()
|
|
382
|
-
this._closeSinkAndSource(err)
|
|
383
|
-
this.onReset?.()
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
_closeSinkAndSource (err?: Error): void {
|
|
387
|
-
this._closeSink(err)
|
|
388
|
-
this._closeSource(err)
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
_closeSink (err?: Error): void {
|
|
392
|
-
// if the sink function is running, cause it to end
|
|
393
|
-
if (this.writeStatus === 'writing') {
|
|
394
|
-
this.log.trace('end sink source')
|
|
395
|
-
this.sinkController.abort()
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
this.onSinkEnd(err)
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
_closeSource (err?: Error): void {
|
|
402
|
-
// if the source is not ending, end it
|
|
403
|
-
if (this.readStatus !== 'closing' && this.readStatus !== 'closed') {
|
|
404
|
-
this.log.trace('ending source with %d bytes to be read by consumer', this.streamSource.readableLength)
|
|
405
|
-
this.readStatus = 'closing'
|
|
406
|
-
this.streamSource.end(err)
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* The remote closed for writing so we should expect to receive no more
|
|
412
|
-
* messages
|
|
413
|
-
*/
|
|
414
|
-
remoteCloseWrite (): void {
|
|
415
|
-
if (this.readStatus === 'closing' || this.readStatus === 'closed') {
|
|
416
|
-
this.log('received remote close write but local source is already closed')
|
|
417
|
-
return
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
this.log.trace('remote close write')
|
|
421
|
-
this._closeSource()
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* The remote closed for reading so we should not send any more
|
|
426
|
-
* messages
|
|
427
|
-
*/
|
|
428
|
-
remoteCloseRead (): void {
|
|
429
|
-
if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
|
|
430
|
-
this.log('received remote close read but local sink is already closed')
|
|
431
|
-
return
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
this.log.trace('remote close read')
|
|
435
|
-
this._closeSink()
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* The underlying muxer has closed, no more messages can be sent or will
|
|
440
|
-
* be received, close immediately to free up resources
|
|
441
|
-
*/
|
|
442
|
-
destroy (): void {
|
|
443
|
-
if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
|
|
444
|
-
this.log('received destroy but we are already closed')
|
|
445
|
-
return
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
this.log.trace('stream destroyed')
|
|
449
|
-
|
|
450
|
-
this._closeSinkAndSource()
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* When an extending class reads data from it's implementation-specific source,
|
|
455
|
-
* call this method to allow the stream consumer to read the data.
|
|
456
|
-
*/
|
|
457
|
-
sourcePush (data: Uint8ArrayList): void {
|
|
458
|
-
this.streamSource.push(data)
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Returns the amount of unread data - can be used to prevent large amounts of
|
|
463
|
-
* data building up when the stream consumer is too slow.
|
|
464
|
-
*/
|
|
465
|
-
sourceReadableLength (): number {
|
|
466
|
-
return this.streamSource.readableLength
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* Send a message to the remote muxer informing them a new stream is being
|
|
471
|
-
* opened
|
|
472
|
-
*/
|
|
473
|
-
abstract sendNewStream (options?: AbortOptions): void | Promise<void>
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* Send a data message to the remote muxer
|
|
477
|
-
*/
|
|
478
|
-
abstract sendData (buf: Uint8ArrayList, options?: AbortOptions): void | Promise<void>
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Send a reset message to the remote muxer
|
|
482
|
-
*/
|
|
483
|
-
abstract sendReset (options?: AbortOptions): void | Promise<void>
|
|
484
|
-
|
|
485
|
-
/**
|
|
486
|
-
* Send a message to the remote muxer, informing them no more data messages
|
|
487
|
-
* will be sent by this end of the stream
|
|
488
|
-
*/
|
|
489
|
-
abstract sendCloseWrite (options?: AbortOptions): void | Promise<void>
|
|
490
|
-
|
|
491
|
-
/**
|
|
492
|
-
* Send a message to the remote muxer, informing them no more data messages
|
|
493
|
-
* will be read by this end of the stream
|
|
494
|
-
*/
|
|
495
|
-
abstract sendCloseRead (options?: AbortOptions): void | Promise<void>
|
|
496
|
-
}
|
|
11
|
+
export const AbstractStream = s.AbstractStream
|
package/src/topology/index.ts
CHANGED
|
@@ -2,9 +2,6 @@ import type { Connection } from '../connection/index.js'
|
|
|
2
2
|
import type { PeerId } from '../peer-id/index.js'
|
|
3
3
|
|
|
4
4
|
export interface Topology {
|
|
5
|
-
min?: number
|
|
6
|
-
max?: number
|
|
7
|
-
|
|
8
5
|
/**
|
|
9
6
|
* If true, invoke `onConnect` for this topology on transient (e.g. short-lived
|
|
10
7
|
* and/or data-limited) connections. (default: false)
|
package/src/transport/index.ts
CHANGED
|
@@ -28,7 +28,7 @@ export interface Listener extends TypedEventTarget<ListenerEvents> {
|
|
|
28
28
|
close(): Promise<void>
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export const
|
|
31
|
+
export const transportSymbol = Symbol.for('@libp2p/transport')
|
|
32
32
|
|
|
33
33
|
export interface ConnectionHandler { (connection: Connection): void }
|
|
34
34
|
|
|
@@ -55,7 +55,7 @@ export interface Transport {
|
|
|
55
55
|
/**
|
|
56
56
|
* Used by the isTransport function
|
|
57
57
|
*/
|
|
58
|
-
[
|
|
58
|
+
[transportSymbol]: true
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
61
|
* Dial a given multiaddr.
|
|
@@ -74,7 +74,7 @@ export interface Transport {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
export function isTransport (other: any): other is Transport {
|
|
77
|
-
return other != null && Boolean(other[
|
|
77
|
+
return other != null && Boolean(other[transportSymbol])
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @packageDocumentation
|
|
3
|
-
*
|
|
4
|
-
* The libp2p keychain provides an API to store keys in a datastore in
|
|
5
|
-
* an encrypted format.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
*
|
|
9
|
-
* ```typescript
|
|
10
|
-
* import { createLibp2p } from 'libp2p'
|
|
11
|
-
* import { FsDatastore } from 'datastore-fs'
|
|
12
|
-
*
|
|
13
|
-
* const node = await createLibp2p({
|
|
14
|
-
* datastore: new FsDatastore('/path/to/dir')
|
|
15
|
-
* })
|
|
16
|
-
*
|
|
17
|
-
* const info = await node.keychain.createKey('my-new-key', 'Ed25519')
|
|
18
|
-
*
|
|
19
|
-
* console.info(info) // { id: '...', name: 'my-new-key' }
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
import type { KeyType } from '../keys/index.js';
|
|
23
|
-
import type { PeerId } from '../peer-id/index.js';
|
|
24
|
-
import type { Multibase } from 'multiformats/bases/interface';
|
|
25
|
-
export interface KeyInfo {
|
|
26
|
-
/**
|
|
27
|
-
* The universally unique key id
|
|
28
|
-
*/
|
|
29
|
-
id: string;
|
|
30
|
-
/**
|
|
31
|
-
* The local key name
|
|
32
|
-
*/
|
|
33
|
-
name: string;
|
|
34
|
-
}
|
|
35
|
-
export interface KeyChain {
|
|
36
|
-
/**
|
|
37
|
-
* Export an existing key as a PEM encrypted PKCS #8 string.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
*
|
|
41
|
-
* ```js
|
|
42
|
-
* await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
43
|
-
* const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
exportKey(name: string, password: string): Promise<Multibase<'m'>>;
|
|
47
|
-
/**
|
|
48
|
-
* Import a new key from a PEM encoded PKCS #8 string.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
*
|
|
52
|
-
* ```js
|
|
53
|
-
* await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
54
|
-
* const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
|
|
55
|
-
* const keyInfo = await libp2p.keychain.importKey('keyTestImport', pemKey, 'password123')
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
importKey(name: string, pem: string, password: string): Promise<KeyInfo>;
|
|
59
|
-
/**
|
|
60
|
-
* Import a new key from a PeerId with a private key component
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
*
|
|
64
|
-
* ```js
|
|
65
|
-
* const keyInfo = await libp2p.keychain.importPeer('keyTestImport', peerIdFromString('12D3Foo...'))
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
importPeer(name: string, peerId: PeerId): Promise<KeyInfo>;
|
|
69
|
-
/**
|
|
70
|
-
* Export an existing key as a PeerId
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
*
|
|
74
|
-
* ```js
|
|
75
|
-
* const peerId = await libp2p.keychain.exportPeerId('key-name')
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
exportPeerId(name: string): Promise<PeerId>;
|
|
79
|
-
/**
|
|
80
|
-
* Create a key in the keychain.
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
*
|
|
84
|
-
* ```js
|
|
85
|
-
* const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
createKey(name: string, type: KeyType, size?: number): Promise<KeyInfo>;
|
|
89
|
-
/**
|
|
90
|
-
* List all the keys.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
*
|
|
94
|
-
* ```js
|
|
95
|
-
* const keyInfos = await libp2p.keychain.listKeys()
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
listKeys(): Promise<KeyInfo[]>;
|
|
99
|
-
/**
|
|
100
|
-
* Removes a key from the keychain.
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
*
|
|
104
|
-
* ```js
|
|
105
|
-
* await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
106
|
-
* const keyInfo = await libp2p.keychain.removeKey('keyTest')
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
removeKey(name: string): Promise<KeyInfo>;
|
|
110
|
-
/**
|
|
111
|
-
* Rename a key in the keychain.
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
*
|
|
115
|
-
* ```js
|
|
116
|
-
* await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
117
|
-
* const keyInfo = await libp2p.keychain.renameKey('keyTest', 'keyNewNtest')
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
renameKey(oldName: string, newName: string): Promise<KeyInfo>;
|
|
121
|
-
/**
|
|
122
|
-
* Find a key by it's id.
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
*
|
|
126
|
-
* ```js
|
|
127
|
-
* const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
128
|
-
* const keyInfo2 = await libp2p.keychain.findKeyById(keyInfo.id)
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
findKeyById(id: string): Promise<KeyInfo>;
|
|
132
|
-
/**
|
|
133
|
-
* Find a key by it's name.
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
*
|
|
137
|
-
* ```js
|
|
138
|
-
* const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
|
|
139
|
-
* const keyInfo2 = await libp2p.keychain.findKeyByName('keyTest')
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
findKeyByName(name: string): Promise<KeyInfo>;
|
|
143
|
-
/**
|
|
144
|
-
* Rotate keychain password and re-encrypt all associated keys
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
*
|
|
148
|
-
* ```js
|
|
149
|
-
* await libp2p.keychain.rotateKeychainPass('oldPassword', 'newPassword')
|
|
150
|
-
* ```
|
|
151
|
-
*/
|
|
152
|
-
rotateKeychainPass(oldPass: string, newPass: string): Promise<void>;
|
|
153
|
-
}
|
|
154
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/keychain/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAE7D,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,QAAQ;IACvB;;;;;;;;;OASG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAA;IAElE;;;;;;;;;;OAUG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAExE;;;;;;;;OAQG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAE1D;;;;;;;;OAQG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAE3C;;;;;;;;OAQG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAEvE;;;;;;;;OAQG;IACH,QAAQ,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC,CAAA;IAE9B;;;;;;;;;OASG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAEzC;;;;;;;;;OASG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAE7D;;;;;;;;;OASG;IACH,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAEzC;;;;;;;;;OASG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAE7C;;;;;;;;OAQG;IACH,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACpE"}
|