livekit-server-sdk 2.18.0 → 2.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +1 -1
  2. package/dist/AccessToken.cjs +4 -1
  3. package/dist/AccessToken.cjs.map +1 -1
  4. package/dist/AccessToken.d.ts.map +1 -1
  5. package/dist/AccessToken.js +4 -1
  6. package/dist/AccessToken.js.map +1 -1
  7. package/dist/IngressClient.cjs +4 -1
  8. package/dist/IngressClient.cjs.map +1 -1
  9. package/dist/IngressClient.d.cts +10 -0
  10. package/dist/IngressClient.d.ts +10 -0
  11. package/dist/IngressClient.d.ts.map +1 -1
  12. package/dist/IngressClient.js +4 -1
  13. package/dist/IngressClient.js.map +1 -1
  14. package/dist/TwirpRPC.cjs +5 -0
  15. package/dist/TwirpRPC.cjs.map +1 -1
  16. package/dist/TwirpRPC.d.cts +2 -1
  17. package/dist/TwirpRPC.d.ts +2 -1
  18. package/dist/TwirpRPC.d.ts.map +1 -1
  19. package/dist/TwirpRPC.js +4 -0
  20. package/dist/TwirpRPC.js.map +1 -1
  21. package/dist/crypto/digest.cjs +4 -16
  22. package/dist/crypto/digest.cjs.map +1 -1
  23. package/dist/crypto/digest.d.ts.map +1 -1
  24. package/dist/crypto/digest.js +4 -6
  25. package/dist/crypto/digest.js.map +1 -1
  26. package/dist/crypto/uuid.cjs +25 -17
  27. package/dist/crypto/uuid.cjs.map +1 -1
  28. package/dist/crypto/uuid.d.cts +2 -1
  29. package/dist/crypto/uuid.d.ts +2 -1
  30. package/dist/crypto/uuid.d.ts.map +1 -1
  31. package/dist/crypto/uuid.js +23 -6
  32. package/dist/crypto/uuid.js.map +1 -1
  33. package/dist/version.cjs +1 -1
  34. package/dist/version.cjs.map +1 -1
  35. package/dist/version.d.cts +1 -1
  36. package/dist/version.d.ts +1 -1
  37. package/dist/version.js +1 -1
  38. package/dist/version.js.map +1 -1
  39. package/package.json +3 -3
  40. package/src/AccessToken.test.ts +13 -0
  41. package/src/AccessToken.ts +3 -0
  42. package/src/IngressClient.ts +14 -1
  43. package/src/TwirpRPC.test.ts +76 -4
  44. package/src/TwirpRPC.ts +8 -1
  45. package/src/crypto/digest.ts +5 -7
  46. package/src/crypto/uuid.ts +24 -6
  47. package/src/version.ts +1 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/TwirpRPC.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JsonValue } from '@bufbuild/protobuf';\nimport {\n FAILOVER_BACKOFF_BASE_MS,\n failoverAttempts,\n hostKey,\n pickNext,\n regionOrigins,\n sleep,\n} from './failover.js';\nimport { SDK_VERSION } from './version.js';\n\n// Identifies the SDK and version to the server on every request. Browsers forbid\n// setting User-Agent via fetch and silently drop it; Node honors it.\nconst USER_AGENT = `livekit-server-sdk-node/${SDK_VERSION}`;\n\n// twirp RPC adapter for client implementation\n\ntype Options = {\n /** Prefix for the RPC requests */\n prefix?: string;\n /** Timeout for fetch requests, in seconds. Must be within the valid range for abort signal timeouts. */\n requestTimeout?: number;\n /** Whether region failover is enabled (LiveKit Cloud hosts only). Defaults to true. */\n failover?: boolean;\n /** @internal test-only: force failover regardless of host. */\n failoverForce?: boolean;\n /** @internal test-only: base retry backoff in ms. */\n failoverBackoffMs?: number;\n};\n\nconst defaultPrefix = '/twirp';\nconst defaultTimeoutSeconds = 10;\n\nexport const livekitPackage = 'livekit';\nexport interface Rpc {\n request(\n service: string,\n method: string,\n data: JsonValue,\n headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any\n timeout?: number,\n ): Promise<string>;\n}\n\nexport class ServerError extends Error {\n status: number;\n code?: string;\n metadata?: Record<string, string>;\n\n constructor(\n name: string,\n message: string,\n status: number,\n code?: string,\n metadata?: Record<string, string>,\n ) {\n super(message);\n this.name = name;\n this.status = status;\n this.code = code;\n this.metadata = metadata;\n }\n}\n\n/** @deprecated use {@link ServerError} */\nexport const TwirpError = ServerError;\n/** @deprecated use {@link ServerError} */\nexport type TwirpError = ServerError;\n\n/**\n * A {@link ServerError} from a SIP dialing call (`createSipParticipant` /\n * `transferSipParticipant`) that failed with a SIP response status. The SIP code\n * and reason are exposed as getters; any other error metadata remains available\n * via {@link ServerError.metadata}.\n */\nexport class SipCallError extends ServerError {\n constructor(\n name: string,\n message: string,\n status: number,\n code?: string,\n metadata?: Record<string, string>,\n ) {\n super(name, SipCallError.describe(message, code, metadata), status, code, metadata);\n this.name = 'SipCallError';\n }\n\n /** The SIP response code of the failed call, e.g. 486 (Busy Here). */\n get sipStatusCode(): number | undefined {\n const raw = this.metadata?.sip_status_code;\n return raw !== undefined ? Number(raw) : undefined;\n }\n\n /** The SIP reason phrase of the failed call, e.g. \"Busy Here\". */\n get sipStatus(): string | undefined {\n return this.metadata?.sip_status;\n }\n\n /** Builds a SipCallError from a ServerError, preserving its code and metadata. */\n static fromServerError(err: ServerError): SipCallError {\n return new SipCallError(err.name, err.message, err.status, err.code, err.metadata);\n }\n\n // describe renders a clear message: the SIP status, the error code, and any\n // other metadata the server attached. Falls back to the raw message when the\n // error carries no SIP status.\n private static describe(fallback: string, code?: string, metadata?: Record<string, string>) {\n const sipCode = metadata?.sip_status_code;\n if (!sipCode) {\n return fallback;\n }\n const reason = metadata?.sip_status;\n let msg = `SIP call failed: ${sipCode}${reason ? ` ${reason}` : ''}`;\n if (code) {\n msg += ` (${code})`;\n }\n const extra = Object.entries(metadata ?? {})\n .filter(([k]) => k !== 'sip_status_code' && k !== 'sip_status' && k !== 'error_details')\n .map(([k, v]) => `${k}=${v}`);\n if (extra.length) {\n msg += ` [${extra.join(', ')}]`;\n }\n return msg;\n }\n}\n\n/**\n * JSON based Twirp V7 RPC\n */\nexport class TwirpRpc {\n host: string;\n\n pkg: string;\n\n prefix: string;\n\n requestTimeout: number;\n\n failover: boolean;\n\n private failoverForce: boolean;\n\n private failoverBackoffMs: number;\n\n constructor(host: string, pkg: string, options?: Options) {\n if (host.startsWith('ws')) {\n host = host.replace('ws', 'http');\n }\n this.host = host;\n this.pkg = pkg;\n this.requestTimeout = options?.requestTimeout ?? defaultTimeoutSeconds;\n this.prefix = options?.prefix || defaultPrefix;\n this.failover = options?.failover ?? true;\n this.failoverForce = options?.failoverForce ?? false;\n this.failoverBackoffMs = options?.failoverBackoffMs ?? FAILOVER_BACKOFF_BASE_MS;\n }\n\n /**\n * Issues a Twirp request, failing over to alternative regions on retryable\n * errors. On any transport error or HTTP 5xx it discovers regions via\n * /settings/regions and replays the request — body and headers intact —\n * against the next untried region, with exponential backoff. A 4xx is\n * returned immediately.\n */\n async request(\n service: string,\n method: string,\n data: any, // eslint-disable-line @typescript-eslint/no-explicit-any\n headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any\n timeout = this.requestTimeout,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ): Promise<any> {\n const path = `${this.prefix}/${this.pkg}.${service}/${method}`;\n const body = JSON.stringify(data);\n const requestHeaders = {\n 'Content-Type': 'application/json;charset=UTF-8',\n 'User-Agent': USER_AGENT,\n ...headers,\n };\n\n const origin = new URL(this.host);\n const maxAttempts = failoverAttempts(\n this.failover,\n origin.hostname,\n this.failoverForce,\n timeout,\n );\n const attempted = new Set([hostKey(origin)]);\n let regions: string[] | undefined;\n let current = this.host;\n\n for (let attempt = 0; attempt < maxAttempts; attempt += 1) {\n const isLast = attempt + 1 >= maxAttempts;\n const init: RequestInit = { method: 'POST', headers: requestHeaders, body };\n if (timeout) {\n init.signal = AbortSignal.timeout(timeout * 1000);\n }\n\n let response: Response | undefined;\n let transportError: unknown;\n try {\n response = await fetch(new URL(path, current), init);\n } catch (e) {\n transportError = e;\n }\n\n if (response?.ok) {\n // Return the raw JSON. Every caller parses it with protobuf-es\n // fromJson(), which per the proto3 JSON spec accepts both the proto\n // field names (snake_case) and their json_name (camelCase), so no key\n // conversion is needed. Converting keys would also corrupt map<string,…>\n // entries (e.g. participant attributes), whose keys are user data.\n return (await response.json()) as Record<string, unknown>;\n }\n\n // Only retryable failures (a transport error or HTTP 5xx) continue;\n // a 4xx is terminal.\n const retryable = transportError !== undefined || (!!response && response.status >= 500);\n let next: string | undefined;\n if (retryable && !isLast) {\n if (!regions) {\n regions = await regionOrigins(origin, headers);\n }\n next = pickNext(regions, attempted);\n }\n\n if (!retryable || next === undefined) {\n if (response) {\n throw await toTwirpError(response);\n }\n throw transportError;\n }\n\n const reason = response ? `status ${response.status}` : transportError;\n console.warn(\n `livekit API request to ${new URL(current).host} failed (${reason}), retrying with fallback url ${next}`,\n );\n await sleep(this.failoverBackoffMs * 2 ** attempt);\n attempted.add(hostKey(new URL(next)));\n current = next;\n }\n\n throw new Error('failover loop exited without returning'); // unreachable\n }\n}\n\n/** Builds a TwirpError from a non-2xx response, mirroring Twirp's JSON error shape. */\nasync function toTwirpError(response: Response): Promise<TwirpError> {\n const isJson = response.headers.get('content-type') === 'application/json';\n let errorMessage = 'Unknown internal error';\n let errorCode: string | undefined = undefined;\n let metadata: Record<string, string> | undefined = undefined;\n try {\n if (isJson) {\n const parsedError = (await response.json()) as Record<string, unknown>;\n if ('msg' in parsedError) {\n errorMessage = <string>parsedError.msg;\n }\n if ('code' in parsedError) {\n errorCode = <string>parsedError.code;\n }\n if ('meta' in parsedError) {\n metadata = <Record<string, string>>parsedError.meta;\n }\n } else {\n errorMessage = await response.text();\n }\n } catch (e) {\n // parsing went wrong, no op and we keep default error message\n console.debug(`Error when trying to parse error message, using defaults`, e);\n }\n return new TwirpError(response.statusText, errorMessage, response.status, errorCode, metadata);\n}\n"],"mappings":"AAIA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;AAI5B,MAAM,aAAa,2BAA2B,WAAW;AAiBzD,MAAM,gBAAgB;AACtB,MAAM,wBAAwB;AAEvB,MAAM,iBAAiB;AAWvB,MAAM,oBAAoB,MAAM;AAAA,EAKrC,Y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,SAAS,GAAG;AAEV,YAAQ,MAAM,4DAA4D,CAAC;AAAA,EAC7E;AACA,SAAO,IAAI,WAAW,SAAS,YAAY,cAAc,SAAS,QAAQ,WAAW,QAAQ;AAC/F;","names":[]}
