From 85a499646fe99beea84f424b6f14bf9866c05e79 Mon Sep 17 00:00:00 2001 From: Marcin Wojtczak Date: Wed, 14 Jan 2026 12:18:37 +0000 Subject: [PATCH] feat: added supportsRTCPeerConnection() --- src/web-capabilities.spec.ts | 41 ++++++++++++++++++++++++++++++++++++ src/web-capabilities.ts | 12 +++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/web-capabilities.spec.ts b/src/web-capabilities.spec.ts index ddc2da2..a41c84d 100644 --- a/src/web-capabilities.spec.ts +++ b/src/web-capabilities.spec.ts @@ -206,4 +206,45 @@ describe('WebCapabilities', () => { ); }); }); + + describe('supportsRTCPeerConnection', () => { + afterEach(() => { + // Clean up window modifications + delete (window as Window & { RTCPeerConnection?: unknown }).RTCPeerConnection; + }); + + /** + * Sets up the window object with or without RTCPeerConnection. + * @param hasRTCPeerConnection - True when it should exist, false otherwise. + */ + const setupWindow = (hasRTCPeerConnection: boolean) => { + if (hasRTCPeerConnection) { + /** + * Mock RTCPeerConnection constructor for testing. + */ + // eslint-disable-next-line @typescript-eslint/no-empty-function + const MockRTCPeerConnection = function MockRTCPeerConnection() {}; + Object.defineProperty(window, 'RTCPeerConnection', { + writable: true, + configurable: true, + value: MockRTCPeerConnection, + }); + } else { + delete (window as Window & { RTCPeerConnection?: unknown }).RTCPeerConnection; + } + }; + + it('returns true when RTCPeerConnection is available', () => { + expect.assertions(1); + setupWindow(true); + expect(WebCapabilities.supportsRTCPeerConnection()).toBe(CapabilityState.CAPABLE); + }); + + it('returns false when RTCPeerConnection is not available', () => { + expect.assertions(1); + setupWindow(false); + + expect(WebCapabilities.supportsRTCPeerConnection()).toBe(CapabilityState.NOT_CAPABLE); + }); + }); }); diff --git a/src/web-capabilities.ts b/src/web-capabilities.ts index f45a79c..09f097d 100644 --- a/src/web-capabilities.ts +++ b/src/web-capabilities.ts @@ -110,4 +110,16 @@ export class WebCapabilities { ? CapabilityState.CAPABLE : CapabilityState.NOT_CAPABLE; } + + /** + * Checks whether the browser supports RTCPeerConnection. This is needed, + * because some users install browser extensions that remove RTCPeerConnection. + * + * @returns A {@link CapabilityState}. + */ + static supportsRTCPeerConnection(): CapabilityState { + return typeof RTCPeerConnection === 'function' + ? CapabilityState.CAPABLE + : CapabilityState.NOT_CAPABLE; + } }