Skip to content

feat: add signal property to request in vite, node environments #13895

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changeset/twelve-jokes-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-node': minor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't actually touch the adapter-node code at all

Suggested change
'@sveltejs/adapter-node': minor

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does touch @sveltejs/kit/node, though, and the Node adapter is bundled, so we need to re-build it and release a new version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On that note, @sveltejs/kit/node's getRequest is also used in local development, in preview mode, and in production in the Vercel adapter. Is this going to have any impact on those?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On that note, @sveltejs/kit/node's getRequest is also used in local development, in preview mode, and in production in the Vercel adapter. Is this going to have any impact on those?

Yes, it would. Apologies for not being clearer. Though, I wasn't aware that the vercel adapter used getRequest. I'll mark @sveltejs/adapter-vercel for a minor version, as well.

'@sveltejs/kit': minor
---

feat: add signal property to request in vite, node environments
46 changes: 46 additions & 0 deletions documentation/docs/25-build-and-deploy/40-adapter-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,49 @@ app.listen(3000, () => {
console.log('listening on port 3000');
});
```

## Aborted requests

> [!NOTE] This feature is available in `vite dev` and `vite preview` servers.

The adapter will fire an `abort` event when the incoming request is cancelled before completion. You can access the [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) via `event.request.signal`.

A pretty typical example of using the `signal` property:

```js
/// file: src/routes/api/object/[slug]/+server.js
/** @type {import('./$types').RequestHandler} */
export async function GET({ request, params }) {
const stream = await s3.getObject("bucket", params.slug, {
signal: request.signal
})

return new Response(stream)
}
```

Another example, this time using [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events):

```js
/// file: src/routes/api/sse/+server.js
/** @type {import('./$types').RequestHandler} */
export function GET({ request }) {
const stream = new ReadableStream({
start(controller) {
const interval = setInterval(() => {
controller.enqueue("data: Hello, world!\n\n\n");
}, 1000);

request.signal.onabort = () => {
clearInterval(interval);
};
},
});

return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
},
});
}
```
8 changes: 8 additions & 0 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,19 @@ export async function getRequest({ request, base, bodySizeLimit }) {
delete headers[':scheme'];
}

const controller = new AbortController();
request.once('close', () => {
if (request.readableAborted) {
controller.abort();
}
});

return new Request(base + request.url, {
// @ts-expect-error
duplex: 'half',
method: request.method,
headers: Object.entries(headers),
signal: controller.signal,
body:
request.method === 'GET' || request.method === 'HEAD'
? undefined
Expand Down
Loading