Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: Make OutgoingMessage more streamlike #45672

Merged
merged 10 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/_http_outgoing.js
Expand Up @@ -45,6 +45,7 @@ const assert = require('internal/assert');
const EE = require('events');
const Stream = require('stream');
const internalUtil = require('internal/util');
const { kIsWritable } = require('internal/streams/utils');
const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http');
const { Buffer } = require('buffer');
const {
Expand Down Expand Up @@ -88,6 +89,7 @@ const kCorked = Symbol('corked');
const kUniqueHeaders = Symbol('kUniqueHeaders');
const kBytesWritten = Symbol('kBytesWritten');
const kEndCalled = Symbol('kEndCalled');
const kErrored = Symbol('errored');

const nop = () => {};

Expand Down Expand Up @@ -146,11 +148,29 @@ function OutgoingMessage() {

this._keepAliveTimeout = 0;

this._onPendingData = nop;
ronag marked this conversation as resolved.
Show resolved Hide resolved
this[kErrored] = null;
}
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
ObjectSetPrototypeOf(OutgoingMessage, Stream);

ObjectDefineProperty(OutgoingMessage.prototype, kIsWritable, {
get() {
return !this.destroyed && !this[kErrored] && !this.finished;
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'errored', {
get() {
return this[kErrored];
}
ronag marked this conversation as resolved.
Show resolved Hide resolved
});

ObjectDefineProperty(OutgoingMessage.prototype, 'closed', {
get() {
return this._closed;
}
ronag marked this conversation as resolved.
Show resolved Hide resolved
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
__proto__: null,
get() {
Expand Down Expand Up @@ -320,6 +340,8 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
}
this.destroyed = true;

this[kErrored] = error;

if (this.socket) {
this.socket.destroy(error);
} else {
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/streams/utils.js
Expand Up @@ -9,6 +9,7 @@ const {
const kDestroyed = Symbol('kDestroyed');
const kIsErrored = Symbol('kIsErrored');
const kIsReadable = Symbol('kIsReadable');
const kIsWritable = Symbol('kIsWritable');
const kIsDisturbed = Symbol('kIsDisturbed');

function isReadableNodeStream(obj, strict = false) {
Expand Down Expand Up @@ -126,6 +127,7 @@ function isReadable(stream) {
}

function isWritable(stream) {
if (stream && stream[kIsWritable] != null) return stream[kIsWritable];
if (typeof stream?.writable !== 'boolean') return null;
if (isDestroyed(stream)) return false;
return isWritableNodeStream(stream) &&
Expand Down Expand Up @@ -262,6 +264,7 @@ function isErrored(stream) {
}

module.exports = {
kIsWritable,
kDestroyed,
isDisturbed,
kIsDisturbed,
Expand Down
1 change: 1 addition & 0 deletions lib/stream.js
Expand Up @@ -54,6 +54,7 @@ const Stream = module.exports = require('internal/streams/legacy').Stream;
Stream.isDisturbed = utils.isDisturbed;
Stream.isErrored = utils.isErrored;
Stream.isReadable = utils.isReadable;
Stream.isWritable = utils.isWritable;
Stream.Readable = require('internal/streams/readable');
for (const key of ObjectKeys(streamReturningOperators)) {
const op = streamReturningOperators[key];
Expand Down