Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: allow http.OutgoingMessage to be used in Writable.toWeb() #45642

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(webStreamResponse instanceof WritableStream, true);
res.end();
})
);

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