@libp2p/webtransport 5.0.51-8484de8a2 → 5.0.51-87bc8d4fb
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 +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/constants.d.ts +2 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +2 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/index.d.ts +4 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +63 -32
- package/dist/src/index.js.map +1 -1
- package/dist/src/listener.d.ts +1 -0
- package/dist/src/listener.d.ts.map +1 -1
- package/dist/src/listener.js.map +1 -1
- package/dist/src/muxer.d.ts +5 -2
- package/dist/src/muxer.d.ts.map +1 -1
- package/dist/src/muxer.js +74 -44
- package/dist/src/muxer.js.map +1 -1
- package/dist/src/stream.d.ts +2 -21
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +47 -75
- package/dist/src/stream.js.map +1 -1
- package/dist/src/utils/inert-duplex.d.ts +3 -0
- package/dist/src/utils/inert-duplex.d.ts.map +1 -0
- package/dist/src/utils/inert-duplex.js +20 -0
- package/dist/src/utils/inert-duplex.js.map +1 -0
- 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 +11 -17
- package/dist/src/utils/parse-multiaddr.js.map +1 -1
- package/package.json +20 -19
- package/src/constants.ts +1 -0
- package/src/index.ts +74 -36
- package/src/listener.ts +1 -0
- package/src/muxer.ts +84 -60
- package/src/stream.ts +55 -88
- package/src/utils/inert-duplex.ts +21 -0
- package/src/utils/parse-multiaddr.ts +12 -20
- package/dist/src/session-to-conn.d.ts +0 -11
- package/dist/src/session-to-conn.d.ts.map +0 -1
- package/dist/src/session-to-conn.js +0 -35
- package/dist/src/session-to-conn.js.map +0 -1
- package/dist/src/utils/webtransport-message-stream.d.ts +0 -18
- package/dist/src/utils/webtransport-message-stream.d.ts.map +0 -1
- package/dist/src/utils/webtransport-message-stream.js +0 -49
- package/dist/src/utils/webtransport-message-stream.js.map +0 -1
- package/src/session-to-conn.ts +0 -50
- package/src/utils/webtransport-message-stream.ts +0 -69
package/src/muxer.ts
CHANGED
|
@@ -1,79 +1,103 @@
|
|
|
1
|
-
import { AbstractStreamMuxer } from '@libp2p/utils'
|
|
2
1
|
import { webtransportBiDiStreamToStream } from './stream.js'
|
|
3
|
-
import
|
|
2
|
+
import { inertDuplex } from './utils/inert-duplex.js'
|
|
4
3
|
import type WebTransport from './webtransport.js'
|
|
5
|
-
import type {
|
|
4
|
+
import type { Logger, Stream, StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface'
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
export interface WebTransportMuxerInit {
|
|
7
|
+
maxInboundStreams: number
|
|
8
|
+
}
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
private reader: ReadableStreamDefaultReader<WebTransportBidirectionalStream>
|
|
10
|
+
export function webtransportMuxer (wt: Pick<WebTransport, 'close' | 'createBidirectionalStream'>, reader: ReadableStreamDefaultReader<WebTransportBidirectionalStream>, log: Logger, config: WebTransportMuxerInit): StreamMuxerFactory {
|
|
11
|
+
let streamIDCounter = 0
|
|
12
|
+
log = log.newScope('muxer')
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
return {
|
|
15
|
+
protocol: 'webtransport',
|
|
16
|
+
createStreamMuxer: (init?: StreamMuxerInit): StreamMuxer => {
|
|
17
|
+
// !TODO handle abort signal when WebTransport supports this.
|
|
18
|
+
const activeStreams: Stream[] = []
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
Promise.resolve()
|
|
21
|
+
.then(async () => {
|
|
22
|
+
//! TODO unclear how to add backpressure here?
|
|
23
|
+
while (true) {
|
|
24
|
+
const { done, value: wtStream } = await reader.read()
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
while (true) {
|
|
28
|
-
const { done, value } = await this.reader.read()
|
|
26
|
+
if (done) {
|
|
27
|
+
break
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
if (activeStreams.length >= config.maxInboundStreams) {
|
|
31
|
+
log(`too many inbound streams open - ${activeStreams.length}/${config.maxInboundStreams}, closing new incoming stream`)
|
|
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
|
+
}
|
|
32
51
|
}
|
|
52
|
+
})
|
|
53
|
+
.catch(err => {
|
|
54
|
+
log.error('could not create a new stream', err)
|
|
55
|
+
})
|
|
33
56
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
}
|
|
57
|
+
const muxer: StreamMuxer = {
|
|
58
|
+
protocol: 'webtransport',
|
|
59
|
+
streams: activeStreams,
|
|
60
|
+
newStream: async (name?: string): Promise<Stream> => {
|
|
61
|
+
log('new outgoing stream', name)
|
|
49
62
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
const wtStream = await wt.createBidirectionalStream()
|
|
64
|
+
const stream = await webtransportBiDiStreamToStream(wtStream, String(streamIDCounter++), init?.direction ?? 'outbound', activeStreams, init?.onStreamEnd, log)
|
|
65
|
+
activeStreams.push(stream)
|
|
53
66
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
String(this.streamIDCounter++),
|
|
57
|
-
'outbound',
|
|
58
|
-
this.log,
|
|
59
|
-
options
|
|
60
|
-
)
|
|
61
|
-
}
|
|
67
|
+
return stream
|
|
68
|
+
},
|
|
62
69
|
|
|
63
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Close all tracked streams and stop the muxer
|
|
72
|
+
*/
|
|
73
|
+
close: async () => {
|
|
74
|
+
log('closing webtransport muxer gracefully')
|
|
64
75
|
|
|
65
|
-
|
|
76
|
+
try {
|
|
77
|
+
wt.close()
|
|
78
|
+
} catch (err: any) {
|
|
79
|
+
muxer.abort(err)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
66
82
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Abort all tracked streams and stop the muxer
|
|
85
|
+
*/
|
|
86
|
+
abort: (err: Error) => {
|
|
87
|
+
log('closing webtransport muxer with err:', err)
|
|
71
88
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
89
|
+
try {
|
|
90
|
+
wt.close()
|
|
91
|
+
} catch (err: any) {
|
|
92
|
+
log.error('webtransport session threw error during close', err)
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
// This stream muxer is webtransport native. Therefore it doesn't plug in with any other duplex.
|
|
97
|
+
...inertDuplex()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return muxer
|
|
77
101
|
}
|
|
78
102
|
}
|
|
79
103
|
}
|
package/src/stream.ts
CHANGED
|
@@ -1,118 +1,85 @@
|
|
|
1
|
-
import { AbstractStream } from '@libp2p/utils'
|
|
1
|
+
import { AbstractStream } from '@libp2p/utils/abstract-stream'
|
|
2
2
|
import { raceSignal } from 'race-signal'
|
|
3
|
-
import
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
3
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
4
|
+
import type { AbortOptions, Direction, Logger, Stream } from '@libp2p/interface'
|
|
5
|
+
import type { AbstractStreamInit } from '@libp2p/utils/abstract-stream'
|
|
6
6
|
|
|
7
7
|
interface WebTransportStreamInit extends AbstractStreamInit {
|
|
8
|
-
|
|
8
|
+
bidiStream: WebTransportBidirectionalStream
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
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.bidiStream.writable.getWriter()
|
|
19
|
+
this.reader = init.bidiStream.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 {
|
|
42
21
|
Promise.resolve()
|
|
43
22
|
.then(async () => {
|
|
44
23
|
while (true) {
|
|
45
24
|
const result = await this.reader.read()
|
|
46
25
|
|
|
47
|
-
if (result.value != null) {
|
|
48
|
-
this.onData(result.value)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
26
|
if (result.done) {
|
|
52
|
-
|
|
53
|
-
this.onRemoteCloseWrite()
|
|
27
|
+
init.log('remote closed write')
|
|
54
28
|
return
|
|
55
29
|
}
|
|
56
30
|
|
|
57
|
-
if (
|
|
58
|
-
|
|
31
|
+
if (result.value != null) {
|
|
32
|
+
this.sourcePush(new Uint8ArrayList(result.value))
|
|
59
33
|
}
|
|
60
34
|
}
|
|
61
35
|
})
|
|
62
36
|
.catch(err => {
|
|
37
|
+
init.log.error('error reading from stream', err)
|
|
63
38
|
this.abort(err)
|
|
64
39
|
})
|
|
65
40
|
.finally(() => {
|
|
66
|
-
this.
|
|
41
|
+
this.remoteCloseWrite()
|
|
67
42
|
})
|
|
68
|
-
}
|
|
69
|
-
|
|
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)
|
|
75
|
-
.catch(err => {
|
|
76
|
-
this.abort(err)
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
43
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
44
|
+
void this.writer.closed
|
|
45
|
+
.then(() => {
|
|
46
|
+
init.log('writer closed')
|
|
47
|
+
})
|
|
48
|
+
.catch((err) => {
|
|
49
|
+
init.log('writer close promise rejected', err)
|
|
50
|
+
})
|
|
51
|
+
.finally(() => {
|
|
52
|
+
this.remoteCloseRead()
|
|
53
|
+
})
|
|
54
|
+
}
|
|
89
55
|
|
|
90
|
-
|
|
56
|
+
sendNewStream (options?: AbortOptions | undefined): void {
|
|
57
|
+
// this is a no-op
|
|
58
|
+
}
|
|
91
59
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
this.abort(err)
|
|
97
|
-
})
|
|
98
|
-
}
|
|
60
|
+
async sendData (buf: Uint8ArrayList, options?: AbortOptions): Promise<void> {
|
|
61
|
+
for (const chunk of buf) {
|
|
62
|
+
this.log('sendData waiting for writer to be ready')
|
|
63
|
+
await raceSignal(this.writer.ready, options?.signal)
|
|
99
64
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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)
|
|
68
|
+
.catch(err => {
|
|
69
|
+
this.log.error('error sending stream data', err)
|
|
70
|
+
})
|
|
103
71
|
}
|
|
104
72
|
}
|
|
105
73
|
|
|
106
|
-
sendReset (
|
|
107
|
-
this.writer
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
})
|
|
74
|
+
async sendReset (options?: AbortOptions): Promise<void> {
|
|
75
|
+
this.log('sendReset aborting writer')
|
|
76
|
+
await raceSignal(this.writer.abort(), options?.signal)
|
|
77
|
+
this.log('sendReset aborted writer')
|
|
111
78
|
}
|
|
112
79
|
|
|
113
80
|
async sendCloseWrite (options?: AbortOptions): Promise<void> {
|
|
114
81
|
this.log('sendCloseWrite closing writer')
|
|
115
|
-
await raceSignal(this.writer.close()
|
|
82
|
+
await raceSignal(this.writer.close(), options?.signal)
|
|
116
83
|
this.log('sendCloseWrite closed writer')
|
|
117
84
|
}
|
|
118
85
|
|
|
@@ -121,23 +88,23 @@ export class WebTransportStream extends AbstractStream {
|
|
|
121
88
|
await raceSignal(this.reader.cancel(), options?.signal)
|
|
122
89
|
this.log('sendCloseRead cancelled reader')
|
|
123
90
|
}
|
|
124
|
-
|
|
125
|
-
sendPause (): void {
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
sendResume (): void {
|
|
130
|
-
this.readData()
|
|
131
|
-
}
|
|
132
91
|
}
|
|
133
92
|
|
|
134
|
-
export function webtransportBiDiStreamToStream (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
stream,
|
|
93
|
+
export async function webtransportBiDiStreamToStream (bidiStream: WebTransportBidirectionalStream, streamId: string, direction: Direction, activeStreams: Stream[], onStreamEnd: undefined | ((s: Stream) => void), log: Logger): Promise<Stream> {
|
|
94
|
+
const stream = new WebTransportStream({
|
|
95
|
+
bidiStream,
|
|
138
96
|
id: streamId,
|
|
139
97
|
direction,
|
|
140
98
|
log: log.newScope(`${direction}:${streamId}`),
|
|
141
|
-
|
|
99
|
+
onEnd: () => {
|
|
100
|
+
const index = activeStreams.findIndex(s => s === stream)
|
|
101
|
+
if (index !== -1) {
|
|
102
|
+
activeStreams.splice(index, 1)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
onStreamEnd?.(stream)
|
|
106
|
+
}
|
|
142
107
|
})
|
|
108
|
+
|
|
109
|
+
return stream
|
|
143
110
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InvalidMultiaddrError } from '@libp2p/interface'
|
|
2
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
-
import {
|
|
3
|
+
import { protocols } from '@multiformats/multiaddr'
|
|
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,28 +25,20 @@ 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
|
-
|
|
28
|
+
const parts = ma.stringTuples()
|
|
29
|
+
const certhashes = parts
|
|
30
|
+
.filter(([name, _]) => name === protocols('certhash').code)
|
|
31
|
+
.map(([_, value]) => decodeCerthashStr(value ?? ''))
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
// only take the first peer id in the multiaddr as it may be a relay
|
|
34
|
+
const remotePeer = parts
|
|
35
|
+
.filter(([name, _]) => name === protocols('p2p').code)
|
|
36
|
+
.map(([_, value]) => peerIdFromString(value ?? ''))[0]
|
|
35
37
|
|
|
36
|
-
|
|
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)
|
|
38
|
+
const opts = ma.toOptions()
|
|
47
39
|
let host = opts.host
|
|
48
40
|
|
|
49
|
-
if (opts.
|
|
41
|
+
if (opts.family === 6 && host?.includes(':')) {
|
|
50
42
|
/**
|
|
51
43
|
* This resolves cases where `new WebTransport()` fails to construct because of an invalid URL being passed.
|
|
52
44
|
*
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { MultiaddrConnection } from '@libp2p/interface';
|
|
2
|
-
import type { AbstractMultiaddrConnectionInit } from '@libp2p/utils';
|
|
3
|
-
export interface WebTransportSessionMultiaddrConnectionInit extends Omit<AbstractMultiaddrConnectionInit, 'name' | 'stream'> {
|
|
4
|
-
cleanUpWTSession(metric: string): void;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Convert a socket into a MultiaddrConnection
|
|
8
|
-
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
9
|
-
*/
|
|
10
|
-
export declare const toMultiaddrConnection: (init: WebTransportSessionMultiaddrConnectionInit) => MultiaddrConnection;
|
|
11
|
-
//# sourceMappingURL=session-to-conn.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session-to-conn.d.ts","sourceRoot":"","sources":["../../src/session-to-conn.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAgB,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,KAAK,EAAE,+BAA+B,EAAc,MAAM,eAAe,CAAA;AAGhF,MAAM,WAAW,0CAA2C,SAAQ,IAAI,CAAC,+BAA+B,EAAE,MAAM,GAAG,QAAQ,CAAC;IAC1H,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACvC;AAoCD;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAAI,MAAM,0CAA0C,KAAG,mBAExF,CAAA"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { AbstractMultiaddrConnection } from '@libp2p/utils';
|
|
2
|
-
class WebTransportSessionMultiaddrConnection extends AbstractMultiaddrConnection {
|
|
3
|
-
cleanUpWTSession;
|
|
4
|
-
constructor(init) {
|
|
5
|
-
super(init);
|
|
6
|
-
this.cleanUpWTSession = init.cleanUpWTSession;
|
|
7
|
-
}
|
|
8
|
-
sendData(data) {
|
|
9
|
-
return {
|
|
10
|
-
sentBytes: data.byteLength,
|
|
11
|
-
canSendMore: true
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
sendReset() {
|
|
15
|
-
this.cleanUpWTSession('abort');
|
|
16
|
-
}
|
|
17
|
-
async sendClose(options) {
|
|
18
|
-
this.cleanUpWTSession('close');
|
|
19
|
-
options?.signal?.throwIfAborted();
|
|
20
|
-
}
|
|
21
|
-
sendPause() {
|
|
22
|
-
// TODO: backpressure?
|
|
23
|
-
}
|
|
24
|
-
sendResume() {
|
|
25
|
-
// TODO: backpressure?
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Convert a socket into a MultiaddrConnection
|
|
30
|
-
* https://github.com/libp2p/interface-transport#multiaddrconnection
|
|
31
|
-
*/
|
|
32
|
-
export const toMultiaddrConnection = (init) => {
|
|
33
|
-
return new WebTransportSessionMultiaddrConnection(init);
|
|
34
|
-
};
|
|
35
|
-
//# sourceMappingURL=session-to-conn.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session-to-conn.js","sourceRoot":"","sources":["../../src/session-to-conn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAA;AAS3D,MAAM,sCAAuC,SAAQ,2BAA2B;IACtE,gBAAgB,CAA0B;IAElD,YAAa,IAAgD;QAC3D,KAAK,CAAC,IAAI,CAAC,CAAA;QAEX,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,CAAA;IAC/C,CAAC;IAED,QAAQ,CAAE,IAAoB;QAC5B,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,UAAU;YAC1B,WAAW,EAAE,IAAI;SAClB,CAAA;IACH,CAAC;IAED,SAAS;QACP,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAA;IAChC,CAAC;IAED,KAAK,CAAC,SAAS,CAAE,OAAsB;QACrC,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAA;QAC9B,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;IACnC,CAAC;IAED,SAAS;QACP,sBAAsB;IACxB,CAAC;IAED,UAAU;QACR,sBAAsB;IACxB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,IAAgD,EAAuB,EAAE;IAC7G,OAAO,IAAI,sCAAsC,CAAC,IAAI,CAAC,CAAA;AACzD,CAAC,CAAA"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { AbstractMessageStream } from '@libp2p/utils';
|
|
2
|
-
import type { AbortOptions } from '@libp2p/interface';
|
|
3
|
-
import type { MessageStreamInit, SendResult } from '@libp2p/utils';
|
|
4
|
-
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
5
|
-
export interface WebTransportMessageStreamInit extends MessageStreamInit {
|
|
6
|
-
stream: WebTransportBidirectionalStream;
|
|
7
|
-
}
|
|
8
|
-
export declare class WebTransportMessageStream extends AbstractMessageStream {
|
|
9
|
-
private writer;
|
|
10
|
-
private reader;
|
|
11
|
-
constructor(init: WebTransportMessageStreamInit);
|
|
12
|
-
close(options?: AbortOptions): Promise<void>;
|
|
13
|
-
sendData(data: Uint8ArrayList): SendResult;
|
|
14
|
-
sendReset(err: Error): void;
|
|
15
|
-
sendPause(): void;
|
|
16
|
-
sendResume(): void;
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=webtransport-message-stream.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webtransport-message-stream.d.ts","sourceRoot":"","sources":["../../../src/utils/webtransport-message-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AAErD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,MAAM,WAAW,6BAA8B,SAAQ,iBAAiB;IACtE,MAAM,EAAE,+BAA+B,CAAA;CACxC;AAED,qBAAa,yBAA0B,SAAQ,qBAAqB;IAClE,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,MAAM,CAAkC;gBAEnC,IAAI,EAAE,6BAA6B;IAwB1C,KAAK,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAInD,QAAQ,CAAE,IAAI,EAAE,cAAc,GAAG,UAAU;IAY3C,SAAS,CAAE,GAAG,EAAE,KAAK,GAAG,IAAI;IAO5B,SAAS,IAAK,IAAI;IAIlB,UAAU,IAAK,IAAI;CAGpB"}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { AbstractMessageStream } from '@libp2p/utils';
|
|
2
|
-
import { raceSignal } from 'race-signal';
|
|
3
|
-
export class WebTransportMessageStream extends AbstractMessageStream {
|
|
4
|
-
writer;
|
|
5
|
-
reader;
|
|
6
|
-
constructor(init) {
|
|
7
|
-
super(init);
|
|
8
|
-
this.writer = init.stream.writable.getWriter();
|
|
9
|
-
this.reader = init.stream.readable.getReader();
|
|
10
|
-
Promise.resolve().then(async () => {
|
|
11
|
-
while (true) {
|
|
12
|
-
const { done, value } = await this.reader.read();
|
|
13
|
-
if (value != null) {
|
|
14
|
-
this.onData(value);
|
|
15
|
-
}
|
|
16
|
-
if (done) {
|
|
17
|
-
break;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
})
|
|
21
|
-
.catch(err => {
|
|
22
|
-
this.abort(err);
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
async close(options) {
|
|
26
|
-
await raceSignal(this.writer.close(), options?.signal);
|
|
27
|
-
}
|
|
28
|
-
sendData(data) {
|
|
29
|
-
this.writer.write(data.subarray())
|
|
30
|
-
.catch(err => {
|
|
31
|
-
this.abort(err);
|
|
32
|
-
});
|
|
33
|
-
return {
|
|
34
|
-
sentBytes: data.byteLength,
|
|
35
|
-
canSendMore: true
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
sendReset(err) {
|
|
39
|
-
this.writer.abort(err)
|
|
40
|
-
.catch(err => {
|
|
41
|
-
this.log.error('could not send reset - %e', err);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
sendPause() {
|
|
45
|
-
}
|
|
46
|
-
sendResume() {
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
//# sourceMappingURL=webtransport-message-stream.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webtransport-message-stream.js","sourceRoot":"","sources":["../../../src/utils/webtransport-message-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AASxC,MAAM,OAAO,yBAA0B,SAAQ,qBAAqB;IAC1D,MAAM,CAAkC;IACxC,MAAM,CAAkC;IAEhD,YAAa,IAAmC;QAC9C,KAAK,CAAC,IAAI,CAAC,CAAA;QAEX,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAA;QAC9C,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAA;QAE9C,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE;YAChC,OAAO,IAAI,EAAE,CAAC;gBACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAA;gBAEhD,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;oBAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;gBACpB,CAAC;gBAED,IAAI,IAAI,EAAE,CAAC;oBACT,MAAK;gBACP,CAAC;YACH,CAAC;QACH,CAAC,CAAC;aACC,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACjB,CAAC,CAAC,CAAA;IACN,CAAC;IAED,KAAK,CAAC,KAAK,CAAE,OAAsB;QACjC,MAAM,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;IACxD,CAAC;IAED,QAAQ,CAAE,IAAoB;QAC5B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;aAC/B,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACjB,CAAC,CAAC,CAAA;QAEJ,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,UAAU;YAC1B,WAAW,EAAE,IAAI;SAClB,CAAA;IACH,CAAC;IAED,SAAS,CAAE,GAAU;QACnB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;aACnB,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAA;QAClD,CAAC,CAAC,CAAA;IACN,CAAC;IAED,SAAS;IAET,CAAC;IAED,UAAU;IAEV,CAAC;CACF"}
|
package/src/session-to-conn.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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
|
-
}
|