import type { ProductId } from "@parity/truapi"; import type { ProductRuntimeConfig, LogLevel, PermissionAuthorizationRequest, PermissionAuthorizationStatus, ExecutableKind, RequiredHostCallbacks, TrUApiProductProvider, WorkerDemandChange } from "../index.js"; import type { HostRole } from "../worker-protocol.js"; export type WebWorkerHostConfig = Omit; export interface WorkerPairingHostRuntime { /** * The encoding core's wire-schema hash, when the core reports one. * * An in-host debugger tap runs on this side of the worker boundary and has no * other way to reach it, so without this it can only stamp frames with the * page bundle's own constant — a different artifact from the core that * actually encoded them. The debugger then refuses to decode, exactly as it * should. Undefined for a core built before the export existed. */ readonly coreWireSchemaHash: string | undefined; createProvider(product: { productId: ProductId; executionKind?: ExecutableKind; }): Promise; disconnectSession(): Promise; cancelPairing(): void; notifySessionStoreChanged(): void; /** * Tell the core the host's contacts changed. Call it whenever a contact is * removed or blocked, so a contact handle the core cached stops resolving. */ notifyContactsChanged(): void; /** * Restore the session persisted in the core's `AuthSession` slot. Resolves * once product frames may use it, so a host can await this at boot before * routing. Rejects when the runtime has been disposed or the worker faulted, * so a host never routes on an activation that did not run. */ activateStoredSession(): Promise; /** * Install an already-paired session the host holds itself, without copying * it into core storage. Rejects on a disposed runtime, as * {@link WorkerPairingHostRuntime.activateStoredSession} does. */ activateExternalSession(blob: Uint8Array): Promise; /** * Establish a session from host-held BIP-39 entropy. * * Signing hosts only. A pairing host has no local secret and rejects this: * it waits for a wallet to answer over the statement-store channel instead. */ activateLocalSession(secret: Uint8Array, liteUsername?: string): Promise; /** * Answer resource allocation as granted without performing it. * * Test hosts only, on a core built with `test-host`. No allowance is * allocated, so preimage submissions also stay in the core: the Bulletin * allowance was never authorized on chain to submit with. */ setGrantAllowancesUnchecked(granted: boolean): Promise; /** * Keep preimage submissions in the core instead of the Bulletin chain. * * Test hosts only, on a core built with `test-host`. For a host whose wallet * answers allowances itself: that Bulletin allowance was never authorized on * chain, so a real `store` is refused at dry-run. The product gets the content * key back and reads the value from the core's lookup cache. A refused * Bulletin allowance still refuses the submission. */ setSubmitPreimagesLocally(local: boolean): Promise; /** * Answer these resource tags as refused, replacing any earlier set. * * Withholding one resource while the rest stay granted is what lets a suite * prove its product survives a refusal it cannot otherwise arrange. */ setWithheldResources(tags: string[]): Promise; /** * Drop the active paired session without notifying the peer. Rejects on a * disposed runtime, as * {@link WorkerPairingHostRuntime.activateStoredSession} does. */ resetSessionState(): Promise; /** * Forget one product: revoke its grants, Allow once answers and keys, and * remove the values it wrote to product storage through * `productStorage.clearProduct`. The session and other products are kept. * Rejects on a disposed runtime, as * {@link WorkerPairingHostRuntime.activateStoredSession} does. */ clearProductState(productId: ProductId): Promise; getPermissionAuthorizationStatus(productId: ProductId, request: PermissionAuthorizationRequest): Promise; getPermissionAuthorizationStatuses(productId: ProductId, requests: PermissionAuthorizationRequest[]): Promise; setPermissionAuthorizationStatus(productId: ProductId, request: PermissionAuthorizationRequest, status: PermissionAuthorizationStatus): Promise; getSessionChatIdentityKey(): Promise; getDeviceStatementKey(): Promise; getDeviceEncryptionKey(): Promise; /** Validate and normalize a product identifier through the Rust core. */ parseProductId(value: string): Promise; getProductSubtreePublicKey(productId: ProductId, timeoutMs?: number): Promise; /** * Take one reference on a product's worker for a modality holder that is on * screen or in flight. Pair every call with one * {@link WorkerPairingHostRuntime.releaseWorker}. The core counts; the * resulting level reaches * {@link WorkerPairingHostRuntime.subscribeWorkerDemand} listeners, and the * host runs and stops the worker executable itself. */ acquireWorker(productId: ProductId): void; /** Release one reference. Releasing with none held is a no-op. */ releaseWorker(productId: ProductId): void; /** * Observe which product workers the host should run. The listener first * receives `wanted: true` for every product wanted right now, then each * change as it happens, and `wanted: false` for every remaining product * when the runtime is disposed. Returns the unsubscribe. */ subscribeWorkerDemand(listener: (change: WorkerDemandChange) => void): () => void; setLogLevel(level: LogLevel): void; dispose(): void; } /** * Why the wire debugger is (not) enabled, so a no-dial is never silent. No * browser store and no runtime switch: the host passes the dial in, the build's * value is the default, and it is resolved once. */ export type DebuggerEnablement = { readonly url: string | null; readonly reason: "enabled-from-option" | "enabled-from-build" | "production-build" | "production-build-configured" | "refused-not-loopback" | "not-configured"; }; /** * Which of the two production verdicts applies. The build half is the one that * matters: the env var is substituted at build time, so it is still readable in * a production bundle, and a build made with it but without * `NODE_ENV=development` is exactly the case that must not go quiet. */ export declare function productionReason(fromOption: string | null | undefined, fromBuild: string | null): "production-build" | "production-build-configured"; /** * Resolve the dev-build switches into one verdict. Exported so the precedence is * testable without a bundler. * * Precedence, where an omitted option is the only one that defers to the build: * * - option set to a URL -> dial it, whatever the build says * - option set null/"" -> OFF, whatever the build says * - option omitted -> the build's value, if it carries one * * Folding `null` in with "omitted" is the easy mistake: it falls through to the * build, leaving an embedder that compiled a URL in no way to refuse the dial * short of rebuilding. A resolved URL is loopback `ws://` or it is refused (§6). */ export declare function resolveDebuggerEnablement(fromOption: string | null | undefined, fromBuild: string | null): DebuggerEnablement; export interface CreateWebWorkerPairingHostRuntimeOptions { logLevel?: LogLevel; hostConfig: WebWorkerHostConfig; initTimeoutMs?: number; /** * Dev-only: a loopback `ws://` wire debugger to stream tapped frames to. * * Omit to take what the build was compiled with * (`VITE_TRUAPI_DEBUGGER_URL`); pass `null` or `""` to refuse it even when the * build carries one. Ignored outside a dev build. Resolved once, at creation, * and not changeable from the page. */ debugger?: string | null; /** * Dev-only: whether to show the built-in indicator while a dial is live. * * Defaults to `true`. Pass `false` only when this host renders its own visible * signal - the point is that a tap streaming frames off this host is never * invisible, not that this particular badge is used. */ debuggerIndicator?: boolean; /** * Host role the worker constructs. Omitted means `"pairing"`. * * `"signing"` requires a worker loading the `testing` WASM bundle, the only * one built with a signing host in it. */ role?: HostRole; /** * How long `dispose()` waits for open `worker.beginOperation` holds before * tearing down anyway. Defaults to 30s. */ operationGraceMs?: number; } export type WebWorkerHostCallbacks = RequiredHostCallbacks; export declare function createWebWorkerPairingHostRuntime(worker: Worker, host: WebWorkerHostCallbacks, options: CreateWebWorkerPairingHostRuntimeOptions): Promise; /** * Put `owner`'s debugger dial into service: say once whether it will dial, show * the endpoint in the page for as long as it does, and hand back the URL the * worker's `init` message carries. * * The three are one decision, so they are one function: a host that resolves a * dial and then reports, badges or forwards something else is the §9 failure. * Taking the resolved enablement as an argument is what makes it testable. */ export declare function installDebuggerDial(owner: object, enablement: DebuggerEnablement, indicator: boolean | undefined): string | null; /** * Take `owner`'s dial out of service. Its worker is gone, so nothing streams on * its account any more, and a badge naming an endpoint no frame reaches is the * silent-tap failure read backwards. */ export declare function releaseDebuggerDial(owner: object): void;