WIP IV improve UX [snapshot task 2]
This commit is contained in:
1 parent
28c90d1d71
commit
58c3e2af86
16 files changed
+779
-36
No files matched your search
+13
-6
@@ -1,3 +1,4 @@
|
||||
import { parseHello, type IdentityHello } from './identity.ts';
|
||||
import { asError, isRecord, isStringArray } from './validation.ts';
|
||||
|
||||
export interface IceOptions {
|
||||
@@ -20,7 +21,7 @@ export interface NetworkSettings {
|
||||
}
|
||||
|
||||
export interface WaitOptions { signal?: AbortSignal; timeout?: number }
|
||||
type SignalOptions = WaitOptions & Pick<IceOptions, 'allowTurn'>;
|
||||
type SignalOptions = WaitOptions & Pick<IceOptions, 'allowTurn'> & { identity?: IdentityHello };
|
||||
export interface Fingerprints { local: string; remote: string }
|
||||
export type IcePeer = EventTarget & Pick<RTCPeerConnection, 'iceGatheringState' | 'connectionState'>;
|
||||
export type BufferedChannel = EventTarget & Pick<RTCDataChannel, 'readyState' | 'bufferedAmount' | 'bufferedAmountLowThreshold'>;
|
||||
@@ -131,8 +132,8 @@ export function getFingerprints(pc: { localDescription: { sdp: string } | null;
|
||||
};
|
||||
}
|
||||
|
||||
export function encodeSignal(sdp: string) {
|
||||
const code = btoa(JSON.stringify({ version: 3, sdp })).replace(/=+$/, '');
|
||||
export function encodeSignal(sdp: string, identity?: IdentityHello) {
|
||||
const code = btoa(JSON.stringify(identity ? { version: 4, sdp, identity: parseHello(identity) } : { version: 3, sdp })).replace(/=+$/, '');
|
||||
if (code.length > MAX_SIGNAL_LENGTH) throw new Error('Connection code is too large. Reduce the configured network interfaces and try again.');
|
||||
return code;
|
||||
}
|
||||
@@ -142,8 +143,9 @@ export function decodeSignal(text: unknown, options?: Pick<IceOptions, 'allowTur
|
||||
try {
|
||||
const clean = text.trim().replace(/\s+/g, '');
|
||||
const data: unknown = JSON.parse(atob(clean));
|
||||
if (!isRecord(data) || data.version !== 3) throw new Error('Both people need the current HellCon version.');
|
||||
if (!isRecord(data) || ![3, 4].includes(data.version as number)) throw new Error('Both people need the current HellCon version.');
|
||||
if (typeof data.sdp !== 'string') throw new Error('Invalid connection description.');
|
||||
if (data.version === 4) parseHello(data.identity);
|
||||
validateDescription(data.sdp, options);
|
||||
return data.sdp;
|
||||
} catch (err) {
|
||||
@@ -157,7 +159,7 @@ export async function createOfferCode(pc: RTCPeerConnection, options: SignalOpti
|
||||
await waitForIceComplete(pc, options);
|
||||
if (!pc.localDescription) throw new Error('Missing local connection description.');
|
||||
validateDescription(pc.localDescription.sdp, options);
|
||||
return encodeSignal(pc.localDescription.sdp);
|
||||
return encodeSignal(pc.localDescription.sdp, options.identity);
|
||||
}
|
||||
|
||||
export async function createAnswerCode(pc: RTCPeerConnection, text: string, options: SignalOptions = {}) {
|
||||
@@ -166,7 +168,7 @@ export async function createAnswerCode(pc: RTCPeerConnection, text: string, opti
|
||||
await waitForIceComplete(pc, options);
|
||||
if (!pc.localDescription) throw new Error('Missing local connection description.');
|
||||
validateDescription(pc.localDescription.sdp, options);
|
||||
return encodeSignal(pc.localDescription.sdp);
|
||||
return encodeSignal(pc.localDescription.sdp, options.identity);
|
||||
}
|
||||
|
||||
export function acceptAnswerCode(pc: RTCPeerConnection, text: string, options?: SignalOptions) {
|
||||
@@ -198,3 +200,8 @@ export function waitForBufferDrain(dc: BufferedChannel, { signal, timeout = 6000
|
||||
check();
|
||||
});
|
||||
}
|
||||
|
||||
export function signalIdentity(text: string): IdentityHello | undefined {
|
||||
const data = JSON.parse(atob(text.trim().replace(/\s+/g, '')));
|
||||
return data.version === 4 ? parseHello(data.identity) : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user