stripe-node icon indicating copy to clipboard operation
stripe-node copied to clipboard

Use `fetch`-based client in Bun

Open Jarred-Sumner opened this issue 1 year ago • 2 comments

In Bun, "node:http" internally is implemented as a wrapper on top of fetch. "stripe" might as well skip the wrapper and just use fetch instead, assuming that it doesn't change the interface for users

In Bun v1.0, we removed the "worker" package.json "exports" condition which unfortunately caused "stripe" to load the "node:http"-based implementation. This "node:http"-based implementation usually works fine, however there is an edgecase where https.Agent has the wrong protocol set, causing errors when a request is retried. This is a difficult to reproduce bug in Bun and not in stripe, which Bun will fix separately.

If we add the BUN_JSC_dumpModuleLoadingState=1 environment variable, Bun logs some information about which modules have loaded.

Bun v0.6.1:

❯ BUN_JSC_dumpModuleLoadingState=1 bun-0.6.1 terminy.js 2>&1 | rg "\.esm.*.js"
Loader [resolve] ./node_modules/stripe/esm/stripe.esm.worker.js
Loader [fetch] ./node_modules/stripe/esm/stripe.esm.worker.js
loader [parsing] ./node_modules/stripe/esm/stripe.esm.worker.js
Loader [link] ./node_modules/stripe/esm/stripe.esm.worker.js
Loader [evaluate] ./node_modules/stripe/esm/stripe.esm.worker.js

Bun v1.1.7:

❯ BUN_JSC_dumpModuleLoadingState=1 bun-1.1.7 terminy.js 2>&1 | rg "\.esm.*.js"
Loader [fetch] ./node_modules/stripe/esm/stripe.esm.node.js
loader [parsing] ./node_modules/stripe/esm/stripe.esm.node.js
Loader [link] ./node_modules/stripe/esm/stripe.esm.node.js
Loader [evaluate] ./node_modules/stripe/esm/stripe.esm.node.js

We can see that in older versions of Bun, it used the worker condition instead of the default condition.

I looked through WebPlatformFunctions.js and it looks like the main downside to this approach is the lack of tryBufferData and a slower secureCompare. I don't have enough context to know if tryBufferData is important - it's for file uploads, is what it seems like?

Jarred-Sumner avatar May 10 '24 11:05 Jarred-Sumner

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar May 10 '24 11:05 CLAassistant

(gentle bump: once you're ready please sign the CLA and this change will be merged!)

anniel-stripe avatar May 10 '24 23:05 anniel-stripe

Thanks for submitting this. We merged a fix that resolves this issue.

prathmesh-stripe avatar Jun 25 '24 16:06 prathmesh-stripe