Use compressed connection codes in the transfer panel

Offers and answers now go through the HC1 codec: the sender creates a
ref per session and the answer must echo it, so pasting an offer as the
answer or an answer for another code is rejected with a specific message
while the live offer stays usable. Pasted input may be a full link.

webrtc.ts drops its Base64 JSON signaling and exchanges validated SDP
only; identity hellos travel in the code envelope. Tests, the browser
check, compare-ice and the docs follow the new format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-19 22:35:44 +02:00
1 parent da2999ed5d
commit 8bd37ac8b1
10 files changed
+68 -89

No files matched your search

+13 -38
View File
@@ -1,5 +1,4 @@
import { parseHello, type IdentityHello } from './identity.ts';
import { asError, isRecord, isStringArray } from './validation.ts';
import { isRecord, isStringArray } from './validation.ts';
export interface IceOptions {
stunUrl?: string | null;
@@ -21,13 +20,13 @@ export interface NetworkSettings {
}
export interface WaitOptions { signal?: AbortSignal; timeout?: number }
type SignalOptions = WaitOptions & Pick<IceOptions, 'allowTurn'> & { identity?: IdentityHello };
type SignalOptions = WaitOptions & Pick<IceOptions, 'allowTurn'>;
export interface Fingerprints { local: string; remote: string }
export type IcePeer = EventTarget & Pick<RTCPeerConnection, 'iceGatheringState' | 'connectionState'>;
export type BufferedChannel = EventTarget & Pick<RTCDataChannel, 'readyState' | 'bufferedAmount' | 'bufferedAmountLowThreshold'>;
type CredentialFetch = (url: string, init: RequestInit) => Promise<Pick<Response, 'ok' | 'json'>>;
// Manual signaling. Codes are encoded connection details, not encrypted secrets.
// Manual signaling; codes.ts encodes the validated descriptions for exchange.
export const CHUNK_SIZE = 16 * 1024;
export const MAX_BUFFERED = 1024 * 1024;
export const MAX_SIGNAL_LENGTH = 64 * 1024;
@@ -132,47 +131,28 @@ export function getFingerprints(pc: { localDescription: { sdp: string } | null;
};
}
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;
}
export function decodeSignal(text: unknown, options?: Pick<IceOptions, 'allowTurn'>): string {
if (typeof text !== 'string' || text.length > MAX_SIGNAL_LENGTH) throw new Error('Connection code is too large.');
try {
const clean = text.trim().replace(/\s+/g, '');
const data: unknown = JSON.parse(atob(clean));
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) {
if (err instanceof SyntaxError || asError(err).name === 'InvalidCharacterError') throw new Error('Invalid connection code. Paste the complete code.');
throw err;
}
}
export async function createOfferCode(pc: RTCPeerConnection, options: SignalOptions = {}) {
// Each step validates the description it produces or applies.
export async function createOffer(pc: RTCPeerConnection, options: SignalOptions = {}) {
await pc.setLocalDescription(await pc.createOffer());
await waitForIceComplete(pc, options);
if (!pc.localDescription) throw new Error('Missing local connection description.');
validateDescription(pc.localDescription.sdp, options);
return encodeSignal(pc.localDescription.sdp, options.identity);
return pc.localDescription.sdp;
}
export async function createAnswerCode(pc: RTCPeerConnection, text: string, options: SignalOptions = {}) {
await pc.setRemoteDescription({ type: 'offer', sdp: decodeSignal(text, options) });
export async function createAnswer(pc: RTCPeerConnection, offer: string, options: SignalOptions = {}) {
validateDescription(offer, options);
await pc.setRemoteDescription({ type: 'offer', sdp: offer });
await pc.setLocalDescription(await pc.createAnswer());
await waitForIceComplete(pc, options);
if (!pc.localDescription) throw new Error('Missing local connection description.');
validateDescription(pc.localDescription.sdp, options);
return encodeSignal(pc.localDescription.sdp, options.identity);
return pc.localDescription.sdp;
}
export function acceptAnswerCode(pc: RTCPeerConnection, text: string, options?: SignalOptions) {
return pc.setRemoteDescription({ type: 'answer', sdp: decodeSignal(text, options) });
export function acceptAnswer(pc: RTCPeerConnection, answer: string, options?: SignalOptions) {
validateDescription(answer, options);
return pc.setRemoteDescription({ type: 'answer', sdp: answer });
}
export function waitForBufferDrain(dc: BufferedChannel, { signal, timeout = 60000 }: WaitOptions = {}) {
@@ -200,8 +180,3 @@ 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;
}