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

🐛 BUG: Sending binary body is broken with node adapter SSR #4053

Closed
1 task done
tkamenoko opened this issue Jul 26, 2022 · 2 comments · Fixed by #4055
Closed
1 task done

🐛 BUG: Sending binary body is broken with node adapter SSR #4053

tkamenoko opened this issue Jul 26, 2022 · 2 comments · Fixed by #4055
Assignees
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority)

Comments

@tkamenoko
Copy link

tkamenoko commented Jul 26, 2022

What version of astro are you using?

0.0.0-rc-20220726043527

Are you using an SSR adapter? If so, which one?

Node

What package manager are you using?

npm

What operating system are you using?

Windows

Describe the Bug

Sending a binary content is broken on api route.

Example request:

const requestBody = Uint8Array.from([0,1,33,233])

const response = await fetch("/api/echo", {
  method: "post",
  body: requestBody,
  headers: { "Content-Type": "application/octet-stream" }
});

const responseBody = new Uint8Array(await response.arrayBuffer())
console.log(requestBody)
console.log(responseBody)

Stdout:

Uint8Array(4) [ 0, 1, 33, 233 ]
Uint8Array(6) [ 0, 1, 33, 239, 191, 189 ]

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-xdurhs-q4pmtc

Participation

  • I am willing to submit a pull request for this issue.
@tkamenoko
Copy link
Author

I found PR #4023 that fixes empty body bug, but it seems to allow only UTF-8 contents.

export class NodeApp extends App {
match(req: IncomingMessage | Request) {
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req));
}
render(req: IncomingMessage | Request) {
if ('on' in req) {
let body: string | undefined = undefined;
let reqBodyComplete = new Promise((resolve, reject) => {
req.on('data', (d) => {
if (body === undefined) {
body = '';
}
if (d instanceof Buffer) {
body += d.toString('utf-8');
} else if (typeof d === 'string') {
body += d;
}
});
req.on('end', () => {
resolve(body);
});
req.on('error', (err) => {
reject(err);
});
});
return reqBodyComplete.then(() => {
return super.render(req instanceof Request ? req : createRequestFromNodeRequest(req, body));
});
}
return super.render(req instanceof Request ? req : createRequestFromNodeRequest(req));
}
}

It always decodes a request body as UTF-8 string.

@matthewp matthewp added - P4: important Violate documented behavior or significantly impacts performance (priority) s1-small labels Jul 26, 2022
@matthewp
Copy link
Contributor

Ah yeah, it should be building up a Buffer I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants