new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · all subjects

runtime/utils

39 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Bun.deepEquals() signature and basic behavior

Bun.deepEquals() checks if two objects are deeply equal. It takes two arguments (the objects to compare) and returns a boolean. This function is used internally by expect().toEqual() in Bun's test runner.

Bun.deepEquals() strict mode parameter

Bun.deepEquals() accepts an optional third argument to enable strict mode. When true is passed as the third argument, strict mode is enabled. In strict mode, undefined values, undefined elements in arrays, sparse arrays, and differences between object literals and class instances with the same properties are not considered equal.

Bun.deepEquals() strict mode differences from non-strict

In non-strict mode, Bun.deepEquals({}, {a: undefined}) returns true, but in strict mode it returns false. Similarly, ["asdf"] and ["asdf", undefined] are equal in non-strict mode but not in strict mode. Sparse arrays like [, 1] are equal to [undefined, 1] in non-strict mode but not in strict mode. Object literals and class instances with identical properties are equal in non-strict mode but not in strict mode.

deflateSync and inflateSync example

Example of DEFLATE compression and decompression: const data = Buffer.from("Hello, world!"); const compressed = Bun.deflateSync("Hello, world!"); // => Uint8Array const decompressed = Bun.inflateSync(compressed); // => Uint8Array

Bun.deflateSync() compresses data with DEFLATE

Bun.deflateSync() compresses a string or Uint8Array using DEFLATE compression and returns a Uint8Array.

Bun.inflateSync() decompresses DEFLATE data

Bun.inflateSync() decompresses a DEFLATE-compressed Uint8Array and returns the decompressed Uint8Array.

Bun.escapeHTML() function signature and behavior

Bun.escapeHTML() escapes HTML characters in a string. The function converts non-string values to a string before escaping. It is optimized for large input.

Bun.escapeHTML() character replacements

