Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1047db4

Browse files
committedNov 29, 2022
http: Make OutgoingMessage more streamlike
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.
1 parent 5664822 commit 1047db4

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed
 

‎lib/_http_outgoing.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const assert = require('internal/assert');
4545
const EE = require('events');
4646
const Stream = require('stream');
4747
const internalUtil = require('internal/util');
48+
const { kIsWritable } = require('internal/streams/utils');
4849
const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http');
4950
const { Buffer } = require('buffer');
5051
const {
@@ -88,6 +89,7 @@ const kCorked = Symbol('corked');
8889
const kUniqueHeaders = Symbol('kUniqueHeaders');
8990
const kBytesWritten = Symbol('kBytesWritten');
9091
const kEndCalled = Symbol('kEndCalled');
92+
const kErrored = Symbol('errored');
9193

9294
const nop = () => {};
9395

@@ -146,11 +148,29 @@ function OutgoingMessage() {
146148

147149
this._keepAliveTimeout = 0;
148150

149-
this._onPendingData = nop;
151+
this[kErrored] = null;
150152
}
151153
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
152154
ObjectSetPrototypeOf(OutgoingMessage, Stream);
153155

156+
ObjectDefineProperty(OutgoingMessage.prototype, kIsWritable, {
157+
get() {
158+
return !this.destroyed && !this[kErrored] && !this.finished;
159+
}
160+
});
161+
162+
ObjectDefineProperty(OutgoingMessage.prototype, 'errored', {
163+
get() {
164+
return this[kErrored];
165+
}
166+
});
167+
168+
ObjectDefineProperty(OutgoingMessage.prototype, 'closed', {
169+
get() {
170+
return this._closed;
171+
}
172+
});
173+
154174
ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
155175
__proto__: null,
156176
get() {
@@ -320,6 +340,8 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
320340
}
321341
this.destroyed = true;
322342

343+
this[kErrored] = error;
344+
323345
if (this.socket) {
324346
this.socket.destroy(error);
325347
} else {

‎lib/internal/streams/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
const kDestroyed = Symbol('kDestroyed');
1010
const kIsErrored = Symbol('kIsErrored');
1111
const kIsReadable = Symbol('kIsReadable');
12+
const kIsWritable = Symbol('kIsWritable');
1213
const kIsDisturbed = Symbol('kIsDisturbed');
1314

1415
function isReadableNodeStream(obj, strict = false) {
@@ -126,6 +127,7 @@ function isReadable(stream) {
126127
}
127128

128129
function isWritable(stream) {
130+
if (stream && stream[kIsWritable] != null) return stream[kIsWritable];
129131
if (typeof stream?.writable !== 'boolean') return null;
130132
if (isDestroyed(stream)) return false;
131133
return isWritableNodeStream(stream) &&
@@ -262,6 +264,7 @@ function isErrored(stream) {
262264
}
263265

264266
module.exports = {
267+
kIsWritable,
265268
kDestroyed,
266269
isDisturbed,
267270
kIsDisturbed,

‎lib/stream.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const Stream = module.exports = require('internal/streams/legacy').Stream;
5454
Stream.isDisturbed = utils.isDisturbed;
5555
Stream.isErrored = utils.isErrored;
5656
Stream.isReadable = utils.isReadable;
57+
Stream.isWritable = utils.isWritable;
5758
Stream.Readable = require('internal/streams/readable');
5859
for (const key of ObjectKeys(streamReturningOperators)) {
5960
const op = streamReturningOperators[key];

0 commit comments

Comments
 (0)
Please sign in to comment.