@libp2p/interface 0.1.2-c4eff4c5 → 0.1.2-c97dea04
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/src/connection/index.d.ts +3 -3
- package/dist/src/stream-muxer/stream.d.ts +6 -0
- package/dist/src/stream-muxer/stream.d.ts.map +1 -1
- package/dist/src/stream-muxer/stream.js +31 -19
- package/dist/src/stream-muxer/stream.js.map +1 -1
- package/package.json +7 -2
- package/src/connection/index.ts +3 -3
- package/src/stream-muxer/stream.ts +45 -23
|
@@ -125,7 +125,7 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
|
|
|
125
125
|
*/
|
|
126
126
|
timeline: StreamTimeline;
|
|
127
127
|
/**
|
|
128
|
-
*
|
|
128
|
+
* The protocol negotiated for this stream
|
|
129
129
|
*/
|
|
130
130
|
protocol?: string;
|
|
131
131
|
/**
|
|
@@ -195,11 +195,11 @@ export interface Connection {
|
|
|
195
195
|
*/
|
|
196
196
|
timeline: ConnectionTimeline;
|
|
197
197
|
/**
|
|
198
|
-
*
|
|
198
|
+
* The multiplexer negotiated for this connection
|
|
199
199
|
*/
|
|
200
200
|
multiplexer?: string;
|
|
201
201
|
/**
|
|
202
|
-
*
|
|
202
|
+
* The encryption protocol negotiated for this connection
|
|
203
203
|
*/
|
|
204
204
|
encryption?: string;
|
|
205
205
|
/**
|
|
@@ -50,6 +50,11 @@ export interface AbstractStreamInit {
|
|
|
50
50
|
* connection when closing the writable end of the stream. (default: 500)
|
|
51
51
|
*/
|
|
52
52
|
closeTimeout?: number;
|
|
53
|
+
/**
|
|
54
|
+
* After the stream sink has closed, a limit on how long it takes to send
|
|
55
|
+
* a close-write message to the remote peer.
|
|
56
|
+
*/
|
|
57
|
+
sendCloseWriteTimeout?: number;
|
|
53
58
|
}
|
|
54
59
|
export declare abstract class AbstractStream implements Stream {
|
|
55
60
|
id: string;
|
|
@@ -70,6 +75,7 @@ export declare abstract class AbstractStream implements Stream {
|
|
|
70
75
|
private readonly onCloseWrite?;
|
|
71
76
|
private readonly onReset?;
|
|
72
77
|
private readonly onAbort?;
|
|
78
|
+
private readonly sendCloseWriteTimeout;
|
|
73
79
|
protected readonly log: Logger;
|
|
74
80
|
constructor(init: AbstractStreamInit);
|
|
75
81
|
sink(source: Source<Uint8ArrayList | Uint8Array>): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AACtH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAG7C,UAAU,MAAM;IACd,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;IACtC,KAAK,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;IAC/C,KAAK,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;IAC/C,OAAO,EAAE,OAAO,CAAA;CACjB;AAMD,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,SAAS,EAAE,SAAS,CAAA;IAEpB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IAEX;;OAEG;IACH,QAAQ,CA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,OAAO,CAAC,IAAI,CAAC;IAErE;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAE,GAAG,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAErF;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEjE;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEtE;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CACtE"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { abortableSource } from 'abortable-iterator';
|
|
2
2
|
import { pushable } from 'it-pushable';
|
|
3
3
|
import defer, {} from 'p-defer';
|
|
4
|
+
import { raceSignal } from 'race-signal';
|
|
4
5
|
import { Uint8ArrayList } from 'uint8arraylist';
|
|
5
6
|
import { CodeError } from '../errors.js';
|
|
6
7
|
const ERR_STREAM_RESET = 'ERR_STREAM_RESET';
|
|
7
8
|
const ERR_SINK_INVALID_STATE = 'ERR_SINK_INVALID_STATE';
|
|
9
|
+
const DEFAULT_SEND_CLOSE_WRITE_TIMEOUT = 5000;
|
|
8
10
|
function isPromise(res) {
|
|
9
11
|
return res != null && typeof res.then === 'function';
|
|
10
12
|
}
|
|
@@ -27,6 +29,7 @@ export class AbstractStream {
|
|
|
27
29
|
onCloseWrite;
|
|
28
30
|
onReset;
|
|
29
31
|
onAbort;
|
|
32
|
+
sendCloseWriteTimeout;
|
|
30
33
|
log;
|
|
31
34
|
constructor(init) {
|
|
32
35
|
this.sinkController = new AbortController();
|
|
@@ -42,6 +45,7 @@ export class AbstractStream {
|
|
|
42
45
|
this.timeline = {
|
|
43
46
|
open: Date.now()
|
|
44
47
|
};
|
|
48
|
+
this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT;
|
|
45
49
|
this.onEnd = init.onEnd;
|
|
46
50
|
this.onCloseRead = init?.onCloseRead;
|
|
47
51
|
this.onCloseWrite = init?.onCloseWrite;
|
|
@@ -55,7 +59,6 @@ export class AbstractStream {
|
|
|
55
59
|
else {
|
|
56
60
|
this.log.trace('source ended');
|
|
57
61
|
}
|
|
58
|
-
this.readStatus = 'closed';
|
|
59
62
|
this.onSourceEnd(err);
|
|
60
63
|
}
|
|
61
64
|
});
|
|
@@ -88,10 +91,15 @@ export class AbstractStream {
|
|
|
88
91
|
await res;
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
|
-
this.log.trace('sink finished reading from source');
|
|
92
|
-
this.writeStatus
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
this.log.trace('sink finished reading from source, write status is "%s"', this.writeStatus);
|
|
95
|
+
if (this.writeStatus === 'writing') {
|
|
96
|
+
this.writeStatus = 'closing';
|
|
97
|
+
this.log.trace('send close write to remote');
|
|
98
|
+
await this.sendCloseWrite({
|
|
99
|
+
signal: AbortSignal.timeout(this.sendCloseWriteTimeout)
|
|
100
|
+
});
|
|
101
|
+
this.writeStatus = 'closed';
|
|
102
|
+
}
|
|
95
103
|
this.onSinkEnd();
|
|
96
104
|
}
|
|
97
105
|
catch (err) {
|
|
@@ -109,6 +117,7 @@ export class AbstractStream {
|
|
|
109
117
|
return;
|
|
110
118
|
}
|
|
111
119
|
this.timeline.closeRead = Date.now();
|
|
120
|
+
this.readStatus = 'closed';
|
|
112
121
|
if (err != null && this.endErr == null) {
|
|
113
122
|
this.endErr = err;
|
|
114
123
|
}
|
|
@@ -116,6 +125,9 @@ export class AbstractStream {
|
|
|
116
125
|
if (this.timeline.closeWrite != null) {
|
|
117
126
|
this.log.trace('source and sink ended');
|
|
118
127
|
this.timeline.close = Date.now();
|
|
128
|
+
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
129
|
+
this.status = 'closed';
|
|
130
|
+
}
|
|
119
131
|
if (this.onEnd != null) {
|
|
120
132
|
this.onEnd(this.endErr);
|
|
121
133
|
}
|
|
@@ -129,6 +141,7 @@ export class AbstractStream {
|
|
|
129
141
|
return;
|
|
130
142
|
}
|
|
131
143
|
this.timeline.closeWrite = Date.now();
|
|
144
|
+
this.writeStatus = 'closed';
|
|
132
145
|
if (err != null && this.endErr == null) {
|
|
133
146
|
this.endErr = err;
|
|
134
147
|
}
|
|
@@ -136,6 +149,9 @@ export class AbstractStream {
|
|
|
136
149
|
if (this.timeline.closeRead != null) {
|
|
137
150
|
this.log.trace('sink and source ended');
|
|
138
151
|
this.timeline.close = Date.now();
|
|
152
|
+
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
153
|
+
this.status = 'closed';
|
|
154
|
+
}
|
|
139
155
|
if (this.onEnd != null) {
|
|
140
156
|
this.onEnd(this.endErr);
|
|
141
157
|
}
|
|
@@ -162,14 +178,14 @@ export class AbstractStream {
|
|
|
162
178
|
this.log.trace('closing readable end of stream with starting read status "%s"', this.readStatus);
|
|
163
179
|
const readStatus = this.readStatus;
|
|
164
180
|
this.readStatus = 'closing';
|
|
165
|
-
if (readStatus === 'ready') {
|
|
166
|
-
this.log.trace('ending internal source queue');
|
|
167
|
-
this.streamSource.end();
|
|
168
|
-
}
|
|
169
181
|
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
|
|
170
182
|
this.log.trace('send close read to remote');
|
|
171
183
|
await this.sendCloseRead(options);
|
|
172
184
|
}
|
|
185
|
+
if (readStatus === 'ready') {
|
|
186
|
+
this.log.trace('ending internal source queue');
|
|
187
|
+
this.streamSource.end();
|
|
188
|
+
}
|
|
173
189
|
this.log.trace('closed readable end of stream');
|
|
174
190
|
}
|
|
175
191
|
async closeWrite(options = {}) {
|
|
@@ -177,13 +193,11 @@ export class AbstractStream {
|
|
|
177
193
|
return;
|
|
178
194
|
}
|
|
179
195
|
this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus);
|
|
180
|
-
const writeStatus = this.writeStatus;
|
|
181
196
|
if (this.writeStatus === 'ready') {
|
|
182
197
|
this.log.trace('sink was never sunk, sink an empty array');
|
|
183
|
-
await this.sink([]);
|
|
198
|
+
await raceSignal(this.sink([]), options.signal);
|
|
184
199
|
}
|
|
185
|
-
this.writeStatus
|
|
186
|
-
if (writeStatus === 'writing') {
|
|
200
|
+
if (this.writeStatus === 'writing') {
|
|
187
201
|
// stop reading from the source passed to `.sink` in the microtask queue
|
|
188
202
|
// - this lets any data queued by the user in the current tick get read
|
|
189
203
|
// before we exit
|
|
@@ -191,14 +205,11 @@ export class AbstractStream {
|
|
|
191
205
|
queueMicrotask(() => {
|
|
192
206
|
this.log.trace('aborting source passed to .sink');
|
|
193
207
|
this.sinkController.abort();
|
|
194
|
-
this.sinkEnd.promise
|
|
208
|
+
raceSignal(this.sinkEnd.promise, options.signal)
|
|
209
|
+
.then(resolve, reject);
|
|
195
210
|
});
|
|
196
211
|
});
|
|
197
212
|
}
|
|
198
|
-
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeWrite == null) {
|
|
199
|
-
this.log.trace('send close write to remote');
|
|
200
|
-
await this.sendCloseWrite(options);
|
|
201
|
-
}
|
|
202
213
|
this.writeStatus = 'closed';
|
|
203
214
|
this.log.trace('closed writable end of stream');
|
|
204
215
|
}
|
|
@@ -234,6 +245,7 @@ export class AbstractStream {
|
|
|
234
245
|
}
|
|
235
246
|
const err = new CodeError('stream reset', ERR_STREAM_RESET);
|
|
236
247
|
this.status = 'reset';
|
|
248
|
+
this.timeline.reset = Date.now();
|
|
237
249
|
this._closeSinkAndSource(err);
|
|
238
250
|
this.onReset?.();
|
|
239
251
|
}
|
|
@@ -290,7 +302,7 @@ export class AbstractStream {
|
|
|
290
302
|
this.log('received destroy but we are already closed');
|
|
291
303
|
return;
|
|
292
304
|
}
|
|
293
|
-
this.log.trace('
|
|
305
|
+
this.log.trace('stream destroyed');
|
|
294
306
|
this._closeSinkAndSource();
|
|
295
307
|
}
|
|
296
308
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAiB,QAAQ,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,KAAK,EAAE,EAAwB,MAAM,SAAS,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAiB,QAAQ,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,KAAK,EAAE,EAAwB,MAAM,SAAS,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAaxC,MAAM,gBAAgB,GAAG,kBAAkB,CAAA;AAC3C,MAAM,sBAAsB,GAAG,wBAAwB,CAAA;AACvD,MAAM,gCAAgC,GAAG,IAAI,CAAA;AA6D7C,SAAS,SAAS,CAAE,GAAS;IAC3B,OAAO,GAAG,IAAI,IAAI,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,UAAU,CAAA;AACtD,CAAC;AAED,MAAM,OAAgB,cAAc;IAC3B,EAAE,CAAQ;IACV,SAAS,CAAW;IACpB,QAAQ,CAAgB;IACxB,QAAQ,CAAS;IACjB,QAAQ,CAAyB;IACjC,MAAM,CAA+C;IACrD,MAAM,CAA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I,CAAC,YAAY,EAAE,CAAA;IACrB,CAAC;IAED;;;OAGG;IACH,eAAe;QACb,IAAI,IAAI,CAAC,WAAW,KAAK,SAAS,IAAI,IAAI,CAAC,WAAW,KAAK,QAAQ,EAAE;YACnE,IAAI,CAAC,GAAG,CAAC,6DAA6D,CAAC,CAAA;YACvE,OAAM;SACP;QAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAA;QACnC,IAAI,CAAC,UAAU,EAAE,CAAA;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,IAAI,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,OAAO,EAAE;YACpF,IAAI,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAA;YACtD,OAAM;SACP;QAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAA;QAElC,IAAI,CAAC,mBAAmB,EAAE,CAAA;IAC5B,CAAC;IAED;;;OAGG;IACH,UAAU,CAAE,IAAoB;QAC9B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;IAED;;;OAGG;IACH,oBAAoB;QAClB,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,CAAA;IACzC,CAAC;CA6BF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/interface",
|
|
3
|
-
"version": "0.1.2-
|
|
3
|
+
"version": "0.1.2-c97dea04",
|
|
4
4
|
"description": "The interface implemented by a libp2p node",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/interface#readme",
|
|
@@ -140,6 +140,7 @@
|
|
|
140
140
|
"eslintConfig": {
|
|
141
141
|
"extends": "ipfs",
|
|
142
142
|
"parserOptions": {
|
|
143
|
+
"project": true,
|
|
143
144
|
"sourceType": "module"
|
|
144
145
|
}
|
|
145
146
|
},
|
|
@@ -163,12 +164,16 @@
|
|
|
163
164
|
"it-stream-types": "^2.0.1",
|
|
164
165
|
"multiformats": "^12.0.1",
|
|
165
166
|
"p-defer": "^4.0.0",
|
|
167
|
+
"race-signal": "^1.0.0",
|
|
166
168
|
"uint8arraylist": "^2.4.3"
|
|
167
169
|
},
|
|
168
170
|
"devDependencies": {
|
|
169
171
|
"@types/sinon": "^10.0.15",
|
|
170
172
|
"aegir": "^40.0.8",
|
|
171
|
-
"
|
|
173
|
+
"delay": "^6.0.0",
|
|
174
|
+
"it-all": "^3.0.3",
|
|
175
|
+
"it-drain": "^3.0.3",
|
|
176
|
+
"sinon": "^16.0.0",
|
|
172
177
|
"sinon-ts": "^1.0.0"
|
|
173
178
|
}
|
|
174
179
|
}
|
package/src/connection/index.ts
CHANGED
|
@@ -146,7 +146,7 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
|
|
|
146
146
|
timeline: StreamTimeline
|
|
147
147
|
|
|
148
148
|
/**
|
|
149
|
-
*
|
|
149
|
+
* The protocol negotiated for this stream
|
|
150
150
|
*/
|
|
151
151
|
protocol?: string
|
|
152
152
|
|
|
@@ -231,12 +231,12 @@ export interface Connection {
|
|
|
231
231
|
timeline: ConnectionTimeline
|
|
232
232
|
|
|
233
233
|
/**
|
|
234
|
-
*
|
|
234
|
+
* The multiplexer negotiated for this connection
|
|
235
235
|
*/
|
|
236
236
|
multiplexer?: string
|
|
237
237
|
|
|
238
238
|
/**
|
|
239
|
-
*
|
|
239
|
+
* The encryption protocol negotiated for this connection
|
|
240
240
|
*/
|
|
241
241
|
encryption?: string
|
|
242
242
|
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { abortableSource } from 'abortable-iterator'
|
|
2
2
|
import { type Pushable, pushable } from 'it-pushable'
|
|
3
3
|
import defer, { type DeferredPromise } from 'p-defer'
|
|
4
|
+
import { raceSignal } from 'race-signal'
|
|
4
5
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
6
|
import { CodeError } from '../errors.js'
|
|
6
7
|
import type { Direction, ReadStatus, Stream, StreamStatus, StreamTimeline, WriteStatus } from '../connection/index.js'
|
|
7
8
|
import type { AbortOptions } from '../index.js'
|
|
8
9
|
import type { Source } from 'it-stream-types'
|
|
9
10
|
|
|
11
|
+
// copied from @libp2p/logger to break a circular dependency
|
|
10
12
|
interface Logger {
|
|
11
13
|
(formatter: any, ...args: any[]): void
|
|
12
14
|
error: (formatter: any, ...args: any[]) => void
|
|
@@ -16,6 +18,7 @@ interface Logger {
|
|
|
16
18
|
|
|
17
19
|
const ERR_STREAM_RESET = 'ERR_STREAM_RESET'
|
|
18
20
|
const ERR_SINK_INVALID_STATE = 'ERR_SINK_INVALID_STATE'
|
|
21
|
+
const DEFAULT_SEND_CLOSE_WRITE_TIMEOUT = 5000
|
|
19
22
|
|
|
20
23
|
export interface AbstractStreamInit {
|
|
21
24
|
/**
|
|
@@ -68,6 +71,12 @@ export interface AbstractStreamInit {
|
|
|
68
71
|
* connection when closing the writable end of the stream. (default: 500)
|
|
69
72
|
*/
|
|
70
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
|
|
71
80
|
}
|
|
72
81
|
|
|
73
82
|
function isPromise (res?: any): res is Promise<void> {
|
|
@@ -94,6 +103,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
94
103
|
private readonly onCloseWrite?: () => void
|
|
95
104
|
private readonly onReset?: () => void
|
|
96
105
|
private readonly onAbort?: (err: Error) => void
|
|
106
|
+
private readonly sendCloseWriteTimeout: number
|
|
97
107
|
|
|
98
108
|
protected readonly log: Logger
|
|
99
109
|
|
|
@@ -113,6 +123,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
113
123
|
this.timeline = {
|
|
114
124
|
open: Date.now()
|
|
115
125
|
}
|
|
126
|
+
this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT
|
|
116
127
|
|
|
117
128
|
this.onEnd = init.onEnd
|
|
118
129
|
this.onCloseRead = init?.onCloseRead
|
|
@@ -128,7 +139,6 @@ export abstract class AbstractStream implements Stream {
|
|
|
128
139
|
this.log.trace('source ended')
|
|
129
140
|
}
|
|
130
141
|
|
|
131
|
-
this.readStatus = 'closed'
|
|
132
142
|
this.onSourceEnd(err)
|
|
133
143
|
}
|
|
134
144
|
})
|
|
@@ -173,11 +183,19 @@ export abstract class AbstractStream implements Stream {
|
|
|
173
183
|
}
|
|
174
184
|
}
|
|
175
185
|
|
|
176
|
-
this.log.trace('sink finished reading from source')
|
|
177
|
-
|
|
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
|
+
}
|
|
178
198
|
|
|
179
|
-
this.log.trace('sink calling closeWrite')
|
|
180
|
-
await this.closeWrite(options)
|
|
181
199
|
this.onSinkEnd()
|
|
182
200
|
} catch (err: any) {
|
|
183
201
|
this.log.trace('sink ended with error, calling abort with error', err)
|
|
@@ -196,6 +214,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
196
214
|
}
|
|
197
215
|
|
|
198
216
|
this.timeline.closeRead = Date.now()
|
|
217
|
+
this.readStatus = 'closed'
|
|
199
218
|
|
|
200
219
|
if (err != null && this.endErr == null) {
|
|
201
220
|
this.endErr = err
|
|
@@ -207,6 +226,10 @@ export abstract class AbstractStream implements Stream {
|
|
|
207
226
|
this.log.trace('source and sink ended')
|
|
208
227
|
this.timeline.close = Date.now()
|
|
209
228
|
|
|
229
|
+
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
230
|
+
this.status = 'closed'
|
|
231
|
+
}
|
|
232
|
+
|
|
210
233
|
if (this.onEnd != null) {
|
|
211
234
|
this.onEnd(this.endErr)
|
|
212
235
|
}
|
|
@@ -221,6 +244,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
221
244
|
}
|
|
222
245
|
|
|
223
246
|
this.timeline.closeWrite = Date.now()
|
|
247
|
+
this.writeStatus = 'closed'
|
|
224
248
|
|
|
225
249
|
if (err != null && this.endErr == null) {
|
|
226
250
|
this.endErr = err
|
|
@@ -232,6 +256,10 @@ export abstract class AbstractStream implements Stream {
|
|
|
232
256
|
this.log.trace('sink and source ended')
|
|
233
257
|
this.timeline.close = Date.now()
|
|
234
258
|
|
|
259
|
+
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
260
|
+
this.status = 'closed'
|
|
261
|
+
}
|
|
262
|
+
|
|
235
263
|
if (this.onEnd != null) {
|
|
236
264
|
this.onEnd(this.endErr)
|
|
237
265
|
}
|
|
@@ -266,16 +294,16 @@ export abstract class AbstractStream implements Stream {
|
|
|
266
294
|
const readStatus = this.readStatus
|
|
267
295
|
this.readStatus = 'closing'
|
|
268
296
|
|
|
269
|
-
if (readStatus === 'ready') {
|
|
270
|
-
this.log.trace('ending internal source queue')
|
|
271
|
-
this.streamSource.end()
|
|
272
|
-
}
|
|
273
|
-
|
|
274
297
|
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
|
|
275
298
|
this.log.trace('send close read to remote')
|
|
276
299
|
await this.sendCloseRead(options)
|
|
277
300
|
}
|
|
278
301
|
|
|
302
|
+
if (readStatus === 'ready') {
|
|
303
|
+
this.log.trace('ending internal source queue')
|
|
304
|
+
this.streamSource.end()
|
|
305
|
+
}
|
|
306
|
+
|
|
279
307
|
this.log.trace('closed readable end of stream')
|
|
280
308
|
}
|
|
281
309
|
|
|
@@ -286,16 +314,13 @@ export abstract class AbstractStream implements Stream {
|
|
|
286
314
|
|
|
287
315
|
this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus)
|
|
288
316
|
|
|
289
|
-
const writeStatus = this.writeStatus
|
|
290
|
-
|
|
291
317
|
if (this.writeStatus === 'ready') {
|
|
292
318
|
this.log.trace('sink was never sunk, sink an empty array')
|
|
293
|
-
await this.sink([])
|
|
294
|
-
}
|
|
295
319
|
|
|
296
|
-
|
|
320
|
+
await raceSignal(this.sink([]), options.signal)
|
|
321
|
+
}
|
|
297
322
|
|
|
298
|
-
if (writeStatus === 'writing') {
|
|
323
|
+
if (this.writeStatus === 'writing') {
|
|
299
324
|
// stop reading from the source passed to `.sink` in the microtask queue
|
|
300
325
|
// - this lets any data queued by the user in the current tick get read
|
|
301
326
|
// before we exit
|
|
@@ -303,16 +328,12 @@ export abstract class AbstractStream implements Stream {
|
|
|
303
328
|
queueMicrotask(() => {
|
|
304
329
|
this.log.trace('aborting source passed to .sink')
|
|
305
330
|
this.sinkController.abort()
|
|
306
|
-
this.sinkEnd.promise
|
|
331
|
+
raceSignal(this.sinkEnd.promise, options.signal)
|
|
332
|
+
.then(resolve, reject)
|
|
307
333
|
})
|
|
308
334
|
})
|
|
309
335
|
}
|
|
310
336
|
|
|
311
|
-
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeWrite == null) {
|
|
312
|
-
this.log.trace('send close write to remote')
|
|
313
|
-
await this.sendCloseWrite(options)
|
|
314
|
-
}
|
|
315
|
-
|
|
316
337
|
this.writeStatus = 'closed'
|
|
317
338
|
|
|
318
339
|
this.log.trace('closed writable end of stream')
|
|
@@ -357,6 +378,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
357
378
|
const err = new CodeError('stream reset', ERR_STREAM_RESET)
|
|
358
379
|
|
|
359
380
|
this.status = 'reset'
|
|
381
|
+
this.timeline.reset = Date.now()
|
|
360
382
|
this._closeSinkAndSource(err)
|
|
361
383
|
this.onReset?.()
|
|
362
384
|
}
|
|
@@ -423,7 +445,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
423
445
|
return
|
|
424
446
|
}
|
|
425
447
|
|
|
426
|
-
this.log.trace('
|
|
448
|
+
this.log.trace('stream destroyed')
|
|
427
449
|
|
|
428
450
|
this._closeSinkAndSource()
|
|
429
451
|
}
|