Skip to content

Commit

Permalink
[fix] prevent responses from hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 8, 2022
1 parent b3ad29d commit 810c52b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 37 deletions.
10 changes: 10 additions & 0 deletions .changeset/few-teachers-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@sveltejs/adapter-node': patch
'@sveltejs/kit': patch
'@sveltejs/adapter-cloudflare': patch
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-vercel': patch
---

[fix] prevent responses from hanging
2 changes: 1 addition & 1 deletion packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
"@types/node": "^16.11.36",
"c8": "^7.11.3",
"compression": "^1.7.4",
"node-fetch": "^3.2.4",
"polka": "^1.0.0-next.22",
"rimraf": "^3.0.2",
"rollup": "^2.75.3",
"sirv": "^2.0.2",
"typescript": "^4.7.4",
"undici": "^5.6.1",
"uvu": "^0.5.3"
}
}
4 changes: 2 additions & 2 deletions packages/adapter-node/tests/smoke.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from 'uvu';
import { create_kit_middleware } from '../src/handler.js';
import * as assert from 'uvu/assert';
import fetch from 'node-fetch';
import polka from 'polka';
import * as assert from 'uvu/assert';
import { fetch } from 'undici';

const { PORT = 3000 } = process.env;
const DEFAULT_SERVER_OPTS = { render: () => {} };
Expand Down
36 changes: 5 additions & 31 deletions packages/kit/src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,12 @@ export async function setResponse(res, response) {

res.writeHead(response.status, headers);

if (response.body) {
let cancelled = false;

const reader = response.body.getReader();

res.on('close', () => {
reader.cancel();
cancelled = true;
});

const next = async () => {
const { done, value } = await reader.read();

if (cancelled) return;

if (done) {
res.end();
return;
}

res.write(Buffer.from(value), (error) => {
if (error) {
console.error('Error writing stream', error);
res.end();
} else {
next();
}
});
};

next();
if (response.body instanceof Readable) {
response.body.pipe(res);
} else {
if (response.body) {
res.write(new Uint8Array(await response.arrayBuffer()));
}
res.end();
}
}
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 810c52b

Please sign in to comment.