Skip to content

Commit 0af46f6

Browse files
committed
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 0af46f6

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/_http_outgoing.js

+24-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,30 @@ 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+
// TODO (ronag): w.ended?
159+
return !this.destroyed && !this[kErrored] && !this.finished;
160+
}
161+
});
162+
163+
ObjectDefineProperty(OutgoingMessage.prototype, 'errored', {
164+
get() {
165+
return this[kErrored];
166+
}
167+
});
168+
169+
ObjectDefineProperty(OutgoingMessage.prototype, 'closed', {
170+
get() {
171+
return this._closed;
172+
}
173+
});
174+
154175
ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
155176
__proto__: null,
156177
get() {
@@ -320,6 +341,8 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
320341
}
321342
this.destroyed = true;
322343

344+
this[kErrored] = error;
345+
323346
if (this.socket) {
324347
this.socket.destroy(error);
325348
} 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,

0 commit comments

Comments
 (0)