Bun

repository·main·Indexed 11 days ago

https://github.com/oven-sh/bun

An all-in-one JavaScript and TypeScript toolkit featuring a high-performance runtime, package manager, bundler, and test runner, designed as a fast replacement for Node.js.

Tokens
335.2K
Snippets
1.3K
Records
1.6K
Agent score
98%

What's inside Bun

  1. What is h3blast?

    main
    h3blast is a high-performance HTTP/3 load generator built on lsquic. It is designed specifically to stress test Bun.serve({ http3: true }) by using the same lsquic, BoringSSL, and packet paths as the Bun server side, providing a more accurate benchmark than tools like curl or Node.js QUIC shims.
  2. Overview of Bun's CSS Bundler features

    main

    Bun's bundler includes built-in support for CSS, providing automated processing for modern and future CSS syntax. Key features include:

    • Transpiling: Automatically converts modern/future CSS features into backwards-compatible syntax and handles vendor prefixing.
    • Minification: Reduces file size for production.
    • CSS Modules: Support for scoped CSS.
    • Tailwind CSS: Supported via a native bundler plugin.
  3. Overview of Bun native APIs

    main

    Bun provides a set of highly optimized native APIs accessible via the Bun global object and several built-in modules. While Bun implements standard Web APIs (like Blob, URL, and Request) wherever possible, it introduces specialized APIs for server-side tasks where no standard exists, such as file I/O, HTTP servers, and shell execution. Using these Bun-native APIs is the recommended way to achieve maximum performance in a Bun environment.

    Bun.serve({
      fetch(req: Request) {
        return new Response("Success!");
      },
    });
  4. What is Bun?

    main

    Bun is an all-in-one toolkit for JavaScript and TypeScript applications, delivered as a single executable named bun.

    It consists of:

    • Bun Runtime: A fast JavaScript runtime written in Rust and powered by JavaScriptCore. It is designed as a drop-in replacement for Node.js, offering reduced startup times and lower memory usage.
    • Built-in Tools: A test runner, script runner, and a Node.js-compatible package manager.

    Bun supports TypeScript and JSX out-of-the-box.

    # Run a TypeScript or JSX file directly
    bun run index.tsx
    
    # Run tests
    bun test
    
    # Run a script defined in package.json
    bun run start
    
    # Install a package
    bun install <pkg>
    
    # Execute a package (similar to npx)
    bunx cowsay 'Hello, world!'
  5. Overview of Node.js Compatibility in Bun

    main

    Bun aims for high compatibility with Node.js APIs, modules, and globals. It is designed to run popular frameworks like Next.js and Express, as well as millions of npm packages intended for Node.js. Bun's compatibility is regularly tested against the Node.js v23 test suite.

    Note: If a package works in Node.js but fails in Bun, it is considered a bug in Bun. You can report such issues via the official issue tracker.

  6. Overview of Elysia features

    main

    Elysia is a Bun-first web framework designed to leverage Bun's HTTP, file system, and hot reloading APIs. Key features include:

    • Express-like syntax: Familiar routing and middleware patterns.
    • Type inference: Strong TypeScript support for request/response handling.
    • Middleware & Plugins: Support for file uploads, JWT authentication, and tRPC.
    • High Performance: Optimized for the Bun runtime, ranking among the fastest Bun web frameworks.
  7. Overview of the Bun toolkit

    main

    Bun is an all-in-one toolkit for developing modern JavaScript and TypeScript applications. It ships as a single, dependency-free binary called bun and provides several integrated tools:

    • Bun Runtime: A fast JavaScript runtime designed as a drop-in replacement for Node.js. It is written in Rust and powered by JavaScriptCore.
    • Bun Package Manager: A high-performance package manager (up to 30x faster than npm) that supports workspaces, overrides, and audits via bun install.
    • Bun Test Runner: A Jest-compatible, TypeScript-first test runner with support for snapshots, DOM, and watch mode via bun test.
    • Bun Bundler: A native bundler for JS/TS/JSX that supports splitting, plugins, and HTML imports via bun build.
    bun run index.tsx  # Run TS/JSX files directly
    bun install <pkg> # Install packages
    bun build ./index.tsx # Bundle a project
    bun test           # Run tests
    bunx cowsay 'Hello' # Execute a package
  8. Overview of Binary Data types in Bun

    main

    Bun provides several Web-standard and Bun-specific classes for handling binary data. Use these types depending on your specific needs:

    • TypedArray: A family of classes (e.g., Uint8Array, Int32Array) providing an Array-like interface for binary data.
    • Buffer: A Node.js-compatible subclass of Uint8Array with extra convenience methods. Note that Buffer is not available in browsers.
    • DataView: A class for reading/writing bytes to an ArrayBuffer at specific offsets, ideal for binary protocols.
    • Blob: A read-only container for binary data (like a file) with a MIME type and size.
    • File: A subclass of Blob that adds name and lastModified properties.
    • BunFile: (Bun-only) A subclass of Blob representing a lazily-loaded file on disk, created via Bun.file(path).
  9. Use node-inspect-extracted for Node.js-compatible object inspection

    main
    The node-inspect-extracted library provides a Bun-adapted implementation of Node.js's util.inspect functionality. It is used to convert JavaScript objects into human-readable strings, mimicking the behavior of Node.js for compatibility purposes.
  10. Web Streams implementation in Bun

    main

    Bun provides a high-performance WHATWG Streams implementation including ReadableStream, WritableStream, TransformStream, and their associated controllers, readers, and writers. It also supports TextEncoderStream, TextDecoderStream, and ByteLength/Count queuing strategies.

    Unlike many runtimes, Bun's implementation is written entirely in C++ (located in src/jsc/bindings/webcore/streams/) with zero JavaScript builtins, ensuring high performance for stream operations.