A shim to insulate apps from WebRTC spec changes and browser prefix differences
Overall
score
98%
The adapter factory provides fine-grained control over which browser shims are applied, allowing developers to create customized adapter instances for specific use cases or performance optimization.
Creates adapter instances with configurable shim options.
/**
* Creates an adapter instance with configurable shim options
* @param window - Object containing window reference (optional)
* @param options - Configuration options for enabling/disabling shims
* @returns Configured adapter instance
*/
function adapterFactory(
{window}?: {window?: Window},
options?: {
shimChrome?: boolean;
shimFirefox?: boolean;
shimSafari?: boolean;
}
): IAdapter;Default Options:
{
shimChrome: true,
shimFirefox: true,
shimSafari: true
}Usage Examples:
import { adapterFactory } from 'webrtc-adapter/src/js/adapter_factory.js';
// Default configuration (all shims enabled)
const adapter = adapterFactory({window});
// Disable Safari shims for performance
const chromeFirefoxAdapter = adapterFactory({window}, {
shimChrome: true,
shimFirefox: true,
shimSafari: false
});
// Chrome-only adapter
const chromeOnlyAdapter = adapterFactory({window}, {
shimChrome: true,
shimFirefox: false,
shimSafari: false
});
// Custom window object (useful for Node.js or testing environments)
const serverAdapter = adapterFactory({window: undefined}, {
shimChrome: false,
shimFirefox: false,
shimSafari: false
});
// Testing with mock window
const mockWindow = { RTCPeerConnection: MockRTCPeerConnection };
const testAdapter = adapterFactory({window: mockWindow});The complete interface returned by the factory function.
interface IAdapter {
/** Browser detection information */
browserDetails: IBrowserDetails;
/** Common shims applied to all browsers */
commonShim: ICommonShim;
/** Browser-specific shims (only populated for detected browser) */
browserShim: IChromeShim | IFirefoxShim | ISafariShim | undefined;
/** Version extraction utility function */
extractVersion: (uastring: string, expr: string, pos: number) => number;
/** Logging control function */
disableLog: (disable: boolean) => string | Error;
/** Deprecation warning control function */
disableWarnings: (disable: boolean) => string | Error;
/** SDP utilities from sdp package dependency */
sdp: typeof SDPUtils;
}The factory applies shims based on browser detection and configuration:
When shimChrome: true and Chrome detected:
adapter.browserShim to Chrome shim interfaceWhen shimFirefox: true and Firefox detected:
adapter.browserShim to Firefox shim interfaceWhen shimSafari: true and Safari detected:
adapter.browserShim to Safari shim interfaceWhen shims are disabled:
adapter.browserShim remains undefinedimport { adapterFactory } from 'webrtc-adapter/src/js/adapter_factory.js';
// Load shims based on environment
const isInstrumented = process.env.NODE_ENV === 'development';
const adapter = adapterFactory({window}, {
shimChrome: !isInstrumented, // Disable for debugging
shimFirefox: true,
shimSafari: true
});// Create adapter for specific library requirements
function createWebRTCAdapter(targetBrowser) {
const options = {
shimChrome: targetBrowser === 'chrome' || targetBrowser === 'all',
shimFirefox: targetBrowser === 'firefox' || targetBrowser === 'all',
shimSafari: targetBrowser === 'safari' || targetBrowser === 'all'
};
return adapterFactory({window}, options);
}
const adapter = createWebRTCAdapter('chrome'); // Chrome-only shims// Node.js environment without window object
const serverAdapter = adapterFactory({window: undefined}, {
shimChrome: false,
shimFirefox: false,
shimSafari: false
});
// Access utilities without browser shims
const version = serverAdapter.extractVersion(userAgentString, pattern, position);Install with Tessl CLI
npx tessl i tessl/npm-webrtc-adapterdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10