0
# Factory and Configuration
1
2
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.
3
4
## Capabilities
5
6
### Adapter Factory Function
7
8
Creates adapter instances with configurable shim options.
9
10
```javascript { .api }
11
/**
12
* Creates an adapter instance with configurable shim options
13
* @param window - Object containing window reference (optional)
14
* @param options - Configuration options for enabling/disabling shims
15
* @returns Configured adapter instance
16
*/
17
function adapterFactory(
18
{window}?: {window?: Window},
19
options?: {
20
shimChrome?: boolean;
21
shimFirefox?: boolean;
22
shimSafari?: boolean;
23
}
24
): IAdapter;
25
```
26
27
**Default Options:**
28
```javascript
29
{
30
shimChrome: true,
31
shimFirefox: true,
32
shimSafari: true
33
}
34
```
35
36
**Usage Examples:**
37
38
```javascript
39
import { adapterFactory } from 'webrtc-adapter/src/js/adapter_factory.js';
40
41
// Default configuration (all shims enabled)
42
const adapter = adapterFactory({window});
43
44
// Disable Safari shims for performance
45
const chromeFirefoxAdapter = adapterFactory({window}, {
46
shimChrome: true,
47
shimFirefox: true,
48
shimSafari: false
49
});
50
51
// Chrome-only adapter
52
const chromeOnlyAdapter = adapterFactory({window}, {
53
shimChrome: true,
54
shimFirefox: false,
55
shimSafari: false
56
});
57
58
// Custom window object (useful for Node.js or testing environments)
59
const serverAdapter = adapterFactory({window: undefined}, {
60
shimChrome: false,
61
shimFirefox: false,
62
shimSafari: false
63
});
64
65
// Testing with mock window
66
const mockWindow = { RTCPeerConnection: MockRTCPeerConnection };
67
const testAdapter = adapterFactory({window: mockWindow});
68
```
69
70
### Adapter Interface
71
72
The complete interface returned by the factory function.
73
74
```javascript { .api }
75
interface IAdapter {
76
/** Browser detection information */
77
browserDetails: IBrowserDetails;
78
/** Common shims applied to all browsers */
79
commonShim: ICommonShim;
80
/** Browser-specific shims (only populated for detected browser) */
81
browserShim: IChromeShim | IFirefoxShim | ISafariShim | undefined;
82
/** Version extraction utility function */
83
extractVersion: (uastring: string, expr: string, pos: number) => number;
84
/** Logging control function */
85
disableLog: (disable: boolean) => string | Error;
86
/** Deprecation warning control function */
87
disableWarnings: (disable: boolean) => string | Error;
88
/** SDP utilities from sdp package dependency */
89
sdp: typeof SDPUtils;
90
}
91
```
92
93
### Configuration Behavior
94
95
The factory applies shims based on browser detection and configuration:
96
97
**When `shimChrome: true` and Chrome detected:**
98
- Applies all Chrome-specific shims
99
- Sets `adapter.browserShim` to Chrome shim interface
100
- Logs "adapter.js shimming chrome."
101
102
**When `shimFirefox: true` and Firefox detected:**
103
- Applies all Firefox-specific shims
104
- Sets `adapter.browserShim` to Firefox shim interface
105
- Logs "adapter.js shimming firefox."
106
107
**When `shimSafari: true` and Safari detected:**
108
- Applies all Safari-specific shims
109
- Sets `adapter.browserShim` to Safari shim interface
110
- Logs "adapter.js shimming safari."
111
112
**When shims are disabled:**
113
- Logs appropriate warning message
114
- Returns minimal adapter with only browser detection
115
- `adapter.browserShim` remains `undefined`
116
117
## Advanced Configuration Patterns
118
119
### Conditional Shim Loading
120
121
```javascript
122
import { adapterFactory } from 'webrtc-adapter/src/js/adapter_factory.js';
123
124
// Load shims based on environment
125
const isInstrumented = process.env.NODE_ENV === 'development';
126
const adapter = adapterFactory({window}, {
127
shimChrome: !isInstrumented, // Disable for debugging
128
shimFirefox: true,
129
shimSafari: true
130
});
131
```
132
133
### Library Integration
134
135
```javascript
136
// Create adapter for specific library requirements
137
function createWebRTCAdapter(targetBrowser) {
138
const options = {
139
shimChrome: targetBrowser === 'chrome' || targetBrowser === 'all',
140
shimFirefox: targetBrowser === 'firefox' || targetBrowser === 'all',
141
shimSafari: targetBrowser === 'safari' || targetBrowser === 'all'
142
};
143
144
return adapterFactory({window}, options);
145
}
146
147
const adapter = createWebRTCAdapter('chrome'); // Chrome-only shims
148
```
149
150
### Server-Side Usage
151
152
```javascript
153
// Node.js environment without window object
154
const serverAdapter = adapterFactory({window: undefined}, {
155
shimChrome: false,
156
shimFirefox: false,
157
shimSafari: false
158
});
159
160
// Access utilities without browser shims
161
const version = serverAdapter.extractVersion(userAgentString, pattern, position);
162
```
163
164
## Performance Considerations
165
166
- **Selective Shimming**: Disabling unused browser shims reduces bundle size and initialization time
167
- **Memory Usage**: Each adapter instance maintains separate shim state
168
- **Browser Detection**: Performed once during adapter creation
169
- **Lazy Loading**: Shims are only applied when the corresponding browser is detected