From bbaca8442a8ae5fb25388359eb452ad2d6a4d065 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Sat, 10 Dec 2022 22:36:40 +0530 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/44188 PR-URL: https://github.com/nodejs/node/pull/45642 Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli --- lib/internal/webstreams/adapters.js | 10 +++++-- ...est-stream-toWeb-allows-server-response.js | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-stream-toWeb-allows-server-response.js diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 0e844d01200751..02411a07a436a5 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -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)) { diff --git a/test/parallel/test-stream-toWeb-allows-server-response.js b/test/parallel/test-stream-toWeb-allows-server-response.js new file mode 100644 index 00000000000000..fd7a14d5965417 --- /dev/null +++ b/test/parallel/test-stream-toWeb-allows-server-response.js @@ -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(); + }) + ); + }) +);