Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113// dev.nibling.npm.getClosure (packages/lexicons/lexicons/dev/nibling/npm/getClosure.json):// the request a client sends and the closure a gateway answers, shared by the kernel that// asks and every gateway that answers. Dependency-free, so a client can bundle it alone.
export const NSID = "dev.nibling.npm.getClosure";/** The query a gateway answers about itself, so a client can check a hostname before keeping it. */export const DESCRIBE_NSID = "dev.nibling.npm.describeGateway";export const CLOSURE_VERSION = 1;export const MAX_SPECS = 50;
export interface ClosureRequest { /** `<name>@<raw spec>`, as a dependent wrote it. */ readonly specs: readonly string[]; /** Package -> the substitute walked at the same version (`esbuild` -> `esbuild-wasm`). */ readonly aliases?: ReadonlyMap<string, string>; /** napi-rs binding suffixes to walk where a package's binding set leaves them out. */ readonly bindings?: readonly string[];}
/** * The answer is NDJSON, one line per event, in the order the walk finds them, so a client * resolves level N while the gateway still walks level N+1: * * - `closure` comes first and names the registry. * - `spec` says `versions` (with the ones this name's earlier lines sent) now holds every * candidate version for `name@raw`. A name's first line carries its `distTags`; a later * one carries only versions not sent before. * - `meta` is the full version document's fields that the abbreviated packument lacks, for * each version the walk picked. * - `absent` is a name the registry answered 404 for; `missing` one the gateway could not * load, which the client fetches itself. * - `wait` says the gateway is still walking while its upstream is slow. It carries nothing, * and comes at least every HEARTBEAT_MS while no other line does, so a client can tell a * slow registry from a gateway that is gone. * - `end` comes last. A stream without it was cut short, and only what arrived holds. */export type ClosureLine = | { readonly t: "closure"; readonly v: typeof CLOSURE_VERSION; readonly registry: string } | { readonly t: "spec"; readonly name: string; readonly raw: string; readonly distTags?: Readonly<Record<string, string>>; readonly versions: Readonly<Record<string, unknown>> } | ({ readonly t: "meta"; readonly id: string } & ClosureMeta) | { readonly t: "absent"; readonly name: string } | { readonly t: "missing"; readonly name: string } | { readonly t: "wait" } | { readonly t: "end" };
/** The longest a gateway goes without sending a line while it walks. */export const HEARTBEAT_MS = 2000;
export const CLOSURE_TYPE = "application/x-ndjson";
export interface ClosureMeta { readonly license?: unknown; readonly funding?: unknown; readonly libc?: unknown;}
/** The query URL for `request` on the gateway at `service` (its service endpoint). */export function closureUrl(service: string, request: ClosureRequest): URL { const url = new URL(`xrpc/${NSID}`, service.endsWith("/") ? service : `${service}/`); for (const spec of [...request.specs].sort()) url.searchParams.append("spec", spec); for (const [pkg, sub] of [...(request.aliases ?? [])].sort(([a], [b]) => (a < b ? -1 : 1))) url.searchParams.append("alias", `${pkg}:${sub}`); for (const suffix of [...(request.bindings ?? [])].sort()) url.searchParams.append("binding", suffix); return url;}
// napi-rs's `<prefix>-<platform>-<arch>[-<abi>]` binding names.const NAPI_BINDING = /^(.+)-(?:darwin|linux|win32|freebsd|android|openharmony|netbsd|openbsd|sunos|aix)-(?:x64|arm64|ia32|arm|riscv64|s390x|ppc64|loong64|universal)(?:-(?:gnu|musl|msvc|gnueabihf|musleabihf|eabi|androideabi))?$/;
/** * The `<prefix>-<suffix>` binding a napi-rs package leaves out: the prefix of two or more * platform bindings among `optional`, all at one spec that `exact` accepts (an exact * version), where `has` (any dependency of the package) has no `<prefix>-<suffix>`. A lone * binding is some other package's, not a loader's set. */export function missingBinding(optional: Iterable<readonly [string, string]>, has: (name: string) => boolean, suffix: string, exact: (spec: string) => boolean): { name: string; spec: string } | undefined { const sets = new Map<string, { count: number; specs: Set<string> }>(); for (const [name, spec] of optional) { const prefix = NAPI_BINDING.exec(name)?.[1]; if (prefix === undefined) continue; const set = sets.get(prefix) ?? { count: 0, specs: new Set() }; set.count++; set.specs.add(spec); sets.set(prefix, set); } for (const [prefix, { count, specs }] of sets) { const name = `${prefix}-${suffix}`; const spec = [...specs][0]!; if (count >= 2 && specs.size === 1 && !has(name) && exact(spec)) return { name, spec }; } return undefined;}
/** Each line of an NDJSON body, parsed, as it arrives. A line that is not JSON ends the * stream, as a cut-short one does: what came before it still holds. */export async function* closureLines(body: ReadableStream<Uint8Array>): AsyncGenerator<unknown> { const decoder = new TextDecoder(); let buffered = ""; for await (const chunk of body as unknown as AsyncIterable<Uint8Array>) { buffered += decoder.decode(chunk, { stream: true }); let newline: number; while ((newline = buffered.indexOf("\n")) !== -1) { const line = buffered.slice(0, newline); buffered = buffered.slice(newline + 1); if (!line.trim()) continue; try { yield JSON.parse(line); } catch { return; } } }}