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

Correctly identify readable node streams #3941

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-houses-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Correctly identify readable node streams
18 changes: 15 additions & 3 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@ export function decode_params(params) {
return params;
}

/** @param {any} obj **/
function is_readable_node_stream(obj) {
// Copied from nodejs sources
return !!(
(
obj &&
typeof obj.pipe === 'function' &&
typeof obj.on === 'function' &&
(!obj._writableState || obj._readableState?.readable !== false) && // Duplex
(!obj._writableState || obj._readableState)
) // Writable has .pipe.
);
}
fehnomenal marked this conversation as resolved.
Show resolved Hide resolved

/** @param {any} body */
export function is_pojo(body) {
if (typeof body !== 'object') return false;

if (body) {
if (body instanceof Uint8Array) return false;

// body could be a node Readable, but we don't want to import
// node built-ins, so we use duck typing
if (body._readableState && body._writableState && body._events) return false;
if (is_readable_node_stream(body)) return false;

// similarly, it could be a web ReadableStream
if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) return false;
Expand Down