// SPDX-FileCopyrightText: 2024 LiveKit, Inc. // // SPDX-License-Identifier: Apache-2.0 import type { ByteStreamInfo as ProtoByteStreamInfo, TextStreamInfo as ProtoTextStreamInfo, } from '@livekit/rtc-ffi-bindings'; import type { ByteStreamInfo, TextStreamInfo } from './data_streams/types.js'; /** convert bigints to numbers preserving undefined values */ export function bigIntToNumber( value: T, ): T extends bigint ? number : undefined { return (value !== undefined ? Number(value) : undefined) as T extends bigint ? number : undefined; } /** convert numbers to bigints preserving undefined values */ export function numberToBigInt( value: T, ): T extends number ? bigint : undefined { return (value !== undefined ? BigInt(value) : undefined) as T extends number ? bigint : undefined; } export function splitUtf8(s: string, n: number): Uint8Array[] { if (n < 4) { throw new Error('n must be at least 4 due to utf8 encoding rules'); } // adapted from https://stackoverflow.com/a/6043797 const result: Uint8Array[] = []; let encoded = new TextEncoder().encode(s); while (encoded.length > n) { let k = n; while (k > 0) { const byte = encoded[k]; if (byte !== undefined && (byte & 0xc0) !== 0x80) { break; } k--; } result.push(encoded.slice(0, k)); encoded = encoded.slice(k); } if (encoded.length > 0) { result.push(encoded); } return result; } /** @internal */ export function textStreamInfoFromProto(info: ProtoTextStreamInfo): TextStreamInfo { return { streamId: info.streamId!, mimeType: info.mimeType!, topic: info.topic!, timestamp: bigIntToNumber(info.timestamp!), totalSize: info.totalLength ? bigIntToNumber(info.totalLength) : undefined, attributes: info.attributes, }; } /** @internal */ export function byteStreamInfoFromProto(info: ProtoByteStreamInfo): ByteStreamInfo { return { streamId: info.streamId!, name: info.name ?? 'unknown', mimeType: info.mimeType!, topic: info.topic!, timestamp: bigIntToNumber(info.timestamp!), totalSize: info.totalLength !== undefined ? bigIntToNumber(info.totalLength) : undefined, attributes: info.attributes, }; }