This page documents Bun's built-in utility APIs for parsing and serializing structured data formats, inspecting JavaScript values, hashing, and working with web platform data types such as FormData. These APIs live on the globalThis.Bun object or are exposed as globals.
For the overall shape of the Bun global object and how it is initialized, see page 9.1 For file I/O helpers such as Bun.file() and Bun.write(), see page 9.2 For the SQL and SQLite APIs, see page 9.5
Bun.YAML is a native YAML 1.2 parser and serializer exposed as a pair of methods: YAML.parse() and YAML.stringify().
The implementation is split into two layers: a JavaScript-facing host object and a high-performance Rust-based parser.
Diagram: YAML API — JS surface to Rust implementation
Sources: src/runtime/api/YAMLObject.rs1-22 src/parsers/yaml.rs23-66
The JS binding is registered in YAMLObject::create(), which produces a host function object with both parse and stringify attached src/runtime/api/YAMLObject.rs14-22 The parser itself is implemented as a generic Parser<Enc> struct in src/parsers/yaml.rs, parameterized by an Encoding trait with associated type Unit (u8 for Latin-1/UTF-8, u16 for UTF-16) src/parsers/yaml.rs3-9 The default call path uses Parser<Utf8> src/parsers/yaml.rs33-35
YAML.parse(input) accepts a wide range of input types in addition to plain strings:
| Input type | Notes |
|---|---|
string | Primary path |
Buffer | Supports sliced views and offsets |
ArrayBuffer | Directly consumed test/js/bun/yaml/yaml.test.ts20-25 |
TypedArray | Uint8Array through BigUint64Array test/js/bun/yaml/yaml.test.ts27-126 |
DataView | Parsed via underlying buffer test/js/bun/yaml/yaml.test.ts128-134 |
Blob, File | Synchronously read; no await needed test/js/bun/yaml/yaml.test.ts136-140 |
Sources: test/js/bun/yaml/yaml.test.ts10-140
Return value behavior:
null src/parsers/yaml.rs47--- separators) are converted into a JavaScript array of values src/parsers/yaml.rs49-64Bun's YAML parser follows YAML 1.2 Core Schema semantics. It handles complex block scalars including various chomping (-, +) and indentation indicators src/parsers/yaml.rs159-244
| YAML token | Bun result | Notes |
|---|---|---|
true, True, TRUE | true (boolean) | Core Schema test/js/bun/yaml/yaml.test.ts365-370 |
yes, no | "yes", "no" (string) | Unlike YAML 1.1, these are not booleans |
0o777 | 511 (integer) | Octal indicator |
.inf, .nan | Infinity, NaN | Floating point constants |
Sources: test/js/bun/yaml/yaml.test.ts365-413 src/parsers/yaml.rs159-244
YAML.stringify(value[, replacer[, space]]) serializes a JavaScript value to YAML. The Stringifier struct performs a pre-pass with find_anchors_and_aliases() to detect values that appear more than once in the object graph so they can be emitted as YAML anchors and aliases src/runtime/api/YAMLObject.rs25-53
The parser surfaces errors through YamlParseError src/parsers/yaml.rs68-76:
OutOfMemory: Arena allocation failure.SyntaxError: Malformed YAML input.StackOverflow: Deeply nested document exceeded stack limit.Bun.TOML.parse(string) parses a TOML document and returns a JavaScript object. It is used internally by the CLI to read bunfig.toml configuration files.
Bun.JSONC.parse(string) parses JSON with Comments. It supports single-line (//) and block (/* */) comments as well as trailing commas, making it compatible with tsconfig.json and package.json extensions.
Bun.inspect(value[, options]) produces a human-readable string representation of any JavaScript value. It is the implementation backing console.log() output.
Diagram: Bun.inspect output for different value types
Sources: test/js/bun/util/inspect.test.js5-251
| Feature | Behavior |
|---|---|
| Getters | Shown as [Getter], never invoked during inspection test/js/bun/util/inspect.test.js27-49 |
| Circularity | Detected and shown as [Circular] |
| Prototypes | Inspects standard prototypes like Request.prototype test/js/bun/util/inspect.test.js5-25 |
| Timers | Shows ID and repeat status for setTimeout/setInterval test/js/bun/util/inspect.test.js85-92 |
| Blobs | Shows size and type; Bun.file shows path or FD test/js/bun/util/inspect.test.js103-112 |
Bun.hash provides fast non-cryptographic hashing. The default implementation uses Wyhash bun_core/util.rs100-104
| Function | Algorithm |
|---|---|
Bun.hash(input) | Wyhash (returns bigint) |
Bun.hash.adler32(input) | Adler-32 |
Bun.hash.crc32(input) | CRC-32 |
Bun.hash.cityHash64(input) | CityHash64 |
Bun.hash.xxHash3(input) | xxHash3 64-bit |
FormData is the standard Web API class for representing form fields and file uploads.
The implementation is provided via FormData.zig src/runtime/webcore/FormData.zig1-20 It supports:
Blob/File objects test/js/web/html/FormData.test.ts6-27File objects or allows overriding during append test/js/web/html/FormData.test.ts36-64Response.formData() and Request.formData() parse multipart/form-data bodies using boundary delimiters test/js/web/html/FormData.test.ts66-159Bun provides high-performance glob matching through the Bun.Glob class and path.matchesGlob.
The underlying engine is implemented in GlobWalker.zig src/glob/GlobWalker.zig1-23 It uses a specialized SyscallAccessor to perform directory traversal and matching efficiently across different operating systems, including Windows-specific optimizations src/glob/GlobWalker.zig98-111
Diagram: Glob Scanning Data Flow
Sources: src/glob/GlobWalker.zig113-181 test/js/bun/glob/scan.test.ts23-50
** for deep directory traversal test/js/bun/glob/scan.test.ts62-82absolute: true option test/js/bun/glob/scan.test.ts145followSymlinks test/js/bun/glob/scan.test.ts44-50Diagram: Utility API implementation layers
Sources: src/runtime/api/YAMLObject.rs1-22 src/parsers/yaml.rs23-66 src/runtime/webcore/FormData.zig1-20 src/glob/GlobWalker.zig1-23 src/bun_core/util.rs100-104
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.