@libp2p/webtransport 5.0.51 → 6.0.0-55b7e5fea
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/dist/index.min.js +11 -12
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +1 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +32 -63
- package/dist/src/index.js.map +1 -1
- package/dist/src/listener.d.ts +0 -1
- package/dist/src/listener.d.ts.map +1 -1
- package/dist/src/listener.js.map +1 -1
- package/dist/src/muxer.d.ts +2 -5
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +44 -74
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/session-to-conn.d.ts +11 -0
- package/dist/src/session-to-conn.d.ts.map +1 -0
- package/dist/src/session-to-conn.js +35 -0
- package/dist/src/session-to-conn.js.map +1 -0
- package/dist/src/stream.d.ts +21 -2
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +75 -47
- package/dist/src/stream.js.map +1 -1
- package/dist/src/utils/parse-multiaddr.d.ts +1 -1
- package/dist/src/utils/parse-multiaddr.d.ts.map +1 -1
- package/dist/src/utils/parse-multiaddr.js +17 -11
- package/dist/src/utils/parse-multiaddr.js.map +1 -1
- package/dist/src/utils/webtransport-message-stream.d.ts +18 -0
- package/dist/src/utils/webtransport-message-stream.d.ts.map +1 -0
- package/dist/src/utils/webtransport-message-stream.js +49 -0
- package/dist/src/utils/webtransport-message-stream.js.map +1 -0
- package/package.json +20 -21
- package/src/index.ts +36 -74
- package/src/listener.ts +0 -1
- package/src/muxer.ts +60 -84
- package/src/session-to-conn.ts +50 -0
- package/src/stream.ts +88 -55
- package/src/utils/parse-multiaddr.ts +20 -12
- package/src/utils/webtransport-message-stream.ts +69 -0
- package/dist/src/constants.d.ts +0 -2
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -2
- package/dist/src/constants.js.map +0 -1
- package/dist/src/utils/inert-duplex.d.ts +0 -3
- package/dist/src/utils/inert-duplex.d.ts.map +0 -1
- package/dist/src/utils/inert-duplex.js +0 -20
- package/dist/src/utils/inert-duplex.js.map +0 -1
- package/dist/typedoc-urls.json +0 -14
- package/src/constants.ts +0 -1
- package/src/utils/inert-duplex.ts +0 -21
package/src/muxer.ts
CHANGED
|
@@ -1,103 +1,79 @@
|
|
|
1
|
+
import { AbstractStreamMuxer } from '@libp2p/utils'
|
|
1
2
|
import { webtransportBiDiStreamToStream } from './stream.js'
|
|
2
|
-
import {
|
|
3
|
+
import type { WebTransportStream } from './stream.ts'
|
|
3
4
|
import type WebTransport from './webtransport.js'
|
|
4
|
-
import type {
|
|
5
|
+
import type { CreateStreamOptions, MultiaddrConnection, StreamMuxer, StreamMuxerFactory } from '@libp2p/interface'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
maxInboundStreams: number
|
|
8
|
-
}
|
|
7
|
+
const PROTOCOL = '/webtransport'
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
class WebTransportStreamMuxer extends AbstractStreamMuxer<WebTransportStream> {
|
|
10
|
+
private webTransport: WebTransport
|
|
11
|
+
private streamIDCounter: number
|
|
12
|
+
private reader: ReadableStreamDefaultReader<WebTransportBidirectionalStream>
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
constructor (webTransport: WebTransport, maConn: MultiaddrConnection) {
|
|
15
|
+
super(maConn, {
|
|
16
|
+
protocol: PROTOCOL,
|
|
17
|
+
name: 'muxer'
|
|
18
|
+
})
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
while (true) {
|
|
24
|
-
const { done, value: wtStream } = await reader.read()
|
|
20
|
+
this.webTransport = webTransport
|
|
21
|
+
this.streamIDCounter = 0
|
|
22
|
+
this.reader = this.webTransport.incomingBidirectionalStreams.getReader()
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
Promise.resolve()
|
|
25
|
+
.then(async () => {
|
|
26
|
+
//! TODO unclear how to add backpressure here?
|
|
27
|
+
while (true) {
|
|
28
|
+
const { done, value } = await this.reader.read()
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// We've reached our limit, close this stream.
|
|
33
|
-
wtStream.writable.close().catch((err: Error) => {
|
|
34
|
-
log.error(`failed to close inbound stream that crossed our maxInboundStream limit: ${err.message}`)
|
|
35
|
-
})
|
|
36
|
-
wtStream.readable.cancel().catch((err: Error) => {
|
|
37
|
-
log.error(`failed to close inbound stream that crossed our maxInboundStream limit: ${err.message}`)
|
|
38
|
-
})
|
|
39
|
-
} else {
|
|
40
|
-
const stream = await webtransportBiDiStreamToStream(
|
|
41
|
-
wtStream,
|
|
42
|
-
String(streamIDCounter++),
|
|
43
|
-
'inbound',
|
|
44
|
-
activeStreams,
|
|
45
|
-
init?.onStreamEnd,
|
|
46
|
-
log
|
|
47
|
-
)
|
|
48
|
-
activeStreams.push(stream)
|
|
49
|
-
init?.onIncomingStream?.(stream)
|
|
50
|
-
}
|
|
30
|
+
if (done || value == null) {
|
|
31
|
+
break
|
|
51
32
|
}
|
|
52
|
-
})
|
|
53
|
-
.catch(err => {
|
|
54
|
-
log.error('could not create a new stream', err)
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
const muxer: StreamMuxer = {
|
|
58
|
-
protocol: 'webtransport',
|
|
59
|
-
streams: activeStreams,
|
|
60
|
-
newStream: async (name?: string): Promise<Stream> => {
|
|
61
|
-
log('new outgoing stream', name)
|
|
62
33
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
34
|
+
this.onRemoteStream(
|
|
35
|
+
webtransportBiDiStreamToStream(
|
|
36
|
+
value,
|
|
37
|
+
String(this.streamIDCounter++),
|
|
38
|
+
'inbound',
|
|
39
|
+
this.log,
|
|
40
|
+
this.streamOptions
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
.catch(err => {
|
|
46
|
+
this.log.error('could not create a new stream - %e', err)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
69
49
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
close: async () => {
|
|
74
|
-
log('closing webtransport muxer gracefully')
|
|
50
|
+
async onCreateStream (options: CreateStreamOptions): Promise<WebTransportStream> {
|
|
51
|
+
const wtStream = await this.webTransport.createBidirectionalStream()
|
|
52
|
+
options?.signal?.throwIfAborted()
|
|
75
53
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
54
|
+
return webtransportBiDiStreamToStream(
|
|
55
|
+
wtStream,
|
|
56
|
+
String(this.streamIDCounter++),
|
|
57
|
+
'outbound',
|
|
58
|
+
this.log,
|
|
59
|
+
options
|
|
60
|
+
)
|
|
61
|
+
}
|
|
82
62
|
|
|
83
|
-
|
|
84
|
-
* Abort all tracked streams and stop the muxer
|
|
85
|
-
*/
|
|
86
|
-
abort: (err: Error) => {
|
|
87
|
-
log('closing webtransport muxer with err:', err)
|
|
63
|
+
onData (): void {
|
|
88
64
|
|
|
89
|
-
|
|
90
|
-
wt.close()
|
|
91
|
-
} catch (err: any) {
|
|
92
|
-
log.error('webtransport session threw error during close', err)
|
|
93
|
-
}
|
|
94
|
-
},
|
|
65
|
+
}
|
|
95
66
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
67
|
+
sendReset (): void {
|
|
68
|
+
this.webTransport.close()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
99
71
|
|
|
100
|
-
|
|
72
|
+
export function webtransportMuxer (webTransport: WebTransport): StreamMuxerFactory {
|
|
73
|
+
return {
|
|
74
|
+
protocol: PROTOCOL,
|
|
75
|
+
createStreamMuxer (maConn: MultiaddrConnection): StreamMuxer {
|
|
76
|
+
return new WebTransportStreamMuxer(webTransport, maConn)
|
|
101
77
|
}
|
|
102
78
|
}
|
|
103
79
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AbstractMultiaddrConnection } from '@libp2p/utils'
|
|
2
|
+
import type { AbortOptions, MultiaddrConnection } from '@libp2p/interface'
|
|
3
|
+
import type { AbstractMultiaddrConnectionInit, SendResult } from '@libp2p/utils'
|
|
4
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
5
|
+
|
|
6
|
+
export interface WebTransportSessionMultiaddrConnectionInit extends Omit<AbstractMultiaddrConnectionInit, 'name' | 'stream'> {
|
|
7
|
+
cleanUpWTSession(metric: string): void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class WebTransportSessionMultiaddrConnection extends AbstractMultiaddrConnection {
|
|
11
|
+
private cleanUpWTSession: (metric: string) => void
|
|
12
|
+
|
|
13
|
+
constructor (init: WebTransportSessionMultiaddrConnectionInit) {
|
|
14
|
+
super(init)
|
|
15
|
+
|
|
16
|
+
this.cleanUpWTSession = init.cleanUpWTSession
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
sendData (data: Uint8ArrayList): SendResult {
|
|
20
|
+
return {
|
|
21
|
+
sentBytes: data.byteLength,
|
|
22
|
+
canSendMore: true
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
sendReset (): void {
|
|
27
|
+
this.cleanUpWTSession('abort')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async sendClose (options?: AbortOptions): Promise<void> {
|
|
31
|
+
this.cleanUpWTSession('close')
|
|
32
|
+
options?.signal?.throwIfAborted()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
sendPause (): void {
|
|
36
|
+
// TODO: backpressure?
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
sendResume (): void {
|
|
40
|
+
// TODO: backpressure?
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Convert a socket into a MultiaddrConnection
|
|
46
|
+
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
47
|
+
*/
|
|
48
|
+
export const toMultiaddrConnection = (init: WebTransportSessionMultiaddrConnectionInit): MultiaddrConnection => {
|
|
49
|
+
return new WebTransportSessionMultiaddrConnection(init)
|
|
50
|
+
}
|
package/src/stream.ts
CHANGED
|
@@ -1,85 +1,118 @@
|
|
|
1
|
-
import { AbstractStream } from '@libp2p/utils
|
|
1
|
+
import { AbstractStream } from '@libp2p/utils'
|
|
2
2
|
import { raceSignal } from 'race-signal'
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
3
|
+
import type { AbortOptions, MessageStreamDirection, Logger, StreamOptions } from '@libp2p/interface'
|
|
4
|
+
import type { AbstractStreamInit, SendResult } from '@libp2p/utils'
|
|
5
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
6
|
|
|
7
7
|
interface WebTransportStreamInit extends AbstractStreamInit {
|
|
8
|
-
|
|
8
|
+
stream: WebTransportBidirectionalStream
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
class WebTransportStream extends AbstractStream {
|
|
11
|
+
export class WebTransportStream extends AbstractStream {
|
|
12
12
|
private readonly writer: WritableStreamDefaultWriter<Uint8Array>
|
|
13
13
|
private readonly reader: ReadableStreamDefaultReader<Uint8Array>
|
|
14
14
|
|
|
15
15
|
constructor (init: WebTransportStreamInit) {
|
|
16
16
|
super(init)
|
|
17
17
|
|
|
18
|
-
this.writer = init.
|
|
19
|
-
this.reader = init.
|
|
18
|
+
this.writer = init.stream.writable.getWriter()
|
|
19
|
+
this.reader = init.stream.readable.getReader()
|
|
20
20
|
|
|
21
|
+
void this.writer.closed
|
|
22
|
+
.then(() => {
|
|
23
|
+
this.log('writer closed gracefully')
|
|
24
|
+
})
|
|
25
|
+
.catch((err) => {
|
|
26
|
+
// chrome/ff send different messages
|
|
27
|
+
if (err.message.includes('STOP_SENDING') || err.message.includes('StopSending')) {
|
|
28
|
+
// err.code === 0 so we may be able to use this to detect remote close
|
|
29
|
+
// read instead?
|
|
30
|
+
this.onRemoteCloseRead()
|
|
31
|
+
} else if (err.message.includes('RESET_STREAM') || err.message.includes('Reset')) {
|
|
32
|
+
this.onRemoteReset()
|
|
33
|
+
} else {
|
|
34
|
+
this.log('writer close promise rejected - %e', err)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
this.readData()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private readData (): void {
|
|
21
42
|
Promise.resolve()
|
|
22
43
|
.then(async () => {
|
|
23
44
|
while (true) {
|
|
24
45
|
const result = await this.reader.read()
|
|
25
46
|
|
|
47
|
+
if (result.value != null) {
|
|
48
|
+
this.onData(result.value)
|
|
49
|
+
}
|
|
50
|
+
|
|
26
51
|
if (result.done) {
|
|
27
|
-
|
|
52
|
+
this.log('remote closed write')
|
|
53
|
+
this.onRemoteCloseWrite()
|
|
28
54
|
return
|
|
29
55
|
}
|
|
30
56
|
|
|
31
|
-
if (
|
|
32
|
-
|
|
57
|
+
if (this.readStatus === 'paused') {
|
|
58
|
+
break
|
|
33
59
|
}
|
|
34
60
|
}
|
|
35
61
|
})
|
|
36
62
|
.catch(err => {
|
|
37
|
-
init.log.error('error reading from stream', err)
|
|
38
63
|
this.abort(err)
|
|
39
64
|
})
|
|
40
65
|
.finally(() => {
|
|
41
|
-
this.
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
void this.writer.closed
|
|
45
|
-
.then(() => {
|
|
46
|
-
init.log('writer closed')
|
|
66
|
+
this.reader.releaseLock()
|
|
47
67
|
})
|
|
48
|
-
.catch((err) => {
|
|
49
|
-
init.log('writer close promise rejected', err)
|
|
50
|
-
})
|
|
51
|
-
.finally(() => {
|
|
52
|
-
this.remoteCloseRead()
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
sendNewStream (options?: AbortOptions | undefined): void {
|
|
57
|
-
// this is a no-op
|
|
58
68
|
}
|
|
59
69
|
|
|
60
|
-
|
|
61
|
-
for
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
// the streams spec recommends not waiting for data to be sent
|
|
66
|
-
// https://streams.spec.whatwg.org/#example-manual-write-dont-await
|
|
67
|
-
this.writer.write(chunk)
|
|
70
|
+
sendData (data: Uint8ArrayList): SendResult {
|
|
71
|
+
// the streams spec recommends not waiting for data to be sent
|
|
72
|
+
// https://streams.spec.whatwg.org/#example-manual-write-dont-await
|
|
73
|
+
for (const buf of data) {
|
|
74
|
+
this.writer.write(buf)
|
|
68
75
|
.catch(err => {
|
|
69
|
-
this.
|
|
76
|
+
this.abort(err)
|
|
70
77
|
})
|
|
71
78
|
}
|
|
79
|
+
|
|
80
|
+
this.log.trace('desired size after sending %d bytes is %d bytes', data.byteLength, this.writer.desiredSize)
|
|
81
|
+
|
|
82
|
+
// null means the stream has errored - https://streams.spec.whatwg.org/#writable-stream-default-writer-get-desired-size
|
|
83
|
+
if (this.writer.desiredSize == null) {
|
|
84
|
+
return {
|
|
85
|
+
sentBytes: data.byteLength,
|
|
86
|
+
canSendMore: false
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const canSendMore = this.writer.desiredSize > 0
|
|
91
|
+
|
|
92
|
+
if (!canSendMore) {
|
|
93
|
+
this.writer.ready.then(() => {
|
|
94
|
+
this.safeDispatchEvent('drain')
|
|
95
|
+
}, (err) => {
|
|
96
|
+
this.abort(err)
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
sentBytes: data.byteLength,
|
|
102
|
+
canSendMore
|
|
103
|
+
}
|
|
72
104
|
}
|
|
73
105
|
|
|
74
|
-
|
|
75
|
-
this.
|
|
76
|
-
|
|
77
|
-
|
|
106
|
+
sendReset (err: Error): void {
|
|
107
|
+
this.writer.abort(err)
|
|
108
|
+
.catch(err => {
|
|
109
|
+
this.log.error('error aborting writer - %e', err)
|
|
110
|
+
})
|
|
78
111
|
}
|
|
79
112
|
|
|
80
113
|
async sendCloseWrite (options?: AbortOptions): Promise<void> {
|
|
81
114
|
this.log('sendCloseWrite closing writer')
|
|
82
|
-
await raceSignal(this.writer.close(), options?.signal)
|
|
115
|
+
await raceSignal(this.writer.close().catch(() => {}), options?.signal)
|
|
83
116
|
this.log('sendCloseWrite closed writer')
|
|
84
117
|
}
|
|
85
118
|
|
|
@@ -88,23 +121,23 @@ class WebTransportStream extends AbstractStream {
|
|
|
88
121
|
await raceSignal(this.reader.cancel(), options?.signal)
|
|
89
122
|
this.log('sendCloseRead cancelled reader')
|
|
90
123
|
}
|
|
124
|
+
|
|
125
|
+
sendPause (): void {
|
|
126
|
+
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
sendResume (): void {
|
|
130
|
+
this.readData()
|
|
131
|
+
}
|
|
91
132
|
}
|
|
92
133
|
|
|
93
|
-
export
|
|
94
|
-
|
|
95
|
-
|
|
134
|
+
export function webtransportBiDiStreamToStream (stream: WebTransportBidirectionalStream, streamId: string, direction: MessageStreamDirection, log: Logger, options?: StreamOptions): WebTransportStream {
|
|
135
|
+
return new WebTransportStream({
|
|
136
|
+
...options,
|
|
137
|
+
stream,
|
|
96
138
|
id: streamId,
|
|
97
139
|
direction,
|
|
98
140
|
log: log.newScope(`${direction}:${streamId}`),
|
|
99
|
-
|
|
100
|
-
const index = activeStreams.findIndex(s => s === stream)
|
|
101
|
-
if (index !== -1) {
|
|
102
|
-
activeStreams.splice(index, 1)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
onStreamEnd?.(stream)
|
|
106
|
-
}
|
|
141
|
+
protocol: ''
|
|
107
142
|
})
|
|
108
|
-
|
|
109
|
-
return stream
|
|
110
143
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InvalidMultiaddrError } from '@libp2p/interface'
|
|
2
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
-
import {
|
|
3
|
+
import { getNetConfig } from '@libp2p/utils'
|
|
4
4
|
import { WebTransport } from '@multiformats/multiaddr-matcher'
|
|
5
5
|
import { bases, digest } from 'multiformats/basics'
|
|
6
6
|
import type { PeerId } from '@libp2p/interface'
|
|
@@ -17,7 +17,7 @@ function decodeCerthashStr (s: string): MultihashDigest {
|
|
|
17
17
|
export interface ParsedMultiaddr {
|
|
18
18
|
url: string
|
|
19
19
|
certhashes: MultihashDigest[]
|
|
20
|
-
remotePeer
|
|
20
|
+
remotePeer: PeerId
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export function parseMultiaddr (ma: Multiaddr): ParsedMultiaddr {
|
|
@@ -25,20 +25,28 @@ export function parseMultiaddr (ma: Multiaddr): ParsedMultiaddr {
|
|
|
25
25
|
throw new InvalidMultiaddrError('Invalid multiaddr, was not a WebTransport address')
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
.filter(([name, _]) => name === protocols('certhash').code)
|
|
31
|
-
.map(([_, value]) => decodeCerthashStr(value ?? ''))
|
|
28
|
+
const certhashes: MultihashDigest[] = []
|
|
29
|
+
let remotePeer: PeerId | undefined
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
for (const components of ma.getComponents()) {
|
|
32
|
+
if (components.name === 'certhash') {
|
|
33
|
+
certhashes.push(decodeCerthashStr(components.value ?? ''))
|
|
34
|
+
}
|
|
37
35
|
|
|
38
|
-
|
|
36
|
+
// only take the first peer id in the multiaddr as it may be a relay
|
|
37
|
+
if (components.name === 'p2p' && remotePeer == null) {
|
|
38
|
+
remotePeer = peerIdFromString(components.value ?? '')
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (remotePeer == null) {
|
|
43
|
+
throw new InvalidMultiaddrError('Remote peer must be present in multiaddr')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const opts = getNetConfig(ma)
|
|
39
47
|
let host = opts.host
|
|
40
48
|
|
|
41
|
-
if (opts.
|
|
49
|
+
if (opts.type === 'ip6' && host.includes(':')) {
|
|
42
50
|
/**
|
|
43
51
|
* This resolves cases where `new WebTransport()` fails to construct because of an invalid URL being passed.
|
|
44
52
|
*
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AbstractMessageStream } from '@libp2p/utils'
|
|
2
|
+
import { raceSignal } from 'race-signal'
|
|
3
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
4
|
+
import type { MessageStreamInit, SendResult } from '@libp2p/utils'
|
|
5
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
|
+
|
|
7
|
+
export interface WebTransportMessageStreamInit extends MessageStreamInit {
|
|
8
|
+
stream: WebTransportBidirectionalStream
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class WebTransportMessageStream extends AbstractMessageStream {
|
|
12
|
+
private writer: WritableStreamDefaultWriter<any>
|
|
13
|
+
private reader: ReadableStreamDefaultReader<any>
|
|
14
|
+
|
|
15
|
+
constructor (init: WebTransportMessageStreamInit) {
|
|
16
|
+
super(init)
|
|
17
|
+
|
|
18
|
+
this.writer = init.stream.writable.getWriter()
|
|
19
|
+
this.reader = init.stream.readable.getReader()
|
|
20
|
+
|
|
21
|
+
Promise.resolve().then(async () => {
|
|
22
|
+
while (true) {
|
|
23
|
+
const { done, value } = await this.reader.read()
|
|
24
|
+
|
|
25
|
+
if (value != null) {
|
|
26
|
+
this.onData(value)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (done) {
|
|
30
|
+
break
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
.catch(err => {
|
|
35
|
+
this.abort(err)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async close (options?: AbortOptions): Promise<void> {
|
|
40
|
+
await raceSignal(this.writer.close(), options?.signal)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
sendData (data: Uint8ArrayList): SendResult {
|
|
44
|
+
this.writer.write(data.subarray())
|
|
45
|
+
.catch(err => {
|
|
46
|
+
this.abort(err)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
sentBytes: data.byteLength,
|
|
51
|
+
canSendMore: true
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
sendReset (err: Error): void {
|
|
56
|
+
this.writer.abort(err)
|
|
57
|
+
.catch(err => {
|
|
58
|
+
this.log.error('could not send reset - %e', err)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
sendPause (): void {
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
sendResume (): void {
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
}
|
package/dist/src/constants.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,OAAQ,CAAA"}
|
package/dist/src/constants.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inert-duplex.d.ts","sourceRoot":"","sources":["../../../src/utils/inert-duplex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAU,MAAM,iBAAiB,CAAA;AAGrD,wBAAgB,WAAW,IAAK,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAiBpD"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// Duplex that does nothing. Needed to fulfill the interface
|
|
2
|
-
export function inertDuplex() {
|
|
3
|
-
return {
|
|
4
|
-
source: {
|
|
5
|
-
[Symbol.asyncIterator]() {
|
|
6
|
-
return {
|
|
7
|
-
async next() {
|
|
8
|
-
// This will never resolve
|
|
9
|
-
return new Promise(() => { });
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
sink: async (source) => {
|
|
15
|
-
// This will never resolve
|
|
16
|
-
return new Promise(() => { });
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
//# sourceMappingURL=inert-duplex.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"inert-duplex.js","sourceRoot":"","sources":["../../../src/utils/inert-duplex.ts"],"names":[],"mappings":"AAEA,4DAA4D;AAC5D,MAAM,UAAU,WAAW;IACzB,OAAO;QACL,MAAM,EAAE;YACN,CAAC,MAAM,CAAC,aAAa,CAAC;gBACpB,OAAO;oBACL,KAAK,CAAC,IAAI;wBACR,0BAA0B;wBAC1B,OAAO,IAAI,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;oBAC/B,CAAC;iBACF,CAAA;YACH,CAAC;SACF;QACD,IAAI,EAAE,KAAK,EAAE,MAAmB,EAAE,EAAE;YAClC,0BAA0B;YAC1B,OAAO,IAAI,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;QAC/B,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"WebTransportCertificate": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportCertificate.html",
|
|
3
|
-
".:WebTransportCertificate": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportCertificate.html",
|
|
4
|
-
"WebTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportComponents.html",
|
|
5
|
-
".:WebTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportComponents.html",
|
|
6
|
-
"WebTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportInit.html",
|
|
7
|
-
".:WebTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportInit.html",
|
|
8
|
-
"WebTransportMetrics": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportMetrics.html",
|
|
9
|
-
".:WebTransportMetrics": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportMetrics.html",
|
|
10
|
-
"WebTransportDialEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_webtransport.WebTransportDialEvents.html",
|
|
11
|
-
".:WebTransportDialEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_webtransport.WebTransportDialEvents.html",
|
|
12
|
-
"webTransport": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webtransport.webTransport.html",
|
|
13
|
-
".:webTransport": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webtransport.webTransport.html"
|
|
14
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const MAX_INBOUND_STREAMS = 1_000
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
2
|
-
|
|
3
|
-
// Duplex that does nothing. Needed to fulfill the interface
|
|
4
|
-
export function inertDuplex (): Duplex<any, any, any> {
|
|
5
|
-
return {
|
|
6
|
-
source: {
|
|
7
|
-
[Symbol.asyncIterator] () {
|
|
8
|
-
return {
|
|
9
|
-
async next () {
|
|
10
|
-
// This will never resolve
|
|
11
|
-
return new Promise(() => { })
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
sink: async (source: Source<any>) => {
|
|
17
|
-
// This will never resolve
|
|
18
|
-
return new Promise(() => { })
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|