{"version":3,"file":"utils.mjs","sources":["../../../../src/room/rpc/utils.ts"],"sourcesContent":["import { RpcError as RpcError_Proto } from '@livekit/protocol';\n\n/** Parameters for initiating an RPC call */\nexport interface PerformRpcParams {\n  /** The `identity` of the destination participant */\n  destinationIdentity: string;\n  /** The method name to call */\n  method: string;\n  /** The method payload */\n  payload: string;\n  /**\n   * Timeout for receiving a response after the initial connection (milliseconds).\n   * If a value less than 8000ms is provided, it will be automatically clamped to 8000ms\n   * to ensure sufficient time for round-trip latency buffering.\n   * Default: 15000ms.\n   */\n  responseTimeout?: number;\n}\n\n/**\n * Data passed to method handler for incoming RPC invocations\n */\nexport interface RpcInvocationData {\n  /**\n   * The unique request ID. Will match at both sides of the call, useful for debugging or logging.\n   */\n  requestId: string;\n\n  /**\n   * The unique participant identity of the caller.\n   */\n  callerIdentity: string;\n\n  /**\n   * The payload of the request. User-definable format, typically JSON.\n   */\n  payload: string;\n\n  /**\n   * The maximum time the caller will wait for a response.\n   */\n  responseTimeout: number;\n}\n\n/**\n * Specialized error handling for RPC methods.\n *\n * Instances of this type, when thrown in a method handler, will have their `message`\n * serialized and sent across the wire. The sender will receive an equivalent error on the other side.\n *\n * Built-in types are included but developers may use any string, with a max length of 256 bytes.\n */\n\nexport class RpcError extends Error {\n  static MAX_MESSAGE_BYTES = 256;\n\n  static MAX_DATA_BYTES = 15360; // 15 KB\n\n  code: number;\n\n  data?: string;\n\n  // More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause\n  cause?: unknown;\n\n  /**\n   * Creates an error object with the given code and message, plus an optional data payload.\n   *\n   * If thrown in an RPC method handler, the error will be sent back to the caller.\n   *\n   * Error codes 1001-1999 are reserved for built-in errors (see RpcError.ErrorCode for their meanings).\n   */\n  constructor(code: number, message: string, data?: string, options?: { cause?: unknown }) {\n    super(message);\n    this.code = code;\n    this.message = truncateBytes(message, RpcError.MAX_MESSAGE_BYTES);\n    this.data = data ? truncateBytes(data, RpcError.MAX_DATA_BYTES) : undefined;\n\n    if (typeof options?.cause !== 'undefined') {\n      this.cause = options?.cause;\n    }\n  }\n\n  /**\n   * @internal\n   */\n  static fromProto(proto: RpcError_Proto) {\n    return new RpcError(proto.code, proto.message, proto.data);\n  }\n\n  /**\n   * @internal\n   */\n  toProto() {\n    return new RpcError_Proto({\n      code: this.code as number,\n      message: this.message,\n      data: this.data,\n    });\n  }\n\n  static ErrorCode = {\n    APPLICATION_ERROR: 1500,\n    CONNECTION_TIMEOUT: 1501,\n    RESPONSE_TIMEOUT: 1502,\n    RECIPIENT_DISCONNECTED: 1503,\n    RESPONSE_PAYLOAD_TOO_LARGE: 1504,\n    SEND_FAILED: 1505,\n    CONNECTION_CLOSED: 1506,\n\n    UNSUPPORTED_METHOD: 1400,\n    RECIPIENT_NOT_FOUND: 1401,\n    REQUEST_PAYLOAD_TOO_LARGE: 1402,\n    UNSUPPORTED_SERVER: 1403,\n    UNSUPPORTED_VERSION: 1404,\n  } as const;\n\n  /**\n   * @internal\n   */\n  static ErrorMessage: Record<keyof typeof RpcError.ErrorCode, string> = {\n    APPLICATION_ERROR: 'Application error in method handler',\n    CONNECTION_TIMEOUT: 'Connection timeout',\n    RESPONSE_TIMEOUT: 'Response timeout',\n    RECIPIENT_DISCONNECTED: 'Recipient disconnected',\n    RESPONSE_PAYLOAD_TOO_LARGE: 'Response payload too large',\n    SEND_FAILED: 'Failed to send',\n    CONNECTION_CLOSED: 'Connection closed',\n\n    UNSUPPORTED_METHOD: 'Method not supported at destination',\n    RECIPIENT_NOT_FOUND: 'Recipient not found',\n    REQUEST_PAYLOAD_TOO_LARGE: 'Request payload too large',\n    UNSUPPORTED_SERVER: 'RPC not supported by server',\n    UNSUPPORTED_VERSION: 'Unsupported RPC version',\n  } as const;\n\n  /**\n   * Creates an error object from the code, with an auto-populated message.\n   *\n   * @internal\n   */\n  static builtIn(\n    key: keyof typeof RpcError.ErrorCode,\n    data?: string,\n    options?: { cause?: unknown },\n  ): RpcError {\n    return new RpcError(RpcError.ErrorCode[key], RpcError.ErrorMessage[key], data, options);\n  }\n}\n\n/*\n * Maximum payload size for RPC requests and responses for clients with a clientProtocol of less\n * than CLIENT_PROTOCOL_DATA_STREAM_RPC.\n *\n * If a payload exceeds this size and the remote client does not support compression,\n * the RPC call will fail with a REQUEST_PAYLOAD_TOO_LARGE(1402) or RESPONSE_PAYLOAD_TOO_LARGE(1504) error.\n */\nexport const MAX_V1_PAYLOAD_BYTES = 15360; // 15 KB\n\n/**\n * Topic used for v2 RPC request data streams.\n * @internal\n */\nexport const RPC_REQUEST_DATA_STREAM_TOPIC = 'lk.rpc_request';\n\n/**\n * Topic used for v2 RPC response data streams.\n * @internal\n */\nexport const RPC_RESPONSE_DATA_STREAM_TOPIC = 'lk.rpc_response';\n\n/** @internal */\nexport enum RpcRequestAttrs {\n  RPC_REQUEST_ID = 'lk.rpc_request_id',\n  RPC_REQUEST_METHOD = 'lk.rpc_request_method',\n  RPC_REQUEST_RESPONSE_TIMEOUT_MS = 'lk.rpc_request_response_timeout_ms',\n  RPC_REQUEST_VERSION = 'lk.rpc_request_version',\n}\n\n/** Initial version of rpc which uses RpcRequest / RpcResponse messages.\n * @internal\n **/\nexport const RPC_VERSION_V1 = 1;\n\n/** Rpc version backed by data streams instead of RpcRequest / RpcResponse.\n * @internal\n **/\nexport const RPC_VERSION_V2 = 2;\n\n/**\n * @internal\n */\nexport function byteLength(str: string): number {\n  const encoder = new TextEncoder();\n  return encoder.encode(str).length;\n}\n\n/**\n * @internal\n */\nexport function truncateBytes(str: string, maxBytes: number): string {\n  if (byteLength(str) <= maxBytes) {\n    return str;\n  }\n\n  let low = 0;\n  let high = str.length;\n  const encoder = new TextEncoder();\n\n  while (low < high) {\n    const mid = Math.floor((low + high + 1) / 2);\n    if (encoder.encode(str.slice(0, mid)).length <= maxBytes) {\n      low = mid;\n    } else {\n      high = mid - 1;\n    }\n  }\n\n  // Never end on the first half of a surrogate pair: it would be encoded as a replacement\n  // character, and the truncated string would no longer round trip.\n  const lastCode = low > 0 ? str.charCodeAt(low - 1) : 0;\n  if (lastCode >= 0xd800 && lastCode <= 0xdbff) {\n    low -= 1;\n  }\n\n  return str.slice(0, low);\n}\n"],"names":["RpcError","Error","constructor","code","message","data","options","truncateBytes","MAX_MESSAGE_BYTES","MAX_DATA_BYTES","undefined","cause","fromProto","proto","toProto","RpcError_Proto","builtIn","key","ErrorCode","ErrorMessage","APPLICATION_ERROR","CONNECTION_TIMEOUT","RESPONSE_TIMEOUT","RECIPIENT_DISCONNECTED","RESPONSE_PAYLOAD_TOO_LARGE","SEND_FAILED","CONNECTION_CLOSED","UNSUPPORTED_METHOD","RECIPIENT_NOT_FOUND","REQUEST_PAYLOAD_TOO_LARGE","UNSUPPORTED_SERVER","UNSUPPORTED_VERSION","MAX_V1_PAYLOAD_BYTES","RPC_REQUEST_DATA_STREAM_TOPIC","RPC_RESPONSE_DATA_STREAM_TOPIC","RpcRequestAttrs","RPC_VERSION_V1","RPC_VERSION_V2","byteLength","str","encoder","TextEncoder","encode","length","maxBytes","low","high","mid","Math","floor","slice","lastCode","charCodeAt"],"mappings":"uDA4CA;;;;;;;AAOG;AAEG,MAAOA,QAAS,SAAQC,KAAK,CAAA;AAYjC;;;;;;AAMG;EACHC,WAAAA,CAAYC,IAAY,EAAEC,OAAe,EAAEC,IAAa,EAAEC,OAA6B,EAAA;IACrF,KAAK,CAACF,OAAO,CAAC;IACd,IAAI,CAACD,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,OAAO,GAAGG,aAAa,CAACH,OAAO,EAAEJ,QAAQ,CAACQ,iBAAiB,CAAC;AACjE,IAAA,IAAI,CAACH,IAAI,GAAGA,IAAI,GAAGE,aAAa,CAACF,IAAI,EAAEL,QAAQ,CAACS,cAAc,CAAC,GAAGC,SAAS;AAE3E,IAAA,IAAI,QAAOJ,OAAO,KAAA,IAAA,IAAPA,OAAO,KAAA,MAAA,GAAA,MAAA,GAAPA,OAAO,CAAEK,KAAK,CAAA,KAAK,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