Add Bun support via a runtime-adaptive HTTP backend - #337
Open
IshanArya wants to merge 10 commits into
Open
Conversation
Author
|
Hey @nikeee is this of interest to you? Otherwise I can maintain it on the side. Appreciate your work. |
Owner
|
In general, this is something I'd consider adding to lean-s3. This pr in particular touches a little bit too much for my taste (e.g., why the CI files?). Bun is not a primary target, and I don't want to support bun:test due to that. I think the correct way is to wait for Bun to implement node:test. We're also focusing on the server-side, so support for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lean-s3 was built on
undici, which doesn't run on Bun (Bun ships an incomplete undici — no usableAgent/Dispatcher, missingBodyReadable.dump()). Calling lean-s3 under Bun crashed withresponse.body.dump is not a function.This PR makes lean-s3 work on both Node.js and Bun while keeping the fast undici path on Node, by introducing a runtime-adaptive HTTP layer.
Why not just use Bun's built-in S3 API?
Bun ships a native
Bun.S3Client, but it's limited compared to lean-s3, so it's not a drop-in replacement for users who need the full feature set. Notable gaps include:CopyObject— you can't copy objects server-side.presignforPUTdoesn't support constraining content length / content type the way lean-s3 does (e.g.presign(key, { method: "PUT", contentLength, type })), and presign-POST policies are limited.DeleteObjects, etc.).So rather than telling Bun users to fall back to
Bun.S3Clientand lose functionality, this PR makes lean-s3 itself run on Bun with the same API and feature set everywhere.What changed
src/http.ts— a small HTTP abstraction exposing a singlerequest()+ normalized response shape (statusCode, lower-casedheaders, and abodywithtext()/dump()/ streamingon("data"|"end"|"error")). It selects a backend once at runtime and caches it:Agent.fetch, normalized to the same shape. The streaming read path reads the webReadableStreamdirectly (noReadable.fromWebround-trip) to keep reads cheap.undicimoved from a hard dependency to anoptionalDependency;S3Client.tsanderror.tsnow go through the shim instead of importing undici directly.S3File.stream()cancellation: cancelling a stream reader could throwController is already closedwhen the in-flight request rejected afterward. Fixed by sharing asettledflag between the stream'sstartandcancel. Benefits Node too.src/test-harness.ts): the suite was written fornode:test+ theexpectpackage, which don't work cleanly under Bun (bun testpartially implementsnode:test, andexpect@30has a Bun interop bug). The harness mapsdescribe/test/before/after/expecttonode:test+expecton Node andbun:teston Bun (bridgingt.skip/t.todoanddescribe(name, { skip }, fn)), so the exact same test files run under both runtimes.LEAN_S3_FORCE_FETCH=1escape hatch to force the fetch backend on Node (debugging / odd environments).docker-compose.ymlfor local MinIO testing and atest:bunscript.DESIGN_DECISIONS.md(dual-backend rationale),CONTRIBUTING.md(Bun + Docker socket notes), andBENCHMARKS.md(Node-vs-Bun comparison).Performance
Benchmarked against a local MinIO. Bun (fetch) is at parity with or faster than Node (undici) across put/get/list, and substantially faster on
HeadObject. On Node, the direct undici path beatsfetch(most visibly on writes) — which is exactly why undici stays the Node default. Full numbers are inBENCHMARKS.md.Testing
npm test): full unit + integration suite (MinIO, LocalStack, S3Mock, Ceph, Garage) green.npm run test:bun): same suite green (255 pass).distverified working on both runtimes against MinIO.Notes / compatibility
undiciis now optional — installs that skip optional deps fall back tofetchautomatically.