Skip to content

Commit da20287

Browse files
committed
quic: simplify QuicStream construction logic
PR-URL: #34351 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 6e30fe7 commit da20287

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

lib/internal/quic/core.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,14 @@ function onStreamReady(streamHandle, id, push_id) {
476476
const stream = new QuicStream({
477477
...session[kStreamOptions],
478478
writable: !(id & 0b10),
479-
}, session, push_id);
480-
stream[kSetHandle](streamHandle);
481-
session[kAddStream](id, stream);
482-
process.nextTick(emit.bind(session, 'stream', stream));
479+
}, session, streamHandle, push_id);
480+
process.nextTick(() => {
481+
try {
482+
session.emit('stream', stream);
483+
} catch (error) {
484+
stream.destroy(error);
485+
}
486+
});
483487
}
484488

485489
// Called by the C++ internals when a stream is closed and
@@ -2188,16 +2192,11 @@ class QuicSession extends EventEmitter {
21882192
if (handle === undefined)
21892193
throw new ERR_OPERATION_FAILED('Unable to create QuicStream');
21902194

2191-
const stream = new QuicStream({
2195+
return new QuicStream({
21922196
highWaterMark,
21932197
defaultEncoding,
21942198
readable: !halfOpen
2195-
}, this);
2196-
2197-
stream[kSetHandle](handle);
2198-
this[kAddStream](stream.id, stream);
2199-
2200-
return stream;
2199+
}, this, handle);
22012200
}
22022201

22032202
get duration() {
@@ -2556,7 +2555,7 @@ class QuicStream extends Duplex {
25562555
stats: undefined,
25572556
};
25582557

2559-
constructor(options, session, push_id) {
2558+
constructor(options, session, handle, push_id) {
25602559
const {
25612560
highWaterMark,
25622561
defaultEncoding,
@@ -2583,11 +2582,7 @@ class QuicStream extends Duplex {
25832582
this._readableState.readingMore = true;
25842583
this.on('pause', streamOnPause);
25852584

2586-
// The QuicStream writes are corked until kSetHandle
2587-
// is set, ensuring that writes are buffered in JavaScript
2588-
// until we have somewhere to send them.
2589-
// TODO(@jasnell): We need a better mechanism for this.
2590-
this.cork();
2585+
this[kSetHandle](handle);
25912586
}
25922587

25932588
// Set handle is called once the QuicSession has been able
@@ -2607,8 +2602,7 @@ class QuicStream extends Duplex {
26072602
state.dataRateHistogram = new Histogram(handle.rate);
26082603
state.dataSizeHistogram = new Histogram(handle.size);
26092604
state.dataAckHistogram = new Histogram(handle.ack);
2610-
this.uncork();
2611-
this.emit('ready');
2605+
state.session[kAddStream](state.id, this);
26122606
} else {
26132607
if (state.dataRateHistogram)
26142608
state.dataRateHistogram[kDestroyHistogram]();
@@ -3008,15 +3002,11 @@ class QuicStream extends Duplex {
30083002
'Push is either disabled or currently blocked.');
30093003
}
30103004

3011-
const stream = new QuicStream({
3005+
return new QuicStream({
30123006
readable: false,
30133007
highWaterMark,
30143008
defaultEncoding,
3015-
}, this.session);
3016-
3017-
stream[kSetHandle](handle);
3018-
this.session[kAddStream](stream.id, stream);
3019-
return stream;
3009+
}, this.session, handle);
30203010
}
30213011

30223012
submitInformationalHeaders(headers = {}) {

0 commit comments

Comments
 (0)