UNPKG

netflux

Version:

Peer to peer isomorphic transport API based on WebRTC and WebSocket. Allows to create/join full mesh network

1,411 lines (1,370 loc) 400 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var crypto = require('crypto'); var textEncoding = require('text-encoding'); var WebSocket = require('uws'); var urlLib = require('url'); var env = {}; env.WebSocket = WebSocket; env.TextEncoder = textEncoding.TextEncoder; env.TextDecoder = textEncoding.TextDecoder; env.cryptoNode = crypto; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __values(o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } var netfluxCSS = 'background-color: #FFCA28; padding: 0 3px'; var debugCSS = 'background-color: #b3ba2e; padding: 0 3px'; var webrtcCSS = 'background-color: #CE93D8; padding: 0 3px'; var channelCSS = 'background-color: #90CAF9; padding: 0 3px'; var topologyCSS = 'background-color: #26A69A; padding: 0 3px'; var signalingCSS = 'background-color: #66BB6A; padding: 0 3px'; var channelBuilderCSS = 'background-color: #F57C00; padding: 0 3px'; var signalingStateCSS = 'background-color: #9FA8DA; padding: 0 2px'; var webGroupStateCSS = 'background-color: #EF9A9A; padding: 0 2px'; var warningeCSS = 'background-color: #FF5252; padding: 0 2px'; var log = { webgroup: function () { }, signalingState: function () { }, webGroupState: function () { }, webrtc: function () { }, channel: function () { }, topology: function () { }, signaling: function () { }, channelBuilder: function () { }, debug: function () { }, warn: function () { }, }; (function (LogLevel) { LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG"; LogLevel[LogLevel["WEB_GROUP"] = 1] = "WEB_GROUP"; LogLevel[LogLevel["WEBRTC"] = 2] = "WEBRTC"; LogLevel[LogLevel["CHANNEL"] = 3] = "CHANNEL"; LogLevel[LogLevel["TOPOLOGY"] = 4] = "TOPOLOGY"; LogLevel[LogLevel["SIGNALING"] = 5] = "SIGNALING"; LogLevel[LogLevel["CHANNEL_BUILDER"] = 6] = "CHANNEL_BUILDER"; })(exports.LogLevel || (exports.LogLevel = {})); var logLevels = []; function setLogLevel() { var levels = []; for (var _i = 0; _i < arguments.length; _i++) { levels[_i] = arguments[_i]; } logLevels = levels; if (logLevels.includes(exports.LogLevel.WEB_GROUP)) { log.webgroup = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX WebGroup%c: " + msg, netfluxCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX WebGroup%c: " + msg, netfluxCSS, ''], rest)); } }; log.signalingState = function (msg, id) { console.info("%cNETFLUX " + id + " WebGroup%c: Signaling: %c" + msg + "%c", netfluxCSS, '', signalingStateCSS, ''); }; log.webGroupState = function (msg, id) { console.info("%cNETFLUX " + id + " WebGroup%c: WebGroup: %c" + msg + "%c", netfluxCSS, '', webGroupStateCSS, ''); }; } else { log.webgroup = function () { }; log.signalingState = function () { }; log.webGroupState = function () { }; } if (logLevels.includes(exports.LogLevel.WEBRTC)) { log.webrtc = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX WebRTC%c: " + msg, webrtcCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX WebRTC%c: " + msg, webrtcCSS, ''], rest)); } }; } else { log.webrtc = function () { }; } if (logLevels.includes(exports.LogLevel.CHANNEL)) { log.channel = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX Channel%c: " + msg, channelCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX Channel%c: " + msg, channelCSS, ''], rest)); } }; } else { log.channel = function () { }; } if (logLevels.includes(exports.LogLevel.TOPOLOGY)) { log.topology = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX Topology%c: " + msg, topologyCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX Topology%c: " + msg, topologyCSS, ''], rest)); } }; } else { log.topology = function () { }; } if (logLevels.includes(exports.LogLevel.SIGNALING)) { log.signaling = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX Signaling%c: " + msg, signalingCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX Signaling%c: " + msg, signalingCSS, ''], rest)); } }; } else { log.signaling = function () { }; } if (logLevels.includes(exports.LogLevel.CHANNEL_BUILDER)) { log.channelBuilder = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX ChannelBuilder%c: " + msg, channelBuilderCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX ChannelBuilder%c: " + msg, channelBuilderCSS, ''], rest)); } }; } else { log.channelBuilder = function () { }; } if (logLevels.includes(exports.LogLevel.DEBUG)) { log.debug = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX Debug%c: " + msg, debugCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX Debug%c: " + msg, debugCSS, ''], rest)); } }; } else { log.debug = function () { }; } log.warn = function (msg) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } if (rest.length === 0) { console.info("%cNETFLUX WARNING%c: " + msg, warningeCSS, ''); } else { console.info.apply(console, __spread(["%cNETFLUX WARNING%c: " + msg, warningeCSS, ''], rest)); } }; } var MIN_ID = 2147483648; var isBrowser = typeof window === 'undefined' ? false : true; function isOnline() { return isBrowser ? navigator.onLine : true; } function isVisible() { return isBrowser ? document.visibilityState === 'visible' : true; } function validateWebSocketURL(url) { var regex = /^(wss|ws):\/\/((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))(:[0-9]{1,5})?(\/.*)?$/; if (!new RegExp(regex, 'i').test(url)) { throw new Error("Invalid URL: " + url); } } function generateKey() { var mask = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; var length = 42; var values = randNumbers(length); var result = ''; for (var i = 0; i < length; i++) { result += mask[values[i] % mask.length]; } return result; } function generateId(exclude) { if (exclude === void 0) { exclude = []; } var id = randNumbers()[0]; if (id < MIN_ID) { id += MIN_ID; } if (exclude.includes(id)) { return generateId(exclude); } return id; } var MAX_KEY_LENGTH = 512; function validateKey(key) { if (typeof key !== 'string') { throw new Error("The key type \"" + typeof key + "\" is not a \"string\""); } else if (key === '') { throw new Error('The key is an empty string'); } else if (key.length > MAX_KEY_LENGTH) { throw new Error("The key length of " + key.length + " exceeds the maximum of " + MAX_KEY_LENGTH + " characters"); } return true; } function extractHostnameAndPort(url) { return url.split('/')[2]; } function randNumbers(length) { if (length === void 0) { length = 1; } var res; if (isBrowser) { res = new Uint32Array(length); env.crypto.getRandomValues(res); } else { res = []; var bytes = env.cryptoNode.randomBytes(4 * length); for (var i = 0; i < bytes.length; i += 4) { res[res.length] = bytes.readUInt32BE(i, true); } } return res; } function isWebSocketSupported() { return !!env.WebSocket; } function isWebRTCSupported() { return !!env.RTCPeerConnection && 'createDataChannel' in env.RTCPeerConnection.prototype; } function isFunction(x) { return typeof x === 'function'; } var _enable_super_gross_mode_that_will_cause_bad_things = false; var config = { Promise: undefined, set useDeprecatedSynchronousErrorHandling(value) { if (value) { var error = new Error(); console.warn('DEPRECATED! RxJS was set to use deprecated synchronous error handling behavior by code at: \n' + error.stack); } else if (_enable_super_gross_mode_that_will_cause_bad_things) { console.log('RxJS: Back to a better error behavior. Thank you. <3'); } _enable_super_gross_mode_that_will_cause_bad_things = value; }, get useDeprecatedSynchronousErrorHandling() { return _enable_super_gross_mode_that_will_cause_bad_things; }, }; function hostReportError(err) { setTimeout(function () { throw err; }); } var empty = { closed: true, next: function (value) { }, error: function (err) { if (config.useDeprecatedSynchronousErrorHandling) { throw err; } else { hostReportError(err); } }, complete: function () { } }; var isArray = Array.isArray || (function (x) { return x && typeof x.length === 'number'; }); function isObject(x) { return x != null && typeof x === 'object'; } var errorObject = { e: {} }; var tryCatchTarget; function tryCatcher() { try { return tryCatchTarget.apply(this, arguments); } catch (e) { errorObject.e = e; return errorObject; } } function tryCatch(fn) { tryCatchTarget = fn; return tryCatcher; } function UnsubscriptionErrorImpl(errors) { Error.call(this); this.message = errors ? errors.length + " errors occurred during unsubscription:\n" + errors.map(function (err, i) { return i + 1 + ") " + err.toString(); }).join('\n ') : ''; this.name = 'UnsubscriptionError'; this.errors = errors; return this; } UnsubscriptionErrorImpl.prototype = Object.create(Error.prototype); var UnsubscriptionError = UnsubscriptionErrorImpl; var Subscription = (function () { function Subscription(unsubscribe) { this.closed = false; this._parent = null; this._parents = null; this._subscriptions = null; if (unsubscribe) { this._unsubscribe = unsubscribe; } } Subscription.prototype.unsubscribe = function () { var hasErrors = false; var errors; if (this.closed) { return; } var _a = this, _parent = _a._parent, _parents = _a._parents, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions; this.closed = true; this._parent = null; this._parents = null; this._subscriptions = null; var index = -1; var len = _parents ? _parents.length : 0; while (_parent) { _parent.remove(this); _parent = ++index < len && _parents[index] || null; } if (isFunction(_unsubscribe)) { var trial = tryCatch(_unsubscribe).call(this); if (trial === errorObject) { hasErrors = true; errors = errors || (errorObject.e instanceof UnsubscriptionError ? flattenUnsubscriptionErrors(errorObject.e.errors) : [errorObject.e]); } } if (isArray(_subscriptions)) { index = -1; len = _subscriptions.length; while (++index < len) { var sub = _subscriptions[index]; if (isObject(sub)) { var trial = tryCatch(sub.unsubscribe).call(sub); if (trial === errorObject) { hasErrors = true; errors = errors || []; var err = errorObject.e; if (err instanceof UnsubscriptionError) { errors = errors.concat(flattenUnsubscriptionErrors(err.errors)); } else { errors.push(err); } } } } } if (hasErrors) { throw new UnsubscriptionError(errors); } }; Subscription.prototype.add = function (teardown) { if (!teardown || (teardown === Subscription.EMPTY)) { return Subscription.EMPTY; } if (teardown === this) { return this; } var subscription = teardown; switch (typeof teardown) { case 'function': subscription = new Subscription(teardown); case 'object': if (subscription.closed || typeof subscription.unsubscribe !== 'function') { return subscription; } else if (this.closed) { subscription.unsubscribe(); return subscription; } else if (typeof subscription._addParent !== 'function') { var tmp = subscription; subscription = new Subscription(); subscription._subscriptions = [tmp]; } break; default: throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.'); } var subscriptions = this._subscriptions || (this._subscriptions = []); subscriptions.push(subscription); subscription._addParent(this); return subscription; }; Subscription.prototype.remove = function (subscription) { var subscriptions = this._subscriptions; if (subscriptions) { var subscriptionIndex = subscriptions.indexOf(subscription); if (subscriptionIndex !== -1) { subscriptions.splice(subscriptionIndex, 1); } } }; Subscription.prototype._addParent = function (parent) { var _a = this, _parent = _a._parent, _parents = _a._parents; if (!_parent || _parent === parent) { this._parent = parent; } else if (!_parents) { this._parents = [parent]; } else if (_parents.indexOf(parent) === -1) { _parents.push(parent); } }; Subscription.EMPTY = (function (empty) { empty.closed = true; return empty; }(new Subscription())); return Subscription; }()); function flattenUnsubscriptionErrors(errors) { return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError) ? err.errors : err); }, []); } var rxSubscriber = (typeof Symbol === 'function' && typeof Symbol.for === 'function') ? Symbol.for('rxSubscriber') : '@@rxSubscriber'; var Subscriber = (function (_super) { __extends(Subscriber, _super); function Subscriber(destinationOrNext, error, complete) { var _this = _super.call(this) || this; _this.syncErrorValue = null; _this.syncErrorThrown = false; _this.syncErrorThrowable = false; _this.isStopped = false; _this._parentSubscription = null; switch (arguments.length) { case 0: _this.destination = empty; break; case 1: if (!destinationOrNext) { _this.destination = empty; break; } if (typeof destinationOrNext === 'object') { if (isTrustedSubscriber(destinationOrNext)) { var trustedSubscriber = destinationOrNext[rxSubscriber](); _this.syncErrorThrowable = trustedSubscriber.syncErrorThrowable; _this.destination = trustedSubscriber; trustedSubscriber._addParentTeardownLogic(_this); } else { _this.syncErrorThrowable = true; _this.destination = new SafeSubscriber(_this, destinationOrNext); } break; } default: _this.syncErrorThrowable = true; _this.destination = new SafeSubscriber(_this, destinationOrNext, error, complete); break; } return _this; } Subscriber.prototype[rxSubscriber] = function () { return this; }; Subscriber.create = function (next, error, complete) { var subscriber = new Subscriber(next, error, complete); subscriber.syncErrorThrowable = false; return subscriber; }; Subscriber.prototype.next = function (value) { if (!this.isStopped) { this._next(value); } }; Subscriber.prototype.error = function (err) { if (!this.isStopped) { this.isStopped = true; this._error(err); this._unsubscribeParentSubscription(); } }; Subscriber.prototype.complete = function () { if (!this.isStopped) { this.isStopped = true; this._complete(); this._unsubscribeParentSubscription(); } }; Subscriber.prototype.unsubscribe = function () { if (this.closed) { return; } this.isStopped = true; _super.prototype.unsubscribe.call(this); }; Subscriber.prototype._next = function (value) { this.destination.next(value); }; Subscriber.prototype._error = function (err) { this.destination.error(err); this.unsubscribe(); }; Subscriber.prototype._complete = function () { this.destination.complete(); this.unsubscribe(); }; Subscriber.prototype._addParentTeardownLogic = function (parentTeardownLogic) { if (parentTeardownLogic !== this) { this._parentSubscription = this.add(parentTeardownLogic); } }; Subscriber.prototype._unsubscribeParentSubscription = function () { if (this._parentSubscription !== null) { this._parentSubscription.unsubscribe(); } }; Subscriber.prototype._unsubscribeAndRecycle = function () { var _a = this, _parent = _a._parent, _parents = _a._parents; this._parent = null; this._parents = null; this.unsubscribe(); this.closed = false; this.isStopped = false; this._parent = _parent; this._parents = _parents; this._parentSubscription = null; return this; }; return Subscriber; }(Subscription)); var SafeSubscriber = (function (_super) { __extends(SafeSubscriber, _super); function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) { var _this = _super.call(this) || this; _this._parentSubscriber = _parentSubscriber; var next; var context = _this; if (isFunction(observerOrNext)) { next = observerOrNext; } else if (observerOrNext) { next = observerOrNext.next; error = observerOrNext.error; complete = observerOrNext.complete; if (observerOrNext !== empty) { context = Object.create(observerOrNext); if (isFunction(context.unsubscribe)) { _this.add(context.unsubscribe.bind(context)); } context.unsubscribe = _this.unsubscribe.bind(_this); } } _this._context = context; _this._next = next; _this._error = error; _this._complete = complete; return _this; } SafeSubscriber.prototype.next = function (value) { if (!this.isStopped && this._next) { var _parentSubscriber = this._parentSubscriber; if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) { this.__tryOrUnsub(this._next, value); } else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) { this.unsubscribe(); } } }; SafeSubscriber.prototype.error = function (err) { if (!this.isStopped) { var _parentSubscriber = this._parentSubscriber; var useDeprecatedSynchronousErrorHandling = config.useDeprecatedSynchronousErrorHandling; if (this._error) { if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) { this.__tryOrUnsub(this._error, err); this.unsubscribe(); } else { this.__tryOrSetError(_parentSubscriber, this._error, err); this.unsubscribe(); } } else if (!_parentSubscriber.syncErrorThrowable) { this.unsubscribe(); if (useDeprecatedSynchronousErrorHandling) { throw err; } hostReportError(err); } else { if (useDeprecatedSynchronousErrorHandling) { _parentSubscriber.syncErrorValue = err; _parentSubscriber.syncErrorThrown = true; } else { hostReportError(err); } this.unsubscribe(); } } }; SafeSubscriber.prototype.complete = function () { var _this = this; if (!this.isStopped) { var _parentSubscriber = this._parentSubscriber; if (this._complete) { var wrappedComplete = function () { return _this._complete.call(_this._context); }; if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) { this.__tryOrUnsub(wrappedComplete); this.unsubscribe(); } else { this.__tryOrSetError(_parentSubscriber, wrappedComplete); this.unsubscribe(); } } else { this.unsubscribe(); } } }; SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) { try { fn.call(this._context, value); } catch (err) { this.unsubscribe(); if (config.useDeprecatedSynchronousErrorHandling) { throw err; } else { hostReportError(err); } } }; SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) { if (!config.useDeprecatedSynchronousErrorHandling) { throw new Error('bad call'); } try { fn.call(this._context, value); } catch (err) { if (config.useDeprecatedSynchronousErrorHandling) { parent.syncErrorValue = err; parent.syncErrorThrown = true; return true; } else { hostReportError(err); return true; } } return false; }; SafeSubscriber.prototype._unsubscribe = function () { var _parentSubscriber = this._parentSubscriber; this._context = null; this._parentSubscriber = null; _parentSubscriber.unsubscribe(); }; return SafeSubscriber; }(Subscriber)); function isTrustedSubscriber(obj) { return obj instanceof Subscriber || ('_addParentTeardownLogic' in obj && obj[rxSubscriber]); } function toSubscriber(nextOrObserver, error, complete) { if (nextOrObserver) { if (nextOrObserver instanceof Subscriber) { return nextOrObserver; } if (nextOrObserver[rxSubscriber]) { return nextOrObserver[rxSubscriber](); } } if (!nextOrObserver && !error && !complete) { return new Subscriber(empty); } return new Subscriber(nextOrObserver, error, complete); } var observable = typeof Symbol === 'function' && Symbol.observable || '@@observable'; function noop() { } function pipeFromArray(fns) { if (!fns) { return noop; } if (fns.length === 1) { return fns[0]; } return function piped(input) { return fns.reduce(function (prev, fn) { return fn(prev); }, input); }; } var Observable = (function () { function Observable(subscribe) { this._isScalar = false; if (subscribe) { this._subscribe = subscribe; } } Observable.prototype.lift = function (operator) { var observable$$1 = new Observable(); observable$$1.source = this; observable$$1.operator = operator; return observable$$1; }; Observable.prototype.subscribe = function (observerOrNext, error, complete) { var operator = this.operator; var sink = toSubscriber(observerOrNext, error, complete); if (operator) { operator.call(sink, this.source); } else { sink._addParentTeardownLogic(this.source || (config.useDeprecatedSynchronousErrorHandling && !sink.syncErrorThrowable) ? this._subscribe(sink) : this._trySubscribe(sink)); } if (config.useDeprecatedSynchronousErrorHandling) { if (sink.syncErrorThrowable) { sink.syncErrorThrowable = false; if (sink.syncErrorThrown) { throw sink.syncErrorValue; } } } return sink; }; Observable.prototype._trySubscribe = function (sink) { try { return this._subscribe(sink); } catch (err) { if (config.useDeprecatedSynchronousErrorHandling) { sink.syncErrorThrown = true; sink.syncErrorValue = err; } sink.error(err); } }; Observable.prototype.forEach = function (next, promiseCtor) { var _this = this; promiseCtor = getPromiseCtor(promiseCtor); return new promiseCtor(function (resolve, reject) { var subscription; subscription = _this.subscribe(function (value) { try { next(value); } catch (err) { reject(err); if (subscription) { subscription.unsubscribe(); } } }, reject, resolve); }); }; Observable.prototype._subscribe = function (subscriber) { var source = this.source; return source && source.subscribe(subscriber); }; Observable.prototype[observable] = function () { return this; }; Observable.prototype.pipe = function () { var operations = []; for (var _i = 0; _i < arguments.length; _i++) { operations[_i] = arguments[_i]; } if (operations.length === 0) { return this; } return pipeFromArray(operations)(this); }; Observable.prototype.toPromise = function (promiseCtor) { var _this = this; promiseCtor = getPromiseCtor(promiseCtor); return new promiseCtor(function (resolve, reject) { var value; _this.subscribe(function (x) { return value = x; }, function (err) { return reject(err); }, function () { return resolve(value); }); }); }; Observable.create = function (subscribe) { return new Observable(subscribe); }; return Observable; }()); function getPromiseCtor(promiseCtor) { if (!promiseCtor) { promiseCtor = config.Promise || Promise; } if (!promiseCtor) { throw new Error('no Promise impl found'); } return promiseCtor; } function ObjectUnsubscribedErrorImpl() { Error.call(this); this.message = 'object unsubscribed'; this.name = 'ObjectUnsubscribedError'; return this; } ObjectUnsubscribedErrorImpl.prototype = Object.create(Error.prototype); var ObjectUnsubscribedError = ObjectUnsubscribedErrorImpl; var SubjectSubscription = (function (_super) { __extends(SubjectSubscription, _super); function SubjectSubscription(subject, subscriber) { var _this = _super.call(this) || this; _this.subject = subject; _this.subscriber = subscriber; _this.closed = false; return _this; } SubjectSubscription.prototype.unsubscribe = function () { if (this.closed) { return; } this.closed = true; var subject = this.subject; var observers = subject.observers; this.subject = null; if (!observers || observers.length === 0 || subject.isStopped || subject.closed) { return; } var subscriberIndex = observers.indexOf(this.subscriber); if (subscriberIndex !== -1) { observers.splice(subscriberIndex, 1); } }; return SubjectSubscription; }(Subscription)); var SubjectSubscriber = (function (_super) { __extends(SubjectSubscriber, _super); function SubjectSubscriber(destination) { var _this = _super.call(this, destination) || this; _this.destination = destination; return _this; } return SubjectSubscriber; }(Subscriber)); var Subject = (function (_super) { __extends(Subject, _super); function Subject() { var _this = _super.call(this) || this; _this.observers = []; _this.closed = false; _this.isStopped = false; _this.hasError = false; _this.thrownError = null; return _this; } Subject.prototype[rxSubscriber] = function () { return new SubjectSubscriber(this); }; Subject.prototype.lift = function (operator) { var subject = new AnonymousSubject(this, this); subject.operator = operator; return subject; }; Subject.prototype.next = function (value) { if (this.closed) { throw new ObjectUnsubscribedError(); } if (!this.isStopped) { var observers = this.observers; var len = observers.length; var copy = observers.slice(); for (var i = 0; i < len; i++) { copy[i].next(value); } } }; Subject.prototype.error = function (err) { if (this.closed) { throw new ObjectUnsubscribedError(); } this.hasError = true; this.thrownError = err; this.isStopped = true; var observers = this.observers; var len = observers.length; var copy = observers.slice(); for (var i = 0; i < len; i++) { copy[i].error(err); } this.observers.length = 0; }; Subject.prototype.complete = function () { if (this.closed) { throw new ObjectUnsubscribedError(); } this.isStopped = true; var observers = this.observers; var len = observers.length; var copy = observers.slice(); for (var i = 0; i < len; i++) { copy[i].complete(); } this.observers.length = 0; }; Subject.prototype.unsubscribe = function () { this.isStopped = true; this.closed = true; this.observers = null; }; Subject.prototype._trySubscribe = function (subscriber) { if (this.closed) { throw new ObjectUnsubscribedError(); } else { return _super.prototype._trySubscribe.call(this, subscriber); } }; Subject.prototype._subscribe = function (subscriber) { if (this.closed) { throw new ObjectUnsubscribedError(); } else if (this.hasError) { subscriber.error(this.thrownError); return Subscription.EMPTY; } else if (this.isStopped) { subscriber.complete(); return Subscription.EMPTY; } else { this.observers.push(subscriber); return new SubjectSubscription(this, subscriber); } }; Subject.prototype.asObservable = function () { var observable = new Observable(); observable.source = this; return observable; }; Subject.create = function (destination, source) { return new AnonymousSubject(destination, source); }; return Subject; }(Observable)); var AnonymousSubject = (function (_super) { __extends(AnonymousSubject, _super); function AnonymousSubject(destination, source) { var _this = _super.call(this) || this; _this.destination = destination; _this.source = source; return _this; } AnonymousSubject.prototype.next = function (value) { var destination = this.destination; if (destination && destination.next) { destination.next(value); } }; AnonymousSubject.prototype.error = function (err) { var destination = this.destination; if (destination && destination.error) { this.destination.error(err); } }; AnonymousSubject.prototype.complete = function () { var destination = this.destination; if (destination && destination.complete) { this.destination.complete(); } }; AnonymousSubject.prototype._subscribe = function (subscriber) { var source = this.source; if (source) { return this.source.subscribe(subscriber); } else { return Subscription.EMPTY; } }; return AnonymousSubject; }(Subject)); function refCount() { return function refCountOperatorFunction(source) { return source.lift(new RefCountOperator(source)); }; } var RefCountOperator = (function () { function RefCountOperator(connectable) { this.connectable = connectable; } RefCountOperator.prototype.call = function (subscriber, source) { var connectable = this.connectable; connectable._refCount++; var refCounter = new RefCountSubscriber(subscriber, connectable); var subscription = source.subscribe(refCounter); if (!refCounter.closed) { refCounter.connection = connectable.connect(); } return subscription; }; return RefCountOperator; }()); var RefCountSubscriber = (function (_super) { __extends(RefCountSubscriber, _super); function RefCountSubscriber(destination, connectable) { var _this = _super.call(this, destination) || this; _this.connectable = connectable; return _this; } RefCountSubscriber.prototype._unsubscribe = function () { var connectable = this.connectable; if (!connectable) { this.connection = null; return; } this.connectable = null; var refCount = connectable._refCount; if (refCount <= 0) { this.connection = null; return; } connectable._refCount = refCount - 1; if (refCount > 1) { this.connection = null; return; } var connection = this.connection; var sharedConnection = connectable._connection; this.connection = null; if (sharedConnection && (!connection || sharedConnection === connection)) { sharedConnection.unsubscribe(); } }; return RefCountSubscriber; }(Subscriber)); var ConnectableObservable = (function (_super) { __extends(ConnectableObservable, _super); function ConnectableObservable(source, subjectFactory) { var _this = _super.call(this) || this; _this.source = source; _this.subjectFactory = subjectFactory; _this._refCount = 0; _this._isComplete = false; return _this; } ConnectableObservable.prototype._subscribe = function (subscriber) { return this.getSubject().subscribe(subscriber); }; ConnectableObservable.prototype.getSubject = function () { var subject = this._subject; if (!subject || subject.isStopped) { this._subject = this.subjectFactory(); } return this._subject; }; ConnectableObservable.prototype.connect = function () { var connection = this._connection; if (!connection) { this._isComplete = false; connection = this._connection = new Subscription(); connection.add(this.source .subscribe(new ConnectableSubscriber(this.getSubject(), this))); if (connection.closed) { this._connection = null; connection = Subscription.EMPTY; } else { this._connection = connection; } } return connection; }; ConnectableObservable.prototype.refCount = function () { return refCount()(this); }; return ConnectableObservable; }(Observable)); var connectableProto = ConnectableObservable.prototype; var connectableObservableDescriptor = { operator: { value: null }, _refCount: { value: 0, writable: true }, _subject: { value: null, writable: true }, _connection: { value: null, writable: true }, _subscribe: { value: connectableProto._subscribe }, _isComplete: { value: connectableProto._isComplete, writable: true }, getSubject: { value: connectableProto.getSubject }, connect: { value: connectableProto.connect }, refCount: { value: connectableProto.refCount } }; var ConnectableSubscriber = (function (_super) { __extends(ConnectableSubscriber, _super); function ConnectableSubscriber(destination, connectable) { var _this = _super.call(this, destination) || this; _this.connectable = connectable; return _this; } ConnectableSubscriber.prototype._error = function (err) { this._unsubscribe(); _super.prototype._error.call(this, err); }; ConnectableSubscriber.prototype._complete = function () { this.connectable._isComplete = true; this._unsubscribe(); _super.prototype._complete.call(this); }; ConnectableSubscriber.prototype._unsubscribe = function () { var connectable = this.connectable; if (connectable) { this.connectable = null; var connection = connectable._connection; connectable._refCount = 0; connectable._subject = null; connectable._connection = null; if (connection) { connection.unsubscribe(); } } }; return ConnectableSubscriber; }(SubjectSubscriber)); var RefCountSubscriber$1 = (function (_super) { __extends(RefCountSubscriber, _super); function RefCountSubscriber(destination, connectable) { var _this = _super.call(this, destination) || this; _this.connectable = connectable; return _this; } RefCountSubscriber.prototype._unsubscribe = function () { var connectable = this.connectable; if (!connectable) { this.connection = null; return; } this.connectable = null; var refCount$$1 = connectable._refCount; if (refCount$$1 <= 0) { this.connection = null; return; } connectable._refCount = refCount$$1 - 1; if (refCount$$1 > 1) { this.connection = null; return; } var connection = this.connection; var sharedConnection = connectable._connection; this.connection = null; if (sharedConnection && (!connection || sharedConnection === connection)) { sharedConnection.unsubscribe(); } }; return RefCountSubscriber; }(Subscriber)); var GroupBySubscriber = (function (_super) { __extends(GroupBySubscriber, _super); function GroupBySubscriber(destination, keySelector, elementSelector, durationSelector, subjectSelector) { var _this = _super.call(this, destination) || this; _this.keySelector = keySelector; _this.elementSelector = elementSelector; _this.durationSelector = durationSelector; _this.subjectSelector = subjectSelector; _this.groups = null; _this.attemptedToUnsubscribe = false; _this.count = 0; return _this; } GroupBySubscriber.prototype._next = function (value) { var key; try { key = this.keySelector(value); } catch (err) { this.error(err); return; } this._group(value, key); }; GroupBySubscriber.prototype._group = function (value, key) { var groups = this.groups; if (!groups) { groups = this.groups = new Map(); } var group = groups.get(key); var element; if (this.elementSelector) { try { element = this.elementSelector(value); } catch (err) { this.error(err); } } else { element = value; } if (!group) { group = (this.subjectSelector ? this.subjectSelector() : new Subject()); groups.set(key, group); var groupedObservable = new GroupedObservable(key, group, this); this.destination.next(groupedObservable); if (this.durationSelector) { var duration = void 0; try { duration = this.durationSelector(new GroupedObservable(key, group)); } catch (err) { this.error(err); return; } this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this))); } } if (!group.closed) { group.next(element); } }; GroupBySubscriber.prototype._error = function (err) { var groups = this.groups; if (groups) { groups.forEach(function (group, key) { group.error(err); }); groups.clear(); } this.destination.error(err); }; GroupBySubscriber.prototype._complete = function () { var groups = this.groups; if (groups) { groups.forEach(function (group, key) { group.complete(); }); groups.clear(); } this.destination.complete(); }; GroupBySubscriber.prototype.removeGroup = function (key) { this.groups.delete(key); }; GroupBySubscriber.prototype.unsubscribe = function () { if (!this.closed) { this.attemptedToUnsubscribe = true; if (this.count === 0) { _super.prototype.unsubscribe.call(this); }