1
+ {"version":3,"sources":["../src/TwirpRPC.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { JsonValue } from '@bufbuild/protobuf';\nimport { randomUUID } from './crypto/uuid.js';\nimport {\n FAILOVER_BACKOFF_BASE_MS,\n failoverAttempts,\n hostKey,\n pickNext,\n regionOrigins,\n sleep,\n} from './failover.js';\nimport { SDK_VERSION } from './version.js';\n\n// Identifies the SDK and version to the server on every request. Browsers forbid\n// setting User-Agent via fetch and silently drop it; Node honors it.\nconst USER_AGENT = `livekit-server-sdk-node/${SDK_VERSION}`;\n\n// Carries a per-request idempotency key. The SDK's auto-retries (see failover)\n// keep the same key across attempts, so the server can identify and deduplicate\n// repeated requests.\nexport const REQUEST_ID_HEADER = 'X-Livekit-Request-Id';\n\n// twirp RPC adapter for client implementation\n\ntype Options = {\n /** Prefix for the RPC requests */\n prefix?: string;\n /** Timeout for fetch requests, in seconds. Must be within the valid range for abort signal timeouts. */\n requestTimeout?: number;\n /** Whether region failover is enabled (LiveKit Cloud hosts only). Defaults to true. */\n failover?: boolean;\n /** @internal test-only: force failover regardless of host. */\n failoverForce?: boolean;\n /** @internal test-only: base retry backoff in ms. */\n failoverBackoffMs?: number;\n};\n\nconst defaultPrefix = '/twirp';\nconst defaultTimeoutSeconds = 10;\n\nexport const livekitPackage = 'livekit';\nexport interface Rpc {\n request(\n service: string,\n method: string,\n data: JsonValue,\n headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any\n timeout?: number,\n ): Promise<string>;\n}\n\nexport class ServerError extends Error {\n status: number;\n code?: string;\n metadata?: Record<string, string>;\n\n constructor(\n name: string,\n message: string,\n status: number,\n code?: string,\n metadata?: Record<string, string>,\n ) {\n super(message);\n this.name = name;\n this.status = status;\n this.code = code;\n this.metadata = metadata;\n }\n}\n\n/** @deprecated use {@link ServerError} */\nexport const TwirpError = ServerError;\n/** @deprecated use {@link ServerError} */\nexport type TwirpError = ServerError;\n\n/**\n * A {@link ServerError} from a SIP dialing call (`createSipParticipant` /\n * `transferSipParticipant`) that failed with a SIP response status. The SIP code\n * and reason are exposed as getters; any other error metadata remains available\n * via {@link ServerError.metadata}.\n */\nexport class SipCallError extends ServerError {\n constructor(\n name: string,\n message: string,\n status: number,\n code?: string,\n metadata?: Record<string, string>,\n ) {\n super(name, SipCallError.describe(message, code, metadata), status, code, metadata);\n this.name = 'SipCallError';\n }\n\n /** The SIP response code of the failed call, e.g. 486 (Busy Here). */\n get sipStatusCode(): number | undefined {\n const raw = this.metadata?.sip_status_code;\n return raw !== undefined ? Number(raw) : undefined;\n }\n\n /** The SIP reason phrase of the failed call, e.g. \"Busy Here\". */\n get sipStatus(): string | undefined {\n return this.metadata?.sip_status;\n }\n\n /** Builds a SipCallError from a ServerError, preserving its code and metadata. */\n static fromServerError(err: ServerError): SipCallError {\n return new SipCallError(err.name, err.message, err.status, err.code, err.metadata);\n }\n\n // describe renders a clear message: the SIP status, the error code, and any\n // other metadata the server attached. Falls back to the raw message when the\n // error carries no SIP status.\n private static describe(fallback: string, code?: string, metadata?: Record<string, string>) {\n const sipCode = metadata?.sip_status_code;\n if (!sipCode) {\n return fallback;\n }\n const reason = metadata?.sip_status;\n let msg = `SIP call failed: ${sipCode}${reason ? ` ${reason}` : ''}`;\n if (code) {\n msg += ` (${code})`;\n }\n const extra = Object.entries(metadata ?? {})\n .filter(([k]) => k !== 'sip_status_code' && k !== 'sip_status' && k !== 'error_details')\n .map(([k, v]) => `${k}=${v}`);\n if (extra.length) {\n msg += ` [${extra.join(', ')}]`;\n }\n return msg;\n }\n}\n\n/**\n * JSON based Twirp V7 RPC\n */\nexport class TwirpRpc {\n host: string;\n\n pkg: string;\n\n prefix: string;\n\n requestTimeout: number;\n\n failover: boolean;\n\n private failoverForce: boolean;\n\n private failoverBackoffMs: number;\n\n constructor(host: string, pkg: string, options?: Options) {\n if (host.startsWith('ws')) {\n host = host.replace('ws', 'http');\n }\n this.host = host;\n this.pkg = pkg;\n this.requestTimeout = options?.requestTimeout ?? defaultTimeoutSeconds;\n this.prefix = options?.prefix || defaultPrefix;\n this.failover = options?.failover ?? true;\n this.failoverForce = options?.failoverForce ?? false;\n this.failoverBackoffMs = options?.failoverBackoffMs ?? FAILOVER_BACKOFF_BASE_MS;\n }\n\n /**\n * Issues a Twirp request, failing over to alternative regions on retryable\n * errors. On any transport error or HTTP 5xx it discovers regions via\n * /settings/regions and replays the request — body and headers intact —\n * against the next untried region, with exponential backoff. A 4xx is\n * returned immediately.\n */\n async request(\n service: string,\n method: string,\n data: any, // eslint-disable-line @typescript-eslint/no-explicit-any\n headers: any, // eslint-disable-line @typescript-eslint/no-explicit-any\n timeout = this.requestTimeout,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ): Promise<any> {\n const path = `${this.prefix}/${this.pkg}.${service}/${method}`;\n const body = JSON.stringify(data);\n const requestHeaders: Record<string, string> = {\n 'Content-Type': 'application/json;charset=UTF-8',\n 'User-Agent': USER_AGENT,\n ...headers,\n };\n requestHeaders[REQUEST_ID_HEADER] = await randomUUID();\n\n const origin = new URL(this.host);\n const maxAttempts = failoverAttempts(\n this.failover,\n origin.hostname,\n this.failoverForce,\n timeout,\n );\n const attempted = new Set([hostKey(origin)]);\n let regions: string[] | undefined;\n let current = this.host;\n\n for (let attempt = 0; attempt < maxAttempts; attempt += 1) {\n const isLast = attempt + 1 >= maxAttempts;\n const init: RequestInit = { method: 'POST', headers: requestHeaders, body };\n if (timeout) {\n init.signal = AbortSignal.timeout(timeout * 1000);\n }\n\n let response: Response | undefined;\n let transportError: unknown;\n try {\n response = await fetch(new URL(path, current), init);\n } catch (e) {\n transportError = e;\n }\n\n if (response?.ok) {\n // Return the raw JSON. Every caller parses it with protobuf-es\n // fromJson(), which per the proto3 JSON spec accepts both the proto\n // field names (snake_case) and their json_name (camelCase), so no key\n // conversion is needed. Converting keys would also corrupt map<string,…>\n // entries (e.g. participant attributes), whose keys are user data.\n return (await response.json()) as Record<string, unknown>;\n }\n\n // Only retryable failures (a transport error or HTTP 5xx) continue;\n // a 4xx is terminal.\n const retryable = transportError !== undefined || (!!response && response.status >= 500);\n let next: string | undefined;\n if (retryable && !isLast) {\n if (!regions) {\n regions = await regionOrigins(origin, headers);\n }\n next = pickNext(regions, attempted);\n }\n\n if (!retryable || next === undefined) {\n if (response) {\n throw await toTwirpError(response);\n }\n throw transportError;\n }\n\n const reason = response ? `status ${response.status}` : transportError;\n console.warn(\n `livekit API request to ${new URL(current).host} failed (${reason}), retrying with fallback url ${next}`,\n );\n await sleep(this.failoverBackoffMs * 2 ** attempt);\n attempted.add(hostKey(new URL(next)));\n current = next;\n }\n\n throw new Error('failover loop exited without returning'); // unreachable\n }\n}\n\n/** Builds a TwirpError from a non-2xx response, mirroring Twirp's JSON error shape. */\nasync function toTwirpError(response: Response): Promise<TwirpError> {\n const isJson = response.headers.get('content-type') === 'application/json';\n let errorMessage = 'Unknown internal error';\n let errorCode: string | undefined = undefined;\n let metadata: Record<string, string> | undefined = undefined;\n try {\n if (isJson) {\n const parsedError = (await response.json()) as Record<string, unknown>;\n if ('msg' in parsedError) {\n errorMessage = <string>parsedError.msg;\n }\n if ('code' in parsedError) {\n errorCode = <string>parsedError.code;\n }\n if ('meta' in parsedError) {\n metadata = <Record<string, string>>parsedError.meta;\n }\n } else {\n errorMessage = await response.text();\n }\n } catch (e) {\n // parsing went wrong, no op and we keep default error message\n console.debug(`Error when trying to parse error message, using defaults`, e);\n }\n return new TwirpError(response.statusText, errorMessage, response.status, errorCode, metadata);\n}\n"],"mappings":"AAIA,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,m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xB,uBAAuB,YAAY;AAAA,MACrC;AACA,UAAI,UAAU,aAAa;AACzB,oBAAoB,YAAY;AAAA,MAClC;AACA,UAAI,UAAU,aAAa;AACzB,mBAAmC,YAAY;AAAA,MACjD;AAAA,IACF,OAAO;AACL,qBAAe,MAAM,SAAS,KAAK;AAAA,IACrC;AAAA,EACF,SAAS,GAAG;AAEV,YAAQ,MAAM,4DAA4D,CAAC;AAAA,EAC7E;AACA,SAAO,IAAI,WAAW,SAAS,YAAY,cAAc,SAAS,QAAQ,WAAW,QAAQ;AAC/F;","names":[]}
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var digest_exports = {};
30
20
  __export(digest_exports, {
@@ -33,13 +23,11 @@ __export(digest_exports, {
33
23
  module.exports = __toCommonJS(digest_exports);
34
24
  async function digest(data) {
35
25
  var _a;
36
- if ((_a = globalThis.crypto) == null ? void 0 : _a.subtle) {
37
- const encoder = new TextEncoder();
38
- return crypto.subtle.digest("SHA-256", encoder.encode(data));
39
- } else {
40
- const nodeCrypto = await import("node:crypto");
41
- return nodeCrypto.createHash("sha256").update(data).digest();
26
+ if (!((_a = globalThis.crypto) == null ? void 0 : _a.subtle)) {
27
+ throw new Error("Web Crypto API is required (globalThis.crypto.subtle)");
42
28
  }
29
+ const encoder = new TextEncoder();
30
+ return crypto.subtle.digest("SHA-256", encoder.encode(data));
43
31
  }
44
32
  // Annotate the CommonJS export names for ESM import in node:
45
33
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/crypto/digest.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Use the Web Crypto API if available, otherwise fallback to Node.js crypto\nexport async function digest(data: string): Promise<ArrayBuffer> {\n if (globalThis.crypto?.subtle) {\n const encoder = new TextEncoder();\n return crypto.subtle.digest('SHA-256', encoder.encode(data));\n } else {\n const nodeCrypto = await import('node:crypto');\n return nodeCrypto.createHash('sha256').update(data).digest();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,eAAsB,OAAO,MAAoC;AALjE;AAME,OAAI,gBAAW,WAAX,mBAAmB,QAAQ;AAC7B,UAAM,UAAU,IAAI,YAAY;AAChC,WAAO,OAAO,OAAO,OAAO,WAAW,QAAQ,OAAO,IAAI,CAAC;AAAA,EAC7D,OAAO;AACL,UAAM,aAAa,MAAM,OAAO,aAAa;AAC7C,WAAO,WAAW,WAAW,QAAQ,EAAE,OAAO,IAAI,EAAE,OAAO;AAAA,EAC7D;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/crypto/digest.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Web Crypto only — no import('node:crypto') so isolate/edge bundlers can resolve this module\nexport async function digest(data: string): Promise<ArrayBuffer> {\n if (!globalThis.crypto?.subtle) {\n throw new Error('Web Crypto API is required (globalThis.crypto.subtle)');\n }\n const encoder = new TextEncoder();\n return crypto.subtle.digest('SHA-256', encoder.encode(data));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,eAAsB,OAAO,MAAoC;AALjE;AAME,MAAI,GAAC,gBAAW,WAAX,mBAAmB,SAAQ;AAC9B,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AACA,QAAM,UAAU,IAAI,YAAY;AAChC,SAAO,OAAO,OAAO,OAAO,WAAW,QAAQ,OAAO,IAAI,CAAC;AAC7D;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"digest.d.ts","sourceRoot":"","sources":["../../src/crypto/digest.ts"],"names":[],"mappings":"AAKA,wBAAsB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAQ/D"}
1
+ {"version":3,"file":"digest.d.ts","sourceRoot":"","sources":["../../src/crypto/digest.ts"],"names":[],"mappings":"AAKA,wBAAsB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAM/D"}
@@ -1,12 +1,10 @@
1
1
  async function digest(data) {
2
2
  var _a;
3
- if ((_a = globalThis.crypto) == null ? void 0 : _a.subtle) {
4
- const encoder = new TextEncoder();
5
- return crypto.subtle.digest("SHA-256", encoder.encode(data));
6
- } else {
7
- const nodeCrypto = await import("node:crypto");
8
- return nodeCrypto.createHash("sha256").update(data).digest();
3
+ if (!((_a = globalThis.crypto) == null ? void 0 : _a.subtle)) {
4
+ throw new Error("Web Crypto API is required (globalThis.crypto.subtle)");
9
5
  }
6
+ const encoder = new TextEncoder();
7
+ return crypto.subtle.digest("SHA-256", encoder.encode(data));
10
8
  }
11
9
  export {
12
10
  digest
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/crypto/digest.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Use the Web Crypto API if available, otherwise fallback to Node.js crypto\nexport async function digest(data: string): Promise<ArrayBuffer> {\n if (globalThis.crypto?.subtle) {\n const encoder = new TextEncoder();\n return crypto.subtle.digest('SHA-256', encoder.encode(data));\n } else {\n const nodeCrypto = await import('node:crypto');\n return nodeCrypto.createHash('sha256').update(data).digest();\n }\n}\n"],"mappings":"AAKA,eAAsB,OAAO,MAAoC;AALjE;AAME,OAAI,gBAAW,WAAX,mBAAmB,QAAQ;AAC7B,UAAM,UAAU,IAAI,YAAY;AAChC,WAAO,OAAO,OAAO,OAAO,WAAW,QAAQ,OAAO,IAAI,CAAC;AAAA,EAC7D,OAAO;AACL,UAAM,aAAa,MAAM,OAAO,aAAa;AAC7C,WAAO,WAAW,WAAW,QAAQ,EAAE,OAAO,IAAI,EAAE,OAAO;AAAA,EAC7D;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/crypto/digest.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Web Crypto only — no import('node:crypto') so isolate/edge bundlers can resolve this module\nexport async function digest(data: string): Promise<ArrayBuffer> {\n if (!globalThis.crypto?.subtle) {\n throw new Error('Web Crypto API is required (globalThis.crypto.subtle)');\n }\n const encoder = new TextEncoder();\n return crypto.subtle.digest('SHA-256', encoder.encode(data));\n}\n"],"mappings":"AAKA,eAAsB,OAAO,MAAoC;AALjE;AAME,MAAI,GAAC,gBAAW,WAAX,mBAAmB,SAAQ;AAC9B,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AACA,QAAM,UAAU,IAAI,YAAY;AAChC,SAAO,OAAO,OAAO,OAAO,WAAW,QAAQ,OAAO,IAAI,CAAC;AAC7D;","names":[]}
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,30 +15,40 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var uuid_exports = {};
30
20
  __export(uuid_exports, {
31
- getRandomBytes: () => getRandomBytes
21
+ getRandomBytes: () => getRandomBytes,
22
+ randomUUID: () => randomUUID
32
23
  });
33
24
  module.exports = __toCommonJS(uuid_exports);
34
25
  async function getRandomBytes(size = 16) {
35
- if (globalThis.crypto) {
36
- return crypto.getRandomValues(new Uint8Array(size));
37
- } else {
38
- const nodeCrypto = await import("node:crypto");
39
- return nodeCrypto.getRandomValues(new Uint8Array(size));
26
+ var _a;
27
+ if (!((_a = globalThis.crypto) == null ? void 0 : _a.getRandomValues)) {
28
+ throw new Error("Web Crypto API is required (globalThis.crypto.getRandomValues)");
40
29
  }
30
+ return crypto.getRandomValues(new Uint8Array(size));
31
+ }
32
+ async function randomUUID() {
33
+ var _a;
34
+ if (typeof ((_a = globalThis.crypto) == null ? void 0 : _a.randomUUID) === "function") {
35
+ return crypto.randomUUID();
36
+ }
37
+ const bytes = await getRandomBytes(16);
38
+ bytes[6] = bytes[6] & 15 | 64;
39
+ bytes[8] = bytes[8] & 63 | 128;
40
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
41
+ return [
42
+ hex.slice(0, 8),
43
+ hex.slice(8, 12),
44
+ hex.slice(12, 16),
45
+ hex.slice(16, 20),
46
+ hex.slice(20)
47
+ ].join("-");
41
48
  }
42
49
  // Annotate the CommonJS export names for ESM import in node:
43
50
  0 && (module.exports = {
44
- getRandomBytes
51
+ getRandomBytes,
52
+ randomUUID
45
53
  });
46
54
  //# sourceMappingURL=uuid.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/crypto/uuid.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Use the Web Crypto API if available, otherwise fallback to Node.js crypto\nexport async function getRandomBytes(size: number = 16): Promise<Uint8Array> {\n if (globalThis.crypto) {\n return crypto.getRandomValues(new Uint8Array(size));\n } else {\n const nodeCrypto = await import('node:crypto');\n return nodeCrypto.getRandomValues(new Uint8Array(size));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,eAAsB,eAAe,OAAe,IAAyB;AAC3E,MAAI,WAAW,QAAQ;AACrB,WAAO,OAAO,gBAAgB,IAAI,WAAW,IAAI,CAAC;AAAA,EACpD,OAAO;AACL,UAAM,aAAa,MAAM,OAAO,aAAa;AAC7C,WAAO,WAAW,gBAAgB,IAAI,WAAW,IAAI,CAAC;AAAA,EACxD;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/crypto/uuid.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Web Crypto only — no import('node:crypto') so isolate/edge bundlers can resolve this module\nexport async function getRandomBytes(size: number = 16): Promise<Uint8Array> {\n if (!globalThis.crypto?.getRandomValues) {\n throw new Error('Web Crypto API is required (globalThis.crypto.getRandomValues)');\n }\n return crypto.getRandomValues(new Uint8Array(size));\n}\n\n// A random RFC 4122 v4 UUID. Prefers the platform's randomUUID (Node 19+, edge\n// runtimes, browsers in a secure context) and otherwise formats random bytes,\n// so it works everywhere getRandomBytes does.\nexport async function randomUUID(): Promise<string> {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return crypto.randomUUID();\n }\n const bytes = await getRandomBytes(16);\n bytes[6] = (bytes[6]! & 0x0f) | 0x40; // version 4\n bytes[8] = (bytes[8]! & 0x3f) | 0x80; // variant 1\n const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');\n return [\n hex.slice(0, 8),\n hex.slice(8, 12),\n hex.slice(12, 16),\n hex.slice(16, 20),\n hex.slice(20),\n ].join('-');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,eAAsB,eAAe,OAAe,IAAyB;AAL7E;AAME,MAAI,GAAC,gBAAW,WAAX,mBAAmB,kBAAiB;AACvC,UAAM,IAAI,MAAM,gEAAgE;AAAA,EAClF;AACA,SAAO,OAAO,gBAAgB,IAAI,WAAW,IAAI,CAAC;AACpD;AAKA,eAAsB,aAA8B;AAfpD;AAgBE,MAAI,SAAO,gBAAW,WAAX,mBAAmB,gBAAe,YAAY;AACvD,WAAO,OAAO,WAAW;AAAA,EAC3B;AACA,QAAM,QAAQ,MAAM,eAAe,EAAE;AACrC,QAAM,CAAC,IAAK,MAAM,CAAC,IAAK,KAAQ;AAChC,QAAM,CAAC,IAAK,MAAM,CAAC,IAAK,KAAQ;AAChC,QAAM,MAAM,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AAC7E,SAAO;AAAA,IACL,IAAI,MAAM,GAAG,CAAC;AAAA,IACd,IAAI,MAAM,GAAG,EAAE;AAAA,IACf,IAAI,MAAM,IAAI,EAAE;AAAA,IAChB,IAAI,MAAM,IAAI,EAAE;AAAA,IAChB,IAAI,MAAM,EAAE;AAAA,EACd,EAAE,KAAK,GAAG;AACZ;","names":[]}
@@ -1,3 +1,4 @@
1
1
  declare function getRandomBytes(size?: number): Promise<Uint8Array>;
2
+ declare function randomUUID(): Promise<string>;
2
3
 
3
- export { getRandomBytes };
4
+ export { getRandomBytes, randomUUID };
@@ -1,3 +1,4 @@
1
1
  declare function getRandomBytes(size?: number): Promise<Uint8Array>;
2
+ declare function randomUUID(): Promise<string>;
2
3
 
3
- export { getRandomBytes };
4
+ export { getRandomBytes, randomUUID };
@@ -1 +1 @@
1
- {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../src/crypto/uuid.ts"],"names":[],"mappings":"AAKA,wBAAsB,cAAc,CAAC,IAAI,GAAE,MAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAO3E"}
1
+ {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../src/crypto/uuid.ts"],"names":[],"mappings":"AAKA,wBAAsB,cAAc,CAAC,IAAI,GAAE,MAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAK3E;AAKD,wBAAsB,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC,CAelD"}
@@ -1,12 +1,29 @@
1
1
  async function getRandomBytes(size = 16) {
2
- if (globalThis.crypto) {
3
- return crypto.getRandomValues(new Uint8Array(size));
4
- } else {
5
- const nodeCrypto = await import("node:crypto");
6
- return nodeCrypto.getRandomValues(new Uint8Array(size));
2
+ var _a;
3
+ if (!((_a = globalThis.crypto) == null ? void 0 : _a.getRandomValues)) {
4
+ throw new Error("Web Crypto API is required (globalThis.crypto.getRandomValues)");
7
5
  }
6
+ return crypto.getRandomValues(new Uint8Array(size));
7
+ }
8
+ async function randomUUID() {
9
+ var _a;
10
+ if (typeof ((_a = globalThis.crypto) == null ? void 0 : _a.randomUUID) === "function") {
11
+ return crypto.randomUUID();
12
+ }
13
+ const bytes = await getRandomBytes(16);
14
+ bytes[6] = bytes[6] & 15 | 64;
15
+ bytes[8] = bytes[8] & 63 | 128;
16
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
17
+ return [
18
+ hex.slice(0, 8),
19
+ hex.slice(8, 12),
20
+ hex.slice(12, 16),
21
+ hex.slice(16, 20),
22
+ hex.slice(20)
23
+ ].join("-");
8
24
  }
9
25
  export {
10
- getRandomBytes
26
+ getRandomBytes,
27
+ randomUUID
11
28
  };
12
29
  //# sourceMappingURL=uuid.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/crypto/uuid.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Use the Web Crypto API if available, otherwise fallback to Node.js crypto\nexport async function getRandomBytes(size: number = 16): Promise<Uint8Array> {\n if (globalThis.crypto) {\n return crypto.getRandomValues(new Uint8Array(size));\n } else {\n const nodeCrypto = await import('node:crypto');\n return nodeCrypto.getRandomValues(new Uint8Array(size));\n }\n}\n"],"mappings":"AAKA,eAAsB,eAAe,OAAe,IAAyB;AAC3E,MAAI,WAAW,QAAQ;AACrB,WAAO,OAAO,gBAAgB,IAAI,WAAW,IAAI,CAAC;AAAA,EACpD,OAAO;AACL,UAAM,aAAa,MAAM,OAAO,aAAa;AAC7C,WAAO,WAAW,gBAAgB,IAAI,WAAW,IAAI,CAAC;AAAA,EACxD;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/crypto/uuid.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\n\n// Web Crypto only — no import('node:crypto') so isolate/edge bundlers can resolve this module\nexport async function getRandomBytes(size: number = 16): Promise<Uint8Array> {\n if (!globalThis.crypto?.getRandomValues) {\n throw new Error('Web Crypto API is required (globalThis.crypto.getRandomValues)');\n }\n return crypto.getRandomValues(new Uint8Array(size));\n}\n\n// A random RFC 4122 v4 UUID. Prefers the platform's randomUUID (Node 19+, edge\n// runtimes, browsers in a secure context) and otherwise formats random bytes,\n// so it works everywhere getRandomBytes does.\nexport async function randomUUID(): Promise<string> {\n if (typeof globalThis.crypto?.randomUUID === 'function') {\n return crypto.randomUUID();\n }\n const bytes = await getRandomBytes(16);\n bytes[6] = (bytes[6]! & 0x0f) | 0x40; // version 4\n bytes[8] = (bytes[8]! & 0x3f) | 0x80; // variant 1\n const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');\n return [\n hex.slice(0, 8),\n hex.slice(8, 12),\n hex.slice(12, 16),\n hex.slice(16, 20),\n hex.slice(20),\n ].join('-');\n}\n"],"mappings":"AAKA,eAAsB,eAAe,OAAe,IAAyB;AAL7E;AAME,MAAI,GAAC,gBAAW,WAAX,mBAAmB,kBAAiB;AACvC,UAAM,IAAI,MAAM,gEAAgE;AAAA,EAClF;AACA,SAAO,OAAO,gBAAgB,IAAI,WAAW,IAAI,CAAC;AACpD;AAKA,eAAsB,aAA8B;AAfpD;AAgBE,MAAI,SAAO,gBAAW,WAAX,mBAAmB,gBAAe,YAAY;AACvD,WAAO,OAAO,WAAW;AAAA,EAC3B;AACA,QAAM,QAAQ,MAAM,eAAe,EAAE;AACrC,QAAM,CAAC,IAAK,MAAM,CAAC,IAAK,KAAQ;AAChC,QAAM,CAAC,IAAK,MAAM,CAAC,IAAK,KAAQ;AAChC,QAAM,MAAM,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AAC7E,SAAO;AAAA,IACL,IAAI,MAAM,GAAG,CAAC;AAAA,IACd,IAAI,MAAM,GAAG,EAAE;AAAA,IACf,IAAI,MAAM,IAAI,EAAE;AAAA,IAChB,IAAI,MAAM,IAAI,EAAE;AAAA,IAChB,IAAI,MAAM,EAAE;AAAA,EACd,EAAE,KAAK,GAAG;AACZ;","names":[]}
package/dist/version.cjs CHANGED
@@ -21,7 +21,7 @@ __export(version_exports, {
21
21
  SDK_VERSION: () => SDK_VERSION
22
22
  });
23
23
  module.exports = __toCommonJS(version_exports);
24
- const SDK_VERSION = "2.18.0";
24
+ const SDK_VERSION = "2.19.1";
25
25
  // Annotate the CommonJS export names for ESM import in node:
26
26
  0 && (module.exports = {
27
27
  SDK_VERSION
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/version.ts"],"sourcesContent":["export const SDK_VERSION = \"2.18.0\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,MAAM,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/version.ts"],"sourcesContent":["export const SDK_VERSION = \"2.19.1\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,MAAM,cAAc;","names":[]}
@@ -1,3 +1,3 @@
1
- declare const SDK_VERSION = "2.18.0";
1
+ declare const SDK_VERSION = "2.19.1";
2
2
 
3
3
  export { SDK_VERSION };
package/dist/version.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare const SDK_VERSION = "2.18.0";
1
+ declare const SDK_VERSION = "2.19.1";
2
2
 
3
3
  export { SDK_VERSION };
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
- const SDK_VERSION = "2.18.0";
1
+ const SDK_VERSION = "2.19.1";
2
2
  export {
3
3
  SDK_VERSION
4
4
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/version.ts"],"sourcesContent":["export const SDK_VERSION = \"2.18.0\";\n"],"mappings":"AAAO,MAAM,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/version.ts"],"sourcesContent":["export const SDK_VERSION = \"2.19.1\";\n"],"mappings":"AAAO,MAAM,cAAc;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "livekit-server-sdk",
3
- "version": "2.18.0",
3
+ "version": "2.19.1",
4
4
  "description": "Server-side SDK for LiveKit",
5
5
  "main": "dist/index.js",
6
6
  "require": "dist/index.cjs",
@@ -31,7 +31,7 @@
31
31
  ],
32
32
  "dependencies": {
33
33
  "@bufbuild/protobuf": "^1.10.1",
34
- "@livekit/protocol": "1.48.0",
34
+ "@livekit/protocol": "1.51.0",
35
35
  "jose": "^5.1.2"
36
36
  },
37
37
  "devDependencies": {
@@ -48,7 +48,7 @@
48
48
  "vitest": "^4.0.0"
49
49
  },
50
50
  "engines": {
51
- "node": ">=18"
51
+ "node": ">=19"
52
52
  },
53
53
  "scripts": {
54
54
  "prebuild": "node -p \"'export const SDK_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
@@ -64,6 +64,19 @@ describe('identity is required for only join grants', () => {
64
64
  });
65
65
 
66
66
  describe('verify token is valid', () => {
67
+ it('rejects a token that omits exp', async () => {
68
+ const secret = new TextEncoder().encode(testSecret);
69
+ const token = await new jose.SignJWT({ video: { roomCreate: true } })
70
+ .setProtectedHeader({ alg: 'HS256' })
71
+ .setIssuer(testApiKey)
72
+ .setSubject('me')
73
+ .setNotBefore(new Date())
74
+ .sign(secret);
75
+
76
+ const v = new TokenVerifier(testApiKey, testSecret);
77
+ await expect(v.verify(token)).rejects.toThrow();
78
+ });
79
+
67
80
  it('can decode encoded token', async () => {
68
81
  const t = new AccessToken(testApiKey, testSecret);
69
82
  t.sha256 = 'abcdefg';
@@ -231,6 +231,9 @@ export class TokenVerifier {
231
231
  const { payload } = await jose.jwtVerify(token, secret, {
232
232
  issuer: this.apiKey,
233
233
  clockTolerance,
234
+ // First-party minters always set exp. Without this, a hand-rolled token
235
+ // with a valid signature and no exp verifies forever (livekit/protocol#1706).
236
+ requiredClaims: ['exp'],
234
237
  });
235
238
  if (!payload) {
236
239
  throw Error('invalid token');
@@ -52,6 +52,11 @@ export interface CreateIngressOptions {
52
52
  * url of the media to pull for ingresses of type URL
53
53
  */
54
54
  url?: string;
55
+ /**
56
+ * whether the ingress is enabled. When set to false, new connection attempts are rejected
57
+ * while the stream key is preserved. The default is true.
58
+ */
59
+ enabled?: boolean;
55
60
  /**
56
61
  * custom audio encoding parameters. optional
57
62
  */
@@ -93,6 +98,11 @@ export interface UpdateIngressOptions {
93
98
  * Transcoding is required for all input types except WHIP. For WHIP, the default is to not transcode.
94
99
  */
95
100
  enableTranscoding?: boolean | undefined;
101
+ /**
102
+ * whether the ingress is enabled. When set to false, new connection attempts are rejected
103
+ * while the stream key is preserved. The default is true.
104
+ */
105
+ enabled?: boolean;
96
106
  /**
97
107
  * custom audio encoding parameters. optional
98
108
  */
@@ -152,6 +162,7 @@ export class IngressClient extends ServiceBase {
152
162
 
153
163
  const roomName: string | undefined = opts.roomName;
154
164
  const enableTranscoding: boolean | undefined = opts.enableTranscoding;
165
+ const enabled: boolean | undefined = opts.enabled;
155
166
  const audio: IngressAudioOptions | undefined = opts.audio;
156
167
  const video: IngressVideoOptions | undefined = opts.video;
157
168
  const participantMetadata: string | undefined = opts.participantMetadata;
@@ -180,6 +191,7 @@ export class IngressClient extends ServiceBase {
180
191
  bypassTranscoding,
181
192
  enableTranscoding,
182
193
  url,
194
+ enabled,
183
195
  audio,
184
196
  video,
185
197
  }).toJson();
@@ -203,7 +215,7 @@ export class IngressClient extends ServiceBase {
203
215
  const participantName: string = opts.participantName || '';
204
216
  const participantIdentity: string = opts.participantIdentity || '';
205
217
  const { participantMetadata } = opts;
206
- const { audio, video, bypassTranscoding, enableTranscoding } = opts;
218
+ const { audio, video, bypassTranscoding, enableTranscoding, enabled } = opts;
207
219
 
208
220
  const req = new UpdateIngressRequest({
209
221
  ingressId,
@@ -214,6 +226,7 @@ export class IngressClient extends ServiceBase {
214
226
  participantMetadata,
215
227
  bypassTranscoding,
216
228
  enableTranscoding,
229
+ enabled,
217
230
  audio,
218
231
  video,
219
232
  }).toJson();
@@ -1,8 +1,8 @@
1
1
  // SPDX-FileCopyrightText: 2026 LiveKit, Inc.
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
- import { describe, expect, it } from 'vitest';
5
- import { ServerError, SipCallError } from './TwirpRPC.js';
4
+ import { afterEach, describe, expect, it, vi } from 'vitest';
5
+ import { REQUEST_ID_HEADER, ServerError, SipCallError, TwirpRpc } from './TwirpRPC.js';
6
6
 
7
7
  describe('SipCallError', () => {
8
8
  it('renders the SIP status, Twirp code, and extra metadata', () => {
@@ -11,7 +11,7 @@ describe('SipCallError', () => {
11
11
  'Too Many Requests',
12
12
  'twirp error: sip status 486',
13
13
  429,
14
- 'resource_exhausted',
14
+ 'failed_precondition',
15
15
  {
16
16
  sip_status_code: '486',
17
17
  sip_status: 'Busy Here',
@@ -30,7 +30,7 @@ describe('SipCallError', () => {
30
30
  expect(printed).toContain('SipCallError');
31
31
  expect(printed).toContain('486');
32
32
  expect(printed).toContain('Busy Here');
33
- expect(printed).toContain('resource_exhausted');
33
+ expect(printed).toContain('failed_precondition');
34
34
  expect(printed).toContain('region=us-east'); // other metadata is surfaced
35
35
  expect(printed).not.toContain('error_details'); // opaque blob is omitted
36
36
  });
@@ -40,3 +40,75 @@ describe('SipCallError', () => {
40
40
  expect(err.message).toBe('boom');
41
41
  });
42
42
  });
43
+
44
+ describe('request id', () => {
45
+ afterEach(() => {
46
+ vi.restoreAllMocks();
47
+ });
48
+
49
+ const okResponse = () =>
50
+ ({ ok: true, status: 200, json: async () => ({}) }) as unknown as Response;
51
+
52
+ const errorResponse = (status: number) =>
53
+ ({
54
+ ok: false,
55
+ status,
56
+ statusText: 'Service Unavailable',
57
+ headers: { get: () => null },
58
+ text: async () => 'unavailable',
59
+ }) as unknown as Response;
60
+
61
+ // The header lets the server dedup a request that the SDK replayed.
62
+ it('stamps a request id on every call', async () => {
63
+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(okResponse());
64
+
65
+ const rpc = new TwirpRpc('https://test.livekit.cloud', 'livekit', { failover: false });
66
+ await rpc.request('RoomService', 'CreateRoom', {}, {});
67
+ await rpc.request('RoomService', 'CreateRoom', {}, {});
68
+
69
+ const ids = fetchSpy.mock.calls.map(
70
+ ([, init]) => (init!.headers as Record<string, string>)[REQUEST_ID_HEADER],
71
+ );
72
+ expect(ids[0]).toBeTruthy();
73
+ expect(ids[1]).toBeTruthy();
74
+ // A new logical call is a new request, so it gets its own id.
75
+ expect(ids[0]).not.toBe(ids[1]);
76
+ });
77
+
78
+ // The id is generated once per logical call, so every failover attempt must
79
+ // carry the same value.
80
+ it('keeps the same request id across failover attempts', async () => {
81
+ let attempt = 0;
82
+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
83
+ // Region discovery, not a replay of the request itself.
84
+ if (`${input}`.endsWith('/settings/regions')) {
85
+ return {
86
+ ok: true,
87
+ status: 200,
88
+ headers: { get: () => 'max-age=0' },
89
+ json: async () => ({
90
+ regions: [
91
+ { url: 'https://r1.retryid.livekit.cloud' },
92
+ { url: 'https://r2.retryid.livekit.cloud' },
93
+ ],
94
+ }),
95
+ } as unknown as Response;
96
+ }
97
+ attempt += 1;
98
+ return attempt < 3 ? errorResponse(503) : okResponse();
99
+ });
100
+
101
+ const rpc = new TwirpRpc('https://primary.retryid.livekit.cloud', 'livekit', {
102
+ failoverBackoffMs: 0,
103
+ });
104
+ await rpc.request('RoomService', 'CreateRoom', {}, {});
105
+
106
+ const ids = fetchSpy.mock.calls
107
+ .filter(([input]) => !`${input}`.endsWith('/settings/regions'))
108
+ .map(([, init]) => (init!.headers as Record<string, string>)[REQUEST_ID_HEADER]);
109
+
110
+ expect(ids).toHaveLength(3);
111
+ expect(ids[0]).toBeTruthy();
112
+ expect(new Set(ids).size).toBe(1);
113
+ });
114
+ });
package/src/TwirpRPC.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  import type { JsonValue } from '@bufbuild/protobuf';
5
+ import { randomUUID } from './crypto/uuid.js';
5
6
  import {
6
7
  FAILOVER_BACKOFF_BASE_MS,
7
8
  failoverAttempts,
@@ -16,6 +17,11 @@ import { SDK_VERSION } from './version.js';
16
17
  // setting User-Agent via fetch and silently drop it; Node honors it.
17
18
  const USER_AGENT = `livekit-server-sdk-node/${SDK_VERSION}`;
18
19
 
20
+ // Carries a per-request idempotency key. The SDK's auto-retries (see failover)
21
+ // keep the same key across attempts, so the server can identify and deduplicate
22
+ // repeated requests.
23
+ export const REQUEST_ID_HEADER = 'X-Livekit-Request-Id';
24
+
19
25
  // twirp RPC adapter for client implementation
20
26
 
21
27
  type Options = {
@@ -175,11 +181,12 @@ export class TwirpRpc {
175
181
  ): Promise<any> {
176
182
  const path = `${this.prefix}/${this.pkg}.${service}/${method}`;
177
183
  const body = JSON.stringify(data);
178
- const requestHeaders = {
184
+ const requestHeaders: Record<string, string> = {
179
185
  'Content-Type': 'application/json;charset=UTF-8',
180
186
  'User-Agent': USER_AGENT,
181
187
  ...headers,
182
188
  };
189
+ requestHeaders[REQUEST_ID_HEADER] = await randomUUID();
183
190
 
184
191
  const origin = new URL(this.host);
185
192
  const maxAttempts = failoverAttempts(
@@ -2,13 +2,11 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
 
5
- // Use the Web Crypto API if available, otherwise fallback to Node.js crypto
5
+ // Web Crypto only — no import('node:crypto') so isolate/edge bundlers can resolve this module
6
6
  export async function digest(data: string): Promise<ArrayBuffer> {
7
- if (globalThis.crypto?.subtle) {
8
- const encoder = new TextEncoder();
9
- return crypto.subtle.digest('SHA-256', encoder.encode(data));
10
- } else {
11
- const nodeCrypto = await import('node:crypto');
12
- return nodeCrypto.createHash('sha256').update(data).digest();
7
+ if (!globalThis.crypto?.subtle) {
8
+ throw new Error('Web Crypto API is required (globalThis.crypto.subtle)');
13
9
  }
10
+ const encoder = new TextEncoder();
11
+ return crypto.subtle.digest('SHA-256', encoder.encode(data));
14
12
  }