Skip to content

Commit

Permalink
quic: simplify QuicStream construction logic
Browse files Browse the repository at this point in the history
PR-URL: #34351
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jasnell committed Jul 23, 2020
1 parent 6e30fe7 commit da20287
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions lib/internal/quic/core.js
Expand Up @@ -476,10 +476,14 @@ function onStreamReady(streamHandle, id, push_id) {
const stream = new QuicStream({
...session[kStreamOptions],
writable: !(id & 0b10),
}, session, push_id);
stream[kSetHandle](streamHandle);
session[kAddStream](id, stream);
process.nextTick(emit.bind(session, 'stream', stream));
}, session, streamHandle, push_id);
process.nextTick(() => {
try {
session.emit('stream', stream);
} catch (error) {
stream.destroy(error);
}
});
}

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

const stream = new QuicStream({
return new QuicStream({
highWaterMark,
defaultEncoding,
readable: !halfOpen
}, this);

stream[kSetHandle](handle);
this[kAddStream](stream.id, stream);

return stream;
}, this, handle);
}

get duration() {
Expand Down Expand Up @@ -2556,7 +2555,7 @@ class QuicStream extends Duplex {
stats: undefined,
};

constructor(options, session, push_id) {
constructor(options, session, handle, push_id) {
const {
highWaterMark,
defaultEncoding,
Expand All @@ -2583,11 +2582,7 @@ class QuicStream extends Duplex {
this._readableState.readingMore = true;
this.on('pause', streamOnPause);

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

// Set handle is called once the QuicSession has been able
Expand All @@ -2607,8 +2602,7 @@ class QuicStream extends Duplex {
state.dataRateHistogram = new Histogram(handle.rate);
state.dataSizeHistogram = new Histogram(handle.size);
state.dataAckHistogram = new Histogram(handle.ack);
this.uncork();
this.emit('ready');
state.session[kAddStream](state.id, this);
} else {
if (state.dataRateHistogram)
state.dataRateHistogram[kDestroyHistogram]();
Expand Down Expand Up @@ -3008,15 +3002,11 @@ class QuicStream extends Duplex {
'Push is either disabled or currently blocked.');
}

const stream = new QuicStream({
return new QuicStream({
readable: false,
highWaterMark,
defaultEncoding,
}, this.session);

stream[kSetHandle](handle);
this.session[kAddStream](stream.id, stream);
return stream;
}, this.session, handle);
}

submitInformationalHeaders(headers = {}) {
Expand Down

0 comments on commit da20287

Please sign in to comment.