inngest / inngest-js

The developer platform for easily building reliable workflows with zero infrastructure for TypeScript & JavaScript
https://www.inngest.com/
GNU General Public License v3.0
370 stars 37 forks source link

[BUG] Cloudflare Worker unsupported based on documentation #617

Closed lorenzodejong closed 2 weeks ago

lorenzodejong commented 2 weeks ago

Describe the bug When initialising a new Cloudflare Worker project using the Wrangler CLI, the default documented setup from the documentation does not work as intended.

To Reproduce Steps to reproduce the behavior:

Cloudflare Worker based on https://developers.cloudflare.com/workers/get-started/guide/

  1. Create a new Worker by running npm create cloudflare@latest (choose Hello world starter and enable TypeScript)
  2. npm i inngest

Setup a client:

// src/inngest/client.ts
import { Inngest } from 'inngest';

export const inngest = new Inngest({ id: 'example' });

Setup a function:

// src/inngest/functions.ts
import { inngest } from './client';

export const helloWorld = inngest.createFunction({ id: 'hello-world' }, { event: 'test/hello.world' }, async ({ event, step }) => {
    await step.sleep('wait-a-moment', '1s');
    return { event, body: 'Hello, World!' };
});

Configure the worker based on the documentation

// src/index.ts
import { serve } from "inngest/cloudflare";
import { inngest } from "./inngest/client";
import { helloWorld } from "./inngest/functions";

export const onRequest = serve({
  client: inngest,
  functions: [helloWorld],
});

Run the worker using npm dev.

Observed behavior: The Worker doesn't handle the setup, it only seems to be compatible with Cloudflare Pages.

It does work when configuring the Worker using the method below, is this the recommended way of configuration?

import { serve } from 'inngest/cloudflare';
import { inngest } from './inngest/client';
import { helloWorld } from './inngest/functions';

export default {
    async fetch(request, env): Promise<Response> {
        return serve({
            client: inngest,
            functions: [helloWorld],
        })({ env: env as Record<string, string>, request });
    },
} satisfies ExportedHandler<Env>;

Expected behavior The Cloudflare Worker is correctly setup using the documented inngest/cloudflare setup OR the documentation is adjusted to reflect the intended configuration.

Code snippets / Logs / Screenshots n/a

System info (please complete the following information):

Additional context n/a

linear[bot] commented 2 weeks ago

INN-3178 [BUG] Cloudflare Worker unsupported based on documentation

jpwilliams commented 2 weeks ago

Thanks for the report, @lorenzodejong!

We'll fix both:

lorenzodejong commented 2 weeks ago

Amazing, thanks @jpwilliams!

Another thing i noticed (if you would like me to create a separate issue let me know, i'd be happy to do so):

With the current implementation the --remote development environment using the Wrangler CLI seems to be unsupported with the local dev server of Inngest.

Basically what you can do with Wrangler is run the following: wrangler dev --remote, which will deploy your Worker to a temporary instance in Cloudflare. This will allow you to connect to any bindings which are unsupported in local development. This opens up a proxy on your localhost, which would theoretically allow you to invoke Inngest events locally forwarding it to the instance running in Cloudflare.

However when trying to register the endpoint to the local dev server of Inngest, it's indicating that the registered environment is detected to be cloud instead of dev (causing the registration to fail). This makes sense from a technical standpoint, as it's actually running in a cloud environment.

I tried to circumvent this by explicitly setting isDev to true on the Inngest client. This does allow registration of the instance in the Inngest dev server, however any invoked event will cause an error from Cloudflare: Error 1003 : Direct IP access not allowed. It seems like the Inngest dev server is explicitly trying to access a port on the host, which is not allowed from Cloudflare.

BernardoSM commented 2 weeks ago

@lorenzodejong I had the same problem with --remote option from cloudflare.

I have some mTLS bindings that are only available with --remote, so I can't use them with inngest right now as I said here: https://github.com/inngest/inngest/issues/1463

lorenzodejong commented 2 weeks ago

@BernardoSM thanks for noticing, since this issue will be resolved from #619 i'm closing this. We can continue the thread from your provided issue.