bun v0.0.76 #138
Jarred-Sumner
announced in
Announcements
bun v0.0.76
#138
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
To upgrade:
What's new
bun-typesnpm package. A PR for @types/bun is waiting review (feel free to nudge them).Bun.serve()is a fast HTTP & HTTPS server that supports theRequestandResponseWeb APIs. The server is a fork of uWebSocketsBun.write()– one API for writing files, pipes, and copying files leveraging the fastest system calls available for the input & platform. It uses theBlobWeb API.Bun.mmap(path)lets you read files as a live-updatingUint8Arrayvia the mmap(2) syscall. Thank you @evanwashere!!Bun.hash(bufferOrString)exposes fast non-cryptographic hashing functions. Useful for things likeETag, not for passwords.Bun.allocUnsafe(length)creates a new Uint8Array ~3.5x faster than new Uint8Array, but it is not zero-initialized. This is similar to Node'sBuffer.allocUnsafe, though without the memory pool currentlyURLexamplesfolderfs.read()andfs.write()and some more tests for fsResponse.redirect(),Response.json(), andResponse.error()have been addedimport.meta.urlis now a file:// url stringBun.resolveandBun.resolveSynclet you resolve the same asimportdoes. It throws aResolveErroron failure (same asimport)Bun.stderrandBun.stdoutnow return aBlobSharedArrayBufferis now enabled (thanks @evanwashere!)New Web APIs in bun.js
Going forward, Bun will first try to rely on WebKit/Safari's implementations of Web APIs rather than writing new ones. This will improve Web API compatibility while reducing bun's scope, without compromising performance
These Web APIs are now available in bun.js and powered by Safari's implementation:
URLURLSearchParamsErrorEventEventEventTargetDOMExceptionHeadersuses WebKit's implementation now instead of a custom oneAbortSignal(not wired up to fetch or fs yet, it exists but not very useful)AbortControllerAlso added:
reportError(does not dispatch an"error"event yet)Additionally, all the builtin constructors in bun now have a
.prototypeproperty (this was missing before)Bun.serve- fast HTTP serverFor a hello world HTTP server that writes "bun!",
Bun.serveserves about 2.5x more requests per second than node.js on Linux:Bigger is better
Code
Bun:
Node:
Usage
Two ways to start an HTTP server with bun.js:
export defaultan object with afetchfunctionIf the file used to start bun has a default export with a
fetchfunction, it will start the http server.fetchreceives aRequestobject and must return either aResponseor aPromise<Response>. In a future version, it might have an additional arguments for things like cookies.Bun.servestarts the http server explicitlyError handling
For error handling, you get an
errorfunction.If
development: trueanderroris not defined or doesn't return aResponse, you will get an exception page with a stack trace:It will hopefully make it easier to debug issues with bun until bun gets debugger support. This error page is based on what
bun devdoes.If the error function returns a
Response, it will be served insteadIf the
errorfunction itself throws anddevelopmentisfalse, a generic 500 page will be shownCurrently, there is no way to stop the HTTP server once started 😅, but that will be added in a future version.
The interface for
Bun.serveis based on what Cloudflare Workers does.Bun.write() – optimizing I/O
Bun.writelets you write, copy or pipe files automatically using the fastest system calls compatible with the input and platform.All this complexity is handled by a single function.
Bug fixes
requireto produce incorrect output depending on how the module was usedbun devrelated to HMR & websockets - Jarred-Sumner/bun@daeede2fs.openSyncnow supportsmodeandflags- Jarred-Sumner/bun@c73fcb0fs.readandfs.writewere incorrectly returning the output offs/promisesversions, this is fixedfsfunction received a TypedArray as input. - Jarred-Sumner/bun@614f64b. This also improves performance of sending array buffers to native a littleResponse's constructor previously readstatusCodeinstead ofstatus. This was incorrect and has been fixed.fs.statreported incorrect information on macOS x64improve performance of accessing
Bun.TranspilerandBun.unsafe29a759aThis discussion was created from the release bun v0.0.76.
Beta Was this translation helpful? Give feedback.
All reactions