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AAA,IACR;AAGA,IAAA,IAAI,QAAA,EAAU,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,MAAM,CAAA;AAGxC,IAAA,QAAA,EAAU,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAO,CAAA,EAAA,EAAK,aAAa,CAAA,CAAA;AAE5C,IAAA;AACP,EAAA;AAEqC,EAAA;AACvC;AAE6E;AAK3E,EAAA;AAEQ,IAAA;AACT,EAAA;AAGyB,EAAA;AACP,EAAA;AACH,EAAA;AACW,EAAA;AAEiB,EAAA;AAClB,IAAA;AAEV,IAAA;AAEK,MAAA;AAEK,QAAA;AAChB,MAAA;AAES,QAAA;AAChB,MAAA;AACW,MAAA;AACc,IAAA;AAEd,MAAA;AACuB,IAAA;AAET,MAAA;AACX,QAAA;AACS,QAAA;AAChB,MAAA;AACgB,QAAA;AACvB,MAAA;AACc,MAAA;AACR,IAAA;AAES,MAAA;AAChB,IAAA;AACD,EAAA;AAGc,EAAA;AAEgB,IAAA;AACE,EAAA;AAClB,IAAA;AACqC,EAAA;AAC5B,IAAA;AACvB,EAAA;AAEO,EAAA;AACR;AAO4B;AACC;AAGyB;AAErC,EAAA;AACG,IAAA;AACT,MAAA;AACR,IAAA;AACiD,IAAA;AAEzB,EAAA;AAC3B;AAiD0E;AACnD,EAAA;AACd,IAAA;AACR,EAAA;AACyB,EAAA;AAC1B;AAY6D;AACP,EAAA;AAC9B,EAAA;AACS,EAAA;AACzB,EAAA;AACR;AAEyE;AACjC,EAAA;AACxC;AAE6D;AACZ,EAAA;AACjD;AAMoD;AACL,EAAA;AAC/C;ADpHwD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/rivet/rivet/rivetkit-typescript/packages/rivetkit/dist/tsup/chunk-RTWH3AX3.cjs","sourcesContent":[null,"import type { ActorKey } from \"@/mod\";\n\nexport const EMPTY_KEY = \"/\";\nexport const KEY_SEPARATOR = \"/\";\nexport const KEYS = {\n\tPERSIST_DATA: Uint8Array.from([1]),\n\tCONN_PREFIX: Uint8Array.from([2]),\n\tINSPECTOR_TOKEN: Uint8Array.from([3]),\n\tKV: Uint8Array.from([4]),\n\tQUEUE_PREFIX: Uint8Array.from([5]),\n\tLAST_PUSHED_ALARM: Uint8Array.from([6]),\n\tWORKFLOW_PREFIX: Uint8Array.from([6]),\n\tTRACES_PREFIX: Uint8Array.from([7]),\n};\n\nexport const STORAGE_VERSION = {\n\tQUEUE: 1,\n\tWORKFLOW: 1,\n\tTRACES: 1,\n} as const;\n\nconst STORAGE_VERSION_BYTES = {\n\tQUEUE: Uint8Array.from([STORAGE_VERSION.QUEUE]),\n\tWORKFLOW: Uint8Array.from([STORAGE_VERSION.WORKFLOW]),\n\tTRACES: Uint8Array.from([STORAGE_VERSION.TRACES]),\n} as const;\n\nconst QUEUE_NAMESPACE = {\n\tMETADATA: Uint8Array.from([1]),\n\tMESSAGES: Uint8Array.from([2]),\n} as const;\n\nconst QUEUE_ID_BYTES = 8;\n\nfunction concatPrefix(prefix: Uint8Array, suffix: Uint8Array): Uint8Array {\n\tconst merged = new Uint8Array(prefix.length + suffix.length);\n\tmerged.set(prefix, 0);\n\tmerged.set(suffix, prefix.length);\n\treturn merged;\n}\n\nconst QUEUE_STORAGE_PREFIX = concatPrefix(\n\tKEYS.QUEUE_PREFIX,\n\tSTORAGE_VERSION_BYTES.QUEUE,\n);\nconst QUEUE_METADATA_KEY = concatPrefix(\n\tQUEUE_STORAGE_PREFIX,\n\tQUEUE_NAMESPACE.METADATA,\n);\nconst QUEUE_MESSAGES_PREFIX = concatPrefix(\n\tQUEUE_STORAGE_PREFIX,\n\tQUEUE_NAMESPACE.MESSAGES,\n);\nconst WORKFLOW_STORAGE_PREFIX = concatPrefix(\n\tKEYS.WORKFLOW_PREFIX,\n\tSTORAGE_VERSION_BYTES.WORKFLOW,\n);\nconst TRACES_STORAGE_PREFIX = concatPrefix(\n\tKEYS.TRACES_PREFIX,\n\tSTORAGE_VERSION_BYTES.TRACES,\n);\n\n// The slash key form the actor REST API and the actor runtime use. The engine translates it to\n// the canonical form (`encodeActorKey`) at those boundaries.\nexport function serializeActorKey(key: ActorKey): string {\n\t// Use a special marker for empty key arrays\n\tif (key.length === 0) {\n\t\treturn EMPTY_KEY;\n\t}\n\n\t// Escape each key part to handle the separator and the empty key marker\n\tconst escapedParts = key.map((part) => {\n\t\t// Handle empty strings by using a special marker\n\t\tif (part === \"\") {\n\t\t\treturn \"\\\\0\"; // Use \\0 as a marker for empty strings\n\t\t}\n\n\t\t// Escape backslashes first to avoid conflicts with our markers\n\t\tlet escaped = part.replace(/\\\\/g, \"\\\\\\\\\");\n\n\t\t// Then escape separators\n\t\tescaped = escaped.replace(/\\//g, `\\\\${KEY_SEPARATOR}`);\n\n\t\treturn escaped;\n\t});\n\n\treturn escapedParts.join(KEY_SEPARATOR);\n}\n\nexport function deserializeActorKey(keyString: string | undefined): ActorKey {\n\t// Check for special empty key marker\n\tif (\n\t\tkeyString === undefined ||\n\t\tkeyString === null ||\n\t\tkeyString === EMPTY_KEY\n\t) {\n\t\treturn [];\n\t}\n\n\t// Split by unescaped separators and unescape the escaped characters\n\tconst parts: string[] = [];\n\tlet currentPart = \"\";\n\tlet escaping = false;\n\tlet isEmptyStringMarker = false;\n\n\tfor (let i = 0; i < keyString.length; i++) {\n\t\tconst char = keyString[i];\n\n\t\tif (escaping) {\n\t\t\t// Handle special escape sequences\n\t\t\tif (char === \"0\") {\n\t\t\t\t// \\0 represents an empty string marker\n\t\t\t\tisEmptyStringMarker = true;\n\t\t\t} else {\n\t\t\t\t// This is an escaped character, add it directly\n\t\t\t\tcurrentPart += char;\n\t\t\t}\n\t\t\tescaping = false;\n\t\t} else if (char === \"\\\\\") {\n\t\t\t// Start of an escape sequence\n\t\t\tescaping = true;\n\t\t} else if (char === KEY_SEPARATOR) {\n\t\t\t// This is a separator\n\t\t\tif (isEmptyStringMarker) {\n\t\t\t\tparts.push(\"\");\n\t\t\t\tisEmptyStringMarker = false;\n\t\t\t} else {\n\t\t\t\tparts.push(currentPart);\n\t\t\t}\n\t\t\tcurrentPart = \"\";\n\t\t} else {\n\t\t\t// Regular character\n\t\t\tcurrentPart += char;\n\t\t}\n\t}\n\n\t// Add the last part\n\tif (escaping) {\n\t\t// Incomplete escape at the end - treat as literal backslash\n\t\tparts.push(`${currentPart}\\\\`);\n\t} else if (isEmptyStringMarker) {\n\t\tparts.push(\"\");\n\t} else if (currentPart !== \"\" || parts.length > 0) {\n\t\tparts.push(currentPart);\n\t}\n\n\treturn parts;\n}\n\n// The canonical actor key: parts joined with `,`, with `\\` and `,` escaped by a backslash, an\n// empty part written as `\\0`, and the empty key as the empty string. The engine hashes, stores\n// and compares only canonical keys, and the gateway's `rvt-key` carries one. The Rust twin is\n// `rivetkit_shared_types::actor_key`; both run the vectors in\n// `rivetkit-rust/packages/shared-types/tests/fixtures/actor_key_vectors.json`.\nconst CANONICAL_SEPARATOR = \",\";\nconst CANONICAL_EMPTY_PART = \"\\\\0\";\n\n/** Encodes `key` in the canonical form. */\nexport function encodeActorKey(key: ActorKey): string {\n\treturn key\n\t\t.map((part) => {\n\t\t\tif (part === \"\") {\n\t\t\t\treturn CANONICAL_EMPTY_PART;\n\t\t\t}\n\t\t\treturn part.replace(/\\\\/g, \"\\\\\\\\\").replace(/,/g, \"\\\\,\");\n\t\t})\n\t\t.join(CANONICAL_SEPARATOR);\n}\n\n/**\n * Decodes a canonical key, or an unescaped `rvt-key` value from an earlier client. Only `\\\\` and\n * `\\,` are escapes; any other backslash is literal, and a part written as exactly `\\0` is empty.\n */\nexport function decodeActorKey(key: string): ActorKey {\n\tif (key === \"\") {\n\t\treturn [];\n\t}\n\n\tconst parts: string[] = [];\n\tlet raw = \"\";\n\tlet current = \"\";\n\tconst finishPart = () => {\n\t\tparts.push(raw === CANONICAL_EMPTY_PART ? \"\" : current);\n\t\traw = \"\";\n\t\tcurrent = \"\";\n\t};\n\tfor (let i = 0; i < key.length; i++) {\n\t\tconst char = key[i];\n\t\tif (char === CANONICAL_SEPARATOR) {\n\t\t\tfinishPart();\n\t\t\tcontinue;\n\t\t}\n\n\t\traw += char;\n\t\tif (char !== \"\\\\\") {\n\t\t\tcurrent += char;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst next = key[i + 1];\n\t\tif (next === \"\\\\\" || next === CANONICAL_SEPARATOR) {\n\t\t\traw += next;\n\t\t\tcurrent += next;\n\t\t\ti++;\n\t\t} else {\n\t\t\tcurrent += char;\n\t\t}\n\t}\n\tfinishPart();\n\treturn parts;\n}\n\n/**\n * The gateway's `rvt-key` query value for `key`: the canonical key, or `undefined` when the\n * parameter is omitted, as it is for the empty key.\n */\nexport function actorKeyToGatewayQuery(key: ActorKey): string | undefined {\n\tif (key.length === 0) {\n\t\treturn undefined;\n\t}\n\treturn encodeActorKey(key);\n}\n\n/** Reads the gateway's `rvt-key` query value. A missing value is the empty key. */\nexport function actorKeyFromGatewayQuery(\n\trvtKey: string | null | undefined,\n): ActorKey {\n\tif (rvtKey === undefined || rvtKey === null) {\n\t\treturn [];\n\t}\n\treturn decodeActorKey(rvtKey);\n}\n\nexport function makePrefixedKey(key: Uint8Array): Uint8Array {\n\tconst prefixed = new Uint8Array(KEYS.KV.length + key.length);\n\tprefixed.set(KEYS.KV, 0);\n\tprefixed.set(key, KEYS.KV.length);\n\treturn prefixed;\n}\n\nexport function removePrefixFromKey(prefixedKey: Uint8Array): Uint8Array {\n\treturn prefixedKey.slice(KEYS.KV.length);\n}\n\nexport function makeWorkflowKey(key: Uint8Array): Uint8Array {\n\treturn concatPrefix(WORKFLOW_STORAGE_PREFIX, key);\n}\n\nexport function makeTracesKey(key: Uint8Array): Uint8Array {\n\treturn concatPrefix(TRACES_STORAGE_PREFIX, key);\n}\n\nexport function workflowStoragePrefix(): Uint8Array {\n\treturn Uint8Array.from(WORKFLOW_STORAGE_PREFIX);\n}\n\nexport function tracesStoragePrefix(): Uint8Array {\n\treturn Uint8Array.from(TRACES_STORAGE_PREFIX);\n}\n\nexport function queueStoragePrefix(): Uint8Array {\n\treturn Uint8Array.from(QUEUE_STORAGE_PREFIX);\n}\n\nexport function queueMetadataKey(): Uint8Array {\n\treturn Uint8Array.from(QUEUE_METADATA_KEY);\n}\n\nexport function queueMessagesPrefix(): Uint8Array {\n\treturn Uint8Array.from(QUEUE_MESSAGES_PREFIX);\n}\n\nexport function makeConnKey(connId: string): Uint8Array {\n\tconst encoder = new TextEncoder();\n\tconst connIdBytes = encoder.encode(connId);\n\tconst key = new Uint8Array(KEYS.CONN_PREFIX.length + connIdBytes.length);\n\tkey.set(KEYS.CONN_PREFIX, 0);\n\tkey.set(connIdBytes, KEYS.CONN_PREFIX.length);\n\treturn key;\n}\n\nexport function makeQueueMessageKey(id: bigint): Uint8Array {\n\tconst key = new Uint8Array(QUEUE_MESSAGES_PREFIX.length + QUEUE_ID_BYTES);\n\tkey.set(QUEUE_MESSAGES_PREFIX, 0);\n\tconst view = new DataView(key.buffer, key.byteOffset, key.byteLength);\n\tview.setBigUint64(QUEUE_MESSAGES_PREFIX.length, id, false);\n\treturn key;\n}\n\nexport function decodeQueueMessageKey(key: Uint8Array): bigint {\n\tconst offset = QUEUE_MESSAGES_PREFIX.length;\n\tif (key.length < offset + QUEUE_ID_BYTES) {\n\t\tthrow new Error(\"Queue key is too short\");\n\t}\n\tfor (let i = 0; i < QUEUE_MESSAGES_PREFIX.length; i++) {\n\t\tif (key[i] !== QUEUE_MESSAGES_PREFIX[i]) {\n\t\t\tthrow new Error(\"Queue key has invalid prefix\");\n\t\t}\n\t}\n\tconst view = new DataView(\n\t\tkey.buffer,\n\t\tkey.byteOffset + offset,\n\t\tQUEUE_ID_BYTES,\n\t);\n\treturn view.getBigUint64(0, false);\n}\n"]}