You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type definitions for better editor integration and TypeScript LSP! There are types for the runtime APIs now in the bun-types npm package. A PR for @types/bun is waiting review (feel free to nudge them).
Bun.serve() is a fast HTTP & HTTPS server that supports the Request and Response Web APIs. The server is a fork of uWebSockets
Bun.write() – one API for writing files, pipes, and copying files leveraging the fastest system calls available for the input & platform. It uses the Blob Web API.
Bun.mmap(path) lets you read files as a live-updating Uint8Array via the mmap(2) syscall. Thank you @evanwashere!!
Bun.hash(bufferOrString) exposes fast non-cryptographic hashing functions. Useful for things like ETag, 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's Buffer.allocUnsafe, though without the memory pool currently
Several web APIs have been added, including URL
A few more examples have been added to the examples folder
Bug fixes to fs.read() and fs.write() and some more tests for fs
Response.redirect(), Response.json(), and Response.error() have been added
import.meta.url is now a file:// url string
Bun.resolve and Bun.resolveSync let you resolve the same as import does. It throws a ResolveError on failure (same as import)
Bun.stderr and Bun.stdout now return a Blob
SharedArrayBuffer is now enabled (thanks @evanwashere!)
Updated Next.js version
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:
If the file used to start bun has a default export with a fetch function, it will start the http server.
// hi.jsexportdefault{fetch(req){returnnewResponse("HI!");},};// bun ./hi.js
fetch receives a Request object and must return either a Response or a Promise<Response>. In a future version, it might have an additional arguments for things like cookies.
All this complexity is handled by a single function.
// Write "Hello World" to output.txtawaitBun.write("output.txt","Hello World");
// log a file to stdoutawaitBun.write(Bun.stdout,Bun.file("input.txt"));
// write the HTTP response body to diskawaitBun.write("index.html",awaitfetch("http://example.com"));// this does the same thingawaitBun.write(Bun.file("index.html"),awaitfetch("http://example.com"));
// copy input.txt to output.txtawaitBun.write("output.txt",Bun.file("input.txt"));
Bug fixes
Fixed a bug on Linux where sometimes the HTTP thread would incorrectly go to sleep, causing requests to hang forever 😢 . Previously, it relied on data in the queues to determine whether it should idle and now it increments a counter.
Fixed a bug that sometimes caused require to produce incorrect output depending on how the module was used
fs.read and fs.write were incorrectly returning the output of fs/promises versions, this is fixed
Fixed a crash that could occur during garbage collection when an fs function received a TypedArray as input. - Jarred-Sumner/bun@614f64b. This also improves performance of sending array buffers to native a little
Response's constructor previously read statusCode instead of status. This was incorrect and has been fixed.
Fixed a bug where fs.stat reported incorrect information on macOS x64
Slight improvement to HMR reliability if reading large files or slow filesystems over websockets - 89cd35f
Fixed a potential infinite loop when generating a sourcemap - fe973a5
improve performance of accessing Bun.Transpiler and Bun.unsafe29a759a
This discussion was created from the release bun v0.0.76.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
To upgrade:
What's new
bun-types
npm package. A PR for @types/bun is waiting review (feel free to nudge them).Bun.serve()
is a fast HTTP & HTTPS server that supports theRequest
andResponse
Web 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 theBlob
Web API.Bun.mmap(path)
lets you read files as a live-updatingUint8Array
via 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 currentlyURL
examples
folderfs.read()
andfs.write()
and some more tests for fsResponse.redirect()
,Response.json()
, andResponse.error()
have been addedimport.meta.url
is now a file:// url stringBun.resolve
andBun.resolveSync
let you resolve the same asimport
does. It throws aResolveError
on failure (same asimport
)Bun.stderr
andBun.stdout
now return aBlob
SharedArrayBuffer
is 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:
URL
URLSearchParams
ErrorEvent
Event
EventTarget
DOMException
Headers
uses WebKit's implementation now instead of a custom oneAbortSignal
(not wired up to fetch or fs yet, it exists but not very useful)AbortController
Also added:
reportError
(does not dispatch an"error"
event yet)Additionally, all the builtin constructors in bun now have a
.prototype
property (this was missing before)Bun.serve
- fast HTTP serverFor a hello world HTTP server that writes "bun!",
Bun.serve
serves 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 default
an object with afetch
functionIf the file used to start bun has a default export with a
fetch
function, it will start the http server.fetch
receives aRequest
object and must return either aResponse
or aPromise<Response>
. In a future version, it might have an additional arguments for things like cookies.Bun.serve
starts the http server explicitlyError handling
For error handling, you get an
error
function.If
development: true
anderror
is 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 dev
does.If the error function returns a
Response
, it will be served insteadIf the
error
function itself throws anddevelopment
isfalse
, 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.serve
is based on what Cloudflare Workers does.Bun.write() – optimizing I/O
Bun.write
lets 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
require
to produce incorrect output depending on how the module was usedbun dev
related to HMR & websockets - Jarred-Sumner/bun@daeede2fs.openSync
now supportsmode
andflags
- Jarred-Sumner/bun@c73fcb0fs.read
andfs.write
were incorrectly returning the output offs/promises
versions, this is fixedfs
function received a TypedArray as input. - Jarred-Sumner/bun@614f64b. This also improves performance of sending array buffers to native a littleResponse
's constructor previously readstatusCode
instead ofstatus
. This was incorrect and has been fixed.fs.stat
reported incorrect information on macOS x64improve performance of accessing
Bun.Transpiler
andBun.unsafe
29a759aThis discussion was created from the release bun v0.0.76.
Beta Was this translation helpful? Give feedback.
All reactions