/** How the vendor signs a delivery. */ export type EventScheme = /** One header carrying the destination's secret as it is, no signature over the body (Cloudflare Notifications' * `cf-webhook-auth`, the secret a webhook destination was made with). */ { kind: 'secret'; header: string; } /** A message id, a timestamp and a signature header (the Standard Webhooks shape): `-id`, * `-timestamp` (seconds) and `-signature: v1,` over `${id}.${timestamp}.${body}`, * keyed by the base64 part of the secret after `secretPrefix`, or by the secret's own UTF-8 bytes (`secretKey: 'utf8'`: * Polar's, whose SDK base64-encodes the secret as it is before verifying). */ | { kind: 'webhook-id'; prefix: string; secretPrefix?: string; secretKey?: 'base64' | 'utf8'; } /** One header of a timestamp and its signatures: `
: t=,v1=` over `${t}.${body}`, * keyed by the secret as it is; `version` names the signature's key when the vendor's is not `v1` (ElevenLabs' * `ElevenLabs-Signature: t=…,v0=…`). */ | { kind: 'timestamp-v1'; header: string; version?: string; } /** One HMAC header over the body (`X-Hub-Signature-256: sha256=`, GitHub's), or over `${timestamp}.${body}` when * the vendor sends its timestamp in `timestampHeader`; `signed` names what is signed otherwise (`{t}` the timestamp * as the header carries it, `{t.s}` and `{t.ms}` in seconds and milliseconds, `{body}` the body, `{method}` and * `{url}` the request's: Slack's `v0:{t}:{body}`, HubSpot v3's `{method}{url}{body}{t}` with `timestampUnit: 'ms'`), * and `algorithm` the hash (SHA-256 unless named: GitHub's older `X-Hub-Signature` is SHA-1). */ | { kind: 'hmac'; header: string; encoding: 'hex' | 'base64'; prefix?: string; timestampHeader?: string; timestampUnit?: 's' | 'ms'; signed?: string; algorithm?: 'sha1' | 'sha256'; } /** An Ed25519 signature (Discord's interactions: `X-Signature-Ed25519` in hex over `{t}{body}`, the seconds in * `X-Signature-Timestamp`), by the private key (PEM) held at the endpoint's field `key` (a dotted path, through its * joins: an application's key pair); `signed` as `hmac`'s, `{t}{body}` unless named. */ | { kind: 'ed25519'; key: string; header: string; timestampHeader: string; signed?: string; encoding?: 'hex' | 'base64'; } /** An RS256 JWT in a header (`Authorization: Bearer `, Pub/Sub push's OIDC token), by the private key (PEM) at the * endpoint's field `key`, carrying the key's `kid`: `iat` and `exp` (`expiresIn` seconds after, an hour unless named) * on the receiver's clock, and the declared `claims`, each `$endpoint.`, `$url` (the delivery's URL) or written * as it is (one left undefined is not sent). */ | { kind: 'oidc-jwt'; key: string; claims: Record; header?: string; prefix?: string; expiresIn?: number; kidPrefix?: string; } /** An RSA (SHA-256) signature over a canonical string by the private key (PEM) at the endpoint's field `key`, with the * URL of the certificate that verifies it (`certUrl`, `$endpoint.` or written as it is), as SNS signs. `signed` * is a template as `hmac`'s (`{body.}` a field of the JSON body), or `fields`: each named body field present, * as `\n\n` (SNS's string to sign). `at` places the signature, the certificate URL and the timestamp * (`timestamp.unit`: ISO 8601 unless named) in headers or, `body`, in fields of the JSON body, the timestamp set * before the string is made. */ | { kind: 'rsa-x509'; key: string; certUrl: string; signed: string | { fields: ReadonlyArray; }; at: 'header' | 'body'; signature: string; certUrlAt: string; timestamp?: { at: string; unit?: 'iso' | 's' | 'ms'; }; encoding?: 'base64' | 'hex'; } /** One header of a plain hash (not an HMAC) of the secret and the body (HubSpot's v1 `X-HubSpot-Signature`: the SHA-256 * of the app's client secret followed by the request body); `signed` names the input, `{secret}` and `{body}`; * `base64-unpadded` is base64 with its trailing `=` removed (E2B's `e2b-signature`) */ | { kind: 'hash'; header: string; signed: string; encoding: 'hex' | 'base64' | 'base64-unpadded'; algorithm?: 'sha256'; }; /** Where the World's endpoints of one kind are kept (a door the vendor's dashboard stands in for writes them): their * stored type and the fields holding the URL, the signing secret, the event types an endpoint takes (all when absent, * `*` and `.*` patterns) and whether it is disabled (`disabled` true), not enabled (`enabled` false) or not live * (`status`). A field may be a dotted path into the row (`config.url`). `url` is needed by an HTTP delivery alone, and * `secret` by a shared-secret scheme alone (an endpoint without one is live without it). */ export type EndpointsDecl = { storedAs: string; url?: string; secret?: string; filter?: string; disabled?: string; enabled?: string; /** When the vendor turns an endpoint off for failing (Polar after ten failures in a row, Clerk and Resend after five * days of failing, Slack under a 5% success rate): after `failures` consecutive failed attempts, when its failures * have run for `failingFor` of World time, or when fewer than `successRate.below` (0 to 1) of its last * `successRate.of` attempts succeeded; the kernel then writes `set` (top-level field → value) on the endpoint's row, * the vendor's own disabled state, which `disabled`, `enabled` or `status` reads. */ disable?: { failures?: number; failingFor?: string; successRate?: { below: number; of: number; }; set: Record; }; /** An endpoint that is live only while a field holds one value (Stripe's `status: 'enabled'`). */ status?: { field: string; live: string; }; /** Which events an endpoint takes by whom they are about: one whose `field` is true takes only events whose * envelope value `value` is set (Stripe's Connect endpoint, `connect: true`, takes connected accounts' events, those * with an `$account`), and any other only events where it is not. */ scope?: { field: string; value: string; }; /** Endpoint fields that must equal an event's values (field → `$value`, or a literal): a repository's hook takes only * its repository's events (GitHub's `{ repository: '$repository' }`), an organization's its organization's. A list * field takes the event when it holds the value. Several matches: the endpoint takes the event when any holds (a * GitHub App's installation: its own events, its account's when it covers all the account's repositories, a listed * repository's). */ match?: Record | ReadonlyArray>; /** Related rows an endpoint's fields read through (`app.hook_url`): each join's name, the stored type it names and the * endpoint's field holding its id (a GitHub installation's URL, secret and events are its App's). */ joins?: Record; /** The event types this kind of endpoint takes (every type when absent), or those it does not (`skip`): two kinds over * the same rows, one taking a type under a `match` the other has no need of (a Slack app is sent `app_mention` only * when a message mentions it, and every `message` its subscription names). */ types?: ReadonlyArray; skip?: ReadonlyArray; }; /** How a delivery reaches its receiver: * - `http` (the default): a POST to the endpoint's URL, through the World's application route and egress rule; * - `socket`: a frame into each open session of the vendor's socket (sockets.ts) bound to the endpoint, a session whose * `state[session]` (`endpoint` unless named) holds the endpoint's id (Discord's Gateway, Slack's Socket Mode). The * frame is `frame`, a template as the envelope is with `$event` the rendered event and `$seq` the delivery's sequence * number (increasing in the twin, never reused), the event itself when absent; a session that resumes after the * last sequence number it saw is sent every later frame of its endpoint again (`SocketSession.resume`). An attempt * no bound session is open for fails; * - `twin`: a request to another vendor's operation, as the app would call it and routed to that vendor's twin in the * World (Gmail publishing to Pub/Sub, S3 to SQS or SNS): `url` with `{endpoint.}` filled from the endpoint, * `body` a template with `$event`, `$event.text` and `$event.base64` (the rendered event, its JSON text, that text * in base64) and `$endpoint.`, `headers` as the delivery's further headers. The receiving twin owns its * retries, acknowledgements and signing: such a delivery declares no scheme (`[]`) and, usually, `retry: 'none'`. */ export type DeliveryTransportDecl = 'http' | { socket: string; session?: string; frame?: unknown; } | { twin: { method?: string; url: string; body?: unknown; headers?: Record; }; }; /** The vendor's retry schedule: the delays after each failed attempt (`['1h', '2h', '4h']`: four attempts in all); * `none`, a single attempt (GitHub's); or exponential backoff within a window where the vendor publishes the window * and not the delays (Vercel within 24 hours, Discord within 10 minutes), which `undocumented` marks: the delays double * from `first` (a minute, or an eighth of a shorter window) while their sum stays within the window, and each packet * records that its schedule is not the vendor's published one. */ export type RetryDecl = ReadonlyArray | 'none' | { backoff: 'exponential'; within: string; undocumented: true; first?: string; }; /** The events a pack's writes send. */ export type EventsDecl = { /** how each delivery is signed; several schemes each add their headers (GitHub's SHA-256 and SHA-1 signatures); `[]` * signs nothing (a delivery into another twin, which signs its own) */ scheme: EventScheme | ReadonlyArray; /** how a delivery reaches its receiver: HTTP to the endpoint's URL unless named */ transport?: DeliveryTransportDecl; /** further headers each delivery carries, templates as the envelope's are, plus `$endpoint.` for the endpoint * it goes to (GitHub's `X-GitHub-Event`, `X-GitHub-Delivery` and `X-GitHub-Hook-ID`), and the attempt's own: * `$attempt` (1 for the first), `$retry` (the retry's number, absent on the first attempt) and `$retryReason` (why * the attempt before failed: `http_timeout`, `http_error` or `connection_failed`; Slack's `x-slack-retry-num` and * `x-slack-retry-reason`). A header whose value is absent is not sent. */ headers?: Record; /** How long an attempt waits for its answer (`3s`, a duration as a scenario's): one not answered in time fails as * `http_timeout`. Absent, the transport's own limit. */ timeout?: string; /** When the body is rendered: once at the write (the default), or at each attempt (Linear's `webhookTimestamp`, * HubSpot's attempt number, Resend loading the object as it is then): the pack's render is called again with the * write and its `attempt`, and the envelope is filled again, with `$attempt` and `$time.attempt.ms`, `.s` and `.iso` * (the receiver's clock) beside the write's values. */ renderAt?: 'write' | 'attempt'; /** Events for one endpoint sent together as one request whose body is their array (HubSpot's up to 100), in the order * they were made, at most `max` a request: each event waits `within` (World time) and its first attempt gathers * every event for the endpoint not yet sent; without `within`, those queued when the first is attempted. */ batch?: { max: number; within?: string; }; /** A write's kernel operation (`user.create`) → the vendor's event type (`user.created`), or several (a Slack message * sends `message` and `app_mention`; `values` withholds one the write does not warrant); a write not named sends none. */ types: Record>; /** The event types the vendor enumerates, when it names every write's event by its resource: an operation that is * one of them sends it, and `.create|update|delete` sends `.created|updated|deleted` when that is * one of them (a vendor whose spec lists its event types: Stripe's). `types` is read first. */ known?: ReadonlyArray; /** The vendor's own resource each event is also kept as (its Events API: Stripe's `GET /v1/events`), the envelope as * its fields under the event's id, whether or not an endpoint takes it. */ store?: string; /** The stored event's field the kernel keeps as the number of its deliveries not yet answered 2xx (Stripe's * `pending_webhooks`): the endpoints it goes to at the write, one fewer at each delivery's first acceptance. */ pending?: string; /** The event as the vendor sends it, a template: a string `$type`, `$data`, `$id`, `$time.ms`, `$time.s`, `$time.iso`, * `$request.client_ip` or `$request.user_agent` is replaced by that value; everything else is sent as written. */ envelope: unknown; /** Where the World's endpoints are kept: one kind, or several (GitHub's repository and organization hooks). */ endpoints: EndpointsDecl | ReadonlyArray; /** The bookkeeping type each delivery is kept as (`_webhook_message`), otherwise `_world_event_delivery`. * Queuing and each transport attempt's outcome are separate retained facts. */ record?: string; /** The vendor's retry schedule (`RetryDecl`), durations as a scenario's are (`90s`, `5m`, `12h`, `3d`). Absent, a * delivery is attempted once. Each attempt is signed when it is sent; one that is not answered 2xx, or not answered, * is failed. A failed or finished delivery can be sent again by hand under its own id (`redeliverEvent`: GitHub's * redelivery). */ retry?: RetryDecl; }; /** A retry declaration's delays in milliseconds, and whether they are the vendor's published ones. */ export declare function retrySchedule(retry: RetryDecl | undefined): { delays: number[]; undocumented: boolean; }; /** An event's further envelope values (`$`-named) the pack computes for a write, from the vendor's own state (the * connected account it is about, the API version it is rendered in): the pack's `semantics/events.ts` `values`. A * value left undefined drops its key from the envelope; `$send: false` keeps the event (stored) but sends it to no * endpoint (an account the vendor no longer reports to). */ export type EventValues = (ctx: C, write: EventWrite, type: string) => Record; /** The write an event reports, as the kernel's write hook sees it. */ export type EventWrite = { operation: string; storedType: string; body: Record; root?: string; occurredAt: string; request?: Request; /** The attempt the event is rendered for, when the vendor renders it at each attempt (`renderAt: 'attempt'`). */ attempt?: number; }; /** How the vendor renders an event's `data` for a write, when it is not the written object as its operation answered * it: the pack's `semantics/events.ts`, under the handler contract's read-only context, for each type the write sends * (a Slack message renders as `message` and as `app_mention`). */ export type EventRender = (ctx: C, write: EventWrite, type: string) => Record; /** A packet as one attempt sends it, signed at that attempt. Its transport outcome and acceptance are separate retained * facts. */ export type EventDelivery = { url: string; body: string; headers: Record; }; /** The transport a delivery is made over; the World's by default, a test's own when given. */ export type EventTransport = (url: string, body: string, headers: Record) => Promise; /** What an attempt is signed with beyond the secret: the request's method (POST unless named), the instant in * milliseconds (the receiver's clock; `seconds` when absent) and the endpoint (joined) whose fields hold an asymmetric * scheme's private key, claims and certificate URL. */ export type SignContext = { method?: string; ms?: number; endpoint?: Record; }; /** Every scheme's headers for one delivery, merged. */ export declare function signEventAll(schemes: EventScheme | ReadonlyArray, secret: string, body: string, seconds: number, url: string, messageId?: string, ctx?: SignContext): Record; /** Every scheme applied to one attempt in its declared order: the headers merged, and the body as it is sent (a scheme * that places its signature in the body changes it, and a later scheme signs the changed body). */ export declare function signPacket(schemes: EventScheme | ReadonlyArray, secret: string, body: string, seconds: number, url: string, messageId?: string, ctx?: SignContext): { headers: Record; body: string; }; /** A delivery's signature checked as its receiver checks it (a Standard Webhooks verifier, a timestamp-v1 verifier, a plain * HMAC header, an Ed25519, OIDC or RSA verifier): true when one of its signatures is the one the secret makes (constant * time), or, for an asymmetric scheme, verifies under `secret` read as the public key or certificate (PEM), and its * timestamp, when the scheme sends one, is within `tolerance` seconds of `now`. `method` and `url` are the request's, * for a scheme that signs them. */ export declare function verifyEvent(scheme: EventScheme, secret: string, body: string, headers: Record, opts?: { tolerance?: number; now?: number; method?: string; url?: string; }): boolean; /** The envelope template filled for one event, and for one endpoint when it names `$endpoint.` (left out when * there is none: the event as kept). A `...` key spreads its filled value into the object. */ export declare function fillEnvelope(template: unknown, values: Record, endpoint?: Record): unknown; /** A delivery's endpoint (joined) as it is now, when it is still live. */ export declare function liveEndpoint(all: ReadonlyArray>, kind: EndpointsDecl, id: string, needsUrl?: boolean): Record | undefined; /** The secret a delivery's attempt is signed with: its endpoint's as it is now (empty when its kind declares none), when * the endpoint is still live. */ export declare function endpointSecret(all: ReadonlyArray>, kind: EndpointsDecl, id: string, needsUrl?: boolean): string | undefined; /** The event types a write's operation sends: `types`, else the vendor's enumerated types (`known`). */ export declare function eventTypesOf(decl: Pick, operation: string): string[]; /** The event type a write's operation sends, if any: its first under `types`, else the vendor's enumerated types * (`known`). */ export declare function eventTypeOf(decl: Pick, operation: string): string | undefined; /** Whether an endpoint's list of event types takes a type: every type when the list is empty, `*` or `.*` * its patterns, else the type itself. */ export declare function takesEvent(list: unknown, type: string): boolean; /** Await already-scheduled first dispatches and consume their retained persistence failures. */ export declare function settleEventDeliveries(root: string): Promise; /** Who watches signed packets before dispatch (a walk reading each as its receiver would). This is not evidence of * transport or acceptance. Set and restored by the walk, as its World store is; unset, nothing watches. */ export type DeliveryObserver = (delivery: EventDelivery & { service: string; }) => void; export declare function setDeliveryObserver(observer: DeliveryObserver | undefined): DeliveryObserver | undefined; /** What the application's server answered a message the vendor waits on (its status, its headers by lower-cased name, * its body), or why it did not answer: `missed` when the World let no request out to it or it could not be reached * (`unreachable`), or it did not answer in time (`timeout`). */ export type ApplicationAnswer = { status: number; headers: Record; body: string; missed?: 'timeout' | 'unreachable'; }; /** Who answers a vendor's requests to the application before the World's application route does, given to a * pack's fetch (`PackFetchOptions.application`) and belonging to that fetch alone: an answer it gives is the * application's; `'unreachable'` is a question nobody answers (a walk's life, a sealed World, takes no other); undefined * is a question it does not take, which goes on as any other does: the application route, the World's egress rule, the * network. A walk's stand-in answers every question (its life's answers, else unreachable); a hosted World's router * answers only what it can answer truthfully (WorldDoors.askInWorld) and leaves the rest. `bodyError` models a * disconnect after the response headers; a caller needing the complete body receives no complete answer, while an * event dispatch acknowledges the response status without reading its body. */ export type ApplicationStandIn = (url: string, init: RequestInit) => Promise<{ status: number; headers?: Record; body: string; bodyError?: string; } | 'unreachable' | undefined>; /** A message the vendor sends the application's server and decides by its answer (Stripe's real-time authorization * request, answered within its window): over the same transport, waited on for at most `within` milliseconds, the * fetch's stand-in first when it was given one (its window the same). */ export declare function askApplication(url: string, init: RequestInit, within: number, standIn?: ApplicationStandIn): Promise; /** Every event a write sends, rendered, stored when the vendor keeps its events, signed, delivered to each endpoint * that takes it, and recorded. */ export declare function deliverEvents(service: string, decl: EventsDecl, write: EventWrite, data: (type: string) => Promise>, transport?: EventTransport, values?: (type: string) => Promise>, application?: ApplicationStandIn, vendor?: string): Promise; /** Who hears that a twin held a packet for a later attempt (its pack's fetch, which sets the timer a running clock * needs). */ export declare function onHeldDelivery(service: string, root: string | undefined, listener: () => void): void; /** How a pack renders an event's `data` again at an attempt (`renderAt: 'attempt'`): its render, called with the write * as it was kept and the attempt's number. */ export type AttemptRender = (write: EventWrite, type: string) => Promise>; /** Who renders a twin's events again at each attempt (its pack's fetch, over the pack's render). */ export declare function onAttemptRender(service: string, root: string | undefined, render: AttemptRender): void; /** The render a twin's attempts call, if its pack's fetch gave one; without it an attempt fills the envelope again with * the write's own `data`. */ export declare const attemptRenderer: (deliveryRoot: string) => AttemptRender | undefined; /** Send a delivery again by hand under its own delivery and message ids (GitHub's redelivery, which a pack's door * calls): one that is not queued, held, being sent or gathered into a batch is queued for one more attempt, signed * then and made after this answers, and not retried after. Answers whether it was queued. */ export declare function redeliverEvent(service: string, root: string | undefined, deliveryId: string, record?: string, transport?: EventTransport): Promise; /** Make every held attempt the World's clock has reached, in the order it fell due, and wait until each is made; a * failed one held again for an attempt the clock has also reached is made in turn. Answer the instant the next held * attempt falls due, if any (ADR 0036 §4: the mover of the clock waits on it). */ export declare function releaseHeldDeliveries(service: string, root: string | undefined, record?: string, transport?: EventTransport): Promise;