socket.io.slim.dev.js 131 KB

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