@libp2p/yamux 7.0.4-0f07e3df5
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 +127 -0
- package/dist/index.min.js +4 -0
- package/dist/index.min.js.map +7 -0
- package/dist/src/config.d.ts +30 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +41 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/constants.d.ts +12 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +22 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/decode.d.ts +34 -0
- package/dist/src/decode.d.ts.map +1 -0
- package/dist/src/decode.js +76 -0
- package/dist/src/decode.js.map +1 -0
- package/dist/src/encode.d.ts +3 -0
- package/dist/src/encode.d.ts.map +1 -0
- package/dist/src/encode.js +19 -0
- package/dist/src/encode.js.map +1 -0
- package/dist/src/errors.d.ts +40 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +70 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/frame.d.ts +54 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frame.js +36 -0
- package/dist/src/frame.js.map +1 -0
- package/dist/src/index.d.ts +85 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +84 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/muxer.d.ts +91 -0
- package/dist/src/muxer.d.ts.map +1 -0
- package/dist/src/muxer.js +428 -0
- package/dist/src/muxer.js.map +1 -0
- package/dist/src/stream.d.ts +96 -0
- package/dist/src/stream.d.ts.map +1 -0
- package/dist/src/stream.js +253 -0
- package/dist/src/stream.js.map +1 -0
- package/package.json +95 -0
- package/src/config.ts +72 -0
- package/src/constants.ts +25 -0
- package/src/decode.ts +105 -0
- package/src/encode.ts +26 -0
- package/src/errors.ts +89 -0
- package/src/frame.ts +64 -0
- package/src/index.ts +90 -0
- package/src/muxer.ts +507 -0
- package/src/stream.ts +313 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { serviceCapabilities } from '@libp2p/interface';
|
|
2
|
+
import { AbstractStreamMuxer } from '@libp2p/utils';
|
|
3
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
4
|
+
import { GoAwayCode } from './frame.js';
|
|
5
|
+
import { YamuxStream } from './stream.js';
|
|
6
|
+
import type { Config } from './config.js';
|
|
7
|
+
import type { AbortOptions, MessageStream, StreamMuxerFactory } from '@libp2p/interface';
|
|
8
|
+
export interface YamuxMuxerInit extends Partial<Config> {
|
|
9
|
+
}
|
|
10
|
+
export declare class Yamux implements StreamMuxerFactory {
|
|
11
|
+
protocol: string;
|
|
12
|
+
private readonly _init;
|
|
13
|
+
constructor(init?: Partial<Config>);
|
|
14
|
+
readonly [Symbol.toStringTag] = "@libp2p/yamux";
|
|
15
|
+
readonly [serviceCapabilities]: string[];
|
|
16
|
+
createStreamMuxer(maConn: MessageStream): YamuxMuxer;
|
|
17
|
+
}
|
|
18
|
+
export interface CloseOptions extends AbortOptions {
|
|
19
|
+
reason?: GoAwayCode;
|
|
20
|
+
}
|
|
21
|
+
export interface ActivePing extends PromiseWithResolvers<number> {
|
|
22
|
+
id: number;
|
|
23
|
+
start: number;
|
|
24
|
+
}
|
|
25
|
+
export declare class YamuxMuxer extends AbstractStreamMuxer<YamuxStream> {
|
|
26
|
+
/** The next stream id to be used when initiating a new stream */
|
|
27
|
+
private nextStreamID;
|
|
28
|
+
/** The next ping id to be used when pinging */
|
|
29
|
+
private nextPingID;
|
|
30
|
+
/** Tracking info for the currently active ping */
|
|
31
|
+
private activePing?;
|
|
32
|
+
/** Round trip time */
|
|
33
|
+
private rtt;
|
|
34
|
+
/** True if client, false if server */
|
|
35
|
+
private client;
|
|
36
|
+
private localGoAway?;
|
|
37
|
+
private remoteGoAway?;
|
|
38
|
+
/** Number of tracked inbound streams */
|
|
39
|
+
private numInboundStreams;
|
|
40
|
+
/** Number of tracked outbound streams */
|
|
41
|
+
private numOutboundStreams;
|
|
42
|
+
private decoder;
|
|
43
|
+
private keepAlive?;
|
|
44
|
+
private enableKeepAlive;
|
|
45
|
+
private keepAliveInterval;
|
|
46
|
+
private maxInboundStreams;
|
|
47
|
+
private maxOutboundStreams;
|
|
48
|
+
constructor(maConn: MessageStream, init?: YamuxMuxerInit);
|
|
49
|
+
onData(buf: Uint8Array | Uint8ArrayList): void;
|
|
50
|
+
onCreateStream(): YamuxStream;
|
|
51
|
+
/**
|
|
52
|
+
* Initiate a ping and wait for a response
|
|
53
|
+
*
|
|
54
|
+
* Note: only a single ping will be initiated at a time.
|
|
55
|
+
* If a ping is already in progress, a new ping will not be initiated.
|
|
56
|
+
*
|
|
57
|
+
* @returns the round-trip-time in milliseconds
|
|
58
|
+
*/
|
|
59
|
+
ping(options?: AbortOptions): Promise<number>;
|
|
60
|
+
/**
|
|
61
|
+
* Get the ping round trip time
|
|
62
|
+
*
|
|
63
|
+
* Note: Will return 0 if no successful ping has yet been completed
|
|
64
|
+
*
|
|
65
|
+
* @returns the round-trip-time in milliseconds
|
|
66
|
+
*/
|
|
67
|
+
getRTT(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Close the muxer
|
|
70
|
+
*/
|
|
71
|
+
close(options?: CloseOptions): Promise<void>;
|
|
72
|
+
abort(err: Error): void;
|
|
73
|
+
onTransportClosed(): void;
|
|
74
|
+
/** Create a new stream */
|
|
75
|
+
private _newStream;
|
|
76
|
+
/**
|
|
77
|
+
* closeStream is used to close a stream once both sides have
|
|
78
|
+
* issued a close.
|
|
79
|
+
*/
|
|
80
|
+
private closeStream;
|
|
81
|
+
private handleFrame;
|
|
82
|
+
private handlePing;
|
|
83
|
+
private handlePingResponse;
|
|
84
|
+
private handleGoAway;
|
|
85
|
+
private handleStreamMessage;
|
|
86
|
+
private incomingStream;
|
|
87
|
+
private sendFrame;
|
|
88
|
+
private sendPing;
|
|
89
|
+
private sendGoAway;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=muxer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OAAO,CAAC,QAAQ;IAchB,OAAO,CAAC,UAAU;CAUnB"}
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import { InvalidParametersError, MuxerClosedError, TooManyOutboundProtocolStreamsError, serviceCapabilities } from '@libp2p/interface';
|
|
2
|
+
import { AbstractStreamMuxer, repeatingTask } from '@libp2p/utils';
|
|
3
|
+
import { raceSignal } from 'race-signal';
|
|
4
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
5
|
+
import { defaultConfig, verifyConfig } from './config.js';
|
|
6
|
+
import { Decoder } from './decode.js';
|
|
7
|
+
import { encodeHeader } from './encode.js';
|
|
8
|
+
import { InvalidFrameError, isProtocolError, NotMatchingPingError, UnRequestedPingError } from './errors.js';
|
|
9
|
+
import { Flag, FrameType, GoAwayCode } from './frame.js';
|
|
10
|
+
import { StreamState, YamuxStream } from './stream.js';
|
|
11
|
+
function debugFrame(header) {
|
|
12
|
+
return {
|
|
13
|
+
type: FrameType[header.type],
|
|
14
|
+
flags: [
|
|
15
|
+
(header.flag & Flag.SYN) === Flag.SYN ? 'SYN' : undefined,
|
|
16
|
+
(header.flag & Flag.ACK) === Flag.ACK ? 'ACK' : undefined,
|
|
17
|
+
(header.flag & Flag.FIN) === Flag.FIN ? 'FIN' : undefined,
|
|
18
|
+
(header.flag & Flag.RST) === Flag.RST ? 'RST' : undefined
|
|
19
|
+
].filter(Boolean),
|
|
20
|
+
streamID: header.streamID,
|
|
21
|
+
length: header.length
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const YAMUX_PROTOCOL_ID = '/yamux/1.0.0';
|
|
25
|
+
export class Yamux {
|
|
26
|
+
protocol = YAMUX_PROTOCOL_ID;
|
|
27
|
+
_init;
|
|
28
|
+
constructor(init = {}) {
|
|
29
|
+
this._init = init;
|
|
30
|
+
}
|
|
31
|
+
[Symbol.toStringTag] = '@libp2p/yamux';
|
|
32
|
+
[serviceCapabilities] = [
|
|
33
|
+
'@libp2p/stream-multiplexing'
|
|
34
|
+
];
|
|
35
|
+
createStreamMuxer(maConn) {
|
|
36
|
+
return new YamuxMuxer(maConn, {
|
|
37
|
+
...this._init
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class YamuxMuxer extends AbstractStreamMuxer {
|
|
42
|
+
/** The next stream id to be used when initiating a new stream */
|
|
43
|
+
nextStreamID;
|
|
44
|
+
/** The next ping id to be used when pinging */
|
|
45
|
+
nextPingID;
|
|
46
|
+
/** Tracking info for the currently active ping */
|
|
47
|
+
activePing;
|
|
48
|
+
/** Round trip time */
|
|
49
|
+
rtt;
|
|
50
|
+
/** True if client, false if server */
|
|
51
|
+
client;
|
|
52
|
+
localGoAway;
|
|
53
|
+
remoteGoAway;
|
|
54
|
+
/** Number of tracked inbound streams */
|
|
55
|
+
numInboundStreams;
|
|
56
|
+
/** Number of tracked outbound streams */
|
|
57
|
+
numOutboundStreams;
|
|
58
|
+
decoder;
|
|
59
|
+
keepAlive;
|
|
60
|
+
enableKeepAlive;
|
|
61
|
+
keepAliveInterval;
|
|
62
|
+
maxInboundStreams;
|
|
63
|
+
maxOutboundStreams;
|
|
64
|
+
constructor(maConn, init = {}) {
|
|
65
|
+
super(maConn, {
|
|
66
|
+
...init,
|
|
67
|
+
protocol: YAMUX_PROTOCOL_ID,
|
|
68
|
+
name: 'yamux'
|
|
69
|
+
});
|
|
70
|
+
this.client = maConn.direction === 'outbound';
|
|
71
|
+
verifyConfig(init);
|
|
72
|
+
this.enableKeepAlive = init.enableKeepAlive ?? defaultConfig.enableKeepAlive;
|
|
73
|
+
this.keepAliveInterval = init.keepAliveInterval ?? defaultConfig.keepAliveInterval;
|
|
74
|
+
this.maxInboundStreams = init.maxInboundStreams ?? defaultConfig.maxInboundStreams;
|
|
75
|
+
this.maxOutboundStreams = init.maxOutboundStreams ?? defaultConfig.maxOutboundStreams;
|
|
76
|
+
this.decoder = new Decoder();
|
|
77
|
+
this.numInboundStreams = 0;
|
|
78
|
+
this.numOutboundStreams = 0;
|
|
79
|
+
// client uses odd streamIDs, server uses even streamIDs
|
|
80
|
+
this.nextStreamID = this.client ? 1 : 2;
|
|
81
|
+
this.nextPingID = 0;
|
|
82
|
+
this.rtt = -1;
|
|
83
|
+
this.log.trace('muxer created');
|
|
84
|
+
if (this.enableKeepAlive) {
|
|
85
|
+
this.log.trace('muxer keepalive enabled interval=%s', this.keepAliveInterval);
|
|
86
|
+
this.keepAlive = repeatingTask(async (options) => {
|
|
87
|
+
try {
|
|
88
|
+
await this.ping(options);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
// TODO: should abort here?
|
|
92
|
+
this.log.error('ping error: %s', err);
|
|
93
|
+
}
|
|
94
|
+
}, this.keepAliveInterval, {
|
|
95
|
+
// send an initial ping to establish RTT
|
|
96
|
+
runImmediately: true
|
|
97
|
+
});
|
|
98
|
+
this.keepAlive.start();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
onData(buf) {
|
|
102
|
+
for (const frame of this.decoder.emitFrames(buf)) {
|
|
103
|
+
this.handleFrame(frame);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
onCreateStream() {
|
|
107
|
+
if (this.remoteGoAway !== undefined) {
|
|
108
|
+
throw new MuxerClosedError('Muxer closed remotely');
|
|
109
|
+
}
|
|
110
|
+
if (this.localGoAway !== undefined) {
|
|
111
|
+
throw new MuxerClosedError('Muxer closed locally');
|
|
112
|
+
}
|
|
113
|
+
const id = this.nextStreamID;
|
|
114
|
+
this.nextStreamID += 2;
|
|
115
|
+
// check against our configured maximum number of outbound streams
|
|
116
|
+
if (this.numOutboundStreams >= this.maxOutboundStreams) {
|
|
117
|
+
throw new TooManyOutboundProtocolStreamsError('max outbound streams exceeded');
|
|
118
|
+
}
|
|
119
|
+
this.log.trace('new outgoing stream id=%s', id);
|
|
120
|
+
const stream = this._newStream(id, StreamState.Init, 'outbound');
|
|
121
|
+
this.numOutboundStreams++;
|
|
122
|
+
// send a window update to open the stream on the receiver end. do this in a
|
|
123
|
+
// microtask so the stream gets added to the streams array by the superclass
|
|
124
|
+
// before we send the SYN flag, otherwise we create a race condition whereby
|
|
125
|
+
// we can receive the ACK before the stream is added to the streams list
|
|
126
|
+
queueMicrotask(() => {
|
|
127
|
+
stream.sendWindowUpdate();
|
|
128
|
+
});
|
|
129
|
+
return stream;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Initiate a ping and wait for a response
|
|
133
|
+
*
|
|
134
|
+
* Note: only a single ping will be initiated at a time.
|
|
135
|
+
* If a ping is already in progress, a new ping will not be initiated.
|
|
136
|
+
*
|
|
137
|
+
* @returns the round-trip-time in milliseconds
|
|
138
|
+
*/
|
|
139
|
+
async ping(options) {
|
|
140
|
+
if (this.remoteGoAway !== undefined) {
|
|
141
|
+
throw new MuxerClosedError('Muxer closed remotely');
|
|
142
|
+
}
|
|
143
|
+
if (this.localGoAway !== undefined) {
|
|
144
|
+
throw new MuxerClosedError('Muxer closed locally');
|
|
145
|
+
}
|
|
146
|
+
if (this.activePing != null) {
|
|
147
|
+
// an active ping is already in progress, piggyback off that
|
|
148
|
+
return raceSignal(this.activePing.promise, options?.signal);
|
|
149
|
+
}
|
|
150
|
+
// An active ping does not yet exist, handle the process here
|
|
151
|
+
// create active ping
|
|
152
|
+
this.activePing = Object.assign(Promise.withResolvers(), {
|
|
153
|
+
id: this.nextPingID++,
|
|
154
|
+
start: Date.now()
|
|
155
|
+
});
|
|
156
|
+
// send ping
|
|
157
|
+
this.sendPing(this.activePing.id);
|
|
158
|
+
// await pong
|
|
159
|
+
try {
|
|
160
|
+
this.rtt = await raceSignal(this.activePing.promise, options?.signal);
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
// clean-up active ping
|
|
164
|
+
this.activePing = undefined;
|
|
165
|
+
}
|
|
166
|
+
return this.rtt;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Get the ping round trip time
|
|
170
|
+
*
|
|
171
|
+
* Note: Will return 0 if no successful ping has yet been completed
|
|
172
|
+
*
|
|
173
|
+
* @returns the round-trip-time in milliseconds
|
|
174
|
+
*/
|
|
175
|
+
getRTT() {
|
|
176
|
+
return this.rtt;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Close the muxer
|
|
180
|
+
*/
|
|
181
|
+
async close(options = {}) {
|
|
182
|
+
if (this.status !== 'open') {
|
|
183
|
+
// already closed
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
const reason = options?.reason ?? GoAwayCode.NormalTermination;
|
|
188
|
+
this.log.trace('muxer close reason=%s', GoAwayCode[reason]);
|
|
189
|
+
await super.close(options);
|
|
190
|
+
// send reason to the other side, allow the other side to close gracefully
|
|
191
|
+
this.sendGoAway(reason);
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
this.keepAlive?.stop();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
abort(err) {
|
|
198
|
+
if (this.status !== 'open') {
|
|
199
|
+
// already closed
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
super.abort(err);
|
|
204
|
+
let reason = GoAwayCode.InternalError;
|
|
205
|
+
if (isProtocolError(err)) {
|
|
206
|
+
reason = err.reason;
|
|
207
|
+
}
|
|
208
|
+
// If reason was provided, use that, otherwise use the presence of `err` to determine the reason
|
|
209
|
+
this.log.error('muxer abort reason=%s error=%s', reason, err);
|
|
210
|
+
// send reason to the other side, allow the other side to close gracefully
|
|
211
|
+
this.sendGoAway(reason);
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
this.keepAlive?.stop();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
onTransportClosed() {
|
|
218
|
+
try {
|
|
219
|
+
super.onTransportClosed();
|
|
220
|
+
}
|
|
221
|
+
finally {
|
|
222
|
+
this.keepAlive?.stop();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/** Create a new stream */
|
|
226
|
+
_newStream(streamId, state, direction) {
|
|
227
|
+
if (this.streams.find(s => s.streamId === streamId) != null) {
|
|
228
|
+
throw new InvalidParametersError('Stream already exists with that id');
|
|
229
|
+
}
|
|
230
|
+
const stream = new YamuxStream({
|
|
231
|
+
...this.streamOptions,
|
|
232
|
+
id: `${streamId}`,
|
|
233
|
+
streamId,
|
|
234
|
+
state,
|
|
235
|
+
direction,
|
|
236
|
+
sendFrame: this.sendFrame.bind(this),
|
|
237
|
+
log: this.log.newScope(`${direction}:${streamId}`),
|
|
238
|
+
getRTT: this.getRTT.bind(this)
|
|
239
|
+
});
|
|
240
|
+
stream.addEventListener('close', () => {
|
|
241
|
+
this.closeStream(streamId);
|
|
242
|
+
}, {
|
|
243
|
+
once: true
|
|
244
|
+
});
|
|
245
|
+
return stream;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* closeStream is used to close a stream once both sides have
|
|
249
|
+
* issued a close.
|
|
250
|
+
*/
|
|
251
|
+
closeStream(id) {
|
|
252
|
+
if (this.client === (id % 2 === 0)) {
|
|
253
|
+
this.numInboundStreams--;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
this.numOutboundStreams--;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
handleFrame(frame) {
|
|
260
|
+
const { streamID, type, length } = frame.header;
|
|
261
|
+
this.log.trace('received frame %o', debugFrame(frame.header));
|
|
262
|
+
if (streamID === 0) {
|
|
263
|
+
switch (type) {
|
|
264
|
+
case FrameType.Ping:
|
|
265
|
+
{
|
|
266
|
+
this.handlePing(frame.header);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
case FrameType.GoAway:
|
|
270
|
+
{
|
|
271
|
+
this.handleGoAway(length);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
default:
|
|
275
|
+
// Invalid state
|
|
276
|
+
throw new InvalidFrameError('Invalid frame type');
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
switch (frame.header.type) {
|
|
281
|
+
case FrameType.Data:
|
|
282
|
+
case FrameType.WindowUpdate:
|
|
283
|
+
{
|
|
284
|
+
this.handleStreamMessage(frame);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
default:
|
|
288
|
+
// Invalid state
|
|
289
|
+
throw new InvalidFrameError('Invalid frame type');
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
handlePing(header) {
|
|
294
|
+
// If the ping is initiated by the sender, send a response
|
|
295
|
+
if (header.flag === Flag.SYN) {
|
|
296
|
+
this.log.trace('received ping request pingId=%s', header.length);
|
|
297
|
+
this.sendPing(header.length, Flag.ACK);
|
|
298
|
+
}
|
|
299
|
+
else if (header.flag === Flag.ACK) {
|
|
300
|
+
this.log.trace('received ping response pingId=%s', header.length);
|
|
301
|
+
this.handlePingResponse(header.length);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
// Invalid state
|
|
305
|
+
throw new InvalidFrameError('Invalid frame flag');
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
handlePingResponse(pingId) {
|
|
309
|
+
if (this.activePing === undefined) {
|
|
310
|
+
// this ping was not requested
|
|
311
|
+
throw new UnRequestedPingError('ping not requested');
|
|
312
|
+
}
|
|
313
|
+
if (this.activePing.id !== pingId) {
|
|
314
|
+
// this ping doesn't match our active ping request
|
|
315
|
+
throw new NotMatchingPingError('ping doesn\'t match our id');
|
|
316
|
+
}
|
|
317
|
+
// valid ping response
|
|
318
|
+
this.activePing.resolve(Date.now() - this.activePing.start);
|
|
319
|
+
}
|
|
320
|
+
handleGoAway(reason) {
|
|
321
|
+
this.log.trace('received GoAway reason=%s', GoAwayCode[reason] ?? 'unknown');
|
|
322
|
+
this.remoteGoAway = reason;
|
|
323
|
+
if (reason === GoAwayCode.NormalTermination) {
|
|
324
|
+
this.onTransportClosed();
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
// reset any streams that are still open and close the muxer
|
|
328
|
+
this.abort(new Error('Remote sent GoAway'));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
handleStreamMessage(frame) {
|
|
332
|
+
const { streamID, flag, type } = frame.header;
|
|
333
|
+
if ((flag & Flag.SYN) === Flag.SYN) {
|
|
334
|
+
this.incomingStream(streamID);
|
|
335
|
+
}
|
|
336
|
+
const stream = this.streams.find(s => s.streamId === streamID);
|
|
337
|
+
if (stream === undefined) {
|
|
338
|
+
this.log.trace('frame for missing stream id=%s', streamID);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
switch (type) {
|
|
342
|
+
case FrameType.WindowUpdate: {
|
|
343
|
+
stream.handleWindowUpdate(frame);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
case FrameType.Data: {
|
|
347
|
+
stream.handleData(frame);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
default:
|
|
351
|
+
throw new Error('unreachable');
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
incomingStream(id) {
|
|
355
|
+
if (this.client !== (id % 2 === 0)) {
|
|
356
|
+
throw new InvalidParametersError('Both endpoints are clients');
|
|
357
|
+
}
|
|
358
|
+
if (this.streams.find(s => s.streamId === id)) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
this.log.trace('new incoming stream id=%s', id);
|
|
362
|
+
if (this.localGoAway !== undefined) {
|
|
363
|
+
// reject (reset) immediately if we are doing a go away
|
|
364
|
+
this.sendFrame({
|
|
365
|
+
type: FrameType.WindowUpdate,
|
|
366
|
+
flag: Flag.RST,
|
|
367
|
+
streamID: id,
|
|
368
|
+
length: 0
|
|
369
|
+
});
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
// check against our configured maximum number of inbound streams
|
|
373
|
+
if (this.numInboundStreams >= this.maxInboundStreams) {
|
|
374
|
+
this.log('maxIncomingStreams exceeded, forcing stream reset');
|
|
375
|
+
this.sendFrame({
|
|
376
|
+
type: FrameType.WindowUpdate,
|
|
377
|
+
flag: Flag.RST,
|
|
378
|
+
streamID: id,
|
|
379
|
+
length: 0
|
|
380
|
+
});
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
// allocate a new stream
|
|
384
|
+
const stream = this._newStream(id, StreamState.SYNReceived, 'inbound');
|
|
385
|
+
this.numInboundStreams++;
|
|
386
|
+
// the stream should now be tracked
|
|
387
|
+
this.onRemoteStream(stream);
|
|
388
|
+
}
|
|
389
|
+
sendFrame(header, data) {
|
|
390
|
+
let encoded;
|
|
391
|
+
if (header.type === FrameType.Data) {
|
|
392
|
+
if (data == null) {
|
|
393
|
+
throw new InvalidFrameError('Invalid frame');
|
|
394
|
+
}
|
|
395
|
+
encoded = new Uint8ArrayList(encodeHeader(header), data);
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
encoded = encodeHeader(header);
|
|
399
|
+
}
|
|
400
|
+
this.log.trace('sending frame %o', debugFrame(header));
|
|
401
|
+
return this.send(encoded);
|
|
402
|
+
}
|
|
403
|
+
sendPing(pingId, flag = Flag.SYN) {
|
|
404
|
+
if (flag === Flag.SYN) {
|
|
405
|
+
this.log.trace('sending ping request pingId=%s', pingId);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
this.log.trace('sending ping response pingId=%s', pingId);
|
|
409
|
+
}
|
|
410
|
+
this.sendFrame({
|
|
411
|
+
type: FrameType.Ping,
|
|
412
|
+
flag,
|
|
413
|
+
streamID: 0,
|
|
414
|
+
length: pingId
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
sendGoAway(reason = GoAwayCode.NormalTermination) {
|
|
418
|
+
this.log('sending GoAway reason=%s', GoAwayCode[reason]);
|
|
419
|
+
this.localGoAway = reason;
|
|
420
|
+
this.sendFrame({
|
|
421
|
+
type: FrameType.GoAway,
|
|
422
|
+
flag: 0,
|
|
423
|
+
streamID: 0,
|
|
424
|
+
length: reason
|
|
425
|
+
});
|
|
426
|
+
}
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,EAAE,IAAI,CAAC,CAAA;QAC1D,CAAC;aAAM,CAAC;YACN,OAAO,GAAG,YAAY,CAAC,MAAM,CAAC,CAAA;QAChC,CAAC;QAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,kBAAkB,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,CAAA;QAEtD,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAC3B,CAAC;IAEO,QAAQ,CAAE,MAAc,EAAE,OAAa,IAAI,CAAC,GAAG;QACrD,IAAI,IAAI,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC;YACtB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,gCAAgC,EAAE,MAAM,CAAC,CAAA;QAC1D,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,iCAAiC,EAAE,MAAM,CAAC,CAAA;QAC3D,CAAC;QACD,IAAI,CAAC,SAAS,CAAC;YACb,IAAI,EAAE,SAAS,CAAC,IAAI;YACpB,IAAI;YACJ,QAAQ,EAAE,CAAC;YACX,MAAM,EAAE,MAAM;SACf,CAAC,CAAA;IACJ,CAAC;IAEO,UAAU,CAAE,SAAqB,UAAU,CAAC,iBAAiB;QACnE,IAAI,CAAC,GAAG,CAAC,0BAA0B,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC,CAAA;QACxD,IAAI,CAAC,WAAW,GAAG,MAAM,CAAA;QACzB,IAAI,CAAC,SAAS,CAAC;YACb,IAAI,EAAE,SAAS,CAAC,MAAM;YACtB,IAAI,EAAE,CAAC;YACP,QAAQ,EAAE,CAAC;YACX,MAAM,EAAE,MAAM;SACf,CAAC,CAAA;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { AbstractStream } from '@libp2p/utils';
|
|
2
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
3
|
+
import type { Frame } from './decode.ts';
|
|
4
|
+
import type { FrameHeader } from './frame.js';
|
|
5
|
+
import type { AbortOptions } from '@libp2p/interface';
|
|
6
|
+
import type { AbstractStreamInit, SendResult } from '@libp2p/utils';
|
|
7
|
+
export declare enum StreamState {
|
|
8
|
+
Init = 0,
|
|
9
|
+
SYNSent = 1,
|
|
10
|
+
SYNReceived = 2,
|
|
11
|
+
Established = 3,
|
|
12
|
+
Finished = 4,
|
|
13
|
+
Paused = 5
|
|
14
|
+
}
|
|
15
|
+
export interface YamuxStreamInit extends AbstractStreamInit {
|
|
16
|
+
streamId: number;
|
|
17
|
+
sendFrame(header: FrameHeader, body?: Uint8ArrayList): boolean;
|
|
18
|
+
getRTT(): number;
|
|
19
|
+
initialStreamWindowSize?: number;
|
|
20
|
+
maxMessageSize?: number;
|
|
21
|
+
maxStreamWindowSize?: number;
|
|
22
|
+
state: StreamState;
|
|
23
|
+
}
|
|
24
|
+
/** YamuxStream is used to represent a logical stream within a session */
|
|
25
|
+
export declare class YamuxStream extends AbstractStream {
|
|
26
|
+
streamId: number;
|
|
27
|
+
state: StreamState;
|
|
28
|
+
/** The number of available bytes to send */
|
|
29
|
+
private sendWindowCapacity;
|
|
30
|
+
/** The number of bytes available to receive in a full window */
|
|
31
|
+
private recvWindow;
|
|
32
|
+
/** The number of available bytes to receive */
|
|
33
|
+
private recvWindowCapacity;
|
|
34
|
+
private maxStreamWindowSize;
|
|
35
|
+
/**
|
|
36
|
+
* An 'epoch' is the time it takes to process and read data
|
|
37
|
+
*
|
|
38
|
+
* Used in conjunction with RTT to determine whether to increase the recvWindow
|
|
39
|
+
*/
|
|
40
|
+
private epochStart;
|
|
41
|
+
private readonly getRTT;
|
|
42
|
+
private readonly sendFrame;
|
|
43
|
+
constructor(init: YamuxStreamInit);
|
|
44
|
+
/**
|
|
45
|
+
* Send a data message to the remote muxer
|
|
46
|
+
*/
|
|
47
|
+
sendData(buf: Uint8ArrayList): SendResult;
|
|
48
|
+
/**
|
|
49
|
+
* Send a reset message to the remote muxer
|
|
50
|
+
*/
|
|
51
|
+
sendReset(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Send a message to the remote muxer, informing them no more data messages
|
|
54
|
+
* will be sent by this end of the stream
|
|
55
|
+
*/
|
|
56
|
+
sendCloseWrite(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Send a message to the remote muxer, informing them no more data messages
|
|
59
|
+
* will be read by this end of the stream - this is a no-op on Yamux streams
|
|
60
|
+
*/
|
|
61
|
+
sendCloseRead(options?: AbortOptions): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Stop sending window updates temporarily - in the interim the the remote
|
|
64
|
+
* send window will exhaust and the remote will stop sending data
|
|
65
|
+
*/
|
|
66
|
+
sendPause(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Start sending window updates as normal
|
|
69
|
+
*/
|
|
70
|
+
sendResume(): void;
|
|
71
|
+
/**
|
|
72
|
+
* handleWindowUpdate is called when the stream receives a window update frame
|
|
73
|
+
*/
|
|
74
|
+
handleWindowUpdate(frame: Frame): void;
|
|
75
|
+
/**
|
|
76
|
+
* handleData is called when the stream receives a data frame
|
|
77
|
+
*/
|
|
78
|
+
handleData(frame: Frame): void;
|
|
79
|
+
/**
|
|
80
|
+
* processFlags is used to update the state of the stream based on set flags, if any.
|
|
81
|
+
*/
|
|
82
|
+
private processFlags;
|
|
83
|
+
/**
|
|
84
|
+
* getSendFlags determines any flags that are appropriate
|
|
85
|
+
* based on the current stream state.
|
|
86
|
+
*
|
|
87
|
+
* The state is updated as a side-effect.
|
|
88
|
+
*/
|
|
89
|
+
private getSendFlags;
|
|
90
|
+
/**
|
|
91
|
+
* Potentially sends a window update enabling further remote writes to take
|
|
92
|
+
* place.
|
|
93
|
+
*/
|
|
94
|
+
sendWindowUpdate(): void;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,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