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

ReadableStream, NodeJS.ReadableStream should be supported in response.body #4499

Closed
fixiabis opened this issue Apr 3, 2022 · 1 comment
Closed

Comments

@fixiabis
Copy link

fixiabis commented Apr 3, 2022

Describe the bug

I want to send large files via stream, but not working, so:

I saw here, looks supported, response.body also could be NodeJS.ReadableStream:

if (response.body instanceof Readable) {
response.body.pipe(res);
} else {
if (response.body) {
res.write(await response.arrayBuffer());
}
res.end();
}

Then I checked implementation in kit/src/runtime/server/endpoint.js:

if (is_pojo(body) && (!type || type.startsWith('application/json'))) {
headers.set('content-type', 'application/json; charset=utf-8');
normalized_body = JSON.stringify(body);
} else {
normalized_body = /** @type {import('types').StrictBody} */ (body);
}

And is_pojo in kit/src/runtime/server/utils.js:

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 && typeof body.pipe === 'function') return false;
// similarly, it could be a web ReadableStream
if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) return false;
}
return true;
}

Looks response.body could be NodeJS.ReadableStream or ReadableStream, but no, because:

if (!is_text(type) && !(body instanceof Uint8Array || is_string(body))) {
return error(
`${preface}: body must be an instance of string or Uint8Array if content-type is not a supported textual content-type`
);
}

There will throw an error preventing me from using it.

Additional type check added currently:

if (
	!is_text(type) &&
	!(
		body instanceof Uint8Array ||
		(typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) ||
		(body !== null && body._readableState && typeof body.pipe === 'function') ||
		is_string(body)
	)
) {

Reproduction

// some endpoint script
export const get = (event) => {
  if (!permitted) {
    return { status: 403, body: 'You do not have permission to access the file.' };
  }

  const fileStream = fs.createReadStream(filePath);
  return { body: fileStream };
};

Logs

No response

System Info

Not important.

Severity

serious, but I can work around it

Additional Information

No response

@Conduitry
Copy link
Member

This is a duplicate of #3419.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants