Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/wet-hairs-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow body to be a binary ReadableStream
7 changes: 5 additions & 2 deletions packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ export async function render_endpoint(event, mod) {

const type = headers.get('content-type');

if (!is_text(type) && !(body instanceof Uint8Array || is_string(body))) {
if (
!is_text(type) &&
!(body instanceof Uint8Array || body instanceof ReadableStream || 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`
`${preface}: body must be an instance of string, Uint8Array or ReadableStream if content-type is not a supported textual content-type`
);
}

Expand Down
14 changes: 14 additions & 0 deletions packages/kit/test/apps/basics/src/routes/endpoint-output/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return {
headers: {
'content-type': 'application/octet-stream'
},
body: new ReadableStream({
start: (controller) => {
controller.enqueue(new Uint8Array([1, 2, 3]));
controller.close();
}
})
};
}
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ test.describe('Endpoints', () => {
const r2 = await request.get('/endpoint-output/simple/__data.json');
expect(r2.status()).toBe(404);
});

test('body can be a binary ReadableStream', async ({ request }) => {
const response = await request.get('/endpoint-output/stream');
const body = await response.body();
expect(Array.from(body)).toEqual([1, 2, 3]);
});
});

test.describe('Encoded paths', () => {
Expand Down