{"version":3,"file":"exchangeProtocol-Dnh6UrVE.mjs","names":[],"sources":["../src/ActionRuntime/Transport/SecureSession/exchangeProtocol.ts"],"sourcesContent":["import type { TActionPayload_Any_JsonObject } from \"../../../ActionDefinition/Action/Payload/ActionPayload.types\";\n\n/**\n * The application-level envelope for secure action traffic over an {@link IExchangeCarrier} (HTTP). An\n * exchange carrier only moves one request frame → one reply frame with no unsolicited push, so the\n * handshake and the per-action token + crypto all ride in this envelope (a JSON string body) rather than\n * on a persistent channel. The three security levels share it:\n *\n * - `none` — no handshake, no token: an `act` envelope carries the plaintext wire both ways.\n * - `authenticated` — a one-time handshake yields a session `token`; each later `act` carries it +\n *   the plaintext wire.\n * - `encrypted` — same, but the wire is AES-GCM ciphertext, base64 in the `c` field.\n *\n * The handshake runs as two `hs` exchanges (hello→welcome, prove→accept). The acceptor keeps **no**\n * in-flight state between them: the `welcome` reply carries a sealed handshake-continuation token `hsc`\n * (the acceptor's `pending` state, opaque to the client), which the connector echoes on the `prove`\n * request. The `accept` reply then carries the (sealed) session `t`oken replayed on every later `act`.\n * All server-side continuity rides these sealed tokens, so no request needs to co-locate with another.\n *\n * Since the shared-base-connect plan's Phase 6 the envelope is **owned by `@nice-code/wire`**\n * (`Connect/exchangeEnvelope.ts`, with `w` opaque — the lane's wire) and driven entirely by wire's\n * `establishPlain/SecureExchangeSession` + `WireExchangeAcceptor`. This module remains the lane's\n * *typed view* of the same byte-frozen JSON — the public API for consumers that speak the envelope\n * with action payloads in the `w` slot.\n */\n\ntype TWireJson = TActionPayload_Any_JsonObject<any, any>;\n\n/** Connector → acceptor request envelope. */\nexport type TExchangeRequest =\n  | { k: \"hs\"; m: string; hsc?: string }\n  | { k: \"act\"; t?: string; w: TWireJson }\n  | { k: \"act\"; t?: string; c: string };\n\n/** Acceptor → connector reply envelope. */\nexport type TExchangeReply =\n  | { k: \"hs\"; m: string; hsc?: string; t?: string }\n  | { k: \"act\"; w: TWireJson }\n  | { k: \"act\"; c: string }\n  | { k: \"err\"; message: string };\n\nexport function encodeExchange(envelope: TExchangeRequest | TExchangeReply): string {\n  return JSON.stringify(envelope);\n}\n\nexport function decodeExchangeRequest(raw: string): TExchangeRequest | undefined {\n  return parse<TExchangeRequest>(raw);\n}\n\nexport function decodeExchangeReply(raw: string): TExchangeReply | undefined {\n  return parse<TExchangeReply>(raw);\n}\n\nfunction parse<T>(raw: string): T | undefined {\n  try {\n    return JSON.parse(raw) as T;\n  } catch {\n    return undefined;\n  }\n}\n\n// --- base64 for the encrypted ciphertext bytes (carried in a JSON string field) -----------------\n// Re-exported from `@nice-code/util` so this module's existing consumers keep importing them from here.\n// They are runtime-agnostic (pure JS via `@scure/base`) — no `btoa`/`atob` (absent in Hermes) or\n// `Buffer` (absent in browsers/Workers), so the secure HTTP exchange works in every JS environment.\nexport { base64ToBytes, bytesToBase64 } from \"@nice-code/util\";\n"],"mappings":";;AAyCA,SAAgB,eAAe,UAAqD;CAClF,OAAO,KAAK,UAAU,QAAQ;AAChC;AAEA,SAAgB,sBAAsB,KAA2C;CAC/E,OAAO,MAAwB,GAAG;AACpC;AAEA,SAAgB,oBAAoB,KAAyC;CAC3E,OAAO,MAAsB,GAAG;AAClC;AAEA,SAAS,MAAS,KAA4B;CAC5C,IAAI;EACF,OAAO,KAAK,MAAM,GAAG;CACvB,QAAQ;EACN;CACF;AACF"}