Skip to content

feat: add support for read in edge environments #13859

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 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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/old-moons-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': minor
---

feat: add support for `('$app/server').read`
3 changes: 3 additions & 0 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ export default function (options = {}) {
return prerender ? emulated.prerender_platform : emulated.platform;
}
};
},
supports: {
read: () => true
}
};
}
Expand Down
28 changes: 27 additions & 1 deletion packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,33 @@ export default {
async fetch(req, env, context) {
await server.init({
// @ts-expect-error env contains environment variables and bindings
env
env,
read: (file) =>
new ReadableStream({
async start(controller) {
try {
const response = await env.ASSETS.fetch(new URL('/' + file, req.url));
if (!response.ok) {
throw new Error(`Failed to fetch (${response.status} - ${response.statusText})`);
}
const reader = response.body.getReader();
/** @returns {Promise<void>} */
async function pump() {
const { done, value } = await reader.read();
if (done) {
controller.close();
return;
}
controller.enqueue(value);
return pump();
}
return pump();
} catch (error) {
console.error(`Error reading file ${file}:`, error);
controller.error(error);
}
}
})
});

// skip cache if "cache-control: no-cache" in request
Expand Down
Binary file added packages/adapter-cloudflare/test/apps/goose.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { read } from '$app/server';
import goose from '../../../../goose.jpg';

export function GET() {
return read(goose);
}
6 changes: 6 additions & 0 deletions packages/adapter-cloudflare/test/apps/pages/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ test('worker works', async ({ page }) => {
await page.goto('/');
await expect(page.locator('h1')).toContainText('Sum: 3');
});

test("('$app/server').read works", async ({ page }) => {
const response = await page.goto('/read');
expect(response.status()).toBe(200);
expect(response.headers()['content-type']).toBe('image/jpeg');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { read } from '$app/server';
import goose from '../../../../goose.jpg';

export function GET() {
return read(goose);
}
6 changes: 6 additions & 0 deletions packages/adapter-cloudflare/test/apps/workers/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ test('worker works', async ({ page }) => {
await page.goto('/');
await expect(page.locator('h1')).toContainText('Sum: 3');
});

test("('$app/server').read works", async ({ page }) => {
const response = await page.goto('/read');
expect(response.status()).toBe(200);
expect(response.headers()['content-type']).toBe('image/jpeg');
});
Loading