Skip to content

Commit

Permalink
lib: allow Writeable.toWeb() to work on http.Outgoing message
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
  • Loading branch information
debadree25 authored and danielleadams committed Jan 3, 2023
1 parent ff03ed1 commit bbaca84
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ function newWritableStreamFromStreamWritable(streamWritable) {
// here because it will return false if streamWritable is a Duplex
// whose writable option is false. For a Duplex that is not writable,
// we want it to pass this check but return a closed WritableStream.
if (typeof streamWritable?._writableState !== 'object') {
// We check if the given stream is a stream.Writable or http.OutgoingMessage
const checkIfWritableOrOutgoingMessage =
streamWritable &&
typeof streamWritable?.write === 'function' &&
typeof streamWritable?.on === 'function';
if (!checkIfWritableOrOutgoingMessage) {
throw new ERR_INVALID_ARG_TYPE(
'streamWritable',
'stream.Writable',
streamWritable);
streamWritable
);
}

if (isDestroyed(streamWritable) || !isWritable(streamWritable)) {
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-stream-toWeb-allows-server-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common');
const { Writable } = require('stream');

const assert = require('assert');
const http = require('http');

// Check if Writable.toWeb works on the response object after creating a server.
const server = http.createServer(
common.mustCall((req, res) => {
const webStreamResponse = Writable.toWeb(res);
assert.strictEqual(webStreamResponse instanceof WritableStream, true);
res.end();
})
);

server.listen(
0,
common.mustCall(() => {
http.get(
{
port: server.address().port,
},
common.mustCall(() => {
server.close();
})
);
})
);

0 comments on commit bbaca84

Please sign in to comment.