rtc-link
Version:
A library for managing WebRTC connections with multiplexed streams, anti-glare handling, and streamlined data channel setup.
100 lines (81 loc) • 3.18 kB
JavaScript
import { test, solo } from 'brittle';
import { establishSignalStream, establishWrtcStream } from '../index.js';
import {createPlexPair, get} from "rxprotoplex";
import b4a from "b4a";
const RUNNING_IN_GITHUB_ACTIONS = !!get(globalThis, "process.env.RUNNING_IN_ACTIONS")
if (!globalThis.window && !RUNNING_IN_GITHUB_ACTIONS) {
console.log("Importing @roamhq/wrtc");
Object.assign(globalThis, await import("@roamhq/wrtc").then(o => o.default));
}
test('establishSignalStream should set up signaling correctly', {skip: !!RUNNING_IN_GITHUB_ACTIONS}, async (t) => {
t.plan(1);
t.comment(`
This setup allows communication between stream1 and stream2,
and between stream3 and stream4, mimicking a network relay
where data travels through stream2 to reach stream4.
`);
const [stream1, stream2] = createPlexPair();
const [stream3, stream4] = createPlexPair();
// Establish signaling between streams
const closer = establishSignalStream(stream2, stream4);
// Do the actual signaling through the established multiplex made above.
const rtc1 = establishWrtcStream(true, stream1);
const rtc2 = establishWrtcStream(false, stream3);
rtc1.on("data", data => {
t.is("hello", b4a.toString(data));
});
rtc2.end(b4a.from("hello"));
t.teardown(() => {
rtc1.destroy();
rtc2.destroy();
closer();
});
});
test('establishWrtcStream should establish a WebRTC connection', {skip: !!RUNNING_IN_GITHUB_ACTIONS}, async (t) => {
t.plan(1);
const [stream1, stream2] = createPlexPair();
const rtc1 = establishWrtcStream(true, stream1);
const rtc2 = establishWrtcStream(false, stream2);
rtc1.on("data", data => {
t.is("hello", b4a.toString(data));
});
rtc2.end(b4a.from("hello"));
t.teardown(() => {
rtc1.destroy();
rtc2.destroy();
});
});
test('establishWrtcStream should establish a WebRTC connection even if both are initiators (anti-glare)', {skip: !!RUNNING_IN_GITHUB_ACTIONS}, async (t) => {
t.comment("Notice in console there is a warning about it.");
t.plan(1);
const [stream1, stream2] = createPlexPair();
const rtc1 = establishWrtcStream(true, stream1);
const rtc2 = establishWrtcStream(true, stream2);
rtc1.on("data", data => {
t.is("hello", b4a.toString(data));
});
rtc2.end(b4a.from("hello"));
t.teardown(() => {
rtc1.destroy();
rtc2.destroy();
});
});
if (RUNNING_IN_GITHUB_ACTIONS) {
test("BYPASS test in github actions.", (t) => {
t.comment(`
TESTS SUCCEED, THE @roamhq/wrtc package that allows usage of webrtc
in node is producing a segfault. This works fine in web browser webrtc.
see: https://github.com/WonderInventions/node-webrtc/issues/7
You can test it in node, however, know that it will cause segfault.
`);
t.pass();
});
} else {
test("Known issue, segfault", (t) => {
t.comment(`
This test will end in segfault, waiting for issue to be resolved via @roamhq/wrtc.
see: https://github.com/WonderInventions/node-webrtc/issues/7
`) ;
t.pass();
});
}