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

[Feature Request] better cloudflare workers support (with streaming) #394

Closed
1 task done
Hebilicious opened this issue May 29, 2023 · 1 comment
Closed
1 task done

Comments

@Hebilicious
Copy link
Member

Hebilicious commented May 29, 2023

Describe the feature

H3 works in cloudflare workers but is quite inconvenient to use, as it needs createCall from unenv #39

#144 sendStream / isStream seems incompatible with the TransformStream style used in cloudflare workers.

Proposed API :

Standard:

const workerStream = {
  fetch(request: Request, env?: any, context?: ExecutionContext) {
    const app = createApp({ debug: false });
    app.use(
      "/",
      eventHandler(async () => {
        return sendNativeResponse(new Response("Hello World"));
      })
    );
    return adapterCloudflareWorker(app, request, env, context);
  },
};

Streaming:

  const workerStream = {
    fetch(request: Request, env?: any, context?: ExecutionContext) {
    const app = createApp({ debug: false });
      app.use(
        "/",
        eventHandler(async () => {
          const response = await fetch(request);
          const { readable, writable } = new TransformStream();
          response.body?.pipeTo(writable);
          return sendNativeResponse(new Response(readable, response));
        })
      );
      return adapterCloudflareWorker(app, request, env, context);
    },
  };

Maybe we can wrap the entire app and allow returning responses without a helper :

const app = createApp({ debug: false })
const cloudflare = adapterCloudflareWorker(app);
app.use(
  "/",
  eventHandler(async () => {
    const response = await fetch(request);
    const { readable, writable } = new TransformStream();
    response.body?.pipeTo(writable);
    return new Response(readable, response);
  })
); 
export default cloudflare // module syntax
cloudflare.fire() // sw syntax 

This would unlock the possiblity for Nuxt to use pipeToWebWriteable to do HTML streaming on cloudflare workers.

Additional information

  • Would you be willing to help implement this feature?
@pi0
Copy link
Member

pi0 commented Aug 1, 2023

Let's track via #458. Cloudflare workers as well as other edge runtimes share the same Web API (Request/Response). We might, later on, add platform-specific instructions to the docs.

@pi0 pi0 closed this as completed Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants