Skip to content

Commit bbaca84

Browse files
debadree25danielleadams
authored andcommitted
lib: allow Writeable.toWeb() to work on http.Outgoing message
Attempted to fix the issue by watering down the condition being checked in internal/streams/utils isWritableNodeStream utility Fixes: #44188 PR-URL: #45642 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent ff03ed1 commit bbaca84

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/internal/webstreams/adapters.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,17 @@ function newWritableStreamFromStreamWritable(streamWritable) {
101101
// here because it will return false if streamWritable is a Duplex
102102
// whose writable option is false. For a Duplex that is not writable,
103103
// we want it to pass this check but return a closed WritableStream.
104-
if (typeof streamWritable?._writableState !== 'object') {
104+
// We check if the given stream is a stream.Writable or http.OutgoingMessage
105+
const checkIfWritableOrOutgoingMessage =
106+
streamWritable &&
107+
typeof streamWritable?.write === 'function' &&
108+
typeof streamWritable?.on === 'function';
109+
if (!checkIfWritableOrOutgoingMessage) {
105110
throw new ERR_INVALID_ARG_TYPE(
106111
'streamWritable',
107112
'stream.Writable',
108-
streamWritable);
113+
streamWritable
114+
);
109115
}
110116

111117
if (isDestroyed(streamWritable) || !isWritable(streamWritable)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Writable } = require('stream');
4+
5+
const assert = require('assert');
6+
const http = require('http');
7+
8+
// Check if Writable.toWeb works on the response object after creating a server.
9+
const server = http.createServer(
10+
common.mustCall((req, res) => {
11+
const webStreamResponse = Writable.toWeb(res);
12+
assert.strictEqual(webStreamResponse instanceof WritableStream, true);
13+
res.end();
14+
})
15+
);
16+
17+
server.listen(
18+
0,
19+
common.mustCall(() => {
20+
http.get(
21+
{
22+
port: server.address().port,
23+
},
24+
common.mustCall(() => {
25+
server.close();
26+
})
27+
);
28+
})
29+
);

0 commit comments

Comments
 (0)