fal-ai / fal-js

The JavaScript client and utilities to fal-serverless with built-in TypeScript definitions
https://fal.ai
MIT License
91 stars 19 forks source link

using the fal proxy in SvelteKit fails if you call locals.auth() #107

Closed spullara closed 2 days ago

spullara commented 2 days ago

It fails when proxying with this error:

TypeError: immutable
    at appendHeader (node:internal/deps/undici/undici:8352:15)
    at _Headers.append (node:internal/deps/undici/undici:8554:16)
    at Module.add_cookies_to_headers (/Users/sam/Projects/chatbench-fal/node_modules/@sveltejs/kit/src/runtime/server/cookie.js:260:11)
    at /Users/sam/Projects/chatbench-fal/node_modules/@sveltejs/kit/src/runtime/server/respond.js:338:6
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/sam/Projects/chatbench-fal/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:128:21
    at async instrumentHandle (/Users/sam/Projects/chatbench-fal/node_modules/@sentry/sveltekit/build/cjs/server/handle.js:116:27)
    at async Module.respond (/Users/sam/Projects/chatbench-fal/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
    at async file:///Users/sam/Projects/chatbench-fal/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22

for example, just doing this in hooks causes this

// New handler to check/create customerId
const handleCustomerId: Handle = async ({ event, resolve }) => {
    await event.locals.auth();

    return resolve(event);
}

import { handle as auth } from './auth';
export const handle = sequence(auth, handleCustomerId);
spullara commented 2 days ago

fix:

diff --git a/node_modules/@fal-ai/server-proxy/src/index.js b/node_modules/@fal-ai/server-proxy/src/index.js
index 3caf2db..7287683 100644
--- a/node_modules/@fal-ai/server-proxy/src/index.js
+++ b/node_modules/@fal-ai/server-proxy/src/index.js
@@ -89,7 +89,7 @@ async function handleRequest(behavior) {
             behavior.sendHeader(key, value);
         }
     });
-    return behavior.sendResponse(res);
+    return behavior.sendResponse(new Response(res.body, res));
 }
 function fromHeaders(headers) {
     // TODO once Header.entries() is available, use that instead
spullara commented 2 days ago

thanks to this comment: https://stackoverflow.com/questions/73753704/sveltekit-typeerror-immutable

drochetti commented 2 days ago

Thanks for the report and fix suggestion @spullara I opened #109 to address it.

I also released 1.1.1-alpha.0 of the @fal-ai/server-proxy. Would you be able to give it a try to confirm it works. That would help as I don't have a Svelte app here with a reproducer ready to go.

spullara commented 2 days ago

that version appears to work for me. thanks!

drochetti commented 2 days ago

@spullara version 1.1.1 is out