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

feat: read assets inside an edge function #11674

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
41 changes: 39 additions & 2 deletions packages/adapter-vercel/files/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,53 @@ import { Server } from 'SERVER';
import { manifest } from 'MANIFEST';

const server = new Server(manifest);

/**
* We don't know the origin until we receive a request, but
* that's guaranteed to happen before we call `read`
* @type {string}
*/
let origin;

const initialized = server.init({
env: /** @type {Record<string, string>} */ (process.env)
env: /** @type {Record<string, string>} */ (process.env),
read: (file) => {
const controller = new AbortController();
const signal = controller.signal;

return new ReadableStream({
async start(controller) {
try {
const response = await fetch(`${origin}/${file}`, { signal });
const reader = /** @type {ReadableStream} */ (response.body).getReader();

while (true) {
const { done, value } = await reader.read();
if (done) break;
controller.enqueue(value);
}

controller.close();
} catch (error) {
controller.error(error);
}
},
cancel(reason) {
controller.abort(reason);
}
});
}
});

/**
* @param {Request} request
* @param {import('../index.js').RequestContext} context
*/
export default async (request, context) => {
await initialized;
if (!origin) {
origin = new URL(request.url).origin;
await initialized;
}

return server.respond(request, {
getClientAddress() {
Expand Down
13 changes: 1 addition & 12 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,7 @@ const plugin = function (defaults = {}) {
},

supports: {
// reading from the filesystem only works in serverless functions
read: ({ config, route }) => {
const runtime = config.runtime ?? defaults.runtime;

if (runtime === 'edge') {
throw new Error(
`${name}: Cannot use \`read\` from \`$app/server\` in route \`${route.id}\` configured with \`runtime: 'edge'\``
);
}

return true;
}
read: () => true
}
};
};
Expand Down
14 changes: 14 additions & 0 deletions sites/kit.svelte.dev/src/routes/delete-me/+server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { read } from '$app/server';
import file from './file.txt?url';

export const config = {
runtime: 'edge'
};

export const prerender = false;

export async function GET() {
const text = await read(file).text();

return new Response(`${text} at ${new Date()}`);
}
1 change: 1 addition & 0 deletions sites/kit.svelte.dev/src/routes/delete-me/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello! This file was read inside an edge function
Loading