Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// A FIFO of byte chunks for length-prefixed framing (control's map// stream, DERP frames): a message that spans many chunks is copied once,// when it is complete, instead of the whole buffer on every chunk. Also// widenConnectAttempts() (Node's connect timeout per address).
const EMPTY = new Uint8Array(0);
export class ChunkQueue { #chunks: Uint8Array[] = []; /** Offset into chunks[0]. */ #head = 0; length = 0;
push(chunk: Uint8Array): void { if (!chunk.length) return; this.#chunks.push(chunk); this.length += chunk.length; }
/** The first n bytes (n <= length), without consuming them; a view when they sit in one chunk. */ peek(n: number): Uint8Array { const c = this.#chunks[0]; if (!c || n === 0) return EMPTY; if (c.length - this.#head >= n) return c.subarray(this.#head, this.#head + n); return this.#copy(n, false); }
/** Removes and returns the first n bytes (n <= length); a view when they sit in one chunk. */ take(n: number): Uint8Array { const c = this.#chunks[0]; if (!c || n === 0) return EMPTY; if (c.length - this.#head >= n) { const out = c.subarray(this.#head, this.#head + n); this.#drop(n); return out; } return this.#copy(n, true); }
clear(): void { this.#chunks = []; this.#head = 0; this.length = 0; }
#copy(n: number, consume: boolean): Uint8Array { const out = new Uint8Array(n); let o = 0, i = 0, pos = this.#head; while (o < n) { const c = this.#chunks[i++]!; const k = Math.min(n - o, c.length - pos); out.set(c.subarray(pos, pos + k), o); o += k; pos = 0; } if (consume) this.#drop(n); return out; }
#drop(n: number): void { this.length -= n; while (n > 0) { const c = this.#chunks[0]!; const avail = c.length - this.#head; if (n < avail) { this.#head += n; return; } n -= avail; this.#chunks.shift(); this.#head = 0; } }}
/** The per-address connect timeout the Tailscale client needs at least on Node (ms). */export const CONNECT_ATTEMPT_MS = 2500;
/** * On Node: raises the process-wide Happy Eyeballs attempt timeout to * CONNECT_ATTEMPT_MS (never lowers it). Node gives each address of a host * 250 ms and then abandons it for the next (a timeout, not a head start), * so a server farther than that (a DERP region on another continent: * Sydney from Europe is 320 ms) never connects where its IPv6 address is * unreachable, and the client dials every region's server. Elsewhere it * does nothing. */export function widenConnectAttempts(): void { const net = (globalThis as { process?: { getBuiltinModule?(id: string): unknown } }).process?.getBuiltinModule?.("node:net") as { getDefaultAutoSelectFamilyAttemptTimeout?(): number; setDefaultAutoSelectFamilyAttemptTimeout?(ms: number): void } | undefined; try { if (net?.getDefaultAutoSelectFamilyAttemptTimeout && net.getDefaultAutoSelectFamilyAttemptTimeout() < CONNECT_ATTEMPT_MS) net.setDefaultAutoSelectFamilyAttemptTimeout?.(CONNECT_ATTEMPT_MS); } catch { // not Node's net }}