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

Use fetch-based client in Bun #2090

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jarred-Sumner
Copy link

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?

In Bun v1.0, for better overall ecosystem compatibility, 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 involving `https.Agent` having the wrong `protocol` set, (causing errors)[oven-sh/bun#10975] when a request is retried. This is a difficult to reproduce bug in Bun and not in `stripe`.

However, in Bun, `"node:http"` internally is implemented as a wrapper on top of `fetch`. So `"stripe"` might as well skip the wrapper and just use fetch instead, assuming that is not a breaking change in of itself.


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

Bun v0.6.1:
```js
❯ 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:
```js
❯ 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
```
@CLAassistant
Copy link

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.

Copy link
Contributor

@anniel-stripe anniel-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the fix!

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?

To answer your question, tryBufferData is used to support uploading files as streams for the File create endpoint So that method will throw an error in Bun (and any environment using the worker export).

@anniel-stripe anniel-stripe enabled auto-merge (squash) May 10, 2024 17:13
@anniel-stripe
Copy link
Contributor

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

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 this pull request may close these issues.

None yet

3 participants