socket.io.slim.dev.js 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837
  1. /*!
  2. * Socket.IO v2.2.0
  3. * (c) 2014-2018 Guillermo Rauch
  4. * Released under the MIT License.
  5. */
  6. (function webpackUniversalModuleDefinition(root, factory) {
  7. if(typeof exports === 'object' && typeof module === 'object')
  8. module.exports = factory();
  9. else if(typeof define === 'function' && define.amd)
  10. define([], factory);
  11. else if(typeof exports === 'object')
  12. exports["io"] = factory();
  13. else
  14. root["io"] = factory();
  15. })(this, function() {
  16. return /******/ (function(modules) { // webpackBootstrap
  17. /******/ // The module cache
  18. /******/ var installedModules = {};
  19. /******/
  20. /******/ // The require function
  21. /******/ function __webpack_require__(moduleId) {
  22. /******/
  23. /******/ // Check if module is in cache
  24. /******/ if(installedModules[moduleId])
  25. /******/ return installedModules[moduleId].exports;
  26. /******/
  27. /******/ // Create a new module (and put it into the cache)
  28. /******/ var module = installedModules[moduleId] = {
  29. /******/ exports: {},
  30. /******/ id: moduleId,
  31. /******/ loaded: false
  32. /******/ };
  33. /******/
  34. /******/ // Execute the module function
  35. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  36. /******/
  37. /******/ // Flag the module as loaded
  38. /******/ module.loaded = true;
  39. /******/
  40. /******/ // Return the exports of the module
  41. /******/ return module.exports;
  42. /******/ }
  43. /******/
  44. /******/
  45. /******/ // expose the modules object (__webpack_modules__)
  46. /******/ __webpack_require__.m = modules;
  47. /******/
  48. /******/ // expose the module cache
  49. /******/ __webpack_require__.c = installedModules;
  50. /******/
  51. /******/ // __webpack_public_path__
  52. /******/ __webpack_require__.p = "";
  53. /******/
  54. /******/ // Load entry module and return exports
  55. /******/ return __webpack_require__(0);
  56. /******/ })
  57. /************************************************************************/
  58. /******/ ([
  59. /* 0 */
  60. /***/ (function(module, exports, __webpack_require__) {
  61. 'use strict';
  62. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  63. /**
  64. * Module dependencies.
  65. */
  66. var url = __webpack_require__(1);
  67. var parser = __webpack_require__(4);
  68. var Manager = __webpack_require__(9);
  69. var debug = __webpack_require__(3)('socket.io-client');
  70. /**
  71. * Module exports.
  72. */
  73. module.exports = exports = lookup;
  74. /**
  75. * Managers cache.
  76. */
  77. var cache = exports.managers = {};
  78. /**
  79. * Looks up an existing `Manager` for multiplexing.
  80. * If the user summons:
  81. *
  82. * `io('http://localhost/a');`
  83. * `io('http://localhost/b');`
  84. *
  85. * We reuse the existing instance based on same scheme/port/host,
  86. * and we initialize sockets for each namespace.
  87. *
  88. * @api public
  89. */
  90. function lookup(uri, opts) {
  91. if ((typeof uri === 'undefined' ? 'undefined' : _typeof(uri)) === 'object') {
  92. opts = uri;
  93. uri = undefined;
  94. }
  95. opts = opts || {};
  96. var parsed = url(uri);
  97. var source = parsed.source;
  98. var id = parsed.id;
  99. var path = parsed.path;
  100. var sameNamespace = cache[id] && path in cache[id].nsps;
  101. var newConnection = opts.forceNew || opts['force new connection'] || false === opts.multiplex || sameNamespace;
  102. var io;
  103. if (newConnection) {
  104. io = Manager(source, opts);
  105. } else {
  106. if (!cache[id]) {
  107. cache[id] = Manager(source, opts);
  108. }
  109. io = cache[id];
  110. }
  111. if (parsed.query && !opts.query) {
  112. opts.query = parsed.query;
  113. }
  114. return io.socket(parsed.path, opts);
  115. }
  116. /**
  117. * Protocol version.
  118. *
  119. * @api public
  120. */
  121. exports.protocol = parser.protocol;
  122. /**
  123. * `connect`.
  124. *
  125. * @param {String} uri
  126. * @api public
  127. */
  128. exports.connect = lookup;
  129. /**
  130. * Expose constructors for standalone build.
  131. *
  132. * @api public
  133. */
  134. exports.Manager = __webpack_require__(9);
  135. exports.Socket = __webpack_require__(33);
  136. /***/ }),
  137. /* 1 */
  138. /***/ (function(module, exports, __webpack_require__) {
  139. 'use strict';
  140. /**
  141. * Module dependencies.
  142. */
  143. var parseuri = __webpack_require__(2);
  144. var debug = __webpack_require__(3)('socket.io-client:url');
  145. /**
  146. * Module exports.
  147. */
  148. module.exports = url;
  149. /**
  150. * URL parser.
  151. *
  152. * @param {String} url
  153. * @param {Object} An object meant to mimic window.location.
  154. * Defaults to window.location.
  155. * @api public
  156. */
  157. function url(uri, loc) {
  158. var obj = uri;
  159. // default to window.location
  160. loc = loc || typeof location !== 'undefined' && location;
  161. if (null == uri) uri = loc.protocol + '//' + loc.host;
  162. // relative path support
  163. if ('string' === typeof uri) {
  164. if ('/' === uri.charAt(0)) {
  165. if ('/' === uri.charAt(1)) {
  166. uri = loc.protocol + uri;
  167. } else {
  168. uri = loc.host + uri;
  169. }
  170. }
  171. if (!/^(https?|wss?):\/\//.test(uri)) {
  172. if ('undefined' !== typeof loc) {
  173. uri = loc.protocol + '//' + uri;
  174. } else {
  175. uri = 'https://' + uri;
  176. }
  177. }
  178. // parse
  179. obj = parseuri(uri);
  180. }
  181. // make sure we treat `localhost:80` and `localhost` equally
  182. if (!obj.port) {
  183. if (/^(http|ws)$/.test(obj.protocol)) {
  184. obj.port = '80';
  185. } else if (/^(http|ws)s$/.test(obj.protocol)) {
  186. obj.port = '443';
  187. }
  188. }
  189. obj.path = obj.path || '/';
  190. var ipv6 = obj.host.indexOf(':') !== -1;
  191. var host = ipv6 ? '[' + obj.host + ']' : obj.host;
  192. // define unique id
  193. obj.id = obj.protocol + '://' + host + ':' + obj.port;
  194. // define href
  195. obj.href = obj.protocol + '://' + host + (loc && loc.port === obj.port ? '' : ':' + obj.port);
  196. return obj;
  197. }
  198. /***/ }),
  199. /* 2 */
  200. /***/ (function(module, exports) {
  201. /**
  202. * Parses an URI
  203. *
  204. * @author Steven Levithan <stevenlevithan.com> (MIT license)
  205. * @api private
  206. */
  207. var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  208. var parts = [
  209. 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
  210. ];
  211. module.exports = function parseuri(str) {
  212. var src = str,
  213. b = str.indexOf('['),
  214. e = str.indexOf(']');
  215. if (b != -1 && e != -1) {
  216. str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
  217. }
  218. var m = re.exec(str || ''),
  219. uri = {},
  220. i = 14;
  221. while (i--) {
  222. uri[parts[i]] = m[i] || '';
  223. }
  224. if (b != -1 && e != -1) {
  225. uri.source = src;
  226. uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
  227. uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
  228. uri.ipv6uri = true;
  229. }
  230. return uri;
  231. };
  232. /***/ }),
  233. /* 3 */
  234. /***/ (function(module, exports) {
  235. "use strict";
  236. module.exports = function () {
  237. return function () {};
  238. };
  239. /***/ }),
  240. /* 4 */
  241. /***/ (function(module, exports, __webpack_require__) {
  242. /**
  243. * Module dependencies.
  244. */
  245. var debug = __webpack_require__(3)('socket.io-parser');
  246. var Emitter = __webpack_require__(5);
  247. var binary = __webpack_require__(6);
  248. var isArray = __webpack_require__(7);
  249. var isBuf = __webpack_require__(8);
  250. /**
  251. * Protocol version.
  252. *
  253. * @api public
  254. */
  255. exports.protocol = 4;
  256. /**
  257. * Packet types.
  258. *
  259. * @api public
  260. */
  261. exports.types = [
  262. 'CONNECT',
  263. 'DISCONNECT',
  264. 'EVENT',
  265. 'ACK',
  266. 'ERROR',
  267. 'BINARY_EVENT',
  268. 'BINARY_ACK'
  269. ];
  270. /**
  271. * Packet type `connect`.
  272. *
  273. * @api public
  274. */
  275. exports.CONNECT = 0;
  276. /**
  277. * Packet type `disconnect`.
  278. *
  279. * @api public
  280. */
  281. exports.DISCONNECT = 1;
  282. /**
  283. * Packet type `event`.
  284. *
  285. * @api public
  286. */
  287. exports.EVENT = 2;
  288. /**
  289. * Packet type `ack`.
  290. *
  291. * @api public
  292. */
  293. exports.ACK = 3;
  294. /**
  295. * Packet type `error`.
  296. *
  297. * @api public
  298. */
  299. exports.ERROR = 4;
  300. /**
  301. * Packet type 'binary event'
  302. *
  303. * @api public
  304. */
  305. exports.BINARY_EVENT = 5;
  306. /**
  307. * Packet type `binary ack`. For acks with binary arguments.
  308. *
  309. * @api public
  310. */
  311. exports.BINARY_ACK = 6;
  312. /**
  313. * Encoder constructor.
  314. *
  315. * @api public
  316. */
  317. exports.Encoder = Encoder;
  318. /**
  319. * Decoder constructor.
  320. *
  321. * @api public
  322. */
  323. exports.Decoder = Decoder;
  324. /**
  325. * A socket.io Encoder instance
  326. *
  327. * @api public
  328. */
  329. function Encoder() {}
  330. var ERROR_PACKET = exports.ERROR + '"encode error"';
  331. /**
  332. * Encode a packet as a single string if non-binary, or as a
  333. * buffer sequence, depending on packet type.
  334. *
  335. * @param {Object} obj - packet object
  336. * @param {Function} callback - function to handle encodings (likely engine.write)
  337. * @return Calls callback with Array of encodings
  338. * @api public
  339. */
  340. Encoder.prototype.encode = function(obj, callback){
  341. if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) {
  342. encodeAsBinary(obj, callback);
  343. } else {
  344. var encoding = encodeAsString(obj);
  345. callback([encoding]);
  346. }
  347. };
  348. /**
  349. * Encode packet as string.
  350. *
  351. * @param {Object} packet
  352. * @return {String} encoded
  353. * @api private
  354. */
  355. function encodeAsString(obj) {
  356. // first is type
  357. var str = '' + obj.type;
  358. // attachments if we have them
  359. if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) {
  360. str += obj.attachments + '-';
  361. }
  362. // if we have a namespace other than `/`
  363. // we append it followed by a comma `,`
  364. if (obj.nsp && '/' !== obj.nsp) {
  365. str += obj.nsp + ',';
  366. }
  367. // immediately followed by the id
  368. if (null != obj.id) {
  369. str += obj.id;
  370. }
  371. // json data
  372. if (null != obj.data) {
  373. var payload = tryStringify(obj.data);
  374. if (payload !== false) {
  375. str += payload;
  376. } else {
  377. return ERROR_PACKET;
  378. }
  379. }
  380. return str;
  381. }
  382. function tryStringify(str) {
  383. try {
  384. return JSON.stringify(str);
  385. } catch(e){
  386. return false;
  387. }
  388. }
  389. /**
  390. * Encode packet as 'buffer sequence' by removing blobs, and
  391. * deconstructing packet into object with placeholders and
  392. * a list of buffers.
  393. *
  394. * @param {Object} packet
  395. * @return {Buffer} encoded
  396. * @api private
  397. */
  398. function encodeAsBinary(obj, callback) {
  399. function writeEncoding(bloblessData) {
  400. var deconstruction = binary.deconstructPacket(bloblessData);
  401. var pack = encodeAsString(deconstruction.packet);
  402. var buffers = deconstruction.buffers;
  403. buffers.unshift(pack); // add packet info to beginning of data list
  404. callback(buffers); // write all the buffers
  405. }
  406. binary.removeBlobs(obj, writeEncoding);
  407. }
  408. /**
  409. * A socket.io Decoder instance
  410. *
  411. * @return {Object} decoder
  412. * @api public
  413. */
  414. function Decoder() {
  415. this.reconstructor = null;
  416. }
  417. /**
  418. * Mix in `Emitter` with Decoder.
  419. */
  420. Emitter(Decoder.prototype);
  421. /**
  422. * Decodes an encoded packet string into packet JSON.
  423. *
  424. * @param {String} obj - encoded packet
  425. * @return {Object} packet
  426. * @api public
  427. */
  428. Decoder.prototype.add = function(obj) {
  429. var packet;
  430. if (typeof obj === 'string') {
  431. packet = decodeString(obj);
  432. if (exports.BINARY_EVENT === packet.type || exports.BINARY_ACK === packet.type) { // binary packet's json
  433. this.reconstructor = new BinaryReconstructor(packet);
  434. // no attachments, labeled binary but no binary data to follow
  435. if (this.reconstructor.reconPack.attachments === 0) {
  436. this.emit('decoded', packet);
  437. }
  438. } else { // non-binary full packet
  439. this.emit('decoded', packet);
  440. }
  441. } else if (isBuf(obj) || obj.base64) { // raw binary data
  442. if (!this.reconstructor) {
  443. throw new Error('got binary data when not reconstructing a packet');
  444. } else {
  445. packet = this.reconstructor.takeBinaryData(obj);
  446. if (packet) { // received final buffer
  447. this.reconstructor = null;
  448. this.emit('decoded', packet);
  449. }
  450. }
  451. } else {
  452. throw new Error('Unknown type: ' + obj);
  453. }
  454. };
  455. /**
  456. * Decode a packet String (JSON data)
  457. *
  458. * @param {String} str
  459. * @return {Object} packet
  460. * @api private
  461. */
  462. function decodeString(str) {
  463. var i = 0;
  464. // look up type
  465. var p = {
  466. type: Number(str.charAt(0))
  467. };
  468. if (null == exports.types[p.type]) {
  469. return error('unknown packet type ' + p.type);
  470. }
  471. // look up attachments if type binary
  472. if (exports.BINARY_EVENT === p.type || exports.BINARY_ACK === p.type) {
  473. var buf = '';
  474. while (str.charAt(++i) !== '-') {
  475. buf += str.charAt(i);
  476. if (i == str.length) break;
  477. }
  478. if (buf != Number(buf) || str.charAt(i) !== '-') {
  479. throw new Error('Illegal attachments');
  480. }
  481. p.attachments = Number(buf);
  482. }
  483. // look up namespace (if any)
  484. if ('/' === str.charAt(i + 1)) {
  485. p.nsp = '';
  486. while (++i) {
  487. var c = str.charAt(i);
  488. if (',' === c) break;
  489. p.nsp += c;
  490. if (i === str.length) break;
  491. }
  492. } else {
  493. p.nsp = '/';
  494. }
  495. // look up id
  496. var next = str.charAt(i + 1);
  497. if ('' !== next && Number(next) == next) {
  498. p.id = '';
  499. while (++i) {
  500. var c = str.charAt(i);
  501. if (null == c || Number(c) != c) {
  502. --i;
  503. break;
  504. }
  505. p.id += str.charAt(i);
  506. if (i === str.length) break;
  507. }
  508. p.id = Number(p.id);
  509. }
  510. // look up json data
  511. if (str.charAt(++i)) {
  512. var payload = tryParse(str.substr(i));
  513. var isPayloadValid = payload !== false && (p.type === exports.ERROR || isArray(payload));
  514. if (isPayloadValid) {
  515. p.data = payload;
  516. } else {
  517. return error('invalid payload');
  518. }
  519. }
  520. return p;
  521. }
  522. function tryParse(str) {
  523. try {
  524. return JSON.parse(str);
  525. } catch(e){
  526. return false;
  527. }
  528. }
  529. /**
  530. * Deallocates a parser's resources
  531. *
  532. * @api public
  533. */
  534. Decoder.prototype.destroy = function() {
  535. if (this.reconstructor) {
  536. this.reconstructor.finishedReconstruction();
  537. }
  538. };
  539. /**
  540. * A manager of a binary event's 'buffer sequence'. Should
  541. * be constructed whenever a packet of type BINARY_EVENT is
  542. * decoded.
  543. *
  544. * @param {Object} packet
  545. * @return {BinaryReconstructor} initialized reconstructor
  546. * @api private
  547. */
  548. function BinaryReconstructor(packet) {
  549. this.reconPack = packet;
  550. this.buffers = [];
  551. }
  552. /**
  553. * Method to be called when binary data received from connection
  554. * after a BINARY_EVENT packet.
  555. *
  556. * @param {Buffer | ArrayBuffer} binData - the raw binary data received
  557. * @return {null | Object} returns null if more binary data is expected or
  558. * a reconstructed packet object if all buffers have been received.
  559. * @api private
  560. */
  561. BinaryReconstructor.prototype.takeBinaryData = function(binData) {
  562. this.buffers.push(binData);
  563. if (this.buffers.length === this.reconPack.attachments) { // done with buffer list
  564. var packet = binary.reconstructPacket(this.reconPack, this.buffers);
  565. this.finishedReconstruction();
  566. return packet;
  567. }
  568. return null;
  569. };
  570. /**
  571. * Cleans up binary packet reconstruction variables.
  572. *
  573. * @api private
  574. */
  575. BinaryReconstructor.prototype.finishedReconstruction = function() {
  576. this.reconPack = null;
  577. this.buffers = [];
  578. };
  579. function error(msg) {
  580. return {
  581. type: exports.ERROR,
  582. data: 'parser error: ' + msg
  583. };
  584. }
  585. /***/ }),
  586. /* 5 */
  587. /***/ (function(module, exports, __webpack_require__) {
  588. /**
  589. * Expose `Emitter`.
  590. */
  591. if (true) {
  592. module.exports = Emitter;
  593. }
  594. /**
  595. * Initialize a new `Emitter`.
  596. *
  597. * @api public
  598. */
  599. function Emitter(obj) {
  600. if (obj) return mixin(obj);
  601. };
  602. /**
  603. * Mixin the emitter properties.
  604. *
  605. * @param {Object} obj
  606. * @return {Object}
  607. * @api private
  608. */
  609. function mixin(obj) {
  610. for (var key in Emitter.prototype) {
  611. obj[key] = Emitter.prototype[key];
  612. }
  613. return obj;
  614. }
  615. /**
  616. * Listen on the given `event` with `fn`.
  617. *
  618. * @param {String} event
  619. * @param {Function} fn
  620. * @return {Emitter}
  621. * @api public
  622. */
  623. Emitter.prototype.on =
  624. Emitter.prototype.addEventListener = function(event, fn){
  625. this._callbacks = this._callbacks || {};
  626. (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
  627. .push(fn);
  628. return this;
  629. };
  630. /**
  631. * Adds an `event` listener that will be invoked a single
  632. * time then automatically removed.
  633. *
  634. * @param {String} event
  635. * @param {Function} fn
  636. * @return {Emitter}
  637. * @api public
  638. */
  639. Emitter.prototype.once = function(event, fn){
  640. function on() {
  641. this.off(event, on);
  642. fn.apply(this, arguments);
  643. }
  644. on.fn = fn;
  645. this.on(event, on);
  646. return this;
  647. };
  648. /**
  649. * Remove the given callback for `event` or all
  650. * registered callbacks.
  651. *
  652. * @param {String} event
  653. * @param {Function} fn
  654. * @return {Emitter}
  655. * @api public
  656. */
  657. Emitter.prototype.off =
  658. Emitter.prototype.removeListener =
  659. Emitter.prototype.removeAllListeners =
  660. Emitter.prototype.removeEventListener = function(event, fn){
  661. this._callbacks = this._callbacks || {};
  662. // all
  663. if (0 == arguments.length) {
  664. this._callbacks = {};
  665. return this;
  666. }
  667. // specific event
  668. var callbacks = this._callbacks['$' + event];
  669. if (!callbacks) return this;
  670. // remove all handlers
  671. if (1 == arguments.length) {
  672. delete this._callbacks['$' + event];
  673. return this;
  674. }
  675. // remove specific handler
  676. var cb;
  677. for (var i = 0; i < callbacks.length; i++) {
  678. cb = callbacks[i];
  679. if (cb === fn || cb.fn === fn) {
  680. callbacks.splice(i, 1);
  681. break;
  682. }
  683. }
  684. return this;
  685. };
  686. /**
  687. * Emit `event` with the given args.
  688. *
  689. * @param {String} event
  690. * @param {Mixed} ...
  691. * @return {Emitter}
  692. */
  693. Emitter.prototype.emit = function(event){
  694. this._callbacks = this._callbacks || {};
  695. var args = [].slice.call(arguments, 1)
  696. , callbacks = this._callbacks['$' + event];
  697. if (callbacks) {
  698. callbacks = callbacks.slice(0);
  699. for (var i = 0, len = callbacks.length; i < len; ++i) {
  700. callbacks[i].apply(this, args);
  701. }
  702. }
  703. return this;
  704. };
  705. /**
  706. * Return array of callbacks for `event`.
  707. *
  708. * @param {String} event
  709. * @return {Array}
  710. * @api public
  711. */
  712. Emitter.prototype.listeners = function(event){
  713. this._callbacks = this._callbacks || {};
  714. return this._callbacks['$' + event] || [];
  715. };
  716. /**
  717. * Check if this emitter has `event` handlers.
  718. *
  719. * @param {String} event
  720. * @return {Boolean}
  721. * @api public
  722. */
  723. Emitter.prototype.hasListeners = function(event){
  724. return !! this.listeners(event).length;
  725. };
  726. /***/ }),
  727. /* 6 */
  728. /***/ (function(module, exports, __webpack_require__) {
  729. /*global Blob,File*/
  730. /**
  731. * Module requirements
  732. */
  733. var isArray = __webpack_require__(7);
  734. var isBuf = __webpack_require__(8);
  735. var toString = Object.prototype.toString;
  736. var withNativeBlob = typeof Blob === 'function' || (typeof Blob !== 'undefined' && toString.call(Blob) === '[object BlobConstructor]');
  737. var withNativeFile = typeof File === 'function' || (typeof File !== 'undefined' && toString.call(File) === '[object FileConstructor]');
  738. /**
  739. * Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder.
  740. * Anything with blobs or files should be fed through removeBlobs before coming
  741. * here.
  742. *
  743. * @param {Object} packet - socket.io event packet
  744. * @return {Object} with deconstructed packet and list of buffers
  745. * @api public
  746. */
  747. exports.deconstructPacket = function(packet) {
  748. var buffers = [];
  749. var packetData = packet.data;
  750. var pack = packet;
  751. pack.data = _deconstructPacket(packetData, buffers);
  752. pack.attachments = buffers.length; // number of binary 'attachments'
  753. return {packet: pack, buffers: buffers};
  754. };
  755. function _deconstructPacket(data, buffers) {
  756. if (!data) return data;
  757. if (isBuf(data)) {
  758. var placeholder = { _placeholder: true, num: buffers.length };
  759. buffers.push(data);
  760. return placeholder;
  761. } else if (isArray(data)) {
  762. var newData = new Array(data.length);
  763. for (var i = 0; i < data.length; i++) {
  764. newData[i] = _deconstructPacket(data[i], buffers);
  765. }
  766. return newData;
  767. } else if (typeof data === 'object' && !(data instanceof Date)) {
  768. var newData = {};
  769. for (var key in data) {
  770. newData[key] = _deconstructPacket(data[key], buffers);
  771. }
  772. return newData;
  773. }
  774. return data;
  775. }
  776. /**
  777. * Reconstructs a binary packet from its placeholder packet and buffers
  778. *
  779. * @param {Object} packet - event packet with placeholders
  780. * @param {Array} buffers - binary buffers to put in placeholder positions
  781. * @return {Object} reconstructed packet
  782. * @api public
  783. */
  784. exports.reconstructPacket = function(packet, buffers) {
  785. packet.data = _reconstructPacket(packet.data, buffers);
  786. packet.attachments = undefined; // no longer useful
  787. return packet;
  788. };
  789. function _reconstructPacket(data, buffers) {
  790. if (!data) return data;
  791. if (data && data._placeholder) {
  792. return buffers[data.num]; // appropriate buffer (should be natural order anyway)
  793. } else if (isArray(data)) {
  794. for (var i = 0; i < data.length; i++) {
  795. data[i] = _reconstructPacket(data[i], buffers);
  796. }
  797. } else if (typeof data === 'object') {
  798. for (var key in data) {
  799. data[key] = _reconstructPacket(data[key], buffers);
  800. }
  801. }
  802. return data;
  803. }
  804. /**
  805. * Asynchronously removes Blobs or Files from data via
  806. * FileReader's readAsArrayBuffer method. Used before encoding
  807. * data as msgpack. Calls callback with the blobless data.
  808. *
  809. * @param {Object} data
  810. * @param {Function} callback
  811. * @api private
  812. */
  813. exports.removeBlobs = function(data, callback) {
  814. function _removeBlobs(obj, curKey, containingObject) {
  815. if (!obj) return obj;
  816. // convert any blob
  817. if ((withNativeBlob && obj instanceof Blob) ||
  818. (withNativeFile && obj instanceof File)) {
  819. pendingBlobs++;
  820. // async filereader
  821. var fileReader = new FileReader();
  822. fileReader.onload = function() { // this.result == arraybuffer
  823. if (containingObject) {
  824. containingObject[curKey] = this.result;
  825. }
  826. else {
  827. bloblessData = this.result;
  828. }
  829. // if nothing pending its callback time
  830. if(! --pendingBlobs) {
  831. callback(bloblessData);
  832. }
  833. };
  834. fileReader.readAsArrayBuffer(obj); // blob -> arraybuffer
  835. } else if (isArray(obj)) { // handle array
  836. for (var i = 0; i < obj.length; i++) {
  837. _removeBlobs(obj[i], i, obj);
  838. }
  839. } else if (typeof obj === 'object' && !isBuf(obj)) { // and object
  840. for (var key in obj) {
  841. _removeBlobs(obj[key], key, obj);
  842. }
  843. }
  844. }
  845. var pendingBlobs = 0;
  846. var bloblessData = data;
  847. _removeBlobs(bloblessData);
  848. if (!pendingBlobs) {
  849. callback(bloblessData);
  850. }
  851. };
  852. /***/ }),
  853. /* 7 */
  854. /***/ (function(module, exports) {
  855. var toString = {}.toString;
  856. module.exports = Array.isArray || function (arr) {
  857. return toString.call(arr) == '[object Array]';
  858. };
  859. /***/ }),
  860. /* 8 */
  861. /***/ (function(module, exports) {
  862. module.exports = isBuf;
  863. var withNativeBuffer = typeof Buffer === 'function' && typeof Buffer.isBuffer === 'function';
  864. var withNativeArrayBuffer = typeof ArrayBuffer === 'function';
  865. var isView = function (obj) {
  866. return typeof ArrayBuffer.isView === 'function' ? ArrayBuffer.isView(obj) : (obj.buffer instanceof ArrayBuffer);
  867. };
  868. /**
  869. * Returns true if obj is a buffer or an arraybuffer.
  870. *
  871. * @api private
  872. */
  873. function isBuf(obj) {
  874. return (withNativeBuffer && Buffer.isBuffer(obj)) ||
  875. (withNativeArrayBuffer && (obj instanceof ArrayBuffer || isView(obj)));
  876. }
  877. /***/ }),
  878. /* 9 */
  879. /***/ (function(module, exports, __webpack_require__) {
  880. 'use strict';
  881. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  882. /**
  883. * Module dependencies.
  884. */
  885. var eio = __webpack_require__(10);
  886. var Socket = __webpack_require__(33);
  887. var Emitter = __webpack_require__(5);
  888. var parser = __webpack_require__(4);
  889. var on = __webpack_require__(35);
  890. var bind = __webpack_require__(36);
  891. var debug = __webpack_require__(3)('socket.io-client:manager');
  892. var indexOf = __webpack_require__(32);
  893. var Backoff = __webpack_require__(37);
  894. /**
  895. * IE6+ hasOwnProperty
  896. */
  897. var has = Object.prototype.hasOwnProperty;
  898. /**
  899. * Module exports
  900. */
  901. module.exports = Manager;
  902. /**
  903. * `Manager` constructor.
  904. *
  905. * @param {String} engine instance or engine uri/opts
  906. * @param {Object} options
  907. * @api public
  908. */
  909. function Manager(uri, opts) {
  910. if (!(this instanceof Manager)) return new Manager(uri, opts);
  911. if (uri && 'object' === (typeof uri === 'undefined' ? 'undefined' : _typeof(uri))) {
  912. opts = uri;
  913. uri = undefined;
  914. }
  915. opts = opts || {};
  916. opts.path = opts.path || '/socket.io';
  917. this.nsps = {};
  918. this.subs = [];
  919. this.opts = opts;
  920. this.reconnection(opts.reconnection !== false);
  921. this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
  922. this.reconnectionDelay(opts.reconnectionDelay || 1000);
  923. this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
  924. this.randomizationFactor(opts.randomizationFactor || 0.5);
  925. this.backoff = new Backoff({
  926. min: this.reconnectionDelay(),
  927. max: this.reconnectionDelayMax(),
  928. jitter: this.randomizationFactor()
  929. });
  930. this.timeout(null == opts.timeout ? 20000 : opts.timeout);
  931. this.readyState = 'closed';
  932. this.uri = uri;
  933. this.connecting = [];
  934. this.lastPing = null;
  935. this.encoding = false;
  936. this.packetBuffer = [];
  937. var _parser = opts.parser || parser;
  938. this.encoder = new _parser.Encoder();
  939. this.decoder = new _parser.Decoder();
  940. this.autoConnect = opts.autoConnect !== false;
  941. if (this.autoConnect) this.open();
  942. }
  943. /**
  944. * Propagate given event to sockets and emit on `this`
  945. *
  946. * @api private
  947. */
  948. Manager.prototype.emitAll = function () {
  949. this.emit.apply(this, arguments);
  950. for (var nsp in this.nsps) {
  951. if (has.call(this.nsps, nsp)) {
  952. this.nsps[nsp].emit.apply(this.nsps[nsp], arguments);
  953. }
  954. }
  955. };
  956. /**
  957. * Update `socket.id` of all sockets
  958. *
  959. * @api private
  960. */
  961. Manager.prototype.updateSocketIds = function () {
  962. for (var nsp in this.nsps) {
  963. if (has.call(this.nsps, nsp)) {
  964. this.nsps[nsp].id = this.generateId(nsp);
  965. }
  966. }
  967. };
  968. /**
  969. * generate `socket.id` for the given `nsp`
  970. *
  971. * @param {String} nsp
  972. * @return {String}
  973. * @api private
  974. */
  975. Manager.prototype.generateId = function (nsp) {
  976. return (nsp === '/' ? '' : nsp + '#') + this.engine.id;
  977. };
  978. /**
  979. * Mix in `Emitter`.
  980. */
  981. Emitter(Manager.prototype);
  982. /**
  983. * Sets the `reconnection` config.
  984. *
  985. * @param {Boolean} true/false if it should automatically reconnect
  986. * @return {Manager} self or value
  987. * @api public
  988. */
  989. Manager.prototype.reconnection = function (v) {
  990. if (!arguments.length) return this._reconnection;
  991. this._reconnection = !!v;
  992. return this;
  993. };
  994. /**
  995. * Sets the reconnection attempts config.
  996. *
  997. * @param {Number} max reconnection attempts before giving up
  998. * @return {Manager} self or value
  999. * @api public
  1000. */
  1001. Manager.prototype.reconnectionAttempts = function (v) {
  1002. if (!arguments.length) return this._reconnectionAttempts;
  1003. this._reconnectionAttempts = v;
  1004. return this;
  1005. };
  1006. /**
  1007. * Sets the delay between reconnections.
  1008. *
  1009. * @param {Number} delay
  1010. * @return {Manager} self or value
  1011. * @api public
  1012. */
  1013. Manager.prototype.reconnectionDelay = function (v) {
  1014. if (!arguments.length) return this._reconnectionDelay;
  1015. this._reconnectionDelay = v;
  1016. this.backoff && this.backoff.setMin(v);
  1017. return this;
  1018. };
  1019. Manager.prototype.randomizationFactor = function (v) {
  1020. if (!arguments.length) return this._randomizationFactor;
  1021. this._randomizationFactor = v;
  1022. this.backoff && this.backoff.setJitter(v);
  1023. return this;
  1024. };
  1025. /**
  1026. * Sets the maximum delay between reconnections.
  1027. *
  1028. * @param {Number} delay
  1029. * @return {Manager} self or value
  1030. * @api public
  1031. */
  1032. Manager.prototype.reconnectionDelayMax = function (v) {
  1033. if (!arguments.length) return this._reconnectionDelayMax;
  1034. this._reconnectionDelayMax = v;
  1035. this.backoff && this.backoff.setMax(v);
  1036. return this;
  1037. };
  1038. /**
  1039. * Sets the connection timeout. `false` to disable
  1040. *
  1041. * @return {Manager} self or value
  1042. * @api public
  1043. */
  1044. Manager.prototype.timeout = function (v) {
  1045. if (!arguments.length) return this._timeout;
  1046. this._timeout = v;
  1047. return this;
  1048. };
  1049. /**
  1050. * Starts trying to reconnect if reconnection is enabled and we have not
  1051. * started reconnecting yet
  1052. *
  1053. * @api private
  1054. */
  1055. Manager.prototype.maybeReconnectOnOpen = function () {
  1056. // Only try to reconnect if it's the first time we're connecting
  1057. if (!this.reconnecting && this._reconnection && this.backoff.attempts === 0) {
  1058. // keeps reconnection from firing twice for the same reconnection loop
  1059. this.reconnect();
  1060. }
  1061. };
  1062. /**
  1063. * Sets the current transport `socket`.
  1064. *
  1065. * @param {Function} optional, callback
  1066. * @return {Manager} self
  1067. * @api public
  1068. */
  1069. Manager.prototype.open = Manager.prototype.connect = function (fn, opts) {
  1070. if (~this.readyState.indexOf('open')) return this;
  1071. this.engine = eio(this.uri, this.opts);
  1072. var socket = this.engine;
  1073. var self = this;
  1074. this.readyState = 'opening';
  1075. this.skipReconnect = false;
  1076. // emit `open`
  1077. var openSub = on(socket, 'open', function () {
  1078. self.onopen();
  1079. fn && fn();
  1080. });
  1081. // emit `connect_error`
  1082. var errorSub = on(socket, 'error', function (data) {
  1083. self.cleanup();
  1084. self.readyState = 'closed';
  1085. self.emitAll('connect_error', data);
  1086. if (fn) {
  1087. var err = new Error('Connection error');
  1088. err.data = data;
  1089. fn(err);
  1090. } else {
  1091. // Only do this if there is no fn to handle the error
  1092. self.maybeReconnectOnOpen();
  1093. }
  1094. });
  1095. // emit `connect_timeout`
  1096. if (false !== this._timeout) {
  1097. var timeout = this._timeout;
  1098. // set timer
  1099. var timer = setTimeout(function () {
  1100. openSub.destroy();
  1101. socket.close();
  1102. socket.emit('error', 'timeout');
  1103. self.emitAll('connect_timeout', timeout);
  1104. }, timeout);
  1105. this.subs.push({
  1106. destroy: function destroy() {
  1107. clearTimeout(timer);
  1108. }
  1109. });
  1110. }
  1111. this.subs.push(openSub);
  1112. this.subs.push(errorSub);
  1113. return this;
  1114. };
  1115. /**
  1116. * Called upon transport open.
  1117. *
  1118. * @api private
  1119. */
  1120. Manager.prototype.onopen = function () {
  1121. // clear old subs
  1122. this.cleanup();
  1123. // mark as open
  1124. this.readyState = 'open';
  1125. this.emit('open');
  1126. // add new subs
  1127. var socket = this.engine;
  1128. this.subs.push(on(socket, 'data', bind(this, 'ondata')));
  1129. this.subs.push(on(socket, 'ping', bind(this, 'onping')));
  1130. this.subs.push(on(socket, 'pong', bind(this, 'onpong')));
  1131. this.subs.push(on(socket, 'error', bind(this, 'onerror')));
  1132. this.subs.push(on(socket, 'close', bind(this, 'onclose')));
  1133. this.subs.push(on(this.decoder, 'decoded', bind(this, 'ondecoded')));
  1134. };
  1135. /**
  1136. * Called upon a ping.
  1137. *
  1138. * @api private
  1139. */
  1140. Manager.prototype.onping = function () {
  1141. this.lastPing = new Date();
  1142. this.emitAll('ping');
  1143. };
  1144. /**
  1145. * Called upon a packet.
  1146. *
  1147. * @api private
  1148. */
  1149. Manager.prototype.onpong = function () {
  1150. this.emitAll('pong', new Date() - this.lastPing);
  1151. };
  1152. /**
  1153. * Called with data.
  1154. *
  1155. * @api private
  1156. */
  1157. Manager.prototype.ondata = function (data) {
  1158. this.decoder.add(data);
  1159. };
  1160. /**
  1161. * Called when parser fully decodes a packet.
  1162. *
  1163. * @api private
  1164. */
  1165. Manager.prototype.ondecoded = function (packet) {
  1166. this.emit('packet', packet);
  1167. };
  1168. /**
  1169. * Called upon socket error.
  1170. *
  1171. * @api private
  1172. */
  1173. Manager.prototype.onerror = function (err) {
  1174. this.emitAll('error', err);
  1175. };
  1176. /**
  1177. * Creates a new socket for the given `nsp`.
  1178. *
  1179. * @return {Socket}
  1180. * @api public
  1181. */
  1182. Manager.prototype.socket = function (nsp, opts) {
  1183. var socket = this.nsps[nsp];
  1184. if (!socket) {
  1185. socket = new Socket(this, nsp, opts);
  1186. this.nsps[nsp] = socket;
  1187. var self = this;
  1188. socket.on('connecting', onConnecting);
  1189. socket.on('connect', function () {
  1190. socket.id = self.generateId(nsp);
  1191. });
  1192. if (this.autoConnect) {
  1193. // manually call here since connecting event is fired before listening
  1194. onConnecting();
  1195. }
  1196. }
  1197. function onConnecting() {
  1198. if (!~indexOf(self.connecting, socket)) {
  1199. self.connecting.push(socket);
  1200. }
  1201. }
  1202. return socket;
  1203. };
  1204. /**
  1205. * Called upon a socket close.
  1206. *
  1207. * @param {Socket} socket
  1208. */
  1209. Manager.prototype.destroy = function (socket) {
  1210. var index = indexOf(this.connecting, socket);
  1211. if (~index) this.connecting.splice(index, 1);
  1212. if (this.connecting.length) return;
  1213. this.close();
  1214. };
  1215. /**
  1216. * Writes a packet.
  1217. *
  1218. * @param {Object} packet
  1219. * @api private
  1220. */
  1221. Manager.prototype.packet = function (packet) {
  1222. var self = this;
  1223. if (packet.query && packet.type === 0) packet.nsp += '?' + packet.query;
  1224. if (!self.encoding) {
  1225. // encode, then write to engine with result
  1226. self.encoding = true;
  1227. this.encoder.encode(packet, function (encodedPackets) {
  1228. for (var i = 0; i < encodedPackets.length; i++) {
  1229. self.engine.write(encodedPackets[i], packet.options);
  1230. }
  1231. self.encoding = false;
  1232. self.processPacketQueue();
  1233. });
  1234. } else {
  1235. // add packet to the queue
  1236. self.packetBuffer.push(packet);
  1237. }
  1238. };
  1239. /**
  1240. * If packet buffer is non-empty, begins encoding the
  1241. * next packet in line.
  1242. *
  1243. * @api private
  1244. */
  1245. Manager.prototype.processPacketQueue = function () {
  1246. if (this.packetBuffer.length > 0 && !this.encoding) {
  1247. var pack = this.packetBuffer.shift();
  1248. this.packet(pack);
  1249. }
  1250. };
  1251. /**
  1252. * Clean up transport subscriptions and packet buffer.
  1253. *
  1254. * @api private
  1255. */
  1256. Manager.prototype.cleanup = function () {
  1257. var subsLength = this.subs.length;
  1258. for (var i = 0; i < subsLength; i++) {
  1259. var sub = this.subs.shift();
  1260. sub.destroy();
  1261. }
  1262. this.packetBuffer = [];
  1263. this.encoding = false;
  1264. this.lastPing = null;
  1265. this.decoder.destroy();
  1266. };
  1267. /**
  1268. * Close the current socket.
  1269. *
  1270. * @api private
  1271. */
  1272. Manager.prototype.close = Manager.prototype.disconnect = function () {
  1273. this.skipReconnect = true;
  1274. this.reconnecting = false;
  1275. if ('opening' === this.readyState) {
  1276. // `onclose` will not fire because
  1277. // an open event never happened
  1278. this.cleanup();
  1279. }
  1280. this.backoff.reset();
  1281. this.readyState = 'closed';
  1282. if (this.engine) this.engine.close();
  1283. };
  1284. /**
  1285. * Called upon engine close.
  1286. *
  1287. * @api private
  1288. */
  1289. Manager.prototype.onclose = function (reason) {
  1290. this.cleanup();
  1291. this.backoff.reset();
  1292. this.readyState = 'closed';
  1293. this.emit('close', reason);
  1294. if (this._reconnection && !this.skipReconnect) {
  1295. this.reconnect();
  1296. }
  1297. };
  1298. /**
  1299. * Attempt a reconnection.
  1300. *
  1301. * @api private
  1302. */
  1303. Manager.prototype.reconnect = function () {
  1304. if (this.reconnecting || this.skipReconnect) return this;
  1305. var self = this;
  1306. if (this.backoff.attempts >= this._reconnectionAttempts) {
  1307. this.backoff.reset();
  1308. this.emitAll('reconnect_failed');
  1309. this.reconnecting = false;
  1310. } else {
  1311. var delay = this.backoff.duration();
  1312. this.reconnecting = true;
  1313. var timer = setTimeout(function () {
  1314. if (self.skipReconnect) return;
  1315. self.emitAll('reconnect_attempt', self.backoff.attempts);
  1316. self.emitAll('reconnecting', self.backoff.attempts);
  1317. // check again for the case socket closed in above events
  1318. if (self.skipReconnect) return;
  1319. self.open(function (err) {
  1320. if (err) {
  1321. self.reconnecting = false;
  1322. self.reconnect();
  1323. self.emitAll('reconnect_error', err.data);
  1324. } else {
  1325. self.onreconnect();
  1326. }
  1327. });
  1328. }, delay);
  1329. this.subs.push({
  1330. destroy: function destroy() {
  1331. clearTimeout(timer);
  1332. }
  1333. });
  1334. }
  1335. };
  1336. /**
  1337. * Called upon successful reconnect.
  1338. *
  1339. * @api private
  1340. */
  1341. Manager.prototype.onreconnect = function () {
  1342. var attempt = this.backoff.attempts;
  1343. this.reconnecting = false;
  1344. this.backoff.reset();
  1345. this.updateSocketIds();
  1346. this.emitAll('reconnect', attempt);
  1347. };
  1348. /***/ }),
  1349. /* 10 */
  1350. /***/ (function(module, exports, __webpack_require__) {
  1351. module.exports = __webpack_require__(11);
  1352. /**
  1353. * Exports parser
  1354. *
  1355. * @api public
  1356. *
  1357. */
  1358. module.exports.parser = __webpack_require__(18);
  1359. /***/ }),
  1360. /* 11 */
  1361. /***/ (function(module, exports, __webpack_require__) {
  1362. /**
  1363. * Module dependencies.
  1364. */
  1365. var transports = __webpack_require__(12);
  1366. var Emitter = __webpack_require__(5);
  1367. var debug = __webpack_require__(3)('engine.io-client:socket');
  1368. var index = __webpack_require__(32);
  1369. var parser = __webpack_require__(18);
  1370. var parseuri = __webpack_require__(2);
  1371. var parseqs = __webpack_require__(26);
  1372. /**
  1373. * Module exports.
  1374. */
  1375. module.exports = Socket;
  1376. /**
  1377. * Socket constructor.
  1378. *
  1379. * @param {String|Object} uri or options
  1380. * @param {Object} options
  1381. * @api public
  1382. */
  1383. function Socket (uri, opts) {
  1384. if (!(this instanceof Socket)) return new Socket(uri, opts);
  1385. opts = opts || {};
  1386. if (uri && 'object' === typeof uri) {
  1387. opts = uri;
  1388. uri = null;
  1389. }
  1390. if (uri) {
  1391. uri = parseuri(uri);
  1392. opts.hostname = uri.host;
  1393. opts.secure = uri.protocol === 'https' || uri.protocol === 'wss';
  1394. opts.port = uri.port;
  1395. if (uri.query) opts.query = uri.query;
  1396. } else if (opts.host) {
  1397. opts.hostname = parseuri(opts.host).host;
  1398. }
  1399. this.secure = null != opts.secure ? opts.secure
  1400. : (typeof location !== 'undefined' && 'https:' === location.protocol);
  1401. if (opts.hostname && !opts.port) {
  1402. // if no port is specified manually, use the protocol default
  1403. opts.port = this.secure ? '443' : '80';
  1404. }
  1405. this.agent = opts.agent || false;
  1406. this.hostname = opts.hostname ||
  1407. (typeof location !== 'undefined' ? location.hostname : 'localhost');
  1408. this.port = opts.port || (typeof location !== 'undefined' && location.port
  1409. ? location.port
  1410. : (this.secure ? 443 : 80));
  1411. this.query = opts.query || {};
  1412. if ('string' === typeof this.query) this.query = parseqs.decode(this.query);
  1413. this.upgrade = false !== opts.upgrade;
  1414. this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
  1415. this.forceJSONP = !!opts.forceJSONP;
  1416. this.jsonp = false !== opts.jsonp;
  1417. this.forceBase64 = !!opts.forceBase64;
  1418. this.enablesXDR = !!opts.enablesXDR;
  1419. this.timestampParam = opts.timestampParam || 't';
  1420. this.timestampRequests = opts.timestampRequests;
  1421. this.transports = opts.transports || ['polling', 'websocket'];
  1422. this.transportOptions = opts.transportOptions || {};
  1423. this.readyState = '';
  1424. this.writeBuffer = [];
  1425. this.prevBufferLen = 0;
  1426. this.policyPort = opts.policyPort || 843;
  1427. this.rememberUpgrade = opts.rememberUpgrade || false;
  1428. this.binaryType = null;
  1429. this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
  1430. this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || {}) : false;
  1431. if (true === this.perMessageDeflate) this.perMessageDeflate = {};
  1432. if (this.perMessageDeflate && null == this.perMessageDeflate.threshold) {
  1433. this.perMessageDeflate.threshold = 1024;
  1434. }
  1435. // SSL options for Node.js client
  1436. this.pfx = opts.pfx || null;
  1437. this.key = opts.key || null;
  1438. this.passphrase = opts.passphrase || null;
  1439. this.cert = opts.cert || null;
  1440. this.ca = opts.ca || null;
  1441. this.ciphers = opts.ciphers || null;
  1442. this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? true : opts.rejectUnauthorized;
  1443. this.forceNode = !!opts.forceNode;
  1444. // detect ReactNative environment
  1445. this.isReactNative = (typeof navigator !== 'undefined' && typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative');
  1446. // other options for Node.js or ReactNative client
  1447. if (typeof self === 'undefined' || this.isReactNative) {
  1448. if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
  1449. this.extraHeaders = opts.extraHeaders;
  1450. }
  1451. if (opts.localAddress) {
  1452. this.localAddress = opts.localAddress;
  1453. }
  1454. }
  1455. // set on handshake
  1456. this.id = null;
  1457. this.upgrades = null;
  1458. this.pingInterval = null;
  1459. this.pingTimeout = null;
  1460. // set on heartbeat
  1461. this.pingIntervalTimer = null;
  1462. this.pingTimeoutTimer = null;
  1463. this.open();
  1464. }
  1465. Socket.priorWebsocketSuccess = false;
  1466. /**
  1467. * Mix in `Emitter`.
  1468. */
  1469. Emitter(Socket.prototype);
  1470. /**
  1471. * Protocol version.
  1472. *
  1473. * @api public
  1474. */
  1475. Socket.protocol = parser.protocol; // this is an int
  1476. /**
  1477. * Expose deps for legacy compatibility
  1478. * and standalone browser access.
  1479. */
  1480. Socket.Socket = Socket;
  1481. Socket.Transport = __webpack_require__(17);
  1482. Socket.transports = __webpack_require__(12);
  1483. Socket.parser = __webpack_require__(18);
  1484. /**
  1485. * Creates transport of the given type.
  1486. *
  1487. * @param {String} transport name
  1488. * @return {Transport}
  1489. * @api private
  1490. */
  1491. Socket.prototype.createTransport = function (name) {
  1492. var query = clone(this.query);
  1493. // append engine.io protocol identifier
  1494. query.EIO = parser.protocol;
  1495. // transport name
  1496. query.transport = name;
  1497. // per-transport options
  1498. var options = this.transportOptions[name] || {};
  1499. // session id if we already have one
  1500. if (this.id) query.sid = this.id;
  1501. var transport = new transports[name]({
  1502. query: query,
  1503. socket: this,
  1504. agent: options.agent || this.agent,
  1505. hostname: options.hostname || this.hostname,
  1506. port: options.port || this.port,
  1507. secure: options.secure || this.secure,
  1508. path: options.path || this.path,
  1509. forceJSONP: options.forceJSONP || this.forceJSONP,
  1510. jsonp: options.jsonp || this.jsonp,
  1511. forceBase64: options.forceBase64 || this.forceBase64,
  1512. enablesXDR: options.enablesXDR || this.enablesXDR,
  1513. timestampRequests: options.timestampRequests || this.timestampRequests,
  1514. timestampParam: options.timestampParam || this.timestampParam,
  1515. policyPort: options.policyPort || this.policyPort,
  1516. pfx: options.pfx || this.pfx,
  1517. key: options.key || this.key,
  1518. passphrase: options.passphrase || this.passphrase,
  1519. cert: options.cert || this.cert,
  1520. ca: options.ca || this.ca,
  1521. ciphers: options.ciphers || this.ciphers,
  1522. rejectUnauthorized: options.rejectUnauthorized || this.rejectUnauthorized,
  1523. perMessageDeflate: options.perMessageDeflate || this.perMessageDeflate,
  1524. extraHeaders: options.extraHeaders || this.extraHeaders,
  1525. forceNode: options.forceNode || this.forceNode,
  1526. localAddress: options.localAddress || this.localAddress,
  1527. requestTimeout: options.requestTimeout || this.requestTimeout,
  1528. protocols: options.protocols || void (0),
  1529. isReactNative: this.isReactNative
  1530. });
  1531. return transport;
  1532. };
  1533. function clone (obj) {
  1534. var o = {};
  1535. for (var i in obj) {
  1536. if (obj.hasOwnProperty(i)) {
  1537. o[i] = obj[i];
  1538. }
  1539. }
  1540. return o;
  1541. }
  1542. /**
  1543. * Initializes transport to use and starts probe.
  1544. *
  1545. * @api private
  1546. */
  1547. Socket.prototype.open = function () {
  1548. var transport;
  1549. if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') !== -1) {
  1550. transport = 'websocket';
  1551. } else if (0 === this.transports.length) {
  1552. // Emit error on next tick so it can be listened to
  1553. var self = this;
  1554. setTimeout(function () {
  1555. self.emit('error', 'No transports available');
  1556. }, 0);
  1557. return;
  1558. } else {
  1559. transport = this.transports[0];
  1560. }
  1561. this.readyState = 'opening';
  1562. // Retry with the next transport if the transport is disabled (jsonp: false)
  1563. try {
  1564. transport = this.createTransport(transport);
  1565. } catch (e) {
  1566. this.transports.shift();
  1567. this.open();
  1568. return;
  1569. }
  1570. transport.open();
  1571. this.setTransport(transport);
  1572. };
  1573. /**
  1574. * Sets the current transport. Disables the existing one (if any).
  1575. *
  1576. * @api private
  1577. */
  1578. Socket.prototype.setTransport = function (transport) {
  1579. var self = this;
  1580. if (this.transport) {
  1581. this.transport.removeAllListeners();
  1582. }
  1583. // set up transport
  1584. this.transport = transport;
  1585. // set up transport listeners
  1586. transport
  1587. .on('drain', function () {
  1588. self.onDrain();
  1589. })
  1590. .on('packet', function (packet) {
  1591. self.onPacket(packet);
  1592. })
  1593. .on('error', function (e) {
  1594. self.onError(e);
  1595. })
  1596. .on('close', function () {
  1597. self.onClose('transport close');
  1598. });
  1599. };
  1600. /**
  1601. * Probes a transport.
  1602. *
  1603. * @param {String} transport name
  1604. * @api private
  1605. */
  1606. Socket.prototype.probe = function (name) {
  1607. var transport = this.createTransport(name, { probe: 1 });
  1608. var failed = false;
  1609. var self = this;
  1610. Socket.priorWebsocketSuccess = false;
  1611. function onTransportOpen () {
  1612. if (self.onlyBinaryUpgrades) {
  1613. var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary;
  1614. failed = failed || upgradeLosesBinary;
  1615. }
  1616. if (failed) return;
  1617. transport.send([{ type: 'ping', data: 'probe' }]);
  1618. transport.once('packet', function (msg) {
  1619. if (failed) return;
  1620. if ('pong' === msg.type && 'probe' === msg.data) {
  1621. self.upgrading = true;
  1622. self.emit('upgrading', transport);
  1623. if (!transport) return;
  1624. Socket.priorWebsocketSuccess = 'websocket' === transport.name;
  1625. self.transport.pause(function () {
  1626. if (failed) return;
  1627. if ('closed' === self.readyState) return;
  1628. cleanup();
  1629. self.setTransport(transport);
  1630. transport.send([{ type: 'upgrade' }]);
  1631. self.emit('upgrade', transport);
  1632. transport = null;
  1633. self.upgrading = false;
  1634. self.flush();
  1635. });
  1636. } else {
  1637. var err = new Error('probe error');
  1638. err.transport = transport.name;
  1639. self.emit('upgradeError', err);
  1640. }
  1641. });
  1642. }
  1643. function freezeTransport () {
  1644. if (failed) return;
  1645. // Any callback called by transport should be ignored since now
  1646. failed = true;
  1647. cleanup();
  1648. transport.close();
  1649. transport = null;
  1650. }
  1651. // Handle any error that happens while probing
  1652. function onerror (err) {
  1653. var error = new Error('probe error: ' + err);
  1654. error.transport = transport.name;
  1655. freezeTransport();
  1656. self.emit('upgradeError', error);
  1657. }
  1658. function onTransportClose () {
  1659. onerror('transport closed');
  1660. }
  1661. // When the socket is closed while we're probing
  1662. function onclose () {
  1663. onerror('socket closed');
  1664. }
  1665. // When the socket is upgraded while we're probing
  1666. function onupgrade (to) {
  1667. if (transport && to.name !== transport.name) {
  1668. freezeTransport();
  1669. }
  1670. }
  1671. // Remove all listeners on the transport and on self
  1672. function cleanup () {
  1673. transport.removeListener('open', onTransportOpen);
  1674. transport.removeListener('error', onerror);
  1675. transport.removeListener('close', onTransportClose);
  1676. self.removeListener('close', onclose);
  1677. self.removeListener('upgrading', onupgrade);
  1678. }
  1679. transport.once('open', onTransportOpen);
  1680. transport.once('error', onerror);
  1681. transport.once('close', onTransportClose);
  1682. this.once('close', onclose);
  1683. this.once('upgrading', onupgrade);
  1684. transport.open();
  1685. };
  1686. /**
  1687. * Called when connection is deemed open.
  1688. *
  1689. * @api public
  1690. */
  1691. Socket.prototype.onOpen = function () {
  1692. this.readyState = 'open';
  1693. Socket.priorWebsocketSuccess = 'websocket' === this.transport.name;
  1694. this.emit('open');
  1695. this.flush();
  1696. // we check for `readyState` in case an `open`
  1697. // listener already closed the socket
  1698. if ('open' === this.readyState && this.upgrade && this.transport.pause) {
  1699. for (var i = 0, l = this.upgrades.length; i < l; i++) {
  1700. this.probe(this.upgrades[i]);
  1701. }
  1702. }
  1703. };
  1704. /**
  1705. * Handles a packet.
  1706. *
  1707. * @api private
  1708. */
  1709. Socket.prototype.onPacket = function (packet) {
  1710. if ('opening' === this.readyState || 'open' === this.readyState ||
  1711. 'closing' === this.readyState) {
  1712. this.emit('packet', packet);
  1713. // Socket is live - any packet counts
  1714. this.emit('heartbeat');
  1715. switch (packet.type) {
  1716. case 'open':
  1717. this.onHandshake(JSON.parse(packet.data));
  1718. break;
  1719. case 'pong':
  1720. this.setPing();
  1721. this.emit('pong');
  1722. break;
  1723. case 'error':
  1724. var err = new Error('server error');
  1725. err.code = packet.data;
  1726. this.onError(err);
  1727. break;
  1728. case 'message':
  1729. this.emit('data', packet.data);
  1730. this.emit('message', packet.data);
  1731. break;
  1732. }
  1733. } else {
  1734. }
  1735. };
  1736. /**
  1737. * Called upon handshake completion.
  1738. *
  1739. * @param {Object} handshake obj
  1740. * @api private
  1741. */
  1742. Socket.prototype.onHandshake = function (data) {
  1743. this.emit('handshake', data);
  1744. this.id = data.sid;
  1745. this.transport.query.sid = data.sid;
  1746. this.upgrades = this.filterUpgrades(data.upgrades);
  1747. this.pingInterval = data.pingInterval;
  1748. this.pingTimeout = data.pingTimeout;
  1749. this.onOpen();
  1750. // In case open handler closes socket
  1751. if ('closed' === this.readyState) return;
  1752. this.setPing();
  1753. // Prolong liveness of socket on heartbeat
  1754. this.removeListener('heartbeat', this.onHeartbeat);
  1755. this.on('heartbeat', this.onHeartbeat);
  1756. };
  1757. /**
  1758. * Resets ping timeout.
  1759. *
  1760. * @api private
  1761. */
  1762. Socket.prototype.onHeartbeat = function (timeout) {
  1763. clearTimeout(this.pingTimeoutTimer);
  1764. var self = this;
  1765. self.pingTimeoutTimer = setTimeout(function () {
  1766. if ('closed' === self.readyState) return;
  1767. self.onClose('ping timeout');
  1768. }, timeout || (self.pingInterval + self.pingTimeout));
  1769. };
  1770. /**
  1771. * Pings server every `this.pingInterval` and expects response
  1772. * within `this.pingTimeout` or closes connection.
  1773. *
  1774. * @api private
  1775. */
  1776. Socket.prototype.setPing = function () {
  1777. var self = this;
  1778. clearTimeout(self.pingIntervalTimer);
  1779. self.pingIntervalTimer = setTimeout(function () {
  1780. self.ping();
  1781. self.onHeartbeat(self.pingTimeout);
  1782. }, self.pingInterval);
  1783. };
  1784. /**
  1785. * Sends a ping packet.
  1786. *
  1787. * @api private
  1788. */
  1789. Socket.prototype.ping = function () {
  1790. var self = this;
  1791. this.sendPacket('ping', function () {
  1792. self.emit('ping');
  1793. });
  1794. };
  1795. /**
  1796. * Called on `drain` event
  1797. *
  1798. * @api private
  1799. */
  1800. Socket.prototype.onDrain = function () {
  1801. this.writeBuffer.splice(0, this.prevBufferLen);
  1802. // setting prevBufferLen = 0 is very important
  1803. // for example, when upgrading, upgrade packet is sent over,
  1804. // and a nonzero prevBufferLen could cause problems on `drain`
  1805. this.prevBufferLen = 0;
  1806. if (0 === this.writeBuffer.length) {
  1807. this.emit('drain');
  1808. } else {
  1809. this.flush();
  1810. }
  1811. };
  1812. /**
  1813. * Flush write buffers.
  1814. *
  1815. * @api private
  1816. */
  1817. Socket.prototype.flush = function () {
  1818. if ('closed' !== this.readyState && this.transport.writable &&
  1819. !this.upgrading && this.writeBuffer.length) {
  1820. this.transport.send(this.writeBuffer);
  1821. // keep track of current length of writeBuffer
  1822. // splice writeBuffer and callbackBuffer on `drain`
  1823. this.prevBufferLen = this.writeBuffer.length;
  1824. this.emit('flush');
  1825. }
  1826. };
  1827. /**
  1828. * Sends a message.
  1829. *
  1830. * @param {String} message.
  1831. * @param {Function} callback function.
  1832. * @param {Object} options.
  1833. * @return {Socket} for chaining.
  1834. * @api public
  1835. */
  1836. Socket.prototype.write =
  1837. Socket.prototype.send = function (msg, options, fn) {
  1838. this.sendPacket('message', msg, options, fn);
  1839. return this;
  1840. };
  1841. /**
  1842. * Sends a packet.
  1843. *
  1844. * @param {String} packet type.
  1845. * @param {String} data.
  1846. * @param {Object} options.
  1847. * @param {Function} callback function.
  1848. * @api private
  1849. */
  1850. Socket.prototype.sendPacket = function (type, data, options, fn) {
  1851. if ('function' === typeof data) {
  1852. fn = data;
  1853. data = undefined;
  1854. }
  1855. if ('function' === typeof options) {
  1856. fn = options;
  1857. options = null;
  1858. }
  1859. if ('closing' === this.readyState || 'closed' === this.readyState) {
  1860. return;
  1861. }
  1862. options = options || {};
  1863. options.compress = false !== options.compress;
  1864. var packet = {
  1865. type: type,
  1866. data: data,
  1867. options: options
  1868. };
  1869. this.emit('packetCreate', packet);
  1870. this.writeBuffer.push(packet);
  1871. if (fn) this.once('flush', fn);
  1872. this.flush();
  1873. };
  1874. /**
  1875. * Closes the connection.
  1876. *
  1877. * @api private
  1878. */
  1879. Socket.prototype.close = function () {
  1880. if ('opening' === this.readyState || 'open' === this.readyState) {
  1881. this.readyState = 'closing';
  1882. var self = this;
  1883. if (this.writeBuffer.length) {
  1884. this.once('drain', function () {
  1885. if (this.upgrading) {
  1886. waitForUpgrade();
  1887. } else {
  1888. close();
  1889. }
  1890. });
  1891. } else if (this.upgrading) {
  1892. waitForUpgrade();
  1893. } else {
  1894. close();
  1895. }
  1896. }
  1897. function close () {
  1898. self.onClose('forced close');
  1899. self.transport.close();
  1900. }
  1901. function cleanupAndClose () {
  1902. self.removeListener('upgrade', cleanupAndClose);
  1903. self.removeListener('upgradeError', cleanupAndClose);
  1904. close();
  1905. }
  1906. function waitForUpgrade () {
  1907. // wait for upgrade to finish since we can't send packets while pausing a transport
  1908. self.once('upgrade', cleanupAndClose);
  1909. self.once('upgradeError', cleanupAndClose);
  1910. }
  1911. return this;
  1912. };
  1913. /**
  1914. * Called upon transport error
  1915. *
  1916. * @api private
  1917. */
  1918. Socket.prototype.onError = function (err) {
  1919. Socket.priorWebsocketSuccess = false;
  1920. this.emit('error', err);
  1921. this.onClose('transport error', err);
  1922. };
  1923. /**
  1924. * Called upon transport close.
  1925. *
  1926. * @api private
  1927. */
  1928. Socket.prototype.onClose = function (reason, desc) {
  1929. if ('opening' === this.readyState || 'open' === this.readyState || 'closing' === this.readyState) {
  1930. var self = this;
  1931. // clear timers
  1932. clearTimeout(this.pingIntervalTimer);
  1933. clearTimeout(this.pingTimeoutTimer);
  1934. // stop event from firing again for transport
  1935. this.transport.removeAllListeners('close');
  1936. // ensure transport won't stay open
  1937. this.transport.close();
  1938. // ignore further transport communication
  1939. this.transport.removeAllListeners();
  1940. // set ready state
  1941. this.readyState = 'closed';
  1942. // clear session id
  1943. this.id = null;
  1944. // emit close event
  1945. this.emit('close', reason, desc);
  1946. // clean buffers after, so users can still
  1947. // grab the buffers on `close` event
  1948. self.writeBuffer = [];
  1949. self.prevBufferLen = 0;
  1950. }
  1951. };
  1952. /**
  1953. * Filters upgrades, returning only those matching client transports.
  1954. *
  1955. * @param {Array} server upgrades
  1956. * @api private
  1957. *
  1958. */
  1959. Socket.prototype.filterUpgrades = function (upgrades) {
  1960. var filteredUpgrades = [];
  1961. for (var i = 0, j = upgrades.length; i < j; i++) {
  1962. if (~index(this.transports, upgrades[i])) filteredUpgrades.push(upgrades[i]);
  1963. }
  1964. return filteredUpgrades;
  1965. };
  1966. /***/ }),
  1967. /* 12 */
  1968. /***/ (function(module, exports, __webpack_require__) {
  1969. /**
  1970. * Module dependencies
  1971. */
  1972. var XMLHttpRequest = __webpack_require__(13);
  1973. var XHR = __webpack_require__(15);
  1974. var JSONP = __webpack_require__(29);
  1975. var websocket = __webpack_require__(30);
  1976. /**
  1977. * Export transports.
  1978. */
  1979. exports.polling = polling;
  1980. exports.websocket = websocket;
  1981. /**
  1982. * Polling transport polymorphic constructor.
  1983. * Decides on xhr vs jsonp based on feature detection.
  1984. *
  1985. * @api private
  1986. */
  1987. function polling (opts) {
  1988. var xhr;
  1989. var xd = false;
  1990. var xs = false;
  1991. var jsonp = false !== opts.jsonp;
  1992. if (typeof location !== 'undefined') {
  1993. var isSSL = 'https:' === location.protocol;
  1994. var port = location.port;
  1995. // some user agents have empty `location.port`
  1996. if (!port) {
  1997. port = isSSL ? 443 : 80;
  1998. }
  1999. xd = opts.hostname !== location.hostname || port !== opts.port;
  2000. xs = opts.secure !== isSSL;
  2001. }
  2002. opts.xdomain = xd;
  2003. opts.xscheme = xs;
  2004. xhr = new XMLHttpRequest(opts);
  2005. if ('open' in xhr && !opts.forceJSONP) {
  2006. return new XHR(opts);
  2007. } else {
  2008. if (!jsonp) throw new Error('JSONP disabled');
  2009. return new JSONP(opts);
  2010. }
  2011. }
  2012. /***/ }),
  2013. /* 13 */
  2014. /***/ (function(module, exports, __webpack_require__) {
  2015. // browser shim for xmlhttprequest module
  2016. var hasCORS = __webpack_require__(14);
  2017. module.exports = function (opts) {
  2018. var xdomain = opts.xdomain;
  2019. // scheme must be same when usign XDomainRequest
  2020. // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
  2021. var xscheme = opts.xscheme;
  2022. // XDomainRequest has a flow of not sending cookie, therefore it should be disabled as a default.
  2023. // https://github.com/Automattic/engine.io-client/pull/217
  2024. var enablesXDR = opts.enablesXDR;
  2025. // XMLHttpRequest can be disabled on IE
  2026. try {
  2027. if ('undefined' !== typeof XMLHttpRequest && (!xdomain || hasCORS)) {
  2028. return new XMLHttpRequest();
  2029. }
  2030. } catch (e) { }
  2031. // Use XDomainRequest for IE8 if enablesXDR is true
  2032. // because loading bar keeps flashing when using jsonp-polling
  2033. // https://github.com/yujiosaka/socke.io-ie8-loading-example
  2034. try {
  2035. if ('undefined' !== typeof XDomainRequest && !xscheme && enablesXDR) {
  2036. return new XDomainRequest();
  2037. }
  2038. } catch (e) { }
  2039. if (!xdomain) {
  2040. try {
  2041. return new self[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
  2042. } catch (e) { }
  2043. }
  2044. };
  2045. /***/ }),
  2046. /* 14 */
  2047. /***/ (function(module, exports) {
  2048. /**
  2049. * Module exports.
  2050. *
  2051. * Logic borrowed from Modernizr:
  2052. *
  2053. * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js
  2054. */
  2055. try {
  2056. module.exports = typeof XMLHttpRequest !== 'undefined' &&
  2057. 'withCredentials' in new XMLHttpRequest();
  2058. } catch (err) {
  2059. // if XMLHttp support is disabled in IE then it will throw
  2060. // when trying to create
  2061. module.exports = false;
  2062. }
  2063. /***/ }),
  2064. /* 15 */
  2065. /***/ (function(module, exports, __webpack_require__) {
  2066. /* global attachEvent */
  2067. /**
  2068. * Module requirements.
  2069. */
  2070. var XMLHttpRequest = __webpack_require__(13);
  2071. var Polling = __webpack_require__(16);
  2072. var Emitter = __webpack_require__(5);
  2073. var inherit = __webpack_require__(27);
  2074. var debug = __webpack_require__(3)('engine.io-client:polling-xhr');
  2075. /**
  2076. * Module exports.
  2077. */
  2078. module.exports = XHR;
  2079. module.exports.Request = Request;
  2080. /**
  2081. * Empty function
  2082. */
  2083. function empty () {}
  2084. /**
  2085. * XHR Polling constructor.
  2086. *
  2087. * @param {Object} opts
  2088. * @api public
  2089. */
  2090. function XHR (opts) {
  2091. Polling.call(this, opts);
  2092. this.requestTimeout = opts.requestTimeout;
  2093. this.extraHeaders = opts.extraHeaders;
  2094. if (typeof location !== 'undefined') {
  2095. var isSSL = 'https:' === location.protocol;
  2096. var port = location.port;
  2097. // some user agents have empty `location.port`
  2098. if (!port) {
  2099. port = isSSL ? 443 : 80;
  2100. }
  2101. this.xd = (typeof location !== 'undefined' && opts.hostname !== location.hostname) ||
  2102. port !== opts.port;
  2103. this.xs = opts.secure !== isSSL;
  2104. }
  2105. }
  2106. /**
  2107. * Inherits from Polling.
  2108. */
  2109. inherit(XHR, Polling);
  2110. /**
  2111. * XHR supports binary
  2112. */
  2113. XHR.prototype.supportsBinary = true;
  2114. /**
  2115. * Creates a request.
  2116. *
  2117. * @param {String} method
  2118. * @api private
  2119. */
  2120. XHR.prototype.request = function (opts) {
  2121. opts = opts || {};
  2122. opts.uri = this.uri();
  2123. opts.xd = this.xd;
  2124. opts.xs = this.xs;
  2125. opts.agent = this.agent || false;
  2126. opts.supportsBinary = this.supportsBinary;
  2127. opts.enablesXDR = this.enablesXDR;
  2128. // SSL options for Node.js client
  2129. opts.pfx = this.pfx;
  2130. opts.key = this.key;
  2131. opts.passphrase = this.passphrase;
  2132. opts.cert = this.cert;
  2133. opts.ca = this.ca;
  2134. opts.ciphers = this.ciphers;
  2135. opts.rejectUnauthorized = this.rejectUnauthorized;
  2136. opts.requestTimeout = this.requestTimeout;
  2137. // other options for Node.js client
  2138. opts.extraHeaders = this.extraHeaders;
  2139. return new Request(opts);
  2140. };
  2141. /**
  2142. * Sends data.
  2143. *
  2144. * @param {String} data to send.
  2145. * @param {Function} called upon flush.
  2146. * @api private
  2147. */
  2148. XHR.prototype.doWrite = function (data, fn) {
  2149. var isBinary = typeof data !== 'string' && data !== undefined;
  2150. var req = this.request({ method: 'POST', data: data, isBinary: isBinary });
  2151. var self = this;
  2152. req.on('success', fn);
  2153. req.on('error', function (err) {
  2154. self.onError('xhr post error', err);
  2155. });
  2156. this.sendXhr = req;
  2157. };
  2158. /**
  2159. * Starts a poll cycle.
  2160. *
  2161. * @api private
  2162. */
  2163. XHR.prototype.doPoll = function () {
  2164. var req = this.request();
  2165. var self = this;
  2166. req.on('data', function (data) {
  2167. self.onData(data);
  2168. });
  2169. req.on('error', function (err) {
  2170. self.onError('xhr poll error', err);
  2171. });
  2172. this.pollXhr = req;
  2173. };
  2174. /**
  2175. * Request constructor
  2176. *
  2177. * @param {Object} options
  2178. * @api public
  2179. */
  2180. function Request (opts) {
  2181. this.method = opts.method || 'GET';
  2182. this.uri = opts.uri;
  2183. this.xd = !!opts.xd;
  2184. this.xs = !!opts.xs;
  2185. this.async = false !== opts.async;
  2186. this.data = undefined !== opts.data ? opts.data : null;
  2187. this.agent = opts.agent;
  2188. this.isBinary = opts.isBinary;
  2189. this.supportsBinary = opts.supportsBinary;
  2190. this.enablesXDR = opts.enablesXDR;
  2191. this.requestTimeout = opts.requestTimeout;
  2192. // SSL options for Node.js client
  2193. this.pfx = opts.pfx;
  2194. this.key = opts.key;
  2195. this.passphrase = opts.passphrase;
  2196. this.cert = opts.cert;
  2197. this.ca = opts.ca;
  2198. this.ciphers = opts.ciphers;
  2199. this.rejectUnauthorized = opts.rejectUnauthorized;
  2200. // other options for Node.js client
  2201. this.extraHeaders = opts.extraHeaders;
  2202. this.create();
  2203. }
  2204. /**
  2205. * Mix in `Emitter`.
  2206. */
  2207. Emitter(Request.prototype);
  2208. /**
  2209. * Creates the XHR object and sends the request.
  2210. *
  2211. * @api private
  2212. */
  2213. Request.prototype.create = function () {
  2214. var opts = { agent: this.agent, xdomain: this.xd, xscheme: this.xs, enablesXDR: this.enablesXDR };
  2215. // SSL options for Node.js client
  2216. opts.pfx = this.pfx;
  2217. opts.key = this.key;
  2218. opts.passphrase = this.passphrase;
  2219. opts.cert = this.cert;
  2220. opts.ca = this.ca;
  2221. opts.ciphers = this.ciphers;
  2222. opts.rejectUnauthorized = this.rejectUnauthorized;
  2223. var xhr = this.xhr = new XMLHttpRequest(opts);
  2224. var self = this;
  2225. try {
  2226. xhr.open(this.method, this.uri, this.async);
  2227. try {
  2228. if (this.extraHeaders) {
  2229. xhr.setDisableHeaderCheck && xhr.setDisableHeaderCheck(true);
  2230. for (var i in this.extraHeaders) {
  2231. if (this.extraHeaders.hasOwnProperty(i)) {
  2232. xhr.setRequestHeader(i, this.extraHeaders[i]);
  2233. }
  2234. }
  2235. }
  2236. } catch (e) {}
  2237. if ('POST' === this.method) {
  2238. try {
  2239. if (this.isBinary) {
  2240. xhr.setRequestHeader('Content-type', 'application/octet-stream');
  2241. } else {
  2242. xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
  2243. }
  2244. } catch (e) {}
  2245. }
  2246. try {
  2247. xhr.setRequestHeader('Accept', '*/*');
  2248. } catch (e) {}
  2249. // ie6 check
  2250. if ('withCredentials' in xhr) {
  2251. xhr.withCredentials = true;
  2252. }
  2253. if (this.requestTimeout) {
  2254. xhr.timeout = this.requestTimeout;
  2255. }
  2256. if (this.hasXDR()) {
  2257. xhr.onload = function () {
  2258. self.onLoad();
  2259. };
  2260. xhr.onerror = function () {
  2261. self.onError(xhr.responseText);
  2262. };
  2263. } else {
  2264. xhr.onreadystatechange = function () {
  2265. if (xhr.readyState === 2) {
  2266. try {
  2267. var contentType = xhr.getResponseHeader('Content-Type');
  2268. if (self.supportsBinary && contentType === 'application/octet-stream') {
  2269. xhr.responseType = 'arraybuffer';
  2270. }
  2271. } catch (e) {}
  2272. }
  2273. if (4 !== xhr.readyState) return;
  2274. if (200 === xhr.status || 1223 === xhr.status) {
  2275. self.onLoad();
  2276. } else {
  2277. // make sure the `error` event handler that's user-set
  2278. // does not throw in the same tick and gets caught here
  2279. setTimeout(function () {
  2280. self.onError(xhr.status);
  2281. }, 0);
  2282. }
  2283. };
  2284. }
  2285. xhr.send(this.data);
  2286. } catch (e) {
  2287. // Need to defer since .create() is called directly fhrom the constructor
  2288. // and thus the 'error' event can only be only bound *after* this exception
  2289. // occurs. Therefore, also, we cannot throw here at all.
  2290. setTimeout(function () {
  2291. self.onError(e);
  2292. }, 0);
  2293. return;
  2294. }
  2295. if (typeof document !== 'undefined') {
  2296. this.index = Request.requestsCount++;
  2297. Request.requests[this.index] = this;
  2298. }
  2299. };
  2300. /**
  2301. * Called upon successful response.
  2302. *
  2303. * @api private
  2304. */
  2305. Request.prototype.onSuccess = function () {
  2306. this.emit('success');
  2307. this.cleanup();
  2308. };
  2309. /**
  2310. * Called if we have data.
  2311. *
  2312. * @api private
  2313. */
  2314. Request.prototype.onData = function (data) {
  2315. this.emit('data', data);
  2316. this.onSuccess();
  2317. };
  2318. /**
  2319. * Called upon error.
  2320. *
  2321. * @api private
  2322. */
  2323. Request.prototype.onError = function (err) {
  2324. this.emit('error', err);
  2325. this.cleanup(true);
  2326. };
  2327. /**
  2328. * Cleans up house.
  2329. *
  2330. * @api private
  2331. */
  2332. Request.prototype.cleanup = function (fromError) {
  2333. if ('undefined' === typeof this.xhr || null === this.xhr) {
  2334. return;
  2335. }
  2336. // xmlhttprequest
  2337. if (this.hasXDR()) {
  2338. this.xhr.onload = this.xhr.onerror = empty;
  2339. } else {
  2340. this.xhr.onreadystatechange = empty;
  2341. }
  2342. if (fromError) {
  2343. try {
  2344. this.xhr.abort();
  2345. } catch (e) {}
  2346. }
  2347. if (typeof document !== 'undefined') {
  2348. delete Request.requests[this.index];
  2349. }
  2350. this.xhr = null;
  2351. };
  2352. /**
  2353. * Called upon load.
  2354. *
  2355. * @api private
  2356. */
  2357. Request.prototype.onLoad = function () {
  2358. var data;
  2359. try {
  2360. var contentType;
  2361. try {
  2362. contentType = this.xhr.getResponseHeader('Content-Type');
  2363. } catch (e) {}
  2364. if (contentType === 'application/octet-stream') {
  2365. data = this.xhr.response || this.xhr.responseText;
  2366. } else {
  2367. data = this.xhr.responseText;
  2368. }
  2369. } catch (e) {
  2370. this.onError(e);
  2371. }
  2372. if (null != data) {
  2373. this.onData(data);
  2374. }
  2375. };
  2376. /**
  2377. * Check if it has XDomainRequest.
  2378. *
  2379. * @api private
  2380. */
  2381. Request.prototype.hasXDR = function () {
  2382. return typeof XDomainRequest !== 'undefined' && !this.xs && this.enablesXDR;
  2383. };
  2384. /**
  2385. * Aborts the request.
  2386. *
  2387. * @api public
  2388. */
  2389. Request.prototype.abort = function () {
  2390. this.cleanup();
  2391. };
  2392. /**
  2393. * Aborts pending requests when unloading the window. This is needed to prevent
  2394. * memory leaks (e.g. when using IE) and to ensure that no spurious error is
  2395. * emitted.
  2396. */
  2397. Request.requestsCount = 0;
  2398. Request.requests = {};
  2399. if (typeof document !== 'undefined') {
  2400. if (typeof attachEvent === 'function') {
  2401. attachEvent('onunload', unloadHandler);
  2402. } else if (typeof addEventListener === 'function') {
  2403. var terminationEvent = 'onpagehide' in self ? 'pagehide' : 'unload';
  2404. addEventListener(terminationEvent, unloadHandler, false);
  2405. }
  2406. }
  2407. function unloadHandler () {
  2408. for (var i in Request.requests) {
  2409. if (Request.requests.hasOwnProperty(i)) {
  2410. Request.requests[i].abort();
  2411. }
  2412. }
  2413. }
  2414. /***/ }),
  2415. /* 16 */
  2416. /***/ (function(module, exports, __webpack_require__) {
  2417. /**
  2418. * Module dependencies.
  2419. */
  2420. var Transport = __webpack_require__(17);
  2421. var parseqs = __webpack_require__(26);
  2422. var parser = __webpack_require__(18);
  2423. var inherit = __webpack_require__(27);
  2424. var yeast = __webpack_require__(28);
  2425. var debug = __webpack_require__(3)('engine.io-client:polling');
  2426. /**
  2427. * Module exports.
  2428. */
  2429. module.exports = Polling;
  2430. /**
  2431. * Is XHR2 supported?
  2432. */
  2433. var hasXHR2 = (function () {
  2434. var XMLHttpRequest = __webpack_require__(13);
  2435. var xhr = new XMLHttpRequest({ xdomain: false });
  2436. return null != xhr.responseType;
  2437. })();
  2438. /**
  2439. * Polling interface.
  2440. *
  2441. * @param {Object} opts
  2442. * @api private
  2443. */
  2444. function Polling (opts) {
  2445. var forceBase64 = (opts && opts.forceBase64);
  2446. if (!hasXHR2 || forceBase64) {
  2447. this.supportsBinary = false;
  2448. }
  2449. Transport.call(this, opts);
  2450. }
  2451. /**
  2452. * Inherits from Transport.
  2453. */
  2454. inherit(Polling, Transport);
  2455. /**
  2456. * Transport name.
  2457. */
  2458. Polling.prototype.name = 'polling';
  2459. /**
  2460. * Opens the socket (triggers polling). We write a PING message to determine
  2461. * when the transport is open.
  2462. *
  2463. * @api private
  2464. */
  2465. Polling.prototype.doOpen = function () {
  2466. this.poll();
  2467. };
  2468. /**
  2469. * Pauses polling.
  2470. *
  2471. * @param {Function} callback upon buffers are flushed and transport is paused
  2472. * @api private
  2473. */
  2474. Polling.prototype.pause = function (onPause) {
  2475. var self = this;
  2476. this.readyState = 'pausing';
  2477. function pause () {
  2478. self.readyState = 'paused';
  2479. onPause();
  2480. }
  2481. if (this.polling || !this.writable) {
  2482. var total = 0;
  2483. if (this.polling) {
  2484. total++;
  2485. this.once('pollComplete', function () {
  2486. --total || pause();
  2487. });
  2488. }
  2489. if (!this.writable) {
  2490. total++;
  2491. this.once('drain', function () {
  2492. --total || pause();
  2493. });
  2494. }
  2495. } else {
  2496. pause();
  2497. }
  2498. };
  2499. /**
  2500. * Starts polling cycle.
  2501. *
  2502. * @api public
  2503. */
  2504. Polling.prototype.poll = function () {
  2505. this.polling = true;
  2506. this.doPoll();
  2507. this.emit('poll');
  2508. };
  2509. /**
  2510. * Overloads onData to detect payloads.
  2511. *
  2512. * @api private
  2513. */
  2514. Polling.prototype.onData = function (data) {
  2515. var self = this;
  2516. var callback = function (packet, index, total) {
  2517. // if its the first message we consider the transport open
  2518. if ('opening' === self.readyState) {
  2519. self.onOpen();
  2520. }
  2521. // if its a close packet, we close the ongoing requests
  2522. if ('close' === packet.type) {
  2523. self.onClose();
  2524. return false;
  2525. }
  2526. // otherwise bypass onData and handle the message
  2527. self.onPacket(packet);
  2528. };
  2529. // decode payload
  2530. parser.decodePayload(data, this.socket.binaryType, callback);
  2531. // if an event did not trigger closing
  2532. if ('closed' !== this.readyState) {
  2533. // if we got data we're not polling
  2534. this.polling = false;
  2535. this.emit('pollComplete');
  2536. if ('open' === this.readyState) {
  2537. this.poll();
  2538. } else {
  2539. }
  2540. }
  2541. };
  2542. /**
  2543. * For polling, send a close packet.
  2544. *
  2545. * @api private
  2546. */
  2547. Polling.prototype.doClose = function () {
  2548. var self = this;
  2549. function close () {
  2550. self.write([{ type: 'close' }]);
  2551. }
  2552. if ('open' === this.readyState) {
  2553. close();
  2554. } else {
  2555. // in case we're trying to close while
  2556. // handshaking is in progress (GH-164)
  2557. this.once('open', close);
  2558. }
  2559. };
  2560. /**
  2561. * Writes a packets payload.
  2562. *
  2563. * @param {Array} data packets
  2564. * @param {Function} drain callback
  2565. * @api private
  2566. */
  2567. Polling.prototype.write = function (packets) {
  2568. var self = this;
  2569. this.writable = false;
  2570. var callbackfn = function () {
  2571. self.writable = true;
  2572. self.emit('drain');
  2573. };
  2574. parser.encodePayload(packets, this.supportsBinary, function (data) {
  2575. self.doWrite(data, callbackfn);
  2576. });
  2577. };
  2578. /**
  2579. * Generates uri for connection.
  2580. *
  2581. * @api private
  2582. */
  2583. Polling.prototype.uri = function () {
  2584. var query = this.query || {};
  2585. var schema = this.secure ? 'https' : 'http';
  2586. var port = '';
  2587. // cache busting is forced
  2588. if (false !== this.timestampRequests) {
  2589. query[this.timestampParam] = yeast();
  2590. }
  2591. if (!this.supportsBinary && !query.sid) {
  2592. query.b64 = 1;
  2593. }
  2594. query = parseqs.encode(query);
  2595. // avoid port if default for schema
  2596. if (this.port && (('https' === schema && Number(this.port) !== 443) ||
  2597. ('http' === schema && Number(this.port) !== 80))) {
  2598. port = ':' + this.port;
  2599. }
  2600. // prepend ? to query
  2601. if (query.length) {
  2602. query = '?' + query;
  2603. }
  2604. var ipv6 = this.hostname.indexOf(':') !== -1;
  2605. return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query;
  2606. };
  2607. /***/ }),
  2608. /* 17 */
  2609. /***/ (function(module, exports, __webpack_require__) {
  2610. /**
  2611. * Module dependencies.
  2612. */
  2613. var parser = __webpack_require__(18);
  2614. var Emitter = __webpack_require__(5);
  2615. /**
  2616. * Module exports.
  2617. */
  2618. module.exports = Transport;
  2619. /**
  2620. * Transport abstract constructor.
  2621. *
  2622. * @param {Object} options.
  2623. * @api private
  2624. */
  2625. function Transport (opts) {
  2626. this.path = opts.path;
  2627. this.hostname = opts.hostname;
  2628. this.port = opts.port;
  2629. this.secure = opts.secure;
  2630. this.query = opts.query;
  2631. this.timestampParam = opts.timestampParam;
  2632. this.timestampRequests = opts.timestampRequests;
  2633. this.readyState = '';
  2634. this.agent = opts.agent || false;
  2635. this.socket = opts.socket;
  2636. this.enablesXDR = opts.enablesXDR;
  2637. // SSL options for Node.js client
  2638. this.pfx = opts.pfx;
  2639. this.key = opts.key;
  2640. this.passphrase = opts.passphrase;
  2641. this.cert = opts.cert;
  2642. this.ca = opts.ca;
  2643. this.ciphers = opts.ciphers;
  2644. this.rejectUnauthorized = opts.rejectUnauthorized;
  2645. this.forceNode = opts.forceNode;
  2646. // results of ReactNative environment detection
  2647. this.isReactNative = opts.isReactNative;
  2648. // other options for Node.js client
  2649. this.extraHeaders = opts.extraHeaders;
  2650. this.localAddress = opts.localAddress;
  2651. }
  2652. /**
  2653. * Mix in `Emitter`.
  2654. */
  2655. Emitter(Transport.prototype);
  2656. /**
  2657. * Emits an error.
  2658. *
  2659. * @param {String} str
  2660. * @return {Transport} for chaining
  2661. * @api public
  2662. */
  2663. Transport.prototype.onError = function (msg, desc) {
  2664. var err = new Error(msg);
  2665. err.type = 'TransportError';
  2666. err.description = desc;
  2667. this.emit('error', err);
  2668. return this;
  2669. };
  2670. /**
  2671. * Opens the transport.
  2672. *
  2673. * @api public
  2674. */
  2675. Transport.prototype.open = function () {
  2676. if ('closed' === this.readyState || '' === this.readyState) {
  2677. this.readyState = 'opening';
  2678. this.doOpen();
  2679. }
  2680. return this;
  2681. };
  2682. /**
  2683. * Closes the transport.
  2684. *
  2685. * @api private
  2686. */
  2687. Transport.prototype.close = function () {
  2688. if ('opening' === this.readyState || 'open' === this.readyState) {
  2689. this.doClose();
  2690. this.onClose();
  2691. }
  2692. return this;
  2693. };
  2694. /**
  2695. * Sends multiple packets.
  2696. *
  2697. * @param {Array} packets
  2698. * @api private
  2699. */
  2700. Transport.prototype.send = function (packets) {
  2701. if ('open' === this.readyState) {
  2702. this.write(packets);
  2703. } else {
  2704. throw new Error('Transport not open');
  2705. }
  2706. };
  2707. /**
  2708. * Called upon open
  2709. *
  2710. * @api private
  2711. */
  2712. Transport.prototype.onOpen = function () {
  2713. this.readyState = 'open';
  2714. this.writable = true;
  2715. this.emit('open');
  2716. };
  2717. /**
  2718. * Called with data.
  2719. *
  2720. * @param {String} data
  2721. * @api private
  2722. */
  2723. Transport.prototype.onData = function (data) {
  2724. var packet = parser.decodePacket(data, this.socket.binaryType);
  2725. this.onPacket(packet);
  2726. };
  2727. /**
  2728. * Called with a decoded packet.
  2729. */
  2730. Transport.prototype.onPacket = function (packet) {
  2731. this.emit('packet', packet);
  2732. };
  2733. /**
  2734. * Called upon close.
  2735. *
  2736. * @api private
  2737. */
  2738. Transport.prototype.onClose = function () {
  2739. this.readyState = 'closed';
  2740. this.emit('close');
  2741. };
  2742. /***/ }),
  2743. /* 18 */
  2744. /***/ (function(module, exports, __webpack_require__) {
  2745. /**
  2746. * Module dependencies.
  2747. */
  2748. var keys = __webpack_require__(19);
  2749. var hasBinary = __webpack_require__(20);
  2750. var sliceBuffer = __webpack_require__(21);
  2751. var after = __webpack_require__(22);
  2752. var utf8 = __webpack_require__(23);
  2753. var base64encoder;
  2754. if (typeof ArrayBuffer !== 'undefined') {
  2755. base64encoder = __webpack_require__(24);
  2756. }
  2757. /**
  2758. * Check if we are running an android browser. That requires us to use
  2759. * ArrayBuffer with polling transports...
  2760. *
  2761. * http://ghinda.net/jpeg-blob-ajax-android/
  2762. */
  2763. var isAndroid = typeof navigator !== 'undefined' && /Android/i.test(navigator.userAgent);
  2764. /**
  2765. * Check if we are running in PhantomJS.
  2766. * Uploading a Blob with PhantomJS does not work correctly, as reported here:
  2767. * https://github.com/ariya/phantomjs/issues/11395
  2768. * @type boolean
  2769. */
  2770. var isPhantomJS = typeof navigator !== 'undefined' && /PhantomJS/i.test(navigator.userAgent);
  2771. /**
  2772. * When true, avoids using Blobs to encode payloads.
  2773. * @type boolean
  2774. */
  2775. var dontSendBlobs = isAndroid || isPhantomJS;
  2776. /**
  2777. * Current protocol version.
  2778. */
  2779. exports.protocol = 3;
  2780. /**
  2781. * Packet types.
  2782. */
  2783. var packets = exports.packets = {
  2784. open: 0 // non-ws
  2785. , close: 1 // non-ws
  2786. , ping: 2
  2787. , pong: 3
  2788. , message: 4
  2789. , upgrade: 5
  2790. , noop: 6
  2791. };
  2792. var packetslist = keys(packets);
  2793. /**
  2794. * Premade error packet.
  2795. */
  2796. var err = { type: 'error', data: 'parser error' };
  2797. /**
  2798. * Create a blob api even for blob builder when vendor prefixes exist
  2799. */
  2800. var Blob = __webpack_require__(25);
  2801. /**
  2802. * Encodes a packet.
  2803. *
  2804. * <packet type id> [ <data> ]
  2805. *
  2806. * Example:
  2807. *
  2808. * 5hello world
  2809. * 3
  2810. * 4
  2811. *
  2812. * Binary is encoded in an identical principle
  2813. *
  2814. * @api private
  2815. */
  2816. exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
  2817. if (typeof supportsBinary === 'function') {
  2818. callback = supportsBinary;
  2819. supportsBinary = false;
  2820. }
  2821. if (typeof utf8encode === 'function') {
  2822. callback = utf8encode;
  2823. utf8encode = null;
  2824. }
  2825. var data = (packet.data === undefined)
  2826. ? undefined
  2827. : packet.data.buffer || packet.data;
  2828. if (typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) {
  2829. return encodeArrayBuffer(packet, supportsBinary, callback);
  2830. } else if (typeof Blob !== 'undefined' && data instanceof Blob) {
  2831. return encodeBlob(packet, supportsBinary, callback);
  2832. }
  2833. // might be an object with { base64: true, data: dataAsBase64String }
  2834. if (data && data.base64) {
  2835. return encodeBase64Object(packet, callback);
  2836. }
  2837. // Sending data as a utf-8 string
  2838. var encoded = packets[packet.type];
  2839. // data fragment is optional
  2840. if (undefined !== packet.data) {
  2841. encoded += utf8encode ? utf8.encode(String(packet.data), { strict: false }) : String(packet.data);
  2842. }
  2843. return callback('' + encoded);
  2844. };
  2845. function encodeBase64Object(packet, callback) {
  2846. // packet data is an object { base64: true, data: dataAsBase64String }
  2847. var message = 'b' + exports.packets[packet.type] + packet.data.data;
  2848. return callback(message);
  2849. }
  2850. /**
  2851. * Encode packet helpers for binary types
  2852. */
  2853. function encodeArrayBuffer(packet, supportsBinary, callback) {
  2854. if (!supportsBinary) {
  2855. return exports.encodeBase64Packet(packet, callback);
  2856. }
  2857. var data = packet.data;
  2858. var contentArray = new Uint8Array(data);
  2859. var resultBuffer = new Uint8Array(1 + data.byteLength);
  2860. resultBuffer[0] = packets[packet.type];
  2861. for (var i = 0; i < contentArray.length; i++) {
  2862. resultBuffer[i+1] = contentArray[i];
  2863. }
  2864. return callback(resultBuffer.buffer);
  2865. }
  2866. function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) {
  2867. if (!supportsBinary) {
  2868. return exports.encodeBase64Packet(packet, callback);
  2869. }
  2870. var fr = new FileReader();
  2871. fr.onload = function() {
  2872. exports.encodePacket({ type: packet.type, data: fr.result }, supportsBinary, true, callback);
  2873. };
  2874. return fr.readAsArrayBuffer(packet.data);
  2875. }
  2876. function encodeBlob(packet, supportsBinary, callback) {
  2877. if (!supportsBinary) {
  2878. return exports.encodeBase64Packet(packet, callback);
  2879. }
  2880. if (dontSendBlobs) {
  2881. return encodeBlobAsArrayBuffer(packet, supportsBinary, callback);
  2882. }
  2883. var length = new Uint8Array(1);
  2884. length[0] = packets[packet.type];
  2885. var blob = new Blob([length.buffer, packet.data]);
  2886. return callback(blob);
  2887. }
  2888. /**
  2889. * Encodes a packet with binary data in a base64 string
  2890. *
  2891. * @param {Object} packet, has `type` and `data`
  2892. * @return {String} base64 encoded message
  2893. */
  2894. exports.encodeBase64Packet = function(packet, callback) {
  2895. var message = 'b' + exports.packets[packet.type];
  2896. if (typeof Blob !== 'undefined' && packet.data instanceof Blob) {
  2897. var fr = new FileReader();
  2898. fr.onload = function() {
  2899. var b64 = fr.result.split(',')[1];
  2900. callback(message + b64);
  2901. };
  2902. return fr.readAsDataURL(packet.data);
  2903. }
  2904. var b64data;
  2905. try {
  2906. b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data));
  2907. } catch (e) {
  2908. // iPhone Safari doesn't let you apply with typed arrays
  2909. var typed = new Uint8Array(packet.data);
  2910. var basic = new Array(typed.length);
  2911. for (var i = 0; i < typed.length; i++) {
  2912. basic[i] = typed[i];
  2913. }
  2914. b64data = String.fromCharCode.apply(null, basic);
  2915. }
  2916. message += btoa(b64data);
  2917. return callback(message);
  2918. };
  2919. /**
  2920. * Decodes a packet. Changes format to Blob if requested.
  2921. *
  2922. * @return {Object} with `type` and `data` (if any)
  2923. * @api private
  2924. */
  2925. exports.decodePacket = function (data, binaryType, utf8decode) {
  2926. if (data === undefined) {
  2927. return err;
  2928. }
  2929. // String data
  2930. if (typeof data === 'string') {
  2931. if (data.charAt(0) === 'b') {
  2932. return exports.decodeBase64Packet(data.substr(1), binaryType);
  2933. }
  2934. if (utf8decode) {
  2935. data = tryDecode(data);
  2936. if (data === false) {
  2937. return err;
  2938. }
  2939. }
  2940. var type = data.charAt(0);
  2941. if (Number(type) != type || !packetslist[type]) {
  2942. return err;
  2943. }
  2944. if (data.length > 1) {
  2945. return { type: packetslist[type], data: data.substring(1) };
  2946. } else {
  2947. return { type: packetslist[type] };
  2948. }
  2949. }
  2950. var asArray = new Uint8Array(data);
  2951. var type = asArray[0];
  2952. var rest = sliceBuffer(data, 1);
  2953. if (Blob && binaryType === 'blob') {
  2954. rest = new Blob([rest]);
  2955. }
  2956. return { type: packetslist[type], data: rest };
  2957. };
  2958. function tryDecode(data) {
  2959. try {
  2960. data = utf8.decode(data, { strict: false });
  2961. } catch (e) {
  2962. return false;
  2963. }
  2964. return data;
  2965. }
  2966. /**
  2967. * Decodes a packet encoded in a base64 string
  2968. *
  2969. * @param {String} base64 encoded message
  2970. * @return {Object} with `type` and `data` (if any)
  2971. */
  2972. exports.decodeBase64Packet = function(msg, binaryType) {
  2973. var type = packetslist[msg.charAt(0)];
  2974. if (!base64encoder) {
  2975. return { type: type, data: { base64: true, data: msg.substr(1) } };
  2976. }
  2977. var data = base64encoder.decode(msg.substr(1));
  2978. if (binaryType === 'blob' && Blob) {
  2979. data = new Blob([data]);
  2980. }
  2981. return { type: type, data: data };
  2982. };
  2983. /**
  2984. * Encodes multiple messages (payload).
  2985. *
  2986. * <length>:data
  2987. *
  2988. * Example:
  2989. *
  2990. * 11:hello world2:hi
  2991. *
  2992. * If any contents are binary, they will be encoded as base64 strings. Base64
  2993. * encoded strings are marked with a b before the length specifier
  2994. *
  2995. * @param {Array} packets
  2996. * @api private
  2997. */
  2998. exports.encodePayload = function (packets, supportsBinary, callback) {
  2999. if (typeof supportsBinary === 'function') {
  3000. callback = supportsBinary;
  3001. supportsBinary = null;
  3002. }
  3003. var isBinary = hasBinary(packets);
  3004. if (supportsBinary && isBinary) {
  3005. if (Blob && !dontSendBlobs) {
  3006. return exports.encodePayloadAsBlob(packets, callback);
  3007. }
  3008. return exports.encodePayloadAsArrayBuffer(packets, callback);
  3009. }
  3010. if (!packets.length) {
  3011. return callback('0:');
  3012. }
  3013. function setLengthHeader(message) {
  3014. return message.length + ':' + message;
  3015. }
  3016. function encodeOne(packet, doneCallback) {
  3017. exports.encodePacket(packet, !isBinary ? false : supportsBinary, false, function(message) {
  3018. doneCallback(null, setLengthHeader(message));
  3019. });
  3020. }
  3021. map(packets, encodeOne, function(err, results) {
  3022. return callback(results.join(''));
  3023. });
  3024. };
  3025. /**
  3026. * Async array map using after
  3027. */
  3028. function map(ary, each, done) {
  3029. var result = new Array(ary.length);
  3030. var next = after(ary.length, done);
  3031. var eachWithIndex = function(i, el, cb) {
  3032. each(el, function(error, msg) {
  3033. result[i] = msg;
  3034. cb(error, result);
  3035. });
  3036. };
  3037. for (var i = 0; i < ary.length; i++) {
  3038. eachWithIndex(i, ary[i], next);
  3039. }
  3040. }
  3041. /*
  3042. * Decodes data when a payload is maybe expected. Possible binary contents are
  3043. * decoded from their base64 representation
  3044. *
  3045. * @param {String} data, callback method
  3046. * @api public
  3047. */
  3048. exports.decodePayload = function (data, binaryType, callback) {
  3049. if (typeof data !== 'string') {
  3050. return exports.decodePayloadAsBinary(data, binaryType, callback);
  3051. }
  3052. if (typeof binaryType === 'function') {
  3053. callback = binaryType;
  3054. binaryType = null;
  3055. }
  3056. var packet;
  3057. if (data === '') {
  3058. // parser error - ignoring payload
  3059. return callback(err, 0, 1);
  3060. }
  3061. var length = '', n, msg;
  3062. for (var i = 0, l = data.length; i < l; i++) {
  3063. var chr = data.charAt(i);
  3064. if (chr !== ':') {
  3065. length += chr;
  3066. continue;
  3067. }
  3068. if (length === '' || (length != (n = Number(length)))) {
  3069. // parser error - ignoring payload
  3070. return callback(err, 0, 1);
  3071. }
  3072. msg = data.substr(i + 1, n);
  3073. if (length != msg.length) {
  3074. // parser error - ignoring payload
  3075. return callback(err, 0, 1);
  3076. }
  3077. if (msg.length) {
  3078. packet = exports.decodePacket(msg, binaryType, false);
  3079. if (err.type === packet.type && err.data === packet.data) {
  3080. // parser error in individual packet - ignoring payload
  3081. return callback(err, 0, 1);
  3082. }
  3083. var ret = callback(packet, i + n, l);
  3084. if (false === ret) return;
  3085. }
  3086. // advance cursor
  3087. i += n;
  3088. length = '';
  3089. }
  3090. if (length !== '') {
  3091. // parser error - ignoring payload
  3092. return callback(err, 0, 1);
  3093. }
  3094. };
  3095. /**
  3096. * Encodes multiple messages (payload) as binary.
  3097. *
  3098. * <1 = binary, 0 = string><number from 0-9><number from 0-9>[...]<number
  3099. * 255><data>
  3100. *
  3101. * Example:
  3102. * 1 3 255 1 2 3, if the binary contents are interpreted as 8 bit integers
  3103. *
  3104. * @param {Array} packets
  3105. * @return {ArrayBuffer} encoded payload
  3106. * @api private
  3107. */
  3108. exports.encodePayloadAsArrayBuffer = function(packets, callback) {
  3109. if (!packets.length) {
  3110. return callback(new ArrayBuffer(0));
  3111. }
  3112. function encodeOne(packet, doneCallback) {
  3113. exports.encodePacket(packet, true, true, function(data) {
  3114. return doneCallback(null, data);
  3115. });
  3116. }
  3117. map(packets, encodeOne, function(err, encodedPackets) {
  3118. var totalLength = encodedPackets.reduce(function(acc, p) {
  3119. var len;
  3120. if (typeof p === 'string'){
  3121. len = p.length;
  3122. } else {
  3123. len = p.byteLength;
  3124. }
  3125. return acc + len.toString().length + len + 2; // string/binary identifier + separator = 2
  3126. }, 0);
  3127. var resultArray = new Uint8Array(totalLength);
  3128. var bufferIndex = 0;
  3129. encodedPackets.forEach(function(p) {
  3130. var isString = typeof p === 'string';
  3131. var ab = p;
  3132. if (isString) {
  3133. var view = new Uint8Array(p.length);
  3134. for (var i = 0; i < p.length; i++) {
  3135. view[i] = p.charCodeAt(i);
  3136. }
  3137. ab = view.buffer;
  3138. }
  3139. if (isString) { // not true binary
  3140. resultArray[bufferIndex++] = 0;
  3141. } else { // true binary
  3142. resultArray[bufferIndex++] = 1;
  3143. }
  3144. var lenStr = ab.byteLength.toString();
  3145. for (var i = 0; i < lenStr.length; i++) {
  3146. resultArray[bufferIndex++] = parseInt(lenStr[i]);
  3147. }
  3148. resultArray[bufferIndex++] = 255;
  3149. var view = new Uint8Array(ab);
  3150. for (var i = 0; i < view.length; i++) {
  3151. resultArray[bufferIndex++] = view[i];
  3152. }
  3153. });
  3154. return callback(resultArray.buffer);
  3155. });
  3156. };
  3157. /**
  3158. * Encode as Blob
  3159. */
  3160. exports.encodePayloadAsBlob = function(packets, callback) {
  3161. function encodeOne(packet, doneCallback) {
  3162. exports.encodePacket(packet, true, true, function(encoded) {
  3163. var binaryIdentifier = new Uint8Array(1);
  3164. binaryIdentifier[0] = 1;
  3165. if (typeof encoded === 'string') {
  3166. var view = new Uint8Array(encoded.length);
  3167. for (var i = 0; i < encoded.length; i++) {
  3168. view[i] = encoded.charCodeAt(i);
  3169. }
  3170. encoded = view.buffer;
  3171. binaryIdentifier[0] = 0;
  3172. }
  3173. var len = (encoded instanceof ArrayBuffer)
  3174. ? encoded.byteLength
  3175. : encoded.size;
  3176. var lenStr = len.toString();
  3177. var lengthAry = new Uint8Array(lenStr.length + 1);
  3178. for (var i = 0; i < lenStr.length; i++) {
  3179. lengthAry[i] = parseInt(lenStr[i]);
  3180. }
  3181. lengthAry[lenStr.length] = 255;
  3182. if (Blob) {
  3183. var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]);
  3184. doneCallback(null, blob);
  3185. }
  3186. });
  3187. }
  3188. map(packets, encodeOne, function(err, results) {
  3189. return callback(new Blob(results));
  3190. });
  3191. };
  3192. /*
  3193. * Decodes data when a payload is maybe expected. Strings are decoded by
  3194. * interpreting each byte as a key code for entries marked to start with 0. See
  3195. * description of encodePayloadAsBinary
  3196. *
  3197. * @param {ArrayBuffer} data, callback method
  3198. * @api public
  3199. */
  3200. exports.decodePayloadAsBinary = function (data, binaryType, callback) {
  3201. if (typeof binaryType === 'function') {
  3202. callback = binaryType;
  3203. binaryType = null;
  3204. }
  3205. var bufferTail = data;
  3206. var buffers = [];
  3207. while (bufferTail.byteLength > 0) {
  3208. var tailArray = new Uint8Array(bufferTail);
  3209. var isString = tailArray[0] === 0;
  3210. var msgLength = '';
  3211. for (var i = 1; ; i++) {
  3212. if (tailArray[i] === 255) break;
  3213. // 310 = char length of Number.MAX_VALUE
  3214. if (msgLength.length > 310) {
  3215. return callback(err, 0, 1);
  3216. }
  3217. msgLength += tailArray[i];
  3218. }
  3219. bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length);
  3220. msgLength = parseInt(msgLength);
  3221. var msg = sliceBuffer(bufferTail, 0, msgLength);
  3222. if (isString) {
  3223. try {
  3224. msg = String.fromCharCode.apply(null, new Uint8Array(msg));
  3225. } catch (e) {
  3226. // iPhone Safari doesn't let you apply to typed arrays
  3227. var typed = new Uint8Array(msg);
  3228. msg = '';
  3229. for (var i = 0; i < typed.length; i++) {
  3230. msg += String.fromCharCode(typed[i]);
  3231. }
  3232. }
  3233. }
  3234. buffers.push(msg);
  3235. bufferTail = sliceBuffer(bufferTail, msgLength);
  3236. }
  3237. var total = buffers.length;
  3238. buffers.forEach(function(buffer, i) {
  3239. callback(exports.decodePacket(buffer, binaryType, true), i, total);
  3240. });
  3241. };
  3242. /***/ }),
  3243. /* 19 */
  3244. /***/ (function(module, exports) {
  3245. /**
  3246. * Gets the keys for an object.
  3247. *
  3248. * @return {Array} keys
  3249. * @api private
  3250. */
  3251. module.exports = Object.keys || function keys (obj){
  3252. var arr = [];
  3253. var has = Object.prototype.hasOwnProperty;
  3254. for (var i in obj) {
  3255. if (has.call(obj, i)) {
  3256. arr.push(i);
  3257. }
  3258. }
  3259. return arr;
  3260. };
  3261. /***/ }),
  3262. /* 20 */
  3263. /***/ (function(module, exports, __webpack_require__) {
  3264. /* global Blob File */
  3265. /*
  3266. * Module requirements.
  3267. */
  3268. var isArray = __webpack_require__(7);
  3269. var toString = Object.prototype.toString;
  3270. var withNativeBlob = typeof Blob === 'function' ||
  3271. typeof Blob !== 'undefined' && toString.call(Blob) === '[object BlobConstructor]';
  3272. var withNativeFile = typeof File === 'function' ||
  3273. typeof File !== 'undefined' && toString.call(File) === '[object FileConstructor]';
  3274. /**
  3275. * Module exports.
  3276. */
  3277. module.exports = hasBinary;
  3278. /**
  3279. * Checks for binary data.
  3280. *
  3281. * Supports Buffer, ArrayBuffer, Blob and File.
  3282. *
  3283. * @param {Object} anything
  3284. * @api public
  3285. */
  3286. function hasBinary (obj) {
  3287. if (!obj || typeof obj !== 'object') {
  3288. return false;
  3289. }
  3290. if (isArray(obj)) {
  3291. for (var i = 0, l = obj.length; i < l; i++) {
  3292. if (hasBinary(obj[i])) {
  3293. return true;
  3294. }
  3295. }
  3296. return false;
  3297. }
  3298. if ((typeof Buffer === 'function' && Buffer.isBuffer && Buffer.isBuffer(obj)) ||
  3299. (typeof ArrayBuffer === 'function' && obj instanceof ArrayBuffer) ||
  3300. (withNativeBlob && obj instanceof Blob) ||
  3301. (withNativeFile && obj instanceof File)
  3302. ) {
  3303. return true;
  3304. }
  3305. // see: https://github.com/Automattic/has-binary/pull/4
  3306. if (obj.toJSON && typeof obj.toJSON === 'function' && arguments.length === 1) {
  3307. return hasBinary(obj.toJSON(), true);
  3308. }
  3309. for (var key in obj) {
  3310. if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
  3311. return true;
  3312. }
  3313. }
  3314. return false;
  3315. }
  3316. /***/ }),
  3317. /* 21 */
  3318. /***/ (function(module, exports) {
  3319. /**
  3320. * An abstraction for slicing an arraybuffer even when
  3321. * ArrayBuffer.prototype.slice is not supported
  3322. *
  3323. * @api public
  3324. */
  3325. module.exports = function(arraybuffer, start, end) {
  3326. var bytes = arraybuffer.byteLength;
  3327. start = start || 0;
  3328. end = end || bytes;
  3329. if (arraybuffer.slice) { return arraybuffer.slice(start, end); }
  3330. if (start < 0) { start += bytes; }
  3331. if (end < 0) { end += bytes; }
  3332. if (end > bytes) { end = bytes; }
  3333. if (start >= bytes || start >= end || bytes === 0) {
  3334. return new ArrayBuffer(0);
  3335. }
  3336. var abv = new Uint8Array(arraybuffer);
  3337. var result = new Uint8Array(end - start);
  3338. for (var i = start, ii = 0; i < end; i++, ii++) {
  3339. result[ii] = abv[i];
  3340. }
  3341. return result.buffer;
  3342. };
  3343. /***/ }),
  3344. /* 22 */
  3345. /***/ (function(module, exports) {
  3346. module.exports = after
  3347. function after(count, callback, err_cb) {
  3348. var bail = false
  3349. err_cb = err_cb || noop
  3350. proxy.count = count
  3351. return (count === 0) ? callback() : proxy
  3352. function proxy(err, result) {
  3353. if (proxy.count <= 0) {
  3354. throw new Error('after called too many times')
  3355. }
  3356. --proxy.count
  3357. // after first error, rest are passed to err_cb
  3358. if (err) {
  3359. bail = true
  3360. callback(err)
  3361. // future error callbacks will go to error handler
  3362. callback = err_cb
  3363. } else if (proxy.count === 0 && !bail) {
  3364. callback(null, result)
  3365. }
  3366. }
  3367. }
  3368. function noop() {}
  3369. /***/ }),
  3370. /* 23 */
  3371. /***/ (function(module, exports) {
  3372. /*! https://mths.be/utf8js v2.1.2 by @mathias */
  3373. var stringFromCharCode = String.fromCharCode;
  3374. // Taken from https://mths.be/punycode
  3375. function ucs2decode(string) {
  3376. var output = [];
  3377. var counter = 0;
  3378. var length = string.length;
  3379. var value;
  3380. var extra;
  3381. while (counter < length) {
  3382. value = string.charCodeAt(counter++);
  3383. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  3384. // high surrogate, and there is a next character
  3385. extra = string.charCodeAt(counter++);
  3386. if ((extra & 0xFC00) == 0xDC00) { // low surrogate
  3387. output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  3388. } else {
  3389. // unmatched surrogate; only append this code unit, in case the next
  3390. // code unit is the high surrogate of a surrogate pair
  3391. output.push(value);
  3392. counter--;
  3393. }
  3394. } else {
  3395. output.push(value);
  3396. }
  3397. }
  3398. return output;
  3399. }
  3400. // Taken from https://mths.be/punycode
  3401. function ucs2encode(array) {
  3402. var length = array.length;
  3403. var index = -1;
  3404. var value;
  3405. var output = '';
  3406. while (++index < length) {
  3407. value = array[index];
  3408. if (value > 0xFFFF) {
  3409. value -= 0x10000;
  3410. output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
  3411. value = 0xDC00 | value & 0x3FF;
  3412. }
  3413. output += stringFromCharCode(value);
  3414. }
  3415. return output;
  3416. }
  3417. function checkScalarValue(codePoint, strict) {
  3418. if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
  3419. if (strict) {
  3420. throw Error(
  3421. 'Lone surrogate U+' + codePoint.toString(16).toUpperCase() +
  3422. ' is not a scalar value'
  3423. );
  3424. }
  3425. return false;
  3426. }
  3427. return true;
  3428. }
  3429. /*--------------------------------------------------------------------------*/
  3430. function createByte(codePoint, shift) {
  3431. return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
  3432. }
  3433. function encodeCodePoint(codePoint, strict) {
  3434. if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
  3435. return stringFromCharCode(codePoint);
  3436. }
  3437. var symbol = '';
  3438. if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
  3439. symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
  3440. }
  3441. else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
  3442. if (!checkScalarValue(codePoint, strict)) {
  3443. codePoint = 0xFFFD;
  3444. }
  3445. symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
  3446. symbol += createByte(codePoint, 6);
  3447. }
  3448. else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
  3449. symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
  3450. symbol += createByte(codePoint, 12);
  3451. symbol += createByte(codePoint, 6);
  3452. }
  3453. symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
  3454. return symbol;
  3455. }
  3456. function utf8encode(string, opts) {
  3457. opts = opts || {};
  3458. var strict = false !== opts.strict;
  3459. var codePoints = ucs2decode(string);
  3460. var length = codePoints.length;
  3461. var index = -1;
  3462. var codePoint;
  3463. var byteString = '';
  3464. while (++index < length) {
  3465. codePoint = codePoints[index];
  3466. byteString += encodeCodePoint(codePoint, strict);
  3467. }
  3468. return byteString;
  3469. }
  3470. /*--------------------------------------------------------------------------*/
  3471. function readContinuationByte() {
  3472. if (byteIndex >= byteCount) {
  3473. throw Error('Invalid byte index');
  3474. }
  3475. var continuationByte = byteArray[byteIndex] & 0xFF;
  3476. byteIndex++;
  3477. if ((continuationByte & 0xC0) == 0x80) {
  3478. return continuationByte & 0x3F;
  3479. }
  3480. // If we end up here, it’s not a continuation byte
  3481. throw Error('Invalid continuation byte');
  3482. }
  3483. function decodeSymbol(strict) {
  3484. var byte1;
  3485. var byte2;
  3486. var byte3;
  3487. var byte4;
  3488. var codePoint;
  3489. if (byteIndex > byteCount) {
  3490. throw Error('Invalid byte index');
  3491. }
  3492. if (byteIndex == byteCount) {
  3493. return false;
  3494. }
  3495. // Read first byte
  3496. byte1 = byteArray[byteIndex] & 0xFF;
  3497. byteIndex++;
  3498. // 1-byte sequence (no continuation bytes)
  3499. if ((byte1 & 0x80) == 0) {
  3500. return byte1;
  3501. }
  3502. // 2-byte sequence
  3503. if ((byte1 & 0xE0) == 0xC0) {
  3504. byte2 = readContinuationByte();
  3505. codePoint = ((byte1 & 0x1F) << 6) | byte2;
  3506. if (codePoint >= 0x80) {
  3507. return codePoint;
  3508. } else {
  3509. throw Error('Invalid continuation byte');
  3510. }
  3511. }
  3512. // 3-byte sequence (may include unpaired surrogates)
  3513. if ((byte1 & 0xF0) == 0xE0) {
  3514. byte2 = readContinuationByte();
  3515. byte3 = readContinuationByte();
  3516. codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
  3517. if (codePoint >= 0x0800) {
  3518. return checkScalarValue(codePoint, strict) ? codePoint : 0xFFFD;
  3519. } else {
  3520. throw Error('Invalid continuation byte');
  3521. }
  3522. }
  3523. // 4-byte sequence
  3524. if ((byte1 & 0xF8) == 0xF0) {
  3525. byte2 = readContinuationByte();
  3526. byte3 = readContinuationByte();
  3527. byte4 = readContinuationByte();
  3528. codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) |
  3529. (byte3 << 0x06) | byte4;
  3530. if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
  3531. return codePoint;
  3532. }
  3533. }
  3534. throw Error('Invalid UTF-8 detected');
  3535. }
  3536. var byteArray;
  3537. var byteCount;
  3538. var byteIndex;
  3539. function utf8decode(byteString, opts) {
  3540. opts = opts || {};
  3541. var strict = false !== opts.strict;
  3542. byteArray = ucs2decode(byteString);
  3543. byteCount = byteArray.length;
  3544. byteIndex = 0;
  3545. var codePoints = [];
  3546. var tmp;
  3547. while ((tmp = decodeSymbol(strict)) !== false) {
  3548. codePoints.push(tmp);
  3549. }
  3550. return ucs2encode(codePoints);
  3551. }
  3552. module.exports = {
  3553. version: '2.1.2',
  3554. encode: utf8encode,
  3555. decode: utf8decode
  3556. };
  3557. /***/ }),
  3558. /* 24 */
  3559. /***/ (function(module, exports) {
  3560. /*
  3561. * base64-arraybuffer
  3562. * https://github.com/niklasvh/base64-arraybuffer
  3563. *
  3564. * Copyright (c) 2012 Niklas von Hertzen
  3565. * Licensed under the MIT license.
  3566. */
  3567. (function(){
  3568. "use strict";
  3569. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  3570. // Use a lookup table to find the index.
  3571. var lookup = new Uint8Array(256);
  3572. for (var i = 0; i < chars.length; i++) {
  3573. lookup[chars.charCodeAt(i)] = i;
  3574. }
  3575. exports.encode = function(arraybuffer) {
  3576. var bytes = new Uint8Array(arraybuffer),
  3577. i, len = bytes.length, base64 = "";
  3578. for (i = 0; i < len; i+=3) {
  3579. base64 += chars[bytes[i] >> 2];
  3580. base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
  3581. base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  3582. base64 += chars[bytes[i + 2] & 63];
  3583. }
  3584. if ((len % 3) === 2) {
  3585. base64 = base64.substring(0, base64.length - 1) + "=";
  3586. } else if (len % 3 === 1) {
  3587. base64 = base64.substring(0, base64.length - 2) + "==";
  3588. }
  3589. return base64;
  3590. };
  3591. exports.decode = function(base64) {
  3592. var bufferLength = base64.length * 0.75,
  3593. len = base64.length, i, p = 0,
  3594. encoded1, encoded2, encoded3, encoded4;
  3595. if (base64[base64.length - 1] === "=") {
  3596. bufferLength--;
  3597. if (base64[base64.length - 2] === "=") {
  3598. bufferLength--;
  3599. }
  3600. }
  3601. var arraybuffer = new ArrayBuffer(bufferLength),
  3602. bytes = new Uint8Array(arraybuffer);
  3603. for (i = 0; i < len; i+=4) {
  3604. encoded1 = lookup[base64.charCodeAt(i)];
  3605. encoded2 = lookup[base64.charCodeAt(i+1)];
  3606. encoded3 = lookup[base64.charCodeAt(i+2)];
  3607. encoded4 = lookup[base64.charCodeAt(i+3)];
  3608. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  3609. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  3610. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  3611. }
  3612. return arraybuffer;
  3613. };
  3614. })();
  3615. /***/ }),
  3616. /* 25 */
  3617. /***/ (function(module, exports) {
  3618. /**
  3619. * Create a blob builder even when vendor prefixes exist
  3620. */
  3621. var BlobBuilder = typeof BlobBuilder !== 'undefined' ? BlobBuilder :
  3622. typeof WebKitBlobBuilder !== 'undefined' ? WebKitBlobBuilder :
  3623. typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder :
  3624. typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder :
  3625. false;
  3626. /**
  3627. * Check if Blob constructor is supported
  3628. */
  3629. var blobSupported = (function() {
  3630. try {
  3631. var a = new Blob(['hi']);
  3632. return a.size === 2;
  3633. } catch(e) {
  3634. return false;
  3635. }
  3636. })();
  3637. /**
  3638. * Check if Blob constructor supports ArrayBufferViews
  3639. * Fails in Safari 6, so we need to map to ArrayBuffers there.
  3640. */
  3641. var blobSupportsArrayBufferView = blobSupported && (function() {
  3642. try {
  3643. var b = new Blob([new Uint8Array([1,2])]);
  3644. return b.size === 2;
  3645. } catch(e) {
  3646. return false;
  3647. }
  3648. })();
  3649. /**
  3650. * Check if BlobBuilder is supported
  3651. */
  3652. var blobBuilderSupported = BlobBuilder
  3653. && BlobBuilder.prototype.append
  3654. && BlobBuilder.prototype.getBlob;
  3655. /**
  3656. * Helper function that maps ArrayBufferViews to ArrayBuffers
  3657. * Used by BlobBuilder constructor and old browsers that didn't
  3658. * support it in the Blob constructor.
  3659. */
  3660. function mapArrayBufferViews(ary) {
  3661. return ary.map(function(chunk) {
  3662. if (chunk.buffer instanceof ArrayBuffer) {
  3663. var buf = chunk.buffer;
  3664. // if this is a subarray, make a copy so we only
  3665. // include the subarray region from the underlying buffer
  3666. if (chunk.byteLength !== buf.byteLength) {
  3667. var copy = new Uint8Array(chunk.byteLength);
  3668. copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength));
  3669. buf = copy.buffer;
  3670. }
  3671. return buf;
  3672. }
  3673. return chunk;
  3674. });
  3675. }
  3676. function BlobBuilderConstructor(ary, options) {
  3677. options = options || {};
  3678. var bb = new BlobBuilder();
  3679. mapArrayBufferViews(ary).forEach(function(part) {
  3680. bb.append(part);
  3681. });
  3682. return (options.type) ? bb.getBlob(options.type) : bb.getBlob();
  3683. };
  3684. function BlobConstructor(ary, options) {
  3685. return new Blob(mapArrayBufferViews(ary), options || {});
  3686. };
  3687. if (typeof Blob !== 'undefined') {
  3688. BlobBuilderConstructor.prototype = Blob.prototype;
  3689. BlobConstructor.prototype = Blob.prototype;
  3690. }
  3691. module.exports = (function() {
  3692. if (blobSupported) {
  3693. return blobSupportsArrayBufferView ? Blob : BlobConstructor;
  3694. } else if (blobBuilderSupported) {
  3695. return BlobBuilderConstructor;
  3696. } else {
  3697. return undefined;
  3698. }
  3699. })();
  3700. /***/ }),
  3701. /* 26 */
  3702. /***/ (function(module, exports) {
  3703. /**
  3704. * Compiles a querystring
  3705. * Returns string representation of the object
  3706. *
  3707. * @param {Object}
  3708. * @api private
  3709. */
  3710. exports.encode = function (obj) {
  3711. var str = '';
  3712. for (var i in obj) {
  3713. if (obj.hasOwnProperty(i)) {
  3714. if (str.length) str += '&';
  3715. str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
  3716. }
  3717. }
  3718. return str;
  3719. };
  3720. /**
  3721. * Parses a simple querystring into an object
  3722. *
  3723. * @param {String} qs
  3724. * @api private
  3725. */
  3726. exports.decode = function(qs){
  3727. var qry = {};
  3728. var pairs = qs.split('&');
  3729. for (var i = 0, l = pairs.length; i < l; i++) {
  3730. var pair = pairs[i].split('=');
  3731. qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
  3732. }
  3733. return qry;
  3734. };
  3735. /***/ }),
  3736. /* 27 */
  3737. /***/ (function(module, exports) {
  3738. module.exports = function(a, b){
  3739. var fn = function(){};
  3740. fn.prototype = b.prototype;
  3741. a.prototype = new fn;
  3742. a.prototype.constructor = a;
  3743. };
  3744. /***/ }),
  3745. /* 28 */
  3746. /***/ (function(module, exports) {
  3747. 'use strict';
  3748. var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'.split('')
  3749. , length = 64
  3750. , map = {}
  3751. , seed = 0
  3752. , i = 0
  3753. , prev;
  3754. /**
  3755. * Return a string representing the specified number.
  3756. *
  3757. * @param {Number} num The number to convert.
  3758. * @returns {String} The string representation of the number.
  3759. * @api public
  3760. */
  3761. function encode(num) {
  3762. var encoded = '';
  3763. do {
  3764. encoded = alphabet[num % length] + encoded;
  3765. num = Math.floor(num / length);
  3766. } while (num > 0);
  3767. return encoded;
  3768. }
  3769. /**
  3770. * Return the integer value specified by the given string.
  3771. *
  3772. * @param {String} str The string to convert.
  3773. * @returns {Number} The integer value represented by the string.
  3774. * @api public
  3775. */
  3776. function decode(str) {
  3777. var decoded = 0;
  3778. for (i = 0; i < str.length; i++) {
  3779. decoded = decoded * length + map[str.charAt(i)];
  3780. }
  3781. return decoded;
  3782. }
  3783. /**
  3784. * Yeast: A tiny growing id generator.
  3785. *
  3786. * @returns {String} A unique id.
  3787. * @api public
  3788. */
  3789. function yeast() {
  3790. var now = encode(+new Date());
  3791. if (now !== prev) return seed = 0, prev = now;
  3792. return now +'.'+ encode(seed++);
  3793. }
  3794. //
  3795. // Map each character to its index.
  3796. //
  3797. for (; i < length; i++) map[alphabet[i]] = i;
  3798. //
  3799. // Expose the `yeast`, `encode` and `decode` functions.
  3800. //
  3801. yeast.encode = encode;
  3802. yeast.decode = decode;
  3803. module.exports = yeast;
  3804. /***/ }),
  3805. /* 29 */
  3806. /***/ (function(module, exports, __webpack_require__) {
  3807. /* WEBPACK VAR INJECTION */(function(global) {/**
  3808. * Module requirements.
  3809. */
  3810. var Polling = __webpack_require__(16);
  3811. var inherit = __webpack_require__(27);
  3812. /**
  3813. * Module exports.
  3814. */
  3815. module.exports = JSONPPolling;
  3816. /**
  3817. * Cached regular expressions.
  3818. */
  3819. var rNewline = /\n/g;
  3820. var rEscapedNewline = /\\n/g;
  3821. /**
  3822. * Global JSONP callbacks.
  3823. */
  3824. var callbacks;
  3825. /**
  3826. * Noop.
  3827. */
  3828. function empty () { }
  3829. /**
  3830. * Until https://github.com/tc39/proposal-global is shipped.
  3831. */
  3832. function glob () {
  3833. return typeof self !== 'undefined' ? self
  3834. : typeof window !== 'undefined' ? window
  3835. : typeof global !== 'undefined' ? global : {};
  3836. }
  3837. /**
  3838. * JSONP Polling constructor.
  3839. *
  3840. * @param {Object} opts.
  3841. * @api public
  3842. */
  3843. function JSONPPolling (opts) {
  3844. Polling.call(this, opts);
  3845. this.query = this.query || {};
  3846. // define global callbacks array if not present
  3847. // we do this here (lazily) to avoid unneeded global pollution
  3848. if (!callbacks) {
  3849. // we need to consider multiple engines in the same page
  3850. var global = glob();
  3851. callbacks = global.___eio = (global.___eio || []);
  3852. }
  3853. // callback identifier
  3854. this.index = callbacks.length;
  3855. // add callback to jsonp global
  3856. var self = this;
  3857. callbacks.push(function (msg) {
  3858. self.onData(msg);
  3859. });
  3860. // append to query string
  3861. this.query.j = this.index;
  3862. // prevent spurious errors from being emitted when the window is unloaded
  3863. if (typeof addEventListener === 'function') {
  3864. addEventListener('beforeunload', function () {
  3865. if (self.script) self.script.onerror = empty;
  3866. }, false);
  3867. }
  3868. }
  3869. /**
  3870. * Inherits from Polling.
  3871. */
  3872. inherit(JSONPPolling, Polling);
  3873. /*
  3874. * JSONP only supports binary as base64 encoded strings
  3875. */
  3876. JSONPPolling.prototype.supportsBinary = false;
  3877. /**
  3878. * Closes the socket.
  3879. *
  3880. * @api private
  3881. */
  3882. JSONPPolling.prototype.doClose = function () {
  3883. if (this.script) {
  3884. this.script.parentNode.removeChild(this.script);
  3885. this.script = null;
  3886. }
  3887. if (this.form) {
  3888. this.form.parentNode.removeChild(this.form);
  3889. this.form = null;
  3890. this.iframe = null;
  3891. }
  3892. Polling.prototype.doClose.call(this);
  3893. };
  3894. /**
  3895. * Starts a poll cycle.
  3896. *
  3897. * @api private
  3898. */
  3899. JSONPPolling.prototype.doPoll = function () {
  3900. var self = this;
  3901. var script = document.createElement('script');
  3902. if (this.script) {
  3903. this.script.parentNode.removeChild(this.script);
  3904. this.script = null;
  3905. }
  3906. script.async = true;
  3907. script.src = this.uri();
  3908. script.onerror = function (e) {
  3909. self.onError('jsonp poll error', e);
  3910. };
  3911. var insertAt = document.getElementsByTagName('script')[0];
  3912. if (insertAt) {
  3913. insertAt.parentNode.insertBefore(script, insertAt);
  3914. } else {
  3915. (document.head || document.body).appendChild(script);
  3916. }
  3917. this.script = script;
  3918. var isUAgecko = 'undefined' !== typeof navigator && /gecko/i.test(navigator.userAgent);
  3919. if (isUAgecko) {
  3920. setTimeout(function () {
  3921. var iframe = document.createElement('iframe');
  3922. document.body.appendChild(iframe);
  3923. document.body.removeChild(iframe);
  3924. }, 100);
  3925. }
  3926. };
  3927. /**
  3928. * Writes with a hidden iframe.
  3929. *
  3930. * @param {String} data to send
  3931. * @param {Function} called upon flush.
  3932. * @api private
  3933. */
  3934. JSONPPolling.prototype.doWrite = function (data, fn) {
  3935. var self = this;
  3936. if (!this.form) {
  3937. var form = document.createElement('form');
  3938. var area = document.createElement('textarea');
  3939. var id = this.iframeId = 'eio_iframe_' + this.index;
  3940. var iframe;
  3941. form.className = 'socketio';
  3942. form.style.position = 'absolute';
  3943. form.style.top = '-1000px';
  3944. form.style.left = '-1000px';
  3945. form.target = id;
  3946. form.method = 'POST';
  3947. form.setAttribute('accept-charset', 'utf-8');
  3948. area.name = 'd';
  3949. form.appendChild(area);
  3950. document.body.appendChild(form);
  3951. this.form = form;
  3952. this.area = area;
  3953. }
  3954. this.form.action = this.uri();
  3955. function complete () {
  3956. initIframe();
  3957. fn();
  3958. }
  3959. function initIframe () {
  3960. if (self.iframe) {
  3961. try {
  3962. self.form.removeChild(self.iframe);
  3963. } catch (e) {
  3964. self.onError('jsonp polling iframe removal error', e);
  3965. }
  3966. }
  3967. try {
  3968. // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
  3969. var html = '<iframe src="javascript:0" name="' + self.iframeId + '">';
  3970. iframe = document.createElement(html);
  3971. } catch (e) {
  3972. iframe = document.createElement('iframe');
  3973. iframe.name = self.iframeId;
  3974. iframe.src = 'javascript:0';
  3975. }
  3976. iframe.id = self.iframeId;
  3977. self.form.appendChild(iframe);
  3978. self.iframe = iframe;
  3979. }
  3980. initIframe();
  3981. // escape \n to prevent it from being converted into \r\n by some UAs
  3982. // double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
  3983. data = data.replace(rEscapedNewline, '\\\n');
  3984. this.area.value = data.replace(rNewline, '\\n');
  3985. try {
  3986. this.form.submit();
  3987. } catch (e) {}
  3988. if (this.iframe.attachEvent) {
  3989. this.iframe.onreadystatechange = function () {
  3990. if (self.iframe.readyState === 'complete') {
  3991. complete();
  3992. }
  3993. };
  3994. } else {
  3995. this.iframe.onload = complete;
  3996. }
  3997. };
  3998. /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))
  3999. /***/ }),
  4000. /* 30 */
  4001. /***/ (function(module, exports, __webpack_require__) {
  4002. /**
  4003. * Module dependencies.
  4004. */
  4005. var Transport = __webpack_require__(17);
  4006. var parser = __webpack_require__(18);
  4007. var parseqs = __webpack_require__(26);
  4008. var inherit = __webpack_require__(27);
  4009. var yeast = __webpack_require__(28);
  4010. var debug = __webpack_require__(3)('engine.io-client:websocket');
  4011. var BrowserWebSocket, NodeWebSocket;
  4012. if (typeof self === 'undefined') {
  4013. try {
  4014. NodeWebSocket = __webpack_require__(31);
  4015. } catch (e) { }
  4016. } else {
  4017. BrowserWebSocket = self.WebSocket || self.MozWebSocket;
  4018. }
  4019. /**
  4020. * Get either the `WebSocket` or `MozWebSocket` globals
  4021. * in the browser or try to resolve WebSocket-compatible
  4022. * interface exposed by `ws` for Node-like environment.
  4023. */
  4024. var WebSocket = BrowserWebSocket || NodeWebSocket;
  4025. /**
  4026. * Module exports.
  4027. */
  4028. module.exports = WS;
  4029. /**
  4030. * WebSocket transport constructor.
  4031. *
  4032. * @api {Object} connection options
  4033. * @api public
  4034. */
  4035. function WS (opts) {
  4036. var forceBase64 = (opts && opts.forceBase64);
  4037. if (forceBase64) {
  4038. this.supportsBinary = false;
  4039. }
  4040. this.perMessageDeflate = opts.perMessageDeflate;
  4041. this.usingBrowserWebSocket = BrowserWebSocket && !opts.forceNode;
  4042. this.protocols = opts.protocols;
  4043. if (!this.usingBrowserWebSocket) {
  4044. WebSocket = NodeWebSocket;
  4045. }
  4046. Transport.call(this, opts);
  4047. }
  4048. /**
  4049. * Inherits from Transport.
  4050. */
  4051. inherit(WS, Transport);
  4052. /**
  4053. * Transport name.
  4054. *
  4055. * @api public
  4056. */
  4057. WS.prototype.name = 'websocket';
  4058. /*
  4059. * WebSockets support binary
  4060. */
  4061. WS.prototype.supportsBinary = true;
  4062. /**
  4063. * Opens socket.
  4064. *
  4065. * @api private
  4066. */
  4067. WS.prototype.doOpen = function () {
  4068. if (!this.check()) {
  4069. // let probe timeout
  4070. return;
  4071. }
  4072. var uri = this.uri();
  4073. var protocols = this.protocols;
  4074. var opts = {
  4075. agent: this.agent,
  4076. perMessageDeflate: this.perMessageDeflate
  4077. };
  4078. // SSL options for Node.js client
  4079. opts.pfx = this.pfx;
  4080. opts.key = this.key;
  4081. opts.passphrase = this.passphrase;
  4082. opts.cert = this.cert;
  4083. opts.ca = this.ca;
  4084. opts.ciphers = this.ciphers;
  4085. opts.rejectUnauthorized = this.rejectUnauthorized;
  4086. if (this.extraHeaders) {
  4087. opts.headers = this.extraHeaders;
  4088. }
  4089. if (this.localAddress) {
  4090. opts.localAddress = this.localAddress;
  4091. }
  4092. try {
  4093. this.ws = this.usingBrowserWebSocket && !this.isReactNative ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts);
  4094. } catch (err) {
  4095. return this.emit('error', err);
  4096. }
  4097. if (this.ws.binaryType === undefined) {
  4098. this.supportsBinary = false;
  4099. }
  4100. if (this.ws.supports && this.ws.supports.binary) {
  4101. this.supportsBinary = true;
  4102. this.ws.binaryType = 'nodebuffer';
  4103. } else {
  4104. this.ws.binaryType = 'arraybuffer';
  4105. }
  4106. this.addEventListeners();
  4107. };
  4108. /**
  4109. * Adds event listeners to the socket
  4110. *
  4111. * @api private
  4112. */
  4113. WS.prototype.addEventListeners = function () {
  4114. var self = this;
  4115. this.ws.onopen = function () {
  4116. self.onOpen();
  4117. };
  4118. this.ws.onclose = function () {
  4119. self.onClose();
  4120. };
  4121. this.ws.onmessage = function (ev) {
  4122. self.onData(ev.data);
  4123. };
  4124. this.ws.onerror = function (e) {
  4125. self.onError('websocket error', e);
  4126. };
  4127. };
  4128. /**
  4129. * Writes data to socket.
  4130. *
  4131. * @param {Array} array of packets.
  4132. * @api private
  4133. */
  4134. WS.prototype.write = function (packets) {
  4135. var self = this;
  4136. this.writable = false;
  4137. // encodePacket efficient as it uses WS framing
  4138. // no need for encodePayload
  4139. var total = packets.length;
  4140. for (var i = 0, l = total; i < l; i++) {
  4141. (function (packet) {
  4142. parser.encodePacket(packet, self.supportsBinary, function (data) {
  4143. if (!self.usingBrowserWebSocket) {
  4144. // always create a new object (GH-437)
  4145. var opts = {};
  4146. if (packet.options) {
  4147. opts.compress = packet.options.compress;
  4148. }
  4149. if (self.perMessageDeflate) {
  4150. var len = 'string' === typeof data ? Buffer.byteLength(data) : data.length;
  4151. if (len < self.perMessageDeflate.threshold) {
  4152. opts.compress = false;
  4153. }
  4154. }
  4155. }
  4156. // Sometimes the websocket has already been closed but the browser didn't
  4157. // have a chance of informing us about it yet, in that case send will
  4158. // throw an error
  4159. try {
  4160. if (self.usingBrowserWebSocket) {
  4161. // TypeError is thrown when passing the second argument on Safari
  4162. self.ws.send(data);
  4163. } else {
  4164. self.ws.send(data, opts);
  4165. }
  4166. } catch (e) {
  4167. }
  4168. --total || done();
  4169. });
  4170. })(packets[i]);
  4171. }
  4172. function done () {
  4173. self.emit('flush');
  4174. // fake drain
  4175. // defer to next tick to allow Socket to clear writeBuffer
  4176. setTimeout(function () {
  4177. self.writable = true;
  4178. self.emit('drain');
  4179. }, 0);
  4180. }
  4181. };
  4182. /**
  4183. * Called upon close
  4184. *
  4185. * @api private
  4186. */
  4187. WS.prototype.onClose = function () {
  4188. Transport.prototype.onClose.call(this);
  4189. };
  4190. /**
  4191. * Closes socket.
  4192. *
  4193. * @api private
  4194. */
  4195. WS.prototype.doClose = function () {
  4196. if (typeof this.ws !== 'undefined') {
  4197. this.ws.close();
  4198. }
  4199. };
  4200. /**
  4201. * Generates uri for connection.
  4202. *
  4203. * @api private
  4204. */
  4205. WS.prototype.uri = function () {
  4206. var query = this.query || {};
  4207. var schema = this.secure ? 'wss' : 'ws';
  4208. var port = '';
  4209. // avoid port if default for schema
  4210. if (this.port && (('wss' === schema && Number(this.port) !== 443) ||
  4211. ('ws' === schema && Number(this.port) !== 80))) {
  4212. port = ':' + this.port;
  4213. }
  4214. // append timestamp to URI
  4215. if (this.timestampRequests) {
  4216. query[this.timestampParam] = yeast();
  4217. }
  4218. // communicate binary support capabilities
  4219. if (!this.supportsBinary) {
  4220. query.b64 = 1;
  4221. }
  4222. query = parseqs.encode(query);
  4223. // prepend ? to query
  4224. if (query.length) {
  4225. query = '?' + query;
  4226. }
  4227. var ipv6 = this.hostname.indexOf(':') !== -1;
  4228. return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query;
  4229. };
  4230. /**
  4231. * Feature detection for WebSocket.
  4232. *
  4233. * @return {Boolean} whether this transport is available.
  4234. * @api public
  4235. */
  4236. WS.prototype.check = function () {
  4237. return !!WebSocket && !('__initialize' in WebSocket && this.name === WS.prototype.name);
  4238. };
  4239. /***/ }),
  4240. /* 31 */
  4241. /***/ (function(module, exports) {
  4242. /* (ignored) */
  4243. /***/ }),
  4244. /* 32 */
  4245. /***/ (function(module, exports) {
  4246. var indexOf = [].indexOf;
  4247. module.exports = function(arr, obj){
  4248. if (indexOf) return arr.indexOf(obj);
  4249. for (var i = 0; i < arr.length; ++i) {
  4250. if (arr[i] === obj) return i;
  4251. }
  4252. return -1;
  4253. };
  4254. /***/ }),
  4255. /* 33 */
  4256. /***/ (function(module, exports, __webpack_require__) {
  4257. 'use strict';
  4258. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  4259. /**
  4260. * Module dependencies.
  4261. */
  4262. var parser = __webpack_require__(4);
  4263. var Emitter = __webpack_require__(5);
  4264. var toArray = __webpack_require__(34);
  4265. var on = __webpack_require__(35);
  4266. var bind = __webpack_require__(36);
  4267. var debug = __webpack_require__(3)('socket.io-client:socket');
  4268. var parseqs = __webpack_require__(26);
  4269. var hasBin = __webpack_require__(20);
  4270. /**
  4271. * Module exports.
  4272. */
  4273. module.exports = exports = Socket;
  4274. /**
  4275. * Internal events (blacklisted).
  4276. * These events can't be emitted by the user.
  4277. *
  4278. * @api private
  4279. */
  4280. var events = {
  4281. connect: 1,
  4282. connect_error: 1,
  4283. connect_timeout: 1,
  4284. connecting: 1,
  4285. disconnect: 1,
  4286. error: 1,
  4287. reconnect: 1,
  4288. reconnect_attempt: 1,
  4289. reconnect_failed: 1,
  4290. reconnect_error: 1,
  4291. reconnecting: 1,
  4292. ping: 1,
  4293. pong: 1
  4294. };
  4295. /**
  4296. * Shortcut to `Emitter#emit`.
  4297. */
  4298. var emit = Emitter.prototype.emit;
  4299. /**
  4300. * `Socket` constructor.
  4301. *
  4302. * @api public
  4303. */
  4304. function Socket(io, nsp, opts) {
  4305. this.io = io;
  4306. this.nsp = nsp;
  4307. this.json = this; // compat
  4308. this.ids = 0;
  4309. this.acks = {};
  4310. this.receiveBuffer = [];
  4311. this.sendBuffer = [];
  4312. this.connected = false;
  4313. this.disconnected = true;
  4314. this.flags = {};
  4315. if (opts && opts.query) {
  4316. this.query = opts.query;
  4317. }
  4318. if (this.io.autoConnect) this.open();
  4319. }
  4320. /**
  4321. * Mix in `Emitter`.
  4322. */
  4323. Emitter(Socket.prototype);
  4324. /**
  4325. * Subscribe to open, close and packet events
  4326. *
  4327. * @api private
  4328. */
  4329. Socket.prototype.subEvents = function () {
  4330. if (this.subs) return;
  4331. var io = this.io;
  4332. this.subs = [on(io, 'open', bind(this, 'onopen')), on(io, 'packet', bind(this, 'onpacket')), on(io, 'close', bind(this, 'onclose'))];
  4333. };
  4334. /**
  4335. * "Opens" the socket.
  4336. *
  4337. * @api public
  4338. */
  4339. Socket.prototype.open = Socket.prototype.connect = function () {
  4340. if (this.connected) return this;
  4341. this.subEvents();
  4342. this.io.open(); // ensure open
  4343. if ('open' === this.io.readyState) this.onopen();
  4344. this.emit('connecting');
  4345. return this;
  4346. };
  4347. /**
  4348. * Sends a `message` event.
  4349. *
  4350. * @return {Socket} self
  4351. * @api public
  4352. */
  4353. Socket.prototype.send = function () {
  4354. var args = toArray(arguments);
  4355. args.unshift('message');
  4356. this.emit.apply(this, args);
  4357. return this;
  4358. };
  4359. /**
  4360. * Override `emit`.
  4361. * If the event is in `events`, it's emitted normally.
  4362. *
  4363. * @param {String} event name
  4364. * @return {Socket} self
  4365. * @api public
  4366. */
  4367. Socket.prototype.emit = function (ev) {
  4368. if (events.hasOwnProperty(ev)) {
  4369. emit.apply(this, arguments);
  4370. return this;
  4371. }
  4372. var args = toArray(arguments);
  4373. var packet = {
  4374. type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT,
  4375. data: args
  4376. };
  4377. packet.options = {};
  4378. packet.options.compress = !this.flags || false !== this.flags.compress;
  4379. // event ack callback
  4380. if ('function' === typeof args[args.length - 1]) {
  4381. this.acks[this.ids] = args.pop();
  4382. packet.id = this.ids++;
  4383. }
  4384. if (this.connected) {
  4385. this.packet(packet);
  4386. } else {
  4387. this.sendBuffer.push(packet);
  4388. }
  4389. this.flags = {};
  4390. return this;
  4391. };
  4392. /**
  4393. * Sends a packet.
  4394. *
  4395. * @param {Object} packet
  4396. * @api private
  4397. */
  4398. Socket.prototype.packet = function (packet) {
  4399. packet.nsp = this.nsp;
  4400. this.io.packet(packet);
  4401. };
  4402. /**
  4403. * Called upon engine `open`.
  4404. *
  4405. * @api private
  4406. */
  4407. Socket.prototype.onopen = function () {
  4408. // write connect packet if necessary
  4409. if ('/' !== this.nsp) {
  4410. if (this.query) {
  4411. var query = _typeof(this.query) === 'object' ? parseqs.encode(this.query) : this.query;
  4412. this.packet({ type: parser.CONNECT, query: query });
  4413. } else {
  4414. this.packet({ type: parser.CONNECT });
  4415. }
  4416. }
  4417. };
  4418. /**
  4419. * Called upon engine `close`.
  4420. *
  4421. * @param {String} reason
  4422. * @api private
  4423. */
  4424. Socket.prototype.onclose = function (reason) {
  4425. this.connected = false;
  4426. this.disconnected = true;
  4427. delete this.id;
  4428. this.emit('disconnect', reason);
  4429. };
  4430. /**
  4431. * Called with socket packet.
  4432. *
  4433. * @param {Object} packet
  4434. * @api private
  4435. */
  4436. Socket.prototype.onpacket = function (packet) {
  4437. var sameNamespace = packet.nsp === this.nsp;
  4438. var rootNamespaceError = packet.type === parser.ERROR && packet.nsp === '/';
  4439. if (!sameNamespace && !rootNamespaceError) return;
  4440. switch (packet.type) {
  4441. case parser.CONNECT:
  4442. this.onconnect();
  4443. break;
  4444. case parser.EVENT:
  4445. this.onevent(packet);
  4446. break;
  4447. case parser.BINARY_EVENT:
  4448. this.onevent(packet);
  4449. break;
  4450. case parser.ACK:
  4451. this.onack(packet);
  4452. break;
  4453. case parser.BINARY_ACK:
  4454. this.onack(packet);
  4455. break;
  4456. case parser.DISCONNECT:
  4457. this.ondisconnect();
  4458. break;
  4459. case parser.ERROR:
  4460. this.emit('error', packet.data);
  4461. break;
  4462. }
  4463. };
  4464. /**
  4465. * Called upon a server event.
  4466. *
  4467. * @param {Object} packet
  4468. * @api private
  4469. */
  4470. Socket.prototype.onevent = function (packet) {
  4471. var args = packet.data || [];
  4472. if (null != packet.id) {
  4473. args.push(this.ack(packet.id));
  4474. }
  4475. if (this.connected) {
  4476. emit.apply(this, args);
  4477. } else {
  4478. this.receiveBuffer.push(args);
  4479. }
  4480. };
  4481. /**
  4482. * Produces an ack callback to emit with an event.
  4483. *
  4484. * @api private
  4485. */
  4486. Socket.prototype.ack = function (id) {
  4487. var self = this;
  4488. var sent = false;
  4489. return function () {
  4490. // prevent double callbacks
  4491. if (sent) return;
  4492. sent = true;
  4493. var args = toArray(arguments);
  4494. self.packet({
  4495. type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
  4496. id: id,
  4497. data: args
  4498. });
  4499. };
  4500. };
  4501. /**
  4502. * Called upon a server acknowlegement.
  4503. *
  4504. * @param {Object} packet
  4505. * @api private
  4506. */
  4507. Socket.prototype.onack = function (packet) {
  4508. var ack = this.acks[packet.id];
  4509. if ('function' === typeof ack) {
  4510. ack.apply(this, packet.data);
  4511. delete this.acks[packet.id];
  4512. } else {}
  4513. };
  4514. /**
  4515. * Called upon server connect.
  4516. *
  4517. * @api private
  4518. */
  4519. Socket.prototype.onconnect = function () {
  4520. this.connected = true;
  4521. this.disconnected = false;
  4522. this.emit('connect');
  4523. this.emitBuffered();
  4524. };
  4525. /**
  4526. * Emit buffered events (received and emitted).
  4527. *
  4528. * @api private
  4529. */
  4530. Socket.prototype.emitBuffered = function () {
  4531. var i;
  4532. for (i = 0; i < this.receiveBuffer.length; i++) {
  4533. emit.apply(this, this.receiveBuffer[i]);
  4534. }
  4535. this.receiveBuffer = [];
  4536. for (i = 0; i < this.sendBuffer.length; i++) {
  4537. this.packet(this.sendBuffer[i]);
  4538. }
  4539. this.sendBuffer = [];
  4540. };
  4541. /**
  4542. * Called upon server disconnect.
  4543. *
  4544. * @api private
  4545. */
  4546. Socket.prototype.ondisconnect = function () {
  4547. this.destroy();
  4548. this.onclose('io server disconnect');
  4549. };
  4550. /**
  4551. * Called upon forced client/server side disconnections,
  4552. * this method ensures the manager stops tracking us and
  4553. * that reconnections don't get triggered for this.
  4554. *
  4555. * @api private.
  4556. */
  4557. Socket.prototype.destroy = function () {
  4558. if (this.subs) {
  4559. // clean subscriptions to avoid reconnections
  4560. for (var i = 0; i < this.subs.length; i++) {
  4561. this.subs[i].destroy();
  4562. }
  4563. this.subs = null;
  4564. }
  4565. this.io.destroy(this);
  4566. };
  4567. /**
  4568. * Disconnects the socket manually.
  4569. *
  4570. * @return {Socket} self
  4571. * @api public
  4572. */
  4573. Socket.prototype.close = Socket.prototype.disconnect = function () {
  4574. if (this.connected) {
  4575. this.packet({ type: parser.DISCONNECT });
  4576. }
  4577. // remove socket from pool
  4578. this.destroy();
  4579. if (this.connected) {
  4580. // fire events
  4581. this.onclose('io client disconnect');
  4582. }
  4583. return this;
  4584. };
  4585. /**
  4586. * Sets the compress flag.
  4587. *
  4588. * @param {Boolean} if `true`, compresses the sending data
  4589. * @return {Socket} self
  4590. * @api public
  4591. */
  4592. Socket.prototype.compress = function (compress) {
  4593. this.flags.compress = compress;
  4594. return this;
  4595. };
  4596. /**
  4597. * Sets the binary flag
  4598. *
  4599. * @param {Boolean} whether the emitted data contains binary
  4600. * @return {Socket} self
  4601. * @api public
  4602. */
  4603. Socket.prototype.binary = function (binary) {
  4604. this.flags.binary = binary;
  4605. return this;
  4606. };
  4607. /***/ }),
  4608. /* 34 */
  4609. /***/ (function(module, exports) {
  4610. module.exports = toArray
  4611. function toArray(list, index) {
  4612. var array = []
  4613. index = index || 0
  4614. for (var i = index || 0; i < list.length; i++) {
  4615. array[i - index] = list[i]
  4616. }
  4617. return array
  4618. }
  4619. /***/ }),
  4620. /* 35 */
  4621. /***/ (function(module, exports) {
  4622. "use strict";
  4623. /**
  4624. * Module exports.
  4625. */
  4626. module.exports = on;
  4627. /**
  4628. * Helper for subscriptions.
  4629. *
  4630. * @param {Object|EventEmitter} obj with `Emitter` mixin or `EventEmitter`
  4631. * @param {String} event name
  4632. * @param {Function} callback
  4633. * @api public
  4634. */
  4635. function on(obj, ev, fn) {
  4636. obj.on(ev, fn);
  4637. return {
  4638. destroy: function destroy() {
  4639. obj.removeListener(ev, fn);
  4640. }
  4641. };
  4642. }
  4643. /***/ }),
  4644. /* 36 */
  4645. /***/ (function(module, exports) {
  4646. /**
  4647. * Slice reference.
  4648. */
  4649. var slice = [].slice;
  4650. /**
  4651. * Bind `obj` to `fn`.
  4652. *
  4653. * @param {Object} obj
  4654. * @param {Function|String} fn or string
  4655. * @return {Function}
  4656. * @api public
  4657. */
  4658. module.exports = function(obj, fn){
  4659. if ('string' == typeof fn) fn = obj[fn];
  4660. if ('function' != typeof fn) throw new Error('bind() requires a function');
  4661. var args = slice.call(arguments, 2);
  4662. return function(){
  4663. return fn.apply(obj, args.concat(slice.call(arguments)));
  4664. }
  4665. };
  4666. /***/ }),
  4667. /* 37 */
  4668. /***/ (function(module, exports) {
  4669. /**
  4670. * Expose `Backoff`.
  4671. */
  4672. module.exports = Backoff;
  4673. /**
  4674. * Initialize backoff timer with `opts`.
  4675. *
  4676. * - `min` initial timeout in milliseconds [100]
  4677. * - `max` max timeout [10000]
  4678. * - `jitter` [0]
  4679. * - `factor` [2]
  4680. *
  4681. * @param {Object} opts
  4682. * @api public
  4683. */
  4684. function Backoff(opts) {
  4685. opts = opts || {};
  4686. this.ms = opts.min || 100;
  4687. this.max = opts.max || 10000;
  4688. this.factor = opts.factor || 2;
  4689. this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
  4690. this.attempts = 0;
  4691. }
  4692. /**
  4693. * Return the backoff duration.
  4694. *
  4695. * @return {Number}
  4696. * @api public
  4697. */
  4698. Backoff.prototype.duration = function(){
  4699. var ms = this.ms * Math.pow(this.factor, this.attempts++);
  4700. if (this.jitter) {
  4701. var rand = Math.random();
  4702. var deviation = Math.floor(rand * this.jitter * ms);
  4703. ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
  4704. }
  4705. return Math.min(ms, this.max) | 0;
  4706. };
  4707. /**
  4708. * Reset the number of attempts.
  4709. *
  4710. * @api public
  4711. */
  4712. Backoff.prototype.reset = function(){
  4713. this.attempts = 0;
  4714. };
  4715. /**
  4716. * Set the minimum duration
  4717. *
  4718. * @api public
  4719. */
  4720. Backoff.prototype.setMin = function(min){
  4721. this.ms = min;
  4722. };
  4723. /**
  4724. * Set the maximum duration
  4725. *
  4726. * @api public
  4727. */
  4728. Backoff.prototype.setMax = function(max){
  4729. this.max = max;
  4730. };
  4731. /**
  4732. * Set the jitter
  4733. *
  4734. * @api public
  4735. */
  4736. Backoff.prototype.setJitter = function(jitter){
  4737. this.jitter = jitter;
  4738. };
  4739. /***/ })
  4740. /******/ ])
  4741. });
  4742. ;
  4743. //# sourceMappingURL=socket.io.slim.dev.js.map