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

Support Event Web API #293

Closed
f-elix opened this issue Mar 28, 2023 · 11 comments
Closed

Support Event Web API #293

f-elix opened this issue Mar 28, 2023 · 11 comments

Comments

@f-elix
Copy link

f-elix commented Mar 28, 2023

Feature Request

Is your feature request related to a problem? Please describe.
I'm trying to deploy an edge function that uses the stripe-node package. Despite the name, it is possible to use it in other runtimes, such as Cloudflare workers and Deno. There is however a problem with Vercel edge functions because the Event API is not supported (see this issue: stripe/stripe-node#1732). An error is thrown whenever the function is invoked.

You can reproduce the issue by deploying and edge function with the following code:

import Stripe from 'stripe';

const stripe = new Stripe(STRIPE_SECRET_KEY, {
	apiVersion: '2022-11-15',
	httpClient: Stripe.createFetchHttpClient()
});

Describe the solution you'd like
Support the Event Web API, as it is part of WinterCG's Minimum Common Web Platform API proposal.

@thillmann
Copy link

I was able to get Stripe to work on Vercel's edge function by using a polyfill (event-target-polyfill) for the events api. This seems to work (at least for the way I'm using Stripe).

@f-elix
Copy link
Author

f-elix commented Apr 8, 2023

@thillmann Thank you so much! All my edge functions using stripe-node now work :)

Still it would be nice if this could be added to Vercel's edge runtime.

@Kikobeats
Copy link
Member

Kikobeats commented Jun 1, 2023

The Edge Runtime core is using event-target-shim which provides Event and EventTarget polyfills.

Maybe this was an issue in the past, but it seems like it's working as expected:

❯ edge-runtime --repl
ƒ => !!Event
true
ƒ => !!EventTarget
true
ƒ =>

I'm going to close for now, but please feel free to comment if I'm missing something 🙏

@f-elix
Copy link
Author

f-elix commented Jun 1, 2023

@Kikobeats, I tried again without event-target-polyfill and I am still getting the same error.

The error is TypeError: Class extends value undefined is not a constructor or null because of an internal stripe-node class that extends Event, so it seems that Event is still undefined at runtime without the polyfill. Hope that helps!

@Kikobeats Kikobeats reopened this Jun 1, 2023
@Kikobeats
Copy link
Member

@f-elix are you using Next.js or Vercel CLI?

Also, if you do globalThis.Event what you get? undefined?

@f-elix
Copy link
Author

f-elix commented Jun 2, 2023

@Kikobeats I'm actually using Sveltekit and deploying with adapter-vercel.

I indeed get undefined if I do globalThis.Event.

@Kikobeats
Copy link
Member

@f-elix this is now fixed; can you check at your end? 🙂

@sundaycrafts
Copy link

I think this issue is still going on. Here is a error message from stripe-node

TypeError: EventTarget is not a constructor
    at (node_modules/stripe/esm/StripeEmitter.js:15:27)
    at (node_modules/stripe/esm/platform/WebPlatformFunctions.js:13:15)
    at (node_modules/stripe/esm/stripe.core.js:48:43)
    at (app/api/order_hook/route.ts:7:15)
    at (webpack/bootstrap:21:0)
    at (node_modules/luxon/src/datetime.js:2248:0)
    at (webpack/runtime/jsonp chunk loading:34:0)
    at (api/my_hook:middleware.js:1:17)

@f-elix
Copy link
Author

f-elix commented Jun 27, 2023

@Kikobeats Sorry for the delay! I'm still getting the same error:

TypeError: Class extends value undefined is not a constructor or null
    at (../../../../node_modules/stripe/esm/StripeEmitter.js:6:6)
    at (../../../../.svelte-kit/vercel-tmp/fn/edge.js:13:7)
    at (../../../../node_modules/stripe/esm/platform/WebPlatformFunctions.js:2:0)
    at (../../../../.svelte-kit/vercel-tmp/fn/edge.js:13:7)
    at (../../../../node_modules/stripe/esm/stripe.esm.worker.js:1:0)
    at (../../../../.svelte-kit/vercel-tmp/fn/edge.js:13:7)
    at (../../../../.svelte-kit/output/server/chunks/stripe.js:1:0)
    at (../../../../.svelte-kit/vercel-tmp/fn/edge.js:13:7)
    at (../../../../.svelte-kit/output/server/entries/pages/_locale_lang_/_slug_slug_signup_/_page.server.ts.js:9:0)
    at (../../../../.svelte-kit/vercel-tmp/fn/edge.js:13:7)

@Kikobeats Kikobeats reopened this Jun 27, 2023
@Kikobeats
Copy link
Member

Kikobeats commented Jul 3, 2023

Hello,

The original issue reported support for Event, but I didn't note EventTarget should be added as well.

Now, both are working properly at production.

source code:

export const config = { runtime: 'edge' }

export default () => {
  return Response.json({
    queueMicrotask: !!queueMicrotask,
    Event: !!Event,
    EventTarget: !!EventTarget
  })
}

deployment: https://edge-api-one.vercel.app/api

I tested manually in a project running npm i stripe on the Edge, and it works flawless.

Can you confirm at your end? 🙂

@f-elix
Copy link
Author

f-elix commented Jul 4, 2023

@Kikobeats Everything works now! No error, and logging Event doesn't show undefined anymore. Thanks a lot for your work :)

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

No branches or pull requests

4 participants