Bun.escapeHTML() makes the following replacements: double quote (") becomes &quot;, ampersand (&) becomes &amp;, single quote (') becomes &#x27;, less-than (<) becomes &lt;, and greater-than (>) becomes &gt;.

Bun.escapeHTML() example

Bun.escapeHTML("<script>alert('Hello World!')</script>"); returns &lt;script&gt;alert(&#x27;Hello World!&#x27;)&lt;/script&gt;

Bun.gunzipSync() decompresses gzip data

Bun.gunzipSync() takes a gzip-compressed Uint8Array and returns a decompressed Uint8Array.

Example of gzip compression with Bun.gzipSync() and Bun.gunzipSync()

const data = Buffer.from("Hello, world!"); const compressed = Bun.gzipSync(data); // => Uint8Array const decompressed = Bun.gunzipSync(compressed); // => Uint8Array

Bun.gzipSync() compresses Uint8Array with gzip

Bun.gzipSync() takes a Uint8Array and returns a gzip-compressed Uint8Array. It can accept a Buffer since Buffer is a Uint8Array subclass.

Bun.password.hash() with Argon2id options

Bun.password.hash() can be called with a second argument to configure hashing parameters. For Argon2id, pass an object with algorithm: 'argon2id', memoryCost (in kibibytes, minimum 8), and timeCost (number of iterations). Example: await Bun.password.hash(password, { algorithm: 'argon2id', memoryCost: 8, timeCost: 3 })

Bun.password.verify() function

Bun.password.verify() verifies a password against a hash. It takes two arguments: the plaintext password and the hash string. It returns a promise resolving to a boolean indicating whether the password matches. The hash stores the algorithm and its parameters, so they do not need to be specified again. Example: const isMatch = await Bun.password.verify('super-secure-pa$$word', hash); // => true

Bun.password.hash() basic usage

Bun.password.hash() securely hashes passwords with no third-party dependencies. It is a built-in function that takes a password string and returns a promise resolving to a hashed password string. By default it uses the Argon2id algorithm. Example: const hash = await Bun.password.hash('super-secure-pa$$word'); produces a hash starting with $argon2id$v=19$m=65536,t=2,p=1$...

Bun.password.hash() with bcrypt options

Bun.password.hash() supports the bcrypt algorithm. To use bcrypt, pass an object with algorithm: 'bcrypt' and cost property with a number between 4-31. Example: await Bun.password.hash(password, { algorithm: 'bcrypt', cost: 4 })

import.meta.file returns current file name

Use import.meta.file to retrieve the name of the current file. It returns only the file name, not the full path. For example, in a file at /a/b/c.ts, import.meta.file returns 'c.ts'.

import.meta.file example

import.meta.file; // => "c.ts" This example shows that when import.meta.file is used in a file named c.ts, it evaluates to the string 'c.ts'.

Example: crypto.randomUUID()

crypto.randomUUID(); // => "123e4567-e89b-42d3-a456-426614174000"

Example: Bun.randomUUIDv7()

Bun.randomUUIDv7(); // => "0196a000-bb12-7000-905e-8039f5d5b206"

crypto.randomUUID() generates UUID v4

Use crypto.randomUUID() to generate a UUID v4. This function works in Bun, Node.js, and browsers with no dependencies. It returns a string like "123e4567-e89b-42d3-a456-426614174000".

Bun.randomUUIDv7() generates UUID v7

Bun provides Bun.randomUUIDv7() to generate a UUID v7. It returns a string like "0196a000-bb12-7000-905e-8039f5d5b206".

Bun.main example usage

console.log(Bun.main);

Bun.main property returns absolute path to entrypoint

The Bun.main property contains the absolute path to the current entrypoint file. The printed path is the file that was executed with bun run. When running bun run index.ts, Bun.main returns /path/to/index.ts. When running bun run foo.ts, Bun.main returns /path/to/foo.ts.

Bun.pathToFileURL() converts absolute path to file URL

Bun.pathToFileURL() takes an absolute path and converts it to a file:// URL. It returns an object with an href property containing the file URL string. For example, Bun.pathToFileURL("/path/to/file.txt").href returns "file:///path/to/file.txt".

Bun.pathToFileURL() example usage

Bun.pathToFileURL("/path/to/file.txt").href; // => "file:///path/to/file.txt"

Bun.sleep() example

await Bun.sleep(1000);

Bun.sleep() signature and behavior

Bun.sleep() returns a void Promise that resolves after a given number of milliseconds. It takes a single parameter: the number of milliseconds to sleep. The function is equivalent to await new Promise(resolve => setTimeout(resolve, ms)).

Bun.revision property

Bun.revision contains the exact git commit hash of oven-sh/bun that was compiled to produce the Bun binary. For example, Bun.revision returns "49231b2cb9aa48497ab966fc0bb6b742dacc4994".

Bun.version property

Bun.version contains the current version of Bun in semver format. For example, Bun.version returns "1.3.3".

Bun.which finds path to executable

Bun.which is a function that finds the absolute path of an executable file, similar to the which command on Unix-like systems. When given an executable name, it returns the full path as a string. If the executable is not found, it returns null.

Bun.which signature and examples

Bun.which(name: string): string | null. Examples: Bun.which("sh") returns "/bin/sh", Bun.which("notfound") returns null, Bun.which("bun") returns "/home/user/.bun/bin/bun".

console.log writes to stdout with line break

The console.log function writes to stdout and automatically appends a line break to the printed data.

performance.now() for precise time measurement

Bun provides the Web-standard performance.now() function to measure time precisely. This function returns elapsed time since application start.

Bun.nanoseconds() returns time in nanoseconds since app start

Bun.nanoseconds() returns the time since the application started in nanoseconds. Use performance.timeOrigin to convert this to a Unix timestamp.

Install @types/bun for TypeScript definitions

@types/bun is the package that provides TypeScript type definitions for Bun's built-in APIs. It should be installed as a dev dependency using `bun add -d @types/bun`. Once installed, the Bun global can be referenced in TypeScript files without editor errors.

Suggested compilerOptions for Bun projects

A tsconfig.json for Bun projects should include these compilerOptions: Environment setup: - lib: ["ESNext"] - target: "ESNext" - module: "Preserve" - moduleDetection: "force" - jsx: "react-jsx" - allowJs: true - types: ["bun"] Bundler mode: - moduleResolution: "bundler" - allowImportingTsExtensions: true - verbatimModuleSyntax: true - noEmit: true Best practices: - strict: true - skipLibCheck: true - noFallthroughCasesInSwitch: true - noUncheckedIndexedAccess: true - noImplicitOverride: true Other flags (disabled by default): - noUnusedLocals: false - noUnusedParameters: false - noPropertyAccessFromIndexSignature: false

Bun supports top-level await, JSX, and .ts imports

Bun supports top-level await, JSX, and imports with .ts extensions natively. TypeScript does not allow these features by default, so the suggested compilerOptions in a Bun project are specifically configured to enable them.

TypeScript 6 and 7 require types compilerOption

When using TypeScript 6.0 or later with Bun, the compilerOptions must include "types": ["bun"] to properly resolve Bun type definitions.

Give your agent this brain