From d02c654bd395e7036be739c7da40eaad1477bace Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 15:06:00 +0800 Subject: [PATCH] Import packages/webrtc/src/util.ts --- packages/webrtc/src/util.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/webrtc/src/util.ts diff --git a/packages/webrtc/src/util.ts b/packages/webrtc/src/util.ts new file mode 100644 index 0000000..1d0b71c --- /dev/null +++ b/packages/webrtc/src/util.ts @@ -0,0 +1,18 @@ +export async function socketText(data: string | ArrayBuffer | Blob): Promise { + if (typeof data === 'string') return data + if (data instanceof ArrayBuffer) return new TextDecoder().decode(data) + return new TextDecoder().decode(await data.arrayBuffer()) +} + +export function toBase64Url(bytes: Uint8Array): string { + const binary = Array.from(bytes, byte => String.fromCharCode(byte)).join('') + const base64 = typeof btoa === 'function' ? btoa(binary) : Buffer.from(bytes).toString('base64') + return base64.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '') +} + +export function fromBase64Url(value: string): Uint8Array { + const normalized = value.replaceAll('-', '+').replaceAll('_', '/') + const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '=') + if (typeof atob === 'function') return Uint8Array.from(atob(padded), char => char.charCodeAt(0)) + return new Uint8Array(Buffer.from(padded, 'base64')) +}