Skip to content

Commit

Permalink
http: Make OutgoingMessage more streamlike
Browse files Browse the repository at this point in the history
Implement missing getters error & closed. Add support for
proper "writable" check through isWritable helper.

We cannot fix the OutgoingMessage.writable propery as that
will break the ecosystem.
  • Loading branch information
ronag committed Nov 29, 2022
1 parent 5664822 commit 1047db4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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;
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];
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'closed', {
get() {
return this._closed;
}
});

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

0 comments on commit 1047db4

Please sign in to comment.