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 · Runtime · all subjects

networking

71 notes in this subject, read out of this brain and free to use. This is page 2 of 2.

Redis pub/sub publisher example

Example of a Redis publisher: import { RedisClient } from 'bun'; const writer = new RedisClient('redis://localhost:6379'); await writer.connect(); await writer.publish('general', 'Hello everyone!'); writer.close();

Redis pub/sub subscriber example

Example of a Redis subscriber: import { RedisClient } from 'bun'; const listener = new RedisClient('redis://localhost:6379'); await listener.connect(); await listener.subscribe('general', (message, channel) => { console.log(`Received: ${message}`); });

Why Bun uses 30 seconds for default DNS cache TTL

Bun uses 30 seconds as the default DNS cache TTL because the system API underneath (getaddrinfo) does not expose the TTL of a DNS entry, so Bun picks a number that is long enough to see caching benefits and short enough to be unlikely to cause issues if a DNS entry changes. Amazon Web Services recommends 5 seconds for the Java Virtual Machine, though the JVM's default is to cache indefinitely.

Import dns module from node:dns or bun

Bun implements its own dns module and the node:dns module. You can import dns with 'import * as dns from "node:dns"' or 'import { dns } from "bun"'.

dns.promises.resolve4 with ttl option

dns.promises.resolve4(hostname, options) resolves IPv4 addresses. With {ttl: true} option, it returns an array of objects with address and ttl properties. Example: await dns.promises.resolve4("bun.com", {ttl: true}) returns [{address: "172.67.161.226", ttl: 0}, ...].

dns.prefetch to warm up DNS lookups

dns.prefetch(hostname: string, port?: number): void resolves a hostname before you need it. This is useful for hosts you know you will connect to soon, such as database hosts. The port parameter is optional. Example: dns.prefetch("bun.com", 443).

Bun.dns.lookup backend option

Bun.dns.lookup() accepts a backend option that selects the resolver implementation. The backend option can be: "c-ares" (asynchronous resolver, reads /etc/resolv.conf directly, default on Linux), "system" (platform's own resolver, default on macOS/Windows/Android), or "getaddrinfo" (alias "libc", POSIX getaddrinfo on thread pool). Example: await dns.lookup("example.com", {backend: "system"}).

DNS cache size and duration in Bun

Bun caches DNS lookups automatically. The cache holds up to 256 entries for a maximum of 30 seconds each. If a connection to a host fails, Bun removes that host's entry from the cache. Simultaneous connections to the same host share one DNS lookup.

DNS cache automatic usage in Bun

Bun uses its DNS cache automatically in: bun install, fetch(), node:http (client), Bun.connect, node:net, and node:tls.

dns.getCacheStats returns DNS cache statistics

dns.getCacheStats() returns an object with these properties: cacheHitsCompleted (number of cache hits completed), cacheHitsInflight (cache hits in flight), cacheMisses (cache misses), size (number of items in DNS cache), errors (number of connection failures), totalCount (total number of connection requests including cache hits and misses). Example: {cacheHitsCompleted: 0, cacheHitsInflight: 0, cacheMisses: 0, size: 0, errors: 0, totalCount: 0}.

Configure DNS cache TTL with environment variable

Bun caches DNS entries for 30 seconds by default. To change the TTL, set the BUN_CONFIG_DNS_TIME_TO_LIVE_SECONDS environment variable. Example: BUN_CONFIG_DNS_TIME_TO_LIVE_SECONDS=5 bun run my-script.ts sets the TTL to 5 seconds.

Give your agent this brain