UNPKG

91.9 kBJavaScriptView Raw
1import {pack as $gCcbY$pack, unpack as $gCcbY$unpack} from "peerjs-js-binarypack";
2import $gCcbY$webrtcadapter from "webrtc-adapter";
3import {Encoder as $gCcbY$Encoder, Decoder as $gCcbY$Decoder} from "cbor-x";
4import {Encoder as $gCcbY$Encoder1, decodeMultiStream as $gCcbY$decodeMultiStream} from "@msgpack/msgpack";
5
6
7function $parcel$export(e, n, v, s) {
8 Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
9}
10class $fcbcc7538a6776d5$export$f1c5f4c9cb95390b {
11 constructor(){
12 this.chunkedMTU = 16300 // The original 60000 bytes setting does not work when sending data from Firefox to Chrome, which is "cut off" after 16384 bytes and delivered individually.
13 ;
14 // Binary stuff
15 this._dataCount = 1;
16 this.chunk = (blob)=>{
17 const chunks = [];
18 const size = blob.byteLength;
19 const total = Math.ceil(size / this.chunkedMTU);
20 let index = 0;
21 let start = 0;
22 while(start < size){
23 const end = Math.min(size, start + this.chunkedMTU);
24 const b = blob.slice(start, end);
25 const chunk = {
26 __peerData: this._dataCount,
27 n: index,
28 data: b,
29 total: total
30 };
31 chunks.push(chunk);
32 start = end;
33 index++;
34 }
35 this._dataCount++;
36 return chunks;
37 };
38 }
39}
40function $fcbcc7538a6776d5$export$52c89ebcdc4f53f2(bufs) {
41 let size = 0;
42 for (const buf of bufs)size += buf.byteLength;
43 const result = new Uint8Array(size);
44 let offset = 0;
45 for (const buf of bufs){
46 result.set(buf, offset);
47 offset += buf.byteLength;
48 }
49 return result;
50}
51
52
53
54
55const $fb63e766cfafaab9$var$webRTCAdapter = //@ts-ignore
56(0, $gCcbY$webrtcadapter).default || (0, $gCcbY$webrtcadapter);
57const $fb63e766cfafaab9$export$25be9502477c137d = new class {
58 isWebRTCSupported() {
59 return typeof RTCPeerConnection !== "undefined";
60 }
61 isBrowserSupported() {
62 const browser = this.getBrowser();
63 const version = this.getVersion();
64 const validBrowser = this.supportedBrowsers.includes(browser);
65 if (!validBrowser) return false;
66 if (browser === "chrome") return version >= this.minChromeVersion;
67 if (browser === "firefox") return version >= this.minFirefoxVersion;
68 if (browser === "safari") return !this.isIOS && version >= this.minSafariVersion;
69 return false;
70 }
71 getBrowser() {
72 return $fb63e766cfafaab9$var$webRTCAdapter.browserDetails.browser;
73 }
74 getVersion() {
75 return $fb63e766cfafaab9$var$webRTCAdapter.browserDetails.version || 0;
76 }
77 isUnifiedPlanSupported() {
78 const browser = this.getBrowser();
79 const version = $fb63e766cfafaab9$var$webRTCAdapter.browserDetails.version || 0;
80 if (browser === "chrome" && version < this.minChromeVersion) return false;
81 if (browser === "firefox" && version >= this.minFirefoxVersion) return true;
82 if (!window.RTCRtpTransceiver || !("currentDirection" in RTCRtpTransceiver.prototype)) return false;
83 let tempPc;
84 let supported = false;
85 try {
86 tempPc = new RTCPeerConnection();
87 tempPc.addTransceiver("audio");
88 supported = true;
89 } catch (e) {} finally{
90 if (tempPc) tempPc.close();
91 }
92 return supported;
93 }
94 toString() {
95 return `Supports:
96 browser:${this.getBrowser()}
97 version:${this.getVersion()}
98 isIOS:${this.isIOS}
99 isWebRTCSupported:${this.isWebRTCSupported()}
100 isBrowserSupported:${this.isBrowserSupported()}
101 isUnifiedPlanSupported:${this.isUnifiedPlanSupported()}`;
102 }
103 constructor(){
104 this.isIOS = [
105 "iPad",
106 "iPhone",
107 "iPod"
108 ].includes(navigator.platform);
109 this.supportedBrowsers = [
110 "firefox",
111 "chrome",
112 "safari"
113 ];
114 this.minFirefoxVersion = 59;
115 this.minChromeVersion = 72;
116 this.minSafariVersion = 605;
117 }
118}();
119
120
121const $9a84a32bf0bf36bb$export$f35f128fd59ea256 = (id)=>{
122 // Allow empty ids
123 return !id || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.test(id);
124};
125
126
127const $0e5fd1585784c252$export$4e61f672936bec77 = ()=>Math.random().toString(36).slice(2);
128
129
130const $4f4134156c446392$var$DEFAULT_CONFIG = {
131 iceServers: [
132 {
133 urls: "stun:stun.l.google.com:19302"
134 },
135 {
136 urls: [
137 "turn:eu-0.turn.peerjs.com:3478",
138 "turn:us-0.turn.peerjs.com:3478"
139 ],
140 username: "peerjs",
141 credential: "peerjsp"
142 }
143 ],
144 sdpSemantics: "unified-plan"
145};
146class $4f4134156c446392$export$f8f26dd395d7e1bd extends (0, $fcbcc7538a6776d5$export$f1c5f4c9cb95390b) {
147 noop() {}
148 blobToArrayBuffer(blob, cb) {
149 const fr = new FileReader();
150 fr.onload = function(evt) {
151 if (evt.target) cb(evt.target.result);
152 };
153 fr.readAsArrayBuffer(blob);
154 return fr;
155 }
156 binaryStringToArrayBuffer(binary) {
157 const byteArray = new Uint8Array(binary.length);
158 for(let i = 0; i < binary.length; i++)byteArray[i] = binary.charCodeAt(i) & 0xff;
159 return byteArray.buffer;
160 }
161 isSecure() {
162 return location.protocol === "https:";
163 }
164 constructor(...args){
165 super(...args);
166 this.CLOUD_HOST = "0.peerjs.com";
167 this.CLOUD_PORT = 443;
168 // Browsers that need chunking:
169 this.chunkedBrowsers = {
170 Chrome: 1,
171 chrome: 1
172 };
173 // Returns browser-agnostic default config
174 this.defaultConfig = $4f4134156c446392$var$DEFAULT_CONFIG;
175 this.browser = (0, $fb63e766cfafaab9$export$25be9502477c137d).getBrowser();
176 this.browserVersion = (0, $fb63e766cfafaab9$export$25be9502477c137d).getVersion();
177 this.pack = $gCcbY$pack;
178 this.unpack = $gCcbY$unpack;
179 /**
180 * A hash of WebRTC features mapped to booleans that correspond to whether the feature is supported by the current browser.
181 *
182 * :::caution
183 * Only the properties documented here are guaranteed to be present on `util.supports`
184 * :::
185 */ this.supports = function() {
186 const supported = {
187 browser: (0, $fb63e766cfafaab9$export$25be9502477c137d).isBrowserSupported(),
188 webRTC: (0, $fb63e766cfafaab9$export$25be9502477c137d).isWebRTCSupported(),
189 audioVideo: false,
190 data: false,
191 binaryBlob: false,
192 reliable: false
193 };
194 if (!supported.webRTC) return supported;
195 let pc;
196 try {
197 pc = new RTCPeerConnection($4f4134156c446392$var$DEFAULT_CONFIG);
198 supported.audioVideo = true;
199 let dc;
200 try {
201 dc = pc.createDataChannel("_PEERJSTEST", {
202 ordered: true
203 });
204 supported.data = true;
205 supported.reliable = !!dc.ordered;
206 // Binary test
207 try {
208 dc.binaryType = "blob";
209 supported.binaryBlob = !(0, $fb63e766cfafaab9$export$25be9502477c137d).isIOS;
210 } catch (e) {}
211 } catch (e) {} finally{
212 if (dc) dc.close();
213 }
214 } catch (e) {} finally{
215 if (pc) pc.close();
216 }
217 return supported;
218 }();
219 // Ensure alphanumeric ids
220 this.validateId = (0, $9a84a32bf0bf36bb$export$f35f128fd59ea256);
221 this.randomToken = (0, $0e5fd1585784c252$export$4e61f672936bec77);
222 }
223}
224const $4f4134156c446392$export$7debb50ef11d5e0b = new $4f4134156c446392$export$f8f26dd395d7e1bd();
225
226
227
228const $257947e92926277a$var$LOG_PREFIX = "PeerJS: ";
229var $257947e92926277a$export$243e62d78d3b544d;
230(function(LogLevel) {
231 /**
232 * Prints no logs.
233 */ LogLevel[LogLevel["Disabled"] = 0] = "Disabled";
234 /**
235 * Prints only errors.
236 */ LogLevel[LogLevel["Errors"] = 1] = "Errors";
237 /**
238 * Prints errors and warnings.
239 */ LogLevel[LogLevel["Warnings"] = 2] = "Warnings";
240 /**
241 * Prints all logs.
242 */ LogLevel[LogLevel["All"] = 3] = "All";
243})($257947e92926277a$export$243e62d78d3b544d || ($257947e92926277a$export$243e62d78d3b544d = {}));
244class $257947e92926277a$var$Logger {
245 get logLevel() {
246 return this._logLevel;
247 }
248 set logLevel(logLevel) {
249 this._logLevel = logLevel;
250 }
251 log(...args) {
252 if (this._logLevel >= 3) this._print(3, ...args);
253 }
254 warn(...args) {
255 if (this._logLevel >= 2) this._print(2, ...args);
256 }
257 error(...args) {
258 if (this._logLevel >= 1) this._print(1, ...args);
259 }
260 setLogFunction(fn) {
261 this._print = fn;
262 }
263 _print(logLevel, ...rest) {
264 const copy = [
265 $257947e92926277a$var$LOG_PREFIX,
266 ...rest
267 ];
268 for(const i in copy)if (copy[i] instanceof Error) copy[i] = "(" + copy[i].name + ") " + copy[i].message;
269 if (logLevel >= 3) console.log(...copy);
270 else if (logLevel >= 2) console.warn("WARNING", ...copy);
271 else if (logLevel >= 1) console.error("ERROR", ...copy);
272 }
273 constructor(){
274 this._logLevel = 0;
275 }
276}
277var $257947e92926277a$export$2e2bcd8739ae039 = new $257947e92926277a$var$Logger();
278
279
280var $c4dcfd1d1ea86647$exports = {};
281"use strict";
282var $c4dcfd1d1ea86647$var$has = Object.prototype.hasOwnProperty, $c4dcfd1d1ea86647$var$prefix = "~";
283/**
284 * Constructor to create a storage for our `EE` objects.
285 * An `Events` instance is a plain object whose properties are event names.
286 *
287 * @constructor
288 * @private
289 */ function $c4dcfd1d1ea86647$var$Events() {}
290//
291// We try to not inherit from `Object.prototype`. In some engines creating an
292// instance in this way is faster than calling `Object.create(null)` directly.
293// If `Object.create(null)` is not supported we prefix the event names with a
294// character to make sure that the built-in object properties are not
295// overridden or used as an attack vector.
296//
297if (Object.create) {
298 $c4dcfd1d1ea86647$var$Events.prototype = Object.create(null);
299 //
300 // This hack is needed because the `__proto__` property is still inherited in
301 // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
302 //
303 if (!new $c4dcfd1d1ea86647$var$Events().__proto__) $c4dcfd1d1ea86647$var$prefix = false;
304}
305/**
306 * Representation of a single event listener.
307 *
308 * @param {Function} fn The listener function.
309 * @param {*} context The context to invoke the listener with.
310 * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
311 * @constructor
312 * @private
313 */ function $c4dcfd1d1ea86647$var$EE(fn, context, once) {
314 this.fn = fn;
315 this.context = context;
316 this.once = once || false;
317}
318/**
319 * Add a listener for a given event.
320 *
321 * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
322 * @param {(String|Symbol)} event The event name.
323 * @param {Function} fn The listener function.
324 * @param {*} context The context to invoke the listener with.
325 * @param {Boolean} once Specify if the listener is a one-time listener.
326 * @returns {EventEmitter}
327 * @private
328 */ function $c4dcfd1d1ea86647$var$addListener(emitter, event, fn, context, once) {
329 if (typeof fn !== "function") throw new TypeError("The listener must be a function");
330 var listener = new $c4dcfd1d1ea86647$var$EE(fn, context || emitter, once), evt = $c4dcfd1d1ea86647$var$prefix ? $c4dcfd1d1ea86647$var$prefix + event : event;
331 if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
332 else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
333 else emitter._events[evt] = [
334 emitter._events[evt],
335 listener
336 ];
337 return emitter;
338}
339/**
340 * Clear event by name.
341 *
342 * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
343 * @param {(String|Symbol)} evt The Event name.
344 * @private
345 */ function $c4dcfd1d1ea86647$var$clearEvent(emitter, evt) {
346 if (--emitter._eventsCount === 0) emitter._events = new $c4dcfd1d1ea86647$var$Events();
347 else delete emitter._events[evt];
348}
349/**
350 * Minimal `EventEmitter` interface that is molded against the Node.js
351 * `EventEmitter` interface.
352 *
353 * @constructor
354 * @public
355 */ function $c4dcfd1d1ea86647$var$EventEmitter() {
356 this._events = new $c4dcfd1d1ea86647$var$Events();
357 this._eventsCount = 0;
358}
359/**
360 * Return an array listing the events for which the emitter has registered
361 * listeners.
362 *
363 * @returns {Array}
364 * @public
365 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.eventNames = function eventNames() {
366 var names = [], events, name;
367 if (this._eventsCount === 0) return names;
368 for(name in events = this._events)if ($c4dcfd1d1ea86647$var$has.call(events, name)) names.push($c4dcfd1d1ea86647$var$prefix ? name.slice(1) : name);
369 if (Object.getOwnPropertySymbols) return names.concat(Object.getOwnPropertySymbols(events));
370 return names;
371};
372/**
373 * Return the listeners registered for a given event.
374 *
375 * @param {(String|Symbol)} event The event name.
376 * @returns {Array} The registered listeners.
377 * @public
378 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.listeners = function listeners(event) {
379 var evt = $c4dcfd1d1ea86647$var$prefix ? $c4dcfd1d1ea86647$var$prefix + event : event, handlers = this._events[evt];
380 if (!handlers) return [];
381 if (handlers.fn) return [
382 handlers.fn
383 ];
384 for(var i = 0, l = handlers.length, ee = new Array(l); i < l; i++)ee[i] = handlers[i].fn;
385 return ee;
386};
387/**
388 * Return the number of listeners listening to a given event.
389 *
390 * @param {(String|Symbol)} event The event name.
391 * @returns {Number} The number of listeners.
392 * @public
393 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.listenerCount = function listenerCount(event) {
394 var evt = $c4dcfd1d1ea86647$var$prefix ? $c4dcfd1d1ea86647$var$prefix + event : event, listeners = this._events[evt];
395 if (!listeners) return 0;
396 if (listeners.fn) return 1;
397 return listeners.length;
398};
399/**
400 * Calls each of the listeners registered for a given event.
401 *
402 * @param {(String|Symbol)} event The event name.
403 * @returns {Boolean} `true` if the event had listeners, else `false`.
404 * @public
405 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
406 var evt = $c4dcfd1d1ea86647$var$prefix ? $c4dcfd1d1ea86647$var$prefix + event : event;
407 if (!this._events[evt]) return false;
408 var listeners = this._events[evt], len = arguments.length, args, i;
409 if (listeners.fn) {
410 if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
411 switch(len){
412 case 1:
413 return listeners.fn.call(listeners.context), true;
414 case 2:
415 return listeners.fn.call(listeners.context, a1), true;
416 case 3:
417 return listeners.fn.call(listeners.context, a1, a2), true;
418 case 4:
419 return listeners.fn.call(listeners.context, a1, a2, a3), true;
420 case 5:
421 return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
422 case 6:
423 return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
424 }
425 for(i = 1, args = new Array(len - 1); i < len; i++)args[i - 1] = arguments[i];
426 listeners.fn.apply(listeners.context, args);
427 } else {
428 var length = listeners.length, j;
429 for(i = 0; i < length; i++){
430 if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
431 switch(len){
432 case 1:
433 listeners[i].fn.call(listeners[i].context);
434 break;
435 case 2:
436 listeners[i].fn.call(listeners[i].context, a1);
437 break;
438 case 3:
439 listeners[i].fn.call(listeners[i].context, a1, a2);
440 break;
441 case 4:
442 listeners[i].fn.call(listeners[i].context, a1, a2, a3);
443 break;
444 default:
445 if (!args) for(j = 1, args = new Array(len - 1); j < len; j++)args[j - 1] = arguments[j];
446 listeners[i].fn.apply(listeners[i].context, args);
447 }
448 }
449 }
450 return true;
451};
452/**
453 * Add a listener for a given event.
454 *
455 * @param {(String|Symbol)} event The event name.
456 * @param {Function} fn The listener function.
457 * @param {*} [context=this] The context to invoke the listener with.
458 * @returns {EventEmitter} `this`.
459 * @public
460 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.on = function on(event, fn, context) {
461 return $c4dcfd1d1ea86647$var$addListener(this, event, fn, context, false);
462};
463/**
464 * Add a one-time listener for a given event.
465 *
466 * @param {(String|Symbol)} event The event name.
467 * @param {Function} fn The listener function.
468 * @param {*} [context=this] The context to invoke the listener with.
469 * @returns {EventEmitter} `this`.
470 * @public
471 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.once = function once(event, fn, context) {
472 return $c4dcfd1d1ea86647$var$addListener(this, event, fn, context, true);
473};
474/**
475 * Remove the listeners of a given event.
476 *
477 * @param {(String|Symbol)} event The event name.
478 * @param {Function} fn Only remove the listeners that match this function.
479 * @param {*} context Only remove the listeners that have this context.
480 * @param {Boolean} once Only remove one-time listeners.
481 * @returns {EventEmitter} `this`.
482 * @public
483 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
484 var evt = $c4dcfd1d1ea86647$var$prefix ? $c4dcfd1d1ea86647$var$prefix + event : event;
485 if (!this._events[evt]) return this;
486 if (!fn) {
487 $c4dcfd1d1ea86647$var$clearEvent(this, evt);
488 return this;
489 }
490 var listeners = this._events[evt];
491 if (listeners.fn) {
492 if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) $c4dcfd1d1ea86647$var$clearEvent(this, evt);
493 } else {
494 for(var i = 0, events = [], length = listeners.length; i < length; i++)if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) events.push(listeners[i]);
495 //
496 // Reset the array, or remove it completely if we have no more listeners.
497 //
498 if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
499 else $c4dcfd1d1ea86647$var$clearEvent(this, evt);
500 }
501 return this;
502};
503/**
504 * Remove all listeners, or those of the specified event.
505 *
506 * @param {(String|Symbol)} [event] The event name.
507 * @returns {EventEmitter} `this`.
508 * @public
509 */ $c4dcfd1d1ea86647$var$EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
510 var evt;
511 if (event) {
512 evt = $c4dcfd1d1ea86647$var$prefix ? $c4dcfd1d1ea86647$var$prefix + event : event;
513 if (this._events[evt]) $c4dcfd1d1ea86647$var$clearEvent(this, evt);
514 } else {
515 this._events = new $c4dcfd1d1ea86647$var$Events();
516 this._eventsCount = 0;
517 }
518 return this;
519};
520//
521// Alias methods names because people roll like that.
522//
523$c4dcfd1d1ea86647$var$EventEmitter.prototype.off = $c4dcfd1d1ea86647$var$EventEmitter.prototype.removeListener;
524$c4dcfd1d1ea86647$var$EventEmitter.prototype.addListener = $c4dcfd1d1ea86647$var$EventEmitter.prototype.on;
525//
526// Expose the prefix.
527//
528$c4dcfd1d1ea86647$var$EventEmitter.prefixed = $c4dcfd1d1ea86647$var$prefix;
529//
530// Allow `EventEmitter` to be imported as module namespace.
531//
532$c4dcfd1d1ea86647$var$EventEmitter.EventEmitter = $c4dcfd1d1ea86647$var$EventEmitter;
533$c4dcfd1d1ea86647$exports = $c4dcfd1d1ea86647$var$EventEmitter;
534
535
536
537var $78455e22dea96b8c$exports = {};
538
539$parcel$export($78455e22dea96b8c$exports, "ConnectionType", () => $78455e22dea96b8c$export$3157d57b4135e3bc);
540$parcel$export($78455e22dea96b8c$exports, "PeerErrorType", () => $78455e22dea96b8c$export$9547aaa2e39030ff);
541$parcel$export($78455e22dea96b8c$exports, "BaseConnectionErrorType", () => $78455e22dea96b8c$export$7974935686149686);
542$parcel$export($78455e22dea96b8c$exports, "DataConnectionErrorType", () => $78455e22dea96b8c$export$49ae800c114df41d);
543$parcel$export($78455e22dea96b8c$exports, "SerializationType", () => $78455e22dea96b8c$export$89f507cf986a947);
544$parcel$export($78455e22dea96b8c$exports, "SocketEventType", () => $78455e22dea96b8c$export$3b5c4a4b6354f023);
545$parcel$export($78455e22dea96b8c$exports, "ServerMessageType", () => $78455e22dea96b8c$export$adb4a1754da6f10d);
546var $78455e22dea96b8c$export$3157d57b4135e3bc;
547(function(ConnectionType) {
548 ConnectionType["Data"] = "data";
549 ConnectionType["Media"] = "media";
550})($78455e22dea96b8c$export$3157d57b4135e3bc || ($78455e22dea96b8c$export$3157d57b4135e3bc = {}));
551var $78455e22dea96b8c$export$9547aaa2e39030ff;
552(function(PeerErrorType) {
553 /**
554 * The client's browser does not support some or all WebRTC features that you are trying to use.
555 */ PeerErrorType["BrowserIncompatible"] = "browser-incompatible";
556 /**
557 * You've already disconnected this peer from the server and can no longer make any new connections on it.
558 */ PeerErrorType["Disconnected"] = "disconnected";
559 /**
560 * The ID passed into the Peer constructor contains illegal characters.
561 */ PeerErrorType["InvalidID"] = "invalid-id";
562 /**
563 * The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).
564 */ PeerErrorType["InvalidKey"] = "invalid-key";
565 /**
566 * Lost or cannot establish a connection to the signalling server.
567 */ PeerErrorType["Network"] = "network";
568 /**
569 * The peer you're trying to connect to does not exist.
570 */ PeerErrorType["PeerUnavailable"] = "peer-unavailable";
571 /**
572 * PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.
573 */ PeerErrorType["SslUnavailable"] = "ssl-unavailable";
574 /**
575 * Unable to reach the server.
576 */ PeerErrorType["ServerError"] = "server-error";
577 /**
578 * An error from the underlying socket.
579 */ PeerErrorType["SocketError"] = "socket-error";
580 /**
581 * The underlying socket closed unexpectedly.
582 */ PeerErrorType["SocketClosed"] = "socket-closed";
583 /**
584 * The ID passed into the Peer constructor is already taken.
585 *
586 * :::caution
587 * This error is not fatal if your peer has open peer-to-peer connections.
588 * This can happen if you attempt to {@apilink Peer.reconnect} a peer that has been disconnected from the server,
589 * but its old ID has now been taken.
590 * :::
591 */ PeerErrorType["UnavailableID"] = "unavailable-id";
592 /**
593 * Native WebRTC errors.
594 */ PeerErrorType["WebRTC"] = "webrtc";
595})($78455e22dea96b8c$export$9547aaa2e39030ff || ($78455e22dea96b8c$export$9547aaa2e39030ff = {}));
596var $78455e22dea96b8c$export$7974935686149686;
597(function(BaseConnectionErrorType) {
598 BaseConnectionErrorType["NegotiationFailed"] = "negotiation-failed";
599 BaseConnectionErrorType["ConnectionClosed"] = "connection-closed";
600})($78455e22dea96b8c$export$7974935686149686 || ($78455e22dea96b8c$export$7974935686149686 = {}));
601var $78455e22dea96b8c$export$49ae800c114df41d;
602(function(DataConnectionErrorType) {
603 DataConnectionErrorType["NotOpenYet"] = "not-open-yet";
604 DataConnectionErrorType["MessageToBig"] = "message-too-big";
605})($78455e22dea96b8c$export$49ae800c114df41d || ($78455e22dea96b8c$export$49ae800c114df41d = {}));
606var $78455e22dea96b8c$export$89f507cf986a947;
607(function(SerializationType) {
608 SerializationType["Binary"] = "binary";
609 SerializationType["BinaryUTF8"] = "binary-utf8";
610 SerializationType["JSON"] = "json";
611 SerializationType["None"] = "raw";
612})($78455e22dea96b8c$export$89f507cf986a947 || ($78455e22dea96b8c$export$89f507cf986a947 = {}));
613var $78455e22dea96b8c$export$3b5c4a4b6354f023;
614(function(SocketEventType) {
615 SocketEventType["Message"] = "message";
616 SocketEventType["Disconnected"] = "disconnected";
617 SocketEventType["Error"] = "error";
618 SocketEventType["Close"] = "close";
619})($78455e22dea96b8c$export$3b5c4a4b6354f023 || ($78455e22dea96b8c$export$3b5c4a4b6354f023 = {}));
620var $78455e22dea96b8c$export$adb4a1754da6f10d;
621(function(ServerMessageType) {
622 ServerMessageType["Heartbeat"] = "HEARTBEAT";
623 ServerMessageType["Candidate"] = "CANDIDATE";
624 ServerMessageType["Offer"] = "OFFER";
625 ServerMessageType["Answer"] = "ANSWER";
626 ServerMessageType["Open"] = "OPEN";
627 ServerMessageType["Error"] = "ERROR";
628 ServerMessageType["IdTaken"] = "ID-TAKEN";
629 ServerMessageType["InvalidKey"] = "INVALID-KEY";
630 ServerMessageType["Leave"] = "LEAVE";
631 ServerMessageType["Expire"] = "EXPIRE";
632})($78455e22dea96b8c$export$adb4a1754da6f10d || ($78455e22dea96b8c$export$adb4a1754da6f10d = {}));
633
634
635var $f5f881ec4575f1fc$exports = {};
636$f5f881ec4575f1fc$exports = JSON.parse('{"name":"peerjs","version":"1.5.2","keywords":["peerjs","webrtc","p2p","rtc"],"description":"PeerJS client","homepage":"https://peerjs.com","bugs":{"url":"https://github.com/peers/peerjs/issues"},"repository":{"type":"git","url":"https://github.com/peers/peerjs"},"license":"MIT","contributors":["Michelle Bu <michelle@michellebu.com>","afrokick <devbyru@gmail.com>","ericz <really.ez@gmail.com>","Jairo <kidandcat@gmail.com>","Jonas Gloning <34194370+jonasgloning@users.noreply.github.com>","Jairo Caro-Accino Viciana <jairo@galax.be>","Carlos Caballero <carlos.caballero.gonzalez@gmail.com>","hc <hheennrryy@gmail.com>","Muhammad Asif <capripio@gmail.com>","PrashoonB <prashoonbhattacharjee@gmail.com>","Harsh Bardhan Mishra <47351025+HarshCasper@users.noreply.github.com>","akotynski <aleksanderkotbury@gmail.com>","lmb <i@lmb.io>","Jairooo <jairocaro@msn.com>","Moritz St\xfcckler <moritz.stueckler@gmail.com>","Simon <crydotsnakegithub@gmail.com>","Denis Lukov <denismassters@gmail.com>","Philipp Hancke <fippo@andyet.net>","Hans Oksendahl <hansoksendahl@gmail.com>","Jess <jessachandler@gmail.com>","khankuan <khankuan@gmail.com>","DUODVK <kurmanov.work@gmail.com>","XiZhao <kwang1imsa@gmail.com>","Matthias Lohr <matthias@lohr.me>","=frank tree <=frnktrb@googlemail.com>","Andre Eckardt <aeckardt@outlook.com>","Chris Cowan <agentme49@gmail.com>","Alex Chuev <alex@chuev.com>","alxnull <alxnull@e.mail.de>","Yemel Jardi <angel.jardi@gmail.com>","Ben Parnell <benjaminparnell.94@gmail.com>","Benny Lichtner <bennlich@gmail.com>","fresheneesz <bitetrudpublic@gmail.com>","bob.barstead@exaptive.com <bob.barstead@exaptive.com>","chandika <chandika@gmail.com>","emersion <contact@emersion.fr>","Christopher Van <cvan@users.noreply.github.com>","eddieherm <edhermoso@gmail.com>","Eduardo Pinho <enet4mikeenet@gmail.com>","Evandro Zanatta <ezanatta@tray.net.br>","Gardner Bickford <gardner@users.noreply.github.com>","Gian Luca <gianluca.cecchi@cynny.com>","PatrickJS <github@gdi2290.com>","jonnyf <github@jonathanfoss.co.uk>","Hizkia Felix <hizkifw@gmail.com>","Hristo Oskov <hristo.oskov@gmail.com>","Isaac Madwed <i.madwed@gmail.com>","Ilya Konanykhin <ilya.konanykhin@gmail.com>","jasonbarry <jasbarry@me.com>","Jonathan Burke <jonathan.burke.1311@googlemail.com>","Josh Hamit <josh.hamit@gmail.com>","Jordan Austin <jrax86@gmail.com>","Joel Wetzell <jwetzell@yahoo.com>","xizhao <kevin.wang@cloudera.com>","Alberto Torres <kungfoobar@gmail.com>","Jonathan Mayol <mayoljonathan@gmail.com>","Jefferson Felix <me@jsfelix.dev>","Rolf Erik Lekang <me@rolflekang.com>","Kevin Mai-Husan Chia <mhchia@users.noreply.github.com>","Pepijn de Vos <pepijndevos@gmail.com>","JooYoung <qkdlql@naver.com>","Tobias Speicher <rootcommander@gmail.com>","Steve Blaurock <sblaurock@gmail.com>","Kyrylo Shegeda <shegeda@ualberta.ca>","Diwank Singh Tomer <singh@diwank.name>","So\u0308ren Balko <Soeren.Balko@gmail.com>","Arpit Solanki <solankiarpit1997@gmail.com>","Yuki Ito <yuki@gnnk.net>","Artur Zayats <zag2art@gmail.com>"],"funding":{"type":"opencollective","url":"https://opencollective.com/peer"},"collective":{"type":"opencollective","url":"https://opencollective.com/peer"},"files":["dist/*"],"sideEffects":["lib/global.ts","lib/supports.ts"],"main":"dist/bundler.cjs","module":"dist/bundler.mjs","browser-minified":"dist/peerjs.min.js","browser-unminified":"dist/peerjs.js","browser-minified-cbor":"dist/serializer.cbor.mjs","browser-minified-msgpack":"dist/serializer.msgpack.mjs","types":"dist/types.d.ts","engines":{"node":">= 14"},"targets":{"types":{"source":"lib/exports.ts"},"main":{"source":"lib/exports.ts","sourceMap":{"inlineSources":true}},"module":{"source":"lib/exports.ts","includeNodeModules":["eventemitter3"],"sourceMap":{"inlineSources":true}},"browser-minified":{"context":"browser","outputFormat":"global","optimize":true,"engines":{"browsers":"chrome >= 83, edge >= 83, firefox >= 80, safari >= 15"},"source":"lib/global.ts"},"browser-unminified":{"context":"browser","outputFormat":"global","optimize":false,"engines":{"browsers":"chrome >= 83, edge >= 83, firefox >= 80, safari >= 15"},"source":"lib/global.ts"},"browser-minified-cbor":{"context":"browser","outputFormat":"esmodule","isLibrary":true,"optimize":true,"engines":{"browsers":"chrome >= 83, edge >= 83, firefox >= 102, safari >= 15"},"source":"lib/dataconnection/StreamConnection/Cbor.ts"},"browser-minified-msgpack":{"context":"browser","outputFormat":"esmodule","isLibrary":true,"optimize":true,"engines":{"browsers":"chrome >= 83, edge >= 83, firefox >= 102, safari >= 15"},"source":"lib/dataconnection/StreamConnection/MsgPack.ts"}},"scripts":{"contributors":"git-authors-cli --print=false && prettier --write package.json && git add package.json package-lock.json && git commit -m \\"chore(contributors): update and sort contributors list\\"","check":"tsc --noEmit && tsc -p e2e/tsconfig.json --noEmit","watch":"parcel watch","build":"rm -rf dist && parcel build","prepublishOnly":"npm run build","test":"jest","test:watch":"jest --watch","coverage":"jest --coverage --collectCoverageFrom=\\"./lib/**\\"","format":"prettier --write .","format:check":"prettier --check .","semantic-release":"semantic-release","e2e":"wdio run e2e/wdio.local.conf.ts","e2e:bstack":"wdio run e2e/wdio.bstack.conf.ts"},"devDependencies":{"@parcel/config-default":"^2.9.3","@parcel/packager-ts":"^2.9.3","@parcel/transformer-typescript-tsc":"^2.9.3","@parcel/transformer-typescript-types":"^2.9.3","@semantic-release/changelog":"^6.0.1","@semantic-release/git":"^10.0.1","@swc/core":"^1.3.27","@swc/jest":"^0.2.24","@types/jasmine":"^4.3.4","@wdio/browserstack-service":"^8.11.2","@wdio/cli":"^8.11.2","@wdio/globals":"^8.11.2","@wdio/jasmine-framework":"^8.11.2","@wdio/local-runner":"^8.11.2","@wdio/spec-reporter":"^8.11.2","@wdio/types":"^8.10.4","http-server":"^14.1.1","jest":"^29.3.1","jest-environment-jsdom":"^29.3.1","mock-socket":"^9.0.0","parcel":"^2.9.3","prettier":"^3.0.0","semantic-release":"^21.0.0","ts-node":"^10.9.1","typescript":"^5.0.0","wdio-geckodriver-service":"^5.0.1"},"dependencies":{"@msgpack/msgpack":"^2.8.0","cbor-x":"1.5.4","eventemitter3":"^4.0.7","peerjs-js-binarypack":"^2.1.0","webrtc-adapter":"^8.0.0"},"alias":{"process":false,"buffer":false}}');
637
638
639class $8f5bfa60836d261d$export$4798917dbf149b79 extends (0, $c4dcfd1d1ea86647$exports.EventEmitter) {
640 constructor(secure, host, port, path, key, pingInterval = 5000){
641 super();
642 this.pingInterval = pingInterval;
643 this._disconnected = true;
644 this._messagesQueue = [];
645 const wsProtocol = secure ? "wss://" : "ws://";
646 this._baseUrl = wsProtocol + host + ":" + port + path + "peerjs?key=" + key;
647 }
648 start(id, token) {
649 this._id = id;
650 const wsUrl = `${this._baseUrl}&id=${id}&token=${token}`;
651 if (!!this._socket || !this._disconnected) return;
652 this._socket = new WebSocket(wsUrl + "&version=" + (0, $f5f881ec4575f1fc$exports.version));
653 this._disconnected = false;
654 this._socket.onmessage = (event)=>{
655 let data;
656 try {
657 data = JSON.parse(event.data);
658 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Server message received:", data);
659 } catch (e) {
660 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Invalid server message", event.data);
661 return;
662 }
663 this.emit((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Message, data);
664 };
665 this._socket.onclose = (event)=>{
666 if (this._disconnected) return;
667 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Socket closed.", event);
668 this._cleanup();
669 this._disconnected = true;
670 this.emit((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Disconnected);
671 };
672 // Take care of the queue of connections if necessary and make sure Peer knows
673 // socket is open.
674 this._socket.onopen = ()=>{
675 if (this._disconnected) return;
676 this._sendQueuedMessages();
677 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Socket open");
678 this._scheduleHeartbeat();
679 };
680 }
681 _scheduleHeartbeat() {
682 this._wsPingTimer = setTimeout(()=>{
683 this._sendHeartbeat();
684 }, this.pingInterval);
685 }
686 _sendHeartbeat() {
687 if (!this._wsOpen()) {
688 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Cannot send heartbeat, because socket closed`);
689 return;
690 }
691 const message = JSON.stringify({
692 type: (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Heartbeat
693 });
694 this._socket.send(message);
695 this._scheduleHeartbeat();
696 }
697 /** Is the websocket currently open? */ _wsOpen() {
698 return !!this._socket && this._socket.readyState === 1;
699 }
700 /** Send queued messages. */ _sendQueuedMessages() {
701 //Create copy of queue and clear it,
702 //because send method push the message back to queue if smth will go wrong
703 const copiedQueue = [
704 ...this._messagesQueue
705 ];
706 this._messagesQueue = [];
707 for (const message of copiedQueue)this.send(message);
708 }
709 /** Exposed send for DC & Peer. */ send(data) {
710 if (this._disconnected) return;
711 // If we didn't get an ID yet, we can't yet send anything so we should queue
712 // up these messages.
713 if (!this._id) {
714 this._messagesQueue.push(data);
715 return;
716 }
717 if (!data.type) {
718 this.emit((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Error, "Invalid message");
719 return;
720 }
721 if (!this._wsOpen()) return;
722 const message = JSON.stringify(data);
723 this._socket.send(message);
724 }
725 close() {
726 if (this._disconnected) return;
727 this._cleanup();
728 this._disconnected = true;
729 }
730 _cleanup() {
731 if (this._socket) {
732 this._socket.onopen = this._socket.onmessage = this._socket.onclose = null;
733 this._socket.close();
734 this._socket = undefined;
735 }
736 clearTimeout(this._wsPingTimer);
737 }
738}
739
740
741
742
743
744
745class $b82fb8fc0514bfc1$export$89e6bb5ad64bf4a {
746 constructor(connection){
747 this.connection = connection;
748 }
749 /** Returns a PeerConnection object set up correctly (for data, media). */ startConnection(options) {
750 const peerConnection = this._startPeerConnection();
751 // Set the connection's PC.
752 this.connection.peerConnection = peerConnection;
753 if (this.connection.type === (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Media && options._stream) this._addTracksToConnection(options._stream, peerConnection);
754 // What do we need to do now?
755 if (options.originator) {
756 const dataConnection = this.connection;
757 const config = {
758 ordered: !!options.reliable
759 };
760 const dataChannel = peerConnection.createDataChannel(dataConnection.label, config);
761 dataConnection._initializeDataChannel(dataChannel);
762 this._makeOffer();
763 } else this.handleSDP("OFFER", options.sdp);
764 }
765 /** Start a PC. */ _startPeerConnection() {
766 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Creating RTCPeerConnection.");
767 const peerConnection = new RTCPeerConnection(this.connection.provider.options.config);
768 this._setupListeners(peerConnection);
769 return peerConnection;
770 }
771 /** Set up various WebRTC listeners. */ _setupListeners(peerConnection) {
772 const peerId = this.connection.peer;
773 const connectionId = this.connection.connectionId;
774 const connectionType = this.connection.type;
775 const provider = this.connection.provider;
776 // ICE CANDIDATES.
777 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Listening for ICE candidates.");
778 peerConnection.onicecandidate = (evt)=>{
779 if (!evt.candidate || !evt.candidate.candidate) return;
780 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Received ICE candidates for ${peerId}:`, evt.candidate);
781 provider.socket.send({
782 type: (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Candidate,
783 payload: {
784 candidate: evt.candidate,
785 type: connectionType,
786 connectionId: connectionId
787 },
788 dst: peerId
789 });
790 };
791 peerConnection.oniceconnectionstatechange = ()=>{
792 switch(peerConnection.iceConnectionState){
793 case "failed":
794 (0, $257947e92926277a$export$2e2bcd8739ae039).log("iceConnectionState is failed, closing connections to " + peerId);
795 this.connection.emitError((0, $78455e22dea96b8c$export$7974935686149686).NegotiationFailed, "Negotiation of connection to " + peerId + " failed.");
796 this.connection.close();
797 break;
798 case "closed":
799 (0, $257947e92926277a$export$2e2bcd8739ae039).log("iceConnectionState is closed, closing connections to " + peerId);
800 this.connection.emitError((0, $78455e22dea96b8c$export$7974935686149686).ConnectionClosed, "Connection to " + peerId + " closed.");
801 this.connection.close();
802 break;
803 case "disconnected":
804 (0, $257947e92926277a$export$2e2bcd8739ae039).log("iceConnectionState changed to disconnected on the connection with " + peerId);
805 break;
806 case "completed":
807 peerConnection.onicecandidate = ()=>{};
808 break;
809 }
810 this.connection.emit("iceStateChanged", peerConnection.iceConnectionState);
811 };
812 // DATACONNECTION.
813 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Listening for data channel");
814 // Fired between offer and answer, so options should already be saved
815 // in the options hash.
816 peerConnection.ondatachannel = (evt)=>{
817 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Received data channel");
818 const dataChannel = evt.channel;
819 const connection = provider.getConnection(peerId, connectionId);
820 connection._initializeDataChannel(dataChannel);
821 };
822 // MEDIACONNECTION.
823 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Listening for remote stream");
824 peerConnection.ontrack = (evt)=>{
825 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Received remote stream");
826 const stream = evt.streams[0];
827 const connection = provider.getConnection(peerId, connectionId);
828 if (connection.type === (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Media) {
829 const mediaConnection = connection;
830 this._addStreamToMediaConnection(stream, mediaConnection);
831 }
832 };
833 }
834 cleanup() {
835 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Cleaning up PeerConnection to " + this.connection.peer);
836 const peerConnection = this.connection.peerConnection;
837 if (!peerConnection) return;
838 this.connection.peerConnection = null;
839 //unsubscribe from all PeerConnection's events
840 peerConnection.onicecandidate = peerConnection.oniceconnectionstatechange = peerConnection.ondatachannel = peerConnection.ontrack = ()=>{};
841 const peerConnectionNotClosed = peerConnection.signalingState !== "closed";
842 let dataChannelNotClosed = false;
843 const dataChannel = this.connection.dataChannel;
844 if (dataChannel) dataChannelNotClosed = !!dataChannel.readyState && dataChannel.readyState !== "closed";
845 if (peerConnectionNotClosed || dataChannelNotClosed) peerConnection.close();
846 }
847 async _makeOffer() {
848 const peerConnection = this.connection.peerConnection;
849 const provider = this.connection.provider;
850 try {
851 const offer = await peerConnection.createOffer(this.connection.options.constraints);
852 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Created offer.");
853 if (this.connection.options.sdpTransform && typeof this.connection.options.sdpTransform === "function") offer.sdp = this.connection.options.sdpTransform(offer.sdp) || offer.sdp;
854 try {
855 await peerConnection.setLocalDescription(offer);
856 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Set localDescription:", offer, `for:${this.connection.peer}`);
857 let payload = {
858 sdp: offer,
859 type: this.connection.type,
860 connectionId: this.connection.connectionId,
861 metadata: this.connection.metadata
862 };
863 if (this.connection.type === (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Data) {
864 const dataConnection = this.connection;
865 payload = {
866 ...payload,
867 label: dataConnection.label,
868 reliable: dataConnection.reliable,
869 serialization: dataConnection.serialization
870 };
871 }
872 provider.socket.send({
873 type: (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Offer,
874 payload: payload,
875 dst: this.connection.peer
876 });
877 } catch (err) {
878 // TODO: investigate why _makeOffer is being called from the answer
879 if (err != "OperationError: Failed to set local offer sdp: Called in wrong state: kHaveRemoteOffer") {
880 provider.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).WebRTC, err);
881 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Failed to setLocalDescription, ", err);
882 }
883 }
884 } catch (err_1) {
885 provider.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).WebRTC, err_1);
886 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Failed to createOffer, ", err_1);
887 }
888 }
889 async _makeAnswer() {
890 const peerConnection = this.connection.peerConnection;
891 const provider = this.connection.provider;
892 try {
893 const answer = await peerConnection.createAnswer();
894 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Created answer.");
895 if (this.connection.options.sdpTransform && typeof this.connection.options.sdpTransform === "function") answer.sdp = this.connection.options.sdpTransform(answer.sdp) || answer.sdp;
896 try {
897 await peerConnection.setLocalDescription(answer);
898 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Set localDescription:`, answer, `for:${this.connection.peer}`);
899 provider.socket.send({
900 type: (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Answer,
901 payload: {
902 sdp: answer,
903 type: this.connection.type,
904 connectionId: this.connection.connectionId
905 },
906 dst: this.connection.peer
907 });
908 } catch (err) {
909 provider.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).WebRTC, err);
910 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Failed to setLocalDescription, ", err);
911 }
912 } catch (err_1) {
913 provider.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).WebRTC, err_1);
914 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Failed to create answer, ", err_1);
915 }
916 }
917 /** Handle an SDP. */ async handleSDP(type, sdp) {
918 sdp = new RTCSessionDescription(sdp);
919 const peerConnection = this.connection.peerConnection;
920 const provider = this.connection.provider;
921 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Setting remote description", sdp);
922 const self = this;
923 try {
924 await peerConnection.setRemoteDescription(sdp);
925 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Set remoteDescription:${type} for:${this.connection.peer}`);
926 if (type === "OFFER") await self._makeAnswer();
927 } catch (err) {
928 provider.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).WebRTC, err);
929 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Failed to setRemoteDescription, ", err);
930 }
931 }
932 /** Handle a candidate. */ async handleCandidate(ice) {
933 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`handleCandidate:`, ice);
934 try {
935 await this.connection.peerConnection.addIceCandidate(ice);
936 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Added ICE candidate for:${this.connection.peer}`);
937 } catch (err) {
938 this.connection.provider.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).WebRTC, err);
939 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Failed to handleCandidate, ", err);
940 }
941 }
942 _addTracksToConnection(stream, peerConnection) {
943 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`add tracks from stream ${stream.id} to peer connection`);
944 if (!peerConnection.addTrack) return (0, $257947e92926277a$export$2e2bcd8739ae039).error(`Your browser does't support RTCPeerConnection#addTrack. Ignored.`);
945 stream.getTracks().forEach((track)=>{
946 peerConnection.addTrack(track, stream);
947 });
948 }
949 _addStreamToMediaConnection(stream, mediaConnection) {
950 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`add stream ${stream.id} to media connection ${mediaConnection.connectionId}`);
951 mediaConnection.addStream(stream);
952 }
953}
954
955
956
957
958
959class $23779d1881157a18$export$6a678e589c8a4542 extends (0, $c4dcfd1d1ea86647$exports.EventEmitter) {
960 /**
961 * Emits a typed error message.
962 *
963 * @internal
964 */ emitError(type, err) {
965 (0, $257947e92926277a$export$2e2bcd8739ae039).error("Error:", err);
966 // @ts-ignore
967 this.emit("error", new $23779d1881157a18$export$98871882f492de82(`${type}`, err));
968 }
969}
970class $23779d1881157a18$export$98871882f492de82 extends Error {
971 /**
972 * @internal
973 */ constructor(type, err){
974 if (typeof err === "string") super(err);
975 else {
976 super();
977 Object.assign(this, err);
978 }
979 this.type = type;
980 }
981}
982
983
984class $5045192fc6d387ba$export$23a2a68283c24d80 extends (0, $23779d1881157a18$export$6a678e589c8a4542) {
985 /**
986 * Whether the media connection is active (e.g. your call has been answered).
987 * You can check this if you want to set a maximum wait time for a one-sided call.
988 */ get open() {
989 return this._open;
990 }
991 constructor(/**
992 * The ID of the peer on the other end of this connection.
993 */ peer, provider, options){
994 super();
995 this.peer = peer;
996 this.provider = provider;
997 this.options = options;
998 this._open = false;
999 this.metadata = options.metadata;
1000 }
1001}
1002
1003
1004class $5c1d08c7c57da9a3$export$4a84e95a2324ac29 extends (0, $5045192fc6d387ba$export$23a2a68283c24d80) {
1005 static #_ = this.ID_PREFIX = "mc_";
1006 /**
1007 * For media connections, this is always 'media'.
1008 */ get type() {
1009 return (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Media;
1010 }
1011 get localStream() {
1012 return this._localStream;
1013 }
1014 get remoteStream() {
1015 return this._remoteStream;
1016 }
1017 constructor(peerId, provider, options){
1018 super(peerId, provider, options);
1019 this._localStream = this.options._stream;
1020 this.connectionId = this.options.connectionId || $5c1d08c7c57da9a3$export$4a84e95a2324ac29.ID_PREFIX + (0, $4f4134156c446392$export$7debb50ef11d5e0b).randomToken();
1021 this._negotiator = new (0, $b82fb8fc0514bfc1$export$89e6bb5ad64bf4a)(this);
1022 if (this._localStream) this._negotiator.startConnection({
1023 _stream: this._localStream,
1024 originator: true
1025 });
1026 }
1027 /** Called by the Negotiator when the DataChannel is ready. */ _initializeDataChannel(dc) {
1028 this.dataChannel = dc;
1029 this.dataChannel.onopen = ()=>{
1030 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`DC#${this.connectionId} dc connection success`);
1031 this.emit("willCloseOnRemote");
1032 };
1033 this.dataChannel.onclose = ()=>{
1034 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`DC#${this.connectionId} dc closed for:`, this.peer);
1035 this.close();
1036 };
1037 }
1038 addStream(remoteStream) {
1039 (0, $257947e92926277a$export$2e2bcd8739ae039).log("Receiving stream", remoteStream);
1040 this._remoteStream = remoteStream;
1041 super.emit("stream", remoteStream); // Should we call this `open`?
1042 }
1043 /**
1044 * @internal
1045 */ handleMessage(message) {
1046 const type = message.type;
1047 const payload = message.payload;
1048 switch(message.type){
1049 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Answer:
1050 // Forward to negotiator
1051 this._negotiator.handleSDP(type, payload.sdp);
1052 this._open = true;
1053 break;
1054 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Candidate:
1055 this._negotiator.handleCandidate(payload.candidate);
1056 break;
1057 default:
1058 (0, $257947e92926277a$export$2e2bcd8739ae039).warn(`Unrecognized message type:${type} from peer:${this.peer}`);
1059 break;
1060 }
1061 }
1062 /**
1063 * When receiving a {@apilink PeerEvents | `call`} event on a peer, you can call
1064 * `answer` on the media connection provided by the callback to accept the call
1065 * and optionally send your own media stream.
1066
1067 *
1068 * @param stream A WebRTC media stream.
1069 * @param options
1070 * @returns
1071 */ answer(stream, options = {}) {
1072 if (this._localStream) {
1073 (0, $257947e92926277a$export$2e2bcd8739ae039).warn("Local stream already exists on this MediaConnection. Are you answering a call twice?");
1074 return;
1075 }
1076 this._localStream = stream;
1077 if (options && options.sdpTransform) this.options.sdpTransform = options.sdpTransform;
1078 this._negotiator.startConnection({
1079 ...this.options._payload,
1080 _stream: stream
1081 });
1082 // Retrieve lost messages stored because PeerConnection not set up.
1083 const messages = this.provider._getMessages(this.connectionId);
1084 for (const message of messages)this.handleMessage(message);
1085 this._open = true;
1086 }
1087 /**
1088 * Exposed functionality for users.
1089 */ /**
1090 * Closes the media connection.
1091 */ close() {
1092 if (this._negotiator) {
1093 this._negotiator.cleanup();
1094 this._negotiator = null;
1095 }
1096 this._localStream = null;
1097 this._remoteStream = null;
1098 if (this.provider) {
1099 this.provider._removeConnection(this);
1100 this.provider = null;
1101 }
1102 if (this.options && this.options._stream) this.options._stream = null;
1103 if (!this.open) return;
1104 this._open = false;
1105 super.emit("close");
1106 }
1107}
1108
1109
1110
1111
1112
1113
1114class $abf266641927cd89$export$2c4e825dc9120f87 {
1115 constructor(_options){
1116 this._options = _options;
1117 }
1118 _buildRequest(method) {
1119 const protocol = this._options.secure ? "https" : "http";
1120 const { host: host, port: port, path: path, key: key } = this._options;
1121 const url = new URL(`${protocol}://${host}:${port}${path}${key}/${method}`);
1122 // TODO: Why timestamp, why random?
1123 url.searchParams.set("ts", `${Date.now()}${Math.random()}`);
1124 url.searchParams.set("version", (0, $f5f881ec4575f1fc$exports.version));
1125 return fetch(url.href, {
1126 referrerPolicy: this._options.referrerPolicy
1127 });
1128 }
1129 /** Get a unique ID from the server via XHR and initialize with it. */ async retrieveId() {
1130 try {
1131 const response = await this._buildRequest("id");
1132 if (response.status !== 200) throw new Error(`Error. Status:${response.status}`);
1133 return response.text();
1134 } catch (error) {
1135 (0, $257947e92926277a$export$2e2bcd8739ae039).error("Error retrieving ID", error);
1136 let pathError = "";
1137 if (this._options.path === "/" && this._options.host !== (0, $4f4134156c446392$export$7debb50ef11d5e0b).CLOUD_HOST) pathError = " If you passed in a `path` to your self-hosted PeerServer, you'll also need to pass in that same path when creating a new Peer.";
1138 throw new Error("Could not get an ID from the server." + pathError);
1139 }
1140 }
1141 /** @deprecated */ async listAllPeers() {
1142 try {
1143 const response = await this._buildRequest("peers");
1144 if (response.status !== 200) {
1145 if (response.status === 401) {
1146 let helpfulError = "";
1147 if (this._options.host === (0, $4f4134156c446392$export$7debb50ef11d5e0b).CLOUD_HOST) helpfulError = "It looks like you're using the cloud server. You can email team@peerjs.com to enable peer listing for your API key.";
1148 else helpfulError = "You need to enable `allow_discovery` on your self-hosted PeerServer to use this feature.";
1149 throw new Error("It doesn't look like you have permission to list peers IDs. " + helpfulError);
1150 }
1151 throw new Error(`Error. Status:${response.status}`);
1152 }
1153 return response.json();
1154 } catch (error) {
1155 (0, $257947e92926277a$export$2e2bcd8739ae039).error("Error retrieving list peers", error);
1156 throw new Error("Could not get list peers from the server." + error);
1157 }
1158 }
1159}
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170class $6366c4ca161bc297$export$d365f7ad9d7df9c9 extends (0, $5045192fc6d387ba$export$23a2a68283c24d80) {
1171 static #_ = this.ID_PREFIX = "dc_";
1172 static #_1 = this.MAX_BUFFERED_AMOUNT = 8388608;
1173 get type() {
1174 return (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Data;
1175 }
1176 constructor(peerId, provider, options){
1177 super(peerId, provider, options);
1178 this.connectionId = this.options.connectionId || $6366c4ca161bc297$export$d365f7ad9d7df9c9.ID_PREFIX + (0, $0e5fd1585784c252$export$4e61f672936bec77)();
1179 this.label = this.options.label || this.connectionId;
1180 this.reliable = !!this.options.reliable;
1181 this._negotiator = new (0, $b82fb8fc0514bfc1$export$89e6bb5ad64bf4a)(this);
1182 this._negotiator.startConnection(this.options._payload || {
1183 originator: true,
1184 reliable: this.reliable
1185 });
1186 }
1187 /** Called by the Negotiator when the DataChannel is ready. */ _initializeDataChannel(dc) {
1188 this.dataChannel = dc;
1189 this.dataChannel.onopen = ()=>{
1190 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`DC#${this.connectionId} dc connection success`);
1191 this._open = true;
1192 this.emit("open");
1193 };
1194 this.dataChannel.onmessage = (e)=>{
1195 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`DC#${this.connectionId} dc onmessage:`, e.data);
1196 // this._handleDataMessage(e);
1197 };
1198 this.dataChannel.onclose = ()=>{
1199 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`DC#${this.connectionId} dc closed for:`, this.peer);
1200 this.close();
1201 };
1202 }
1203 /**
1204 * Exposed functionality for users.
1205 */ /** Allows user to close connection. */ close(options) {
1206 if (options?.flush) {
1207 this.send({
1208 __peerData: {
1209 type: "close"
1210 }
1211 });
1212 return;
1213 }
1214 if (this._negotiator) {
1215 this._negotiator.cleanup();
1216 this._negotiator = null;
1217 }
1218 if (this.provider) {
1219 this.provider._removeConnection(this);
1220 this.provider = null;
1221 }
1222 if (this.dataChannel) {
1223 this.dataChannel.onopen = null;
1224 this.dataChannel.onmessage = null;
1225 this.dataChannel.onclose = null;
1226 this.dataChannel = null;
1227 }
1228 if (!this.open) return;
1229 this._open = false;
1230 super.emit("close");
1231 }
1232 /** Allows user to send data. */ send(data, chunked = false) {
1233 if (!this.open) {
1234 this.emitError((0, $78455e22dea96b8c$export$49ae800c114df41d).NotOpenYet, "Connection is not open. You should listen for the `open` event before sending messages.");
1235 return;
1236 }
1237 return this._send(data, chunked);
1238 }
1239 async handleMessage(message) {
1240 const payload = message.payload;
1241 switch(message.type){
1242 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Answer:
1243 await this._negotiator.handleSDP(message.type, payload.sdp);
1244 break;
1245 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Candidate:
1246 await this._negotiator.handleCandidate(payload.candidate);
1247 break;
1248 default:
1249 (0, $257947e92926277a$export$2e2bcd8739ae039).warn("Unrecognized message type:", message.type, "from peer:", this.peer);
1250 break;
1251 }
1252 }
1253}
1254
1255
1256class $a229bedbcaa6ca23$export$ff7c9d4c11d94e8b extends (0, $6366c4ca161bc297$export$d365f7ad9d7df9c9) {
1257 get bufferSize() {
1258 return this._bufferSize;
1259 }
1260 _initializeDataChannel(dc) {
1261 super._initializeDataChannel(dc);
1262 this.dataChannel.binaryType = "arraybuffer";
1263 this.dataChannel.addEventListener("message", (e)=>this._handleDataMessage(e));
1264 }
1265 _bufferedSend(msg) {
1266 if (this._buffering || !this._trySend(msg)) {
1267 this._buffer.push(msg);
1268 this._bufferSize = this._buffer.length;
1269 }
1270 }
1271 // Returns true if the send succeeds.
1272 _trySend(msg) {
1273 if (!this.open) return false;
1274 if (this.dataChannel.bufferedAmount > (0, $6366c4ca161bc297$export$d365f7ad9d7df9c9).MAX_BUFFERED_AMOUNT) {
1275 this._buffering = true;
1276 setTimeout(()=>{
1277 this._buffering = false;
1278 this._tryBuffer();
1279 }, 50);
1280 return false;
1281 }
1282 try {
1283 this.dataChannel.send(msg);
1284 } catch (e) {
1285 (0, $257947e92926277a$export$2e2bcd8739ae039).error(`DC#:${this.connectionId} Error when sending:`, e);
1286 this._buffering = true;
1287 this.close();
1288 return false;
1289 }
1290 return true;
1291 }
1292 // Try to send the first message in the buffer.
1293 _tryBuffer() {
1294 if (!this.open) return;
1295 if (this._buffer.length === 0) return;
1296 const msg = this._buffer[0];
1297 if (this._trySend(msg)) {
1298 this._buffer.shift();
1299 this._bufferSize = this._buffer.length;
1300 this._tryBuffer();
1301 }
1302 }
1303 close(options) {
1304 if (options?.flush) {
1305 this.send({
1306 __peerData: {
1307 type: "close"
1308 }
1309 });
1310 return;
1311 }
1312 this._buffer = [];
1313 this._bufferSize = 0;
1314 super.close();
1315 }
1316 constructor(...args){
1317 super(...args);
1318 this._buffer = [];
1319 this._bufferSize = 0;
1320 this._buffering = false;
1321 }
1322}
1323
1324
1325
1326
1327class $9fcfddb3ae148f88$export$f0a5a64d5bb37108 extends (0, $a229bedbcaa6ca23$export$ff7c9d4c11d94e8b) {
1328 close(options) {
1329 super.close(options);
1330 this._chunkedData = {};
1331 }
1332 constructor(peerId, provider, options){
1333 super(peerId, provider, options);
1334 this.chunker = new (0, $fcbcc7538a6776d5$export$f1c5f4c9cb95390b)();
1335 this.serialization = (0, $78455e22dea96b8c$export$89f507cf986a947).Binary;
1336 this._chunkedData = {};
1337 }
1338 // Handles a DataChannel message.
1339 _handleDataMessage({ data: data }) {
1340 const deserializedData = (0, $gCcbY$unpack)(data);
1341 // PeerJS specific message
1342 const peerData = deserializedData["__peerData"];
1343 if (peerData) {
1344 if (peerData.type === "close") {
1345 this.close();
1346 return;
1347 }
1348 // Chunked data -- piece things back together.
1349 // @ts-ignore
1350 this._handleChunk(deserializedData);
1351 return;
1352 }
1353 this.emit("data", deserializedData);
1354 }
1355 _handleChunk(data) {
1356 const id = data.__peerData;
1357 const chunkInfo = this._chunkedData[id] || {
1358 data: [],
1359 count: 0,
1360 total: data.total
1361 };
1362 chunkInfo.data[data.n] = new Uint8Array(data.data);
1363 chunkInfo.count++;
1364 this._chunkedData[id] = chunkInfo;
1365 if (chunkInfo.total === chunkInfo.count) {
1366 // Clean up before making the recursive call to `_handleDataMessage`.
1367 delete this._chunkedData[id];
1368 // We've received all the chunks--time to construct the complete data.
1369 // const data = new Blob(chunkInfo.data);
1370 const data = (0, $fcbcc7538a6776d5$export$52c89ebcdc4f53f2)(chunkInfo.data);
1371 this._handleDataMessage({
1372 data: data
1373 });
1374 }
1375 }
1376 _send(data, chunked) {
1377 const blob = (0, $gCcbY$pack)(data);
1378 if (blob instanceof Promise) return this._send_blob(blob);
1379 if (!chunked && blob.byteLength > this.chunker.chunkedMTU) {
1380 this._sendChunks(blob);
1381 return;
1382 }
1383 this._bufferedSend(blob);
1384 }
1385 async _send_blob(blobPromise) {
1386 const blob = await blobPromise;
1387 if (blob.byteLength > this.chunker.chunkedMTU) {
1388 this._sendChunks(blob);
1389 return;
1390 }
1391 this._bufferedSend(blob);
1392 }
1393 _sendChunks(blob) {
1394 const blobs = this.chunker.chunk(blob);
1395 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`DC#${this.connectionId} Try to send ${blobs.length} chunks...`);
1396 for (const blob of blobs)this.send(blob, true);
1397 }
1398}
1399
1400
1401
1402
1403class $bbaee3f15f714663$export$6f88fe47d32c9c94 extends (0, $a229bedbcaa6ca23$export$ff7c9d4c11d94e8b) {
1404 _handleDataMessage({ data: data }) {
1405 super.emit("data", data);
1406 }
1407 _send(data, _chunked) {
1408 this._bufferedSend(data);
1409 }
1410 constructor(...args){
1411 super(...args);
1412 this.serialization = (0, $78455e22dea96b8c$export$89f507cf986a947).None;
1413 }
1414}
1415
1416
1417
1418
1419
1420class $817f931e3f9096cf$export$48880ac635f47186 extends (0, $a229bedbcaa6ca23$export$ff7c9d4c11d94e8b) {
1421 // Handles a DataChannel message.
1422 _handleDataMessage({ data: data }) {
1423 const deserializedData = this.parse(this.decoder.decode(data));
1424 // PeerJS specific message
1425 const peerData = deserializedData["__peerData"];
1426 if (peerData && peerData.type === "close") {
1427 this.close();
1428 return;
1429 }
1430 this.emit("data", deserializedData);
1431 }
1432 _send(data, _chunked) {
1433 const encodedData = this.encoder.encode(this.stringify(data));
1434 if (encodedData.byteLength >= (0, $4f4134156c446392$export$7debb50ef11d5e0b).chunkedMTU) {
1435 this.emitError((0, $78455e22dea96b8c$export$49ae800c114df41d).MessageToBig, "Message too big for JSON channel");
1436 return;
1437 }
1438 this._bufferedSend(encodedData);
1439 }
1440 constructor(...args){
1441 super(...args);
1442 this.serialization = (0, $78455e22dea96b8c$export$89f507cf986a947).JSON;
1443 this.encoder = new TextEncoder();
1444 this.decoder = new TextDecoder();
1445 this.stringify = JSON.stringify;
1446 this.parse = JSON.parse;
1447 }
1448}
1449
1450
1451
1452class $416260bce337df90$var$PeerOptions {
1453}
1454class $416260bce337df90$export$ecd1fc136c422448 extends (0, $23779d1881157a18$export$6a678e589c8a4542) {
1455 static #_ = this.DEFAULT_KEY = "peerjs";
1456 /**
1457 * The brokering ID of this peer
1458 *
1459 * If no ID was specified in {@apilink Peer | the constructor},
1460 * this will be `undefined` until the {@apilink PeerEvents | `open`} event is emitted.
1461 */ get id() {
1462 return this._id;
1463 }
1464 get options() {
1465 return this._options;
1466 }
1467 get open() {
1468 return this._open;
1469 }
1470 /**
1471 * @internal
1472 */ get socket() {
1473 return this._socket;
1474 }
1475 /**
1476 * A hash of all connections associated with this peer, keyed by the remote peer's ID.
1477 * @deprecated
1478 * Return type will change from Object to Map<string,[]>
1479 */ get connections() {
1480 const plainConnections = Object.create(null);
1481 for (const [k, v] of this._connections)plainConnections[k] = v;
1482 return plainConnections;
1483 }
1484 /**
1485 * true if this peer and all of its connections can no longer be used.
1486 */ get destroyed() {
1487 return this._destroyed;
1488 }
1489 /**
1490 * false if there is an active connection to the PeerServer.
1491 */ get disconnected() {
1492 return this._disconnected;
1493 }
1494 constructor(id, options){
1495 super();
1496 this._serializers = {
1497 raw: (0, $bbaee3f15f714663$export$6f88fe47d32c9c94),
1498 json: (0, $817f931e3f9096cf$export$48880ac635f47186),
1499 binary: (0, $9fcfddb3ae148f88$export$f0a5a64d5bb37108),
1500 "binary-utf8": (0, $9fcfddb3ae148f88$export$f0a5a64d5bb37108),
1501 default: (0, $9fcfddb3ae148f88$export$f0a5a64d5bb37108)
1502 };
1503 this._id = null;
1504 this._lastServerId = null;
1505 // States.
1506 this._destroyed = false // Connections have been killed
1507 ;
1508 this._disconnected = false // Connection to PeerServer killed but P2P connections still active
1509 ;
1510 this._open = false // Sockets and such are not yet open.
1511 ;
1512 this._connections = new Map() // All connections for this peer.
1513 ;
1514 this._lostMessages = new Map() // src => [list of messages]
1515 ;
1516 let userId;
1517 // Deal with overloading
1518 if (id && id.constructor == Object) options = id;
1519 else if (id) userId = id.toString();
1520 // Configurize options
1521 options = {
1522 debug: 0,
1523 host: (0, $4f4134156c446392$export$7debb50ef11d5e0b).CLOUD_HOST,
1524 port: (0, $4f4134156c446392$export$7debb50ef11d5e0b).CLOUD_PORT,
1525 path: "/",
1526 key: $416260bce337df90$export$ecd1fc136c422448.DEFAULT_KEY,
1527 token: (0, $4f4134156c446392$export$7debb50ef11d5e0b).randomToken(),
1528 config: (0, $4f4134156c446392$export$7debb50ef11d5e0b).defaultConfig,
1529 referrerPolicy: "strict-origin-when-cross-origin",
1530 serializers: {},
1531 ...options
1532 };
1533 this._options = options;
1534 this._serializers = {
1535 ...this._serializers,
1536 ...this.options.serializers
1537 };
1538 // Detect relative URL host.
1539 if (this._options.host === "/") this._options.host = window.location.hostname;
1540 // Set path correctly.
1541 if (this._options.path) {
1542 if (this._options.path[0] !== "/") this._options.path = "/" + this._options.path;
1543 if (this._options.path[this._options.path.length - 1] !== "/") this._options.path += "/";
1544 }
1545 // Set whether we use SSL to same as current host
1546 if (this._options.secure === undefined && this._options.host !== (0, $4f4134156c446392$export$7debb50ef11d5e0b).CLOUD_HOST) this._options.secure = (0, $4f4134156c446392$export$7debb50ef11d5e0b).isSecure();
1547 else if (this._options.host == (0, $4f4134156c446392$export$7debb50ef11d5e0b).CLOUD_HOST) this._options.secure = true;
1548 // Set a custom log function if present
1549 if (this._options.logFunction) (0, $257947e92926277a$export$2e2bcd8739ae039).setLogFunction(this._options.logFunction);
1550 (0, $257947e92926277a$export$2e2bcd8739ae039).logLevel = this._options.debug || 0;
1551 this._api = new (0, $abf266641927cd89$export$2c4e825dc9120f87)(options);
1552 this._socket = this._createServerConnection();
1553 // Sanity checks
1554 // Ensure WebRTC supported
1555 if (!(0, $4f4134156c446392$export$7debb50ef11d5e0b).supports.audioVideo && !(0, $4f4134156c446392$export$7debb50ef11d5e0b).supports.data) {
1556 this._delayedAbort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).BrowserIncompatible, "The current browser does not support WebRTC");
1557 return;
1558 }
1559 // Ensure alphanumeric id
1560 if (!!userId && !(0, $4f4134156c446392$export$7debb50ef11d5e0b).validateId(userId)) {
1561 this._delayedAbort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).InvalidID, `ID "${userId}" is invalid`);
1562 return;
1563 }
1564 if (userId) this._initialize(userId);
1565 else this._api.retrieveId().then((id)=>this._initialize(id)).catch((error)=>this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).ServerError, error));
1566 }
1567 _createServerConnection() {
1568 const socket = new (0, $8f5bfa60836d261d$export$4798917dbf149b79)(this._options.secure, this._options.host, this._options.port, this._options.path, this._options.key, this._options.pingInterval);
1569 socket.on((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Message, (data)=>{
1570 this._handleMessage(data);
1571 });
1572 socket.on((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Error, (error)=>{
1573 this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).SocketError, error);
1574 });
1575 socket.on((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Disconnected, ()=>{
1576 if (this.disconnected) return;
1577 this.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).Network, "Lost connection to server.");
1578 this.disconnect();
1579 });
1580 socket.on((0, $78455e22dea96b8c$export$3b5c4a4b6354f023).Close, ()=>{
1581 if (this.disconnected) return;
1582 this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).SocketClosed, "Underlying socket is already closed.");
1583 });
1584 return socket;
1585 }
1586 /** Initialize a connection with the server. */ _initialize(id) {
1587 this._id = id;
1588 this.socket.start(id, this._options.token);
1589 }
1590 /** Handles messages from the server. */ _handleMessage(message) {
1591 const type = message.type;
1592 const payload = message.payload;
1593 const peerId = message.src;
1594 switch(type){
1595 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Open:
1596 this._lastServerId = this.id;
1597 this._open = true;
1598 this.emit("open", this.id);
1599 break;
1600 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Error:
1601 this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).ServerError, payload.msg);
1602 break;
1603 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).IdTaken:
1604 this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).UnavailableID, `ID "${this.id}" is taken`);
1605 break;
1606 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).InvalidKey:
1607 this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).InvalidKey, `API KEY "${this._options.key}" is invalid`);
1608 break;
1609 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Leave:
1610 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Received leave message from ${peerId}`);
1611 this._cleanupPeer(peerId);
1612 this._connections.delete(peerId);
1613 break;
1614 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Expire:
1615 this.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).PeerUnavailable, `Could not connect to peer ${peerId}`);
1616 break;
1617 case (0, $78455e22dea96b8c$export$adb4a1754da6f10d).Offer:
1618 {
1619 // we should consider switching this to CALL/CONNECT, but this is the least breaking option.
1620 const connectionId = payload.connectionId;
1621 let connection = this.getConnection(peerId, connectionId);
1622 if (connection) {
1623 connection.close();
1624 (0, $257947e92926277a$export$2e2bcd8739ae039).warn(`Offer received for existing Connection ID:${connectionId}`);
1625 }
1626 // Create a new connection.
1627 if (payload.type === (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Media) {
1628 const mediaConnection = new (0, $5c1d08c7c57da9a3$export$4a84e95a2324ac29)(peerId, this, {
1629 connectionId: connectionId,
1630 _payload: payload,
1631 metadata: payload.metadata
1632 });
1633 connection = mediaConnection;
1634 this._addConnection(peerId, connection);
1635 this.emit("call", mediaConnection);
1636 } else if (payload.type === (0, $78455e22dea96b8c$export$3157d57b4135e3bc).Data) {
1637 const dataConnection = new this._serializers[payload.serialization](peerId, this, {
1638 connectionId: connectionId,
1639 _payload: payload,
1640 metadata: payload.metadata,
1641 label: payload.label,
1642 serialization: payload.serialization,
1643 reliable: payload.reliable
1644 });
1645 connection = dataConnection;
1646 this._addConnection(peerId, connection);
1647 this.emit("connection", dataConnection);
1648 } else {
1649 (0, $257947e92926277a$export$2e2bcd8739ae039).warn(`Received malformed connection type:${payload.type}`);
1650 return;
1651 }
1652 // Find messages.
1653 const messages = this._getMessages(connectionId);
1654 for (const message of messages)connection.handleMessage(message);
1655 break;
1656 }
1657 default:
1658 {
1659 if (!payload) {
1660 (0, $257947e92926277a$export$2e2bcd8739ae039).warn(`You received a malformed message from ${peerId} of type ${type}`);
1661 return;
1662 }
1663 const connectionId = payload.connectionId;
1664 const connection = this.getConnection(peerId, connectionId);
1665 if (connection && connection.peerConnection) // Pass it on.
1666 connection.handleMessage(message);
1667 else if (connectionId) // Store for possible later use
1668 this._storeMessage(connectionId, message);
1669 else (0, $257947e92926277a$export$2e2bcd8739ae039).warn("You received an unrecognized message:", message);
1670 break;
1671 }
1672 }
1673 }
1674 /** Stores messages without a set up connection, to be claimed later. */ _storeMessage(connectionId, message) {
1675 if (!this._lostMessages.has(connectionId)) this._lostMessages.set(connectionId, []);
1676 this._lostMessages.get(connectionId).push(message);
1677 }
1678 /**
1679 * Retrieve messages from lost message store
1680 * @internal
1681 */ //TODO Change it to private
1682 _getMessages(connectionId) {
1683 const messages = this._lostMessages.get(connectionId);
1684 if (messages) {
1685 this._lostMessages.delete(connectionId);
1686 return messages;
1687 }
1688 return [];
1689 }
1690 /**
1691 * Connects to the remote peer specified by id and returns a data connection.
1692 * @param peer The brokering ID of the remote peer (their {@apilink Peer.id}).
1693 * @param options for specifying details about Peer Connection
1694 */ connect(peer, options = {}) {
1695 options = {
1696 serialization: "default",
1697 ...options
1698 };
1699 if (this.disconnected) {
1700 (0, $257947e92926277a$export$2e2bcd8739ae039).warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect, or call reconnect on this peer if you believe its ID to still be available.");
1701 this.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).Disconnected, "Cannot connect to new Peer after disconnecting from server.");
1702 return;
1703 }
1704 const dataConnection = new this._serializers[options.serialization](peer, this, options);
1705 this._addConnection(peer, dataConnection);
1706 return dataConnection;
1707 }
1708 /**
1709 * Calls the remote peer specified by id and returns a media connection.
1710 * @param peer The brokering ID of the remote peer (their peer.id).
1711 * @param stream The caller's media stream
1712 * @param options Metadata associated with the connection, passed in by whoever initiated the connection.
1713 */ call(peer, stream, options = {}) {
1714 if (this.disconnected) {
1715 (0, $257947e92926277a$export$2e2bcd8739ae039).warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect.");
1716 this.emitError((0, $78455e22dea96b8c$export$9547aaa2e39030ff).Disconnected, "Cannot connect to new Peer after disconnecting from server.");
1717 return;
1718 }
1719 if (!stream) {
1720 (0, $257947e92926277a$export$2e2bcd8739ae039).error("To call a peer, you must provide a stream from your browser's `getUserMedia`.");
1721 return;
1722 }
1723 const mediaConnection = new (0, $5c1d08c7c57da9a3$export$4a84e95a2324ac29)(peer, this, {
1724 ...options,
1725 _stream: stream
1726 });
1727 this._addConnection(peer, mediaConnection);
1728 return mediaConnection;
1729 }
1730 /** Add a data/media connection to this peer. */ _addConnection(peerId, connection) {
1731 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`add connection ${connection.type}:${connection.connectionId} to peerId:${peerId}`);
1732 if (!this._connections.has(peerId)) this._connections.set(peerId, []);
1733 this._connections.get(peerId).push(connection);
1734 }
1735 //TODO should be private
1736 _removeConnection(connection) {
1737 const connections = this._connections.get(connection.peer);
1738 if (connections) {
1739 const index = connections.indexOf(connection);
1740 if (index !== -1) connections.splice(index, 1);
1741 }
1742 //remove from lost messages
1743 this._lostMessages.delete(connection.connectionId);
1744 }
1745 /** Retrieve a data/media connection for this peer. */ getConnection(peerId, connectionId) {
1746 const connections = this._connections.get(peerId);
1747 if (!connections) return null;
1748 for (const connection of connections){
1749 if (connection.connectionId === connectionId) return connection;
1750 }
1751 return null;
1752 }
1753 _delayedAbort(type, message) {
1754 setTimeout(()=>{
1755 this._abort(type, message);
1756 }, 0);
1757 }
1758 /**
1759 * Emits an error message and destroys the Peer.
1760 * The Peer is not destroyed if it's in a disconnected state, in which case
1761 * it retains its disconnected state and its existing connections.
1762 */ _abort(type, message) {
1763 (0, $257947e92926277a$export$2e2bcd8739ae039).error("Aborting!");
1764 this.emitError(type, message);
1765 if (!this._lastServerId) this.destroy();
1766 else this.disconnect();
1767 }
1768 /**
1769 * Destroys the Peer: closes all active connections as well as the connection
1770 * to the server.
1771 *
1772 * :::caution
1773 * This cannot be undone; the respective peer object will no longer be able
1774 * to create or receive any connections, its ID will be forfeited on the server,
1775 * and all of its data and media connections will be closed.
1776 * :::
1777 */ destroy() {
1778 if (this.destroyed) return;
1779 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Destroy peer with ID:${this.id}`);
1780 this.disconnect();
1781 this._cleanup();
1782 this._destroyed = true;
1783 this.emit("close");
1784 }
1785 /** Disconnects every connection on this peer. */ _cleanup() {
1786 for (const peerId of this._connections.keys()){
1787 this._cleanupPeer(peerId);
1788 this._connections.delete(peerId);
1789 }
1790 this.socket.removeAllListeners();
1791 }
1792 /** Closes all connections to this peer. */ _cleanupPeer(peerId) {
1793 const connections = this._connections.get(peerId);
1794 if (!connections) return;
1795 for (const connection of connections)connection.close();
1796 }
1797 /**
1798 * Disconnects the Peer's connection to the PeerServer. Does not close any
1799 * active connections.
1800 * Warning: The peer can no longer create or accept connections after being
1801 * disconnected. It also cannot reconnect to the server.
1802 */ disconnect() {
1803 if (this.disconnected) return;
1804 const currentId = this.id;
1805 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Disconnect peer with ID:${currentId}`);
1806 this._disconnected = true;
1807 this._open = false;
1808 this.socket.close();
1809 this._lastServerId = currentId;
1810 this._id = null;
1811 this.emit("disconnected", currentId);
1812 }
1813 /** Attempts to reconnect with the same ID.
1814 *
1815 * Only {@apilink Peer.disconnect | disconnected peers} can be reconnected.
1816 * Destroyed peers cannot be reconnected.
1817 * If the connection fails (as an example, if the peer's old ID is now taken),
1818 * the peer's existing connections will not close, but any associated errors events will fire.
1819 */ reconnect() {
1820 if (this.disconnected && !this.destroyed) {
1821 (0, $257947e92926277a$export$2e2bcd8739ae039).log(`Attempting reconnection to server with ID ${this._lastServerId}`);
1822 this._disconnected = false;
1823 this._initialize(this._lastServerId);
1824 } else if (this.destroyed) throw new Error("This peer cannot reconnect to the server. It has already been destroyed.");
1825 else if (!this.disconnected && !this.open) // Do nothing. We're still connecting the first time.
1826 (0, $257947e92926277a$export$2e2bcd8739ae039).error("In a hurry? We're still trying to make the initial connection!");
1827 else throw new Error(`Peer ${this.id} cannot reconnect because it is not disconnected from the server!`);
1828 }
1829 /**
1830 * Get a list of available peer IDs. If you're running your own server, you'll
1831 * want to set allow_discovery: true in the PeerServer options. If you're using
1832 * the cloud server, email team@peerjs.com to get the functionality enabled for
1833 * your key.
1834 */ listAllPeers(cb = (_)=>{}) {
1835 this._api.listAllPeers().then((peers)=>cb(peers)).catch((error)=>this._abort((0, $78455e22dea96b8c$export$9547aaa2e39030ff).ServerError, error));
1836 }
1837}
1838
1839
1840
1841
1842
1843
1844class $20dbe68149d7aad9$export$72aa44612e2200cd extends (0, $6366c4ca161bc297$export$d365f7ad9d7df9c9) {
1845 constructor(peerId, provider, options){
1846 super(peerId, provider, {
1847 ...options,
1848 reliable: true
1849 });
1850 this._CHUNK_SIZE = 32768;
1851 this._splitStream = new TransformStream({
1852 transform: (chunk, controller)=>{
1853 for(let split = 0; split < chunk.length; split += this._CHUNK_SIZE)controller.enqueue(chunk.subarray(split, split + this._CHUNK_SIZE));
1854 }
1855 });
1856 this._rawSendStream = new WritableStream({
1857 write: async (chunk, controller)=>{
1858 const openEvent = new Promise((resolve)=>this.dataChannel.addEventListener("bufferedamountlow", resolve, {
1859 once: true
1860 }));
1861 // if we can send the chunk now, send it
1862 // if not, we wait until at least half of the sending buffer is free again
1863 await (this.dataChannel.bufferedAmount <= (0, $6366c4ca161bc297$export$d365f7ad9d7df9c9).MAX_BUFFERED_AMOUNT - chunk.byteLength || openEvent);
1864 // TODO: what can go wrong here?
1865 try {
1866 this.dataChannel.send(chunk);
1867 } catch (e) {
1868 (0, $257947e92926277a$export$2e2bcd8739ae039).error(`DC#:${this.connectionId} Error when sending:`, e);
1869 controller.error(e);
1870 this.close();
1871 }
1872 }
1873 });
1874 this.writer = this._splitStream.writable.getWriter();
1875 this._rawReadStream = new ReadableStream({
1876 start: (controller)=>{
1877 this.once("open", ()=>{
1878 this.dataChannel.addEventListener("message", (e)=>{
1879 controller.enqueue(e.data);
1880 });
1881 });
1882 }
1883 });
1884 this._splitStream.readable.pipeTo(this._rawSendStream);
1885 }
1886 _initializeDataChannel(dc) {
1887 super._initializeDataChannel(dc);
1888 this.dataChannel.binaryType = "arraybuffer";
1889 this.dataChannel.bufferedAmountLowThreshold = (0, $6366c4ca161bc297$export$d365f7ad9d7df9c9).MAX_BUFFERED_AMOUNT / 2;
1890 }
1891}
1892
1893
1894const $dcf98445f54823f4$var$NullValue = Symbol.for(null);
1895function $dcf98445f54823f4$var$concatUint8Array(buffer1, buffer2) {
1896 const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
1897 tmp.set(buffer1, 0);
1898 tmp.set(buffer2, buffer1.byteLength);
1899 return new Uint8Array(tmp.buffer);
1900}
1901const $dcf98445f54823f4$var$iterateOver = async function*(stream) {
1902 const reader = stream.getReader();
1903 try {
1904 while(true){
1905 const { done: done, value: value } = await reader.read();
1906 if (done) return;
1907 yield value;
1908 }
1909 } finally{
1910 reader.releaseLock();
1911 }
1912};
1913class $dcf98445f54823f4$export$7e9583c3c8a0a2cc extends (0, $20dbe68149d7aad9$export$72aa44612e2200cd) {
1914 constructor(peerId, provider, options){
1915 super(peerId, provider, {
1916 ...options,
1917 reliable: true
1918 });
1919 this.serialization = "Cbor";
1920 this._encoder = new (0, $gCcbY$Encoder)();
1921 this._decoder = new (0, $gCcbY$Decoder)();
1922 this._decoderStream = new TransformStream({
1923 transform: (abchunk, controller)=>{
1924 let chunk = new Uint8Array(abchunk);
1925 if (this._inc) {
1926 chunk = $dcf98445f54823f4$var$concatUint8Array(this._inc, chunk);
1927 this._inc = null;
1928 }
1929 let values;
1930 try {
1931 values = this._decoder.decodeMultiple(chunk);
1932 } catch (error) {
1933 if (error.incomplete) {
1934 this._inc = chunk.subarray(error.lastPosition);
1935 values = error.values;
1936 } else throw error;
1937 } finally{
1938 for (let value of values || []){
1939 if (value === null) value = $dcf98445f54823f4$var$NullValue;
1940 controller.enqueue(value);
1941 }
1942 }
1943 }
1944 });
1945 this._rawReadStream.pipeTo(this._decoderStream.writable);
1946 (async ()=>{
1947 for await (const msg of $dcf98445f54823f4$var$iterateOver(this._decoderStream.readable)){
1948 if (msg.__peerData?.type === "close") {
1949 this.close();
1950 return;
1951 }
1952 this.emit("data", msg);
1953 }
1954 })();
1955 }
1956 _send(data) {
1957 return this.writer.write(this._encoder.encode(data));
1958 }
1959}
1960
1961
1962class $3849fc09dd8f140b$export$f6a74c7ffa2903e6 extends (0, $416260bce337df90$export$ecd1fc136c422448) {
1963 constructor(...args){
1964 super(...args);
1965 this._serializers = {
1966 Cbor: $dcf98445f54823f4$export$7e9583c3c8a0a2cc,
1967 default: (0, $dcf98445f54823f4$export$7e9583c3c8a0a2cc)
1968 };
1969 }
1970}
1971
1972
1973
1974
1975
1976class $6e39230ab36396ad$export$80f5de1a66c4d624 extends (0, $20dbe68149d7aad9$export$72aa44612e2200cd) {
1977 constructor(peerId, provider, options){
1978 super(peerId, provider, options);
1979 this.serialization = "MsgPack";
1980 this._encoder = new (0, $gCcbY$Encoder1)();
1981 (async ()=>{
1982 for await (const msg of (0, $gCcbY$decodeMultiStream)(this._rawReadStream)){
1983 // @ts-ignore
1984 if (msg.__peerData?.type === "close") {
1985 this.close();
1986 return;
1987 }
1988 this.emit("data", msg);
1989 }
1990 })();
1991 }
1992 _send(data) {
1993 return this.writer.write(this._encoder.encode(data));
1994 }
1995}
1996
1997
1998class $1e0aff16be2c328e$export$d72c7bf8eef50853 extends (0, $416260bce337df90$export$ecd1fc136c422448) {
1999 constructor(...args){
2000 super(...args);
2001 this._serializers = {
2002 MsgPack: $6e39230ab36396ad$export$80f5de1a66c4d624,
2003 default: (0, $6e39230ab36396ad$export$80f5de1a66c4d624)
2004 };
2005 }
2006}
2007
2008
2009
2010
2011
2012
2013
2014
2015var $dd0187d7f28e386f$export$2e2bcd8739ae039 = (0, $416260bce337df90$export$ecd1fc136c422448);
2016
2017
2018export {$dd0187d7f28e386f$export$2e2bcd8739ae039 as default, $4f4134156c446392$export$7debb50ef11d5e0b as util, $a229bedbcaa6ca23$export$ff7c9d4c11d94e8b as BufferedConnection, $20dbe68149d7aad9$export$72aa44612e2200cd as StreamConnection, $dcf98445f54823f4$export$7e9583c3c8a0a2cc as Cbor, $6e39230ab36396ad$export$80f5de1a66c4d624 as MsgPack, $416260bce337df90$export$ecd1fc136c422448 as Peer, $1e0aff16be2c328e$export$d72c7bf8eef50853 as MsgPackPeer, $3849fc09dd8f140b$export$f6a74c7ffa2903e6 as CborPeer, $23779d1881157a18$export$98871882f492de82 as PeerError, $78455e22dea96b8c$export$3157d57b4135e3bc as ConnectionType, $78455e22dea96b8c$export$9547aaa2e39030ff as PeerErrorType, $78455e22dea96b8c$export$7974935686149686 as BaseConnectionErrorType, $78455e22dea96b8c$export$49ae800c114df41d as DataConnectionErrorType, $78455e22dea96b8c$export$89f507cf986a947 as SerializationType, $78455e22dea96b8c$export$3b5c4a4b6354f023 as SocketEventType, $78455e22dea96b8c$export$adb4a1754da6f10d as ServerMessageType};
2019//# sourceMappingURL=bundler.mjs.map