cloudflare / workers-sdk

โ›…๏ธ Home to Wrangler, the CLI for Cloudflare Workersยฎ
https://developers.cloudflare.com/workers/
Apache License 2.0
2.41k stars 590 forks source link

๐Ÿ› BUG: issues with async operations in Worker using Turborepo task execution #6125

Closed lorenzodejong closed 1 week ago

lorenzodejong commented 1 week ago

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

3.60.3 [Wrangler]

What version of Node are you using?

20.12.2

What operating system and version are you using?

MacOS Sonoma 14.5

Describe the Bug

Observed behavior

When initialising a Cloudflare Worker inside of a Turborepo and executing the wrangler development server using turbo dev, the Worker is facing issues with async operations.

Expected behavior

The Worker executes async operations correctly in Turborepo's task execution.

Steps to reproduce

Example repository: https://github.com/lorenzodejong/worker-fetch-test

Modify the worker src/index.ts to include an async fetch call:

export default {
    async fetch(request, env, ctx): Promise<Response> {
        const res = await fetch('https://api.sampleapis.com/coffee/hot');
        const movies = await res.json();
        console.log(movies);

        return new Response('Hello World!');
    },
} satisfies ExportedHandler<Env>;

Execute the worker from the root of the monorepo using pnpm turbo dev. Invoke the worker.

Observed result:

 [wrangler:err] Error: internal error
โ”‚     at async Object.fetch (file:///Users/x/Projects/personal/worker-fetch-test/apps/worker
โ”‚ -test/src/index.ts:16:15)
โ”‚     at async jsonError (file:///Users/x/Projects/personal/worker-fetch-test/node_modules/.
โ”‚ pnpm/wrangler@3.61.0_@cloudflare+workers-types@4.20240620.0/node_modules/wrangler/templates/middleware
โ”‚ /middleware-miniflare3-json-error.ts:22:10)
โ”‚     at async drainBody (file:///Users/x/Projects/personal/worker-fetch-test/node_modules/.
โ”‚ pnpm/wrangler@3.61.0_@cloudflare+workers-types@4.20240620.0/node_modules/wrangler/templates/middleware
โ”‚ /middleware-ensure-req-body-drained.ts:5:10)

Please provide a link to a minimal reproduction

https://github.com/lorenzodejong/worker-fetch-test

Please provide any relevant error logs

No response

threepointone commented 1 week ago

I was able to reproduce this. I assume you're using something like cloudflare warp or a proxy. Turbo v2 expects you to whitelist all environment variables (so called "strict mode"). Here are a couple of workarounds:

Let me know if this fixes your problem

lorenzodejong commented 1 week ago

Hey @threepointone! Thanks for the quick response, you're actually right about the fact that i'm on proxy. I tried out the fix and it works ๐Ÿ‘.

And hats off for the amazing work you've done with Partykit around the Cloudflare ecosystem, keep on going!