anonymousconstconst anonymous: TokenSourceNo token: only what is public can be read.
Where the token of a host comes from, a closed union: none (what is public), a token given in code (what the tests use, a throwaway of a server of their own), the environment through a function that reads it so that nothing reads the process behind a caller's back, or a provider, a function that gives it or says why not, which is how a short-lived token is fetched and how a secret is asked of a person through the credential port of Headless. A token is never part of a place, never in a URL and never in a sentence: it travels in one header of one request, and what is written of one is [redacted].
import { anonymous, staticToken, redactor } from "./token.ts";
const t = staticToken("ghp_exampleexampleexample");
t.kind === "ok" && t.value.kind; // "static"
redactor(["ghp_exampleexampleexample"])("the host said ghp_exampleexampleexample"); // "the host said [redacted]"anonymousconstconst anonymous: TokenSourceNo token: only what is public can be read.
cachedTokenfunctionfunction cachedToken(source: TokenSource, now?: () => number): () => Promise<Result<string | undefined, string>>A source that asks its provider again only after ttlMs (never, for a token in code and the environment), so a person is asked once and a short-lived token is fetched when it is about to lapse.
environmentTokenconstconst environmentToken: (env: (name: string) => string | undefined, name: string) => TokenSourceThe token of a variable of the environment: a function that reads it, which is the only place the process is touched.
providedTokenconstconst providedToken: (fetch: () => Promise<Result<string, string>>, ttlMs?: number | undefined) => TokenSourceThe token by a function: for a short-lived token, which it gives again, and for a secret asked of a person.
redactorfunctionfunction redactor(tokens: readonly string[]): (text: string) => stringA function that takes every token it is given out of a sentence, so that no failure, no detail and no log line holds one.
resolveTokenfunctionfunction resolveToken(source: TokenSource): Promise<Result<string | undefined, string>>The token a source gives now: none for the source that has none, or why it gives none. A sentence never holds a token.
staticTokenfunctionfunction staticToken(token: string): Result<TokenSource, string>A token given in code, or why it is not one a host would take.
TokenSourcetypeno test imports ittype TokenSource =
| { readonly kind: "none" }
| { readonly kind: "static"; readonly token: string }
| { readonly kind: "environment"; readonly env: (name: string) => string | undefined; readonly name: string }
| { readonly kind: "provider"; readonly fetch: () => Promise<Result<string, string>>; readonly ttlMs?: number };Where the token comes from.
usableconstconst usable: (token: string) => booleanWhether a text can be a token in a header.