@marianmeres/webrtc 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md CHANGED
@@ -114,9 +114,11 @@ ERROR --RESET--> IDLE
114
114
  ### Constructor
115
115
 
116
116
  ```typescript
117
- new WebRtcManager(factory: WebRtcFactory, config?: WebRtcManagerConfig)
117
+ new WebRtcManager<TContext = unknown>(factory: WebRtcFactory, config?: WebRtcManagerConfig)
118
118
  ```
119
119
 
120
+ **Type Parameter:** `TContext` - Optional type for the `context` property (default: `unknown`)
121
+
120
122
  ### Logger Interface
121
123
 
122
124
  Console-compatible logger interface for custom logging implementations.
@@ -166,6 +168,7 @@ interface WebRtcManagerConfig {
166
168
  | remoteStream | MediaStream \| null | Remote audio stream |
167
169
  | dataChannels | ReadonlyMap<string, RTCDataChannel> | Active data channels |
168
170
  | peerConnection | RTCPeerConnection \| null | Underlying connection |
171
+ | context | TContext \| null | User-defined context for arbitrary data |
169
172
 
170
173
  ### Lifecycle Methods
171
174
 
package/API.md CHANGED
@@ -31,9 +31,15 @@ The main class for managing WebRTC connections.
31
31
  ### Constructor
32
32
 
33
33
  ```typescript
34
- new WebRtcManager(factory: WebRtcFactory, config?: WebRtcManagerConfig)
34
+ new WebRtcManager<TContext = unknown>(factory: WebRtcFactory, config?: WebRtcManagerConfig)
35
35
  ```
36
36
 
37
+ **Type Parameters:**
38
+
39
+ | Name | Default | Description |
40
+ |------|---------|-------------|
41
+ | TContext | `unknown` | Type for the `context` property |
42
+
37
43
  **Parameters:**
38
44
 
39
45
  | Name | Type | Required | Description |
@@ -123,6 +129,33 @@ Returns the underlying RTCPeerConnection, or `null` if not initialized.
123
129
 
124
130
  ---
125
131
 
132
+ #### context
133
+
134
+ ```typescript
135
+ context: TContext | null
136
+ ```
137
+
138
+ User-defined context object for storing arbitrary data associated with this manager. The class accepts an optional generic type parameter for type-safe context access.
139
+
140
+ **Type Parameter:** `TContext` - The type of the context object (default: `unknown`)
141
+
142
+ **Default:** `null`
143
+
144
+ **Example:**
145
+
146
+ ```typescript
147
+ // With type parameter for full type safety:
148
+ const manager = new WebRtcManager<{ audioStream: MediaStream; sessionId: string }>(factory);
149
+ manager.context = { audioStream: myStream, sessionId: '123' };
150
+ manager.context.audioStream; // typed as MediaStream
151
+
152
+ // Without type parameter (backwards compatible):
153
+ const manager = new WebRtcManager(factory);
154
+ manager.context = { anything: 'goes' };
155
+ ```
156
+
157
+ ---
158
+
126
159
  ### Lifecycle Methods
127
160
 
128
161
  #### initialize()
package/README.md CHANGED
@@ -38,9 +38,11 @@ The manager doesn't handle the signaling transport layer - you're responsible fo
38
38
  ### Constructor
39
39
 
40
40
  ```typescript
41
- const manager = new WebRtcManager(factory, config);
41
+ const manager = new WebRtcManager<TContext>(factory, config);
42
42
  ```
43
43
 
44
+ - `TContext`: Optional type parameter for the `context` property (default: `unknown`)
45
+
44
46
  - `factory`: Object implementing `WebRtcFactory` interface (provides `createPeerConnection`, `getUserMedia`, `enumerateDevices`)
45
47
  - `config`: Optional configuration object
46
48
 
@@ -64,6 +66,7 @@ manager.localStream // MediaStream | null
64
66
  manager.remoteStream // MediaStream | null
65
67
  manager.dataChannels // ReadonlyMap<string, RTCDataChannel>
66
68
  manager.peerConnection // RTCPeerConnection | null
69
+ manager.context // TContext | null - user-defined data
67
70
  ```
68
71
 
69
72
  ### Lifecycle Methods
@@ -456,24 +459,31 @@ Mock-based tests for the manager's logic and state transitions:
456
459
  deno task test
457
460
  ```
458
461
 
459
- ### Browser Integration Tests
462
+ ### Interactive Example
460
463
 
461
- Real peer-to-peer connection tests running in a browser environment:
464
+ The `example/` directory contains a working demo of two peers communicating via WebRTC data channels:
462
465
 
463
466
  ```bash
464
- deno task test:browser
467
+ # Build the example bundle
468
+ deno task example:build
469
+
470
+ # Serve the example directory
471
+ cd example && deno run -A jsr:@std/http/file-server
465
472
  ```
466
473
 
467
- This builds the test bundle and starts a local server. Open the provided URL in your browser to run the tests interactively.
474
+ Then open `http://localhost:8000/` in your browser.
468
475
 
469
- The browser tests verify:
470
- - Actual P2P connections between peers
471
- - Data channel message exchange
472
- - ICE candidate exchange
473
- - Connection state transitions
474
- - Resource cleanup
476
+ **Structure:**
477
+ - `index.html` - Parent page with two side-by-side iframes, acts as signaling relay via `postMessage`
478
+ - `peer1.html` - The "offerer" peer (click "Connect" to initiate)
479
+ - `peer2.html` - The "answerer" peer (waits for connection)
475
480
 
476
- See [tests/browser/README.md](tests/browser/README.md) for more details.
481
+ This example demonstrates:
482
+ - P2P connection establishment without a signaling server (uses `postMessage` between iframes)
483
+ - SDP offer/answer exchange
484
+ - ICE candidate exchange
485
+ - Data channel creation and message passing
486
+ - State change monitoring
477
487
 
478
488
  ## License
479
489
 
@@ -25,7 +25,7 @@ import { type WebRtcFactory, type WebRtcManagerConfig, WebRtcState, type WebRtcE
25
25
  * await manager.setLocalDescription(offer);
26
26
  * ```
27
27
  */
28
- export declare class WebRtcManager {
28
+ export declare class WebRtcManager<TContext = unknown> {
29
29
  #private;
30
30
  /** Event emitted when connection state changes. Payload: {@link WebRtcState} */
31
31
  static readonly EVENT_STATE_CHANGE = "state_change";
@@ -51,6 +51,24 @@ export declare class WebRtcManager {
51
51
  static readonly EVENT_MICROPHONE_FAILED = "microphone_failed";
52
52
  /** Event emitted when an error occurs. Payload: `Error` */
53
53
  static readonly EVENT_ERROR = "error";
54
+ /**
55
+ * User-defined context object for storing arbitrary data associated with this manager.
56
+ * Useful for attaching application-specific state (e.g., audio streams, metadata)
57
+ * without modifying the manager internals.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * // With type parameter for full type safety:
62
+ * const manager = new WebRtcManager<{ audioStream: MediaStream; sessionId: string }>(factory);
63
+ * manager.context = { audioStream: myStream, sessionId: '123' };
64
+ * manager.context.audioStream; // typed as MediaStream
65
+ *
66
+ * // Without type parameter (backwards compatible):
67
+ * const manager = new WebRtcManager(factory);
68
+ * manager.context = { anything: 'goes' };
69
+ * ```
70
+ */
71
+ context: TContext | null;
54
72
  /**
55
73
  * Creates a new WebRtcManager instance.
56
74
  * @param factory - Factory object providing WebRTC primitives (peer connection, media, devices).
@@ -88,6 +88,24 @@ export class WebRtcManager {
88
88
  #remoteStream = null;
89
89
  #dataChannels = new Map();
90
90
  #reconnectAttempts = 0;
91
+ /**
92
+ * User-defined context object for storing arbitrary data associated with this manager.
93
+ * Useful for attaching application-specific state (e.g., audio streams, metadata)
94
+ * without modifying the manager internals.
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * // With type parameter for full type safety:
99
+ * const manager = new WebRtcManager<{ audioStream: MediaStream; sessionId: string }>(factory);
100
+ * manager.context = { audioStream: myStream, sessionId: '123' };
101
+ * manager.context.audioStream; // typed as MediaStream
102
+ *
103
+ * // Without type parameter (backwards compatible):
104
+ * const manager = new WebRtcManager(factory);
105
+ * manager.context = { anything: 'goes' };
106
+ * ```
107
+ */
108
+ context = null;
91
109
  #reconnectTimer = null;
92
110
  #fullReconnectTimeoutTimer = null;
93
111
  #deviceChangeHandler = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/webrtc",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "main": "dist/mod.js",
6
6
  "types": "dist/mod.d.ts",