Translate everything to typescript
This commit is contained in:
1 parent
c3ca3e1752
commit
e9818aaeda
27 files changed
+901
-552
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { iceConfiguration, validateDescription, getFingerprints, encodeSignal, decodeSignal, waitForIceComplete, waitForBufferDrain, MAX_SIGNAL_LENGTH, MAX_BUFFERED } from '../src/lib/webrtc.ts';
|
||||
const fingerprint = Array.from({ length: 32 }, (_, i) => i.toString(16).padStart(2, '0')).join(':');
|
||||
const sdp = `v=0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\na=fingerprint:sha-256 ${fingerprint}\r\n`;
|
||||
|
||||
test('TURN is absent by default and dormant credentials cannot enable it', () => {
|
||||
assert.deepEqual(iceConfiguration(), { iceServers: [] });
|
||||
const settings = { stunUrl: 'stun:example.com:3478', turnUrl: 'turn:relay.example', username: 'user', credential: 'secret' };
|
||||
assert.equal(iceConfiguration(settings).iceServers.length, 1);
|
||||
assert.equal(iceConfiguration({ ...settings, allowTurn: true }).iceServers.length, 2);
|
||||
assert.throws(() => iceConfiguration({ stunUrl: 'turn:relay.example' }));
|
||||
assert.throws(() => iceConfiguration({ allowTurn: true }));
|
||||
});
|
||||
|
||||
test('remote relay candidates require local explicit opt-in', () => {
|
||||
const relayed = sdp + 'a=candidate:1 1 udp 10 192.0.2.1 5000 typ relay raddr 0.0.0.0 rport 0\r\n';
|
||||
assert.throws(() => decodeSignal(encodeSignal(relayed)), /explicitly enable TURN/);
|
||||
assert.equal(decodeSignal(encodeSignal(relayed), { allowTurn: true }), relayed);
|
||||
});
|
||||
|
||||
test('fingerprints show all 32 bytes and reject ambiguous descriptions', () => {
|
||||
const expected = fingerprint.toUpperCase().replaceAll(':', ' ');
|
||||
assert.equal(validateDescription(sdp), expected);
|
||||
assert.deepEqual(getFingerprints({ localDescription: { sdp }, remoteDescription: { sdp } }), { local: expected, remote: expected });
|
||||
assert.throws(() => validateDescription(sdp + `a=fingerprint:sha-256 ${fingerprint}\r\n`));
|
||||
assert.throws(() => validateDescription(sdp.replace(fingerprint, 'AA:BB:CC:DD:EE')));
|
||||
assert.throws(() => validateDescription(sdp + 'm=audio 9 UDP/TLS/RTP/SAVPF 111\r\n'));
|
||||
});
|
||||
|
||||
test('signals round-trip but reject legacy, malformed and oversized codes', () => {
|
||||
assert.equal(decodeSignal(encodeSignal(sdp)), sdp);
|
||||
assert.throws(() => decodeSignal(btoa(sdp)));
|
||||
assert.throws(() => decodeSignal('bad!'));
|
||||
assert.throws(() => decodeSignal('a'.repeat(MAX_SIGNAL_LENGTH + 1)));
|
||||
assert.throws(() => decodeSignal(btoa(JSON.stringify({ version: 1, sdp }))));
|
||||
});
|
||||
|
||||
class Peer extends EventTarget { iceGatheringState: RTCIceGatheringState = 'gathering'; connectionState: RTCPeerConnectionState = 'new'; }
|
||||
|
||||
test('ICE gathering waits for completion, rejects timeout, and supports cancellation', async () => {
|
||||
const pc = new Peer();
|
||||
const completed = waitForIceComplete(pc, { timeout: 100 });
|
||||
pc.iceGatheringState = 'complete'; pc.dispatchEvent(new Event('icegatheringstatechange')); await completed;
|
||||
await assert.rejects(waitForIceComplete(new Peer(), { timeout: 5 }), /timed out/);
|
||||
const controller = new AbortController();
|
||||
const cancelled = waitForIceComplete(new Peer(), { signal: controller.signal }); controller.abort();
|
||||
await assert.rejects(cancelled, /cancelled/);
|
||||
});
|
||||
|
||||
test('buffer wait resolves when already drained and rejects close or cancellation', async () => {
|
||||
const channel = new class extends EventTarget {
|
||||
readyState: RTCDataChannelState = 'open';
|
||||
bufferedAmount = 0;
|
||||
bufferedAmountLowThreshold = 0;
|
||||
}();
|
||||
await waitForBufferDrain(channel);
|
||||
channel.bufferedAmount = MAX_BUFFERED + 1;
|
||||
const wait = waitForBufferDrain(channel); channel.readyState = 'closed'; channel.dispatchEvent(new Event('close'));
|
||||
await assert.rejects(wait, /interrupted/);
|
||||
channel.readyState = 'open'; const controller = new AbortController();
|
||||
const cancelled = waitForBufferDrain(channel, { signal: controller.signal }); controller.abort();
|
||||
await assert.rejects(cancelled, /interrupted/);
|
||||
});
|
||||
Reference in new issue
Block a user