1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240 |
- /*!
- * Socket.IO v4.4.1
- * (c) 2014-2022 Guillermo Rauch
- * Released under the MIT License.
- */
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
- typeof define === 'function' && define.amd ? define(factory) :
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.io = factory());
- })(this, (function () { 'use strict';
- function _typeof(obj) {
- "@babel/helpers - typeof";
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
- _typeof = function (obj) {
- return typeof obj;
- };
- } else {
- _typeof = function (obj) {
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
- };
- }
- return _typeof(obj);
- }
- function _classCallCheck(instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- }
- function _defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
- function _createClass(Constructor, protoProps, staticProps) {
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
- if (staticProps) _defineProperties(Constructor, staticProps);
- return Constructor;
- }
- function _extends() {
- _extends = Object.assign || function (target) {
- for (var i = 1; i < arguments.length; i++) {
- var source = arguments[i];
- for (var key in source) {
- if (Object.prototype.hasOwnProperty.call(source, key)) {
- target[key] = source[key];
- }
- }
- }
- return target;
- };
- return _extends.apply(this, arguments);
- }
- function _inherits(subClass, superClass) {
- if (typeof superClass !== "function" && superClass !== null) {
- throw new TypeError("Super expression must either be null or a function");
- }
- subClass.prototype = Object.create(superClass && superClass.prototype, {
- constructor: {
- value: subClass,
- writable: true,
- configurable: true
- }
- });
- if (superClass) _setPrototypeOf(subClass, superClass);
- }
- function _getPrototypeOf(o) {
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
- return o.__proto__ || Object.getPrototypeOf(o);
- };
- return _getPrototypeOf(o);
- }
- function _setPrototypeOf(o, p) {
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
- o.__proto__ = p;
- return o;
- };
- return _setPrototypeOf(o, p);
- }
- function _isNativeReflectConstruct() {
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
- if (Reflect.construct.sham) return false;
- if (typeof Proxy === "function") return true;
- try {
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
- return true;
- } catch (e) {
- return false;
- }
- }
- function _assertThisInitialized(self) {
- if (self === void 0) {
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
- }
- return self;
- }
- function _possibleConstructorReturn(self, call) {
- if (call && (typeof call === "object" || typeof call === "function")) {
- return call;
- } else if (call !== void 0) {
- throw new TypeError("Derived constructors may only return object or undefined");
- }
- return _assertThisInitialized(self);
- }
- function _createSuper(Derived) {
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
- return function _createSuperInternal() {
- var Super = _getPrototypeOf(Derived),
- result;
- if (hasNativeReflectConstruct) {
- var NewTarget = _getPrototypeOf(this).constructor;
- result = Reflect.construct(Super, arguments, NewTarget);
- } else {
- result = Super.apply(this, arguments);
- }
- return _possibleConstructorReturn(this, result);
- };
- }
- function _superPropBase(object, property) {
- while (!Object.prototype.hasOwnProperty.call(object, property)) {
- object = _getPrototypeOf(object);
- if (object === null) break;
- }
- return object;
- }
- function _get(target, property, receiver) {
- if (typeof Reflect !== "undefined" && Reflect.get) {
- _get = Reflect.get;
- } else {
- _get = function _get(target, property, receiver) {
- var base = _superPropBase(target, property);
- if (!base) return;
- var desc = Object.getOwnPropertyDescriptor(base, property);
- if (desc.get) {
- return desc.get.call(receiver);
- }
- return desc.value;
- };
- }
- return _get(target, property, receiver || target);
- }
- function _unsupportedIterableToArray(o, minLen) {
- if (!o) return;
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
- var n = Object.prototype.toString.call(o).slice(8, -1);
- if (n === "Object" && o.constructor) n = o.constructor.name;
- if (n === "Map" || n === "Set") return Array.from(o);
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
- }
- function _arrayLikeToArray(arr, len) {
- if (len == null || len > arr.length) len = arr.length;
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
- return arr2;
- }
- function _createForOfIteratorHelper(o, allowArrayLike) {
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
- if (!it) {
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
- if (it) o = it;
- var i = 0;
- var F = function () {};
- return {
- s: F,
- n: function () {
- if (i >= o.length) return {
- done: true
- };
- return {
- done: false,
- value: o[i++]
- };
- },
- e: function (e) {
- throw e;
- },
- f: F
- };
- }
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
- }
- var normalCompletion = true,
- didErr = false,
- err;
- return {
- s: function () {
- it = it.call(o);
- },
- n: function () {
- var step = it.next();
- normalCompletion = step.done;
- return step;
- },
- e: function (e) {
- didErr = true;
- err = e;
- },
- f: function () {
- try {
- if (!normalCompletion && it.return != null) it.return();
- } finally {
- if (didErr) throw err;
- }
- }
- };
- }
- /**
- * Parses an URI
- *
- * @author Steven Levithan <stevenlevithan.com> (MIT license)
- * @api private
- */
- var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
- var parts = ['source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'];
- var parseuri = function parseuri(str) {
- var src = str,
- b = str.indexOf('['),
- e = str.indexOf(']');
- if (b != -1 && e != -1) {
- str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
- }
- var m = re.exec(str || ''),
- uri = {},
- i = 14;
- while (i--) {
- uri[parts[i]] = m[i] || '';
- }
- if (b != -1 && e != -1) {
- uri.source = src;
- uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
- uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
- uri.ipv6uri = true;
- }
- uri.pathNames = pathNames(uri, uri['path']);
- uri.queryKey = queryKey(uri, uri['query']);
- return uri;
- };
- function pathNames(obj, path) {
- var regx = /\/{2,9}/g,
- names = path.replace(regx, "/").split("/");
- if (path.substr(0, 1) == '/' || path.length === 0) {
- names.splice(0, 1);
- }
- if (path.substr(path.length - 1, 1) == '/') {
- names.splice(names.length - 1, 1);
- }
- return names;
- }
- function queryKey(uri, query) {
- var data = {};
- query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) {
- if ($1) {
- data[$1] = $2;
- }
- });
- return data;
- }
- /**
- * URL parser.
- *
- * @param uri - url
- * @param path - the request path of the connection
- * @param loc - An object meant to mimic window.location.
- * Defaults to window.location.
- * @public
- */
- function url(uri) {
- var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
- var loc = arguments.length > 2 ? arguments[2] : undefined;
- var obj = uri; // default to window.location
- loc = loc || typeof location !== "undefined" && location;
- if (null == uri) uri = loc.protocol + "//" + loc.host; // relative path support
- if (typeof uri === "string") {
- if ("/" === uri.charAt(0)) {
- if ("/" === uri.charAt(1)) {
- uri = loc.protocol + uri;
- } else {
- uri = loc.host + uri;
- }
- }
- if (!/^(https?|wss?):\/\//.test(uri)) {
- if ("undefined" !== typeof loc) {
- uri = loc.protocol + "//" + uri;
- } else {
- uri = "https://" + uri;
- }
- } // parse
- obj = parseuri(uri);
- } // make sure we treat `localhost:80` and `localhost` equally
- if (!obj.port) {
- if (/^(http|ws)$/.test(obj.protocol)) {
- obj.port = "80";
- } else if (/^(http|ws)s$/.test(obj.protocol)) {
- obj.port = "443";
- }
- }
- obj.path = obj.path || "/";
- var ipv6 = obj.host.indexOf(":") !== -1;
- var host = ipv6 ? "[" + obj.host + "]" : obj.host; // define unique id
- obj.id = obj.protocol + "://" + host + ":" + obj.port + path; // define href
- obj.href = obj.protocol + "://" + host + (loc && loc.port === obj.port ? "" : ":" + obj.port);
- return obj;
- }
- var hasCors = {exports: {}};
- /**
- * Module exports.
- *
- * Logic borrowed from Modernizr:
- *
- * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js
- */
- try {
- hasCors.exports = typeof XMLHttpRequest !== 'undefined' && 'withCredentials' in new XMLHttpRequest();
- } catch (err) {
- // if XMLHttp support is disabled in IE then it will throw
- // when trying to create
- hasCors.exports = false;
- }
- var hasCORS = hasCors.exports;
- var globalThis = (function () {
- if (typeof self !== "undefined") {
- return self;
- } else if (typeof window !== "undefined") {
- return window;
- } else {
- return Function("return this")();
- }
- })();
- // browser shim for xmlhttprequest module
- function XMLHttpRequest$1 (opts) {
- var xdomain = opts.xdomain; // XMLHttpRequest can be disabled on IE
- try {
- if ("undefined" !== typeof XMLHttpRequest && (!xdomain || hasCORS)) {
- return new XMLHttpRequest();
- }
- } catch (e) {}
- if (!xdomain) {
- try {
- return new globalThis[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
- } catch (e) {}
- }
- }
- function pick(obj) {
- for (var _len = arguments.length, attr = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- attr[_key - 1] = arguments[_key];
- }
- return attr.reduce(function (acc, k) {
- if (obj.hasOwnProperty(k)) {
- acc[k] = obj[k];
- }
- return acc;
- }, {});
- } // Keep a reference to the real timeout functions so they can be used when overridden
- var NATIVE_SET_TIMEOUT = setTimeout;
- var NATIVE_CLEAR_TIMEOUT = clearTimeout;
- function installTimerFunctions(obj, opts) {
- if (opts.useNativeTimers) {
- obj.setTimeoutFn = NATIVE_SET_TIMEOUT.bind(globalThis);
- obj.clearTimeoutFn = NATIVE_CLEAR_TIMEOUT.bind(globalThis);
- } else {
- obj.setTimeoutFn = setTimeout.bind(globalThis);
- obj.clearTimeoutFn = clearTimeout.bind(globalThis);
- }
- }
- /**
- * Expose `Emitter`.
- */
- var Emitter_1 = Emitter;
- /**
- * Initialize a new `Emitter`.
- *
- * @api public
- */
- function Emitter(obj) {
- if (obj) return mixin(obj);
- }
- /**
- * Mixin the emitter properties.
- *
- * @param {Object} obj
- * @return {Object}
- * @api private
- */
- function mixin(obj) {
- for (var key in Emitter.prototype) {
- obj[key] = Emitter.prototype[key];
- }
- return obj;
- }
- /**
- * Listen on the given `event` with `fn`.
- *
- * @param {String} event
- * @param {Function} fn
- * @return {Emitter}
- * @api public
- */
- Emitter.prototype.on = Emitter.prototype.addEventListener = function (event, fn) {
- this._callbacks = this._callbacks || {};
- (this._callbacks['$' + event] = this._callbacks['$' + event] || []).push(fn);
- return this;
- };
- /**
- * Adds an `event` listener that will be invoked a single
- * time then automatically removed.
- *
- * @param {String} event
- * @param {Function} fn
- * @return {Emitter}
- * @api public
- */
- Emitter.prototype.once = function (event, fn) {
- function on() {
- this.off(event, on);
- fn.apply(this, arguments);
- }
- on.fn = fn;
- this.on(event, on);
- return this;
- };
- /**
- * Remove the given callback for `event` or all
- * registered callbacks.
- *
- * @param {String} event
- * @param {Function} fn
- * @return {Emitter}
- * @api public
- */
- Emitter.prototype.off = Emitter.prototype.removeListener = Emitter.prototype.removeAllListeners = Emitter.prototype.removeEventListener = function (event, fn) {
- this._callbacks = this._callbacks || {}; // all
- if (0 == arguments.length) {
- this._callbacks = {};
- return this;
- } // specific event
- var callbacks = this._callbacks['$' + event];
- if (!callbacks) return this; // remove all handlers
- if (1 == arguments.length) {
- delete this._callbacks['$' + event];
- return this;
- } // remove specific handler
- var cb;
- for (var i = 0; i < callbacks.length; i++) {
- cb = callbacks[i];
- if (cb === fn || cb.fn === fn) {
- callbacks.splice(i, 1);
- break;
- }
- } // Remove event specific arrays for event types that no
- // one is subscribed for to avoid memory leak.
- if (callbacks.length === 0) {
- delete this._callbacks['$' + event];
- }
- return this;
- };
- /**
- * Emit `event` with the given args.
- *
- * @param {String} event
- * @param {Mixed} ...
- * @return {Emitter}
- */
- Emitter.prototype.emit = function (event) {
- this._callbacks = this._callbacks || {};
- var args = new Array(arguments.length - 1),
- callbacks = this._callbacks['$' + event];
- for (var i = 1; i < arguments.length; i++) {
- args[i - 1] = arguments[i];
- }
- if (callbacks) {
- callbacks = callbacks.slice(0);
- for (var i = 0, len = callbacks.length; i < len; ++i) {
- callbacks[i].apply(this, args);
- }
- }
- return this;
- }; // alias used for reserved events (protected method)
- Emitter.prototype.emitReserved = Emitter.prototype.emit;
- /**
- * Return array of callbacks for `event`.
- *
- * @param {String} event
- * @return {Array}
- * @api public
- */
- Emitter.prototype.listeners = function (event) {
- this._callbacks = this._callbacks || {};
- return this._callbacks['$' + event] || [];
- };
- /**
- * Check if this emitter has `event` handlers.
- *
- * @param {String} event
- * @return {Boolean}
- * @api public
- */
- Emitter.prototype.hasListeners = function (event) {
- return !!this.listeners(event).length;
- };
- var PACKET_TYPES = Object.create(null); // no Map = no polyfill
- PACKET_TYPES["open"] = "0";
- PACKET_TYPES["close"] = "1";
- PACKET_TYPES["ping"] = "2";
- PACKET_TYPES["pong"] = "3";
- PACKET_TYPES["message"] = "4";
- PACKET_TYPES["upgrade"] = "5";
- PACKET_TYPES["noop"] = "6";
- var PACKET_TYPES_REVERSE = Object.create(null);
- Object.keys(PACKET_TYPES).forEach(function (key) {
- PACKET_TYPES_REVERSE[PACKET_TYPES[key]] = key;
- });
- var ERROR_PACKET = {
- type: "error",
- data: "parser error"
- };
- var withNativeBlob$1 = typeof Blob === "function" || typeof Blob !== "undefined" && Object.prototype.toString.call(Blob) === "[object BlobConstructor]";
- var withNativeArrayBuffer$2 = typeof ArrayBuffer === "function"; // ArrayBuffer.isView method is not defined in IE10
- var isView$1 = function isView(obj) {
- return typeof ArrayBuffer.isView === "function" ? ArrayBuffer.isView(obj) : obj && obj.buffer instanceof ArrayBuffer;
- };
- var encodePacket = function encodePacket(_ref, supportsBinary, callback) {
- var type = _ref.type,
- data = _ref.data;
- if (withNativeBlob$1 && data instanceof Blob) {
- if (supportsBinary) {
- return callback(data);
- } else {
- return encodeBlobAsBase64(data, callback);
- }
- } else if (withNativeArrayBuffer$2 && (data instanceof ArrayBuffer || isView$1(data))) {
- if (supportsBinary) {
- return callback(data);
- } else {
- return encodeBlobAsBase64(new Blob([data]), callback);
- }
- } // plain string
- return callback(PACKET_TYPES[type] + (data || ""));
- };
- var encodeBlobAsBase64 = function encodeBlobAsBase64(data, callback) {
- var fileReader = new FileReader();
- fileReader.onload = function () {
- var content = fileReader.result.split(",")[1];
- callback("b" + content);
- };
- return fileReader.readAsDataURL(data);
- };
- /*
- * base64-arraybuffer 1.0.1 <https://github.com/niklasvh/base64-arraybuffer>
- * Copyright (c) 2021 Niklas von Hertzen <https://hertzen.com>
- * Released under MIT License
- */
- var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // Use a lookup table to find the index.
- var lookup$1 = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
- for (var i$1 = 0; i$1 < chars.length; i$1++) {
- lookup$1[chars.charCodeAt(i$1)] = i$1;
- }
- var decode$1 = function decode(base64) {
- var bufferLength = base64.length * 0.75,
- len = base64.length,
- i,
- p = 0,
- encoded1,
- encoded2,
- encoded3,
- encoded4;
- if (base64[base64.length - 1] === '=') {
- bufferLength--;
- if (base64[base64.length - 2] === '=') {
- bufferLength--;
- }
- }
- var arraybuffer = new ArrayBuffer(bufferLength),
- bytes = new Uint8Array(arraybuffer);
- for (i = 0; i < len; i += 4) {
- encoded1 = lookup$1[base64.charCodeAt(i)];
- encoded2 = lookup$1[base64.charCodeAt(i + 1)];
- encoded3 = lookup$1[base64.charCodeAt(i + 2)];
- encoded4 = lookup$1[base64.charCodeAt(i + 3)];
- bytes[p++] = encoded1 << 2 | encoded2 >> 4;
- bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2;
- bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63;
- }
- return arraybuffer;
- };
- var withNativeArrayBuffer$1 = typeof ArrayBuffer === "function";
- var decodePacket = function decodePacket(encodedPacket, binaryType) {
- if (typeof encodedPacket !== "string") {
- return {
- type: "message",
- data: mapBinary(encodedPacket, binaryType)
- };
- }
- var type = encodedPacket.charAt(0);
- if (type === "b") {
- return {
- type: "message",
- data: decodeBase64Packet(encodedPacket.substring(1), binaryType)
- };
- }
- var packetType = PACKET_TYPES_REVERSE[type];
- if (!packetType) {
- return ERROR_PACKET;
- }
- return encodedPacket.length > 1 ? {
- type: PACKET_TYPES_REVERSE[type],
- data: encodedPacket.substring(1)
- } : {
- type: PACKET_TYPES_REVERSE[type]
- };
- };
- var decodeBase64Packet = function decodeBase64Packet(data, binaryType) {
- if (withNativeArrayBuffer$1) {
- var decoded = decode$1(data);
- return mapBinary(decoded, binaryType);
- } else {
- return {
- base64: true,
- data: data
- }; // fallback for old browsers
- }
- };
- var mapBinary = function mapBinary(data, binaryType) {
- switch (binaryType) {
- case "blob":
- return data instanceof ArrayBuffer ? new Blob([data]) : data;
- case "arraybuffer":
- default:
- return data;
- // assuming the data is already an ArrayBuffer
- }
- };
- var SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
- var encodePayload = function encodePayload(packets, callback) {
- // some packets may be added to the array while encoding, so the initial length must be saved
- var length = packets.length;
- var encodedPackets = new Array(length);
- var count = 0;
- packets.forEach(function (packet, i) {
- // force base64 encoding for binary packets
- encodePacket(packet, false, function (encodedPacket) {
- encodedPackets[i] = encodedPacket;
- if (++count === length) {
- callback(encodedPackets.join(SEPARATOR));
- }
- });
- });
- };
- var decodePayload = function decodePayload(encodedPayload, binaryType) {
- var encodedPackets = encodedPayload.split(SEPARATOR);
- var packets = [];
- for (var i = 0; i < encodedPackets.length; i++) {
- var decodedPacket = decodePacket(encodedPackets[i], binaryType);
- packets.push(decodedPacket);
- if (decodedPacket.type === "error") {
- break;
- }
- }
- return packets;
- };
- var protocol$1 = 4;
- var Transport = /*#__PURE__*/function (_Emitter) {
- _inherits(Transport, _Emitter);
- var _super = _createSuper(Transport);
- /**
- * Transport abstract constructor.
- *
- * @param {Object} options.
- * @api private
- */
- function Transport(opts) {
- var _this;
- _classCallCheck(this, Transport);
- _this = _super.call(this);
- _this.writable = false;
- installTimerFunctions(_assertThisInitialized(_this), opts);
- _this.opts = opts;
- _this.query = opts.query;
- _this.readyState = "";
- _this.socket = opts.socket;
- return _this;
- }
- /**
- * Emits an error.
- *
- * @param {String} str
- * @return {Transport} for chaining
- * @api protected
- */
- _createClass(Transport, [{
- key: "onError",
- value: function onError(msg, desc) {
- var err = new Error(msg); // @ts-ignore
- err.type = "TransportError"; // @ts-ignore
- err.description = desc;
- _get(_getPrototypeOf(Transport.prototype), "emit", this).call(this, "error", err);
- return this;
- }
- /**
- * Opens the transport.
- *
- * @api public
- */
- }, {
- key: "open",
- value: function open() {
- if ("closed" === this.readyState || "" === this.readyState) {
- this.readyState = "opening";
- this.doOpen();
- }
- return this;
- }
- /**
- * Closes the transport.
- *
- * @api public
- */
- }, {
- key: "close",
- value: function close() {
- if ("opening" === this.readyState || "open" === this.readyState) {
- this.doClose();
- this.onClose();
- }
- return this;
- }
- /**
- * Sends multiple packets.
- *
- * @param {Array} packets
- * @api public
- */
- }, {
- key: "send",
- value: function send(packets) {
- if ("open" === this.readyState) {
- this.write(packets);
- }
- }
- /**
- * Called upon open
- *
- * @api protected
- */
- }, {
- key: "onOpen",
- value: function onOpen() {
- this.readyState = "open";
- this.writable = true;
- _get(_getPrototypeOf(Transport.prototype), "emit", this).call(this, "open");
- }
- /**
- * Called with data.
- *
- * @param {String} data
- * @api protected
- */
- }, {
- key: "onData",
- value: function onData(data) {
- var packet = decodePacket(data, this.socket.binaryType);
- this.onPacket(packet);
- }
- /**
- * Called with a decoded packet.
- *
- * @api protected
- */
- }, {
- key: "onPacket",
- value: function onPacket(packet) {
- _get(_getPrototypeOf(Transport.prototype), "emit", this).call(this, "packet", packet);
- }
- /**
- * Called upon close.
- *
- * @api protected
- */
- }, {
- key: "onClose",
- value: function onClose() {
- this.readyState = "closed";
- _get(_getPrototypeOf(Transport.prototype), "emit", this).call(this, "close");
- }
- }]);
- return Transport;
- }(Emitter_1);
- var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'.split(''),
- length = 64,
- map = {},
- seed = 0,
- i = 0,
- prev;
- /**
- * Return a string representing the specified number.
- *
- * @param {Number} num The number to convert.
- * @returns {String} The string representation of the number.
- * @api public
- */
- function encode(num) {
- var encoded = '';
- do {
- encoded = alphabet[num % length] + encoded;
- num = Math.floor(num / length);
- } while (num > 0);
- return encoded;
- }
- /**
- * Return the integer value specified by the given string.
- *
- * @param {String} str The string to convert.
- * @returns {Number} The integer value represented by the string.
- * @api public
- */
- function decode(str) {
- var decoded = 0;
- for (i = 0; i < str.length; i++) {
- decoded = decoded * length + map[str.charAt(i)];
- }
- return decoded;
- }
- /**
- * Yeast: A tiny growing id generator.
- *
- * @returns {String} A unique id.
- * @api public
- */
- function yeast() {
- var now = encode(+new Date());
- if (now !== prev) return seed = 0, prev = now;
- return now + '.' + encode(seed++);
- } //
- // Map each character to its index.
- //
- for (; i < length; i++) {
- map[alphabet[i]] = i;
- } //
- // Expose the `yeast`, `encode` and `decode` functions.
- //
- yeast.encode = encode;
- yeast.decode = decode;
- var yeast_1 = yeast;
- var parseqs = {};
- /**
- * Compiles a querystring
- * Returns string representation of the object
- *
- * @param {Object}
- * @api private
- */
- parseqs.encode = function (obj) {
- var str = '';
- for (var i in obj) {
- if (obj.hasOwnProperty(i)) {
- if (str.length) str += '&';
- str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
- }
- }
- return str;
- };
- /**
- * Parses a simple querystring into an object
- *
- * @param {String} qs
- * @api private
- */
- parseqs.decode = function (qs) {
- var qry = {};
- var pairs = qs.split('&');
- for (var i = 0, l = pairs.length; i < l; i++) {
- var pair = pairs[i].split('=');
- qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
- }
- return qry;
- };
- var Polling = /*#__PURE__*/function (_Transport) {
- _inherits(Polling, _Transport);
- var _super = _createSuper(Polling);
- function Polling() {
- var _this;
- _classCallCheck(this, Polling);
- _this = _super.apply(this, arguments);
- _this.polling = false;
- return _this;
- }
- /**
- * Transport name.
- */
- _createClass(Polling, [{
- key: "name",
- get: function get() {
- return "polling";
- }
- /**
- * Opens the socket (triggers polling). We write a PING message to determine
- * when the transport is open.
- *
- * @api private
- */
- }, {
- key: "doOpen",
- value: function doOpen() {
- this.poll();
- }
- /**
- * Pauses polling.
- *
- * @param {Function} callback upon buffers are flushed and transport is paused
- * @api private
- */
- }, {
- key: "pause",
- value: function pause(onPause) {
- var _this2 = this;
- this.readyState = "pausing";
- var pause = function pause() {
- _this2.readyState = "paused";
- onPause();
- };
- if (this.polling || !this.writable) {
- var total = 0;
- if (this.polling) {
- total++;
- this.once("pollComplete", function () {
- --total || pause();
- });
- }
- if (!this.writable) {
- total++;
- this.once("drain", function () {
- --total || pause();
- });
- }
- } else {
- pause();
- }
- }
- /**
- * Starts polling cycle.
- *
- * @api public
- */
- }, {
- key: "poll",
- value: function poll() {
- this.polling = true;
- this.doPoll();
- this.emit("poll");
- }
- /**
- * Overloads onData to detect payloads.
- *
- * @api private
- */
- }, {
- key: "onData",
- value: function onData(data) {
- var _this3 = this;
- var callback = function callback(packet) {
- // if its the first message we consider the transport open
- if ("opening" === _this3.readyState && packet.type === "open") {
- _this3.onOpen();
- } // if its a close packet, we close the ongoing requests
- if ("close" === packet.type) {
- _this3.onClose();
- return false;
- } // otherwise bypass onData and handle the message
- _this3.onPacket(packet);
- }; // decode payload
- decodePayload(data, this.socket.binaryType).forEach(callback); // if an event did not trigger closing
- if ("closed" !== this.readyState) {
- // if we got data we're not polling
- this.polling = false;
- this.emit("pollComplete");
- if ("open" === this.readyState) {
- this.poll();
- }
- }
- }
- /**
- * For polling, send a close packet.
- *
- * @api private
- */
- }, {
- key: "doClose",
- value: function doClose() {
- var _this4 = this;
- var close = function close() {
- _this4.write([{
- type: "close"
- }]);
- };
- if ("open" === this.readyState) {
- close();
- } else {
- // in case we're trying to close while
- // handshaking is in progress (GH-164)
- this.once("open", close);
- }
- }
- /**
- * Writes a packets payload.
- *
- * @param {Array} data packets
- * @param {Function} drain callback
- * @api private
- */
- }, {
- key: "write",
- value: function write(packets) {
- var _this5 = this;
- this.writable = false;
- encodePayload(packets, function (data) {
- _this5.doWrite(data, function () {
- _this5.writable = true;
- _this5.emit("drain");
- });
- });
- }
- /**
- * Generates uri for connection.
- *
- * @api private
- */
- }, {
- key: "uri",
- value: function uri() {
- var query = this.query || {};
- var schema = this.opts.secure ? "https" : "http";
- var port = ""; // cache busting is forced
- if (false !== this.opts.timestampRequests) {
- query[this.opts.timestampParam] = yeast_1();
- }
- if (!this.supportsBinary && !query.sid) {
- query.b64 = 1;
- } // avoid port if default for schema
- if (this.opts.port && ("https" === schema && Number(this.opts.port) !== 443 || "http" === schema && Number(this.opts.port) !== 80)) {
- port = ":" + this.opts.port;
- }
- var encodedQuery = parseqs.encode(query);
- var ipv6 = this.opts.hostname.indexOf(":") !== -1;
- return schema + "://" + (ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) + port + this.opts.path + (encodedQuery.length ? "?" + encodedQuery : "");
- }
- }]);
- return Polling;
- }(Transport);
- /**
- * Empty function
- */
- function empty() {}
- var hasXHR2 = function () {
- var xhr = new XMLHttpRequest$1({
- xdomain: false
- });
- return null != xhr.responseType;
- }();
- var XHR = /*#__PURE__*/function (_Polling) {
- _inherits(XHR, _Polling);
- var _super = _createSuper(XHR);
- /**
- * XHR Polling constructor.
- *
- * @param {Object} opts
- * @api public
- */
- function XHR(opts) {
- var _this;
- _classCallCheck(this, XHR);
- _this = _super.call(this, opts);
- if (typeof location !== "undefined") {
- var isSSL = "https:" === location.protocol;
- var port = location.port; // some user agents have empty `location.port`
- if (!port) {
- port = isSSL ? "443" : "80";
- }
- _this.xd = typeof location !== "undefined" && opts.hostname !== location.hostname || port !== opts.port;
- _this.xs = opts.secure !== isSSL;
- }
- /**
- * XHR supports binary
- */
- var forceBase64 = opts && opts.forceBase64;
- _this.supportsBinary = hasXHR2 && !forceBase64;
- return _this;
- }
- /**
- * Creates a request.
- *
- * @param {String} method
- * @api private
- */
- _createClass(XHR, [{
- key: "request",
- value: function request() {
- var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- _extends(opts, {
- xd: this.xd,
- xs: this.xs
- }, this.opts);
- return new Request(this.uri(), opts);
- }
- /**
- * Sends data.
- *
- * @param {String} data to send.
- * @param {Function} called upon flush.
- * @api private
- */
- }, {
- key: "doWrite",
- value: function doWrite(data, fn) {
- var _this2 = this;
- var req = this.request({
- method: "POST",
- data: data
- });
- req.on("success", fn);
- req.on("error", function (err) {
- _this2.onError("xhr post error", err);
- });
- }
- /**
- * Starts a poll cycle.
- *
- * @api private
- */
- }, {
- key: "doPoll",
- value: function doPoll() {
- var _this3 = this;
- var req = this.request();
- req.on("data", this.onData.bind(this));
- req.on("error", function (err) {
- _this3.onError("xhr poll error", err);
- });
- this.pollXhr = req;
- }
- }]);
- return XHR;
- }(Polling);
- var Request = /*#__PURE__*/function (_Emitter) {
- _inherits(Request, _Emitter);
- var _super2 = _createSuper(Request);
- /**
- * Request constructor
- *
- * @param {Object} options
- * @api public
- */
- function Request(uri, opts) {
- var _this4;
- _classCallCheck(this, Request);
- _this4 = _super2.call(this);
- installTimerFunctions(_assertThisInitialized(_this4), opts);
- _this4.opts = opts;
- _this4.method = opts.method || "GET";
- _this4.uri = uri;
- _this4.async = false !== opts.async;
- _this4.data = undefined !== opts.data ? opts.data : null;
- _this4.create();
- return _this4;
- }
- /**
- * Creates the XHR object and sends the request.
- *
- * @api private
- */
- _createClass(Request, [{
- key: "create",
- value: function create() {
- var _this5 = this;
- var opts = pick(this.opts, "agent", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "autoUnref");
- opts.xdomain = !!this.opts.xd;
- opts.xscheme = !!this.opts.xs;
- var xhr = this.xhr = new XMLHttpRequest$1(opts);
- try {
- xhr.open(this.method, this.uri, this.async);
- try {
- if (this.opts.extraHeaders) {
- xhr.setDisableHeaderCheck && xhr.setDisableHeaderCheck(true);
- for (var i in this.opts.extraHeaders) {
- if (this.opts.extraHeaders.hasOwnProperty(i)) {
- xhr.setRequestHeader(i, this.opts.extraHeaders[i]);
- }
- }
- }
- } catch (e) {}
- if ("POST" === this.method) {
- try {
- xhr.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
- } catch (e) {}
- }
- try {
- xhr.setRequestHeader("Accept", "*/*");
- } catch (e) {} // ie6 check
- if ("withCredentials" in xhr) {
- xhr.withCredentials = this.opts.withCredentials;
- }
- if (this.opts.requestTimeout) {
- xhr.timeout = this.opts.requestTimeout;
- }
- xhr.onreadystatechange = function () {
- if (4 !== xhr.readyState) return;
- if (200 === xhr.status || 1223 === xhr.status) {
- _this5.onLoad();
- } else {
- // make sure the `error` event handler that's user-set
- // does not throw in the same tick and gets caught here
- _this5.setTimeoutFn(function () {
- _this5.onError(typeof xhr.status === "number" ? xhr.status : 0);
- }, 0);
- }
- };
- xhr.send(this.data);
- } catch (e) {
- // Need to defer since .create() is called directly from the constructor
- // and thus the 'error' event can only be only bound *after* this exception
- // occurs. Therefore, also, we cannot throw here at all.
- this.setTimeoutFn(function () {
- _this5.onError(e);
- }, 0);
- return;
- }
- if (typeof document !== "undefined") {
- this.index = Request.requestsCount++;
- Request.requests[this.index] = this;
- }
- }
- /**
- * Called upon successful response.
- *
- * @api private
- */
- }, {
- key: "onSuccess",
- value: function onSuccess() {
- this.emit("success");
- this.cleanup();
- }
- /**
- * Called if we have data.
- *
- * @api private
- */
- }, {
- key: "onData",
- value: function onData(data) {
- this.emit("data", data);
- this.onSuccess();
- }
- /**
- * Called upon error.
- *
- * @api private
- */
- }, {
- key: "onError",
- value: function onError(err) {
- this.emit("error", err);
- this.cleanup(true);
- }
- /**
- * Cleans up house.
- *
- * @api private
- */
- }, {
- key: "cleanup",
- value: function cleanup(fromError) {
- if ("undefined" === typeof this.xhr || null === this.xhr) {
- return;
- }
- this.xhr.onreadystatechange = empty;
- if (fromError) {
- try {
- this.xhr.abort();
- } catch (e) {}
- }
- if (typeof document !== "undefined") {
- delete Request.requests[this.index];
- }
- this.xhr = null;
- }
- /**
- * Called upon load.
- *
- * @api private
- */
- }, {
- key: "onLoad",
- value: function onLoad() {
- var data = this.xhr.responseText;
- if (data !== null) {
- this.onData(data);
- }
- }
- /**
- * Aborts the request.
- *
- * @api public
- */
- }, {
- key: "abort",
- value: function abort() {
- this.cleanup();
- }
- }]);
- return Request;
- }(Emitter_1);
- Request.requestsCount = 0;
- Request.requests = {};
- /**
- * Aborts pending requests when unloading the window. This is needed to prevent
- * memory leaks (e.g. when using IE) and to ensure that no spurious error is
- * emitted.
- */
- if (typeof document !== "undefined") {
- // @ts-ignore
- if (typeof attachEvent === "function") {
- // @ts-ignore
- attachEvent("onunload", unloadHandler);
- } else if (typeof addEventListener === "function") {
- var terminationEvent = "onpagehide" in globalThis ? "pagehide" : "unload";
- addEventListener(terminationEvent, unloadHandler, false);
- }
- }
- function unloadHandler() {
- for (var i in Request.requests) {
- if (Request.requests.hasOwnProperty(i)) {
- Request.requests[i].abort();
- }
- }
- }
- var nextTick = function () {
- var isPromiseAvailable = typeof Promise === "function" && typeof Promise.resolve === "function";
- if (isPromiseAvailable) {
- return function (cb) {
- return Promise.resolve().then(cb);
- };
- } else {
- return function (cb, setTimeoutFn) {
- return setTimeoutFn(cb, 0);
- };
- }
- }();
- var WebSocket = globalThis.WebSocket || globalThis.MozWebSocket;
- var usingBrowserWebSocket = true;
- var defaultBinaryType = "arraybuffer";
- var isReactNative = typeof navigator !== "undefined" && typeof navigator.product === "string" && navigator.product.toLowerCase() === "reactnative";
- var WS = /*#__PURE__*/function (_Transport) {
- _inherits(WS, _Transport);
- var _super = _createSuper(WS);
- /**
- * WebSocket transport constructor.
- *
- * @api {Object} connection options
- * @api public
- */
- function WS(opts) {
- var _this;
- _classCallCheck(this, WS);
- _this = _super.call(this, opts);
- _this.supportsBinary = !opts.forceBase64;
- return _this;
- }
- /**
- * Transport name.
- *
- * @api public
- */
- _createClass(WS, [{
- key: "name",
- get: function get() {
- return "websocket";
- }
- /**
- * Opens socket.
- *
- * @api private
- */
- }, {
- key: "doOpen",
- value: function doOpen() {
- if (!this.check()) {
- // let probe timeout
- return;
- }
- var uri = this.uri();
- var protocols = this.opts.protocols; // React Native only supports the 'headers' option, and will print a warning if anything else is passed
- var opts = isReactNative ? {} : pick(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
- if (this.opts.extraHeaders) {
- opts.headers = this.opts.extraHeaders;
- }
- try {
- this.ws = usingBrowserWebSocket && !isReactNative ? protocols ? new WebSocket(uri, protocols) : new WebSocket(uri) : new WebSocket(uri, protocols, opts);
- } catch (err) {
- return this.emit("error", err);
- }
- this.ws.binaryType = this.socket.binaryType || defaultBinaryType;
- this.addEventListeners();
- }
- /**
- * Adds event listeners to the socket
- *
- * @api private
- */
- }, {
- key: "addEventListeners",
- value: function addEventListeners() {
- var _this2 = this;
- this.ws.onopen = function () {
- if (_this2.opts.autoUnref) {
- _this2.ws._socket.unref();
- }
- _this2.onOpen();
- };
- this.ws.onclose = this.onClose.bind(this);
- this.ws.onmessage = function (ev) {
- return _this2.onData(ev.data);
- };
- this.ws.onerror = function (e) {
- return _this2.onError("websocket error", e);
- };
- }
- /**
- * Writes data to socket.
- *
- * @param {Array} array of packets.
- * @api private
- */
- }, {
- key: "write",
- value: function write(packets) {
- var _this3 = this;
- this.writable = false; // encodePacket efficient as it uses WS framing
- // no need for encodePayload
- var _loop = function _loop(i) {
- var packet = packets[i];
- var lastPacket = i === packets.length - 1;
- encodePacket(packet, _this3.supportsBinary, function (data) {
- // always create a new object (GH-437)
- var opts = {};
- // have a chance of informing us about it yet, in that case send will
- // throw an error
- try {
- if (usingBrowserWebSocket) {
- // TypeError is thrown when passing the second argument on Safari
- _this3.ws.send(data);
- }
- } catch (e) {}
- if (lastPacket) {
- // fake drain
- // defer to next tick to allow Socket to clear writeBuffer
- nextTick(function () {
- _this3.writable = true;
- _this3.emit("drain");
- }, _this3.setTimeoutFn);
- }
- });
- };
- for (var i = 0; i < packets.length; i++) {
- _loop(i);
- }
- }
- /**
- * Closes socket.
- *
- * @api private
- */
- }, {
- key: "doClose",
- value: function doClose() {
- if (typeof this.ws !== "undefined") {
- this.ws.close();
- this.ws = null;
- }
- }
- /**
- * Generates uri for connection.
- *
- * @api private
- */
- }, {
- key: "uri",
- value: function uri() {
- var query = this.query || {};
- var schema = this.opts.secure ? "wss" : "ws";
- var port = ""; // avoid port if default for schema
- if (this.opts.port && ("wss" === schema && Number(this.opts.port) !== 443 || "ws" === schema && Number(this.opts.port) !== 80)) {
- port = ":" + this.opts.port;
- } // append timestamp to URI
- if (this.opts.timestampRequests) {
- query[this.opts.timestampParam] = yeast_1();
- } // communicate binary support capabilities
- if (!this.supportsBinary) {
- query.b64 = 1;
- }
- var encodedQuery = parseqs.encode(query);
- var ipv6 = this.opts.hostname.indexOf(":") !== -1;
- return schema + "://" + (ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) + port + this.opts.path + (encodedQuery.length ? "?" + encodedQuery : "");
- }
- /**
- * Feature detection for WebSocket.
- *
- * @return {Boolean} whether this transport is available.
- * @api public
- */
- }, {
- key: "check",
- value: function check() {
- return !!WebSocket && !("__initialize" in WebSocket && this.name === WS.prototype.name);
- }
- }]);
- return WS;
- }(Transport);
- var transports = {
- websocket: WS,
- polling: XHR
- };
- var Socket$1 = /*#__PURE__*/function (_Emitter) {
- _inherits(Socket, _Emitter);
- var _super = _createSuper(Socket);
- /**
- * Socket constructor.
- *
- * @param {String|Object} uri or options
- * @param {Object} opts - options
- * @api public
- */
- function Socket(uri) {
- var _this;
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- _classCallCheck(this, Socket);
- _this = _super.call(this);
- if (uri && "object" === _typeof(uri)) {
- opts = uri;
- uri = null;
- }
- if (uri) {
- uri = parseuri(uri);
- opts.hostname = uri.host;
- opts.secure = uri.protocol === "https" || uri.protocol === "wss";
- opts.port = uri.port;
- if (uri.query) opts.query = uri.query;
- } else if (opts.host) {
- opts.hostname = parseuri(opts.host).host;
- }
- installTimerFunctions(_assertThisInitialized(_this), opts);
- _this.secure = null != opts.secure ? opts.secure : typeof location !== "undefined" && "https:" === location.protocol;
- if (opts.hostname && !opts.port) {
- // if no port is specified manually, use the protocol default
- opts.port = _this.secure ? "443" : "80";
- }
- _this.hostname = opts.hostname || (typeof location !== "undefined" ? location.hostname : "localhost");
- _this.port = opts.port || (typeof location !== "undefined" && location.port ? location.port : _this.secure ? "443" : "80");
- _this.transports = opts.transports || ["polling", "websocket"];
- _this.readyState = "";
- _this.writeBuffer = [];
- _this.prevBufferLen = 0;
- _this.opts = _extends({
- path: "/engine.io",
- agent: false,
- withCredentials: false,
- upgrade: true,
- timestampParam: "t",
- rememberUpgrade: false,
- rejectUnauthorized: true,
- perMessageDeflate: {
- threshold: 1024
- },
- transportOptions: {},
- closeOnBeforeunload: true
- }, opts);
- _this.opts.path = _this.opts.path.replace(/\/$/, "") + "/";
- if (typeof _this.opts.query === "string") {
- _this.opts.query = parseqs.decode(_this.opts.query);
- } // set on handshake
- _this.id = null;
- _this.upgrades = null;
- _this.pingInterval = null;
- _this.pingTimeout = null; // set on heartbeat
- _this.pingTimeoutTimer = null;
- if (typeof addEventListener === "function") {
- if (_this.opts.closeOnBeforeunload) {
- // Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
- // ensures every browser behaves the same (no "disconnect" event at the Socket.IO level when the page is
- // closed/reloaded)
- addEventListener("beforeunload", function () {
- if (_this.transport) {
- // silently close the transport
- _this.transport.removeAllListeners();
- _this.transport.close();
- }
- }, false);
- }
- if (_this.hostname !== "localhost") {
- _this.offlineEventListener = function () {
- _this.onClose("transport close");
- };
- addEventListener("offline", _this.offlineEventListener, false);
- }
- }
- _this.open();
- return _this;
- }
- /**
- * Creates transport of the given type.
- *
- * @param {String} transport name
- * @return {Transport}
- * @api private
- */
- _createClass(Socket, [{
- key: "createTransport",
- value: function createTransport(name) {
- var query = clone(this.opts.query); // append engine.io protocol identifier
- query.EIO = protocol$1; // transport name
- query.transport = name; // session id if we already have one
- if (this.id) query.sid = this.id;
- var opts = _extends({}, this.opts.transportOptions[name], this.opts, {
- query: query,
- socket: this,
- hostname: this.hostname,
- secure: this.secure,
- port: this.port
- });
- return new transports[name](opts);
- }
- /**
- * Initializes transport to use and starts probe.
- *
- * @api private
- */
- }, {
- key: "open",
- value: function open() {
- var _this2 = this;
- var transport;
- if (this.opts.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf("websocket") !== -1) {
- transport = "websocket";
- } else if (0 === this.transports.length) {
- // Emit error on next tick so it can be listened to
- this.setTimeoutFn(function () {
- _this2.emitReserved("error", "No transports available");
- }, 0);
- return;
- } else {
- transport = this.transports[0];
- }
- this.readyState = "opening"; // Retry with the next transport if the transport is disabled (jsonp: false)
- try {
- transport = this.createTransport(transport);
- } catch (e) {
- this.transports.shift();
- this.open();
- return;
- }
- transport.open();
- this.setTransport(transport);
- }
- /**
- * Sets the current transport. Disables the existing one (if any).
- *
- * @api private
- */
- }, {
- key: "setTransport",
- value: function setTransport(transport) {
- var _this3 = this;
- if (this.transport) {
- this.transport.removeAllListeners();
- } // set up transport
- this.transport = transport; // set up transport listeners
- transport.on("drain", this.onDrain.bind(this)).on("packet", this.onPacket.bind(this)).on("error", this.onError.bind(this)).on("close", function () {
- _this3.onClose("transport close");
- });
- }
- /**
- * Probes a transport.
- *
- * @param {String} transport name
- * @api private
- */
- }, {
- key: "probe",
- value: function probe(name) {
- var _this4 = this;
- var transport = this.createTransport(name);
- var failed = false;
- Socket.priorWebsocketSuccess = false;
- var onTransportOpen = function onTransportOpen() {
- if (failed) return;
- transport.send([{
- type: "ping",
- data: "probe"
- }]);
- transport.once("packet", function (msg) {
- if (failed) return;
- if ("pong" === msg.type && "probe" === msg.data) {
- _this4.upgrading = true;
- _this4.emitReserved("upgrading", transport);
- if (!transport) return;
- Socket.priorWebsocketSuccess = "websocket" === transport.name;
- _this4.transport.pause(function () {
- if (failed) return;
- if ("closed" === _this4.readyState) return;
- cleanup();
- _this4.setTransport(transport);
- transport.send([{
- type: "upgrade"
- }]);
- _this4.emitReserved("upgrade", transport);
- transport = null;
- _this4.upgrading = false;
- _this4.flush();
- });
- } else {
- var err = new Error("probe error"); // @ts-ignore
- err.transport = transport.name;
- _this4.emitReserved("upgradeError", err);
- }
- });
- };
- function freezeTransport() {
- if (failed) return; // Any callback called by transport should be ignored since now
- failed = true;
- cleanup();
- transport.close();
- transport = null;
- } // Handle any error that happens while probing
- var onerror = function onerror(err) {
- var error = new Error("probe error: " + err); // @ts-ignore
- error.transport = transport.name;
- freezeTransport();
- _this4.emitReserved("upgradeError", error);
- };
- function onTransportClose() {
- onerror("transport closed");
- } // When the socket is closed while we're probing
- function onclose() {
- onerror("socket closed");
- } // When the socket is upgraded while we're probing
- function onupgrade(to) {
- if (transport && to.name !== transport.name) {
- freezeTransport();
- }
- } // Remove all listeners on the transport and on self
- var cleanup = function cleanup() {
- transport.removeListener("open", onTransportOpen);
- transport.removeListener("error", onerror);
- transport.removeListener("close", onTransportClose);
- _this4.off("close", onclose);
- _this4.off("upgrading", onupgrade);
- };
- transport.once("open", onTransportOpen);
- transport.once("error", onerror);
- transport.once("close", onTransportClose);
- this.once("close", onclose);
- this.once("upgrading", onupgrade);
- transport.open();
- }
- /**
- * Called when connection is deemed open.
- *
- * @api private
- */
- }, {
- key: "onOpen",
- value: function onOpen() {
- this.readyState = "open";
- Socket.priorWebsocketSuccess = "websocket" === this.transport.name;
- this.emitReserved("open");
- this.flush(); // we check for `readyState` in case an `open`
- // listener already closed the socket
- if ("open" === this.readyState && this.opts.upgrade && this.transport.pause) {
- var i = 0;
- var l = this.upgrades.length;
- for (; i < l; i++) {
- this.probe(this.upgrades[i]);
- }
- }
- }
- /**
- * Handles a packet.
- *
- * @api private
- */
- }, {
- key: "onPacket",
- value: function onPacket(packet) {
- if ("opening" === this.readyState || "open" === this.readyState || "closing" === this.readyState) {
- this.emitReserved("packet", packet); // Socket is live - any packet counts
- this.emitReserved("heartbeat");
- switch (packet.type) {
- case "open":
- this.onHandshake(JSON.parse(packet.data));
- break;
- case "ping":
- this.resetPingTimeout();
- this.sendPacket("pong");
- this.emitReserved("ping");
- this.emitReserved("pong");
- break;
- case "error":
- var err = new Error("server error"); // @ts-ignore
- err.code = packet.data;
- this.onError(err);
- break;
- case "message":
- this.emitReserved("data", packet.data);
- this.emitReserved("message", packet.data);
- break;
- }
- }
- }
- /**
- * Called upon handshake completion.
- *
- * @param {Object} data - handshake obj
- * @api private
- */
- }, {
- key: "onHandshake",
- value: function onHandshake(data) {
- this.emitReserved("handshake", data);
- this.id = data.sid;
- this.transport.query.sid = data.sid;
- this.upgrades = this.filterUpgrades(data.upgrades);
- this.pingInterval = data.pingInterval;
- this.pingTimeout = data.pingTimeout;
- this.onOpen(); // In case open handler closes socket
- if ("closed" === this.readyState) return;
- this.resetPingTimeout();
- }
- /**
- * Sets and resets ping timeout timer based on server pings.
- *
- * @api private
- */
- }, {
- key: "resetPingTimeout",
- value: function resetPingTimeout() {
- var _this5 = this;
- this.clearTimeoutFn(this.pingTimeoutTimer);
- this.pingTimeoutTimer = this.setTimeoutFn(function () {
- _this5.onClose("ping timeout");
- }, this.pingInterval + this.pingTimeout);
- if (this.opts.autoUnref) {
- this.pingTimeoutTimer.unref();
- }
- }
- /**
- * Called on `drain` event
- *
- * @api private
- */
- }, {
- key: "onDrain",
- value: function onDrain() {
- this.writeBuffer.splice(0, this.prevBufferLen); // setting prevBufferLen = 0 is very important
- // for example, when upgrading, upgrade packet is sent over,
- // and a nonzero prevBufferLen could cause problems on `drain`
- this.prevBufferLen = 0;
- if (0 === this.writeBuffer.length) {
- this.emitReserved("drain");
- } else {
- this.flush();
- }
- }
- /**
- * Flush write buffers.
- *
- * @api private
- */
- }, {
- key: "flush",
- value: function flush() {
- if ("closed" !== this.readyState && this.transport.writable && !this.upgrading && this.writeBuffer.length) {
- this.transport.send(this.writeBuffer); // keep track of current length of writeBuffer
- // splice writeBuffer and callbackBuffer on `drain`
- this.prevBufferLen = this.writeBuffer.length;
- this.emitReserved("flush");
- }
- }
- /**
- * Sends a message.
- *
- * @param {String} message.
- * @param {Function} callback function.
- * @param {Object} options.
- * @return {Socket} for chaining.
- * @api public
- */
- }, {
- key: "write",
- value: function write(msg, options, fn) {
- this.sendPacket("message", msg, options, fn);
- return this;
- }
- }, {
- key: "send",
- value: function send(msg, options, fn) {
- this.sendPacket("message", msg, options, fn);
- return this;
- }
- /**
- * Sends a packet.
- *
- * @param {String} packet type.
- * @param {String} data.
- * @param {Object} options.
- * @param {Function} callback function.
- * @api private
- */
- }, {
- key: "sendPacket",
- value: function sendPacket(type, data, options, fn) {
- if ("function" === typeof data) {
- fn = data;
- data = undefined;
- }
- if ("function" === typeof options) {
- fn = options;
- options = null;
- }
- if ("closing" === this.readyState || "closed" === this.readyState) {
- return;
- }
- options = options || {};
- options.compress = false !== options.compress;
- var packet = {
- type: type,
- data: data,
- options: options
- };
- this.emitReserved("packetCreate", packet);
- this.writeBuffer.push(packet);
- if (fn) this.once("flush", fn);
- this.flush();
- }
- /**
- * Closes the connection.
- *
- * @api public
- */
- }, {
- key: "close",
- value: function close() {
- var _this6 = this;
- var close = function close() {
- _this6.onClose("forced close");
- _this6.transport.close();
- };
- var cleanupAndClose = function cleanupAndClose() {
- _this6.off("upgrade", cleanupAndClose);
- _this6.off("upgradeError", cleanupAndClose);
- close();
- };
- var waitForUpgrade = function waitForUpgrade() {
- // wait for upgrade to finish since we can't send packets while pausing a transport
- _this6.once("upgrade", cleanupAndClose);
- _this6.once("upgradeError", cleanupAndClose);
- };
- if ("opening" === this.readyState || "open" === this.readyState) {
- this.readyState = "closing";
- if (this.writeBuffer.length) {
- this.once("drain", function () {
- if (_this6.upgrading) {
- waitForUpgrade();
- } else {
- close();
- }
- });
- } else if (this.upgrading) {
- waitForUpgrade();
- } else {
- close();
- }
- }
- return this;
- }
- /**
- * Called upon transport error
- *
- * @api private
- */
- }, {
- key: "onError",
- value: function onError(err) {
- Socket.priorWebsocketSuccess = false;
- this.emitReserved("error", err);
- this.onClose("transport error", err);
- }
- /**
- * Called upon transport close.
- *
- * @api private
- */
- }, {
- key: "onClose",
- value: function onClose(reason, desc) {
- if ("opening" === this.readyState || "open" === this.readyState || "closing" === this.readyState) {
- // clear timers
- this.clearTimeoutFn(this.pingTimeoutTimer); // stop event from firing again for transport
- this.transport.removeAllListeners("close"); // ensure transport won't stay open
- this.transport.close(); // ignore further transport communication
- this.transport.removeAllListeners();
- if (typeof removeEventListener === "function") {
- removeEventListener("offline", this.offlineEventListener, false);
- } // set ready state
- this.readyState = "closed"; // clear session id
- this.id = null; // emit close event
- this.emitReserved("close", reason, desc); // clean buffers after, so users can still
- // grab the buffers on `close` event
- this.writeBuffer = [];
- this.prevBufferLen = 0;
- }
- }
- /**
- * Filters upgrades, returning only those matching client transports.
- *
- * @param {Array} server upgrades
- * @api private
- *
- */
- }, {
- key: "filterUpgrades",
- value: function filterUpgrades(upgrades) {
- var filteredUpgrades = [];
- var i = 0;
- var j = upgrades.length;
- for (; i < j; i++) {
- if (~this.transports.indexOf(upgrades[i])) filteredUpgrades.push(upgrades[i]);
- }
- return filteredUpgrades;
- }
- }]);
- return Socket;
- }(Emitter_1);
- Socket$1.protocol = protocol$1;
- function clone(obj) {
- var o = {};
- for (var i in obj) {
- if (obj.hasOwnProperty(i)) {
- o[i] = obj[i];
- }
- }
- return o;
- }
- var withNativeArrayBuffer = typeof ArrayBuffer === "function";
- var isView = function isView(obj) {
- return typeof ArrayBuffer.isView === "function" ? ArrayBuffer.isView(obj) : obj.buffer instanceof ArrayBuffer;
- };
- var toString = Object.prototype.toString;
- var withNativeBlob = typeof Blob === "function" || typeof Blob !== "undefined" && toString.call(Blob) === "[object BlobConstructor]";
- var withNativeFile = typeof File === "function" || typeof File !== "undefined" && toString.call(File) === "[object FileConstructor]";
- /**
- * Returns true if obj is a Buffer, an ArrayBuffer, a Blob or a File.
- *
- * @private
- */
- function isBinary(obj) {
- return withNativeArrayBuffer && (obj instanceof ArrayBuffer || isView(obj)) || withNativeBlob && obj instanceof Blob || withNativeFile && obj instanceof File;
- }
- function hasBinary(obj, toJSON) {
- if (!obj || _typeof(obj) !== "object") {
- return false;
- }
- if (Array.isArray(obj)) {
- for (var i = 0, l = obj.length; i < l; i++) {
- if (hasBinary(obj[i])) {
- return true;
- }
- }
- return false;
- }
- if (isBinary(obj)) {
- return true;
- }
- if (obj.toJSON && typeof obj.toJSON === "function" && arguments.length === 1) {
- return hasBinary(obj.toJSON(), true);
- }
- for (var key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
- return true;
- }
- }
- return false;
- }
- /**
- * Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
- *
- * @param {Object} packet - socket.io event packet
- * @return {Object} with deconstructed packet and list of buffers
- * @public
- */
- function deconstructPacket(packet) {
- var buffers = [];
- var packetData = packet.data;
- var pack = packet;
- pack.data = _deconstructPacket(packetData, buffers);
- pack.attachments = buffers.length; // number of binary 'attachments'
- return {
- packet: pack,
- buffers: buffers
- };
- }
- function _deconstructPacket(data, buffers) {
- if (!data) return data;
- if (isBinary(data)) {
- var placeholder = {
- _placeholder: true,
- num: buffers.length
- };
- buffers.push(data);
- return placeholder;
- } else if (Array.isArray(data)) {
- var newData = new Array(data.length);
- for (var i = 0; i < data.length; i++) {
- newData[i] = _deconstructPacket(data[i], buffers);
- }
- return newData;
- } else if (_typeof(data) === "object" && !(data instanceof Date)) {
- var _newData = {};
- for (var key in data) {
- if (data.hasOwnProperty(key)) {
- _newData[key] = _deconstructPacket(data[key], buffers);
- }
- }
- return _newData;
- }
- return data;
- }
- /**
- * Reconstructs a binary packet from its placeholder packet and buffers
- *
- * @param {Object} packet - event packet with placeholders
- * @param {Array} buffers - binary buffers to put in placeholder positions
- * @return {Object} reconstructed packet
- * @public
- */
- function reconstructPacket(packet, buffers) {
- packet.data = _reconstructPacket(packet.data, buffers);
- packet.attachments = undefined; // no longer useful
- return packet;
- }
- function _reconstructPacket(data, buffers) {
- if (!data) return data;
- if (data && data._placeholder) {
- return buffers[data.num]; // appropriate buffer (should be natural order anyway)
- } else if (Array.isArray(data)) {
- for (var i = 0; i < data.length; i++) {
- data[i] = _reconstructPacket(data[i], buffers);
- }
- } else if (_typeof(data) === "object") {
- for (var key in data) {
- if (data.hasOwnProperty(key)) {
- data[key] = _reconstructPacket(data[key], buffers);
- }
- }
- }
- return data;
- }
- /**
- * Protocol version.
- *
- * @public
- */
- var protocol = 5;
- var PacketType;
- (function (PacketType) {
- PacketType[PacketType["CONNECT"] = 0] = "CONNECT";
- PacketType[PacketType["DISCONNECT"] = 1] = "DISCONNECT";
- PacketType[PacketType["EVENT"] = 2] = "EVENT";
- PacketType[PacketType["ACK"] = 3] = "ACK";
- PacketType[PacketType["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
- PacketType[PacketType["BINARY_EVENT"] = 5] = "BINARY_EVENT";
- PacketType[PacketType["BINARY_ACK"] = 6] = "BINARY_ACK";
- })(PacketType || (PacketType = {}));
- /**
- * A socket.io Encoder instance
- */
- var Encoder = /*#__PURE__*/function () {
- function Encoder() {
- _classCallCheck(this, Encoder);
- }
- _createClass(Encoder, [{
- key: "encode",
- value:
- /**
- * Encode a packet as a single string if non-binary, or as a
- * buffer sequence, depending on packet type.
- *
- * @param {Object} obj - packet object
- */
- function encode(obj) {
- if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) {
- if (hasBinary(obj)) {
- obj.type = obj.type === PacketType.EVENT ? PacketType.BINARY_EVENT : PacketType.BINARY_ACK;
- return this.encodeAsBinary(obj);
- }
- }
- return [this.encodeAsString(obj)];
- }
- /**
- * Encode packet as string.
- */
- }, {
- key: "encodeAsString",
- value: function encodeAsString(obj) {
- // first is type
- var str = "" + obj.type; // attachments if we have them
- if (obj.type === PacketType.BINARY_EVENT || obj.type === PacketType.BINARY_ACK) {
- str += obj.attachments + "-";
- } // if we have a namespace other than `/`
- // we append it followed by a comma `,`
- if (obj.nsp && "/" !== obj.nsp) {
- str += obj.nsp + ",";
- } // immediately followed by the id
- if (null != obj.id) {
- str += obj.id;
- } // json data
- if (null != obj.data) {
- str += JSON.stringify(obj.data);
- }
- return str;
- }
- /**
- * Encode packet as 'buffer sequence' by removing blobs, and
- * deconstructing packet into object with placeholders and
- * a list of buffers.
- */
- }, {
- key: "encodeAsBinary",
- value: function encodeAsBinary(obj) {
- var deconstruction = deconstructPacket(obj);
- var pack = this.encodeAsString(deconstruction.packet);
- var buffers = deconstruction.buffers;
- buffers.unshift(pack); // add packet info to beginning of data list
- return buffers; // write all the buffers
- }
- }]);
- return Encoder;
- }();
- /**
- * A socket.io Decoder instance
- *
- * @return {Object} decoder
- */
- var Decoder = /*#__PURE__*/function (_Emitter) {
- _inherits(Decoder, _Emitter);
- var _super = _createSuper(Decoder);
- function Decoder() {
- _classCallCheck(this, Decoder);
- return _super.call(this);
- }
- /**
- * Decodes an encoded packet string into packet JSON.
- *
- * @param {String} obj - encoded packet
- */
- _createClass(Decoder, [{
- key: "add",
- value: function add(obj) {
- var packet;
- if (typeof obj === "string") {
- packet = this.decodeString(obj);
- if (packet.type === PacketType.BINARY_EVENT || packet.type === PacketType.BINARY_ACK) {
- // binary packet's json
- this.reconstructor = new BinaryReconstructor(packet); // no attachments, labeled binary but no binary data to follow
- if (packet.attachments === 0) {
- _get(_getPrototypeOf(Decoder.prototype), "emitReserved", this).call(this, "decoded", packet);
- }
- } else {
- // non-binary full packet
- _get(_getPrototypeOf(Decoder.prototype), "emitReserved", this).call(this, "decoded", packet);
- }
- } else if (isBinary(obj) || obj.base64) {
- // raw binary data
- if (!this.reconstructor) {
- throw new Error("got binary data when not reconstructing a packet");
- } else {
- packet = this.reconstructor.takeBinaryData(obj);
- if (packet) {
- // received final buffer
- this.reconstructor = null;
- _get(_getPrototypeOf(Decoder.prototype), "emitReserved", this).call(this, "decoded", packet);
- }
- }
- } else {
- throw new Error("Unknown type: " + obj);
- }
- }
- /**
- * Decode a packet String (JSON data)
- *
- * @param {String} str
- * @return {Object} packet
- */
- }, {
- key: "decodeString",
- value: function decodeString(str) {
- var i = 0; // look up type
- var p = {
- type: Number(str.charAt(0))
- };
- if (PacketType[p.type] === undefined) {
- throw new Error("unknown packet type " + p.type);
- } // look up attachments if type binary
- if (p.type === PacketType.BINARY_EVENT || p.type === PacketType.BINARY_ACK) {
- var start = i + 1;
- while (str.charAt(++i) !== "-" && i != str.length) {}
- var buf = str.substring(start, i);
- if (buf != Number(buf) || str.charAt(i) !== "-") {
- throw new Error("Illegal attachments");
- }
- p.attachments = Number(buf);
- } // look up namespace (if any)
- if ("/" === str.charAt(i + 1)) {
- var _start = i + 1;
- while (++i) {
- var c = str.charAt(i);
- if ("," === c) break;
- if (i === str.length) break;
- }
- p.nsp = str.substring(_start, i);
- } else {
- p.nsp = "/";
- } // look up id
- var next = str.charAt(i + 1);
- if ("" !== next && Number(next) == next) {
- var _start2 = i + 1;
- while (++i) {
- var _c = str.charAt(i);
- if (null == _c || Number(_c) != _c) {
- --i;
- break;
- }
- if (i === str.length) break;
- }
- p.id = Number(str.substring(_start2, i + 1));
- } // look up json data
- if (str.charAt(++i)) {
- var payload = tryParse(str.substr(i));
- if (Decoder.isPayloadValid(p.type, payload)) {
- p.data = payload;
- } else {
- throw new Error("invalid payload");
- }
- }
- return p;
- }
- }, {
- key: "destroy",
- value:
- /**
- * Deallocates a parser's resources
- */
- function destroy() {
- if (this.reconstructor) {
- this.reconstructor.finishedReconstruction();
- }
- }
- }], [{
- key: "isPayloadValid",
- value: function isPayloadValid(type, payload) {
- switch (type) {
- case PacketType.CONNECT:
- return _typeof(payload) === "object";
- case PacketType.DISCONNECT:
- return payload === undefined;
- case PacketType.CONNECT_ERROR:
- return typeof payload === "string" || _typeof(payload) === "object";
- case PacketType.EVENT:
- case PacketType.BINARY_EVENT:
- return Array.isArray(payload) && payload.length > 0;
- case PacketType.ACK:
- case PacketType.BINARY_ACK:
- return Array.isArray(payload);
- }
- }
- }]);
- return Decoder;
- }(Emitter_1);
- function tryParse(str) {
- try {
- return JSON.parse(str);
- } catch (e) {
- return false;
- }
- }
- /**
- * A manager of a binary event's 'buffer sequence'. Should
- * be constructed whenever a packet of type BINARY_EVENT is
- * decoded.
- *
- * @param {Object} packet
- * @return {BinaryReconstructor} initialized reconstructor
- */
- var BinaryReconstructor = /*#__PURE__*/function () {
- function BinaryReconstructor(packet) {
- _classCallCheck(this, BinaryReconstructor);
- this.packet = packet;
- this.buffers = [];
- this.reconPack = packet;
- }
- /**
- * Method to be called when binary data received from connection
- * after a BINARY_EVENT packet.
- *
- * @param {Buffer | ArrayBuffer} binData - the raw binary data received
- * @return {null | Object} returns null if more binary data is expected or
- * a reconstructed packet object if all buffers have been received.
- */
- _createClass(BinaryReconstructor, [{
- key: "takeBinaryData",
- value: function takeBinaryData(binData) {
- this.buffers.push(binData);
- if (this.buffers.length === this.reconPack.attachments) {
- // done with buffer list
- var packet = reconstructPacket(this.reconPack, this.buffers);
- this.finishedReconstruction();
- return packet;
- }
- return null;
- }
- /**
- * Cleans up binary packet reconstruction variables.
- */
- }, {
- key: "finishedReconstruction",
- value: function finishedReconstruction() {
- this.reconPack = null;
- this.buffers = [];
- }
- }]);
- return BinaryReconstructor;
- }();
- var parser = /*#__PURE__*/Object.freeze({
- __proto__: null,
- protocol: protocol,
- get PacketType () { return PacketType; },
- Encoder: Encoder,
- Decoder: Decoder
- });
- function on(obj, ev, fn) {
- obj.on(ev, fn);
- return function subDestroy() {
- obj.off(ev, fn);
- };
- }
- /**
- * Internal events.
- * These events can't be emitted by the user.
- */
- var RESERVED_EVENTS = Object.freeze({
- connect: 1,
- connect_error: 1,
- disconnect: 1,
- disconnecting: 1,
- // EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
- newListener: 1,
- removeListener: 1
- });
- var Socket = /*#__PURE__*/function (_Emitter) {
- _inherits(Socket, _Emitter);
- var _super = _createSuper(Socket);
- /**
- * `Socket` constructor.
- *
- * @public
- */
- function Socket(io, nsp, opts) {
- var _this;
- _classCallCheck(this, Socket);
- _this = _super.call(this);
- _this.connected = false;
- _this.disconnected = true;
- _this.receiveBuffer = [];
- _this.sendBuffer = [];
- _this.ids = 0;
- _this.acks = {};
- _this.flags = {};
- _this.io = io;
- _this.nsp = nsp;
- if (opts && opts.auth) {
- _this.auth = opts.auth;
- }
- if (_this.io._autoConnect) _this.open();
- return _this;
- }
- /**
- * Subscribe to open, close and packet events
- *
- * @private
- */
- _createClass(Socket, [{
- key: "subEvents",
- value: function subEvents() {
- if (this.subs) return;
- var io = this.io;
- this.subs = [on(io, "open", this.onopen.bind(this)), on(io, "packet", this.onpacket.bind(this)), on(io, "error", this.onerror.bind(this)), on(io, "close", this.onclose.bind(this))];
- }
- /**
- * Whether the Socket will try to reconnect when its Manager connects or reconnects
- */
- }, {
- key: "active",
- get: function get() {
- return !!this.subs;
- }
- /**
- * "Opens" the socket.
- *
- * @public
- */
- }, {
- key: "connect",
- value: function connect() {
- if (this.connected) return this;
- this.subEvents();
- if (!this.io["_reconnecting"]) this.io.open(); // ensure open
- if ("open" === this.io._readyState) this.onopen();
- return this;
- }
- /**
- * Alias for connect()
- */
- }, {
- key: "open",
- value: function open() {
- return this.connect();
- }
- /**
- * Sends a `message` event.
- *
- * @return self
- * @public
- */
- }, {
- key: "send",
- value: function send() {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- args.unshift("message");
- this.emit.apply(this, args);
- return this;
- }
- /**
- * Override `emit`.
- * If the event is in `events`, it's emitted normally.
- *
- * @return self
- * @public
- */
- }, {
- key: "emit",
- value: function emit(ev) {
- if (RESERVED_EVENTS.hasOwnProperty(ev)) {
- throw new Error('"' + ev + '" is a reserved event name');
- }
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
- args[_key2 - 1] = arguments[_key2];
- }
- args.unshift(ev);
- var packet = {
- type: PacketType.EVENT,
- data: args
- };
- packet.options = {};
- packet.options.compress = this.flags.compress !== false; // event ack callback
- if ("function" === typeof args[args.length - 1]) {
- var id = this.ids++;
- var ack = args.pop();
- this._registerAckCallback(id, ack);
- packet.id = id;
- }
- var isTransportWritable = this.io.engine && this.io.engine.transport && this.io.engine.transport.writable;
- var discardPacket = this.flags["volatile"] && (!isTransportWritable || !this.connected);
- if (discardPacket) ; else if (this.connected) {
- this.packet(packet);
- } else {
- this.sendBuffer.push(packet);
- }
- this.flags = {};
- return this;
- }
- /**
- * @private
- */
- }, {
- key: "_registerAckCallback",
- value: function _registerAckCallback(id, ack) {
- var _this2 = this;
- var timeout = this.flags.timeout;
- if (timeout === undefined) {
- this.acks[id] = ack;
- return;
- } // @ts-ignore
- var timer = this.io.setTimeoutFn(function () {
- delete _this2.acks[id];
- for (var i = 0; i < _this2.sendBuffer.length; i++) {
- if (_this2.sendBuffer[i].id === id) {
- _this2.sendBuffer.splice(i, 1);
- }
- }
- ack.call(_this2, new Error("operation has timed out"));
- }, timeout);
- this.acks[id] = function () {
- // @ts-ignore
- _this2.io.clearTimeoutFn(timer);
- for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
- args[_key3] = arguments[_key3];
- }
- ack.apply(_this2, [null].concat(args));
- };
- }
- /**
- * Sends a packet.
- *
- * @param packet
- * @private
- */
- }, {
- key: "packet",
- value: function packet(_packet) {
- _packet.nsp = this.nsp;
- this.io._packet(_packet);
- }
- /**
- * Called upon engine `open`.
- *
- * @private
- */
- }, {
- key: "onopen",
- value: function onopen() {
- var _this3 = this;
- if (typeof this.auth == "function") {
- this.auth(function (data) {
- _this3.packet({
- type: PacketType.CONNECT,
- data: data
- });
- });
- } else {
- this.packet({
- type: PacketType.CONNECT,
- data: this.auth
- });
- }
- }
- /**
- * Called upon engine or manager `error`.
- *
- * @param err
- * @private
- */
- }, {
- key: "onerror",
- value: function onerror(err) {
- if (!this.connected) {
- this.emitReserved("connect_error", err);
- }
- }
- /**
- * Called upon engine `close`.
- *
- * @param reason
- * @private
- */
- }, {
- key: "onclose",
- value: function onclose(reason) {
- this.connected = false;
- this.disconnected = true;
- delete this.id;
- this.emitReserved("disconnect", reason);
- }
- /**
- * Called with socket packet.
- *
- * @param packet
- * @private
- */
- }, {
- key: "onpacket",
- value: function onpacket(packet) {
- var sameNamespace = packet.nsp === this.nsp;
- if (!sameNamespace) return;
- switch (packet.type) {
- case PacketType.CONNECT:
- if (packet.data && packet.data.sid) {
- var id = packet.data.sid;
- this.onconnect(id);
- } else {
- this.emitReserved("connect_error", new Error("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)"));
- }
- break;
- case PacketType.EVENT:
- this.onevent(packet);
- break;
- case PacketType.BINARY_EVENT:
- this.onevent(packet);
- break;
- case PacketType.ACK:
- this.onack(packet);
- break;
- case PacketType.BINARY_ACK:
- this.onack(packet);
- break;
- case PacketType.DISCONNECT:
- this.ondisconnect();
- break;
- case PacketType.CONNECT_ERROR:
- this.destroy();
- var err = new Error(packet.data.message); // @ts-ignore
- err.data = packet.data.data;
- this.emitReserved("connect_error", err);
- break;
- }
- }
- /**
- * Called upon a server event.
- *
- * @param packet
- * @private
- */
- }, {
- key: "onevent",
- value: function onevent(packet) {
- var args = packet.data || [];
- if (null != packet.id) {
- args.push(this.ack(packet.id));
- }
- if (this.connected) {
- this.emitEvent(args);
- } else {
- this.receiveBuffer.push(Object.freeze(args));
- }
- }
- }, {
- key: "emitEvent",
- value: function emitEvent(args) {
- if (this._anyListeners && this._anyListeners.length) {
- var listeners = this._anyListeners.slice();
- var _iterator = _createForOfIteratorHelper(listeners),
- _step;
- try {
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
- var listener = _step.value;
- listener.apply(this, args);
- }
- } catch (err) {
- _iterator.e(err);
- } finally {
- _iterator.f();
- }
- }
- _get(_getPrototypeOf(Socket.prototype), "emit", this).apply(this, args);
- }
- /**
- * Produces an ack callback to emit with an event.
- *
- * @private
- */
- }, {
- key: "ack",
- value: function ack(id) {
- var self = this;
- var sent = false;
- return function () {
- // prevent double callbacks
- if (sent) return;
- sent = true;
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
- args[_key4] = arguments[_key4];
- }
- self.packet({
- type: PacketType.ACK,
- id: id,
- data: args
- });
- };
- }
- /**
- * Called upon a server acknowlegement.
- *
- * @param packet
- * @private
- */
- }, {
- key: "onack",
- value: function onack(packet) {
- var ack = this.acks[packet.id];
- if ("function" === typeof ack) {
- ack.apply(this, packet.data);
- delete this.acks[packet.id];
- }
- }
- /**
- * Called upon server connect.
- *
- * @private
- */
- }, {
- key: "onconnect",
- value: function onconnect(id) {
- this.id = id;
- this.connected = true;
- this.disconnected = false;
- this.emitBuffered();
- this.emitReserved("connect");
- }
- /**
- * Emit buffered events (received and emitted).
- *
- * @private
- */
- }, {
- key: "emitBuffered",
- value: function emitBuffered() {
- var _this4 = this;
- this.receiveBuffer.forEach(function (args) {
- return _this4.emitEvent(args);
- });
- this.receiveBuffer = [];
- this.sendBuffer.forEach(function (packet) {
- return _this4.packet(packet);
- });
- this.sendBuffer = [];
- }
- /**
- * Called upon server disconnect.
- *
- * @private
- */
- }, {
- key: "ondisconnect",
- value: function ondisconnect() {
- this.destroy();
- this.onclose("io server disconnect");
- }
- /**
- * Called upon forced client/server side disconnections,
- * this method ensures the manager stops tracking us and
- * that reconnections don't get triggered for this.
- *
- * @private
- */
- }, {
- key: "destroy",
- value: function destroy() {
- if (this.subs) {
- // clean subscriptions to avoid reconnections
- this.subs.forEach(function (subDestroy) {
- return subDestroy();
- });
- this.subs = undefined;
- }
- this.io["_destroy"](this);
- }
- /**
- * Disconnects the socket manually.
- *
- * @return self
- * @public
- */
- }, {
- key: "disconnect",
- value: function disconnect() {
- if (this.connected) {
- this.packet({
- type: PacketType.DISCONNECT
- });
- } // remove socket from pool
- this.destroy();
- if (this.connected) {
- // fire events
- this.onclose("io client disconnect");
- }
- return this;
- }
- /**
- * Alias for disconnect()
- *
- * @return self
- * @public
- */
- }, {
- key: "close",
- value: function close() {
- return this.disconnect();
- }
- /**
- * Sets the compress flag.
- *
- * @param compress - if `true`, compresses the sending data
- * @return self
- * @public
- */
- }, {
- key: "compress",
- value: function compress(_compress) {
- this.flags.compress = _compress;
- return this;
- }
- /**
- * Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not
- * ready to send messages.
- *
- * @returns self
- * @public
- */
- }, {
- key: "volatile",
- get: function get() {
- this.flags["volatile"] = true;
- return this;
- }
- /**
- * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
- * given number of milliseconds have elapsed without an acknowledgement from the server:
- *
- * ```
- * socket.timeout(5000).emit("my-event", (err) => {
- * if (err) {
- * // the server did not acknowledge the event in the given delay
- * }
- * });
- * ```
- *
- * @returns self
- * @public
- */
- }, {
- key: "timeout",
- value: function timeout(_timeout) {
- this.flags.timeout = _timeout;
- return this;
- }
- /**
- * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
- * callback.
- *
- * @param listener
- * @public
- */
- }, {
- key: "onAny",
- value: function onAny(listener) {
- this._anyListeners = this._anyListeners || [];
- this._anyListeners.push(listener);
- return this;
- }
- /**
- * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
- * callback. The listener is added to the beginning of the listeners array.
- *
- * @param listener
- * @public
- */
- }, {
- key: "prependAny",
- value: function prependAny(listener) {
- this._anyListeners = this._anyListeners || [];
- this._anyListeners.unshift(listener);
- return this;
- }
- /**
- * Removes the listener that will be fired when any event is emitted.
- *
- * @param listener
- * @public
- */
- }, {
- key: "offAny",
- value: function offAny(listener) {
- if (!this._anyListeners) {
- return this;
- }
- if (listener) {
- var listeners = this._anyListeners;
- for (var i = 0; i < listeners.length; i++) {
- if (listener === listeners[i]) {
- listeners.splice(i, 1);
- return this;
- }
- }
- } else {
- this._anyListeners = [];
- }
- return this;
- }
- /**
- * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
- * e.g. to remove listeners.
- *
- * @public
- */
- }, {
- key: "listenersAny",
- value: function listenersAny() {
- return this._anyListeners || [];
- }
- }]);
- return Socket;
- }(Emitter_1);
- /**
- * Expose `Backoff`.
- */
- var backo2 = Backoff;
- /**
- * Initialize backoff timer with `opts`.
- *
- * - `min` initial timeout in milliseconds [100]
- * - `max` max timeout [10000]
- * - `jitter` [0]
- * - `factor` [2]
- *
- * @param {Object} opts
- * @api public
- */
- function Backoff(opts) {
- opts = opts || {};
- this.ms = opts.min || 100;
- this.max = opts.max || 10000;
- this.factor = opts.factor || 2;
- this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
- this.attempts = 0;
- }
- /**
- * Return the backoff duration.
- *
- * @return {Number}
- * @api public
- */
- Backoff.prototype.duration = function () {
- var ms = this.ms * Math.pow(this.factor, this.attempts++);
- if (this.jitter) {
- var rand = Math.random();
- var deviation = Math.floor(rand * this.jitter * ms);
- ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
- }
- return Math.min(ms, this.max) | 0;
- };
- /**
- * Reset the number of attempts.
- *
- * @api public
- */
- Backoff.prototype.reset = function () {
- this.attempts = 0;
- };
- /**
- * Set the minimum duration
- *
- * @api public
- */
- Backoff.prototype.setMin = function (min) {
- this.ms = min;
- };
- /**
- * Set the maximum duration
- *
- * @api public
- */
- Backoff.prototype.setMax = function (max) {
- this.max = max;
- };
- /**
- * Set the jitter
- *
- * @api public
- */
- Backoff.prototype.setJitter = function (jitter) {
- this.jitter = jitter;
- };
- var Manager = /*#__PURE__*/function (_Emitter) {
- _inherits(Manager, _Emitter);
- var _super = _createSuper(Manager);
- function Manager(uri, opts) {
- var _this;
- _classCallCheck(this, Manager);
- var _a;
- _this = _super.call(this);
- _this.nsps = {};
- _this.subs = [];
- if (uri && "object" === _typeof(uri)) {
- opts = uri;
- uri = undefined;
- }
- opts = opts || {};
- opts.path = opts.path || "/socket.io";
- _this.opts = opts;
- installTimerFunctions(_assertThisInitialized(_this), opts);
- _this.reconnection(opts.reconnection !== false);
- _this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
- _this.reconnectionDelay(opts.reconnectionDelay || 1000);
- _this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
- _this.randomizationFactor((_a = opts.randomizationFactor) !== null && _a !== void 0 ? _a : 0.5);
- _this.backoff = new backo2({
- min: _this.reconnectionDelay(),
- max: _this.reconnectionDelayMax(),
- jitter: _this.randomizationFactor()
- });
- _this.timeout(null == opts.timeout ? 20000 : opts.timeout);
- _this._readyState = "closed";
- _this.uri = uri;
- var _parser = opts.parser || parser;
- _this.encoder = new _parser.Encoder();
- _this.decoder = new _parser.Decoder();
- _this._autoConnect = opts.autoConnect !== false;
- if (_this._autoConnect) _this.open();
- return _this;
- }
- _createClass(Manager, [{
- key: "reconnection",
- value: function reconnection(v) {
- if (!arguments.length) return this._reconnection;
- this._reconnection = !!v;
- return this;
- }
- }, {
- key: "reconnectionAttempts",
- value: function reconnectionAttempts(v) {
- if (v === undefined) return this._reconnectionAttempts;
- this._reconnectionAttempts = v;
- return this;
- }
- }, {
- key: "reconnectionDelay",
- value: function reconnectionDelay(v) {
- var _a;
- if (v === undefined) return this._reconnectionDelay;
- this._reconnectionDelay = v;
- (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMin(v);
- return this;
- }
- }, {
- key: "randomizationFactor",
- value: function randomizationFactor(v) {
- var _a;
- if (v === undefined) return this._randomizationFactor;
- this._randomizationFactor = v;
- (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setJitter(v);
- return this;
- }
- }, {
- key: "reconnectionDelayMax",
- value: function reconnectionDelayMax(v) {
- var _a;
- if (v === undefined) return this._reconnectionDelayMax;
- this._reconnectionDelayMax = v;
- (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMax(v);
- return this;
- }
- }, {
- key: "timeout",
- value: function timeout(v) {
- if (!arguments.length) return this._timeout;
- this._timeout = v;
- return this;
- }
- /**
- * Starts trying to reconnect if reconnection is enabled and we have not
- * started reconnecting yet
- *
- * @private
- */
- }, {
- key: "maybeReconnectOnOpen",
- value: function maybeReconnectOnOpen() {
- // Only try to reconnect if it's the first time we're connecting
- if (!this._reconnecting && this._reconnection && this.backoff.attempts === 0) {
- // keeps reconnection from firing twice for the same reconnection loop
- this.reconnect();
- }
- }
- /**
- * Sets the current transport `socket`.
- *
- * @param {Function} fn - optional, callback
- * @return self
- * @public
- */
- }, {
- key: "open",
- value: function open(fn) {
- var _this2 = this;
- if (~this._readyState.indexOf("open")) return this;
- this.engine = new Socket$1(this.uri, this.opts);
- var socket = this.engine;
- var self = this;
- this._readyState = "opening";
- this.skipReconnect = false; // emit `open`
- var openSubDestroy = on(socket, "open", function () {
- self.onopen();
- fn && fn();
- }); // emit `error`
- var errorSub = on(socket, "error", function (err) {
- self.cleanup();
- self._readyState = "closed";
- _this2.emitReserved("error", err);
- if (fn) {
- fn(err);
- } else {
- // Only do this if there is no fn to handle the error
- self.maybeReconnectOnOpen();
- }
- });
- if (false !== this._timeout) {
- var timeout = this._timeout;
- if (timeout === 0) {
- openSubDestroy(); // prevents a race condition with the 'open' event
- } // set timer
- var timer = this.setTimeoutFn(function () {
- openSubDestroy();
- socket.close(); // @ts-ignore
- socket.emit("error", new Error("timeout"));
- }, timeout);
- if (this.opts.autoUnref) {
- timer.unref();
- }
- this.subs.push(function subDestroy() {
- clearTimeout(timer);
- });
- }
- this.subs.push(openSubDestroy);
- this.subs.push(errorSub);
- return this;
- }
- /**
- * Alias for open()
- *
- * @return self
- * @public
- */
- }, {
- key: "connect",
- value: function connect(fn) {
- return this.open(fn);
- }
- /**
- * Called upon transport open.
- *
- * @private
- */
- }, {
- key: "onopen",
- value: function onopen() {
- // clear old subs
- this.cleanup(); // mark as open
- this._readyState = "open";
- this.emitReserved("open"); // add new subs
- var socket = this.engine;
- this.subs.push(on(socket, "ping", this.onping.bind(this)), on(socket, "data", this.ondata.bind(this)), on(socket, "error", this.onerror.bind(this)), on(socket, "close", this.onclose.bind(this)), on(this.decoder, "decoded", this.ondecoded.bind(this)));
- }
- /**
- * Called upon a ping.
- *
- * @private
- */
- }, {
- key: "onping",
- value: function onping() {
- this.emitReserved("ping");
- }
- /**
- * Called with data.
- *
- * @private
- */
- }, {
- key: "ondata",
- value: function ondata(data) {
- this.decoder.add(data);
- }
- /**
- * Called when parser fully decodes a packet.
- *
- * @private
- */
- }, {
- key: "ondecoded",
- value: function ondecoded(packet) {
- this.emitReserved("packet", packet);
- }
- /**
- * Called upon socket error.
- *
- * @private
- */
- }, {
- key: "onerror",
- value: function onerror(err) {
- this.emitReserved("error", err);
- }
- /**
- * Creates a new socket for the given `nsp`.
- *
- * @return {Socket}
- * @public
- */
- }, {
- key: "socket",
- value: function socket(nsp, opts) {
- var socket = this.nsps[nsp];
- if (!socket) {
- socket = new Socket(this, nsp, opts);
- this.nsps[nsp] = socket;
- }
- return socket;
- }
- /**
- * Called upon a socket close.
- *
- * @param socket
- * @private
- */
- }, {
- key: "_destroy",
- value: function _destroy(socket) {
- var nsps = Object.keys(this.nsps);
- for (var _i = 0, _nsps = nsps; _i < _nsps.length; _i++) {
- var nsp = _nsps[_i];
- var _socket = this.nsps[nsp];
- if (_socket.active) {
- return;
- }
- }
- this._close();
- }
- /**
- * Writes a packet.
- *
- * @param packet
- * @private
- */
- }, {
- key: "_packet",
- value: function _packet(packet) {
- var encodedPackets = this.encoder.encode(packet);
- for (var i = 0; i < encodedPackets.length; i++) {
- this.engine.write(encodedPackets[i], packet.options);
- }
- }
- /**
- * Clean up transport subscriptions and packet buffer.
- *
- * @private
- */
- }, {
- key: "cleanup",
- value: function cleanup() {
- this.subs.forEach(function (subDestroy) {
- return subDestroy();
- });
- this.subs.length = 0;
- this.decoder.destroy();
- }
- /**
- * Close the current socket.
- *
- * @private
- */
- }, {
- key: "_close",
- value: function _close() {
- this.skipReconnect = true;
- this._reconnecting = false;
- this.onclose("forced close");
- if (this.engine) this.engine.close();
- }
- /**
- * Alias for close()
- *
- * @private
- */
- }, {
- key: "disconnect",
- value: function disconnect() {
- return this._close();
- }
- /**
- * Called upon engine close.
- *
- * @private
- */
- }, {
- key: "onclose",
- value: function onclose(reason) {
- this.cleanup();
- this.backoff.reset();
- this._readyState = "closed";
- this.emitReserved("close", reason);
- if (this._reconnection && !this.skipReconnect) {
- this.reconnect();
- }
- }
- /**
- * Attempt a reconnection.
- *
- * @private
- */
- }, {
- key: "reconnect",
- value: function reconnect() {
- var _this3 = this;
- if (this._reconnecting || this.skipReconnect) return this;
- var self = this;
- if (this.backoff.attempts >= this._reconnectionAttempts) {
- this.backoff.reset();
- this.emitReserved("reconnect_failed");
- this._reconnecting = false;
- } else {
- var delay = this.backoff.duration();
- this._reconnecting = true;
- var timer = this.setTimeoutFn(function () {
- if (self.skipReconnect) return;
- _this3.emitReserved("reconnect_attempt", self.backoff.attempts); // check again for the case socket closed in above events
- if (self.skipReconnect) return;
- self.open(function (err) {
- if (err) {
- self._reconnecting = false;
- self.reconnect();
- _this3.emitReserved("reconnect_error", err);
- } else {
- self.onreconnect();
- }
- });
- }, delay);
- if (this.opts.autoUnref) {
- timer.unref();
- }
- this.subs.push(function subDestroy() {
- clearTimeout(timer);
- });
- }
- }
- /**
- * Called upon successful reconnect.
- *
- * @private
- */
- }, {
- key: "onreconnect",
- value: function onreconnect() {
- var attempt = this.backoff.attempts;
- this._reconnecting = false;
- this.backoff.reset();
- this.emitReserved("reconnect", attempt);
- }
- }]);
- return Manager;
- }(Emitter_1);
- /**
- * Managers cache.
- */
- var cache = {};
- function lookup(uri, opts) {
- if (_typeof(uri) === "object") {
- opts = uri;
- uri = undefined;
- }
- opts = opts || {};
- var parsed = url(uri, opts.path || "/socket.io");
- var source = parsed.source;
- var id = parsed.id;
- var path = parsed.path;
- var sameNamespace = cache[id] && path in cache[id]["nsps"];
- var newConnection = opts.forceNew || opts["force new connection"] || false === opts.multiplex || sameNamespace;
- var io;
- if (newConnection) {
- io = new Manager(source, opts);
- } else {
- if (!cache[id]) {
- cache[id] = new Manager(source, opts);
- }
- io = cache[id];
- }
- if (parsed.query && !opts.query) {
- opts.query = parsed.queryKey;
- }
- return io.socket(parsed.path, opts);
- } // so that "lookup" can be used both as a function (e.g. `io(...)`) and as a
- // namespace (e.g. `io.connect(...)`), for backward compatibility
- _extends(lookup, {
- Manager: Manager,
- Socket: Socket,
- io: lookup,
- connect: lookup
- });
- return lookup;
- }));
- //# sourceMappingURL=socket.io.js.map
|