index.standalone.js 719 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var require$$2 = require('util');
  4. var require$$0 = require('buffer');
  5. var require$$1 = require('events');
  6. var require$$0$1 = require('stream');
  7. var require$$1$1 = require('crypto');
  8. var require$$2$1 = require('url');
  9. var require$$0$2 = require('assert');
  10. var require$$1$2 = require('net');
  11. var require$$2$2 = require('tls');
  12. var require$$1$3 = require('@firebase/util');
  13. var require$$2$3 = require('tslib');
  14. var require$$3 = require('@firebase/logger');
  15. var component = require('@firebase/component');
  16. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  17. var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2);
  18. var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
  19. var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
  20. var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
  21. var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
  22. var require$$2__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$2$1);
  23. var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
  24. var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
  25. var require$$2__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$2$2);
  26. var require$$1__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$1$3);
  27. var require$$2__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$2$3);
  28. var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3);
  29. var index_standalone = {};
  30. var safeBuffer = {exports: {}};
  31. /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  32. (function (module, exports) {
  33. /* eslint-disable node/no-deprecated-api */
  34. var buffer = require$$0__default["default"];
  35. var Buffer = buffer.Buffer;
  36. // alternative to using Object.keys for old browsers
  37. function copyProps (src, dst) {
  38. for (var key in src) {
  39. dst[key] = src[key];
  40. }
  41. }
  42. if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
  43. module.exports = buffer;
  44. } else {
  45. // Copy properties from require('buffer')
  46. copyProps(buffer, exports);
  47. exports.Buffer = SafeBuffer;
  48. }
  49. function SafeBuffer (arg, encodingOrOffset, length) {
  50. return Buffer(arg, encodingOrOffset, length)
  51. }
  52. SafeBuffer.prototype = Object.create(Buffer.prototype);
  53. // Copy static methods from Buffer
  54. copyProps(Buffer, SafeBuffer);
  55. SafeBuffer.from = function (arg, encodingOrOffset, length) {
  56. if (typeof arg === 'number') {
  57. throw new TypeError('Argument must not be a number')
  58. }
  59. return Buffer(arg, encodingOrOffset, length)
  60. };
  61. SafeBuffer.alloc = function (size, fill, encoding) {
  62. if (typeof size !== 'number') {
  63. throw new TypeError('Argument must be a number')
  64. }
  65. var buf = Buffer(size);
  66. if (fill !== undefined) {
  67. if (typeof encoding === 'string') {
  68. buf.fill(fill, encoding);
  69. } else {
  70. buf.fill(fill);
  71. }
  72. } else {
  73. buf.fill(0);
  74. }
  75. return buf
  76. };
  77. SafeBuffer.allocUnsafe = function (size) {
  78. if (typeof size !== 'number') {
  79. throw new TypeError('Argument must be a number')
  80. }
  81. return Buffer(size)
  82. };
  83. SafeBuffer.allocUnsafeSlow = function (size) {
  84. if (typeof size !== 'number') {
  85. throw new TypeError('Argument must be a number')
  86. }
  87. return buffer.SlowBuffer(size)
  88. };
  89. }(safeBuffer, safeBuffer.exports));
  90. var streams$1 = {};
  91. /**
  92. Streams in a WebSocket connection
  93. ---------------------------------
  94. We model a WebSocket as two duplex streams: one stream is for the wire protocol
  95. over an I/O socket, and the other is for incoming/outgoing messages.
  96. +----------+ +---------+ +----------+
  97. [1] write(chunk) -->| ~~~~~~~~ +----->| parse() +----->| ~~~~~~~~ +--> emit('data') [2]
  98. | | +----+----+ | |
  99. | | | | |
  100. | IO | | [5] | Messages |
  101. | | V | |
  102. | | +---------+ | |
  103. [4] emit('data') <--+ ~~~~~~~~ |<-----+ frame() |<-----+ ~~~~~~~~ |<-- write(chunk) [3]
  104. +----------+ +---------+ +----------+
  105. Message transfer in each direction is simple: IO receives a byte stream [1] and
  106. sends this stream for parsing. The parser will periodically emit a complete
  107. message text on the Messages stream [2]. Similarly, when messages are written
  108. to the Messages stream [3], they are framed using the WebSocket wire format and
  109. emitted via IO [4].
  110. There is a feedback loop via [5] since some input from [1] will be things like
  111. ping, pong and close frames. In these cases the protocol responds by emitting
  112. responses directly back to [4] rather than emitting messages via [2].
  113. For the purposes of flow control, we consider the sources of each Readable
  114. stream to be as follows:
  115. * [2] receives input from [1]
  116. * [4] receives input from [1] and [3]
  117. The classes below express the relationships described above without prescribing
  118. anything about how parse() and frame() work, other than assuming they emit
  119. 'data' events to the IO and Messages streams. They will work with any protocol
  120. driver having these two methods.
  121. **/
  122. var Stream$3 = require$$0__default$1["default"].Stream,
  123. util$c = require$$2__default["default"];
  124. var IO = function(driver) {
  125. this.readable = this.writable = true;
  126. this._paused = false;
  127. this._driver = driver;
  128. };
  129. util$c.inherits(IO, Stream$3);
  130. // The IO pause() and resume() methods will be called when the socket we are
  131. // piping to gets backed up and drains. Since IO output [4] comes from IO input
  132. // [1] and Messages input [3], we need to tell both of those to return false
  133. // from write() when this stream is paused.
  134. IO.prototype.pause = function() {
  135. this._paused = true;
  136. this._driver.messages._paused = true;
  137. };
  138. IO.prototype.resume = function() {
  139. this._paused = false;
  140. this.emit('drain');
  141. var messages = this._driver.messages;
  142. messages._paused = false;
  143. messages.emit('drain');
  144. };
  145. // When we receive input from a socket, send it to the parser and tell the
  146. // source whether to back off.
  147. IO.prototype.write = function(chunk) {
  148. if (!this.writable) return false;
  149. this._driver.parse(chunk);
  150. return !this._paused;
  151. };
  152. // The IO end() method will be called when the socket piping into it emits
  153. // 'close' or 'end', i.e. the socket is closed. In this situation the Messages
  154. // stream will not emit any more data so we emit 'end'.
  155. IO.prototype.end = function(chunk) {
  156. if (!this.writable) return;
  157. if (chunk !== undefined) this.write(chunk);
  158. this.writable = false;
  159. var messages = this._driver.messages;
  160. if (messages.readable) {
  161. messages.readable = messages.writable = false;
  162. messages.emit('end');
  163. }
  164. };
  165. IO.prototype.destroy = function() {
  166. this.end();
  167. };
  168. var Messages = function(driver) {
  169. this.readable = this.writable = true;
  170. this._paused = false;
  171. this._driver = driver;
  172. };
  173. util$c.inherits(Messages, Stream$3);
  174. // The Messages pause() and resume() methods will be called when the app that's
  175. // processing the messages gets backed up and drains. If we're emitting
  176. // messages too fast we should tell the source to slow down. Message output [2]
  177. // comes from IO input [1].
  178. Messages.prototype.pause = function() {
  179. this._driver.io._paused = true;
  180. };
  181. Messages.prototype.resume = function() {
  182. this._driver.io._paused = false;
  183. this._driver.io.emit('drain');
  184. };
  185. // When we receive messages from the user, send them to the formatter and tell
  186. // the source whether to back off.
  187. Messages.prototype.write = function(message) {
  188. if (!this.writable) return false;
  189. if (typeof message === 'string') this._driver.text(message);
  190. else this._driver.binary(message);
  191. return !this._paused;
  192. };
  193. // The Messages end() method will be called when a stream piping into it emits
  194. // 'end'. Many streams may be piped into the WebSocket and one of them ending
  195. // does not mean the whole socket is done, so just process the input and move
  196. // on leaving the socket open.
  197. Messages.prototype.end = function(message) {
  198. if (message !== undefined) this.write(message);
  199. };
  200. Messages.prototype.destroy = function() {};
  201. streams$1.IO = IO;
  202. streams$1.Messages = Messages;
  203. var Headers$3 = function() {
  204. this.clear();
  205. };
  206. Headers$3.prototype.ALLOWED_DUPLICATES = ['set-cookie', 'set-cookie2', 'warning', 'www-authenticate'];
  207. Headers$3.prototype.clear = function() {
  208. this._sent = {};
  209. this._lines = [];
  210. };
  211. Headers$3.prototype.set = function(name, value) {
  212. if (value === undefined) return;
  213. name = this._strip(name);
  214. value = this._strip(value);
  215. var key = name.toLowerCase();
  216. if (!this._sent.hasOwnProperty(key) || this.ALLOWED_DUPLICATES.indexOf(key) >= 0) {
  217. this._sent[key] = true;
  218. this._lines.push(name + ': ' + value + '\r\n');
  219. }
  220. };
  221. Headers$3.prototype.toString = function() {
  222. return this._lines.join('');
  223. };
  224. Headers$3.prototype._strip = function(string) {
  225. return string.toString().replace(/^ */, '').replace(/ *$/, '');
  226. };
  227. var headers = Headers$3;
  228. var Buffer$9 = safeBuffer.exports.Buffer;
  229. var StreamReader = function() {
  230. this._queue = [];
  231. this._queueSize = 0;
  232. this._offset = 0;
  233. };
  234. StreamReader.prototype.put = function(buffer) {
  235. if (!buffer || buffer.length === 0) return;
  236. if (!Buffer$9.isBuffer(buffer)) buffer = Buffer$9.from(buffer);
  237. this._queue.push(buffer);
  238. this._queueSize += buffer.length;
  239. };
  240. StreamReader.prototype.read = function(length) {
  241. if (length > this._queueSize) return null;
  242. if (length === 0) return Buffer$9.alloc(0);
  243. this._queueSize -= length;
  244. var queue = this._queue,
  245. remain = length,
  246. first = queue[0],
  247. buffers, buffer;
  248. if (first.length >= length) {
  249. if (first.length === length) {
  250. return queue.shift();
  251. } else {
  252. buffer = first.slice(0, length);
  253. queue[0] = first.slice(length);
  254. return buffer;
  255. }
  256. }
  257. for (var i = 0, n = queue.length; i < n; i++) {
  258. if (remain < queue[i].length) break;
  259. remain -= queue[i].length;
  260. }
  261. buffers = queue.splice(0, i);
  262. if (remain > 0 && queue.length > 0) {
  263. buffers.push(queue[0].slice(0, remain));
  264. queue[0] = queue[0].slice(remain);
  265. }
  266. return Buffer$9.concat(buffers, length);
  267. };
  268. StreamReader.prototype.eachByte = function(callback, context) {
  269. var buffer, n, index;
  270. while (this._queue.length > 0) {
  271. buffer = this._queue[0];
  272. n = buffer.length;
  273. while (this._offset < n) {
  274. index = this._offset;
  275. this._offset += 1;
  276. callback.call(context, buffer[index]);
  277. }
  278. this._offset = 0;
  279. this._queue.shift();
  280. }
  281. };
  282. var stream_reader = StreamReader;
  283. var Buffer$8 = safeBuffer.exports.Buffer,
  284. Emitter = require$$1__default["default"].EventEmitter,
  285. util$b = require$$2__default["default"],
  286. streams = streams$1,
  287. Headers$2 = headers,
  288. Reader = stream_reader;
  289. var Base$7 = function(request, url, options) {
  290. Emitter.call(this);
  291. Base$7.validateOptions(options || {}, ['maxLength', 'masking', 'requireMasking', 'protocols']);
  292. this._request = request;
  293. this._reader = new Reader();
  294. this._options = options || {};
  295. this._maxLength = this._options.maxLength || this.MAX_LENGTH;
  296. this._headers = new Headers$2();
  297. this.__queue = [];
  298. this.readyState = 0;
  299. this.url = url;
  300. this.io = new streams.IO(this);
  301. this.messages = new streams.Messages(this);
  302. this._bindEventListeners();
  303. };
  304. util$b.inherits(Base$7, Emitter);
  305. Base$7.isWebSocket = function(request) {
  306. var connection = request.headers.connection || '',
  307. upgrade = request.headers.upgrade || '';
  308. return request.method === 'GET' &&
  309. connection.toLowerCase().split(/ *, */).indexOf('upgrade') >= 0 &&
  310. upgrade.toLowerCase() === 'websocket';
  311. };
  312. Base$7.validateOptions = function(options, validKeys) {
  313. for (var key in options) {
  314. if (validKeys.indexOf(key) < 0)
  315. throw new Error('Unrecognized option: ' + key);
  316. }
  317. };
  318. var instance$b = {
  319. // This is 64MB, small enough for an average VPS to handle without
  320. // crashing from process out of memory
  321. MAX_LENGTH: 0x3ffffff,
  322. STATES: ['connecting', 'open', 'closing', 'closed'],
  323. _bindEventListeners: function() {
  324. var self = this;
  325. // Protocol errors are informational and do not have to be handled
  326. this.messages.on('error', function() {});
  327. this.on('message', function(event) {
  328. var messages = self.messages;
  329. if (messages.readable) messages.emit('data', event.data);
  330. });
  331. this.on('error', function(error) {
  332. var messages = self.messages;
  333. if (messages.readable) messages.emit('error', error);
  334. });
  335. this.on('close', function() {
  336. var messages = self.messages;
  337. if (!messages.readable) return;
  338. messages.readable = messages.writable = false;
  339. messages.emit('end');
  340. });
  341. },
  342. getState: function() {
  343. return this.STATES[this.readyState] || null;
  344. },
  345. addExtension: function(extension) {
  346. return false;
  347. },
  348. setHeader: function(name, value) {
  349. if (this.readyState > 0) return false;
  350. this._headers.set(name, value);
  351. return true;
  352. },
  353. start: function() {
  354. if (this.readyState !== 0) return false;
  355. if (!Base$7.isWebSocket(this._request))
  356. return this._failHandshake(new Error('Not a WebSocket request'));
  357. var response;
  358. try {
  359. response = this._handshakeResponse();
  360. } catch (error) {
  361. return this._failHandshake(error);
  362. }
  363. this._write(response);
  364. if (this._stage !== -1) this._open();
  365. return true;
  366. },
  367. _failHandshake: function(error) {
  368. var headers = new Headers$2();
  369. headers.set('Content-Type', 'text/plain');
  370. headers.set('Content-Length', Buffer$8.byteLength(error.message, 'utf8'));
  371. headers = ['HTTP/1.1 400 Bad Request', headers.toString(), error.message];
  372. this._write(Buffer$8.from(headers.join('\r\n'), 'utf8'));
  373. this._fail('protocol_error', error.message);
  374. return false;
  375. },
  376. text: function(message) {
  377. return this.frame(message);
  378. },
  379. binary: function(message) {
  380. return false;
  381. },
  382. ping: function() {
  383. return false;
  384. },
  385. pong: function() {
  386. return false;
  387. },
  388. close: function(reason, code) {
  389. if (this.readyState !== 1) return false;
  390. this.readyState = 3;
  391. this.emit('close', new Base$7.CloseEvent(null, null));
  392. return true;
  393. },
  394. _open: function() {
  395. this.readyState = 1;
  396. this.__queue.forEach(function(args) { this.frame.apply(this, args); }, this);
  397. this.__queue = [];
  398. this.emit('open', new Base$7.OpenEvent());
  399. },
  400. _queue: function(message) {
  401. this.__queue.push(message);
  402. return true;
  403. },
  404. _write: function(chunk) {
  405. var io = this.io;
  406. if (io.readable) io.emit('data', chunk);
  407. },
  408. _fail: function(type, message) {
  409. this.readyState = 2;
  410. this.emit('error', new Error(message));
  411. this.close();
  412. }
  413. };
  414. for (var key$b in instance$b)
  415. Base$7.prototype[key$b] = instance$b[key$b];
  416. Base$7.ConnectEvent = function() {};
  417. Base$7.OpenEvent = function() {};
  418. Base$7.CloseEvent = function(code, reason) {
  419. this.code = code;
  420. this.reason = reason;
  421. };
  422. Base$7.MessageEvent = function(data) {
  423. this.data = data;
  424. };
  425. Base$7.PingEvent = function(data) {
  426. this.data = data;
  427. };
  428. Base$7.PongEvent = function(data) {
  429. this.data = data;
  430. };
  431. var base = Base$7;
  432. var httpParser = {};
  433. /*jshint node:true */
  434. var assert = require$$0__default$2["default"];
  435. httpParser.HTTPParser = HTTPParser;
  436. function HTTPParser(type) {
  437. assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE || type === undefined);
  438. if (type === undefined) ; else {
  439. this.initialize(type);
  440. }
  441. }
  442. HTTPParser.prototype.initialize = function (type, async_resource) {
  443. assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE);
  444. this.type = type;
  445. this.state = type + '_LINE';
  446. this.info = {
  447. headers: [],
  448. upgrade: false
  449. };
  450. this.trailers = [];
  451. this.line = '';
  452. this.isChunked = false;
  453. this.connection = '';
  454. this.headerSize = 0; // for preventing too big headers
  455. this.body_bytes = null;
  456. this.isUserCall = false;
  457. this.hadError = false;
  458. };
  459. HTTPParser.encoding = 'ascii';
  460. HTTPParser.maxHeaderSize = 80 * 1024; // maxHeaderSize (in bytes) is configurable, but 80kb by default;
  461. HTTPParser.REQUEST = 'REQUEST';
  462. HTTPParser.RESPONSE = 'RESPONSE';
  463. // Note: *not* starting with kOnHeaders=0 line the Node parser, because any
  464. // newly added constants (kOnTimeout in Node v12.19.0) will overwrite 0!
  465. var kOnHeaders = HTTPParser.kOnHeaders = 1;
  466. var kOnHeadersComplete = HTTPParser.kOnHeadersComplete = 2;
  467. var kOnBody = HTTPParser.kOnBody = 3;
  468. var kOnMessageComplete = HTTPParser.kOnMessageComplete = 4;
  469. // Some handler stubs, needed for compatibility
  470. HTTPParser.prototype[kOnHeaders] =
  471. HTTPParser.prototype[kOnHeadersComplete] =
  472. HTTPParser.prototype[kOnBody] =
  473. HTTPParser.prototype[kOnMessageComplete] = function () {};
  474. var compatMode0_12 = true;
  475. Object.defineProperty(HTTPParser, 'kOnExecute', {
  476. get: function () {
  477. // hack for backward compatibility
  478. compatMode0_12 = false;
  479. return 99;
  480. }
  481. });
  482. var methods = httpParser.methods = HTTPParser.methods = [
  483. 'DELETE',
  484. 'GET',
  485. 'HEAD',
  486. 'POST',
  487. 'PUT',
  488. 'CONNECT',
  489. 'OPTIONS',
  490. 'TRACE',
  491. 'COPY',
  492. 'LOCK',
  493. 'MKCOL',
  494. 'MOVE',
  495. 'PROPFIND',
  496. 'PROPPATCH',
  497. 'SEARCH',
  498. 'UNLOCK',
  499. 'BIND',
  500. 'REBIND',
  501. 'UNBIND',
  502. 'ACL',
  503. 'REPORT',
  504. 'MKACTIVITY',
  505. 'CHECKOUT',
  506. 'MERGE',
  507. 'M-SEARCH',
  508. 'NOTIFY',
  509. 'SUBSCRIBE',
  510. 'UNSUBSCRIBE',
  511. 'PATCH',
  512. 'PURGE',
  513. 'MKCALENDAR',
  514. 'LINK',
  515. 'UNLINK'
  516. ];
  517. var method_connect = methods.indexOf('CONNECT');
  518. HTTPParser.prototype.reinitialize = HTTPParser;
  519. HTTPParser.prototype.close =
  520. HTTPParser.prototype.pause =
  521. HTTPParser.prototype.resume =
  522. HTTPParser.prototype.free = function () {};
  523. HTTPParser.prototype._compatMode0_11 = false;
  524. HTTPParser.prototype.getAsyncId = function() { return 0; };
  525. var headerState = {
  526. REQUEST_LINE: true,
  527. RESPONSE_LINE: true,
  528. HEADER: true
  529. };
  530. HTTPParser.prototype.execute = function (chunk, start, length) {
  531. if (!(this instanceof HTTPParser)) {
  532. throw new TypeError('not a HTTPParser');
  533. }
  534. // backward compat to node < 0.11.4
  535. // Note: the start and length params were removed in newer version
  536. start = start || 0;
  537. length = typeof length === 'number' ? length : chunk.length;
  538. this.chunk = chunk;
  539. this.offset = start;
  540. var end = this.end = start + length;
  541. try {
  542. while (this.offset < end) {
  543. if (this[this.state]()) {
  544. break;
  545. }
  546. }
  547. } catch (err) {
  548. if (this.isUserCall) {
  549. throw err;
  550. }
  551. this.hadError = true;
  552. return err;
  553. }
  554. this.chunk = null;
  555. length = this.offset - start;
  556. if (headerState[this.state]) {
  557. this.headerSize += length;
  558. if (this.headerSize > HTTPParser.maxHeaderSize) {
  559. return new Error('max header size exceeded');
  560. }
  561. }
  562. return length;
  563. };
  564. var stateFinishAllowed = {
  565. REQUEST_LINE: true,
  566. RESPONSE_LINE: true,
  567. BODY_RAW: true
  568. };
  569. HTTPParser.prototype.finish = function () {
  570. if (this.hadError) {
  571. return;
  572. }
  573. if (!stateFinishAllowed[this.state]) {
  574. return new Error('invalid state for EOF');
  575. }
  576. if (this.state === 'BODY_RAW') {
  577. this.userCall()(this[kOnMessageComplete]());
  578. }
  579. };
  580. // These three methods are used for an internal speed optimization, and it also
  581. // works if theses are noops. Basically consume() asks us to read the bytes
  582. // ourselves, but if we don't do it we get them through execute().
  583. HTTPParser.prototype.consume =
  584. HTTPParser.prototype.unconsume =
  585. HTTPParser.prototype.getCurrentBuffer = function () {};
  586. //For correct error handling - see HTTPParser#execute
  587. //Usage: this.userCall()(userFunction('arg'));
  588. HTTPParser.prototype.userCall = function () {
  589. this.isUserCall = true;
  590. var self = this;
  591. return function (ret) {
  592. self.isUserCall = false;
  593. return ret;
  594. };
  595. };
  596. HTTPParser.prototype.nextRequest = function () {
  597. this.userCall()(this[kOnMessageComplete]());
  598. this.reinitialize(this.type);
  599. };
  600. HTTPParser.prototype.consumeLine = function () {
  601. var end = this.end,
  602. chunk = this.chunk;
  603. for (var i = this.offset; i < end; i++) {
  604. if (chunk[i] === 0x0a) { // \n
  605. var line = this.line + chunk.toString(HTTPParser.encoding, this.offset, i);
  606. if (line.charAt(line.length - 1) === '\r') {
  607. line = line.substr(0, line.length - 1);
  608. }
  609. this.line = '';
  610. this.offset = i + 1;
  611. return line;
  612. }
  613. }
  614. //line split over multiple chunks
  615. this.line += chunk.toString(HTTPParser.encoding, this.offset, this.end);
  616. this.offset = this.end;
  617. };
  618. var headerExp = /^([^: \t]+):[ \t]*((?:.*[^ \t])|)/;
  619. var headerContinueExp = /^[ \t]+(.*[^ \t])/;
  620. HTTPParser.prototype.parseHeader = function (line, headers) {
  621. if (line.indexOf('\r') !== -1) {
  622. throw parseErrorCode('HPE_LF_EXPECTED');
  623. }
  624. var match = headerExp.exec(line);
  625. var k = match && match[1];
  626. if (k) { // skip empty string (malformed header)
  627. headers.push(k);
  628. headers.push(match[2]);
  629. } else {
  630. var matchContinue = headerContinueExp.exec(line);
  631. if (matchContinue && headers.length) {
  632. if (headers[headers.length - 1]) {
  633. headers[headers.length - 1] += ' ';
  634. }
  635. headers[headers.length - 1] += matchContinue[1];
  636. }
  637. }
  638. };
  639. var requestExp = /^([A-Z-]+) ([^ ]+) HTTP\/(\d)\.(\d)$/;
  640. HTTPParser.prototype.REQUEST_LINE = function () {
  641. var line = this.consumeLine();
  642. if (!line) {
  643. return;
  644. }
  645. var match = requestExp.exec(line);
  646. if (match === null) {
  647. throw parseErrorCode('HPE_INVALID_CONSTANT');
  648. }
  649. this.info.method = this._compatMode0_11 ? match[1] : methods.indexOf(match[1]);
  650. if (this.info.method === -1) {
  651. throw new Error('invalid request method');
  652. }
  653. this.info.url = match[2];
  654. this.info.versionMajor = +match[3];
  655. this.info.versionMinor = +match[4];
  656. this.body_bytes = 0;
  657. this.state = 'HEADER';
  658. };
  659. var responseExp = /^HTTP\/(\d)\.(\d) (\d{3}) ?(.*)$/;
  660. HTTPParser.prototype.RESPONSE_LINE = function () {
  661. var line = this.consumeLine();
  662. if (!line) {
  663. return;
  664. }
  665. var match = responseExp.exec(line);
  666. if (match === null) {
  667. throw parseErrorCode('HPE_INVALID_CONSTANT');
  668. }
  669. this.info.versionMajor = +match[1];
  670. this.info.versionMinor = +match[2];
  671. var statusCode = this.info.statusCode = +match[3];
  672. this.info.statusMessage = match[4];
  673. // Implied zero length.
  674. if ((statusCode / 100 | 0) === 1 || statusCode === 204 || statusCode === 304) {
  675. this.body_bytes = 0;
  676. }
  677. this.state = 'HEADER';
  678. };
  679. HTTPParser.prototype.shouldKeepAlive = function () {
  680. if (this.info.versionMajor > 0 && this.info.versionMinor > 0) {
  681. if (this.connection.indexOf('close') !== -1) {
  682. return false;
  683. }
  684. } else if (this.connection.indexOf('keep-alive') === -1) {
  685. return false;
  686. }
  687. if (this.body_bytes !== null || this.isChunked) { // || skipBody
  688. return true;
  689. }
  690. return false;
  691. };
  692. HTTPParser.prototype.HEADER = function () {
  693. var line = this.consumeLine();
  694. if (line === undefined) {
  695. return;
  696. }
  697. var info = this.info;
  698. if (line) {
  699. this.parseHeader(line, info.headers);
  700. } else {
  701. var headers = info.headers;
  702. var hasContentLength = false;
  703. var currentContentLengthValue;
  704. var hasUpgradeHeader = false;
  705. for (var i = 0; i < headers.length; i += 2) {
  706. switch (headers[i].toLowerCase()) {
  707. case 'transfer-encoding':
  708. this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
  709. break;
  710. case 'content-length':
  711. currentContentLengthValue = +headers[i + 1];
  712. if (hasContentLength) {
  713. // Fix duplicate Content-Length header with same values.
  714. // Throw error only if values are different.
  715. // Known issues:
  716. // https://github.com/request/request/issues/2091#issuecomment-328715113
  717. // https://github.com/nodejs/node/issues/6517#issuecomment-216263771
  718. if (currentContentLengthValue !== this.body_bytes) {
  719. throw parseErrorCode('HPE_UNEXPECTED_CONTENT_LENGTH');
  720. }
  721. } else {
  722. hasContentLength = true;
  723. this.body_bytes = currentContentLengthValue;
  724. }
  725. break;
  726. case 'connection':
  727. this.connection += headers[i + 1].toLowerCase();
  728. break;
  729. case 'upgrade':
  730. hasUpgradeHeader = true;
  731. break;
  732. }
  733. }
  734. // if both isChunked and hasContentLength, isChunked wins
  735. // This is required so the body is parsed using the chunked method, and matches
  736. // Chrome's behavior. We could, maybe, ignore them both (would get chunked
  737. // encoding into the body), and/or disable shouldKeepAlive to be more
  738. // resilient.
  739. if (this.isChunked && hasContentLength) {
  740. hasContentLength = false;
  741. this.body_bytes = null;
  742. }
  743. // Logic from https://github.com/nodejs/http-parser/blob/921d5585515a153fa00e411cf144280c59b41f90/http_parser.c#L1727-L1737
  744. // "For responses, "Upgrade: foo" and "Connection: upgrade" are
  745. // mandatory only when it is a 101 Switching Protocols response,
  746. // otherwise it is purely informational, to announce support.
  747. if (hasUpgradeHeader && this.connection.indexOf('upgrade') != -1) {
  748. info.upgrade = this.type === HTTPParser.REQUEST || info.statusCode === 101;
  749. } else {
  750. info.upgrade = info.method === method_connect;
  751. }
  752. if (this.isChunked && info.upgrade) {
  753. this.isChunked = false;
  754. }
  755. info.shouldKeepAlive = this.shouldKeepAlive();
  756. //problem which also exists in original node: we should know skipBody before calling onHeadersComplete
  757. var skipBody;
  758. if (compatMode0_12) {
  759. skipBody = this.userCall()(this[kOnHeadersComplete](info));
  760. } else {
  761. skipBody = this.userCall()(this[kOnHeadersComplete](info.versionMajor,
  762. info.versionMinor, info.headers, info.method, info.url, info.statusCode,
  763. info.statusMessage, info.upgrade, info.shouldKeepAlive));
  764. }
  765. if (skipBody === 2) {
  766. this.nextRequest();
  767. return true;
  768. } else if (this.isChunked && !skipBody) {
  769. this.state = 'BODY_CHUNKHEAD';
  770. } else if (skipBody || this.body_bytes === 0) {
  771. this.nextRequest();
  772. // For older versions of node (v6.x and older?), that return skipBody=1 or skipBody=true,
  773. // need this "return true;" if it's an upgrade request.
  774. return info.upgrade;
  775. } else if (this.body_bytes === null) {
  776. this.state = 'BODY_RAW';
  777. } else {
  778. this.state = 'BODY_SIZED';
  779. }
  780. }
  781. };
  782. HTTPParser.prototype.BODY_CHUNKHEAD = function () {
  783. var line = this.consumeLine();
  784. if (line === undefined) {
  785. return;
  786. }
  787. this.body_bytes = parseInt(line, 16);
  788. if (!this.body_bytes) {
  789. this.state = 'BODY_CHUNKTRAILERS';
  790. } else {
  791. this.state = 'BODY_CHUNK';
  792. }
  793. };
  794. HTTPParser.prototype.BODY_CHUNK = function () {
  795. var length = Math.min(this.end - this.offset, this.body_bytes);
  796. this.userCall()(this[kOnBody](this.chunk, this.offset, length));
  797. this.offset += length;
  798. this.body_bytes -= length;
  799. if (!this.body_bytes) {
  800. this.state = 'BODY_CHUNKEMPTYLINE';
  801. }
  802. };
  803. HTTPParser.prototype.BODY_CHUNKEMPTYLINE = function () {
  804. var line = this.consumeLine();
  805. if (line === undefined) {
  806. return;
  807. }
  808. assert.equal(line, '');
  809. this.state = 'BODY_CHUNKHEAD';
  810. };
  811. HTTPParser.prototype.BODY_CHUNKTRAILERS = function () {
  812. var line = this.consumeLine();
  813. if (line === undefined) {
  814. return;
  815. }
  816. if (line) {
  817. this.parseHeader(line, this.trailers);
  818. } else {
  819. if (this.trailers.length) {
  820. this.userCall()(this[kOnHeaders](this.trailers, ''));
  821. }
  822. this.nextRequest();
  823. }
  824. };
  825. HTTPParser.prototype.BODY_RAW = function () {
  826. var length = this.end - this.offset;
  827. this.userCall()(this[kOnBody](this.chunk, this.offset, length));
  828. this.offset = this.end;
  829. };
  830. HTTPParser.prototype.BODY_SIZED = function () {
  831. var length = Math.min(this.end - this.offset, this.body_bytes);
  832. this.userCall()(this[kOnBody](this.chunk, this.offset, length));
  833. this.offset += length;
  834. this.body_bytes -= length;
  835. if (!this.body_bytes) {
  836. this.nextRequest();
  837. }
  838. };
  839. // backward compat to node < 0.11.6
  840. ['Headers', 'HeadersComplete', 'Body', 'MessageComplete'].forEach(function (name) {
  841. var k = HTTPParser['kOn' + name];
  842. Object.defineProperty(HTTPParser.prototype, 'on' + name, {
  843. get: function () {
  844. return this[k];
  845. },
  846. set: function (to) {
  847. // hack for backward compatibility
  848. this._compatMode0_11 = true;
  849. method_connect = 'CONNECT';
  850. return (this[k] = to);
  851. }
  852. });
  853. });
  854. function parseErrorCode(code) {
  855. var err = new Error('Parse Error');
  856. err.code = code;
  857. return err;
  858. }
  859. var NodeHTTPParser = httpParser.HTTPParser,
  860. Buffer$7 = safeBuffer.exports.Buffer;
  861. var TYPES = {
  862. request: NodeHTTPParser.REQUEST || 'request',
  863. response: NodeHTTPParser.RESPONSE || 'response'
  864. };
  865. var HttpParser$3 = function(type) {
  866. this._type = type;
  867. this._parser = new NodeHTTPParser(TYPES[type]);
  868. this._complete = false;
  869. this.headers = {};
  870. var current = null,
  871. self = this;
  872. this._parser.onHeaderField = function(b, start, length) {
  873. current = b.toString('utf8', start, start + length).toLowerCase();
  874. };
  875. this._parser.onHeaderValue = function(b, start, length) {
  876. var value = b.toString('utf8', start, start + length);
  877. if (self.headers.hasOwnProperty(current))
  878. self.headers[current] += ', ' + value;
  879. else
  880. self.headers[current] = value;
  881. };
  882. this._parser.onHeadersComplete = this._parser[NodeHTTPParser.kOnHeadersComplete] =
  883. function(majorVersion, minorVersion, headers, method, pathname, statusCode) {
  884. var info = arguments[0];
  885. if (typeof info === 'object') {
  886. method = info.method;
  887. pathname = info.url;
  888. statusCode = info.statusCode;
  889. headers = info.headers;
  890. }
  891. self.method = (typeof method === 'number') ? HttpParser$3.METHODS[method] : method;
  892. self.statusCode = statusCode;
  893. self.url = pathname;
  894. if (!headers) return;
  895. for (var i = 0, n = headers.length, key, value; i < n; i += 2) {
  896. key = headers[i].toLowerCase();
  897. value = headers[i+1];
  898. if (self.headers.hasOwnProperty(key))
  899. self.headers[key] += ', ' + value;
  900. else
  901. self.headers[key] = value;
  902. }
  903. self._complete = true;
  904. };
  905. };
  906. HttpParser$3.METHODS = {
  907. 0: 'DELETE',
  908. 1: 'GET',
  909. 2: 'HEAD',
  910. 3: 'POST',
  911. 4: 'PUT',
  912. 5: 'CONNECT',
  913. 6: 'OPTIONS',
  914. 7: 'TRACE',
  915. 8: 'COPY',
  916. 9: 'LOCK',
  917. 10: 'MKCOL',
  918. 11: 'MOVE',
  919. 12: 'PROPFIND',
  920. 13: 'PROPPATCH',
  921. 14: 'SEARCH',
  922. 15: 'UNLOCK',
  923. 16: 'BIND',
  924. 17: 'REBIND',
  925. 18: 'UNBIND',
  926. 19: 'ACL',
  927. 20: 'REPORT',
  928. 21: 'MKACTIVITY',
  929. 22: 'CHECKOUT',
  930. 23: 'MERGE',
  931. 24: 'M-SEARCH',
  932. 25: 'NOTIFY',
  933. 26: 'SUBSCRIBE',
  934. 27: 'UNSUBSCRIBE',
  935. 28: 'PATCH',
  936. 29: 'PURGE',
  937. 30: 'MKCALENDAR',
  938. 31: 'LINK',
  939. 32: 'UNLINK'
  940. };
  941. var VERSION = process.version
  942. ? process.version.match(/[0-9]+/g).map(function(n) { return parseInt(n, 10) })
  943. : [];
  944. if (VERSION[0] === 0 && VERSION[1] === 12) {
  945. HttpParser$3.METHODS[16] = 'REPORT';
  946. HttpParser$3.METHODS[17] = 'MKACTIVITY';
  947. HttpParser$3.METHODS[18] = 'CHECKOUT';
  948. HttpParser$3.METHODS[19] = 'MERGE';
  949. HttpParser$3.METHODS[20] = 'M-SEARCH';
  950. HttpParser$3.METHODS[21] = 'NOTIFY';
  951. HttpParser$3.METHODS[22] = 'SUBSCRIBE';
  952. HttpParser$3.METHODS[23] = 'UNSUBSCRIBE';
  953. HttpParser$3.METHODS[24] = 'PATCH';
  954. HttpParser$3.METHODS[25] = 'PURGE';
  955. }
  956. HttpParser$3.prototype.isComplete = function() {
  957. return this._complete;
  958. };
  959. HttpParser$3.prototype.parse = function(chunk) {
  960. var consumed = this._parser.execute(chunk, 0, chunk.length);
  961. if (typeof consumed !== 'number') {
  962. this.error = consumed;
  963. this._complete = true;
  964. return;
  965. }
  966. if (this._complete)
  967. this.body = (consumed < chunk.length)
  968. ? chunk.slice(consumed)
  969. : Buffer$7.alloc(0);
  970. };
  971. var http_parser = HttpParser$3;
  972. var TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/,
  973. NOTOKEN = /([^!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z])/g,
  974. QUOTED = /"((?:\\[\x00-\x7f]|[^\x00-\x08\x0a-\x1f\x7f"\\])*)"/,
  975. PARAM = new RegExp(TOKEN.source + '(?:=(?:' + TOKEN.source + '|' + QUOTED.source + '))?'),
  976. EXT = new RegExp(TOKEN.source + '(?: *; *' + PARAM.source + ')*', 'g'),
  977. EXT_LIST = new RegExp('^' + EXT.source + '(?: *, *' + EXT.source + ')*$'),
  978. NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/;
  979. var hasOwnProperty = Object.prototype.hasOwnProperty;
  980. var Parser$1 = {
  981. parseHeader: function(header) {
  982. var offers = new Offers();
  983. if (header === '' || header === undefined) return offers;
  984. if (!EXT_LIST.test(header))
  985. throw new SyntaxError('Invalid Sec-WebSocket-Extensions header: ' + header);
  986. var values = header.match(EXT);
  987. values.forEach(function(value) {
  988. var params = value.match(new RegExp(PARAM.source, 'g')),
  989. name = params.shift(),
  990. offer = {};
  991. params.forEach(function(param) {
  992. var args = param.match(PARAM), key = args[1], data;
  993. if (args[2] !== undefined) {
  994. data = args[2];
  995. } else if (args[3] !== undefined) {
  996. data = args[3].replace(/\\/g, '');
  997. } else {
  998. data = true;
  999. }
  1000. if (NUMBER.test(data)) data = parseFloat(data);
  1001. if (hasOwnProperty.call(offer, key)) {
  1002. offer[key] = [].concat(offer[key]);
  1003. offer[key].push(data);
  1004. } else {
  1005. offer[key] = data;
  1006. }
  1007. }, this);
  1008. offers.push(name, offer);
  1009. }, this);
  1010. return offers;
  1011. },
  1012. serializeParams: function(name, params) {
  1013. var values = [];
  1014. var print = function(key, value) {
  1015. if (value instanceof Array) {
  1016. value.forEach(function(v) { print(key, v); });
  1017. } else if (value === true) {
  1018. values.push(key);
  1019. } else if (typeof value === 'number') {
  1020. values.push(key + '=' + value);
  1021. } else if (NOTOKEN.test(value)) {
  1022. values.push(key + '="' + value.replace(/"/g, '\\"') + '"');
  1023. } else {
  1024. values.push(key + '=' + value);
  1025. }
  1026. };
  1027. for (var key in params) print(key, params[key]);
  1028. return [name].concat(values).join('; ');
  1029. }
  1030. };
  1031. var Offers = function() {
  1032. this._byName = {};
  1033. this._inOrder = [];
  1034. };
  1035. Offers.prototype.push = function(name, params) {
  1036. if (!hasOwnProperty.call(this._byName, name))
  1037. this._byName[name] = [];
  1038. this._byName[name].push(params);
  1039. this._inOrder.push({ name: name, params: params });
  1040. };
  1041. Offers.prototype.eachOffer = function(callback, context) {
  1042. var list = this._inOrder;
  1043. for (var i = 0, n = list.length; i < n; i++)
  1044. callback.call(context, list[i].name, list[i].params);
  1045. };
  1046. Offers.prototype.byName = function(name) {
  1047. return this._byName[name] || [];
  1048. };
  1049. Offers.prototype.toArray = function() {
  1050. return this._inOrder.slice();
  1051. };
  1052. var parser = Parser$1;
  1053. var RingBuffer$2 = function(bufferSize) {
  1054. this._bufferSize = bufferSize;
  1055. this.clear();
  1056. };
  1057. RingBuffer$2.prototype.clear = function() {
  1058. this._buffer = new Array(this._bufferSize);
  1059. this._ringOffset = 0;
  1060. this._ringSize = this._bufferSize;
  1061. this._head = 0;
  1062. this._tail = 0;
  1063. this.length = 0;
  1064. };
  1065. RingBuffer$2.prototype.push = function(value) {
  1066. var expandBuffer = false,
  1067. expandRing = false;
  1068. if (this._ringSize < this._bufferSize) {
  1069. expandBuffer = (this._tail === 0);
  1070. } else if (this._ringOffset === this._ringSize) {
  1071. expandBuffer = true;
  1072. expandRing = (this._tail === 0);
  1073. }
  1074. if (expandBuffer) {
  1075. this._tail = this._bufferSize;
  1076. this._buffer = this._buffer.concat(new Array(this._bufferSize));
  1077. this._bufferSize = this._buffer.length;
  1078. if (expandRing)
  1079. this._ringSize = this._bufferSize;
  1080. }
  1081. this._buffer[this._tail] = value;
  1082. this.length += 1;
  1083. if (this._tail < this._ringSize) this._ringOffset += 1;
  1084. this._tail = (this._tail + 1) % this._bufferSize;
  1085. };
  1086. RingBuffer$2.prototype.peek = function() {
  1087. if (this.length === 0) return void 0;
  1088. return this._buffer[this._head];
  1089. };
  1090. RingBuffer$2.prototype.shift = function() {
  1091. if (this.length === 0) return void 0;
  1092. var value = this._buffer[this._head];
  1093. this._buffer[this._head] = void 0;
  1094. this.length -= 1;
  1095. this._ringOffset -= 1;
  1096. if (this._ringOffset === 0 && this.length > 0) {
  1097. this._head = this._ringSize;
  1098. this._ringOffset = this.length;
  1099. this._ringSize = this._bufferSize;
  1100. } else {
  1101. this._head = (this._head + 1) % this._ringSize;
  1102. }
  1103. return value;
  1104. };
  1105. var ring_buffer = RingBuffer$2;
  1106. var RingBuffer$1 = ring_buffer;
  1107. var Functor$1 = function(session, method) {
  1108. this._session = session;
  1109. this._method = method;
  1110. this._queue = new RingBuffer$1(Functor$1.QUEUE_SIZE);
  1111. this._stopped = false;
  1112. this.pending = 0;
  1113. };
  1114. Functor$1.QUEUE_SIZE = 8;
  1115. Functor$1.prototype.call = function(error, message, callback, context) {
  1116. if (this._stopped) return;
  1117. var record = { error: error, message: message, callback: callback, context: context, done: false },
  1118. called = false,
  1119. self = this;
  1120. this._queue.push(record);
  1121. if (record.error) {
  1122. record.done = true;
  1123. this._stop();
  1124. return this._flushQueue();
  1125. }
  1126. var handler = function(err, msg) {
  1127. if (!(called ^ (called = true))) return;
  1128. if (err) {
  1129. self._stop();
  1130. record.error = err;
  1131. record.message = null;
  1132. } else {
  1133. record.message = msg;
  1134. }
  1135. record.done = true;
  1136. self._flushQueue();
  1137. };
  1138. try {
  1139. this._session[this._method](message, handler);
  1140. } catch (err) {
  1141. handler(err);
  1142. }
  1143. };
  1144. Functor$1.prototype._stop = function() {
  1145. this.pending = this._queue.length;
  1146. this._stopped = true;
  1147. };
  1148. Functor$1.prototype._flushQueue = function() {
  1149. var queue = this._queue, record;
  1150. while (queue.length > 0 && queue.peek().done) {
  1151. record = queue.shift();
  1152. if (record.error) {
  1153. this.pending = 0;
  1154. queue.clear();
  1155. } else {
  1156. this.pending -= 1;
  1157. }
  1158. record.callback.call(record.context, record.error, record.message);
  1159. }
  1160. };
  1161. var functor = Functor$1;
  1162. var RingBuffer = ring_buffer;
  1163. var Pledge$2 = function() {
  1164. this._complete = false;
  1165. this._callbacks = new RingBuffer(Pledge$2.QUEUE_SIZE);
  1166. };
  1167. Pledge$2.QUEUE_SIZE = 4;
  1168. Pledge$2.all = function(list) {
  1169. var pledge = new Pledge$2(),
  1170. pending = list.length,
  1171. n = pending;
  1172. if (pending === 0) pledge.done();
  1173. while (n--) list[n].then(function() {
  1174. pending -= 1;
  1175. if (pending === 0) pledge.done();
  1176. });
  1177. return pledge;
  1178. };
  1179. Pledge$2.prototype.then = function(callback) {
  1180. if (this._complete) callback();
  1181. else this._callbacks.push(callback);
  1182. };
  1183. Pledge$2.prototype.done = function() {
  1184. this._complete = true;
  1185. var callbacks = this._callbacks, callback;
  1186. while (callback = callbacks.shift()) callback();
  1187. };
  1188. var pledge = Pledge$2;
  1189. var Functor = functor,
  1190. Pledge$1 = pledge;
  1191. var Cell$1 = function(tuple) {
  1192. this._ext = tuple[0];
  1193. this._session = tuple[1];
  1194. this._functors = {
  1195. incoming: new Functor(this._session, 'processIncomingMessage'),
  1196. outgoing: new Functor(this._session, 'processOutgoingMessage')
  1197. };
  1198. };
  1199. Cell$1.prototype.pending = function(direction) {
  1200. var functor = this._functors[direction];
  1201. if (!functor._stopped) functor.pending += 1;
  1202. };
  1203. Cell$1.prototype.incoming = function(error, message, callback, context) {
  1204. this._exec('incoming', error, message, callback, context);
  1205. };
  1206. Cell$1.prototype.outgoing = function(error, message, callback, context) {
  1207. this._exec('outgoing', error, message, callback, context);
  1208. };
  1209. Cell$1.prototype.close = function() {
  1210. this._closed = this._closed || new Pledge$1();
  1211. this._doClose();
  1212. return this._closed;
  1213. };
  1214. Cell$1.prototype._exec = function(direction, error, message, callback, context) {
  1215. this._functors[direction].call(error, message, function(err, msg) {
  1216. if (err) err.message = this._ext.name + ': ' + err.message;
  1217. callback.call(context, err, msg);
  1218. this._doClose();
  1219. }, this);
  1220. };
  1221. Cell$1.prototype._doClose = function() {
  1222. var fin = this._functors.incoming,
  1223. fout = this._functors.outgoing;
  1224. if (!this._closed || fin.pending + fout.pending !== 0) return;
  1225. if (this._session) this._session.close();
  1226. this._session = null;
  1227. this._closed.done();
  1228. };
  1229. var cell = Cell$1;
  1230. var Cell = cell,
  1231. Pledge = pledge;
  1232. var Pipeline$1 = function(sessions) {
  1233. this._cells = sessions.map(function(session) { return new Cell(session) });
  1234. this._stopped = { incoming: false, outgoing: false };
  1235. };
  1236. Pipeline$1.prototype.processIncomingMessage = function(message, callback, context) {
  1237. if (this._stopped.incoming) return;
  1238. this._loop('incoming', this._cells.length - 1, -1, -1, message, callback, context);
  1239. };
  1240. Pipeline$1.prototype.processOutgoingMessage = function(message, callback, context) {
  1241. if (this._stopped.outgoing) return;
  1242. this._loop('outgoing', 0, this._cells.length, 1, message, callback, context);
  1243. };
  1244. Pipeline$1.prototype.close = function(callback, context) {
  1245. this._stopped = { incoming: true, outgoing: true };
  1246. var closed = this._cells.map(function(a) { return a.close() });
  1247. if (callback)
  1248. Pledge.all(closed).then(function() { callback.call(context); });
  1249. };
  1250. Pipeline$1.prototype._loop = function(direction, start, end, step, message, callback, context) {
  1251. var cells = this._cells,
  1252. n = cells.length,
  1253. self = this;
  1254. while (n--) cells[n].pending(direction);
  1255. var pipe = function(index, error, msg) {
  1256. if (index === end) return callback.call(context, error, msg);
  1257. cells[index][direction](error, msg, function(err, m) {
  1258. if (err) self._stopped[direction] = true;
  1259. pipe(index + step, err, m);
  1260. });
  1261. };
  1262. pipe(start, null, message);
  1263. };
  1264. var pipeline = Pipeline$1;
  1265. var Parser = parser,
  1266. Pipeline = pipeline;
  1267. var Extensions$1 = function() {
  1268. this._rsv1 = this._rsv2 = this._rsv3 = null;
  1269. this._byName = {};
  1270. this._inOrder = [];
  1271. this._sessions = [];
  1272. this._index = {};
  1273. };
  1274. Extensions$1.MESSAGE_OPCODES = [1, 2];
  1275. var instance$a = {
  1276. add: function(ext) {
  1277. if (typeof ext.name !== 'string') throw new TypeError('extension.name must be a string');
  1278. if (ext.type !== 'permessage') throw new TypeError('extension.type must be "permessage"');
  1279. if (typeof ext.rsv1 !== 'boolean') throw new TypeError('extension.rsv1 must be true or false');
  1280. if (typeof ext.rsv2 !== 'boolean') throw new TypeError('extension.rsv2 must be true or false');
  1281. if (typeof ext.rsv3 !== 'boolean') throw new TypeError('extension.rsv3 must be true or false');
  1282. if (this._byName.hasOwnProperty(ext.name))
  1283. throw new TypeError('An extension with name "' + ext.name + '" is already registered');
  1284. this._byName[ext.name] = ext;
  1285. this._inOrder.push(ext);
  1286. },
  1287. generateOffer: function() {
  1288. var sessions = [],
  1289. offer = [],
  1290. index = {};
  1291. this._inOrder.forEach(function(ext) {
  1292. var session = ext.createClientSession();
  1293. if (!session) return;
  1294. var record = [ext, session];
  1295. sessions.push(record);
  1296. index[ext.name] = record;
  1297. var offers = session.generateOffer();
  1298. offers = offers ? [].concat(offers) : [];
  1299. offers.forEach(function(off) {
  1300. offer.push(Parser.serializeParams(ext.name, off));
  1301. }, this);
  1302. }, this);
  1303. this._sessions = sessions;
  1304. this._index = index;
  1305. return offer.length > 0 ? offer.join(', ') : null;
  1306. },
  1307. activate: function(header) {
  1308. var responses = Parser.parseHeader(header),
  1309. sessions = [];
  1310. responses.eachOffer(function(name, params) {
  1311. var record = this._index[name];
  1312. if (!record)
  1313. throw new Error('Server sent an extension response for unknown extension "' + name + '"');
  1314. var ext = record[0],
  1315. session = record[1],
  1316. reserved = this._reserved(ext);
  1317. if (reserved)
  1318. throw new Error('Server sent two extension responses that use the RSV' +
  1319. reserved[0] + ' bit: "' +
  1320. reserved[1] + '" and "' + ext.name + '"');
  1321. if (session.activate(params) !== true)
  1322. throw new Error('Server sent unacceptable extension parameters: ' +
  1323. Parser.serializeParams(name, params));
  1324. this._reserve(ext);
  1325. sessions.push(record);
  1326. }, this);
  1327. this._sessions = sessions;
  1328. this._pipeline = new Pipeline(sessions);
  1329. },
  1330. generateResponse: function(header) {
  1331. var sessions = [],
  1332. response = [],
  1333. offers = Parser.parseHeader(header);
  1334. this._inOrder.forEach(function(ext) {
  1335. var offer = offers.byName(ext.name);
  1336. if (offer.length === 0 || this._reserved(ext)) return;
  1337. var session = ext.createServerSession(offer);
  1338. if (!session) return;
  1339. this._reserve(ext);
  1340. sessions.push([ext, session]);
  1341. response.push(Parser.serializeParams(ext.name, session.generateResponse()));
  1342. }, this);
  1343. this._sessions = sessions;
  1344. this._pipeline = new Pipeline(sessions);
  1345. return response.length > 0 ? response.join(', ') : null;
  1346. },
  1347. validFrameRsv: function(frame) {
  1348. var allowed = { rsv1: false, rsv2: false, rsv3: false },
  1349. ext;
  1350. if (Extensions$1.MESSAGE_OPCODES.indexOf(frame.opcode) >= 0) {
  1351. for (var i = 0, n = this._sessions.length; i < n; i++) {
  1352. ext = this._sessions[i][0];
  1353. allowed.rsv1 = allowed.rsv1 || ext.rsv1;
  1354. allowed.rsv2 = allowed.rsv2 || ext.rsv2;
  1355. allowed.rsv3 = allowed.rsv3 || ext.rsv3;
  1356. }
  1357. }
  1358. return (allowed.rsv1 || !frame.rsv1) &&
  1359. (allowed.rsv2 || !frame.rsv2) &&
  1360. (allowed.rsv3 || !frame.rsv3);
  1361. },
  1362. processIncomingMessage: function(message, callback, context) {
  1363. this._pipeline.processIncomingMessage(message, callback, context);
  1364. },
  1365. processOutgoingMessage: function(message, callback, context) {
  1366. this._pipeline.processOutgoingMessage(message, callback, context);
  1367. },
  1368. close: function(callback, context) {
  1369. if (!this._pipeline) return callback.call(context);
  1370. this._pipeline.close(callback, context);
  1371. },
  1372. _reserve: function(ext) {
  1373. this._rsv1 = this._rsv1 || (ext.rsv1 && ext.name);
  1374. this._rsv2 = this._rsv2 || (ext.rsv2 && ext.name);
  1375. this._rsv3 = this._rsv3 || (ext.rsv3 && ext.name);
  1376. },
  1377. _reserved: function(ext) {
  1378. if (this._rsv1 && ext.rsv1) return [1, this._rsv1];
  1379. if (this._rsv2 && ext.rsv2) return [2, this._rsv2];
  1380. if (this._rsv3 && ext.rsv3) return [3, this._rsv3];
  1381. return false;
  1382. }
  1383. };
  1384. for (var key$a in instance$a)
  1385. Extensions$1.prototype[key$a] = instance$a[key$a];
  1386. var websocket_extensions = Extensions$1;
  1387. var Frame$1 = function() {};
  1388. var instance$9 = {
  1389. final: false,
  1390. rsv1: false,
  1391. rsv2: false,
  1392. rsv3: false,
  1393. opcode: null,
  1394. masked: false,
  1395. maskingKey: null,
  1396. lengthBytes: 1,
  1397. length: 0,
  1398. payload: null
  1399. };
  1400. for (var key$9 in instance$9)
  1401. Frame$1.prototype[key$9] = instance$9[key$9];
  1402. var frame = Frame$1;
  1403. var Buffer$6 = safeBuffer.exports.Buffer;
  1404. var Message$1 = function() {
  1405. this.rsv1 = false;
  1406. this.rsv2 = false;
  1407. this.rsv3 = false;
  1408. this.opcode = null;
  1409. this.length = 0;
  1410. this._chunks = [];
  1411. };
  1412. var instance$8 = {
  1413. read: function() {
  1414. return this.data = this.data || Buffer$6.concat(this._chunks, this.length);
  1415. },
  1416. pushFrame: function(frame) {
  1417. this.rsv1 = this.rsv1 || frame.rsv1;
  1418. this.rsv2 = this.rsv2 || frame.rsv2;
  1419. this.rsv3 = this.rsv3 || frame.rsv3;
  1420. if (this.opcode === null) this.opcode = frame.opcode;
  1421. this._chunks.push(frame.payload);
  1422. this.length += frame.length;
  1423. }
  1424. };
  1425. for (var key$8 in instance$8)
  1426. Message$1.prototype[key$8] = instance$8[key$8];
  1427. var message = Message$1;
  1428. var Buffer$5 = safeBuffer.exports.Buffer,
  1429. crypto$2 = require$$1__default$1["default"],
  1430. util$a = require$$2__default["default"],
  1431. Extensions = websocket_extensions,
  1432. Base$6 = base,
  1433. Frame = frame,
  1434. Message = message;
  1435. var Hybi$2 = function(request, url, options) {
  1436. Base$6.apply(this, arguments);
  1437. this._extensions = new Extensions();
  1438. this._stage = 0;
  1439. this._masking = this._options.masking;
  1440. this._protocols = this._options.protocols || [];
  1441. this._requireMasking = this._options.requireMasking;
  1442. this._pingCallbacks = {};
  1443. if (typeof this._protocols === 'string')
  1444. this._protocols = this._protocols.split(/ *, */);
  1445. if (!this._request) return;
  1446. var protos = this._request.headers['sec-websocket-protocol'],
  1447. supported = this._protocols;
  1448. if (protos !== undefined) {
  1449. if (typeof protos === 'string') protos = protos.split(/ *, */);
  1450. this.protocol = protos.filter(function(p) { return supported.indexOf(p) >= 0 })[0];
  1451. }
  1452. this.version = 'hybi-' + Hybi$2.VERSION;
  1453. };
  1454. util$a.inherits(Hybi$2, Base$6);
  1455. Hybi$2.VERSION = '13';
  1456. Hybi$2.mask = function(payload, mask, offset) {
  1457. if (!mask || mask.length === 0) return payload;
  1458. offset = offset || 0;
  1459. for (var i = 0, n = payload.length - offset; i < n; i++) {
  1460. payload[offset + i] = payload[offset + i] ^ mask[i % 4];
  1461. }
  1462. return payload;
  1463. };
  1464. Hybi$2.generateAccept = function(key) {
  1465. var sha1 = crypto$2.createHash('sha1');
  1466. sha1.update(key + Hybi$2.GUID);
  1467. return sha1.digest('base64');
  1468. };
  1469. Hybi$2.GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
  1470. var instance$7 = {
  1471. FIN: 0x80,
  1472. MASK: 0x80,
  1473. RSV1: 0x40,
  1474. RSV2: 0x20,
  1475. RSV3: 0x10,
  1476. OPCODE: 0x0F,
  1477. LENGTH: 0x7F,
  1478. OPCODES: {
  1479. continuation: 0,
  1480. text: 1,
  1481. binary: 2,
  1482. close: 8,
  1483. ping: 9,
  1484. pong: 10
  1485. },
  1486. OPCODE_CODES: [0, 1, 2, 8, 9, 10],
  1487. MESSAGE_OPCODES: [0, 1, 2],
  1488. OPENING_OPCODES: [1, 2],
  1489. ERRORS: {
  1490. normal_closure: 1000,
  1491. going_away: 1001,
  1492. protocol_error: 1002,
  1493. unacceptable: 1003,
  1494. encoding_error: 1007,
  1495. policy_violation: 1008,
  1496. too_large: 1009,
  1497. extension_error: 1010,
  1498. unexpected_condition: 1011
  1499. },
  1500. ERROR_CODES: [1000, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1011],
  1501. DEFAULT_ERROR_CODE: 1000,
  1502. MIN_RESERVED_ERROR: 3000,
  1503. MAX_RESERVED_ERROR: 4999,
  1504. // http://www.w3.org/International/questions/qa-forms-utf-8.en.php
  1505. UTF8_MATCH: /^([\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*$/,
  1506. addExtension: function(extension) {
  1507. this._extensions.add(extension);
  1508. return true;
  1509. },
  1510. parse: function(chunk) {
  1511. this._reader.put(chunk);
  1512. var buffer = true;
  1513. while (buffer) {
  1514. switch (this._stage) {
  1515. case 0:
  1516. buffer = this._reader.read(1);
  1517. if (buffer) this._parseOpcode(buffer[0]);
  1518. break;
  1519. case 1:
  1520. buffer = this._reader.read(1);
  1521. if (buffer) this._parseLength(buffer[0]);
  1522. break;
  1523. case 2:
  1524. buffer = this._reader.read(this._frame.lengthBytes);
  1525. if (buffer) this._parseExtendedLength(buffer);
  1526. break;
  1527. case 3:
  1528. buffer = this._reader.read(4);
  1529. if (buffer) {
  1530. this._stage = 4;
  1531. this._frame.maskingKey = buffer;
  1532. }
  1533. break;
  1534. case 4:
  1535. buffer = this._reader.read(this._frame.length);
  1536. if (buffer) {
  1537. this._stage = 0;
  1538. this._emitFrame(buffer);
  1539. }
  1540. break;
  1541. default:
  1542. buffer = null;
  1543. }
  1544. }
  1545. },
  1546. text: function(message) {
  1547. if (this.readyState > 1) return false;
  1548. return this.frame(message, 'text');
  1549. },
  1550. binary: function(message) {
  1551. if (this.readyState > 1) return false;
  1552. return this.frame(message, 'binary');
  1553. },
  1554. ping: function(message, callback) {
  1555. if (this.readyState > 1) return false;
  1556. message = message || '';
  1557. if (callback) this._pingCallbacks[message] = callback;
  1558. return this.frame(message, 'ping');
  1559. },
  1560. pong: function(message) {
  1561. if (this.readyState > 1) return false;
  1562. message = message ||'';
  1563. return this.frame(message, 'pong');
  1564. },
  1565. close: function(reason, code) {
  1566. reason = reason || '';
  1567. code = code || this.ERRORS.normal_closure;
  1568. if (this.readyState <= 0) {
  1569. this.readyState = 3;
  1570. this.emit('close', new Base$6.CloseEvent(code, reason));
  1571. return true;
  1572. } else if (this.readyState === 1) {
  1573. this.readyState = 2;
  1574. this._extensions.close(function() { this.frame(reason, 'close', code); }, this);
  1575. return true;
  1576. } else {
  1577. return false;
  1578. }
  1579. },
  1580. frame: function(buffer, type, code) {
  1581. if (this.readyState <= 0) return this._queue([buffer, type, code]);
  1582. if (this.readyState > 2) return false;
  1583. if (buffer instanceof Array) buffer = Buffer$5.from(buffer);
  1584. if (typeof buffer === 'number') buffer = buffer.toString();
  1585. var message = new Message(),
  1586. isText = (typeof buffer === 'string'),
  1587. payload, copy;
  1588. message.rsv1 = message.rsv2 = message.rsv3 = false;
  1589. message.opcode = this.OPCODES[type || (isText ? 'text' : 'binary')];
  1590. payload = isText ? Buffer$5.from(buffer, 'utf8') : buffer;
  1591. if (code) {
  1592. copy = payload;
  1593. payload = Buffer$5.allocUnsafe(2 + copy.length);
  1594. payload.writeUInt16BE(code, 0);
  1595. copy.copy(payload, 2);
  1596. }
  1597. message.data = payload;
  1598. var onMessageReady = function(message) {
  1599. var frame = new Frame();
  1600. frame.final = true;
  1601. frame.rsv1 = message.rsv1;
  1602. frame.rsv2 = message.rsv2;
  1603. frame.rsv3 = message.rsv3;
  1604. frame.opcode = message.opcode;
  1605. frame.masked = !!this._masking;
  1606. frame.length = message.data.length;
  1607. frame.payload = message.data;
  1608. if (frame.masked) frame.maskingKey = crypto$2.randomBytes(4);
  1609. this._sendFrame(frame);
  1610. };
  1611. if (this.MESSAGE_OPCODES.indexOf(message.opcode) >= 0)
  1612. this._extensions.processOutgoingMessage(message, function(error, message) {
  1613. if (error) return this._fail('extension_error', error.message);
  1614. onMessageReady.call(this, message);
  1615. }, this);
  1616. else
  1617. onMessageReady.call(this, message);
  1618. return true;
  1619. },
  1620. _sendFrame: function(frame) {
  1621. var length = frame.length,
  1622. header = (length <= 125) ? 2 : (length <= 65535 ? 4 : 10),
  1623. offset = header + (frame.masked ? 4 : 0),
  1624. buffer = Buffer$5.allocUnsafe(offset + length),
  1625. masked = frame.masked ? this.MASK : 0;
  1626. buffer[0] = (frame.final ? this.FIN : 0) |
  1627. (frame.rsv1 ? this.RSV1 : 0) |
  1628. (frame.rsv2 ? this.RSV2 : 0) |
  1629. (frame.rsv3 ? this.RSV3 : 0) |
  1630. frame.opcode;
  1631. if (length <= 125) {
  1632. buffer[1] = masked | length;
  1633. } else if (length <= 65535) {
  1634. buffer[1] = masked | 126;
  1635. buffer.writeUInt16BE(length, 2);
  1636. } else {
  1637. buffer[1] = masked | 127;
  1638. buffer.writeUInt32BE(Math.floor(length / 0x100000000), 2);
  1639. buffer.writeUInt32BE(length % 0x100000000, 6);
  1640. }
  1641. frame.payload.copy(buffer, offset);
  1642. if (frame.masked) {
  1643. frame.maskingKey.copy(buffer, header);
  1644. Hybi$2.mask(buffer, frame.maskingKey, offset);
  1645. }
  1646. this._write(buffer);
  1647. },
  1648. _handshakeResponse: function() {
  1649. var secKey = this._request.headers['sec-websocket-key'],
  1650. version = this._request.headers['sec-websocket-version'];
  1651. if (version !== Hybi$2.VERSION)
  1652. throw new Error('Unsupported WebSocket version: ' + version);
  1653. if (typeof secKey !== 'string')
  1654. throw new Error('Missing handshake request header: Sec-WebSocket-Key');
  1655. this._headers.set('Upgrade', 'websocket');
  1656. this._headers.set('Connection', 'Upgrade');
  1657. this._headers.set('Sec-WebSocket-Accept', Hybi$2.generateAccept(secKey));
  1658. if (this.protocol) this._headers.set('Sec-WebSocket-Protocol', this.protocol);
  1659. var extensions = this._extensions.generateResponse(this._request.headers['sec-websocket-extensions']);
  1660. if (extensions) this._headers.set('Sec-WebSocket-Extensions', extensions);
  1661. var start = 'HTTP/1.1 101 Switching Protocols',
  1662. headers = [start, this._headers.toString(), ''];
  1663. return Buffer$5.from(headers.join('\r\n'), 'utf8');
  1664. },
  1665. _shutdown: function(code, reason, error) {
  1666. delete this._frame;
  1667. delete this._message;
  1668. this._stage = 5;
  1669. var sendCloseFrame = (this.readyState === 1);
  1670. this.readyState = 2;
  1671. this._extensions.close(function() {
  1672. if (sendCloseFrame) this.frame(reason, 'close', code);
  1673. this.readyState = 3;
  1674. if (error) this.emit('error', new Error(reason));
  1675. this.emit('close', new Base$6.CloseEvent(code, reason));
  1676. }, this);
  1677. },
  1678. _fail: function(type, message) {
  1679. if (this.readyState > 1) return;
  1680. this._shutdown(this.ERRORS[type], message, true);
  1681. },
  1682. _parseOpcode: function(octet) {
  1683. var rsvs = [this.RSV1, this.RSV2, this.RSV3].map(function(rsv) {
  1684. return (octet & rsv) === rsv;
  1685. });
  1686. var frame = this._frame = new Frame();
  1687. frame.final = (octet & this.FIN) === this.FIN;
  1688. frame.rsv1 = rsvs[0];
  1689. frame.rsv2 = rsvs[1];
  1690. frame.rsv3 = rsvs[2];
  1691. frame.opcode = (octet & this.OPCODE);
  1692. this._stage = 1;
  1693. if (!this._extensions.validFrameRsv(frame))
  1694. return this._fail('protocol_error',
  1695. 'One or more reserved bits are on: reserved1 = ' + (frame.rsv1 ? 1 : 0) +
  1696. ', reserved2 = ' + (frame.rsv2 ? 1 : 0) +
  1697. ', reserved3 = ' + (frame.rsv3 ? 1 : 0));
  1698. if (this.OPCODE_CODES.indexOf(frame.opcode) < 0)
  1699. return this._fail('protocol_error', 'Unrecognized frame opcode: ' + frame.opcode);
  1700. if (this.MESSAGE_OPCODES.indexOf(frame.opcode) < 0 && !frame.final)
  1701. return this._fail('protocol_error', 'Received fragmented control frame: opcode = ' + frame.opcode);
  1702. if (this._message && this.OPENING_OPCODES.indexOf(frame.opcode) >= 0)
  1703. return this._fail('protocol_error', 'Received new data frame but previous continuous frame is unfinished');
  1704. },
  1705. _parseLength: function(octet) {
  1706. var frame = this._frame;
  1707. frame.masked = (octet & this.MASK) === this.MASK;
  1708. frame.length = (octet & this.LENGTH);
  1709. if (frame.length >= 0 && frame.length <= 125) {
  1710. this._stage = frame.masked ? 3 : 4;
  1711. if (!this._checkFrameLength()) return;
  1712. } else {
  1713. this._stage = 2;
  1714. frame.lengthBytes = (frame.length === 126 ? 2 : 8);
  1715. }
  1716. if (this._requireMasking && !frame.masked)
  1717. return this._fail('unacceptable', 'Received unmasked frame but masking is required');
  1718. },
  1719. _parseExtendedLength: function(buffer) {
  1720. var frame = this._frame;
  1721. frame.length = this._readUInt(buffer);
  1722. this._stage = frame.masked ? 3 : 4;
  1723. if (this.MESSAGE_OPCODES.indexOf(frame.opcode) < 0 && frame.length > 125)
  1724. return this._fail('protocol_error', 'Received control frame having too long payload: ' + frame.length);
  1725. if (!this._checkFrameLength()) return;
  1726. },
  1727. _checkFrameLength: function() {
  1728. var length = this._message ? this._message.length : 0;
  1729. if (length + this._frame.length > this._maxLength) {
  1730. this._fail('too_large', 'WebSocket frame length too large');
  1731. return false;
  1732. } else {
  1733. return true;
  1734. }
  1735. },
  1736. _emitFrame: function(buffer) {
  1737. var frame = this._frame,
  1738. payload = frame.payload = Hybi$2.mask(buffer, frame.maskingKey),
  1739. opcode = frame.opcode,
  1740. message,
  1741. code, reason,
  1742. callbacks, callback;
  1743. delete this._frame;
  1744. if (opcode === this.OPCODES.continuation) {
  1745. if (!this._message) return this._fail('protocol_error', 'Received unexpected continuation frame');
  1746. this._message.pushFrame(frame);
  1747. }
  1748. if (opcode === this.OPCODES.text || opcode === this.OPCODES.binary) {
  1749. this._message = new Message();
  1750. this._message.pushFrame(frame);
  1751. }
  1752. if (frame.final && this.MESSAGE_OPCODES.indexOf(opcode) >= 0)
  1753. return this._emitMessage(this._message);
  1754. if (opcode === this.OPCODES.close) {
  1755. code = (payload.length >= 2) ? payload.readUInt16BE(0) : null;
  1756. reason = (payload.length > 2) ? this._encode(payload.slice(2)) : null;
  1757. if (!(payload.length === 0) &&
  1758. !(code !== null && code >= this.MIN_RESERVED_ERROR && code <= this.MAX_RESERVED_ERROR) &&
  1759. this.ERROR_CODES.indexOf(code) < 0)
  1760. code = this.ERRORS.protocol_error;
  1761. if (payload.length > 125 || (payload.length > 2 && !reason))
  1762. code = this.ERRORS.protocol_error;
  1763. this._shutdown(code || this.DEFAULT_ERROR_CODE, reason || '');
  1764. }
  1765. if (opcode === this.OPCODES.ping) {
  1766. this.frame(payload, 'pong');
  1767. this.emit('ping', new Base$6.PingEvent(payload.toString()));
  1768. }
  1769. if (opcode === this.OPCODES.pong) {
  1770. callbacks = this._pingCallbacks;
  1771. message = this._encode(payload);
  1772. callback = callbacks[message];
  1773. delete callbacks[message];
  1774. if (callback) callback();
  1775. this.emit('pong', new Base$6.PongEvent(payload.toString()));
  1776. }
  1777. },
  1778. _emitMessage: function(message) {
  1779. var message = this._message;
  1780. message.read();
  1781. delete this._message;
  1782. this._extensions.processIncomingMessage(message, function(error, message) {
  1783. if (error) return this._fail('extension_error', error.message);
  1784. var payload = message.data;
  1785. if (message.opcode === this.OPCODES.text) payload = this._encode(payload);
  1786. if (payload === null)
  1787. return this._fail('encoding_error', 'Could not decode a text frame as UTF-8');
  1788. else
  1789. this.emit('message', new Base$6.MessageEvent(payload));
  1790. }, this);
  1791. },
  1792. _encode: function(buffer) {
  1793. try {
  1794. var string = buffer.toString('binary', 0, buffer.length);
  1795. if (!this.UTF8_MATCH.test(string)) return null;
  1796. } catch (e) {}
  1797. return buffer.toString('utf8', 0, buffer.length);
  1798. },
  1799. _readUInt: function(buffer) {
  1800. if (buffer.length === 2) return buffer.readUInt16BE(0);
  1801. return buffer.readUInt32BE(0) * 0x100000000 +
  1802. buffer.readUInt32BE(4);
  1803. }
  1804. };
  1805. for (var key$7 in instance$7)
  1806. Hybi$2.prototype[key$7] = instance$7[key$7];
  1807. var hybi = Hybi$2;
  1808. var Buffer$4 = safeBuffer.exports.Buffer,
  1809. Stream$2 = require$$0__default$1["default"].Stream,
  1810. url$2 = require$$2__default$1["default"],
  1811. util$9 = require$$2__default["default"],
  1812. Base$5 = base,
  1813. Headers$1 = headers,
  1814. HttpParser$2 = http_parser;
  1815. var PORTS = { 'ws:': 80, 'wss:': 443 };
  1816. var Proxy$1 = function(client, origin, options) {
  1817. this._client = client;
  1818. this._http = new HttpParser$2('response');
  1819. this._origin = (typeof client.url === 'object') ? client.url : url$2.parse(client.url);
  1820. this._url = (typeof origin === 'object') ? origin : url$2.parse(origin);
  1821. this._options = options || {};
  1822. this._state = 0;
  1823. this.readable = this.writable = true;
  1824. this._paused = false;
  1825. this._headers = new Headers$1();
  1826. this._headers.set('Host', this._origin.host);
  1827. this._headers.set('Connection', 'keep-alive');
  1828. this._headers.set('Proxy-Connection', 'keep-alive');
  1829. var auth = this._url.auth && Buffer$4.from(this._url.auth, 'utf8').toString('base64');
  1830. if (auth) this._headers.set('Proxy-Authorization', 'Basic ' + auth);
  1831. };
  1832. util$9.inherits(Proxy$1, Stream$2);
  1833. var instance$6 = {
  1834. setHeader: function(name, value) {
  1835. if (this._state !== 0) return false;
  1836. this._headers.set(name, value);
  1837. return true;
  1838. },
  1839. start: function() {
  1840. if (this._state !== 0) return false;
  1841. this._state = 1;
  1842. var origin = this._origin,
  1843. port = origin.port || PORTS[origin.protocol],
  1844. start = 'CONNECT ' + origin.hostname + ':' + port + ' HTTP/1.1';
  1845. var headers = [start, this._headers.toString(), ''];
  1846. this.emit('data', Buffer$4.from(headers.join('\r\n'), 'utf8'));
  1847. return true;
  1848. },
  1849. pause: function() {
  1850. this._paused = true;
  1851. },
  1852. resume: function() {
  1853. this._paused = false;
  1854. this.emit('drain');
  1855. },
  1856. write: function(chunk) {
  1857. if (!this.writable) return false;
  1858. this._http.parse(chunk);
  1859. if (!this._http.isComplete()) return !this._paused;
  1860. this.statusCode = this._http.statusCode;
  1861. this.headers = this._http.headers;
  1862. if (this.statusCode === 200) {
  1863. this.emit('connect', new Base$5.ConnectEvent());
  1864. } else {
  1865. var message = "Can't establish a connection to the server at " + this._origin.href;
  1866. this.emit('error', new Error(message));
  1867. }
  1868. this.end();
  1869. return !this._paused;
  1870. },
  1871. end: function(chunk) {
  1872. if (!this.writable) return;
  1873. if (chunk !== undefined) this.write(chunk);
  1874. this.readable = this.writable = false;
  1875. this.emit('close');
  1876. this.emit('end');
  1877. },
  1878. destroy: function() {
  1879. this.end();
  1880. }
  1881. };
  1882. for (var key$6 in instance$6)
  1883. Proxy$1.prototype[key$6] = instance$6[key$6];
  1884. var proxy = Proxy$1;
  1885. var Buffer$3 = safeBuffer.exports.Buffer,
  1886. crypto$1 = require$$1__default$1["default"],
  1887. url$1 = require$$2__default$1["default"],
  1888. util$8 = require$$2__default["default"],
  1889. HttpParser$1 = http_parser,
  1890. Base$4 = base,
  1891. Hybi$1 = hybi,
  1892. Proxy = proxy;
  1893. var Client$2 = function(_url, options) {
  1894. this.version = 'hybi-' + Hybi$1.VERSION;
  1895. Hybi$1.call(this, null, _url, options);
  1896. this.readyState = -1;
  1897. this._key = Client$2.generateKey();
  1898. this._accept = Hybi$1.generateAccept(this._key);
  1899. this._http = new HttpParser$1('response');
  1900. var uri = url$1.parse(this.url),
  1901. auth = uri.auth && Buffer$3.from(uri.auth, 'utf8').toString('base64');
  1902. if (this.VALID_PROTOCOLS.indexOf(uri.protocol) < 0)
  1903. throw new Error(this.url + ' is not a valid WebSocket URL');
  1904. this._pathname = (uri.pathname || '/') + (uri.search || '');
  1905. this._headers.set('Host', uri.host);
  1906. this._headers.set('Upgrade', 'websocket');
  1907. this._headers.set('Connection', 'Upgrade');
  1908. this._headers.set('Sec-WebSocket-Key', this._key);
  1909. this._headers.set('Sec-WebSocket-Version', Hybi$1.VERSION);
  1910. if (this._protocols.length > 0)
  1911. this._headers.set('Sec-WebSocket-Protocol', this._protocols.join(', '));
  1912. if (auth)
  1913. this._headers.set('Authorization', 'Basic ' + auth);
  1914. };
  1915. util$8.inherits(Client$2, Hybi$1);
  1916. Client$2.generateKey = function() {
  1917. return crypto$1.randomBytes(16).toString('base64');
  1918. };
  1919. var instance$5 = {
  1920. VALID_PROTOCOLS: ['ws:', 'wss:'],
  1921. proxy: function(origin, options) {
  1922. return new Proxy(this, origin, options);
  1923. },
  1924. start: function() {
  1925. if (this.readyState !== -1) return false;
  1926. this._write(this._handshakeRequest());
  1927. this.readyState = 0;
  1928. return true;
  1929. },
  1930. parse: function(chunk) {
  1931. if (this.readyState === 3) return;
  1932. if (this.readyState > 0) return Hybi$1.prototype.parse.call(this, chunk);
  1933. this._http.parse(chunk);
  1934. if (!this._http.isComplete()) return;
  1935. this._validateHandshake();
  1936. if (this.readyState === 3) return;
  1937. this._open();
  1938. this.parse(this._http.body);
  1939. },
  1940. _handshakeRequest: function() {
  1941. var extensions = this._extensions.generateOffer();
  1942. if (extensions)
  1943. this._headers.set('Sec-WebSocket-Extensions', extensions);
  1944. var start = 'GET ' + this._pathname + ' HTTP/1.1',
  1945. headers = [start, this._headers.toString(), ''];
  1946. return Buffer$3.from(headers.join('\r\n'), 'utf8');
  1947. },
  1948. _failHandshake: function(message) {
  1949. message = 'Error during WebSocket handshake: ' + message;
  1950. this.readyState = 3;
  1951. this.emit('error', new Error(message));
  1952. this.emit('close', new Base$4.CloseEvent(this.ERRORS.protocol_error, message));
  1953. },
  1954. _validateHandshake: function() {
  1955. this.statusCode = this._http.statusCode;
  1956. this.headers = this._http.headers;
  1957. if (this._http.error)
  1958. return this._failHandshake(this._http.error.message);
  1959. if (this._http.statusCode !== 101)
  1960. return this._failHandshake('Unexpected response code: ' + this._http.statusCode);
  1961. var headers = this._http.headers,
  1962. upgrade = headers['upgrade'] || '',
  1963. connection = headers['connection'] || '',
  1964. accept = headers['sec-websocket-accept'] || '',
  1965. protocol = headers['sec-websocket-protocol'] || '';
  1966. if (upgrade === '')
  1967. return this._failHandshake("'Upgrade' header is missing");
  1968. if (upgrade.toLowerCase() !== 'websocket')
  1969. return this._failHandshake("'Upgrade' header value is not 'WebSocket'");
  1970. if (connection === '')
  1971. return this._failHandshake("'Connection' header is missing");
  1972. if (connection.toLowerCase() !== 'upgrade')
  1973. return this._failHandshake("'Connection' header value is not 'Upgrade'");
  1974. if (accept !== this._accept)
  1975. return this._failHandshake('Sec-WebSocket-Accept mismatch');
  1976. this.protocol = null;
  1977. if (protocol !== '') {
  1978. if (this._protocols.indexOf(protocol) < 0)
  1979. return this._failHandshake('Sec-WebSocket-Protocol mismatch');
  1980. else
  1981. this.protocol = protocol;
  1982. }
  1983. try {
  1984. this._extensions.activate(this.headers['sec-websocket-extensions']);
  1985. } catch (e) {
  1986. return this._failHandshake(e.message);
  1987. }
  1988. }
  1989. };
  1990. for (var key$5 in instance$5)
  1991. Client$2.prototype[key$5] = instance$5[key$5];
  1992. var client$1 = Client$2;
  1993. var Buffer$2 = safeBuffer.exports.Buffer,
  1994. Base$3 = base,
  1995. util$7 = require$$2__default["default"];
  1996. var Draft75$2 = function(request, url, options) {
  1997. Base$3.apply(this, arguments);
  1998. this._stage = 0;
  1999. this.version = 'hixie-75';
  2000. this._headers.set('Upgrade', 'WebSocket');
  2001. this._headers.set('Connection', 'Upgrade');
  2002. this._headers.set('WebSocket-Origin', this._request.headers.origin);
  2003. this._headers.set('WebSocket-Location', this.url);
  2004. };
  2005. util$7.inherits(Draft75$2, Base$3);
  2006. var instance$4 = {
  2007. close: function() {
  2008. if (this.readyState === 3) return false;
  2009. this.readyState = 3;
  2010. this.emit('close', new Base$3.CloseEvent(null, null));
  2011. return true;
  2012. },
  2013. parse: function(chunk) {
  2014. if (this.readyState > 1) return;
  2015. this._reader.put(chunk);
  2016. this._reader.eachByte(function(octet) {
  2017. var message;
  2018. switch (this._stage) {
  2019. case -1:
  2020. this._body.push(octet);
  2021. this._sendHandshakeBody();
  2022. break;
  2023. case 0:
  2024. this._parseLeadingByte(octet);
  2025. break;
  2026. case 1:
  2027. this._length = (octet & 0x7F) + 128 * this._length;
  2028. if (this._closing && this._length === 0) {
  2029. return this.close();
  2030. }
  2031. else if ((octet & 0x80) !== 0x80) {
  2032. if (this._length === 0) {
  2033. this._stage = 0;
  2034. }
  2035. else {
  2036. this._skipped = 0;
  2037. this._stage = 2;
  2038. }
  2039. }
  2040. break;
  2041. case 2:
  2042. if (octet === 0xFF) {
  2043. this._stage = 0;
  2044. message = Buffer$2.from(this._buffer).toString('utf8', 0, this._buffer.length);
  2045. this.emit('message', new Base$3.MessageEvent(message));
  2046. }
  2047. else {
  2048. if (this._length) {
  2049. this._skipped += 1;
  2050. if (this._skipped === this._length)
  2051. this._stage = 0;
  2052. } else {
  2053. this._buffer.push(octet);
  2054. if (this._buffer.length > this._maxLength) return this.close();
  2055. }
  2056. }
  2057. break;
  2058. }
  2059. }, this);
  2060. },
  2061. frame: function(buffer) {
  2062. if (this.readyState === 0) return this._queue([buffer]);
  2063. if (this.readyState > 1) return false;
  2064. if (typeof buffer !== 'string') buffer = buffer.toString();
  2065. var length = Buffer$2.byteLength(buffer),
  2066. frame = Buffer$2.allocUnsafe(length + 2);
  2067. frame[0] = 0x00;
  2068. frame.write(buffer, 1);
  2069. frame[frame.length - 1] = 0xFF;
  2070. this._write(frame);
  2071. return true;
  2072. },
  2073. _handshakeResponse: function() {
  2074. var start = 'HTTP/1.1 101 Web Socket Protocol Handshake',
  2075. headers = [start, this._headers.toString(), ''];
  2076. return Buffer$2.from(headers.join('\r\n'), 'utf8');
  2077. },
  2078. _parseLeadingByte: function(octet) {
  2079. if ((octet & 0x80) === 0x80) {
  2080. this._length = 0;
  2081. this._stage = 1;
  2082. } else {
  2083. delete this._length;
  2084. delete this._skipped;
  2085. this._buffer = [];
  2086. this._stage = 2;
  2087. }
  2088. }
  2089. };
  2090. for (var key$4 in instance$4)
  2091. Draft75$2.prototype[key$4] = instance$4[key$4];
  2092. var draft75 = Draft75$2;
  2093. var Buffer$1 = safeBuffer.exports.Buffer,
  2094. Base$2 = base,
  2095. Draft75$1 = draft75,
  2096. crypto = require$$1__default$1["default"],
  2097. util$6 = require$$2__default["default"];
  2098. var numberFromKey = function(key) {
  2099. return parseInt((key.match(/[0-9]/g) || []).join(''), 10);
  2100. };
  2101. var spacesInKey = function(key) {
  2102. return (key.match(/ /g) || []).length;
  2103. };
  2104. var Draft76$1 = function(request, url, options) {
  2105. Draft75$1.apply(this, arguments);
  2106. this._stage = -1;
  2107. this._body = [];
  2108. this.version = 'hixie-76';
  2109. this._headers.clear();
  2110. this._headers.set('Upgrade', 'WebSocket');
  2111. this._headers.set('Connection', 'Upgrade');
  2112. this._headers.set('Sec-WebSocket-Origin', this._request.headers.origin);
  2113. this._headers.set('Sec-WebSocket-Location', this.url);
  2114. };
  2115. util$6.inherits(Draft76$1, Draft75$1);
  2116. var instance$3 = {
  2117. BODY_SIZE: 8,
  2118. start: function() {
  2119. if (!Draft75$1.prototype.start.call(this)) return false;
  2120. this._started = true;
  2121. this._sendHandshakeBody();
  2122. return true;
  2123. },
  2124. close: function() {
  2125. if (this.readyState === 3) return false;
  2126. if (this.readyState === 1) this._write(Buffer$1.from([0xFF, 0x00]));
  2127. this.readyState = 3;
  2128. this.emit('close', new Base$2.CloseEvent(null, null));
  2129. return true;
  2130. },
  2131. _handshakeResponse: function() {
  2132. var headers = this._request.headers,
  2133. key1 = headers['sec-websocket-key1'],
  2134. key2 = headers['sec-websocket-key2'];
  2135. if (!key1) throw new Error('Missing required header: Sec-WebSocket-Key1');
  2136. if (!key2) throw new Error('Missing required header: Sec-WebSocket-Key2');
  2137. var number1 = numberFromKey(key1),
  2138. spaces1 = spacesInKey(key1),
  2139. number2 = numberFromKey(key2),
  2140. spaces2 = spacesInKey(key2);
  2141. if (number1 % spaces1 !== 0 || number2 % spaces2 !== 0)
  2142. throw new Error('Client sent invalid Sec-WebSocket-Key headers');
  2143. this._keyValues = [number1 / spaces1, number2 / spaces2];
  2144. var start = 'HTTP/1.1 101 WebSocket Protocol Handshake',
  2145. headers = [start, this._headers.toString(), ''];
  2146. return Buffer$1.from(headers.join('\r\n'), 'binary');
  2147. },
  2148. _handshakeSignature: function() {
  2149. if (this._body.length < this.BODY_SIZE) return null;
  2150. var md5 = crypto.createHash('md5'),
  2151. buffer = Buffer$1.allocUnsafe(8 + this.BODY_SIZE);
  2152. buffer.writeUInt32BE(this._keyValues[0], 0);
  2153. buffer.writeUInt32BE(this._keyValues[1], 4);
  2154. Buffer$1.from(this._body).copy(buffer, 8, 0, this.BODY_SIZE);
  2155. md5.update(buffer);
  2156. return Buffer$1.from(md5.digest('binary'), 'binary');
  2157. },
  2158. _sendHandshakeBody: function() {
  2159. if (!this._started) return;
  2160. var signature = this._handshakeSignature();
  2161. if (!signature) return;
  2162. this._write(signature);
  2163. this._stage = 0;
  2164. this._open();
  2165. if (this._body.length > this.BODY_SIZE)
  2166. this.parse(this._body.slice(this.BODY_SIZE));
  2167. },
  2168. _parseLeadingByte: function(octet) {
  2169. if (octet !== 0xFF)
  2170. return Draft75$1.prototype._parseLeadingByte.call(this, octet);
  2171. this._closing = true;
  2172. this._length = 0;
  2173. this._stage = 1;
  2174. }
  2175. };
  2176. for (var key$3 in instance$3)
  2177. Draft76$1.prototype[key$3] = instance$3[key$3];
  2178. var draft76 = Draft76$1;
  2179. var util$5 = require$$2__default["default"],
  2180. HttpParser = http_parser,
  2181. Base$1 = base,
  2182. Draft75 = draft75,
  2183. Draft76 = draft76,
  2184. Hybi = hybi;
  2185. var Server$1 = function(options) {
  2186. Base$1.call(this, null, null, options);
  2187. this._http = new HttpParser('request');
  2188. };
  2189. util$5.inherits(Server$1, Base$1);
  2190. var instance$2 = {
  2191. EVENTS: ['open', 'message', 'error', 'close', 'ping', 'pong'],
  2192. _bindEventListeners: function() {
  2193. this.messages.on('error', function() {});
  2194. this.on('error', function() {});
  2195. },
  2196. parse: function(chunk) {
  2197. if (this._delegate) return this._delegate.parse(chunk);
  2198. this._http.parse(chunk);
  2199. if (!this._http.isComplete()) return;
  2200. this.method = this._http.method;
  2201. this.url = this._http.url;
  2202. this.headers = this._http.headers;
  2203. this.body = this._http.body;
  2204. var self = this;
  2205. this._delegate = Server$1.http(this, this._options);
  2206. this._delegate.messages = this.messages;
  2207. this._delegate.io = this.io;
  2208. this._open();
  2209. this.EVENTS.forEach(function(event) {
  2210. this._delegate.on(event, function(e) { self.emit(event, e); });
  2211. }, this);
  2212. this.protocol = this._delegate.protocol;
  2213. this.version = this._delegate.version;
  2214. this.parse(this._http.body);
  2215. this.emit('connect', new Base$1.ConnectEvent());
  2216. },
  2217. _open: function() {
  2218. this.__queue.forEach(function(msg) {
  2219. this._delegate[msg[0]].apply(this._delegate, msg[1]);
  2220. }, this);
  2221. this.__queue = [];
  2222. }
  2223. };
  2224. ['addExtension', 'setHeader', 'start', 'frame', 'text', 'binary', 'ping', 'close'].forEach(function(method) {
  2225. instance$2[method] = function() {
  2226. if (this._delegate) {
  2227. return this._delegate[method].apply(this._delegate, arguments);
  2228. } else {
  2229. this.__queue.push([method, arguments]);
  2230. return true;
  2231. }
  2232. };
  2233. });
  2234. for (var key$2 in instance$2)
  2235. Server$1.prototype[key$2] = instance$2[key$2];
  2236. Server$1.isSecureRequest = function(request) {
  2237. if (request.connection && request.connection.authorized !== undefined) return true;
  2238. if (request.socket && request.socket.secure) return true;
  2239. var headers = request.headers;
  2240. if (!headers) return false;
  2241. if (headers['https'] === 'on') return true;
  2242. if (headers['x-forwarded-ssl'] === 'on') return true;
  2243. if (headers['x-forwarded-scheme'] === 'https') return true;
  2244. if (headers['x-forwarded-proto'] === 'https') return true;
  2245. return false;
  2246. };
  2247. Server$1.determineUrl = function(request) {
  2248. var scheme = this.isSecureRequest(request) ? 'wss:' : 'ws:';
  2249. return scheme + '//' + request.headers.host + request.url;
  2250. };
  2251. Server$1.http = function(request, options) {
  2252. options = options || {};
  2253. if (options.requireMasking === undefined) options.requireMasking = true;
  2254. var headers = request.headers,
  2255. version = headers['sec-websocket-version'],
  2256. key = headers['sec-websocket-key'],
  2257. key1 = headers['sec-websocket-key1'],
  2258. key2 = headers['sec-websocket-key2'],
  2259. url = this.determineUrl(request);
  2260. if (version || key)
  2261. return new Hybi(request, url, options);
  2262. else if (key1 || key2)
  2263. return new Draft76(request, url, options);
  2264. else
  2265. return new Draft75(request, url, options);
  2266. };
  2267. var server = Server$1;
  2268. // Protocol references:
  2269. //
  2270. // * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
  2271. // * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
  2272. // * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
  2273. var Base = base,
  2274. Client$1 = client$1,
  2275. Server = server;
  2276. var Driver = {
  2277. client: function(url, options) {
  2278. options = options || {};
  2279. if (options.masking === undefined) options.masking = true;
  2280. return new Client$1(url, options);
  2281. },
  2282. server: function(options) {
  2283. options = options || {};
  2284. if (options.requireMasking === undefined) options.requireMasking = true;
  2285. return new Server(options);
  2286. },
  2287. http: function() {
  2288. return Server.http.apply(Server, arguments);
  2289. },
  2290. isSecureRequest: function(request) {
  2291. return Server.isSecureRequest(request);
  2292. },
  2293. isWebSocket: function(request) {
  2294. return Base.isWebSocket(request);
  2295. },
  2296. validateOptions: function(options, validKeys) {
  2297. Base.validateOptions(options, validKeys);
  2298. }
  2299. };
  2300. var driver$4 = Driver;
  2301. var Event$3 = function(eventType, options) {
  2302. this.type = eventType;
  2303. for (var key in options)
  2304. this[key] = options[key];
  2305. };
  2306. Event$3.prototype.initEvent = function(eventType, canBubble, cancelable) {
  2307. this.type = eventType;
  2308. this.bubbles = canBubble;
  2309. this.cancelable = cancelable;
  2310. };
  2311. Event$3.prototype.stopPropagation = function() {};
  2312. Event$3.prototype.preventDefault = function() {};
  2313. Event$3.CAPTURING_PHASE = 1;
  2314. Event$3.AT_TARGET = 2;
  2315. Event$3.BUBBLING_PHASE = 3;
  2316. var event = Event$3;
  2317. var Event$2 = event;
  2318. var EventTarget$2 = {
  2319. onopen: null,
  2320. onmessage: null,
  2321. onerror: null,
  2322. onclose: null,
  2323. addEventListener: function(eventType, listener, useCapture) {
  2324. this.on(eventType, listener);
  2325. },
  2326. removeEventListener: function(eventType, listener, useCapture) {
  2327. this.removeListener(eventType, listener);
  2328. },
  2329. dispatchEvent: function(event) {
  2330. event.target = event.currentTarget = this;
  2331. event.eventPhase = Event$2.AT_TARGET;
  2332. if (this['on' + event.type])
  2333. this['on' + event.type](event);
  2334. this.emit(event.type, event);
  2335. }
  2336. };
  2337. var event_target = EventTarget$2;
  2338. var Stream$1 = require$$0__default$1["default"].Stream,
  2339. util$4 = require$$2__default["default"],
  2340. driver$3 = driver$4,
  2341. EventTarget$1 = event_target,
  2342. Event$1 = event;
  2343. var API$3 = function(options) {
  2344. options = options || {};
  2345. driver$3.validateOptions(options, ['headers', 'extensions', 'maxLength', 'ping', 'proxy', 'tls', 'ca']);
  2346. this.readable = this.writable = true;
  2347. var headers = options.headers;
  2348. if (headers) {
  2349. for (var name in headers) this._driver.setHeader(name, headers[name]);
  2350. }
  2351. var extensions = options.extensions;
  2352. if (extensions) {
  2353. [].concat(extensions).forEach(this._driver.addExtension, this._driver);
  2354. }
  2355. this._ping = options.ping;
  2356. this._pingId = 0;
  2357. this.readyState = API$3.CONNECTING;
  2358. this.bufferedAmount = 0;
  2359. this.protocol = '';
  2360. this.url = this._driver.url;
  2361. this.version = this._driver.version;
  2362. var self = this;
  2363. this._driver.on('open', function(e) { self._open(); });
  2364. this._driver.on('message', function(e) { self._receiveMessage(e.data); });
  2365. this._driver.on('close', function(e) { self._beginClose(e.reason, e.code); });
  2366. this._driver.on('error', function(error) {
  2367. self._emitError(error.message);
  2368. });
  2369. this.on('error', function() {});
  2370. this._driver.messages.on('drain', function() {
  2371. self.emit('drain');
  2372. });
  2373. if (this._ping)
  2374. this._pingTimer = setInterval(function() {
  2375. self._pingId += 1;
  2376. self.ping(self._pingId.toString());
  2377. }, this._ping * 1000);
  2378. this._configureStream();
  2379. if (!this._proxy) {
  2380. this._stream.pipe(this._driver.io);
  2381. this._driver.io.pipe(this._stream);
  2382. }
  2383. };
  2384. util$4.inherits(API$3, Stream$1);
  2385. API$3.CONNECTING = 0;
  2386. API$3.OPEN = 1;
  2387. API$3.CLOSING = 2;
  2388. API$3.CLOSED = 3;
  2389. API$3.CLOSE_TIMEOUT = 30000;
  2390. var instance$1 = {
  2391. write: function(data) {
  2392. return this.send(data);
  2393. },
  2394. end: function(data) {
  2395. if (data !== undefined) this.send(data);
  2396. this.close();
  2397. },
  2398. pause: function() {
  2399. return this._driver.messages.pause();
  2400. },
  2401. resume: function() {
  2402. return this._driver.messages.resume();
  2403. },
  2404. send: function(data) {
  2405. if (this.readyState > API$3.OPEN) return false;
  2406. if (!(data instanceof Buffer)) data = String(data);
  2407. return this._driver.messages.write(data);
  2408. },
  2409. ping: function(message, callback) {
  2410. if (this.readyState > API$3.OPEN) return false;
  2411. return this._driver.ping(message, callback);
  2412. },
  2413. close: function(code, reason) {
  2414. if (code === undefined) code = 1000;
  2415. if (reason === undefined) reason = '';
  2416. if (code !== 1000 && (code < 3000 || code > 4999))
  2417. throw new Error("Failed to execute 'close' on WebSocket: " +
  2418. "The code must be either 1000, or between 3000 and 4999. " +
  2419. code + " is neither.");
  2420. if (this.readyState < API$3.CLOSING) {
  2421. var self = this;
  2422. this._closeTimer = setTimeout(function() {
  2423. self._beginClose('', 1006);
  2424. }, API$3.CLOSE_TIMEOUT);
  2425. }
  2426. if (this.readyState !== API$3.CLOSED) this.readyState = API$3.CLOSING;
  2427. this._driver.close(reason, code);
  2428. },
  2429. _configureStream: function() {
  2430. var self = this;
  2431. this._stream.setTimeout(0);
  2432. this._stream.setNoDelay(true);
  2433. ['close', 'end'].forEach(function(event) {
  2434. this._stream.on(event, function() { self._finalizeClose(); });
  2435. }, this);
  2436. this._stream.on('error', function(error) {
  2437. self._emitError('Network error: ' + self.url + ': ' + error.message);
  2438. self._finalizeClose();
  2439. });
  2440. },
  2441. _open: function() {
  2442. if (this.readyState !== API$3.CONNECTING) return;
  2443. this.readyState = API$3.OPEN;
  2444. this.protocol = this._driver.protocol || '';
  2445. var event = new Event$1('open');
  2446. event.initEvent('open', false, false);
  2447. this.dispatchEvent(event);
  2448. },
  2449. _receiveMessage: function(data) {
  2450. if (this.readyState > API$3.OPEN) return false;
  2451. if (this.readable) this.emit('data', data);
  2452. var event = new Event$1('message', { data: data });
  2453. event.initEvent('message', false, false);
  2454. this.dispatchEvent(event);
  2455. },
  2456. _emitError: function(message) {
  2457. if (this.readyState >= API$3.CLOSING) return;
  2458. var event = new Event$1('error', { message: message });
  2459. event.initEvent('error', false, false);
  2460. this.dispatchEvent(event);
  2461. },
  2462. _beginClose: function(reason, code) {
  2463. if (this.readyState === API$3.CLOSED) return;
  2464. this.readyState = API$3.CLOSING;
  2465. this._closeParams = [reason, code];
  2466. if (this._stream) {
  2467. this._stream.destroy();
  2468. if (!this._stream.readable) this._finalizeClose();
  2469. }
  2470. },
  2471. _finalizeClose: function() {
  2472. if (this.readyState === API$3.CLOSED) return;
  2473. this.readyState = API$3.CLOSED;
  2474. if (this._closeTimer) clearTimeout(this._closeTimer);
  2475. if (this._pingTimer) clearInterval(this._pingTimer);
  2476. if (this._stream) this._stream.end();
  2477. if (this.readable) this.emit('end');
  2478. this.readable = this.writable = false;
  2479. var reason = this._closeParams ? this._closeParams[0] : '',
  2480. code = this._closeParams ? this._closeParams[1] : 1006;
  2481. var event = new Event$1('close', { code: code, reason: reason });
  2482. event.initEvent('close', false, false);
  2483. this.dispatchEvent(event);
  2484. }
  2485. };
  2486. for (var method$1 in instance$1) API$3.prototype[method$1] = instance$1[method$1];
  2487. for (var key$1 in EventTarget$1) API$3.prototype[key$1] = EventTarget$1[key$1];
  2488. var api = API$3;
  2489. var util$3 = require$$2__default["default"],
  2490. net = require$$1__default$2["default"],
  2491. tls = require$$2__default$2["default"],
  2492. url = require$$2__default$1["default"],
  2493. driver$2 = driver$4,
  2494. API$2 = api;
  2495. var DEFAULT_PORTS = { 'http:': 80, 'https:': 443, 'ws:':80, 'wss:': 443 },
  2496. SECURE_PROTOCOLS = ['https:', 'wss:'];
  2497. var Client = function(_url, protocols, options) {
  2498. options = options || {};
  2499. this.url = _url;
  2500. this._driver = driver$2.client(this.url, { maxLength: options.maxLength, protocols: protocols });
  2501. ['open', 'error'].forEach(function(event) {
  2502. this._driver.on(event, function() {
  2503. self.headers = self._driver.headers;
  2504. self.statusCode = self._driver.statusCode;
  2505. });
  2506. }, this);
  2507. var proxy = options.proxy || {},
  2508. endpoint = url.parse(proxy.origin || this.url),
  2509. port = endpoint.port || DEFAULT_PORTS[endpoint.protocol],
  2510. secure = SECURE_PROTOCOLS.indexOf(endpoint.protocol) >= 0,
  2511. onConnect = function() { self._onConnect(); },
  2512. netOptions = options.net || {},
  2513. originTLS = options.tls || {},
  2514. socketTLS = proxy.origin ? (proxy.tls || {}) : originTLS,
  2515. self = this;
  2516. netOptions.host = socketTLS.host = endpoint.hostname;
  2517. netOptions.port = socketTLS.port = port;
  2518. originTLS.ca = originTLS.ca || options.ca;
  2519. socketTLS.servername = socketTLS.servername || endpoint.hostname;
  2520. this._stream = secure
  2521. ? tls.connect(socketTLS, onConnect)
  2522. : net.connect(netOptions, onConnect);
  2523. if (proxy.origin) this._configureProxy(proxy, originTLS);
  2524. API$2.call(this, options);
  2525. };
  2526. util$3.inherits(Client, API$2);
  2527. Client.prototype._onConnect = function() {
  2528. var worker = this._proxy || this._driver;
  2529. worker.start();
  2530. };
  2531. Client.prototype._configureProxy = function(proxy, originTLS) {
  2532. var uri = url.parse(this.url),
  2533. secure = SECURE_PROTOCOLS.indexOf(uri.protocol) >= 0,
  2534. self = this,
  2535. name;
  2536. this._proxy = this._driver.proxy(proxy.origin);
  2537. if (proxy.headers) {
  2538. for (name in proxy.headers) this._proxy.setHeader(name, proxy.headers[name]);
  2539. }
  2540. this._proxy.pipe(this._stream, { end: false });
  2541. this._stream.pipe(this._proxy);
  2542. this._proxy.on('connect', function() {
  2543. if (secure) {
  2544. var options = { socket: self._stream, servername: uri.hostname };
  2545. for (name in originTLS) options[name] = originTLS[name];
  2546. self._stream = tls.connect(options);
  2547. self._configureStream();
  2548. }
  2549. self._driver.io.pipe(self._stream);
  2550. self._stream.pipe(self._driver.io);
  2551. self._driver.start();
  2552. });
  2553. this._proxy.on('error', function(error) {
  2554. self._driver.emit('error', error);
  2555. });
  2556. };
  2557. var client = Client;
  2558. var Stream = require$$0__default$1["default"].Stream,
  2559. util$2 = require$$2__default["default"],
  2560. driver$1 = driver$4,
  2561. Headers = headers,
  2562. API$1 = api,
  2563. EventTarget = event_target,
  2564. Event = event;
  2565. var EventSource = function(request, response, options) {
  2566. this.writable = true;
  2567. options = options || {};
  2568. this._stream = response.socket;
  2569. this._ping = options.ping || this.DEFAULT_PING;
  2570. this._retry = options.retry || this.DEFAULT_RETRY;
  2571. var scheme = driver$1.isSecureRequest(request) ? 'https:' : 'http:';
  2572. this.url = scheme + '//' + request.headers.host + request.url;
  2573. this.lastEventId = request.headers['last-event-id'] || '';
  2574. this.readyState = API$1.CONNECTING;
  2575. var headers = new Headers(),
  2576. self = this;
  2577. if (options.headers) {
  2578. for (var key in options.headers) headers.set(key, options.headers[key]);
  2579. }
  2580. if (!this._stream || !this._stream.writable) return;
  2581. process.nextTick(function() { self._open(); });
  2582. this._stream.setTimeout(0);
  2583. this._stream.setNoDelay(true);
  2584. var handshake = 'HTTP/1.1 200 OK\r\n' +
  2585. 'Content-Type: text/event-stream\r\n' +
  2586. 'Cache-Control: no-cache, no-store\r\n' +
  2587. 'Connection: close\r\n' +
  2588. headers.toString() +
  2589. '\r\n' +
  2590. 'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n';
  2591. this._write(handshake);
  2592. this._stream.on('drain', function() { self.emit('drain'); });
  2593. if (this._ping)
  2594. this._pingTimer = setInterval(function() { self.ping(); }, this._ping * 1000);
  2595. ['error', 'end'].forEach(function(event) {
  2596. self._stream.on(event, function() { self.close(); });
  2597. });
  2598. };
  2599. util$2.inherits(EventSource, Stream);
  2600. EventSource.isEventSource = function(request) {
  2601. if (request.method !== 'GET') return false;
  2602. var accept = (request.headers.accept || '').split(/\s*,\s*/);
  2603. return accept.indexOf('text/event-stream') >= 0;
  2604. };
  2605. var instance = {
  2606. DEFAULT_PING: 10,
  2607. DEFAULT_RETRY: 5,
  2608. _write: function(chunk) {
  2609. if (!this.writable) return false;
  2610. try {
  2611. return this._stream.write(chunk, 'utf8');
  2612. } catch (e) {
  2613. return false;
  2614. }
  2615. },
  2616. _open: function() {
  2617. if (this.readyState !== API$1.CONNECTING) return;
  2618. this.readyState = API$1.OPEN;
  2619. var event = new Event('open');
  2620. event.initEvent('open', false, false);
  2621. this.dispatchEvent(event);
  2622. },
  2623. write: function(message) {
  2624. return this.send(message);
  2625. },
  2626. end: function(message) {
  2627. if (message !== undefined) this.write(message);
  2628. this.close();
  2629. },
  2630. send: function(message, options) {
  2631. if (this.readyState > API$1.OPEN) return false;
  2632. message = String(message).replace(/(\r\n|\r|\n)/g, '$1data: ');
  2633. options = options || {};
  2634. var frame = '';
  2635. if (options.event) frame += 'event: ' + options.event + '\r\n';
  2636. if (options.id) frame += 'id: ' + options.id + '\r\n';
  2637. frame += 'data: ' + message + '\r\n\r\n';
  2638. return this._write(frame);
  2639. },
  2640. ping: function() {
  2641. return this._write(':\r\n\r\n');
  2642. },
  2643. close: function() {
  2644. if (this.readyState > API$1.OPEN) return false;
  2645. this.readyState = API$1.CLOSED;
  2646. this.writable = false;
  2647. if (this._pingTimer) clearInterval(this._pingTimer);
  2648. if (this._stream) this._stream.end();
  2649. var event = new Event('close');
  2650. event.initEvent('close', false, false);
  2651. this.dispatchEvent(event);
  2652. return true;
  2653. }
  2654. };
  2655. for (var method in instance) EventSource.prototype[method] = instance[method];
  2656. for (var key in EventTarget) EventSource.prototype[key] = EventTarget[key];
  2657. var eventsource = EventSource;
  2658. var util$1 = require$$2__default["default"],
  2659. driver = driver$4,
  2660. API = api;
  2661. var WebSocket$1 = function(request, socket, body, protocols, options) {
  2662. options = options || {};
  2663. this._stream = socket;
  2664. this._driver = driver.http(request, { maxLength: options.maxLength, protocols: protocols });
  2665. var self = this;
  2666. if (!this._stream || !this._stream.writable) return;
  2667. if (!this._stream.readable) return this._stream.end();
  2668. var catchup = function() { self._stream.removeListener('data', catchup); };
  2669. this._stream.on('data', catchup);
  2670. API.call(this, options);
  2671. process.nextTick(function() {
  2672. self._driver.start();
  2673. self._driver.io.write(body);
  2674. });
  2675. };
  2676. util$1.inherits(WebSocket$1, API);
  2677. WebSocket$1.isWebSocket = function(request) {
  2678. return driver.isWebSocket(request);
  2679. };
  2680. WebSocket$1.validateOptions = function(options, validKeys) {
  2681. driver.validateOptions(options, validKeys);
  2682. };
  2683. WebSocket$1.WebSocket = WebSocket$1;
  2684. WebSocket$1.Client = client;
  2685. WebSocket$1.EventSource = eventsource;
  2686. var websocket = WebSocket$1;
  2687. Object.defineProperty(index_standalone, '__esModule', { value: true });
  2688. var Websocket = websocket;
  2689. var util = require$$1__default$3["default"];
  2690. var tslib = require$$2__default$3["default"];
  2691. var logger$1 = require$$3__default["default"];
  2692. function _interopDefaultLegacy$1 (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  2693. var Websocket__default = /*#__PURE__*/_interopDefaultLegacy$1(Websocket);
  2694. /**
  2695. * @license
  2696. * Copyright 2017 Google LLC
  2697. *
  2698. * Licensed under the Apache License, Version 2.0 (the "License");
  2699. * you may not use this file except in compliance with the License.
  2700. * You may obtain a copy of the License at
  2701. *
  2702. * http://www.apache.org/licenses/LICENSE-2.0
  2703. *
  2704. * Unless required by applicable law or agreed to in writing, software
  2705. * distributed under the License is distributed on an "AS IS" BASIS,
  2706. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2707. * See the License for the specific language governing permissions and
  2708. * limitations under the License.
  2709. */
  2710. var PROTOCOL_VERSION = '5';
  2711. var VERSION_PARAM = 'v';
  2712. var TRANSPORT_SESSION_PARAM = 's';
  2713. var REFERER_PARAM = 'r';
  2714. var FORGE_REF = 'f';
  2715. // Matches console.firebase.google.com, firebase-console-*.corp.google.com and
  2716. // firebase.corp.google.com
  2717. var FORGE_DOMAIN_RE = /(console\.firebase|firebase-console-\w+\.corp|firebase\.corp)\.google\.com/;
  2718. var LAST_SESSION_PARAM = 'ls';
  2719. var APPLICATION_ID_PARAM = 'p';
  2720. var APP_CHECK_TOKEN_PARAM = 'ac';
  2721. var WEBSOCKET = 'websocket';
  2722. var LONG_POLLING = 'long_polling';
  2723. /**
  2724. * @license
  2725. * Copyright 2017 Google LLC
  2726. *
  2727. * Licensed under the Apache License, Version 2.0 (the "License");
  2728. * you may not use this file except in compliance with the License.
  2729. * You may obtain a copy of the License at
  2730. *
  2731. * http://www.apache.org/licenses/LICENSE-2.0
  2732. *
  2733. * Unless required by applicable law or agreed to in writing, software
  2734. * distributed under the License is distributed on an "AS IS" BASIS,
  2735. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2736. * See the License for the specific language governing permissions and
  2737. * limitations under the License.
  2738. */
  2739. /**
  2740. * Wraps a DOM Storage object and:
  2741. * - automatically encode objects as JSON strings before storing them to allow us to store arbitrary types.
  2742. * - prefixes names with "firebase:" to avoid collisions with app data.
  2743. *
  2744. * We automatically (see storage.js) create two such wrappers, one for sessionStorage,
  2745. * and one for localStorage.
  2746. *
  2747. */
  2748. var DOMStorageWrapper = /** @class */ (function () {
  2749. /**
  2750. * @param domStorage_ - The underlying storage object (e.g. localStorage or sessionStorage)
  2751. */
  2752. function DOMStorageWrapper(domStorage_) {
  2753. this.domStorage_ = domStorage_;
  2754. // Use a prefix to avoid collisions with other stuff saved by the app.
  2755. this.prefix_ = 'firebase:';
  2756. }
  2757. /**
  2758. * @param key - The key to save the value under
  2759. * @param value - The value being stored, or null to remove the key.
  2760. */
  2761. DOMStorageWrapper.prototype.set = function (key, value) {
  2762. if (value == null) {
  2763. this.domStorage_.removeItem(this.prefixedName_(key));
  2764. }
  2765. else {
  2766. this.domStorage_.setItem(this.prefixedName_(key), util.stringify(value));
  2767. }
  2768. };
  2769. /**
  2770. * @returns The value that was stored under this key, or null
  2771. */
  2772. DOMStorageWrapper.prototype.get = function (key) {
  2773. var storedVal = this.domStorage_.getItem(this.prefixedName_(key));
  2774. if (storedVal == null) {
  2775. return null;
  2776. }
  2777. else {
  2778. return util.jsonEval(storedVal);
  2779. }
  2780. };
  2781. DOMStorageWrapper.prototype.remove = function (key) {
  2782. this.domStorage_.removeItem(this.prefixedName_(key));
  2783. };
  2784. DOMStorageWrapper.prototype.prefixedName_ = function (name) {
  2785. return this.prefix_ + name;
  2786. };
  2787. DOMStorageWrapper.prototype.toString = function () {
  2788. return this.domStorage_.toString();
  2789. };
  2790. return DOMStorageWrapper;
  2791. }());
  2792. /**
  2793. * @license
  2794. * Copyright 2017 Google LLC
  2795. *
  2796. * Licensed under the Apache License, Version 2.0 (the "License");
  2797. * you may not use this file except in compliance with the License.
  2798. * You may obtain a copy of the License at
  2799. *
  2800. * http://www.apache.org/licenses/LICENSE-2.0
  2801. *
  2802. * Unless required by applicable law or agreed to in writing, software
  2803. * distributed under the License is distributed on an "AS IS" BASIS,
  2804. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2805. * See the License for the specific language governing permissions and
  2806. * limitations under the License.
  2807. */
  2808. /**
  2809. * An in-memory storage implementation that matches the API of DOMStorageWrapper
  2810. * (TODO: create interface for both to implement).
  2811. */
  2812. var MemoryStorage = /** @class */ (function () {
  2813. function MemoryStorage() {
  2814. this.cache_ = {};
  2815. this.isInMemoryStorage = true;
  2816. }
  2817. MemoryStorage.prototype.set = function (key, value) {
  2818. if (value == null) {
  2819. delete this.cache_[key];
  2820. }
  2821. else {
  2822. this.cache_[key] = value;
  2823. }
  2824. };
  2825. MemoryStorage.prototype.get = function (key) {
  2826. if (util.contains(this.cache_, key)) {
  2827. return this.cache_[key];
  2828. }
  2829. return null;
  2830. };
  2831. MemoryStorage.prototype.remove = function (key) {
  2832. delete this.cache_[key];
  2833. };
  2834. return MemoryStorage;
  2835. }());
  2836. /**
  2837. * @license
  2838. * Copyright 2017 Google LLC
  2839. *
  2840. * Licensed under the Apache License, Version 2.0 (the "License");
  2841. * you may not use this file except in compliance with the License.
  2842. * You may obtain a copy of the License at
  2843. *
  2844. * http://www.apache.org/licenses/LICENSE-2.0
  2845. *
  2846. * Unless required by applicable law or agreed to in writing, software
  2847. * distributed under the License is distributed on an "AS IS" BASIS,
  2848. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2849. * See the License for the specific language governing permissions and
  2850. * limitations under the License.
  2851. */
  2852. /**
  2853. * Helper to create a DOMStorageWrapper or else fall back to MemoryStorage.
  2854. * TODO: Once MemoryStorage and DOMStorageWrapper have a shared interface this method annotation should change
  2855. * to reflect this type
  2856. *
  2857. * @param domStorageName - Name of the underlying storage object
  2858. * (e.g. 'localStorage' or 'sessionStorage').
  2859. * @returns Turning off type information until a common interface is defined.
  2860. */
  2861. var createStoragefor = function (domStorageName) {
  2862. try {
  2863. // NOTE: just accessing "localStorage" or "window['localStorage']" may throw a security exception,
  2864. // so it must be inside the try/catch.
  2865. if (typeof window !== 'undefined' &&
  2866. typeof window[domStorageName] !== 'undefined') {
  2867. // Need to test cache. Just because it's here doesn't mean it works
  2868. var domStorage = window[domStorageName];
  2869. domStorage.setItem('firebase:sentinel', 'cache');
  2870. domStorage.removeItem('firebase:sentinel');
  2871. return new DOMStorageWrapper(domStorage);
  2872. }
  2873. }
  2874. catch (e) { }
  2875. // Failed to create wrapper. Just return in-memory storage.
  2876. // TODO: log?
  2877. return new MemoryStorage();
  2878. };
  2879. /** A storage object that lasts across sessions */
  2880. var PersistentStorage = createStoragefor('localStorage');
  2881. /** A storage object that only lasts one session */
  2882. var SessionStorage = createStoragefor('sessionStorage');
  2883. /**
  2884. * @license
  2885. * Copyright 2017 Google LLC
  2886. *
  2887. * Licensed under the Apache License, Version 2.0 (the "License");
  2888. * you may not use this file except in compliance with the License.
  2889. * You may obtain a copy of the License at
  2890. *
  2891. * http://www.apache.org/licenses/LICENSE-2.0
  2892. *
  2893. * Unless required by applicable law or agreed to in writing, software
  2894. * distributed under the License is distributed on an "AS IS" BASIS,
  2895. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2896. * See the License for the specific language governing permissions and
  2897. * limitations under the License.
  2898. */
  2899. var logClient$1 = new logger$1.Logger('@firebase/database');
  2900. /**
  2901. * Returns a locally-unique ID (generated by just incrementing up from 0 each time its called).
  2902. */
  2903. var LUIDGenerator = (function () {
  2904. var id = 1;
  2905. return function () {
  2906. return id++;
  2907. };
  2908. })();
  2909. /**
  2910. * Sha1 hash of the input string
  2911. * @param str - The string to hash
  2912. * @returns {!string} The resulting hash
  2913. */
  2914. var sha1 = function (str) {
  2915. var utf8Bytes = util.stringToByteArray(str);
  2916. var sha1 = new util.Sha1();
  2917. sha1.update(utf8Bytes);
  2918. var sha1Bytes = sha1.digest();
  2919. return util.base64.encodeByteArray(sha1Bytes);
  2920. };
  2921. var buildLogMessage_ = function () {
  2922. var varArgs = [];
  2923. for (var _i = 0; _i < arguments.length; _i++) {
  2924. varArgs[_i] = arguments[_i];
  2925. }
  2926. var message = '';
  2927. for (var i = 0; i < varArgs.length; i++) {
  2928. var arg = varArgs[i];
  2929. if (Array.isArray(arg) ||
  2930. (arg &&
  2931. typeof arg === 'object' &&
  2932. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2933. typeof arg.length === 'number')) {
  2934. message += buildLogMessage_.apply(null, arg);
  2935. }
  2936. else if (typeof arg === 'object') {
  2937. message += util.stringify(arg);
  2938. }
  2939. else {
  2940. message += arg;
  2941. }
  2942. message += ' ';
  2943. }
  2944. return message;
  2945. };
  2946. /**
  2947. * Use this for all debug messages in Firebase.
  2948. */
  2949. var logger = null;
  2950. /**
  2951. * Flag to check for log availability on first log message
  2952. */
  2953. var firstLog_ = true;
  2954. /**
  2955. * The implementation of Firebase.enableLogging (defined here to break dependencies)
  2956. * @param logger_ - A flag to turn on logging, or a custom logger
  2957. * @param persistent - Whether or not to persist logging settings across refreshes
  2958. */
  2959. var enableLogging$1 = function (logger_, persistent) {
  2960. util.assert(!persistent || logger_ === true || logger_ === false, "Can't turn on custom loggers persistently.");
  2961. if (logger_ === true) {
  2962. logClient$1.logLevel = logger$1.LogLevel.VERBOSE;
  2963. logger = logClient$1.log.bind(logClient$1);
  2964. if (persistent) {
  2965. SessionStorage.set('logging_enabled', true);
  2966. }
  2967. }
  2968. else if (typeof logger_ === 'function') {
  2969. logger = logger_;
  2970. }
  2971. else {
  2972. logger = null;
  2973. SessionStorage.remove('logging_enabled');
  2974. }
  2975. };
  2976. var log = function () {
  2977. var varArgs = [];
  2978. for (var _i = 0; _i < arguments.length; _i++) {
  2979. varArgs[_i] = arguments[_i];
  2980. }
  2981. if (firstLog_ === true) {
  2982. firstLog_ = false;
  2983. if (logger === null && SessionStorage.get('logging_enabled') === true) {
  2984. enableLogging$1(true);
  2985. }
  2986. }
  2987. if (logger) {
  2988. var message = buildLogMessage_.apply(null, varArgs);
  2989. logger(message);
  2990. }
  2991. };
  2992. var logWrapper = function (prefix) {
  2993. return function () {
  2994. var varArgs = [];
  2995. for (var _i = 0; _i < arguments.length; _i++) {
  2996. varArgs[_i] = arguments[_i];
  2997. }
  2998. log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs), false));
  2999. };
  3000. };
  3001. var error = function () {
  3002. var varArgs = [];
  3003. for (var _i = 0; _i < arguments.length; _i++) {
  3004. varArgs[_i] = arguments[_i];
  3005. }
  3006. var message = 'FIREBASE INTERNAL ERROR: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs), false));
  3007. logClient$1.error(message);
  3008. };
  3009. var fatal = function () {
  3010. var varArgs = [];
  3011. for (var _i = 0; _i < arguments.length; _i++) {
  3012. varArgs[_i] = arguments[_i];
  3013. }
  3014. var message = "FIREBASE FATAL ERROR: ".concat(buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs), false)));
  3015. logClient$1.error(message);
  3016. throw new Error(message);
  3017. };
  3018. var warn$1 = function () {
  3019. var varArgs = [];
  3020. for (var _i = 0; _i < arguments.length; _i++) {
  3021. varArgs[_i] = arguments[_i];
  3022. }
  3023. var message = 'FIREBASE WARNING: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs), false));
  3024. logClient$1.warn(message);
  3025. };
  3026. /**
  3027. * Logs a warning if the containing page uses https. Called when a call to new Firebase
  3028. * does not use https.
  3029. */
  3030. var warnIfPageIsSecure = function () {
  3031. // Be very careful accessing browser globals. Who knows what may or may not exist.
  3032. if (typeof window !== 'undefined' &&
  3033. window.location &&
  3034. window.location.protocol &&
  3035. window.location.protocol.indexOf('https:') !== -1) {
  3036. warn$1('Insecure Firebase access from a secure page. ' +
  3037. 'Please use https in calls to new Firebase().');
  3038. }
  3039. };
  3040. /**
  3041. * Returns true if data is NaN, or +/- Infinity.
  3042. */
  3043. var isInvalidJSONNumber = function (data) {
  3044. return (typeof data === 'number' &&
  3045. (data !== data || // NaN
  3046. data === Number.POSITIVE_INFINITY ||
  3047. data === Number.NEGATIVE_INFINITY));
  3048. };
  3049. var executeWhenDOMReady = function (fn) {
  3050. if (util.isNodeSdk() || document.readyState === 'complete') {
  3051. fn();
  3052. }
  3053. else {
  3054. // Modeled after jQuery. Try DOMContentLoaded and onreadystatechange (which
  3055. // fire before onload), but fall back to onload.
  3056. var called_1 = false;
  3057. var wrappedFn_1 = function () {
  3058. if (!document.body) {
  3059. setTimeout(wrappedFn_1, Math.floor(10));
  3060. return;
  3061. }
  3062. if (!called_1) {
  3063. called_1 = true;
  3064. fn();
  3065. }
  3066. };
  3067. if (document.addEventListener) {
  3068. document.addEventListener('DOMContentLoaded', wrappedFn_1, false);
  3069. // fallback to onload.
  3070. window.addEventListener('load', wrappedFn_1, false);
  3071. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3072. }
  3073. else if (document.attachEvent) {
  3074. // IE.
  3075. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3076. document.attachEvent('onreadystatechange', function () {
  3077. if (document.readyState === 'complete') {
  3078. wrappedFn_1();
  3079. }
  3080. });
  3081. // fallback to onload.
  3082. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3083. window.attachEvent('onload', wrappedFn_1);
  3084. // jQuery has an extra hack for IE that we could employ (based on
  3085. // http://javascript.nwbox.com/IEContentLoaded/) But it looks really old.
  3086. // I'm hoping we don't need it.
  3087. }
  3088. }
  3089. };
  3090. /**
  3091. * Minimum key name. Invalid for actual data, used as a marker to sort before any valid names
  3092. */
  3093. var MIN_NAME = '[MIN_NAME]';
  3094. /**
  3095. * Maximum key name. Invalid for actual data, used as a marker to sort above any valid names
  3096. */
  3097. var MAX_NAME = '[MAX_NAME]';
  3098. /**
  3099. * Compares valid Firebase key names, plus min and max name
  3100. */
  3101. var nameCompare = function (a, b) {
  3102. if (a === b) {
  3103. return 0;
  3104. }
  3105. else if (a === MIN_NAME || b === MAX_NAME) {
  3106. return -1;
  3107. }
  3108. else if (b === MIN_NAME || a === MAX_NAME) {
  3109. return 1;
  3110. }
  3111. else {
  3112. var aAsInt = tryParseInt(a), bAsInt = tryParseInt(b);
  3113. if (aAsInt !== null) {
  3114. if (bAsInt !== null) {
  3115. return aAsInt - bAsInt === 0 ? a.length - b.length : aAsInt - bAsInt;
  3116. }
  3117. else {
  3118. return -1;
  3119. }
  3120. }
  3121. else if (bAsInt !== null) {
  3122. return 1;
  3123. }
  3124. else {
  3125. return a < b ? -1 : 1;
  3126. }
  3127. }
  3128. };
  3129. /**
  3130. * @returns {!number} comparison result.
  3131. */
  3132. var stringCompare = function (a, b) {
  3133. if (a === b) {
  3134. return 0;
  3135. }
  3136. else if (a < b) {
  3137. return -1;
  3138. }
  3139. else {
  3140. return 1;
  3141. }
  3142. };
  3143. var requireKey = function (key, obj) {
  3144. if (obj && key in obj) {
  3145. return obj[key];
  3146. }
  3147. else {
  3148. throw new Error('Missing required key (' + key + ') in object: ' + util.stringify(obj));
  3149. }
  3150. };
  3151. var ObjectToUniqueKey = function (obj) {
  3152. if (typeof obj !== 'object' || obj === null) {
  3153. return util.stringify(obj);
  3154. }
  3155. var keys = [];
  3156. // eslint-disable-next-line guard-for-in
  3157. for (var k in obj) {
  3158. keys.push(k);
  3159. }
  3160. // Export as json, but with the keys sorted.
  3161. keys.sort();
  3162. var key = '{';
  3163. for (var i = 0; i < keys.length; i++) {
  3164. if (i !== 0) {
  3165. key += ',';
  3166. }
  3167. key += util.stringify(keys[i]);
  3168. key += ':';
  3169. key += ObjectToUniqueKey(obj[keys[i]]);
  3170. }
  3171. key += '}';
  3172. return key;
  3173. };
  3174. /**
  3175. * Splits a string into a number of smaller segments of maximum size
  3176. * @param str - The string
  3177. * @param segsize - The maximum number of chars in the string.
  3178. * @returns The string, split into appropriately-sized chunks
  3179. */
  3180. var splitStringBySize = function (str, segsize) {
  3181. var len = str.length;
  3182. if (len <= segsize) {
  3183. return [str];
  3184. }
  3185. var dataSegs = [];
  3186. for (var c = 0; c < len; c += segsize) {
  3187. if (c + segsize > len) {
  3188. dataSegs.push(str.substring(c, len));
  3189. }
  3190. else {
  3191. dataSegs.push(str.substring(c, c + segsize));
  3192. }
  3193. }
  3194. return dataSegs;
  3195. };
  3196. /**
  3197. * Apply a function to each (key, value) pair in an object or
  3198. * apply a function to each (index, value) pair in an array
  3199. * @param obj - The object or array to iterate over
  3200. * @param fn - The function to apply
  3201. */
  3202. function each(obj, fn) {
  3203. for (var key in obj) {
  3204. if (obj.hasOwnProperty(key)) {
  3205. fn(key, obj[key]);
  3206. }
  3207. }
  3208. }
  3209. /**
  3210. * Borrowed from http://hg.secondlife.com/llsd/src/tip/js/typedarray.js (MIT License)
  3211. * I made one modification at the end and removed the NaN / Infinity
  3212. * handling (since it seemed broken [caused an overflow] and we don't need it). See MJL comments.
  3213. * @param v - A double
  3214. *
  3215. */
  3216. var doubleToIEEE754String = function (v) {
  3217. util.assert(!isInvalidJSONNumber(v), 'Invalid JSON number'); // MJL
  3218. var ebits = 11, fbits = 52;
  3219. var bias = (1 << (ebits - 1)) - 1;
  3220. var s, e, f, ln, i;
  3221. // Compute sign, exponent, fraction
  3222. // Skip NaN / Infinity handling --MJL.
  3223. if (v === 0) {
  3224. e = 0;
  3225. f = 0;
  3226. s = 1 / v === -Infinity ? 1 : 0;
  3227. }
  3228. else {
  3229. s = v < 0;
  3230. v = Math.abs(v);
  3231. if (v >= Math.pow(2, 1 - bias)) {
  3232. // Normalized
  3233. ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
  3234. e = ln + bias;
  3235. f = Math.round(v * Math.pow(2, fbits - ln) - Math.pow(2, fbits));
  3236. }
  3237. else {
  3238. // Denormalized
  3239. e = 0;
  3240. f = Math.round(v / Math.pow(2, 1 - bias - fbits));
  3241. }
  3242. }
  3243. // Pack sign, exponent, fraction
  3244. var bits = [];
  3245. for (i = fbits; i; i -= 1) {
  3246. bits.push(f % 2 ? 1 : 0);
  3247. f = Math.floor(f / 2);
  3248. }
  3249. for (i = ebits; i; i -= 1) {
  3250. bits.push(e % 2 ? 1 : 0);
  3251. e = Math.floor(e / 2);
  3252. }
  3253. bits.push(s ? 1 : 0);
  3254. bits.reverse();
  3255. var str = bits.join('');
  3256. // Return the data as a hex string. --MJL
  3257. var hexByteString = '';
  3258. for (i = 0; i < 64; i += 8) {
  3259. var hexByte = parseInt(str.substr(i, 8), 2).toString(16);
  3260. if (hexByte.length === 1) {
  3261. hexByte = '0' + hexByte;
  3262. }
  3263. hexByteString = hexByteString + hexByte;
  3264. }
  3265. return hexByteString.toLowerCase();
  3266. };
  3267. /**
  3268. * Used to detect if we're in a Chrome content script (which executes in an
  3269. * isolated environment where long-polling doesn't work).
  3270. */
  3271. var isChromeExtensionContentScript = function () {
  3272. return !!(typeof window === 'object' &&
  3273. window['chrome'] &&
  3274. window['chrome']['extension'] &&
  3275. !/^chrome/.test(window.location.href));
  3276. };
  3277. /**
  3278. * Used to detect if we're in a Windows 8 Store app.
  3279. */
  3280. var isWindowsStoreApp = function () {
  3281. // Check for the presence of a couple WinRT globals
  3282. return typeof Windows === 'object' && typeof Windows.UI === 'object';
  3283. };
  3284. /**
  3285. * Converts a server error code to a Javascript Error
  3286. */
  3287. function errorForServerCode(code, query) {
  3288. var reason = 'Unknown Error';
  3289. if (code === 'too_big') {
  3290. reason =
  3291. 'The data requested exceeds the maximum size ' +
  3292. 'that can be accessed with a single request.';
  3293. }
  3294. else if (code === 'permission_denied') {
  3295. reason = "Client doesn't have permission to access the desired data.";
  3296. }
  3297. else if (code === 'unavailable') {
  3298. reason = 'The service is unavailable';
  3299. }
  3300. var error = new Error(code + ' at ' + query._path.toString() + ': ' + reason);
  3301. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3302. error.code = code.toUpperCase();
  3303. return error;
  3304. }
  3305. /**
  3306. * Used to test for integer-looking strings
  3307. */
  3308. var INTEGER_REGEXP_ = new RegExp('^-?(0*)\\d{1,10}$');
  3309. /**
  3310. * For use in keys, the minimum possible 32-bit integer.
  3311. */
  3312. var INTEGER_32_MIN = -2147483648;
  3313. /**
  3314. * For use in kyes, the maximum possible 32-bit integer.
  3315. */
  3316. var INTEGER_32_MAX = 2147483647;
  3317. /**
  3318. * If the string contains a 32-bit integer, return it. Else return null.
  3319. */
  3320. var tryParseInt = function (str) {
  3321. if (INTEGER_REGEXP_.test(str)) {
  3322. var intVal = Number(str);
  3323. if (intVal >= INTEGER_32_MIN && intVal <= INTEGER_32_MAX) {
  3324. return intVal;
  3325. }
  3326. }
  3327. return null;
  3328. };
  3329. /**
  3330. * Helper to run some code but catch any exceptions and re-throw them later.
  3331. * Useful for preventing user callbacks from breaking internal code.
  3332. *
  3333. * Re-throwing the exception from a setTimeout is a little evil, but it's very
  3334. * convenient (we don't have to try to figure out when is a safe point to
  3335. * re-throw it), and the behavior seems reasonable:
  3336. *
  3337. * * If you aren't pausing on exceptions, you get an error in the console with
  3338. * the correct stack trace.
  3339. * * If you're pausing on all exceptions, the debugger will pause on your
  3340. * exception and then again when we rethrow it.
  3341. * * If you're only pausing on uncaught exceptions, the debugger will only pause
  3342. * on us re-throwing it.
  3343. *
  3344. * @param fn - The code to guard.
  3345. */
  3346. var exceptionGuard = function (fn) {
  3347. try {
  3348. fn();
  3349. }
  3350. catch (e) {
  3351. // Re-throw exception when it's safe.
  3352. setTimeout(function () {
  3353. // It used to be that "throw e" would result in a good console error with
  3354. // relevant context, but as of Chrome 39, you just get the firebase.js
  3355. // file/line number where we re-throw it, which is useless. So we log
  3356. // e.stack explicitly.
  3357. var stack = e.stack || '';
  3358. warn$1('Exception was thrown by user callback.', stack);
  3359. throw e;
  3360. }, Math.floor(0));
  3361. }
  3362. };
  3363. /**
  3364. * @returns {boolean} true if we think we're currently being crawled.
  3365. */
  3366. var beingCrawled = function () {
  3367. var userAgent = (typeof window === 'object' &&
  3368. window['navigator'] &&
  3369. window['navigator']['userAgent']) ||
  3370. '';
  3371. // For now we whitelist the most popular crawlers. We should refine this to be the set of crawlers we
  3372. // believe to support JavaScript/AJAX rendering.
  3373. // NOTE: Google Webmaster Tools doesn't really belong, but their "This is how a visitor to your website
  3374. // would have seen the page" is flaky if we don't treat it as a crawler.
  3375. return (userAgent.search(/googlebot|google webmaster tools|bingbot|yahoo! slurp|baiduspider|yandexbot|duckduckbot/i) >= 0);
  3376. };
  3377. /**
  3378. * Same as setTimeout() except on Node.JS it will /not/ prevent the process from exiting.
  3379. *
  3380. * It is removed with clearTimeout() as normal.
  3381. *
  3382. * @param fn - Function to run.
  3383. * @param time - Milliseconds to wait before running.
  3384. * @returns The setTimeout() return value.
  3385. */
  3386. var setTimeoutNonBlocking = function (fn, time) {
  3387. var timeout = setTimeout(fn, time);
  3388. // Note: at the time of this comment, unrefTimer is under the unstable set of APIs. Run with --unstable to enable the API.
  3389. if (typeof timeout === 'number' &&
  3390. // @ts-ignore Is only defined in Deno environments.
  3391. typeof Deno !== 'undefined' &&
  3392. // @ts-ignore Deno and unrefTimer are only defined in Deno environments.
  3393. Deno['unrefTimer']) {
  3394. // @ts-ignore Deno and unrefTimer are only defined in Deno environments.
  3395. Deno.unrefTimer(timeout);
  3396. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3397. }
  3398. else if (typeof timeout === 'object' && timeout['unref']) {
  3399. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3400. timeout['unref']();
  3401. }
  3402. return timeout;
  3403. };
  3404. /**
  3405. * @license
  3406. * Copyright 2017 Google LLC
  3407. *
  3408. * Licensed under the Apache License, Version 2.0 (the "License");
  3409. * you may not use this file except in compliance with the License.
  3410. * You may obtain a copy of the License at
  3411. *
  3412. * http://www.apache.org/licenses/LICENSE-2.0
  3413. *
  3414. * Unless required by applicable law or agreed to in writing, software
  3415. * distributed under the License is distributed on an "AS IS" BASIS,
  3416. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3417. * See the License for the specific language governing permissions and
  3418. * limitations under the License.
  3419. */
  3420. /**
  3421. * A class that holds metadata about a Repo object
  3422. */
  3423. var RepoInfo = /** @class */ (function () {
  3424. /**
  3425. * @param host - Hostname portion of the url for the repo
  3426. * @param secure - Whether or not this repo is accessed over ssl
  3427. * @param namespace - The namespace represented by the repo
  3428. * @param webSocketOnly - Whether to prefer websockets over all other transports (used by Nest).
  3429. * @param nodeAdmin - Whether this instance uses Admin SDK credentials
  3430. * @param persistenceKey - Override the default session persistence storage key
  3431. */
  3432. function RepoInfo(host, secure, namespace, webSocketOnly, nodeAdmin, persistenceKey, includeNamespaceInQueryParams, isUsingEmulator) {
  3433. if (nodeAdmin === void 0) { nodeAdmin = false; }
  3434. if (persistenceKey === void 0) { persistenceKey = ''; }
  3435. if (includeNamespaceInQueryParams === void 0) { includeNamespaceInQueryParams = false; }
  3436. if (isUsingEmulator === void 0) { isUsingEmulator = false; }
  3437. this.secure = secure;
  3438. this.namespace = namespace;
  3439. this.webSocketOnly = webSocketOnly;
  3440. this.nodeAdmin = nodeAdmin;
  3441. this.persistenceKey = persistenceKey;
  3442. this.includeNamespaceInQueryParams = includeNamespaceInQueryParams;
  3443. this.isUsingEmulator = isUsingEmulator;
  3444. this._host = host.toLowerCase();
  3445. this._domain = this._host.substr(this._host.indexOf('.') + 1);
  3446. this.internalHost =
  3447. PersistentStorage.get('host:' + host) || this._host;
  3448. }
  3449. RepoInfo.prototype.isCacheableHost = function () {
  3450. return this.internalHost.substr(0, 2) === 's-';
  3451. };
  3452. RepoInfo.prototype.isCustomHost = function () {
  3453. return (this._domain !== 'firebaseio.com' &&
  3454. this._domain !== 'firebaseio-demo.com');
  3455. };
  3456. Object.defineProperty(RepoInfo.prototype, "host", {
  3457. get: function () {
  3458. return this._host;
  3459. },
  3460. set: function (newHost) {
  3461. if (newHost !== this.internalHost) {
  3462. this.internalHost = newHost;
  3463. if (this.isCacheableHost()) {
  3464. PersistentStorage.set('host:' + this._host, this.internalHost);
  3465. }
  3466. }
  3467. },
  3468. enumerable: false,
  3469. configurable: true
  3470. });
  3471. RepoInfo.prototype.toString = function () {
  3472. var str = this.toURLString();
  3473. if (this.persistenceKey) {
  3474. str += '<' + this.persistenceKey + '>';
  3475. }
  3476. return str;
  3477. };
  3478. RepoInfo.prototype.toURLString = function () {
  3479. var protocol = this.secure ? 'https://' : 'http://';
  3480. var query = this.includeNamespaceInQueryParams
  3481. ? "?ns=".concat(this.namespace)
  3482. : '';
  3483. return "".concat(protocol).concat(this.host, "/").concat(query);
  3484. };
  3485. return RepoInfo;
  3486. }());
  3487. function repoInfoNeedsQueryParam(repoInfo) {
  3488. return (repoInfo.host !== repoInfo.internalHost ||
  3489. repoInfo.isCustomHost() ||
  3490. repoInfo.includeNamespaceInQueryParams);
  3491. }
  3492. /**
  3493. * Returns the websocket URL for this repo
  3494. * @param repoInfo - RepoInfo object
  3495. * @param type - of connection
  3496. * @param params - list
  3497. * @returns The URL for this repo
  3498. */
  3499. function repoInfoConnectionURL(repoInfo, type, params) {
  3500. util.assert(typeof type === 'string', 'typeof type must == string');
  3501. util.assert(typeof params === 'object', 'typeof params must == object');
  3502. var connURL;
  3503. if (type === WEBSOCKET) {
  3504. connURL =
  3505. (repoInfo.secure ? 'wss://' : 'ws://') + repoInfo.internalHost + '/.ws?';
  3506. }
  3507. else if (type === LONG_POLLING) {
  3508. connURL =
  3509. (repoInfo.secure ? 'https://' : 'http://') +
  3510. repoInfo.internalHost +
  3511. '/.lp?';
  3512. }
  3513. else {
  3514. throw new Error('Unknown connection type: ' + type);
  3515. }
  3516. if (repoInfoNeedsQueryParam(repoInfo)) {
  3517. params['ns'] = repoInfo.namespace;
  3518. }
  3519. var pairs = [];
  3520. each(params, function (key, value) {
  3521. pairs.push(key + '=' + value);
  3522. });
  3523. return connURL + pairs.join('&');
  3524. }
  3525. /**
  3526. * @license
  3527. * Copyright 2017 Google LLC
  3528. *
  3529. * Licensed under the Apache License, Version 2.0 (the "License");
  3530. * you may not use this file except in compliance with the License.
  3531. * You may obtain a copy of the License at
  3532. *
  3533. * http://www.apache.org/licenses/LICENSE-2.0
  3534. *
  3535. * Unless required by applicable law or agreed to in writing, software
  3536. * distributed under the License is distributed on an "AS IS" BASIS,
  3537. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3538. * See the License for the specific language governing permissions and
  3539. * limitations under the License.
  3540. */
  3541. /**
  3542. * Tracks a collection of stats.
  3543. */
  3544. var StatsCollection = /** @class */ (function () {
  3545. function StatsCollection() {
  3546. this.counters_ = {};
  3547. }
  3548. StatsCollection.prototype.incrementCounter = function (name, amount) {
  3549. if (amount === void 0) { amount = 1; }
  3550. if (!util.contains(this.counters_, name)) {
  3551. this.counters_[name] = 0;
  3552. }
  3553. this.counters_[name] += amount;
  3554. };
  3555. StatsCollection.prototype.get = function () {
  3556. return util.deepCopy(this.counters_);
  3557. };
  3558. return StatsCollection;
  3559. }());
  3560. /**
  3561. * @license
  3562. * Copyright 2017 Google LLC
  3563. *
  3564. * Licensed under the Apache License, Version 2.0 (the "License");
  3565. * you may not use this file except in compliance with the License.
  3566. * You may obtain a copy of the License at
  3567. *
  3568. * http://www.apache.org/licenses/LICENSE-2.0
  3569. *
  3570. * Unless required by applicable law or agreed to in writing, software
  3571. * distributed under the License is distributed on an "AS IS" BASIS,
  3572. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3573. * See the License for the specific language governing permissions and
  3574. * limitations under the License.
  3575. */
  3576. var collections = {};
  3577. var reporters = {};
  3578. function statsManagerGetCollection(repoInfo) {
  3579. var hashString = repoInfo.toString();
  3580. if (!collections[hashString]) {
  3581. collections[hashString] = new StatsCollection();
  3582. }
  3583. return collections[hashString];
  3584. }
  3585. function statsManagerGetOrCreateReporter(repoInfo, creatorFunction) {
  3586. var hashString = repoInfo.toString();
  3587. if (!reporters[hashString]) {
  3588. reporters[hashString] = creatorFunction();
  3589. }
  3590. return reporters[hashString];
  3591. }
  3592. /**
  3593. * @license
  3594. * Copyright 2019 Google LLC
  3595. *
  3596. * Licensed under the Apache License, Version 2.0 (the "License");
  3597. * you may not use this file except in compliance with the License.
  3598. * You may obtain a copy of the License at
  3599. *
  3600. * http://www.apache.org/licenses/LICENSE-2.0
  3601. *
  3602. * Unless required by applicable law or agreed to in writing, software
  3603. * distributed under the License is distributed on an "AS IS" BASIS,
  3604. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3605. * See the License for the specific language governing permissions and
  3606. * limitations under the License.
  3607. */
  3608. /** The semver (www.semver.org) version of the SDK. */
  3609. var SDK_VERSION = '';
  3610. /**
  3611. * SDK_VERSION should be set before any database instance is created
  3612. * @internal
  3613. */
  3614. function setSDKVersion(version) {
  3615. SDK_VERSION = version;
  3616. }
  3617. /**
  3618. * @license
  3619. * Copyright 2017 Google LLC
  3620. *
  3621. * Licensed under the Apache License, Version 2.0 (the "License");
  3622. * you may not use this file except in compliance with the License.
  3623. * You may obtain a copy of the License at
  3624. *
  3625. * http://www.apache.org/licenses/LICENSE-2.0
  3626. *
  3627. * Unless required by applicable law or agreed to in writing, software
  3628. * distributed under the License is distributed on an "AS IS" BASIS,
  3629. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3630. * See the License for the specific language governing permissions and
  3631. * limitations under the License.
  3632. */
  3633. var WEBSOCKET_MAX_FRAME_SIZE = 16384;
  3634. var WEBSOCKET_KEEPALIVE_INTERVAL = 45000;
  3635. var WebSocketImpl = null;
  3636. if (typeof MozWebSocket !== 'undefined') {
  3637. WebSocketImpl = MozWebSocket;
  3638. }
  3639. else if (typeof WebSocket !== 'undefined') {
  3640. WebSocketImpl = WebSocket;
  3641. }
  3642. function setWebSocketImpl(impl) {
  3643. WebSocketImpl = impl;
  3644. }
  3645. /**
  3646. * Create a new websocket connection with the given callbacks.
  3647. */
  3648. var WebSocketConnection = /** @class */ (function () {
  3649. /**
  3650. * @param connId identifier for this transport
  3651. * @param repoInfo The info for the websocket endpoint.
  3652. * @param applicationId The Firebase App ID for this project.
  3653. * @param appCheckToken The App Check Token for this client.
  3654. * @param authToken The Auth Token for this client.
  3655. * @param transportSessionId Optional transportSessionId if this is connecting
  3656. * to an existing transport session
  3657. * @param lastSessionId Optional lastSessionId if there was a previous
  3658. * connection
  3659. */
  3660. function WebSocketConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
  3661. this.connId = connId;
  3662. this.applicationId = applicationId;
  3663. this.appCheckToken = appCheckToken;
  3664. this.authToken = authToken;
  3665. this.keepaliveTimer = null;
  3666. this.frames = null;
  3667. this.totalFrames = 0;
  3668. this.bytesSent = 0;
  3669. this.bytesReceived = 0;
  3670. this.log_ = logWrapper(this.connId);
  3671. this.stats_ = statsManagerGetCollection(repoInfo);
  3672. this.connURL = WebSocketConnection.connectionURL_(repoInfo, transportSessionId, lastSessionId, appCheckToken, applicationId);
  3673. this.nodeAdmin = repoInfo.nodeAdmin;
  3674. }
  3675. /**
  3676. * @param repoInfo - The info for the websocket endpoint.
  3677. * @param transportSessionId - Optional transportSessionId if this is connecting to an existing transport
  3678. * session
  3679. * @param lastSessionId - Optional lastSessionId if there was a previous connection
  3680. * @returns connection url
  3681. */
  3682. WebSocketConnection.connectionURL_ = function (repoInfo, transportSessionId, lastSessionId, appCheckToken, applicationId) {
  3683. var urlParams = {};
  3684. urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
  3685. if (!util.isNodeSdk() &&
  3686. typeof location !== 'undefined' &&
  3687. location.hostname &&
  3688. FORGE_DOMAIN_RE.test(location.hostname)) {
  3689. urlParams[REFERER_PARAM] = FORGE_REF;
  3690. }
  3691. if (transportSessionId) {
  3692. urlParams[TRANSPORT_SESSION_PARAM] = transportSessionId;
  3693. }
  3694. if (lastSessionId) {
  3695. urlParams[LAST_SESSION_PARAM] = lastSessionId;
  3696. }
  3697. if (appCheckToken) {
  3698. urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
  3699. }
  3700. if (applicationId) {
  3701. urlParams[APPLICATION_ID_PARAM] = applicationId;
  3702. }
  3703. return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
  3704. };
  3705. /**
  3706. * @param onMessage - Callback when messages arrive
  3707. * @param onDisconnect - Callback with connection lost.
  3708. */
  3709. WebSocketConnection.prototype.open = function (onMessage, onDisconnect) {
  3710. var _this = this;
  3711. this.onDisconnect = onDisconnect;
  3712. this.onMessage = onMessage;
  3713. this.log_('Websocket connecting to ' + this.connURL);
  3714. this.everConnected_ = false;
  3715. // Assume failure until proven otherwise.
  3716. PersistentStorage.set('previous_websocket_failure', true);
  3717. try {
  3718. var options = void 0;
  3719. if (util.isNodeSdk()) {
  3720. var device = this.nodeAdmin ? 'AdminNode' : 'Node';
  3721. // UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
  3722. options = {
  3723. headers: {
  3724. 'User-Agent': "Firebase/".concat(PROTOCOL_VERSION, "/").concat(SDK_VERSION, "/").concat(process.platform, "/").concat(device),
  3725. 'X-Firebase-GMPID': this.applicationId || ''
  3726. }
  3727. };
  3728. // If using Node with admin creds, AppCheck-related checks are unnecessary.
  3729. // Note that we send the credentials here even if they aren't admin credentials, which is
  3730. // not a problem.
  3731. // Note that this header is just used to bypass appcheck, and the token should still be sent
  3732. // through the websocket connection once it is established.
  3733. if (this.authToken) {
  3734. options.headers['Authorization'] = "Bearer ".concat(this.authToken);
  3735. }
  3736. if (this.appCheckToken) {
  3737. options.headers['X-Firebase-AppCheck'] = this.appCheckToken;
  3738. }
  3739. // Plumb appropriate http_proxy environment variable into faye-websocket if it exists.
  3740. var env = process['env'];
  3741. var proxy = this.connURL.indexOf('wss://') === 0
  3742. ? env['HTTPS_PROXY'] || env['https_proxy']
  3743. : env['HTTP_PROXY'] || env['http_proxy'];
  3744. if (proxy) {
  3745. options['proxy'] = { origin: proxy };
  3746. }
  3747. }
  3748. this.mySock = new WebSocketImpl(this.connURL, [], options);
  3749. }
  3750. catch (e) {
  3751. this.log_('Error instantiating WebSocket.');
  3752. var error = e.message || e.data;
  3753. if (error) {
  3754. this.log_(error);
  3755. }
  3756. this.onClosed_();
  3757. return;
  3758. }
  3759. this.mySock.onopen = function () {
  3760. _this.log_('Websocket connected.');
  3761. _this.everConnected_ = true;
  3762. };
  3763. this.mySock.onclose = function () {
  3764. _this.log_('Websocket connection was disconnected.');
  3765. _this.mySock = null;
  3766. _this.onClosed_();
  3767. };
  3768. this.mySock.onmessage = function (m) {
  3769. _this.handleIncomingFrame(m);
  3770. };
  3771. this.mySock.onerror = function (e) {
  3772. _this.log_('WebSocket error. Closing connection.');
  3773. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3774. var error = e.message || e.data;
  3775. if (error) {
  3776. _this.log_(error);
  3777. }
  3778. _this.onClosed_();
  3779. };
  3780. };
  3781. /**
  3782. * No-op for websockets, we don't need to do anything once the connection is confirmed as open
  3783. */
  3784. WebSocketConnection.prototype.start = function () { };
  3785. WebSocketConnection.forceDisallow = function () {
  3786. WebSocketConnection.forceDisallow_ = true;
  3787. };
  3788. WebSocketConnection.isAvailable = function () {
  3789. var isOldAndroid = false;
  3790. if (typeof navigator !== 'undefined' && navigator.userAgent) {
  3791. var oldAndroidRegex = /Android ([0-9]{0,}\.[0-9]{0,})/;
  3792. var oldAndroidMatch = navigator.userAgent.match(oldAndroidRegex);
  3793. if (oldAndroidMatch && oldAndroidMatch.length > 1) {
  3794. if (parseFloat(oldAndroidMatch[1]) < 4.4) {
  3795. isOldAndroid = true;
  3796. }
  3797. }
  3798. }
  3799. return (!isOldAndroid &&
  3800. WebSocketImpl !== null &&
  3801. !WebSocketConnection.forceDisallow_);
  3802. };
  3803. /**
  3804. * Returns true if we previously failed to connect with this transport.
  3805. */
  3806. WebSocketConnection.previouslyFailed = function () {
  3807. // If our persistent storage is actually only in-memory storage,
  3808. // we default to assuming that it previously failed to be safe.
  3809. return (PersistentStorage.isInMemoryStorage ||
  3810. PersistentStorage.get('previous_websocket_failure') === true);
  3811. };
  3812. WebSocketConnection.prototype.markConnectionHealthy = function () {
  3813. PersistentStorage.remove('previous_websocket_failure');
  3814. };
  3815. WebSocketConnection.prototype.appendFrame_ = function (data) {
  3816. this.frames.push(data);
  3817. if (this.frames.length === this.totalFrames) {
  3818. var fullMess = this.frames.join('');
  3819. this.frames = null;
  3820. var jsonMess = util.jsonEval(fullMess);
  3821. //handle the message
  3822. this.onMessage(jsonMess);
  3823. }
  3824. };
  3825. /**
  3826. * @param frameCount - The number of frames we are expecting from the server
  3827. */
  3828. WebSocketConnection.prototype.handleNewFrameCount_ = function (frameCount) {
  3829. this.totalFrames = frameCount;
  3830. this.frames = [];
  3831. };
  3832. /**
  3833. * Attempts to parse a frame count out of some text. If it can't, assumes a value of 1
  3834. * @returns Any remaining data to be process, or null if there is none
  3835. */
  3836. WebSocketConnection.prototype.extractFrameCount_ = function (data) {
  3837. util.assert(this.frames === null, 'We already have a frame buffer');
  3838. // TODO: The server is only supposed to send up to 9999 frames (i.e. length <= 4), but that isn't being enforced
  3839. // currently. So allowing larger frame counts (length <= 6). See https://app.asana.com/0/search/8688598998380/8237608042508
  3840. if (data.length <= 6) {
  3841. var frameCount = Number(data);
  3842. if (!isNaN(frameCount)) {
  3843. this.handleNewFrameCount_(frameCount);
  3844. return null;
  3845. }
  3846. }
  3847. this.handleNewFrameCount_(1);
  3848. return data;
  3849. };
  3850. /**
  3851. * Process a websocket frame that has arrived from the server.
  3852. * @param mess - The frame data
  3853. */
  3854. WebSocketConnection.prototype.handleIncomingFrame = function (mess) {
  3855. if (this.mySock === null) {
  3856. return; // Chrome apparently delivers incoming packets even after we .close() the connection sometimes.
  3857. }
  3858. var data = mess['data'];
  3859. this.bytesReceived += data.length;
  3860. this.stats_.incrementCounter('bytes_received', data.length);
  3861. this.resetKeepAlive();
  3862. if (this.frames !== null) {
  3863. // we're buffering
  3864. this.appendFrame_(data);
  3865. }
  3866. else {
  3867. // try to parse out a frame count, otherwise, assume 1 and process it
  3868. var remainingData = this.extractFrameCount_(data);
  3869. if (remainingData !== null) {
  3870. this.appendFrame_(remainingData);
  3871. }
  3872. }
  3873. };
  3874. /**
  3875. * Send a message to the server
  3876. * @param data - The JSON object to transmit
  3877. */
  3878. WebSocketConnection.prototype.send = function (data) {
  3879. this.resetKeepAlive();
  3880. var dataStr = util.stringify(data);
  3881. this.bytesSent += dataStr.length;
  3882. this.stats_.incrementCounter('bytes_sent', dataStr.length);
  3883. //We can only fit a certain amount in each websocket frame, so we need to split this request
  3884. //up into multiple pieces if it doesn't fit in one request.
  3885. var dataSegs = splitStringBySize(dataStr, WEBSOCKET_MAX_FRAME_SIZE);
  3886. //Send the length header
  3887. if (dataSegs.length > 1) {
  3888. this.sendString_(String(dataSegs.length));
  3889. }
  3890. //Send the actual data in segments.
  3891. for (var i = 0; i < dataSegs.length; i++) {
  3892. this.sendString_(dataSegs[i]);
  3893. }
  3894. };
  3895. WebSocketConnection.prototype.shutdown_ = function () {
  3896. this.isClosed_ = true;
  3897. if (this.keepaliveTimer) {
  3898. clearInterval(this.keepaliveTimer);
  3899. this.keepaliveTimer = null;
  3900. }
  3901. if (this.mySock) {
  3902. this.mySock.close();
  3903. this.mySock = null;
  3904. }
  3905. };
  3906. WebSocketConnection.prototype.onClosed_ = function () {
  3907. if (!this.isClosed_) {
  3908. this.log_('WebSocket is closing itself');
  3909. this.shutdown_();
  3910. // since this is an internal close, trigger the close listener
  3911. if (this.onDisconnect) {
  3912. this.onDisconnect(this.everConnected_);
  3913. this.onDisconnect = null;
  3914. }
  3915. }
  3916. };
  3917. /**
  3918. * External-facing close handler.
  3919. * Close the websocket and kill the connection.
  3920. */
  3921. WebSocketConnection.prototype.close = function () {
  3922. if (!this.isClosed_) {
  3923. this.log_('WebSocket is being closed');
  3924. this.shutdown_();
  3925. }
  3926. };
  3927. /**
  3928. * Kill the current keepalive timer and start a new one, to ensure that it always fires N seconds after
  3929. * the last activity.
  3930. */
  3931. WebSocketConnection.prototype.resetKeepAlive = function () {
  3932. var _this = this;
  3933. clearInterval(this.keepaliveTimer);
  3934. this.keepaliveTimer = setInterval(function () {
  3935. //If there has been no websocket activity for a while, send a no-op
  3936. if (_this.mySock) {
  3937. _this.sendString_('0');
  3938. }
  3939. _this.resetKeepAlive();
  3940. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3941. }, Math.floor(WEBSOCKET_KEEPALIVE_INTERVAL));
  3942. };
  3943. /**
  3944. * Send a string over the websocket.
  3945. *
  3946. * @param str - String to send.
  3947. */
  3948. WebSocketConnection.prototype.sendString_ = function (str) {
  3949. // Firefox seems to sometimes throw exceptions (NS_ERROR_UNEXPECTED) from websocket .send()
  3950. // calls for some unknown reason. We treat these as an error and disconnect.
  3951. // See https://app.asana.com/0/58926111402292/68021340250410
  3952. try {
  3953. this.mySock.send(str);
  3954. }
  3955. catch (e) {
  3956. this.log_('Exception thrown from WebSocket.send():', e.message || e.data, 'Closing connection.');
  3957. setTimeout(this.onClosed_.bind(this), 0);
  3958. }
  3959. };
  3960. /**
  3961. * Number of response before we consider the connection "healthy."
  3962. */
  3963. WebSocketConnection.responsesRequiredToBeHealthy = 2;
  3964. /**
  3965. * Time to wait for the connection te become healthy before giving up.
  3966. */
  3967. WebSocketConnection.healthyTimeout = 30000;
  3968. return WebSocketConnection;
  3969. }());
  3970. /**
  3971. * @license
  3972. * Copyright 2021 Google LLC
  3973. *
  3974. * Licensed under the Apache License, Version 2.0 (the "License");
  3975. * you may not use this file except in compliance with the License.
  3976. * You may obtain a copy of the License at
  3977. *
  3978. * http://www.apache.org/licenses/LICENSE-2.0
  3979. *
  3980. * Unless required by applicable law or agreed to in writing, software
  3981. * distributed under the License is distributed on an "AS IS" BASIS,
  3982. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3983. * See the License for the specific language governing permissions and
  3984. * limitations under the License.
  3985. */
  3986. /**
  3987. * Abstraction around AppCheck's token fetching capabilities.
  3988. */
  3989. var AppCheckTokenProvider = /** @class */ (function () {
  3990. function AppCheckTokenProvider(appName_, appCheckProvider) {
  3991. var _this = this;
  3992. this.appName_ = appName_;
  3993. this.appCheckProvider = appCheckProvider;
  3994. this.appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
  3995. if (!this.appCheck) {
  3996. appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); });
  3997. }
  3998. }
  3999. AppCheckTokenProvider.prototype.getToken = function (forceRefresh) {
  4000. var _this = this;
  4001. if (!this.appCheck) {
  4002. return new Promise(function (resolve, reject) {
  4003. // Support delayed initialization of FirebaseAppCheck. This allows our
  4004. // customers to initialize the RTDB SDK before initializing Firebase
  4005. // AppCheck and ensures that all requests are authenticated if a token
  4006. // becomes available before the timoeout below expires.
  4007. setTimeout(function () {
  4008. if (_this.appCheck) {
  4009. _this.getToken(forceRefresh).then(resolve, reject);
  4010. }
  4011. else {
  4012. resolve(null);
  4013. }
  4014. }, 0);
  4015. });
  4016. }
  4017. return this.appCheck.getToken(forceRefresh);
  4018. };
  4019. AppCheckTokenProvider.prototype.addTokenChangeListener = function (listener) {
  4020. var _a;
  4021. (_a = this.appCheckProvider) === null || _a === void 0 ? void 0 : _a.get().then(function (appCheck) { return appCheck.addTokenListener(listener); });
  4022. };
  4023. AppCheckTokenProvider.prototype.notifyForInvalidToken = function () {
  4024. warn$1("Provided AppCheck credentials for the app named \"".concat(this.appName_, "\" ") +
  4025. 'are invalid. This usually indicates your app was not initialized correctly.');
  4026. };
  4027. return AppCheckTokenProvider;
  4028. }());
  4029. /**
  4030. * @license
  4031. * Copyright 2017 Google LLC
  4032. *
  4033. * Licensed under the Apache License, Version 2.0 (the "License");
  4034. * you may not use this file except in compliance with the License.
  4035. * You may obtain a copy of the License at
  4036. *
  4037. * http://www.apache.org/licenses/LICENSE-2.0
  4038. *
  4039. * Unless required by applicable law or agreed to in writing, software
  4040. * distributed under the License is distributed on an "AS IS" BASIS,
  4041. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4042. * See the License for the specific language governing permissions and
  4043. * limitations under the License.
  4044. */
  4045. /**
  4046. * Abstraction around FirebaseApp's token fetching capabilities.
  4047. */
  4048. var FirebaseAuthTokenProvider = /** @class */ (function () {
  4049. function FirebaseAuthTokenProvider(appName_, firebaseOptions_, authProvider_) {
  4050. var _this = this;
  4051. this.appName_ = appName_;
  4052. this.firebaseOptions_ = firebaseOptions_;
  4053. this.authProvider_ = authProvider_;
  4054. this.auth_ = null;
  4055. this.auth_ = authProvider_.getImmediate({ optional: true });
  4056. if (!this.auth_) {
  4057. authProvider_.onInit(function (auth) { return (_this.auth_ = auth); });
  4058. }
  4059. }
  4060. FirebaseAuthTokenProvider.prototype.getToken = function (forceRefresh) {
  4061. var _this = this;
  4062. if (!this.auth_) {
  4063. return new Promise(function (resolve, reject) {
  4064. // Support delayed initialization of FirebaseAuth. This allows our
  4065. // customers to initialize the RTDB SDK before initializing Firebase
  4066. // Auth and ensures that all requests are authenticated if a token
  4067. // becomes available before the timoeout below expires.
  4068. setTimeout(function () {
  4069. if (_this.auth_) {
  4070. _this.getToken(forceRefresh).then(resolve, reject);
  4071. }
  4072. else {
  4073. resolve(null);
  4074. }
  4075. }, 0);
  4076. });
  4077. }
  4078. return this.auth_.getToken(forceRefresh).catch(function (error) {
  4079. // TODO: Need to figure out all the cases this is raised and whether
  4080. // this makes sense.
  4081. if (error && error.code === 'auth/token-not-initialized') {
  4082. log('Got auth/token-not-initialized error. Treating as null token.');
  4083. return null;
  4084. }
  4085. else {
  4086. return Promise.reject(error);
  4087. }
  4088. });
  4089. };
  4090. FirebaseAuthTokenProvider.prototype.addTokenChangeListener = function (listener) {
  4091. // TODO: We might want to wrap the listener and call it with no args to
  4092. // avoid a leaky abstraction, but that makes removing the listener harder.
  4093. if (this.auth_) {
  4094. this.auth_.addAuthTokenListener(listener);
  4095. }
  4096. else {
  4097. this.authProvider_
  4098. .get()
  4099. .then(function (auth) { return auth.addAuthTokenListener(listener); });
  4100. }
  4101. };
  4102. FirebaseAuthTokenProvider.prototype.removeTokenChangeListener = function (listener) {
  4103. this.authProvider_
  4104. .get()
  4105. .then(function (auth) { return auth.removeAuthTokenListener(listener); });
  4106. };
  4107. FirebaseAuthTokenProvider.prototype.notifyForInvalidToken = function () {
  4108. var errorMessage = 'Provided authentication credentials for the app named "' +
  4109. this.appName_ +
  4110. '" are invalid. This usually indicates your app was not ' +
  4111. 'initialized correctly. ';
  4112. if ('credential' in this.firebaseOptions_) {
  4113. errorMessage +=
  4114. 'Make sure the "credential" property provided to initializeApp() ' +
  4115. 'is authorized to access the specified "databaseURL" and is from the correct ' +
  4116. 'project.';
  4117. }
  4118. else if ('serviceAccount' in this.firebaseOptions_) {
  4119. errorMessage +=
  4120. 'Make sure the "serviceAccount" property provided to initializeApp() ' +
  4121. 'is authorized to access the specified "databaseURL" and is from the correct ' +
  4122. 'project.';
  4123. }
  4124. else {
  4125. errorMessage +=
  4126. 'Make sure the "apiKey" and "databaseURL" properties provided to ' +
  4127. 'initializeApp() match the values provided for your app at ' +
  4128. 'https://console.firebase.google.com/.';
  4129. }
  4130. warn$1(errorMessage);
  4131. };
  4132. return FirebaseAuthTokenProvider;
  4133. }());
  4134. /* AuthTokenProvider that supplies a constant token. Used by Admin SDK or mockUserToken with emulators. */
  4135. var EmulatorTokenProvider = /** @class */ (function () {
  4136. function EmulatorTokenProvider(accessToken) {
  4137. this.accessToken = accessToken;
  4138. }
  4139. EmulatorTokenProvider.prototype.getToken = function (forceRefresh) {
  4140. return Promise.resolve({
  4141. accessToken: this.accessToken
  4142. });
  4143. };
  4144. EmulatorTokenProvider.prototype.addTokenChangeListener = function (listener) {
  4145. // Invoke the listener immediately to match the behavior in Firebase Auth
  4146. // (see packages/auth/src/auth.js#L1807)
  4147. listener(this.accessToken);
  4148. };
  4149. EmulatorTokenProvider.prototype.removeTokenChangeListener = function (listener) { };
  4150. EmulatorTokenProvider.prototype.notifyForInvalidToken = function () { };
  4151. /** A string that is treated as an admin access token by the RTDB emulator. Used by Admin SDK. */
  4152. EmulatorTokenProvider.OWNER = 'owner';
  4153. return EmulatorTokenProvider;
  4154. }());
  4155. /**
  4156. * @license
  4157. * Copyright 2017 Google LLC
  4158. *
  4159. * Licensed under the Apache License, Version 2.0 (the "License");
  4160. * you may not use this file except in compliance with the License.
  4161. * You may obtain a copy of the License at
  4162. *
  4163. * http://www.apache.org/licenses/LICENSE-2.0
  4164. *
  4165. * Unless required by applicable law or agreed to in writing, software
  4166. * distributed under the License is distributed on an "AS IS" BASIS,
  4167. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4168. * See the License for the specific language governing permissions and
  4169. * limitations under the License.
  4170. */
  4171. /**
  4172. * This class ensures the packets from the server arrive in order
  4173. * This class takes data from the server and ensures it gets passed into the callbacks in order.
  4174. */
  4175. var PacketReceiver = /** @class */ (function () {
  4176. /**
  4177. * @param onMessage_
  4178. */
  4179. function PacketReceiver(onMessage_) {
  4180. this.onMessage_ = onMessage_;
  4181. this.pendingResponses = [];
  4182. this.currentResponseNum = 0;
  4183. this.closeAfterResponse = -1;
  4184. this.onClose = null;
  4185. }
  4186. PacketReceiver.prototype.closeAfter = function (responseNum, callback) {
  4187. this.closeAfterResponse = responseNum;
  4188. this.onClose = callback;
  4189. if (this.closeAfterResponse < this.currentResponseNum) {
  4190. this.onClose();
  4191. this.onClose = null;
  4192. }
  4193. };
  4194. /**
  4195. * Each message from the server comes with a response number, and an array of data. The responseNumber
  4196. * allows us to ensure that we process them in the right order, since we can't be guaranteed that all
  4197. * browsers will respond in the same order as the requests we sent
  4198. */
  4199. PacketReceiver.prototype.handleResponse = function (requestNum, data) {
  4200. var _this = this;
  4201. this.pendingResponses[requestNum] = data;
  4202. var _loop_1 = function () {
  4203. var toProcess = this_1.pendingResponses[this_1.currentResponseNum];
  4204. delete this_1.pendingResponses[this_1.currentResponseNum];
  4205. var _loop_2 = function (i) {
  4206. if (toProcess[i]) {
  4207. exceptionGuard(function () {
  4208. _this.onMessage_(toProcess[i]);
  4209. });
  4210. }
  4211. };
  4212. for (var i = 0; i < toProcess.length; ++i) {
  4213. _loop_2(i);
  4214. }
  4215. if (this_1.currentResponseNum === this_1.closeAfterResponse) {
  4216. if (this_1.onClose) {
  4217. this_1.onClose();
  4218. this_1.onClose = null;
  4219. }
  4220. return "break";
  4221. }
  4222. this_1.currentResponseNum++;
  4223. };
  4224. var this_1 = this;
  4225. while (this.pendingResponses[this.currentResponseNum]) {
  4226. var state_1 = _loop_1();
  4227. if (state_1 === "break")
  4228. break;
  4229. }
  4230. };
  4231. return PacketReceiver;
  4232. }());
  4233. /**
  4234. * @license
  4235. * Copyright 2017 Google LLC
  4236. *
  4237. * Licensed under the Apache License, Version 2.0 (the "License");
  4238. * you may not use this file except in compliance with the License.
  4239. * You may obtain a copy of the License at
  4240. *
  4241. * http://www.apache.org/licenses/LICENSE-2.0
  4242. *
  4243. * Unless required by applicable law or agreed to in writing, software
  4244. * distributed under the License is distributed on an "AS IS" BASIS,
  4245. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4246. * See the License for the specific language governing permissions and
  4247. * limitations under the License.
  4248. */
  4249. // URL query parameters associated with longpolling
  4250. var FIREBASE_LONGPOLL_START_PARAM = 'start';
  4251. var FIREBASE_LONGPOLL_CLOSE_COMMAND = 'close';
  4252. var FIREBASE_LONGPOLL_COMMAND_CB_NAME = 'pLPCommand';
  4253. var FIREBASE_LONGPOLL_DATA_CB_NAME = 'pRTLPCB';
  4254. var FIREBASE_LONGPOLL_ID_PARAM = 'id';
  4255. var FIREBASE_LONGPOLL_PW_PARAM = 'pw';
  4256. var FIREBASE_LONGPOLL_SERIAL_PARAM = 'ser';
  4257. var FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = 'cb';
  4258. var FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = 'seg';
  4259. var FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = 'ts';
  4260. var FIREBASE_LONGPOLL_DATA_PARAM = 'd';
  4261. var FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = 'dframe';
  4262. //Data size constants.
  4263. //TODO: Perf: the maximum length actually differs from browser to browser.
  4264. // We should check what browser we're on and set accordingly.
  4265. var MAX_URL_DATA_SIZE = 1870;
  4266. var SEG_HEADER_SIZE = 30; //ie: &seg=8299234&ts=982389123&d=
  4267. var MAX_PAYLOAD_SIZE = MAX_URL_DATA_SIZE - SEG_HEADER_SIZE;
  4268. /**
  4269. * Keepalive period
  4270. * send a fresh request at minimum every 25 seconds. Opera has a maximum request
  4271. * length of 30 seconds that we can't exceed.
  4272. */
  4273. var KEEPALIVE_REQUEST_INTERVAL = 25000;
  4274. /**
  4275. * How long to wait before aborting a long-polling connection attempt.
  4276. */
  4277. var LP_CONNECT_TIMEOUT = 30000;
  4278. /**
  4279. * This class manages a single long-polling connection.
  4280. */
  4281. var BrowserPollConnection = /** @class */ (function () {
  4282. /**
  4283. * @param connId An identifier for this connection, used for logging
  4284. * @param repoInfo The info for the endpoint to send data to.
  4285. * @param applicationId The Firebase App ID for this project.
  4286. * @param appCheckToken The AppCheck token for this client.
  4287. * @param authToken The AuthToken to use for this connection.
  4288. * @param transportSessionId Optional transportSessionid if we are
  4289. * reconnecting for an existing transport session
  4290. * @param lastSessionId Optional lastSessionId if the PersistentConnection has
  4291. * already created a connection previously
  4292. */
  4293. function BrowserPollConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
  4294. var _this = this;
  4295. this.connId = connId;
  4296. this.repoInfo = repoInfo;
  4297. this.applicationId = applicationId;
  4298. this.appCheckToken = appCheckToken;
  4299. this.authToken = authToken;
  4300. this.transportSessionId = transportSessionId;
  4301. this.lastSessionId = lastSessionId;
  4302. this.bytesSent = 0;
  4303. this.bytesReceived = 0;
  4304. this.everConnected_ = false;
  4305. this.log_ = logWrapper(connId);
  4306. this.stats_ = statsManagerGetCollection(repoInfo);
  4307. this.urlFn = function (params) {
  4308. // Always add the token if we have one.
  4309. if (_this.appCheckToken) {
  4310. params[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
  4311. }
  4312. return repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
  4313. };
  4314. }
  4315. /**
  4316. * @param onMessage - Callback when messages arrive
  4317. * @param onDisconnect - Callback with connection lost.
  4318. */
  4319. BrowserPollConnection.prototype.open = function (onMessage, onDisconnect) {
  4320. var _this = this;
  4321. this.curSegmentNum = 0;
  4322. this.onDisconnect_ = onDisconnect;
  4323. this.myPacketOrderer = new PacketReceiver(onMessage);
  4324. this.isClosed_ = false;
  4325. this.connectTimeoutTimer_ = setTimeout(function () {
  4326. _this.log_('Timed out trying to connect.');
  4327. // Make sure we clear the host cache
  4328. _this.onClosed_();
  4329. _this.connectTimeoutTimer_ = null;
  4330. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4331. }, Math.floor(LP_CONNECT_TIMEOUT));
  4332. // Ensure we delay the creation of the iframe until the DOM is loaded.
  4333. executeWhenDOMReady(function () {
  4334. if (_this.isClosed_) {
  4335. return;
  4336. }
  4337. //Set up a callback that gets triggered once a connection is set up.
  4338. _this.scriptTagHolder = new FirebaseIFrameScriptHolder(function () {
  4339. var args = [];
  4340. for (var _i = 0; _i < arguments.length; _i++) {
  4341. args[_i] = arguments[_i];
  4342. }
  4343. var _a = tslib.__read(args, 5), command = _a[0], arg1 = _a[1], arg2 = _a[2]; _a[3]; _a[4];
  4344. _this.incrementIncomingBytes_(args);
  4345. if (!_this.scriptTagHolder) {
  4346. return; // we closed the connection.
  4347. }
  4348. if (_this.connectTimeoutTimer_) {
  4349. clearTimeout(_this.connectTimeoutTimer_);
  4350. _this.connectTimeoutTimer_ = null;
  4351. }
  4352. _this.everConnected_ = true;
  4353. if (command === FIREBASE_LONGPOLL_START_PARAM) {
  4354. _this.id = arg1;
  4355. _this.password = arg2;
  4356. }
  4357. else if (command === FIREBASE_LONGPOLL_CLOSE_COMMAND) {
  4358. // Don't clear the host cache. We got a response from the server, so we know it's reachable
  4359. if (arg1) {
  4360. // We aren't expecting any more data (other than what the server's already in the process of sending us
  4361. // through our already open polls), so don't send any more.
  4362. _this.scriptTagHolder.sendNewPolls = false;
  4363. // arg1 in this case is the last response number sent by the server. We should try to receive
  4364. // all of the responses up to this one before closing
  4365. _this.myPacketOrderer.closeAfter(arg1, function () {
  4366. _this.onClosed_();
  4367. });
  4368. }
  4369. else {
  4370. _this.onClosed_();
  4371. }
  4372. }
  4373. else {
  4374. throw new Error('Unrecognized command received: ' + command);
  4375. }
  4376. }, function () {
  4377. var args = [];
  4378. for (var _i = 0; _i < arguments.length; _i++) {
  4379. args[_i] = arguments[_i];
  4380. }
  4381. var _a = tslib.__read(args, 2), pN = _a[0], data = _a[1];
  4382. _this.incrementIncomingBytes_(args);
  4383. _this.myPacketOrderer.handleResponse(pN, data);
  4384. }, function () {
  4385. _this.onClosed_();
  4386. }, _this.urlFn);
  4387. //Send the initial request to connect. The serial number is simply to keep the browser from pulling previous results
  4388. //from cache.
  4389. var urlParams = {};
  4390. urlParams[FIREBASE_LONGPOLL_START_PARAM] = 't';
  4391. urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = Math.floor(Math.random() * 100000000);
  4392. if (_this.scriptTagHolder.uniqueCallbackIdentifier) {
  4393. urlParams[FIREBASE_LONGPOLL_CALLBACK_ID_PARAM] =
  4394. _this.scriptTagHolder.uniqueCallbackIdentifier;
  4395. }
  4396. urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
  4397. if (_this.transportSessionId) {
  4398. urlParams[TRANSPORT_SESSION_PARAM] = _this.transportSessionId;
  4399. }
  4400. if (_this.lastSessionId) {
  4401. urlParams[LAST_SESSION_PARAM] = _this.lastSessionId;
  4402. }
  4403. if (_this.applicationId) {
  4404. urlParams[APPLICATION_ID_PARAM] = _this.applicationId;
  4405. }
  4406. if (_this.appCheckToken) {
  4407. urlParams[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
  4408. }
  4409. if (typeof location !== 'undefined' &&
  4410. location.hostname &&
  4411. FORGE_DOMAIN_RE.test(location.hostname)) {
  4412. urlParams[REFERER_PARAM] = FORGE_REF;
  4413. }
  4414. var connectURL = _this.urlFn(urlParams);
  4415. _this.log_('Connecting via long-poll to ' + connectURL);
  4416. _this.scriptTagHolder.addTag(connectURL, function () {
  4417. /* do nothing */
  4418. });
  4419. });
  4420. };
  4421. /**
  4422. * Call this when a handshake has completed successfully and we want to consider the connection established
  4423. */
  4424. BrowserPollConnection.prototype.start = function () {
  4425. this.scriptTagHolder.startLongPoll(this.id, this.password);
  4426. this.addDisconnectPingFrame(this.id, this.password);
  4427. };
  4428. /**
  4429. * Forces long polling to be considered as a potential transport
  4430. */
  4431. BrowserPollConnection.forceAllow = function () {
  4432. BrowserPollConnection.forceAllow_ = true;
  4433. };
  4434. /**
  4435. * Forces longpolling to not be considered as a potential transport
  4436. */
  4437. BrowserPollConnection.forceDisallow = function () {
  4438. BrowserPollConnection.forceDisallow_ = true;
  4439. };
  4440. // Static method, use string literal so it can be accessed in a generic way
  4441. BrowserPollConnection.isAvailable = function () {
  4442. if (util.isNodeSdk()) {
  4443. return false;
  4444. }
  4445. else if (BrowserPollConnection.forceAllow_) {
  4446. return true;
  4447. }
  4448. else {
  4449. // NOTE: In React-Native there's normally no 'document', but if you debug a React-Native app in
  4450. // the Chrome debugger, 'document' is defined, but document.createElement is null (2015/06/08).
  4451. return (!BrowserPollConnection.forceDisallow_ &&
  4452. typeof document !== 'undefined' &&
  4453. document.createElement != null &&
  4454. !isChromeExtensionContentScript() &&
  4455. !isWindowsStoreApp());
  4456. }
  4457. };
  4458. /**
  4459. * No-op for polling
  4460. */
  4461. BrowserPollConnection.prototype.markConnectionHealthy = function () { };
  4462. /**
  4463. * Stops polling and cleans up the iframe
  4464. */
  4465. BrowserPollConnection.prototype.shutdown_ = function () {
  4466. this.isClosed_ = true;
  4467. if (this.scriptTagHolder) {
  4468. this.scriptTagHolder.close();
  4469. this.scriptTagHolder = null;
  4470. }
  4471. //remove the disconnect frame, which will trigger an XHR call to the server to tell it we're leaving.
  4472. if (this.myDisconnFrame) {
  4473. document.body.removeChild(this.myDisconnFrame);
  4474. this.myDisconnFrame = null;
  4475. }
  4476. if (this.connectTimeoutTimer_) {
  4477. clearTimeout(this.connectTimeoutTimer_);
  4478. this.connectTimeoutTimer_ = null;
  4479. }
  4480. };
  4481. /**
  4482. * Triggered when this transport is closed
  4483. */
  4484. BrowserPollConnection.prototype.onClosed_ = function () {
  4485. if (!this.isClosed_) {
  4486. this.log_('Longpoll is closing itself');
  4487. this.shutdown_();
  4488. if (this.onDisconnect_) {
  4489. this.onDisconnect_(this.everConnected_);
  4490. this.onDisconnect_ = null;
  4491. }
  4492. }
  4493. };
  4494. /**
  4495. * External-facing close handler. RealTime has requested we shut down. Kill our connection and tell the server
  4496. * that we've left.
  4497. */
  4498. BrowserPollConnection.prototype.close = function () {
  4499. if (!this.isClosed_) {
  4500. this.log_('Longpoll is being closed.');
  4501. this.shutdown_();
  4502. }
  4503. };
  4504. /**
  4505. * Send the JSON object down to the server. It will need to be stringified, base64 encoded, and then
  4506. * broken into chunks (since URLs have a small maximum length).
  4507. * @param data - The JSON data to transmit.
  4508. */
  4509. BrowserPollConnection.prototype.send = function (data) {
  4510. var dataStr = util.stringify(data);
  4511. this.bytesSent += dataStr.length;
  4512. this.stats_.incrementCounter('bytes_sent', dataStr.length);
  4513. //first, lets get the base64-encoded data
  4514. var base64data = util.base64Encode(dataStr);
  4515. //We can only fit a certain amount in each URL, so we need to split this request
  4516. //up into multiple pieces if it doesn't fit in one request.
  4517. var dataSegs = splitStringBySize(base64data, MAX_PAYLOAD_SIZE);
  4518. //Enqueue each segment for transmission. We assign each chunk a sequential ID and a total number
  4519. //of segments so that we can reassemble the packet on the server.
  4520. for (var i = 0; i < dataSegs.length; i++) {
  4521. this.scriptTagHolder.enqueueSegment(this.curSegmentNum, dataSegs.length, dataSegs[i]);
  4522. this.curSegmentNum++;
  4523. }
  4524. };
  4525. /**
  4526. * This is how we notify the server that we're leaving.
  4527. * We aren't able to send requests with DHTML on a window close event, but we can
  4528. * trigger XHR requests in some browsers (everything but Opera basically).
  4529. */
  4530. BrowserPollConnection.prototype.addDisconnectPingFrame = function (id, pw) {
  4531. if (util.isNodeSdk()) {
  4532. return;
  4533. }
  4534. this.myDisconnFrame = document.createElement('iframe');
  4535. var urlParams = {};
  4536. urlParams[FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM] = 't';
  4537. urlParams[FIREBASE_LONGPOLL_ID_PARAM] = id;
  4538. urlParams[FIREBASE_LONGPOLL_PW_PARAM] = pw;
  4539. this.myDisconnFrame.src = this.urlFn(urlParams);
  4540. this.myDisconnFrame.style.display = 'none';
  4541. document.body.appendChild(this.myDisconnFrame);
  4542. };
  4543. /**
  4544. * Used to track the bytes received by this client
  4545. */
  4546. BrowserPollConnection.prototype.incrementIncomingBytes_ = function (args) {
  4547. // TODO: This is an annoying perf hit just to track the number of incoming bytes. Maybe it should be opt-in.
  4548. var bytesReceived = util.stringify(args).length;
  4549. this.bytesReceived += bytesReceived;
  4550. this.stats_.incrementCounter('bytes_received', bytesReceived);
  4551. };
  4552. return BrowserPollConnection;
  4553. }());
  4554. /*********************************************************************************************
  4555. * A wrapper around an iframe that is used as a long-polling script holder.
  4556. *********************************************************************************************/
  4557. var FirebaseIFrameScriptHolder = /** @class */ (function () {
  4558. /**
  4559. * @param commandCB - The callback to be called when control commands are recevied from the server.
  4560. * @param onMessageCB - The callback to be triggered when responses arrive from the server.
  4561. * @param onDisconnect - The callback to be triggered when this tag holder is closed
  4562. * @param urlFn - A function that provides the URL of the endpoint to send data to.
  4563. */
  4564. function FirebaseIFrameScriptHolder(commandCB, onMessageCB, onDisconnect, urlFn) {
  4565. this.onDisconnect = onDisconnect;
  4566. this.urlFn = urlFn;
  4567. //We maintain a count of all of the outstanding requests, because if we have too many active at once it can cause
  4568. //problems in some browsers.
  4569. this.outstandingRequests = new Set();
  4570. //A queue of the pending segments waiting for transmission to the server.
  4571. this.pendingSegs = [];
  4572. //A serial number. We use this for two things:
  4573. // 1) A way to ensure the browser doesn't cache responses to polls
  4574. // 2) A way to make the server aware when long-polls arrive in a different order than we started them. The
  4575. // server needs to release both polls in this case or it will cause problems in Opera since Opera can only execute
  4576. // JSONP code in the order it was added to the iframe.
  4577. this.currentSerial = Math.floor(Math.random() * 100000000);
  4578. // This gets set to false when we're "closing down" the connection (e.g. we're switching transports but there's still
  4579. // incoming data from the server that we're waiting for).
  4580. this.sendNewPolls = true;
  4581. if (!util.isNodeSdk()) {
  4582. //Each script holder registers a couple of uniquely named callbacks with the window. These are called from the
  4583. //iframes where we put the long-polling script tags. We have two callbacks:
  4584. // 1) Command Callback - Triggered for control issues, like starting a connection.
  4585. // 2) Message Callback - Triggered when new data arrives.
  4586. this.uniqueCallbackIdentifier = LUIDGenerator();
  4587. window[FIREBASE_LONGPOLL_COMMAND_CB_NAME + this.uniqueCallbackIdentifier] = commandCB;
  4588. window[FIREBASE_LONGPOLL_DATA_CB_NAME + this.uniqueCallbackIdentifier] =
  4589. onMessageCB;
  4590. //Create an iframe for us to add script tags to.
  4591. this.myIFrame = FirebaseIFrameScriptHolder.createIFrame_();
  4592. // Set the iframe's contents.
  4593. var script = '';
  4594. // if we set a javascript url, it's IE and we need to set the document domain. The javascript url is sufficient
  4595. // for ie9, but ie8 needs to do it again in the document itself.
  4596. if (this.myIFrame.src &&
  4597. this.myIFrame.src.substr(0, 'javascript:'.length) === 'javascript:') {
  4598. var currentDomain = document.domain;
  4599. script = '<script>document.domain="' + currentDomain + '";</script>';
  4600. }
  4601. var iframeContents = '<html><body>' + script + '</body></html>';
  4602. try {
  4603. this.myIFrame.doc.open();
  4604. this.myIFrame.doc.write(iframeContents);
  4605. this.myIFrame.doc.close();
  4606. }
  4607. catch (e) {
  4608. log('frame writing exception');
  4609. if (e.stack) {
  4610. log(e.stack);
  4611. }
  4612. log(e);
  4613. }
  4614. }
  4615. else {
  4616. this.commandCB = commandCB;
  4617. this.onMessageCB = onMessageCB;
  4618. }
  4619. }
  4620. /**
  4621. * Each browser has its own funny way to handle iframes. Here we mush them all together into one object that I can
  4622. * actually use.
  4623. */
  4624. FirebaseIFrameScriptHolder.createIFrame_ = function () {
  4625. var iframe = document.createElement('iframe');
  4626. iframe.style.display = 'none';
  4627. // This is necessary in order to initialize the document inside the iframe
  4628. if (document.body) {
  4629. document.body.appendChild(iframe);
  4630. try {
  4631. // If document.domain has been modified in IE, this will throw an error, and we need to set the
  4632. // domain of the iframe's document manually. We can do this via a javascript: url as the src attribute
  4633. // Also note that we must do this *after* the iframe has been appended to the page. Otherwise it doesn't work.
  4634. var a = iframe.contentWindow.document;
  4635. if (!a) {
  4636. // Apologies for the log-spam, I need to do something to keep closure from optimizing out the assignment above.
  4637. log('No IE domain setting required');
  4638. }
  4639. }
  4640. catch (e) {
  4641. var domain = document.domain;
  4642. iframe.src =
  4643. "javascript:void((function(){document.open();document.domain='" +
  4644. domain +
  4645. "';document.close();})())";
  4646. }
  4647. }
  4648. else {
  4649. // LongPollConnection attempts to delay initialization until the document is ready, so hopefully this
  4650. // never gets hit.
  4651. throw 'Document body has not initialized. Wait to initialize Firebase until after the document is ready.';
  4652. }
  4653. // Get the document of the iframe in a browser-specific way.
  4654. if (iframe.contentDocument) {
  4655. iframe.doc = iframe.contentDocument; // Firefox, Opera, Safari
  4656. }
  4657. else if (iframe.contentWindow) {
  4658. iframe.doc = iframe.contentWindow.document; // Internet Explorer
  4659. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4660. }
  4661. else if (iframe.document) {
  4662. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4663. iframe.doc = iframe.document; //others?
  4664. }
  4665. return iframe;
  4666. };
  4667. /**
  4668. * Cancel all outstanding queries and remove the frame.
  4669. */
  4670. FirebaseIFrameScriptHolder.prototype.close = function () {
  4671. var _this = this;
  4672. //Mark this iframe as dead, so no new requests are sent.
  4673. this.alive = false;
  4674. if (this.myIFrame) {
  4675. //We have to actually remove all of the html inside this iframe before removing it from the
  4676. //window, or IE will continue loading and executing the script tags we've already added, which
  4677. //can lead to some errors being thrown. Setting textContent seems to be the safest way to do this.
  4678. this.myIFrame.doc.body.textContent = '';
  4679. setTimeout(function () {
  4680. if (_this.myIFrame !== null) {
  4681. document.body.removeChild(_this.myIFrame);
  4682. _this.myIFrame = null;
  4683. }
  4684. }, Math.floor(0));
  4685. }
  4686. // Protect from being called recursively.
  4687. var onDisconnect = this.onDisconnect;
  4688. if (onDisconnect) {
  4689. this.onDisconnect = null;
  4690. onDisconnect();
  4691. }
  4692. };
  4693. /**
  4694. * Actually start the long-polling session by adding the first script tag(s) to the iframe.
  4695. * @param id - The ID of this connection
  4696. * @param pw - The password for this connection
  4697. */
  4698. FirebaseIFrameScriptHolder.prototype.startLongPoll = function (id, pw) {
  4699. this.myID = id;
  4700. this.myPW = pw;
  4701. this.alive = true;
  4702. //send the initial request. If there are requests queued, make sure that we transmit as many as we are currently able to.
  4703. while (this.newRequest_()) { }
  4704. };
  4705. /**
  4706. * This is called any time someone might want a script tag to be added. It adds a script tag when there aren't
  4707. * too many outstanding requests and we are still alive.
  4708. *
  4709. * If there are outstanding packet segments to send, it sends one. If there aren't, it sends a long-poll anyways if
  4710. * needed.
  4711. */
  4712. FirebaseIFrameScriptHolder.prototype.newRequest_ = function () {
  4713. // We keep one outstanding request open all the time to receive data, but if we need to send data
  4714. // (pendingSegs.length > 0) then we create a new request to send the data. The server will automatically
  4715. // close the old request.
  4716. if (this.alive &&
  4717. this.sendNewPolls &&
  4718. this.outstandingRequests.size < (this.pendingSegs.length > 0 ? 2 : 1)) {
  4719. //construct our url
  4720. this.currentSerial++;
  4721. var urlParams = {};
  4722. urlParams[FIREBASE_LONGPOLL_ID_PARAM] = this.myID;
  4723. urlParams[FIREBASE_LONGPOLL_PW_PARAM] = this.myPW;
  4724. urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = this.currentSerial;
  4725. var theURL = this.urlFn(urlParams);
  4726. //Now add as much data as we can.
  4727. var curDataString = '';
  4728. var i = 0;
  4729. while (this.pendingSegs.length > 0) {
  4730. //first, lets see if the next segment will fit.
  4731. var nextSeg = this.pendingSegs[0];
  4732. if (nextSeg.d.length +
  4733. SEG_HEADER_SIZE +
  4734. curDataString.length <=
  4735. MAX_URL_DATA_SIZE) {
  4736. //great, the segment will fit. Lets append it.
  4737. var theSeg = this.pendingSegs.shift();
  4738. curDataString =
  4739. curDataString +
  4740. '&' +
  4741. FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM +
  4742. i +
  4743. '=' +
  4744. theSeg.seg +
  4745. '&' +
  4746. FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET +
  4747. i +
  4748. '=' +
  4749. theSeg.ts +
  4750. '&' +
  4751. FIREBASE_LONGPOLL_DATA_PARAM +
  4752. i +
  4753. '=' +
  4754. theSeg.d;
  4755. i++;
  4756. }
  4757. else {
  4758. break;
  4759. }
  4760. }
  4761. theURL = theURL + curDataString;
  4762. this.addLongPollTag_(theURL, this.currentSerial);
  4763. return true;
  4764. }
  4765. else {
  4766. return false;
  4767. }
  4768. };
  4769. /**
  4770. * Queue a packet for transmission to the server.
  4771. * @param segnum - A sequential id for this packet segment used for reassembly
  4772. * @param totalsegs - The total number of segments in this packet
  4773. * @param data - The data for this segment.
  4774. */
  4775. FirebaseIFrameScriptHolder.prototype.enqueueSegment = function (segnum, totalsegs, data) {
  4776. //add this to the queue of segments to send.
  4777. this.pendingSegs.push({ seg: segnum, ts: totalsegs, d: data });
  4778. //send the data immediately if there isn't already data being transmitted, unless
  4779. //startLongPoll hasn't been called yet.
  4780. if (this.alive) {
  4781. this.newRequest_();
  4782. }
  4783. };
  4784. /**
  4785. * Add a script tag for a regular long-poll request.
  4786. * @param url - The URL of the script tag.
  4787. * @param serial - The serial number of the request.
  4788. */
  4789. FirebaseIFrameScriptHolder.prototype.addLongPollTag_ = function (url, serial) {
  4790. var _this = this;
  4791. //remember that we sent this request.
  4792. this.outstandingRequests.add(serial);
  4793. var doNewRequest = function () {
  4794. _this.outstandingRequests.delete(serial);
  4795. _this.newRequest_();
  4796. };
  4797. // If this request doesn't return on its own accord (by the server sending us some data), we'll
  4798. // create a new one after the KEEPALIVE interval to make sure we always keep a fresh request open.
  4799. var keepaliveTimeout = setTimeout(doNewRequest, Math.floor(KEEPALIVE_REQUEST_INTERVAL));
  4800. var readyStateCB = function () {
  4801. // Request completed. Cancel the keepalive.
  4802. clearTimeout(keepaliveTimeout);
  4803. // Trigger a new request so we can continue receiving data.
  4804. doNewRequest();
  4805. };
  4806. this.addTag(url, readyStateCB);
  4807. };
  4808. /**
  4809. * Add an arbitrary script tag to the iframe.
  4810. * @param url - The URL for the script tag source.
  4811. * @param loadCB - A callback to be triggered once the script has loaded.
  4812. */
  4813. FirebaseIFrameScriptHolder.prototype.addTag = function (url, loadCB) {
  4814. var _this = this;
  4815. if (util.isNodeSdk()) {
  4816. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4817. this.doNodeLongPoll(url, loadCB);
  4818. }
  4819. else {
  4820. setTimeout(function () {
  4821. try {
  4822. // if we're already closed, don't add this poll
  4823. if (!_this.sendNewPolls) {
  4824. return;
  4825. }
  4826. var newScript_1 = _this.myIFrame.doc.createElement('script');
  4827. newScript_1.type = 'text/javascript';
  4828. newScript_1.async = true;
  4829. newScript_1.src = url;
  4830. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4831. newScript_1.onload = newScript_1.onreadystatechange =
  4832. function () {
  4833. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4834. var rstate = newScript_1.readyState;
  4835. if (!rstate || rstate === 'loaded' || rstate === 'complete') {
  4836. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4837. newScript_1.onload = newScript_1.onreadystatechange = null;
  4838. if (newScript_1.parentNode) {
  4839. newScript_1.parentNode.removeChild(newScript_1);
  4840. }
  4841. loadCB();
  4842. }
  4843. };
  4844. newScript_1.onerror = function () {
  4845. log('Long-poll script failed to load: ' + url);
  4846. _this.sendNewPolls = false;
  4847. _this.close();
  4848. };
  4849. _this.myIFrame.doc.body.appendChild(newScript_1);
  4850. }
  4851. catch (e) {
  4852. // TODO: we should make this error visible somehow
  4853. }
  4854. }, Math.floor(1));
  4855. }
  4856. };
  4857. return FirebaseIFrameScriptHolder;
  4858. }());
  4859. /**
  4860. * @license
  4861. * Copyright 2017 Google LLC
  4862. *
  4863. * Licensed under the Apache License, Version 2.0 (the "License");
  4864. * you may not use this file except in compliance with the License.
  4865. * You may obtain a copy of the License at
  4866. *
  4867. * http://www.apache.org/licenses/LICENSE-2.0
  4868. *
  4869. * Unless required by applicable law or agreed to in writing, software
  4870. * distributed under the License is distributed on an "AS IS" BASIS,
  4871. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4872. * See the License for the specific language governing permissions and
  4873. * limitations under the License.
  4874. */
  4875. /**
  4876. * Currently simplistic, this class manages what transport a Connection should use at various stages of its
  4877. * lifecycle.
  4878. *
  4879. * It starts with longpolling in a browser, and httppolling on node. It then upgrades to websockets if
  4880. * they are available.
  4881. */
  4882. var TransportManager = /** @class */ (function () {
  4883. /**
  4884. * @param repoInfo - Metadata around the namespace we're connecting to
  4885. */
  4886. function TransportManager(repoInfo) {
  4887. this.initTransports_(repoInfo);
  4888. }
  4889. Object.defineProperty(TransportManager, "ALL_TRANSPORTS", {
  4890. get: function () {
  4891. return [BrowserPollConnection, WebSocketConnection];
  4892. },
  4893. enumerable: false,
  4894. configurable: true
  4895. });
  4896. Object.defineProperty(TransportManager, "IS_TRANSPORT_INITIALIZED", {
  4897. /**
  4898. * Returns whether transport has been selected to ensure WebSocketConnection or BrowserPollConnection are not called after
  4899. * TransportManager has already set up transports_
  4900. */
  4901. get: function () {
  4902. return this.globalTransportInitialized_;
  4903. },
  4904. enumerable: false,
  4905. configurable: true
  4906. });
  4907. TransportManager.prototype.initTransports_ = function (repoInfo) {
  4908. var e_1, _a;
  4909. var isWebSocketsAvailable = WebSocketConnection && WebSocketConnection['isAvailable']();
  4910. var isSkipPollConnection = isWebSocketsAvailable && !WebSocketConnection.previouslyFailed();
  4911. if (repoInfo.webSocketOnly) {
  4912. if (!isWebSocketsAvailable) {
  4913. warn$1("wss:// URL used, but browser isn't known to support websockets. Trying anyway.");
  4914. }
  4915. isSkipPollConnection = true;
  4916. }
  4917. if (isSkipPollConnection) {
  4918. this.transports_ = [WebSocketConnection];
  4919. }
  4920. else {
  4921. var transports = (this.transports_ = []);
  4922. try {
  4923. for (var _b = tslib.__values(TransportManager.ALL_TRANSPORTS), _c = _b.next(); !_c.done; _c = _b.next()) {
  4924. var transport = _c.value;
  4925. if (transport && transport['isAvailable']()) {
  4926. transports.push(transport);
  4927. }
  4928. }
  4929. }
  4930. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  4931. finally {
  4932. try {
  4933. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  4934. }
  4935. finally { if (e_1) throw e_1.error; }
  4936. }
  4937. TransportManager.globalTransportInitialized_ = true;
  4938. }
  4939. };
  4940. /**
  4941. * @returns The constructor for the initial transport to use
  4942. */
  4943. TransportManager.prototype.initialTransport = function () {
  4944. if (this.transports_.length > 0) {
  4945. return this.transports_[0];
  4946. }
  4947. else {
  4948. throw new Error('No transports available');
  4949. }
  4950. };
  4951. /**
  4952. * @returns The constructor for the next transport, or null
  4953. */
  4954. TransportManager.prototype.upgradeTransport = function () {
  4955. if (this.transports_.length > 1) {
  4956. return this.transports_[1];
  4957. }
  4958. else {
  4959. return null;
  4960. }
  4961. };
  4962. // Keeps track of whether the TransportManager has already chosen a transport to use
  4963. TransportManager.globalTransportInitialized_ = false;
  4964. return TransportManager;
  4965. }());
  4966. /**
  4967. * @license
  4968. * Copyright 2017 Google LLC
  4969. *
  4970. * Licensed under the Apache License, Version 2.0 (the "License");
  4971. * you may not use this file except in compliance with the License.
  4972. * You may obtain a copy of the License at
  4973. *
  4974. * http://www.apache.org/licenses/LICENSE-2.0
  4975. *
  4976. * Unless required by applicable law or agreed to in writing, software
  4977. * distributed under the License is distributed on an "AS IS" BASIS,
  4978. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4979. * See the License for the specific language governing permissions and
  4980. * limitations under the License.
  4981. */
  4982. // Abort upgrade attempt if it takes longer than 60s.
  4983. var UPGRADE_TIMEOUT = 60000;
  4984. // For some transports (WebSockets), we need to "validate" the transport by exchanging a few requests and responses.
  4985. // If we haven't sent enough requests within 5s, we'll start sending noop ping requests.
  4986. var DELAY_BEFORE_SENDING_EXTRA_REQUESTS = 5000;
  4987. // If the initial data sent triggers a lot of bandwidth (i.e. it's a large put or a listen for a large amount of data)
  4988. // then we may not be able to exchange our ping/pong requests within the healthy timeout. So if we reach the timeout
  4989. // but we've sent/received enough bytes, we don't cancel the connection.
  4990. var BYTES_SENT_HEALTHY_OVERRIDE = 10 * 1024;
  4991. var BYTES_RECEIVED_HEALTHY_OVERRIDE = 100 * 1024;
  4992. var MESSAGE_TYPE = 't';
  4993. var MESSAGE_DATA = 'd';
  4994. var CONTROL_SHUTDOWN = 's';
  4995. var CONTROL_RESET = 'r';
  4996. var CONTROL_ERROR = 'e';
  4997. var CONTROL_PONG = 'o';
  4998. var SWITCH_ACK = 'a';
  4999. var END_TRANSMISSION = 'n';
  5000. var PING = 'p';
  5001. var SERVER_HELLO = 'h';
  5002. /**
  5003. * Creates a new real-time connection to the server using whichever method works
  5004. * best in the current browser.
  5005. */
  5006. var Connection = /** @class */ (function () {
  5007. /**
  5008. * @param id - an id for this connection
  5009. * @param repoInfo_ - the info for the endpoint to connect to
  5010. * @param applicationId_ - the Firebase App ID for this project
  5011. * @param appCheckToken_ - The App Check Token for this device.
  5012. * @param authToken_ - The auth token for this session.
  5013. * @param onMessage_ - the callback to be triggered when a server-push message arrives
  5014. * @param onReady_ - the callback to be triggered when this connection is ready to send messages.
  5015. * @param onDisconnect_ - the callback to be triggered when a connection was lost
  5016. * @param onKill_ - the callback to be triggered when this connection has permanently shut down.
  5017. * @param lastSessionId - last session id in persistent connection. is used to clean up old session in real-time server
  5018. */
  5019. function Connection(id, repoInfo_, applicationId_, appCheckToken_, authToken_, onMessage_, onReady_, onDisconnect_, onKill_, lastSessionId) {
  5020. this.id = id;
  5021. this.repoInfo_ = repoInfo_;
  5022. this.applicationId_ = applicationId_;
  5023. this.appCheckToken_ = appCheckToken_;
  5024. this.authToken_ = authToken_;
  5025. this.onMessage_ = onMessage_;
  5026. this.onReady_ = onReady_;
  5027. this.onDisconnect_ = onDisconnect_;
  5028. this.onKill_ = onKill_;
  5029. this.lastSessionId = lastSessionId;
  5030. this.connectionCount = 0;
  5031. this.pendingDataMessages = [];
  5032. this.state_ = 0 /* RealtimeState.CONNECTING */;
  5033. this.log_ = logWrapper('c:' + this.id + ':');
  5034. this.transportManager_ = new TransportManager(repoInfo_);
  5035. this.log_('Connection created');
  5036. this.start_();
  5037. }
  5038. /**
  5039. * Starts a connection attempt
  5040. */
  5041. Connection.prototype.start_ = function () {
  5042. var _this = this;
  5043. var conn = this.transportManager_.initialTransport();
  5044. this.conn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, null, this.lastSessionId);
  5045. // For certain transports (WebSockets), we need to send and receive several messages back and forth before we
  5046. // can consider the transport healthy.
  5047. this.primaryResponsesRequired_ = conn['responsesRequiredToBeHealthy'] || 0;
  5048. var onMessageReceived = this.connReceiver_(this.conn_);
  5049. var onConnectionLost = this.disconnReceiver_(this.conn_);
  5050. this.tx_ = this.conn_;
  5051. this.rx_ = this.conn_;
  5052. this.secondaryConn_ = null;
  5053. this.isHealthy_ = false;
  5054. /*
  5055. * Firefox doesn't like when code from one iframe tries to create another iframe by way of the parent frame.
  5056. * This can occur in the case of a redirect, i.e. we guessed wrong on what server to connect to and received a reset.
  5057. * Somehow, setTimeout seems to make this ok. That doesn't make sense from a security perspective, since you should
  5058. * still have the context of your originating frame.
  5059. */
  5060. setTimeout(function () {
  5061. // this.conn_ gets set to null in some of the tests. Check to make sure it still exists before using it
  5062. _this.conn_ && _this.conn_.open(onMessageReceived, onConnectionLost);
  5063. }, Math.floor(0));
  5064. var healthyTimeoutMS = conn['healthyTimeout'] || 0;
  5065. if (healthyTimeoutMS > 0) {
  5066. this.healthyTimeout_ = setTimeoutNonBlocking(function () {
  5067. _this.healthyTimeout_ = null;
  5068. if (!_this.isHealthy_) {
  5069. if (_this.conn_ &&
  5070. _this.conn_.bytesReceived > BYTES_RECEIVED_HEALTHY_OVERRIDE) {
  5071. _this.log_('Connection exceeded healthy timeout but has received ' +
  5072. _this.conn_.bytesReceived +
  5073. ' bytes. Marking connection healthy.');
  5074. _this.isHealthy_ = true;
  5075. _this.conn_.markConnectionHealthy();
  5076. }
  5077. else if (_this.conn_ &&
  5078. _this.conn_.bytesSent > BYTES_SENT_HEALTHY_OVERRIDE) {
  5079. _this.log_('Connection exceeded healthy timeout but has sent ' +
  5080. _this.conn_.bytesSent +
  5081. ' bytes. Leaving connection alive.');
  5082. // NOTE: We don't want to mark it healthy, since we have no guarantee that the bytes have made it to
  5083. // the server.
  5084. }
  5085. else {
  5086. _this.log_('Closing unhealthy connection after timeout.');
  5087. _this.close();
  5088. }
  5089. }
  5090. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5091. }, Math.floor(healthyTimeoutMS));
  5092. }
  5093. };
  5094. Connection.prototype.nextTransportId_ = function () {
  5095. return 'c:' + this.id + ':' + this.connectionCount++;
  5096. };
  5097. Connection.prototype.disconnReceiver_ = function (conn) {
  5098. var _this = this;
  5099. return function (everConnected) {
  5100. if (conn === _this.conn_) {
  5101. _this.onConnectionLost_(everConnected);
  5102. }
  5103. else if (conn === _this.secondaryConn_) {
  5104. _this.log_('Secondary connection lost.');
  5105. _this.onSecondaryConnectionLost_();
  5106. }
  5107. else {
  5108. _this.log_('closing an old connection');
  5109. }
  5110. };
  5111. };
  5112. Connection.prototype.connReceiver_ = function (conn) {
  5113. var _this = this;
  5114. return function (message) {
  5115. if (_this.state_ !== 2 /* RealtimeState.DISCONNECTED */) {
  5116. if (conn === _this.rx_) {
  5117. _this.onPrimaryMessageReceived_(message);
  5118. }
  5119. else if (conn === _this.secondaryConn_) {
  5120. _this.onSecondaryMessageReceived_(message);
  5121. }
  5122. else {
  5123. _this.log_('message on old connection');
  5124. }
  5125. }
  5126. };
  5127. };
  5128. /**
  5129. * @param dataMsg - An arbitrary data message to be sent to the server
  5130. */
  5131. Connection.prototype.sendRequest = function (dataMsg) {
  5132. // wrap in a data message envelope and send it on
  5133. var msg = { t: 'd', d: dataMsg };
  5134. this.sendData_(msg);
  5135. };
  5136. Connection.prototype.tryCleanupConnection = function () {
  5137. if (this.tx_ === this.secondaryConn_ && this.rx_ === this.secondaryConn_) {
  5138. this.log_('cleaning up and promoting a connection: ' + this.secondaryConn_.connId);
  5139. this.conn_ = this.secondaryConn_;
  5140. this.secondaryConn_ = null;
  5141. // the server will shutdown the old connection
  5142. }
  5143. };
  5144. Connection.prototype.onSecondaryControl_ = function (controlData) {
  5145. if (MESSAGE_TYPE in controlData) {
  5146. var cmd = controlData[MESSAGE_TYPE];
  5147. if (cmd === SWITCH_ACK) {
  5148. this.upgradeIfSecondaryHealthy_();
  5149. }
  5150. else if (cmd === CONTROL_RESET) {
  5151. // Most likely the session wasn't valid. Abandon the switch attempt
  5152. this.log_('Got a reset on secondary, closing it');
  5153. this.secondaryConn_.close();
  5154. // If we were already using this connection for something, than we need to fully close
  5155. if (this.tx_ === this.secondaryConn_ ||
  5156. this.rx_ === this.secondaryConn_) {
  5157. this.close();
  5158. }
  5159. }
  5160. else if (cmd === CONTROL_PONG) {
  5161. this.log_('got pong on secondary.');
  5162. this.secondaryResponsesRequired_--;
  5163. this.upgradeIfSecondaryHealthy_();
  5164. }
  5165. }
  5166. };
  5167. Connection.prototype.onSecondaryMessageReceived_ = function (parsedData) {
  5168. var layer = requireKey('t', parsedData);
  5169. var data = requireKey('d', parsedData);
  5170. if (layer === 'c') {
  5171. this.onSecondaryControl_(data);
  5172. }
  5173. else if (layer === 'd') {
  5174. // got a data message, but we're still second connection. Need to buffer it up
  5175. this.pendingDataMessages.push(data);
  5176. }
  5177. else {
  5178. throw new Error('Unknown protocol layer: ' + layer);
  5179. }
  5180. };
  5181. Connection.prototype.upgradeIfSecondaryHealthy_ = function () {
  5182. if (this.secondaryResponsesRequired_ <= 0) {
  5183. this.log_('Secondary connection is healthy.');
  5184. this.isHealthy_ = true;
  5185. this.secondaryConn_.markConnectionHealthy();
  5186. this.proceedWithUpgrade_();
  5187. }
  5188. else {
  5189. // Send a ping to make sure the connection is healthy.
  5190. this.log_('sending ping on secondary.');
  5191. this.secondaryConn_.send({ t: 'c', d: { t: PING, d: {} } });
  5192. }
  5193. };
  5194. Connection.prototype.proceedWithUpgrade_ = function () {
  5195. // tell this connection to consider itself open
  5196. this.secondaryConn_.start();
  5197. // send ack
  5198. this.log_('sending client ack on secondary');
  5199. this.secondaryConn_.send({ t: 'c', d: { t: SWITCH_ACK, d: {} } });
  5200. // send end packet on primary transport, switch to sending on this one
  5201. // can receive on this one, buffer responses until end received on primary transport
  5202. this.log_('Ending transmission on primary');
  5203. this.conn_.send({ t: 'c', d: { t: END_TRANSMISSION, d: {} } });
  5204. this.tx_ = this.secondaryConn_;
  5205. this.tryCleanupConnection();
  5206. };
  5207. Connection.prototype.onPrimaryMessageReceived_ = function (parsedData) {
  5208. // Must refer to parsedData properties in quotes, so closure doesn't touch them.
  5209. var layer = requireKey('t', parsedData);
  5210. var data = requireKey('d', parsedData);
  5211. if (layer === 'c') {
  5212. this.onControl_(data);
  5213. }
  5214. else if (layer === 'd') {
  5215. this.onDataMessage_(data);
  5216. }
  5217. };
  5218. Connection.prototype.onDataMessage_ = function (message) {
  5219. this.onPrimaryResponse_();
  5220. // We don't do anything with data messages, just kick them up a level
  5221. this.onMessage_(message);
  5222. };
  5223. Connection.prototype.onPrimaryResponse_ = function () {
  5224. if (!this.isHealthy_) {
  5225. this.primaryResponsesRequired_--;
  5226. if (this.primaryResponsesRequired_ <= 0) {
  5227. this.log_('Primary connection is healthy.');
  5228. this.isHealthy_ = true;
  5229. this.conn_.markConnectionHealthy();
  5230. }
  5231. }
  5232. };
  5233. Connection.prototype.onControl_ = function (controlData) {
  5234. var cmd = requireKey(MESSAGE_TYPE, controlData);
  5235. if (MESSAGE_DATA in controlData) {
  5236. var payload = controlData[MESSAGE_DATA];
  5237. if (cmd === SERVER_HELLO) {
  5238. var handshakePayload = tslib.__assign({}, payload);
  5239. if (this.repoInfo_.isUsingEmulator) {
  5240. // Upon connecting, the emulator will pass the hostname that it's aware of, but we prefer the user's set hostname via `connectDatabaseEmulator` over what the emulator passes.
  5241. handshakePayload.h = this.repoInfo_.host;
  5242. }
  5243. this.onHandshake_(handshakePayload);
  5244. }
  5245. else if (cmd === END_TRANSMISSION) {
  5246. this.log_('recvd end transmission on primary');
  5247. this.rx_ = this.secondaryConn_;
  5248. for (var i = 0; i < this.pendingDataMessages.length; ++i) {
  5249. this.onDataMessage_(this.pendingDataMessages[i]);
  5250. }
  5251. this.pendingDataMessages = [];
  5252. this.tryCleanupConnection();
  5253. }
  5254. else if (cmd === CONTROL_SHUTDOWN) {
  5255. // This was previously the 'onKill' callback passed to the lower-level connection
  5256. // payload in this case is the reason for the shutdown. Generally a human-readable error
  5257. this.onConnectionShutdown_(payload);
  5258. }
  5259. else if (cmd === CONTROL_RESET) {
  5260. // payload in this case is the host we should contact
  5261. this.onReset_(payload);
  5262. }
  5263. else if (cmd === CONTROL_ERROR) {
  5264. error('Server Error: ' + payload);
  5265. }
  5266. else if (cmd === CONTROL_PONG) {
  5267. this.log_('got pong on primary.');
  5268. this.onPrimaryResponse_();
  5269. this.sendPingOnPrimaryIfNecessary_();
  5270. }
  5271. else {
  5272. error('Unknown control packet command: ' + cmd);
  5273. }
  5274. }
  5275. };
  5276. /**
  5277. * @param handshake - The handshake data returned from the server
  5278. */
  5279. Connection.prototype.onHandshake_ = function (handshake) {
  5280. var timestamp = handshake.ts;
  5281. var version = handshake.v;
  5282. var host = handshake.h;
  5283. this.sessionId = handshake.s;
  5284. this.repoInfo_.host = host;
  5285. // if we've already closed the connection, then don't bother trying to progress further
  5286. if (this.state_ === 0 /* RealtimeState.CONNECTING */) {
  5287. this.conn_.start();
  5288. this.onConnectionEstablished_(this.conn_, timestamp);
  5289. if (PROTOCOL_VERSION !== version) {
  5290. warn$1('Protocol version mismatch detected');
  5291. }
  5292. // TODO: do we want to upgrade? when? maybe a delay?
  5293. this.tryStartUpgrade_();
  5294. }
  5295. };
  5296. Connection.prototype.tryStartUpgrade_ = function () {
  5297. var conn = this.transportManager_.upgradeTransport();
  5298. if (conn) {
  5299. this.startUpgrade_(conn);
  5300. }
  5301. };
  5302. Connection.prototype.startUpgrade_ = function (conn) {
  5303. var _this = this;
  5304. this.secondaryConn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, this.sessionId);
  5305. // For certain transports (WebSockets), we need to send and receive several messages back and forth before we
  5306. // can consider the transport healthy.
  5307. this.secondaryResponsesRequired_ =
  5308. conn['responsesRequiredToBeHealthy'] || 0;
  5309. var onMessage = this.connReceiver_(this.secondaryConn_);
  5310. var onDisconnect = this.disconnReceiver_(this.secondaryConn_);
  5311. this.secondaryConn_.open(onMessage, onDisconnect);
  5312. // If we haven't successfully upgraded after UPGRADE_TIMEOUT, give up and kill the secondary.
  5313. setTimeoutNonBlocking(function () {
  5314. if (_this.secondaryConn_) {
  5315. _this.log_('Timed out trying to upgrade.');
  5316. _this.secondaryConn_.close();
  5317. }
  5318. }, Math.floor(UPGRADE_TIMEOUT));
  5319. };
  5320. Connection.prototype.onReset_ = function (host) {
  5321. this.log_('Reset packet received. New host: ' + host);
  5322. this.repoInfo_.host = host;
  5323. // TODO: if we're already "connected", we need to trigger a disconnect at the next layer up.
  5324. // We don't currently support resets after the connection has already been established
  5325. if (this.state_ === 1 /* RealtimeState.CONNECTED */) {
  5326. this.close();
  5327. }
  5328. else {
  5329. // Close whatever connections we have open and start again.
  5330. this.closeConnections_();
  5331. this.start_();
  5332. }
  5333. };
  5334. Connection.prototype.onConnectionEstablished_ = function (conn, timestamp) {
  5335. var _this = this;
  5336. this.log_('Realtime connection established.');
  5337. this.conn_ = conn;
  5338. this.state_ = 1 /* RealtimeState.CONNECTED */;
  5339. if (this.onReady_) {
  5340. this.onReady_(timestamp, this.sessionId);
  5341. this.onReady_ = null;
  5342. }
  5343. // If after 5 seconds we haven't sent enough requests to the server to get the connection healthy,
  5344. // send some pings.
  5345. if (this.primaryResponsesRequired_ === 0) {
  5346. this.log_('Primary connection is healthy.');
  5347. this.isHealthy_ = true;
  5348. }
  5349. else {
  5350. setTimeoutNonBlocking(function () {
  5351. _this.sendPingOnPrimaryIfNecessary_();
  5352. }, Math.floor(DELAY_BEFORE_SENDING_EXTRA_REQUESTS));
  5353. }
  5354. };
  5355. Connection.prototype.sendPingOnPrimaryIfNecessary_ = function () {
  5356. // If the connection isn't considered healthy yet, we'll send a noop ping packet request.
  5357. if (!this.isHealthy_ && this.state_ === 1 /* RealtimeState.CONNECTED */) {
  5358. this.log_('sending ping on primary.');
  5359. this.sendData_({ t: 'c', d: { t: PING, d: {} } });
  5360. }
  5361. };
  5362. Connection.prototype.onSecondaryConnectionLost_ = function () {
  5363. var conn = this.secondaryConn_;
  5364. this.secondaryConn_ = null;
  5365. if (this.tx_ === conn || this.rx_ === conn) {
  5366. // we are relying on this connection already in some capacity. Therefore, a failure is real
  5367. this.close();
  5368. }
  5369. };
  5370. /**
  5371. * @param everConnected - Whether or not the connection ever reached a server. Used to determine if
  5372. * we should flush the host cache
  5373. */
  5374. Connection.prototype.onConnectionLost_ = function (everConnected) {
  5375. this.conn_ = null;
  5376. // NOTE: IF you're seeing a Firefox error for this line, I think it might be because it's getting
  5377. // called on window close and RealtimeState.CONNECTING is no longer defined. Just a guess.
  5378. if (!everConnected && this.state_ === 0 /* RealtimeState.CONNECTING */) {
  5379. this.log_('Realtime connection failed.');
  5380. // Since we failed to connect at all, clear any cached entry for this namespace in case the machine went away
  5381. if (this.repoInfo_.isCacheableHost()) {
  5382. PersistentStorage.remove('host:' + this.repoInfo_.host);
  5383. // reset the internal host to what we would show the user, i.e. <ns>.firebaseio.com
  5384. this.repoInfo_.internalHost = this.repoInfo_.host;
  5385. }
  5386. }
  5387. else if (this.state_ === 1 /* RealtimeState.CONNECTED */) {
  5388. this.log_('Realtime connection lost.');
  5389. }
  5390. this.close();
  5391. };
  5392. Connection.prototype.onConnectionShutdown_ = function (reason) {
  5393. this.log_('Connection shutdown command received. Shutting down...');
  5394. if (this.onKill_) {
  5395. this.onKill_(reason);
  5396. this.onKill_ = null;
  5397. }
  5398. // We intentionally don't want to fire onDisconnect (kill is a different case),
  5399. // so clear the callback.
  5400. this.onDisconnect_ = null;
  5401. this.close();
  5402. };
  5403. Connection.prototype.sendData_ = function (data) {
  5404. if (this.state_ !== 1 /* RealtimeState.CONNECTED */) {
  5405. throw 'Connection is not connected';
  5406. }
  5407. else {
  5408. this.tx_.send(data);
  5409. }
  5410. };
  5411. /**
  5412. * Cleans up this connection, calling the appropriate callbacks
  5413. */
  5414. Connection.prototype.close = function () {
  5415. if (this.state_ !== 2 /* RealtimeState.DISCONNECTED */) {
  5416. this.log_('Closing realtime connection.');
  5417. this.state_ = 2 /* RealtimeState.DISCONNECTED */;
  5418. this.closeConnections_();
  5419. if (this.onDisconnect_) {
  5420. this.onDisconnect_();
  5421. this.onDisconnect_ = null;
  5422. }
  5423. }
  5424. };
  5425. Connection.prototype.closeConnections_ = function () {
  5426. this.log_('Shutting down all connections');
  5427. if (this.conn_) {
  5428. this.conn_.close();
  5429. this.conn_ = null;
  5430. }
  5431. if (this.secondaryConn_) {
  5432. this.secondaryConn_.close();
  5433. this.secondaryConn_ = null;
  5434. }
  5435. if (this.healthyTimeout_) {
  5436. clearTimeout(this.healthyTimeout_);
  5437. this.healthyTimeout_ = null;
  5438. }
  5439. };
  5440. return Connection;
  5441. }());
  5442. /**
  5443. * @license
  5444. * Copyright 2017 Google LLC
  5445. *
  5446. * Licensed under the Apache License, Version 2.0 (the "License");
  5447. * you may not use this file except in compliance with the License.
  5448. * You may obtain a copy of the License at
  5449. *
  5450. * http://www.apache.org/licenses/LICENSE-2.0
  5451. *
  5452. * Unless required by applicable law or agreed to in writing, software
  5453. * distributed under the License is distributed on an "AS IS" BASIS,
  5454. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5455. * See the License for the specific language governing permissions and
  5456. * limitations under the License.
  5457. */
  5458. /**
  5459. * Interface defining the set of actions that can be performed against the Firebase server
  5460. * (basically corresponds to our wire protocol).
  5461. *
  5462. * @interface
  5463. */
  5464. var ServerActions = /** @class */ (function () {
  5465. function ServerActions() {
  5466. }
  5467. ServerActions.prototype.put = function (pathString, data, onComplete, hash) { };
  5468. ServerActions.prototype.merge = function (pathString, data, onComplete, hash) { };
  5469. /**
  5470. * Refreshes the auth token for the current connection.
  5471. * @param token - The authentication token
  5472. */
  5473. ServerActions.prototype.refreshAuthToken = function (token) { };
  5474. /**
  5475. * Refreshes the app check token for the current connection.
  5476. * @param token The app check token
  5477. */
  5478. ServerActions.prototype.refreshAppCheckToken = function (token) { };
  5479. ServerActions.prototype.onDisconnectPut = function (pathString, data, onComplete) { };
  5480. ServerActions.prototype.onDisconnectMerge = function (pathString, data, onComplete) { };
  5481. ServerActions.prototype.onDisconnectCancel = function (pathString, onComplete) { };
  5482. ServerActions.prototype.reportStats = function (stats) { };
  5483. return ServerActions;
  5484. }());
  5485. /**
  5486. * @license
  5487. * Copyright 2017 Google LLC
  5488. *
  5489. * Licensed under the Apache License, Version 2.0 (the "License");
  5490. * you may not use this file except in compliance with the License.
  5491. * You may obtain a copy of the License at
  5492. *
  5493. * http://www.apache.org/licenses/LICENSE-2.0
  5494. *
  5495. * Unless required by applicable law or agreed to in writing, software
  5496. * distributed under the License is distributed on an "AS IS" BASIS,
  5497. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5498. * See the License for the specific language governing permissions and
  5499. * limitations under the License.
  5500. */
  5501. /**
  5502. * Base class to be used if you want to emit events. Call the constructor with
  5503. * the set of allowed event names.
  5504. */
  5505. var EventEmitter = /** @class */ (function () {
  5506. function EventEmitter(allowedEvents_) {
  5507. this.allowedEvents_ = allowedEvents_;
  5508. this.listeners_ = {};
  5509. util.assert(Array.isArray(allowedEvents_) && allowedEvents_.length > 0, 'Requires a non-empty array');
  5510. }
  5511. /**
  5512. * To be called by derived classes to trigger events.
  5513. */
  5514. EventEmitter.prototype.trigger = function (eventType) {
  5515. var varArgs = [];
  5516. for (var _i = 1; _i < arguments.length; _i++) {
  5517. varArgs[_i - 1] = arguments[_i];
  5518. }
  5519. if (Array.isArray(this.listeners_[eventType])) {
  5520. // Clone the list, since callbacks could add/remove listeners.
  5521. var listeners = tslib.__spreadArray([], tslib.__read(this.listeners_[eventType]), false);
  5522. for (var i = 0; i < listeners.length; i++) {
  5523. listeners[i].callback.apply(listeners[i].context, varArgs);
  5524. }
  5525. }
  5526. };
  5527. EventEmitter.prototype.on = function (eventType, callback, context) {
  5528. this.validateEventType_(eventType);
  5529. this.listeners_[eventType] = this.listeners_[eventType] || [];
  5530. this.listeners_[eventType].push({ callback: callback, context: context });
  5531. var eventData = this.getInitialEvent(eventType);
  5532. if (eventData) {
  5533. callback.apply(context, eventData);
  5534. }
  5535. };
  5536. EventEmitter.prototype.off = function (eventType, callback, context) {
  5537. this.validateEventType_(eventType);
  5538. var listeners = this.listeners_[eventType] || [];
  5539. for (var i = 0; i < listeners.length; i++) {
  5540. if (listeners[i].callback === callback &&
  5541. (!context || context === listeners[i].context)) {
  5542. listeners.splice(i, 1);
  5543. return;
  5544. }
  5545. }
  5546. };
  5547. EventEmitter.prototype.validateEventType_ = function (eventType) {
  5548. util.assert(this.allowedEvents_.find(function (et) {
  5549. return et === eventType;
  5550. }), 'Unknown event: ' + eventType);
  5551. };
  5552. return EventEmitter;
  5553. }());
  5554. /**
  5555. * @license
  5556. * Copyright 2017 Google LLC
  5557. *
  5558. * Licensed under the Apache License, Version 2.0 (the "License");
  5559. * you may not use this file except in compliance with the License.
  5560. * You may obtain a copy of the License at
  5561. *
  5562. * http://www.apache.org/licenses/LICENSE-2.0
  5563. *
  5564. * Unless required by applicable law or agreed to in writing, software
  5565. * distributed under the License is distributed on an "AS IS" BASIS,
  5566. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5567. * See the License for the specific language governing permissions and
  5568. * limitations under the License.
  5569. */
  5570. /**
  5571. * Monitors online state (as reported by window.online/offline events).
  5572. *
  5573. * The expectation is that this could have many false positives (thinks we are online
  5574. * when we're not), but no false negatives. So we can safely use it to determine when
  5575. * we definitely cannot reach the internet.
  5576. */
  5577. var OnlineMonitor = /** @class */ (function (_super) {
  5578. tslib.__extends(OnlineMonitor, _super);
  5579. function OnlineMonitor() {
  5580. var _this = _super.call(this, ['online']) || this;
  5581. _this.online_ = true;
  5582. // We've had repeated complaints that Cordova apps can get stuck "offline", e.g.
  5583. // https://forum.ionicframework.com/t/firebase-connection-is-lost-and-never-come-back/43810
  5584. // It would seem that the 'online' event does not always fire consistently. So we disable it
  5585. // for Cordova.
  5586. if (typeof window !== 'undefined' &&
  5587. typeof window.addEventListener !== 'undefined' &&
  5588. !util.isMobileCordova()) {
  5589. window.addEventListener('online', function () {
  5590. if (!_this.online_) {
  5591. _this.online_ = true;
  5592. _this.trigger('online', true);
  5593. }
  5594. }, false);
  5595. window.addEventListener('offline', function () {
  5596. if (_this.online_) {
  5597. _this.online_ = false;
  5598. _this.trigger('online', false);
  5599. }
  5600. }, false);
  5601. }
  5602. return _this;
  5603. }
  5604. OnlineMonitor.getInstance = function () {
  5605. return new OnlineMonitor();
  5606. };
  5607. OnlineMonitor.prototype.getInitialEvent = function (eventType) {
  5608. util.assert(eventType === 'online', 'Unknown event type: ' + eventType);
  5609. return [this.online_];
  5610. };
  5611. OnlineMonitor.prototype.currentlyOnline = function () {
  5612. return this.online_;
  5613. };
  5614. return OnlineMonitor;
  5615. }(EventEmitter));
  5616. /**
  5617. * @license
  5618. * Copyright 2017 Google LLC
  5619. *
  5620. * Licensed under the Apache License, Version 2.0 (the "License");
  5621. * you may not use this file except in compliance with the License.
  5622. * You may obtain a copy of the License at
  5623. *
  5624. * http://www.apache.org/licenses/LICENSE-2.0
  5625. *
  5626. * Unless required by applicable law or agreed to in writing, software
  5627. * distributed under the License is distributed on an "AS IS" BASIS,
  5628. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5629. * See the License for the specific language governing permissions and
  5630. * limitations under the License.
  5631. */
  5632. /** Maximum key depth. */
  5633. var MAX_PATH_DEPTH = 32;
  5634. /** Maximum number of (UTF8) bytes in a Firebase path. */
  5635. var MAX_PATH_LENGTH_BYTES = 768;
  5636. /**
  5637. * An immutable object representing a parsed path. It's immutable so that you
  5638. * can pass them around to other functions without worrying about them changing
  5639. * it.
  5640. */
  5641. var Path = /** @class */ (function () {
  5642. /**
  5643. * @param pathOrString - Path string to parse, or another path, or the raw
  5644. * tokens array
  5645. */
  5646. function Path(pathOrString, pieceNum) {
  5647. if (pieceNum === void 0) {
  5648. this.pieces_ = pathOrString.split('/');
  5649. // Remove empty pieces.
  5650. var copyTo = 0;
  5651. for (var i = 0; i < this.pieces_.length; i++) {
  5652. if (this.pieces_[i].length > 0) {
  5653. this.pieces_[copyTo] = this.pieces_[i];
  5654. copyTo++;
  5655. }
  5656. }
  5657. this.pieces_.length = copyTo;
  5658. this.pieceNum_ = 0;
  5659. }
  5660. else {
  5661. this.pieces_ = pathOrString;
  5662. this.pieceNum_ = pieceNum;
  5663. }
  5664. }
  5665. Path.prototype.toString = function () {
  5666. var pathString = '';
  5667. for (var i = this.pieceNum_; i < this.pieces_.length; i++) {
  5668. if (this.pieces_[i] !== '') {
  5669. pathString += '/' + this.pieces_[i];
  5670. }
  5671. }
  5672. return pathString || '/';
  5673. };
  5674. return Path;
  5675. }());
  5676. function newEmptyPath() {
  5677. return new Path('');
  5678. }
  5679. function pathGetFront(path) {
  5680. if (path.pieceNum_ >= path.pieces_.length) {
  5681. return null;
  5682. }
  5683. return path.pieces_[path.pieceNum_];
  5684. }
  5685. /**
  5686. * @returns The number of segments in this path
  5687. */
  5688. function pathGetLength(path) {
  5689. return path.pieces_.length - path.pieceNum_;
  5690. }
  5691. function pathPopFront(path) {
  5692. var pieceNum = path.pieceNum_;
  5693. if (pieceNum < path.pieces_.length) {
  5694. pieceNum++;
  5695. }
  5696. return new Path(path.pieces_, pieceNum);
  5697. }
  5698. function pathGetBack(path) {
  5699. if (path.pieceNum_ < path.pieces_.length) {
  5700. return path.pieces_[path.pieces_.length - 1];
  5701. }
  5702. return null;
  5703. }
  5704. function pathToUrlEncodedString(path) {
  5705. var pathString = '';
  5706. for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
  5707. if (path.pieces_[i] !== '') {
  5708. pathString += '/' + encodeURIComponent(String(path.pieces_[i]));
  5709. }
  5710. }
  5711. return pathString || '/';
  5712. }
  5713. /**
  5714. * Shallow copy of the parts of the path.
  5715. *
  5716. */
  5717. function pathSlice(path, begin) {
  5718. if (begin === void 0) { begin = 0; }
  5719. return path.pieces_.slice(path.pieceNum_ + begin);
  5720. }
  5721. function pathParent(path) {
  5722. if (path.pieceNum_ >= path.pieces_.length) {
  5723. return null;
  5724. }
  5725. var pieces = [];
  5726. for (var i = path.pieceNum_; i < path.pieces_.length - 1; i++) {
  5727. pieces.push(path.pieces_[i]);
  5728. }
  5729. return new Path(pieces, 0);
  5730. }
  5731. function pathChild(path, childPathObj) {
  5732. var pieces = [];
  5733. for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
  5734. pieces.push(path.pieces_[i]);
  5735. }
  5736. if (childPathObj instanceof Path) {
  5737. for (var i = childPathObj.pieceNum_; i < childPathObj.pieces_.length; i++) {
  5738. pieces.push(childPathObj.pieces_[i]);
  5739. }
  5740. }
  5741. else {
  5742. var childPieces = childPathObj.split('/');
  5743. for (var i = 0; i < childPieces.length; i++) {
  5744. if (childPieces[i].length > 0) {
  5745. pieces.push(childPieces[i]);
  5746. }
  5747. }
  5748. }
  5749. return new Path(pieces, 0);
  5750. }
  5751. /**
  5752. * @returns True if there are no segments in this path
  5753. */
  5754. function pathIsEmpty(path) {
  5755. return path.pieceNum_ >= path.pieces_.length;
  5756. }
  5757. /**
  5758. * @returns The path from outerPath to innerPath
  5759. */
  5760. function newRelativePath(outerPath, innerPath) {
  5761. var outer = pathGetFront(outerPath), inner = pathGetFront(innerPath);
  5762. if (outer === null) {
  5763. return innerPath;
  5764. }
  5765. else if (outer === inner) {
  5766. return newRelativePath(pathPopFront(outerPath), pathPopFront(innerPath));
  5767. }
  5768. else {
  5769. throw new Error('INTERNAL ERROR: innerPath (' +
  5770. innerPath +
  5771. ') is not within ' +
  5772. 'outerPath (' +
  5773. outerPath +
  5774. ')');
  5775. }
  5776. }
  5777. /**
  5778. * @returns -1, 0, 1 if left is less, equal, or greater than the right.
  5779. */
  5780. function pathCompare(left, right) {
  5781. var leftKeys = pathSlice(left, 0);
  5782. var rightKeys = pathSlice(right, 0);
  5783. for (var i = 0; i < leftKeys.length && i < rightKeys.length; i++) {
  5784. var cmp = nameCompare(leftKeys[i], rightKeys[i]);
  5785. if (cmp !== 0) {
  5786. return cmp;
  5787. }
  5788. }
  5789. if (leftKeys.length === rightKeys.length) {
  5790. return 0;
  5791. }
  5792. return leftKeys.length < rightKeys.length ? -1 : 1;
  5793. }
  5794. /**
  5795. * @returns true if paths are the same.
  5796. */
  5797. function pathEquals(path, other) {
  5798. if (pathGetLength(path) !== pathGetLength(other)) {
  5799. return false;
  5800. }
  5801. for (var i = path.pieceNum_, j = other.pieceNum_; i <= path.pieces_.length; i++, j++) {
  5802. if (path.pieces_[i] !== other.pieces_[j]) {
  5803. return false;
  5804. }
  5805. }
  5806. return true;
  5807. }
  5808. /**
  5809. * @returns True if this path is a parent of (or the same as) other
  5810. */
  5811. function pathContains(path, other) {
  5812. var i = path.pieceNum_;
  5813. var j = other.pieceNum_;
  5814. if (pathGetLength(path) > pathGetLength(other)) {
  5815. return false;
  5816. }
  5817. while (i < path.pieces_.length) {
  5818. if (path.pieces_[i] !== other.pieces_[j]) {
  5819. return false;
  5820. }
  5821. ++i;
  5822. ++j;
  5823. }
  5824. return true;
  5825. }
  5826. /**
  5827. * Dynamic (mutable) path used to count path lengths.
  5828. *
  5829. * This class is used to efficiently check paths for valid
  5830. * length (in UTF8 bytes) and depth (used in path validation).
  5831. *
  5832. * Throws Error exception if path is ever invalid.
  5833. *
  5834. * The definition of a path always begins with '/'.
  5835. */
  5836. var ValidationPath = /** @class */ (function () {
  5837. /**
  5838. * @param path - Initial Path.
  5839. * @param errorPrefix_ - Prefix for any error messages.
  5840. */
  5841. function ValidationPath(path, errorPrefix_) {
  5842. this.errorPrefix_ = errorPrefix_;
  5843. this.parts_ = pathSlice(path, 0);
  5844. /** Initialize to number of '/' chars needed in path. */
  5845. this.byteLength_ = Math.max(1, this.parts_.length);
  5846. for (var i = 0; i < this.parts_.length; i++) {
  5847. this.byteLength_ += util.stringLength(this.parts_[i]);
  5848. }
  5849. validationPathCheckValid(this);
  5850. }
  5851. return ValidationPath;
  5852. }());
  5853. function validationPathPush(validationPath, child) {
  5854. // Count the needed '/'
  5855. if (validationPath.parts_.length > 0) {
  5856. validationPath.byteLength_ += 1;
  5857. }
  5858. validationPath.parts_.push(child);
  5859. validationPath.byteLength_ += util.stringLength(child);
  5860. validationPathCheckValid(validationPath);
  5861. }
  5862. function validationPathPop(validationPath) {
  5863. var last = validationPath.parts_.pop();
  5864. validationPath.byteLength_ -= util.stringLength(last);
  5865. // Un-count the previous '/'
  5866. if (validationPath.parts_.length > 0) {
  5867. validationPath.byteLength_ -= 1;
  5868. }
  5869. }
  5870. function validationPathCheckValid(validationPath) {
  5871. if (validationPath.byteLength_ > MAX_PATH_LENGTH_BYTES) {
  5872. throw new Error(validationPath.errorPrefix_ +
  5873. 'has a key path longer than ' +
  5874. MAX_PATH_LENGTH_BYTES +
  5875. ' bytes (' +
  5876. validationPath.byteLength_ +
  5877. ').');
  5878. }
  5879. if (validationPath.parts_.length > MAX_PATH_DEPTH) {
  5880. throw new Error(validationPath.errorPrefix_ +
  5881. 'path specified exceeds the maximum depth that can be written (' +
  5882. MAX_PATH_DEPTH +
  5883. ') or object contains a cycle ' +
  5884. validationPathToErrorString(validationPath));
  5885. }
  5886. }
  5887. /**
  5888. * String for use in error messages - uses '.' notation for path.
  5889. */
  5890. function validationPathToErrorString(validationPath) {
  5891. if (validationPath.parts_.length === 0) {
  5892. return '';
  5893. }
  5894. return "in property '" + validationPath.parts_.join('.') + "'";
  5895. }
  5896. /**
  5897. * @license
  5898. * Copyright 2017 Google LLC
  5899. *
  5900. * Licensed under the Apache License, Version 2.0 (the "License");
  5901. * you may not use this file except in compliance with the License.
  5902. * You may obtain a copy of the License at
  5903. *
  5904. * http://www.apache.org/licenses/LICENSE-2.0
  5905. *
  5906. * Unless required by applicable law or agreed to in writing, software
  5907. * distributed under the License is distributed on an "AS IS" BASIS,
  5908. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5909. * See the License for the specific language governing permissions and
  5910. * limitations under the License.
  5911. */
  5912. var VisibilityMonitor = /** @class */ (function (_super) {
  5913. tslib.__extends(VisibilityMonitor, _super);
  5914. function VisibilityMonitor() {
  5915. var _this = _super.call(this, ['visible']) || this;
  5916. var hidden;
  5917. var visibilityChange;
  5918. if (typeof document !== 'undefined' &&
  5919. typeof document.addEventListener !== 'undefined') {
  5920. if (typeof document['hidden'] !== 'undefined') {
  5921. // Opera 12.10 and Firefox 18 and later support
  5922. visibilityChange = 'visibilitychange';
  5923. hidden = 'hidden';
  5924. }
  5925. else if (typeof document['mozHidden'] !== 'undefined') {
  5926. visibilityChange = 'mozvisibilitychange';
  5927. hidden = 'mozHidden';
  5928. }
  5929. else if (typeof document['msHidden'] !== 'undefined') {
  5930. visibilityChange = 'msvisibilitychange';
  5931. hidden = 'msHidden';
  5932. }
  5933. else if (typeof document['webkitHidden'] !== 'undefined') {
  5934. visibilityChange = 'webkitvisibilitychange';
  5935. hidden = 'webkitHidden';
  5936. }
  5937. }
  5938. // Initially, we always assume we are visible. This ensures that in browsers
  5939. // without page visibility support or in cases where we are never visible
  5940. // (e.g. chrome extension), we act as if we are visible, i.e. don't delay
  5941. // reconnects
  5942. _this.visible_ = true;
  5943. if (visibilityChange) {
  5944. document.addEventListener(visibilityChange, function () {
  5945. var visible = !document[hidden];
  5946. if (visible !== _this.visible_) {
  5947. _this.visible_ = visible;
  5948. _this.trigger('visible', visible);
  5949. }
  5950. }, false);
  5951. }
  5952. return _this;
  5953. }
  5954. VisibilityMonitor.getInstance = function () {
  5955. return new VisibilityMonitor();
  5956. };
  5957. VisibilityMonitor.prototype.getInitialEvent = function (eventType) {
  5958. util.assert(eventType === 'visible', 'Unknown event type: ' + eventType);
  5959. return [this.visible_];
  5960. };
  5961. return VisibilityMonitor;
  5962. }(EventEmitter));
  5963. /**
  5964. * @license
  5965. * Copyright 2017 Google LLC
  5966. *
  5967. * Licensed under the Apache License, Version 2.0 (the "License");
  5968. * you may not use this file except in compliance with the License.
  5969. * You may obtain a copy of the License at
  5970. *
  5971. * http://www.apache.org/licenses/LICENSE-2.0
  5972. *
  5973. * Unless required by applicable law or agreed to in writing, software
  5974. * distributed under the License is distributed on an "AS IS" BASIS,
  5975. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5976. * See the License for the specific language governing permissions and
  5977. * limitations under the License.
  5978. */
  5979. var RECONNECT_MIN_DELAY = 1000;
  5980. var RECONNECT_MAX_DELAY_DEFAULT = 60 * 5 * 1000; // 5 minutes in milliseconds (Case: 1858)
  5981. var RECONNECT_MAX_DELAY_FOR_ADMINS = 30 * 1000; // 30 seconds for admin clients (likely to be a backend server)
  5982. var RECONNECT_DELAY_MULTIPLIER = 1.3;
  5983. var RECONNECT_DELAY_RESET_TIMEOUT = 30000; // Reset delay back to MIN_DELAY after being connected for 30sec.
  5984. var SERVER_KILL_INTERRUPT_REASON = 'server_kill';
  5985. // If auth fails repeatedly, we'll assume something is wrong and log a warning / back off.
  5986. var INVALID_TOKEN_THRESHOLD = 3;
  5987. /**
  5988. * Firebase connection. Abstracts wire protocol and handles reconnecting.
  5989. *
  5990. * NOTE: All JSON objects sent to the realtime connection must have property names enclosed
  5991. * in quotes to make sure the closure compiler does not minify them.
  5992. */
  5993. var PersistentConnection = /** @class */ (function (_super) {
  5994. tslib.__extends(PersistentConnection, _super);
  5995. /**
  5996. * @param repoInfo_ - Data about the namespace we are connecting to
  5997. * @param applicationId_ - The Firebase App ID for this project
  5998. * @param onDataUpdate_ - A callback for new data from the server
  5999. */
  6000. function PersistentConnection(repoInfo_, applicationId_, onDataUpdate_, onConnectStatus_, onServerInfoUpdate_, authTokenProvider_, appCheckTokenProvider_, authOverride_) {
  6001. var _this = _super.call(this) || this;
  6002. _this.repoInfo_ = repoInfo_;
  6003. _this.applicationId_ = applicationId_;
  6004. _this.onDataUpdate_ = onDataUpdate_;
  6005. _this.onConnectStatus_ = onConnectStatus_;
  6006. _this.onServerInfoUpdate_ = onServerInfoUpdate_;
  6007. _this.authTokenProvider_ = authTokenProvider_;
  6008. _this.appCheckTokenProvider_ = appCheckTokenProvider_;
  6009. _this.authOverride_ = authOverride_;
  6010. // Used for diagnostic logging.
  6011. _this.id = PersistentConnection.nextPersistentConnectionId_++;
  6012. _this.log_ = logWrapper('p:' + _this.id + ':');
  6013. _this.interruptReasons_ = {};
  6014. _this.listens = new Map();
  6015. _this.outstandingPuts_ = [];
  6016. _this.outstandingGets_ = [];
  6017. _this.outstandingPutCount_ = 0;
  6018. _this.outstandingGetCount_ = 0;
  6019. _this.onDisconnectRequestQueue_ = [];
  6020. _this.connected_ = false;
  6021. _this.reconnectDelay_ = RECONNECT_MIN_DELAY;
  6022. _this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_DEFAULT;
  6023. _this.securityDebugCallback_ = null;
  6024. _this.lastSessionId = null;
  6025. _this.establishConnectionTimer_ = null;
  6026. _this.visible_ = false;
  6027. // Before we get connected, we keep a queue of pending messages to send.
  6028. _this.requestCBHash_ = {};
  6029. _this.requestNumber_ = 0;
  6030. _this.realtime_ = null;
  6031. _this.authToken_ = null;
  6032. _this.appCheckToken_ = null;
  6033. _this.forceTokenRefresh_ = false;
  6034. _this.invalidAuthTokenCount_ = 0;
  6035. _this.invalidAppCheckTokenCount_ = 0;
  6036. _this.firstConnection_ = true;
  6037. _this.lastConnectionAttemptTime_ = null;
  6038. _this.lastConnectionEstablishedTime_ = null;
  6039. if (authOverride_ && !util.isNodeSdk()) {
  6040. throw new Error('Auth override specified in options, but not supported on non Node.js platforms');
  6041. }
  6042. VisibilityMonitor.getInstance().on('visible', _this.onVisible_, _this);
  6043. if (repoInfo_.host.indexOf('fblocal') === -1) {
  6044. OnlineMonitor.getInstance().on('online', _this.onOnline_, _this);
  6045. }
  6046. return _this;
  6047. }
  6048. PersistentConnection.prototype.sendRequest = function (action, body, onResponse) {
  6049. var curReqNum = ++this.requestNumber_;
  6050. var msg = { r: curReqNum, a: action, b: body };
  6051. this.log_(util.stringify(msg));
  6052. util.assert(this.connected_, "sendRequest call when we're not connected not allowed.");
  6053. this.realtime_.sendRequest(msg);
  6054. if (onResponse) {
  6055. this.requestCBHash_[curReqNum] = onResponse;
  6056. }
  6057. };
  6058. PersistentConnection.prototype.get = function (query) {
  6059. this.initConnection_();
  6060. var deferred = new util.Deferred();
  6061. var request = {
  6062. p: query._path.toString(),
  6063. q: query._queryObject
  6064. };
  6065. var outstandingGet = {
  6066. action: 'g',
  6067. request: request,
  6068. onComplete: function (message) {
  6069. var payload = message['d'];
  6070. if (message['s'] === 'ok') {
  6071. deferred.resolve(payload);
  6072. }
  6073. else {
  6074. deferred.reject(payload);
  6075. }
  6076. }
  6077. };
  6078. this.outstandingGets_.push(outstandingGet);
  6079. this.outstandingGetCount_++;
  6080. var index = this.outstandingGets_.length - 1;
  6081. if (this.connected_) {
  6082. this.sendGet_(index);
  6083. }
  6084. return deferred.promise;
  6085. };
  6086. PersistentConnection.prototype.listen = function (query, currentHashFn, tag, onComplete) {
  6087. this.initConnection_();
  6088. var queryId = query._queryIdentifier;
  6089. var pathString = query._path.toString();
  6090. this.log_('Listen called for ' + pathString + ' ' + queryId);
  6091. if (!this.listens.has(pathString)) {
  6092. this.listens.set(pathString, new Map());
  6093. }
  6094. util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'listen() called for non-default but complete query');
  6095. util.assert(!this.listens.get(pathString).has(queryId), "listen() called twice for same path/queryId.");
  6096. var listenSpec = {
  6097. onComplete: onComplete,
  6098. hashFn: currentHashFn,
  6099. query: query,
  6100. tag: tag
  6101. };
  6102. this.listens.get(pathString).set(queryId, listenSpec);
  6103. if (this.connected_) {
  6104. this.sendListen_(listenSpec);
  6105. }
  6106. };
  6107. PersistentConnection.prototype.sendGet_ = function (index) {
  6108. var _this = this;
  6109. var get = this.outstandingGets_[index];
  6110. this.sendRequest('g', get.request, function (message) {
  6111. delete _this.outstandingGets_[index];
  6112. _this.outstandingGetCount_--;
  6113. if (_this.outstandingGetCount_ === 0) {
  6114. _this.outstandingGets_ = [];
  6115. }
  6116. if (get.onComplete) {
  6117. get.onComplete(message);
  6118. }
  6119. });
  6120. };
  6121. PersistentConnection.prototype.sendListen_ = function (listenSpec) {
  6122. var _this = this;
  6123. var query = listenSpec.query;
  6124. var pathString = query._path.toString();
  6125. var queryId = query._queryIdentifier;
  6126. this.log_('Listen on ' + pathString + ' for ' + queryId);
  6127. var req = { /*path*/ p: pathString };
  6128. var action = 'q';
  6129. // Only bother to send query if it's non-default.
  6130. if (listenSpec.tag) {
  6131. req['q'] = query._queryObject;
  6132. req['t'] = listenSpec.tag;
  6133. }
  6134. req[ /*hash*/'h'] = listenSpec.hashFn();
  6135. this.sendRequest(action, req, function (message) {
  6136. var payload = message[ /*data*/'d'];
  6137. var status = message[ /*status*/'s'];
  6138. // print warnings in any case...
  6139. PersistentConnection.warnOnListenWarnings_(payload, query);
  6140. var currentListenSpec = _this.listens.get(pathString) &&
  6141. _this.listens.get(pathString).get(queryId);
  6142. // only trigger actions if the listen hasn't been removed and readded
  6143. if (currentListenSpec === listenSpec) {
  6144. _this.log_('listen response', message);
  6145. if (status !== 'ok') {
  6146. _this.removeListen_(pathString, queryId);
  6147. }
  6148. if (listenSpec.onComplete) {
  6149. listenSpec.onComplete(status, payload);
  6150. }
  6151. }
  6152. });
  6153. };
  6154. PersistentConnection.warnOnListenWarnings_ = function (payload, query) {
  6155. if (payload && typeof payload === 'object' && util.contains(payload, 'w')) {
  6156. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6157. var warnings = util.safeGet(payload, 'w');
  6158. if (Array.isArray(warnings) && ~warnings.indexOf('no_index')) {
  6159. var indexSpec = '".indexOn": "' + query._queryParams.getIndex().toString() + '"';
  6160. var indexPath = query._path.toString();
  6161. warn$1("Using an unspecified index. Your data will be downloaded and " +
  6162. "filtered on the client. Consider adding ".concat(indexSpec, " at ") +
  6163. "".concat(indexPath, " to your security rules for better performance."));
  6164. }
  6165. }
  6166. };
  6167. PersistentConnection.prototype.refreshAuthToken = function (token) {
  6168. this.authToken_ = token;
  6169. this.log_('Auth token refreshed');
  6170. if (this.authToken_) {
  6171. this.tryAuth();
  6172. }
  6173. else {
  6174. //If we're connected we want to let the server know to unauthenticate us. If we're not connected, simply delete
  6175. //the credential so we dont become authenticated next time we connect.
  6176. if (this.connected_) {
  6177. this.sendRequest('unauth', {}, function () { });
  6178. }
  6179. }
  6180. this.reduceReconnectDelayIfAdminCredential_(token);
  6181. };
  6182. PersistentConnection.prototype.reduceReconnectDelayIfAdminCredential_ = function (credential) {
  6183. // NOTE: This isn't intended to be bulletproof (a malicious developer can always just modify the client).
  6184. // Additionally, we don't bother resetting the max delay back to the default if auth fails / expires.
  6185. var isFirebaseSecret = credential && credential.length === 40;
  6186. if (isFirebaseSecret || util.isAdmin(credential)) {
  6187. this.log_('Admin auth credential detected. Reducing max reconnect time.');
  6188. this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
  6189. }
  6190. };
  6191. PersistentConnection.prototype.refreshAppCheckToken = function (token) {
  6192. this.appCheckToken_ = token;
  6193. this.log_('App check token refreshed');
  6194. if (this.appCheckToken_) {
  6195. this.tryAppCheck();
  6196. }
  6197. else {
  6198. //If we're connected we want to let the server know to unauthenticate us.
  6199. //If we're not connected, simply delete the credential so we dont become
  6200. // authenticated next time we connect.
  6201. if (this.connected_) {
  6202. this.sendRequest('unappeck', {}, function () { });
  6203. }
  6204. }
  6205. };
  6206. /**
  6207. * Attempts to authenticate with the given credentials. If the authentication attempt fails, it's triggered like
  6208. * a auth revoked (the connection is closed).
  6209. */
  6210. PersistentConnection.prototype.tryAuth = function () {
  6211. var _this = this;
  6212. if (this.connected_ && this.authToken_) {
  6213. var token_1 = this.authToken_;
  6214. var authMethod = util.isValidFormat(token_1) ? 'auth' : 'gauth';
  6215. var requestData = { cred: token_1 };
  6216. if (this.authOverride_ === null) {
  6217. requestData['noauth'] = true;
  6218. }
  6219. else if (typeof this.authOverride_ === 'object') {
  6220. requestData['authvar'] = this.authOverride_;
  6221. }
  6222. this.sendRequest(authMethod, requestData, function (res) {
  6223. var status = res[ /*status*/'s'];
  6224. var data = res[ /*data*/'d'] || 'error';
  6225. if (_this.authToken_ === token_1) {
  6226. if (status === 'ok') {
  6227. _this.invalidAuthTokenCount_ = 0;
  6228. }
  6229. else {
  6230. // Triggers reconnect and force refresh for auth token
  6231. _this.onAuthRevoked_(status, data);
  6232. }
  6233. }
  6234. });
  6235. }
  6236. };
  6237. /**
  6238. * Attempts to authenticate with the given token. If the authentication
  6239. * attempt fails, it's triggered like the token was revoked (the connection is
  6240. * closed).
  6241. */
  6242. PersistentConnection.prototype.tryAppCheck = function () {
  6243. var _this = this;
  6244. if (this.connected_ && this.appCheckToken_) {
  6245. this.sendRequest('appcheck', { 'token': this.appCheckToken_ }, function (res) {
  6246. var status = res[ /*status*/'s'];
  6247. var data = res[ /*data*/'d'] || 'error';
  6248. if (status === 'ok') {
  6249. _this.invalidAppCheckTokenCount_ = 0;
  6250. }
  6251. else {
  6252. _this.onAppCheckRevoked_(status, data);
  6253. }
  6254. });
  6255. }
  6256. };
  6257. /**
  6258. * @inheritDoc
  6259. */
  6260. PersistentConnection.prototype.unlisten = function (query, tag) {
  6261. var pathString = query._path.toString();
  6262. var queryId = query._queryIdentifier;
  6263. this.log_('Unlisten called for ' + pathString + ' ' + queryId);
  6264. util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'unlisten() called for non-default but complete query');
  6265. var listen = this.removeListen_(pathString, queryId);
  6266. if (listen && this.connected_) {
  6267. this.sendUnlisten_(pathString, queryId, query._queryObject, tag);
  6268. }
  6269. };
  6270. PersistentConnection.prototype.sendUnlisten_ = function (pathString, queryId, queryObj, tag) {
  6271. this.log_('Unlisten on ' + pathString + ' for ' + queryId);
  6272. var req = { /*path*/ p: pathString };
  6273. var action = 'n';
  6274. // Only bother sending queryId if it's non-default.
  6275. if (tag) {
  6276. req['q'] = queryObj;
  6277. req['t'] = tag;
  6278. }
  6279. this.sendRequest(action, req);
  6280. };
  6281. PersistentConnection.prototype.onDisconnectPut = function (pathString, data, onComplete) {
  6282. this.initConnection_();
  6283. if (this.connected_) {
  6284. this.sendOnDisconnect_('o', pathString, data, onComplete);
  6285. }
  6286. else {
  6287. this.onDisconnectRequestQueue_.push({
  6288. pathString: pathString,
  6289. action: 'o',
  6290. data: data,
  6291. onComplete: onComplete
  6292. });
  6293. }
  6294. };
  6295. PersistentConnection.prototype.onDisconnectMerge = function (pathString, data, onComplete) {
  6296. this.initConnection_();
  6297. if (this.connected_) {
  6298. this.sendOnDisconnect_('om', pathString, data, onComplete);
  6299. }
  6300. else {
  6301. this.onDisconnectRequestQueue_.push({
  6302. pathString: pathString,
  6303. action: 'om',
  6304. data: data,
  6305. onComplete: onComplete
  6306. });
  6307. }
  6308. };
  6309. PersistentConnection.prototype.onDisconnectCancel = function (pathString, onComplete) {
  6310. this.initConnection_();
  6311. if (this.connected_) {
  6312. this.sendOnDisconnect_('oc', pathString, null, onComplete);
  6313. }
  6314. else {
  6315. this.onDisconnectRequestQueue_.push({
  6316. pathString: pathString,
  6317. action: 'oc',
  6318. data: null,
  6319. onComplete: onComplete
  6320. });
  6321. }
  6322. };
  6323. PersistentConnection.prototype.sendOnDisconnect_ = function (action, pathString, data, onComplete) {
  6324. var request = { /*path*/ p: pathString, /*data*/ d: data };
  6325. this.log_('onDisconnect ' + action, request);
  6326. this.sendRequest(action, request, function (response) {
  6327. if (onComplete) {
  6328. setTimeout(function () {
  6329. onComplete(response[ /*status*/'s'], response[ /* data */'d']);
  6330. }, Math.floor(0));
  6331. }
  6332. });
  6333. };
  6334. PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
  6335. this.putInternal('p', pathString, data, onComplete, hash);
  6336. };
  6337. PersistentConnection.prototype.merge = function (pathString, data, onComplete, hash) {
  6338. this.putInternal('m', pathString, data, onComplete, hash);
  6339. };
  6340. PersistentConnection.prototype.putInternal = function (action, pathString, data, onComplete, hash) {
  6341. this.initConnection_();
  6342. var request = {
  6343. /*path*/ p: pathString,
  6344. /*data*/ d: data
  6345. };
  6346. if (hash !== undefined) {
  6347. request[ /*hash*/'h'] = hash;
  6348. }
  6349. // TODO: Only keep track of the most recent put for a given path?
  6350. this.outstandingPuts_.push({
  6351. action: action,
  6352. request: request,
  6353. onComplete: onComplete
  6354. });
  6355. this.outstandingPutCount_++;
  6356. var index = this.outstandingPuts_.length - 1;
  6357. if (this.connected_) {
  6358. this.sendPut_(index);
  6359. }
  6360. else {
  6361. this.log_('Buffering put: ' + pathString);
  6362. }
  6363. };
  6364. PersistentConnection.prototype.sendPut_ = function (index) {
  6365. var _this = this;
  6366. var action = this.outstandingPuts_[index].action;
  6367. var request = this.outstandingPuts_[index].request;
  6368. var onComplete = this.outstandingPuts_[index].onComplete;
  6369. this.outstandingPuts_[index].queued = this.connected_;
  6370. this.sendRequest(action, request, function (message) {
  6371. _this.log_(action + ' response', message);
  6372. delete _this.outstandingPuts_[index];
  6373. _this.outstandingPutCount_--;
  6374. // Clean up array occasionally.
  6375. if (_this.outstandingPutCount_ === 0) {
  6376. _this.outstandingPuts_ = [];
  6377. }
  6378. if (onComplete) {
  6379. onComplete(message[ /*status*/'s'], message[ /* data */'d']);
  6380. }
  6381. });
  6382. };
  6383. PersistentConnection.prototype.reportStats = function (stats) {
  6384. var _this = this;
  6385. // If we're not connected, we just drop the stats.
  6386. if (this.connected_) {
  6387. var request = { /*counters*/ c: stats };
  6388. this.log_('reportStats', request);
  6389. this.sendRequest(/*stats*/ 's', request, function (result) {
  6390. var status = result[ /*status*/'s'];
  6391. if (status !== 'ok') {
  6392. var errorReason = result[ /* data */'d'];
  6393. _this.log_('reportStats', 'Error sending stats: ' + errorReason);
  6394. }
  6395. });
  6396. }
  6397. };
  6398. PersistentConnection.prototype.onDataMessage_ = function (message) {
  6399. if ('r' in message) {
  6400. // this is a response
  6401. this.log_('from server: ' + util.stringify(message));
  6402. var reqNum = message['r'];
  6403. var onResponse = this.requestCBHash_[reqNum];
  6404. if (onResponse) {
  6405. delete this.requestCBHash_[reqNum];
  6406. onResponse(message[ /*body*/'b']);
  6407. }
  6408. }
  6409. else if ('error' in message) {
  6410. throw 'A server-side error has occurred: ' + message['error'];
  6411. }
  6412. else if ('a' in message) {
  6413. // a and b are action and body, respectively
  6414. this.onDataPush_(message['a'], message['b']);
  6415. }
  6416. };
  6417. PersistentConnection.prototype.onDataPush_ = function (action, body) {
  6418. this.log_('handleServerMessage', action, body);
  6419. if (action === 'd') {
  6420. this.onDataUpdate_(body[ /*path*/'p'], body[ /*data*/'d'],
  6421. /*isMerge*/ false, body['t']);
  6422. }
  6423. else if (action === 'm') {
  6424. this.onDataUpdate_(body[ /*path*/'p'], body[ /*data*/'d'],
  6425. /*isMerge=*/ true, body['t']);
  6426. }
  6427. else if (action === 'c') {
  6428. this.onListenRevoked_(body[ /*path*/'p'], body[ /*query*/'q']);
  6429. }
  6430. else if (action === 'ac') {
  6431. this.onAuthRevoked_(body[ /*status code*/'s'], body[ /* explanation */'d']);
  6432. }
  6433. else if (action === 'apc') {
  6434. this.onAppCheckRevoked_(body[ /*status code*/'s'], body[ /* explanation */'d']);
  6435. }
  6436. else if (action === 'sd') {
  6437. this.onSecurityDebugPacket_(body);
  6438. }
  6439. else {
  6440. error('Unrecognized action received from server: ' +
  6441. util.stringify(action) +
  6442. '\nAre you using the latest client?');
  6443. }
  6444. };
  6445. PersistentConnection.prototype.onReady_ = function (timestamp, sessionId) {
  6446. this.log_('connection ready');
  6447. this.connected_ = true;
  6448. this.lastConnectionEstablishedTime_ = new Date().getTime();
  6449. this.handleTimestamp_(timestamp);
  6450. this.lastSessionId = sessionId;
  6451. if (this.firstConnection_) {
  6452. this.sendConnectStats_();
  6453. }
  6454. this.restoreState_();
  6455. this.firstConnection_ = false;
  6456. this.onConnectStatus_(true);
  6457. };
  6458. PersistentConnection.prototype.scheduleConnect_ = function (timeout) {
  6459. var _this = this;
  6460. util.assert(!this.realtime_, "Scheduling a connect when we're already connected/ing?");
  6461. if (this.establishConnectionTimer_) {
  6462. clearTimeout(this.establishConnectionTimer_);
  6463. }
  6464. // NOTE: Even when timeout is 0, it's important to do a setTimeout to work around an infuriating "Security Error" in
  6465. // Firefox when trying to write to our long-polling iframe in some scenarios (e.g. Forge or our unit tests).
  6466. this.establishConnectionTimer_ = setTimeout(function () {
  6467. _this.establishConnectionTimer_ = null;
  6468. _this.establishConnection_();
  6469. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6470. }, Math.floor(timeout));
  6471. };
  6472. PersistentConnection.prototype.initConnection_ = function () {
  6473. if (!this.realtime_ && this.firstConnection_) {
  6474. this.scheduleConnect_(0);
  6475. }
  6476. };
  6477. PersistentConnection.prototype.onVisible_ = function (visible) {
  6478. // NOTE: Tabbing away and back to a window will defeat our reconnect backoff, but I think that's fine.
  6479. if (visible &&
  6480. !this.visible_ &&
  6481. this.reconnectDelay_ === this.maxReconnectDelay_) {
  6482. this.log_('Window became visible. Reducing delay.');
  6483. this.reconnectDelay_ = RECONNECT_MIN_DELAY;
  6484. if (!this.realtime_) {
  6485. this.scheduleConnect_(0);
  6486. }
  6487. }
  6488. this.visible_ = visible;
  6489. };
  6490. PersistentConnection.prototype.onOnline_ = function (online) {
  6491. if (online) {
  6492. this.log_('Browser went online.');
  6493. this.reconnectDelay_ = RECONNECT_MIN_DELAY;
  6494. if (!this.realtime_) {
  6495. this.scheduleConnect_(0);
  6496. }
  6497. }
  6498. else {
  6499. this.log_('Browser went offline. Killing connection.');
  6500. if (this.realtime_) {
  6501. this.realtime_.close();
  6502. }
  6503. }
  6504. };
  6505. PersistentConnection.prototype.onRealtimeDisconnect_ = function () {
  6506. this.log_('data client disconnected');
  6507. this.connected_ = false;
  6508. this.realtime_ = null;
  6509. // Since we don't know if our sent transactions succeeded or not, we need to cancel them.
  6510. this.cancelSentTransactions_();
  6511. // Clear out the pending requests.
  6512. this.requestCBHash_ = {};
  6513. if (this.shouldReconnect_()) {
  6514. if (!this.visible_) {
  6515. this.log_("Window isn't visible. Delaying reconnect.");
  6516. this.reconnectDelay_ = this.maxReconnectDelay_;
  6517. this.lastConnectionAttemptTime_ = new Date().getTime();
  6518. }
  6519. else if (this.lastConnectionEstablishedTime_) {
  6520. // If we've been connected long enough, reset reconnect delay to minimum.
  6521. var timeSinceLastConnectSucceeded = new Date().getTime() - this.lastConnectionEstablishedTime_;
  6522. if (timeSinceLastConnectSucceeded > RECONNECT_DELAY_RESET_TIMEOUT) {
  6523. this.reconnectDelay_ = RECONNECT_MIN_DELAY;
  6524. }
  6525. this.lastConnectionEstablishedTime_ = null;
  6526. }
  6527. var timeSinceLastConnectAttempt = new Date().getTime() - this.lastConnectionAttemptTime_;
  6528. var reconnectDelay = Math.max(0, this.reconnectDelay_ - timeSinceLastConnectAttempt);
  6529. reconnectDelay = Math.random() * reconnectDelay;
  6530. this.log_('Trying to reconnect in ' + reconnectDelay + 'ms');
  6531. this.scheduleConnect_(reconnectDelay);
  6532. // Adjust reconnect delay for next time.
  6533. this.reconnectDelay_ = Math.min(this.maxReconnectDelay_, this.reconnectDelay_ * RECONNECT_DELAY_MULTIPLIER);
  6534. }
  6535. this.onConnectStatus_(false);
  6536. };
  6537. PersistentConnection.prototype.establishConnection_ = function () {
  6538. return tslib.__awaiter(this, void 0, void 0, function () {
  6539. var onDataMessage, onReady, onDisconnect_1, connId, lastSessionId, canceled_1, connection_1, closeFn, sendRequestFn, forceRefresh, _a, authToken, appCheckToken, error_1;
  6540. var _this = this;
  6541. return tslib.__generator(this, function (_b) {
  6542. switch (_b.label) {
  6543. case 0:
  6544. if (!this.shouldReconnect_()) return [3 /*break*/, 4];
  6545. this.log_('Making a connection attempt');
  6546. this.lastConnectionAttemptTime_ = new Date().getTime();
  6547. this.lastConnectionEstablishedTime_ = null;
  6548. onDataMessage = this.onDataMessage_.bind(this);
  6549. onReady = this.onReady_.bind(this);
  6550. onDisconnect_1 = this.onRealtimeDisconnect_.bind(this);
  6551. connId = this.id + ':' + PersistentConnection.nextConnectionId_++;
  6552. lastSessionId = this.lastSessionId;
  6553. canceled_1 = false;
  6554. connection_1 = null;
  6555. closeFn = function () {
  6556. if (connection_1) {
  6557. connection_1.close();
  6558. }
  6559. else {
  6560. canceled_1 = true;
  6561. onDisconnect_1();
  6562. }
  6563. };
  6564. sendRequestFn = function (msg) {
  6565. util.assert(connection_1, "sendRequest call when we're not connected not allowed.");
  6566. connection_1.sendRequest(msg);
  6567. };
  6568. this.realtime_ = {
  6569. close: closeFn,
  6570. sendRequest: sendRequestFn
  6571. };
  6572. forceRefresh = this.forceTokenRefresh_;
  6573. this.forceTokenRefresh_ = false;
  6574. _b.label = 1;
  6575. case 1:
  6576. _b.trys.push([1, 3, , 4]);
  6577. return [4 /*yield*/, Promise.all([
  6578. this.authTokenProvider_.getToken(forceRefresh),
  6579. this.appCheckTokenProvider_.getToken(forceRefresh)
  6580. ])];
  6581. case 2:
  6582. _a = tslib.__read.apply(void 0, [_b.sent(), 2]), authToken = _a[0], appCheckToken = _a[1];
  6583. if (!canceled_1) {
  6584. log('getToken() completed. Creating connection.');
  6585. this.authToken_ = authToken && authToken.accessToken;
  6586. this.appCheckToken_ = appCheckToken && appCheckToken.token;
  6587. connection_1 = new Connection(connId, this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, onDataMessage, onReady, onDisconnect_1,
  6588. /* onKill= */ function (reason) {
  6589. warn$1(reason + ' (' + _this.repoInfo_.toString() + ')');
  6590. _this.interrupt(SERVER_KILL_INTERRUPT_REASON);
  6591. }, lastSessionId);
  6592. }
  6593. else {
  6594. log('getToken() completed but was canceled');
  6595. }
  6596. return [3 /*break*/, 4];
  6597. case 3:
  6598. error_1 = _b.sent();
  6599. this.log_('Failed to get token: ' + error_1);
  6600. if (!canceled_1) {
  6601. if (this.repoInfo_.nodeAdmin) {
  6602. // This may be a critical error for the Admin Node.js SDK, so log a warning.
  6603. // But getToken() may also just have temporarily failed, so we still want to
  6604. // continue retrying.
  6605. warn$1(error_1);
  6606. }
  6607. closeFn();
  6608. }
  6609. return [3 /*break*/, 4];
  6610. case 4: return [2 /*return*/];
  6611. }
  6612. });
  6613. });
  6614. };
  6615. PersistentConnection.prototype.interrupt = function (reason) {
  6616. log('Interrupting connection for reason: ' + reason);
  6617. this.interruptReasons_[reason] = true;
  6618. if (this.realtime_) {
  6619. this.realtime_.close();
  6620. }
  6621. else {
  6622. if (this.establishConnectionTimer_) {
  6623. clearTimeout(this.establishConnectionTimer_);
  6624. this.establishConnectionTimer_ = null;
  6625. }
  6626. if (this.connected_) {
  6627. this.onRealtimeDisconnect_();
  6628. }
  6629. }
  6630. };
  6631. PersistentConnection.prototype.resume = function (reason) {
  6632. log('Resuming connection for reason: ' + reason);
  6633. delete this.interruptReasons_[reason];
  6634. if (util.isEmpty(this.interruptReasons_)) {
  6635. this.reconnectDelay_ = RECONNECT_MIN_DELAY;
  6636. if (!this.realtime_) {
  6637. this.scheduleConnect_(0);
  6638. }
  6639. }
  6640. };
  6641. PersistentConnection.prototype.handleTimestamp_ = function (timestamp) {
  6642. var delta = timestamp - new Date().getTime();
  6643. this.onServerInfoUpdate_({ serverTimeOffset: delta });
  6644. };
  6645. PersistentConnection.prototype.cancelSentTransactions_ = function () {
  6646. for (var i = 0; i < this.outstandingPuts_.length; i++) {
  6647. var put = this.outstandingPuts_[i];
  6648. if (put && /*hash*/ 'h' in put.request && put.queued) {
  6649. if (put.onComplete) {
  6650. put.onComplete('disconnect');
  6651. }
  6652. delete this.outstandingPuts_[i];
  6653. this.outstandingPutCount_--;
  6654. }
  6655. }
  6656. // Clean up array occasionally.
  6657. if (this.outstandingPutCount_ === 0) {
  6658. this.outstandingPuts_ = [];
  6659. }
  6660. };
  6661. PersistentConnection.prototype.onListenRevoked_ = function (pathString, query) {
  6662. // Remove the listen and manufacture a "permission_denied" error for the failed listen.
  6663. var queryId;
  6664. if (!query) {
  6665. queryId = 'default';
  6666. }
  6667. else {
  6668. queryId = query.map(function (q) { return ObjectToUniqueKey(q); }).join('$');
  6669. }
  6670. var listen = this.removeListen_(pathString, queryId);
  6671. if (listen && listen.onComplete) {
  6672. listen.onComplete('permission_denied');
  6673. }
  6674. };
  6675. PersistentConnection.prototype.removeListen_ = function (pathString, queryId) {
  6676. var normalizedPathString = new Path(pathString).toString(); // normalize path.
  6677. var listen;
  6678. if (this.listens.has(normalizedPathString)) {
  6679. var map = this.listens.get(normalizedPathString);
  6680. listen = map.get(queryId);
  6681. map.delete(queryId);
  6682. if (map.size === 0) {
  6683. this.listens.delete(normalizedPathString);
  6684. }
  6685. }
  6686. else {
  6687. // all listens for this path has already been removed
  6688. listen = undefined;
  6689. }
  6690. return listen;
  6691. };
  6692. PersistentConnection.prototype.onAuthRevoked_ = function (statusCode, explanation) {
  6693. log('Auth token revoked: ' + statusCode + '/' + explanation);
  6694. this.authToken_ = null;
  6695. this.forceTokenRefresh_ = true;
  6696. this.realtime_.close();
  6697. if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
  6698. // We'll wait a couple times before logging the warning / increasing the
  6699. // retry period since oauth tokens will report as "invalid" if they're
  6700. // just expired. Plus there may be transient issues that resolve themselves.
  6701. this.invalidAuthTokenCount_++;
  6702. if (this.invalidAuthTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
  6703. // Set a long reconnect delay because recovery is unlikely
  6704. this.reconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
  6705. // Notify the auth token provider that the token is invalid, which will log
  6706. // a warning
  6707. this.authTokenProvider_.notifyForInvalidToken();
  6708. }
  6709. }
  6710. };
  6711. PersistentConnection.prototype.onAppCheckRevoked_ = function (statusCode, explanation) {
  6712. log('App check token revoked: ' + statusCode + '/' + explanation);
  6713. this.appCheckToken_ = null;
  6714. this.forceTokenRefresh_ = true;
  6715. // Note: We don't close the connection as the developer may not have
  6716. // enforcement enabled. The backend closes connections with enforcements.
  6717. if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
  6718. // We'll wait a couple times before logging the warning / increasing the
  6719. // retry period since oauth tokens will report as "invalid" if they're
  6720. // just expired. Plus there may be transient issues that resolve themselves.
  6721. this.invalidAppCheckTokenCount_++;
  6722. if (this.invalidAppCheckTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
  6723. this.appCheckTokenProvider_.notifyForInvalidToken();
  6724. }
  6725. }
  6726. };
  6727. PersistentConnection.prototype.onSecurityDebugPacket_ = function (body) {
  6728. if (this.securityDebugCallback_) {
  6729. this.securityDebugCallback_(body);
  6730. }
  6731. else {
  6732. if ('msg' in body) {
  6733. console.log('FIREBASE: ' + body['msg'].replace('\n', '\nFIREBASE: '));
  6734. }
  6735. }
  6736. };
  6737. PersistentConnection.prototype.restoreState_ = function () {
  6738. var e_1, _a, e_2, _b;
  6739. //Re-authenticate ourselves if we have a credential stored.
  6740. this.tryAuth();
  6741. this.tryAppCheck();
  6742. try {
  6743. // Puts depend on having received the corresponding data update from the server before they complete, so we must
  6744. // make sure to send listens before puts.
  6745. for (var _c = tslib.__values(this.listens.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
  6746. var queries = _d.value;
  6747. try {
  6748. for (var _e = (e_2 = void 0, tslib.__values(queries.values())), _f = _e.next(); !_f.done; _f = _e.next()) {
  6749. var listenSpec = _f.value;
  6750. this.sendListen_(listenSpec);
  6751. }
  6752. }
  6753. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  6754. finally {
  6755. try {
  6756. if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
  6757. }
  6758. finally { if (e_2) throw e_2.error; }
  6759. }
  6760. }
  6761. }
  6762. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  6763. finally {
  6764. try {
  6765. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  6766. }
  6767. finally { if (e_1) throw e_1.error; }
  6768. }
  6769. for (var i = 0; i < this.outstandingPuts_.length; i++) {
  6770. if (this.outstandingPuts_[i]) {
  6771. this.sendPut_(i);
  6772. }
  6773. }
  6774. while (this.onDisconnectRequestQueue_.length) {
  6775. var request = this.onDisconnectRequestQueue_.shift();
  6776. this.sendOnDisconnect_(request.action, request.pathString, request.data, request.onComplete);
  6777. }
  6778. for (var i = 0; i < this.outstandingGets_.length; i++) {
  6779. if (this.outstandingGets_[i]) {
  6780. this.sendGet_(i);
  6781. }
  6782. }
  6783. };
  6784. /**
  6785. * Sends client stats for first connection
  6786. */
  6787. PersistentConnection.prototype.sendConnectStats_ = function () {
  6788. var stats = {};
  6789. var clientName = 'js';
  6790. if (util.isNodeSdk()) {
  6791. if (this.repoInfo_.nodeAdmin) {
  6792. clientName = 'admin_node';
  6793. }
  6794. else {
  6795. clientName = 'node';
  6796. }
  6797. }
  6798. stats['sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-')] = 1;
  6799. if (util.isMobileCordova()) {
  6800. stats['framework.cordova'] = 1;
  6801. }
  6802. else if (util.isReactNative()) {
  6803. stats['framework.reactnative'] = 1;
  6804. }
  6805. this.reportStats(stats);
  6806. };
  6807. PersistentConnection.prototype.shouldReconnect_ = function () {
  6808. var online = OnlineMonitor.getInstance().currentlyOnline();
  6809. return util.isEmpty(this.interruptReasons_) && online;
  6810. };
  6811. PersistentConnection.nextPersistentConnectionId_ = 0;
  6812. /**
  6813. * Counter for number of connections created. Mainly used for tagging in the logs
  6814. */
  6815. PersistentConnection.nextConnectionId_ = 0;
  6816. return PersistentConnection;
  6817. }(ServerActions));
  6818. /**
  6819. * @license
  6820. * Copyright 2017 Google LLC
  6821. *
  6822. * Licensed under the Apache License, Version 2.0 (the "License");
  6823. * you may not use this file except in compliance with the License.
  6824. * You may obtain a copy of the License at
  6825. *
  6826. * http://www.apache.org/licenses/LICENSE-2.0
  6827. *
  6828. * Unless required by applicable law or agreed to in writing, software
  6829. * distributed under the License is distributed on an "AS IS" BASIS,
  6830. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6831. * See the License for the specific language governing permissions and
  6832. * limitations under the License.
  6833. */
  6834. var NamedNode = /** @class */ (function () {
  6835. function NamedNode(name, node) {
  6836. this.name = name;
  6837. this.node = node;
  6838. }
  6839. NamedNode.Wrap = function (name, node) {
  6840. return new NamedNode(name, node);
  6841. };
  6842. return NamedNode;
  6843. }());
  6844. /**
  6845. * @license
  6846. * Copyright 2017 Google LLC
  6847. *
  6848. * Licensed under the Apache License, Version 2.0 (the "License");
  6849. * you may not use this file except in compliance with the License.
  6850. * You may obtain a copy of the License at
  6851. *
  6852. * http://www.apache.org/licenses/LICENSE-2.0
  6853. *
  6854. * Unless required by applicable law or agreed to in writing, software
  6855. * distributed under the License is distributed on an "AS IS" BASIS,
  6856. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6857. * See the License for the specific language governing permissions and
  6858. * limitations under the License.
  6859. */
  6860. var Index = /** @class */ (function () {
  6861. function Index() {
  6862. }
  6863. /**
  6864. * @returns A standalone comparison function for
  6865. * this index
  6866. */
  6867. Index.prototype.getCompare = function () {
  6868. return this.compare.bind(this);
  6869. };
  6870. /**
  6871. * Given a before and after value for a node, determine if the indexed value has changed. Even if they are different,
  6872. * it's possible that the changes are isolated to parts of the snapshot that are not indexed.
  6873. *
  6874. *
  6875. * @returns True if the portion of the snapshot being indexed changed between oldNode and newNode
  6876. */
  6877. Index.prototype.indexedValueChanged = function (oldNode, newNode) {
  6878. var oldWrapped = new NamedNode(MIN_NAME, oldNode);
  6879. var newWrapped = new NamedNode(MIN_NAME, newNode);
  6880. return this.compare(oldWrapped, newWrapped) !== 0;
  6881. };
  6882. /**
  6883. * @returns a node wrapper that will sort equal to or less than
  6884. * any other node wrapper, using this index
  6885. */
  6886. Index.prototype.minPost = function () {
  6887. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6888. return NamedNode.MIN;
  6889. };
  6890. return Index;
  6891. }());
  6892. /**
  6893. * @license
  6894. * Copyright 2017 Google LLC
  6895. *
  6896. * Licensed under the Apache License, Version 2.0 (the "License");
  6897. * you may not use this file except in compliance with the License.
  6898. * You may obtain a copy of the License at
  6899. *
  6900. * http://www.apache.org/licenses/LICENSE-2.0
  6901. *
  6902. * Unless required by applicable law or agreed to in writing, software
  6903. * distributed under the License is distributed on an "AS IS" BASIS,
  6904. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6905. * See the License for the specific language governing permissions and
  6906. * limitations under the License.
  6907. */
  6908. var __EMPTY_NODE;
  6909. var KeyIndex = /** @class */ (function (_super) {
  6910. tslib.__extends(KeyIndex, _super);
  6911. function KeyIndex() {
  6912. return _super !== null && _super.apply(this, arguments) || this;
  6913. }
  6914. Object.defineProperty(KeyIndex, "__EMPTY_NODE", {
  6915. get: function () {
  6916. return __EMPTY_NODE;
  6917. },
  6918. set: function (val) {
  6919. __EMPTY_NODE = val;
  6920. },
  6921. enumerable: false,
  6922. configurable: true
  6923. });
  6924. KeyIndex.prototype.compare = function (a, b) {
  6925. return nameCompare(a.name, b.name);
  6926. };
  6927. KeyIndex.prototype.isDefinedOn = function (node) {
  6928. // We could probably return true here (since every node has a key), but it's never called
  6929. // so just leaving unimplemented for now.
  6930. throw util.assertionError('KeyIndex.isDefinedOn not expected to be called.');
  6931. };
  6932. KeyIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
  6933. return false; // The key for a node never changes.
  6934. };
  6935. KeyIndex.prototype.minPost = function () {
  6936. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6937. return NamedNode.MIN;
  6938. };
  6939. KeyIndex.prototype.maxPost = function () {
  6940. // TODO: This should really be created once and cached in a static property, but
  6941. // NamedNode isn't defined yet, so I can't use it in a static. Bleh.
  6942. return new NamedNode(MAX_NAME, __EMPTY_NODE);
  6943. };
  6944. KeyIndex.prototype.makePost = function (indexValue, name) {
  6945. util.assert(typeof indexValue === 'string', 'KeyIndex indexValue must always be a string.');
  6946. // We just use empty node, but it'll never be compared, since our comparator only looks at name.
  6947. return new NamedNode(indexValue, __EMPTY_NODE);
  6948. };
  6949. /**
  6950. * @returns String representation for inclusion in a query spec
  6951. */
  6952. KeyIndex.prototype.toString = function () {
  6953. return '.key';
  6954. };
  6955. return KeyIndex;
  6956. }(Index));
  6957. var KEY_INDEX = new KeyIndex();
  6958. /**
  6959. * @license
  6960. * Copyright 2017 Google LLC
  6961. *
  6962. * Licensed under the Apache License, Version 2.0 (the "License");
  6963. * you may not use this file except in compliance with the License.
  6964. * You may obtain a copy of the License at
  6965. *
  6966. * http://www.apache.org/licenses/LICENSE-2.0
  6967. *
  6968. * Unless required by applicable law or agreed to in writing, software
  6969. * distributed under the License is distributed on an "AS IS" BASIS,
  6970. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6971. * See the License for the specific language governing permissions and
  6972. * limitations under the License.
  6973. */
  6974. /**
  6975. * An iterator over an LLRBNode.
  6976. */
  6977. var SortedMapIterator = /** @class */ (function () {
  6978. /**
  6979. * @param node - Node to iterate.
  6980. * @param isReverse_ - Whether or not to iterate in reverse
  6981. */
  6982. function SortedMapIterator(node, startKey, comparator, isReverse_, resultGenerator_) {
  6983. if (resultGenerator_ === void 0) { resultGenerator_ = null; }
  6984. this.isReverse_ = isReverse_;
  6985. this.resultGenerator_ = resultGenerator_;
  6986. this.nodeStack_ = [];
  6987. var cmp = 1;
  6988. while (!node.isEmpty()) {
  6989. node = node;
  6990. cmp = startKey ? comparator(node.key, startKey) : 1;
  6991. // flip the comparison if we're going in reverse
  6992. if (isReverse_) {
  6993. cmp *= -1;
  6994. }
  6995. if (cmp < 0) {
  6996. // This node is less than our start key. ignore it
  6997. if (this.isReverse_) {
  6998. node = node.left;
  6999. }
  7000. else {
  7001. node = node.right;
  7002. }
  7003. }
  7004. else if (cmp === 0) {
  7005. // This node is exactly equal to our start key. Push it on the stack, but stop iterating;
  7006. this.nodeStack_.push(node);
  7007. break;
  7008. }
  7009. else {
  7010. // This node is greater than our start key, add it to the stack and move to the next one
  7011. this.nodeStack_.push(node);
  7012. if (this.isReverse_) {
  7013. node = node.right;
  7014. }
  7015. else {
  7016. node = node.left;
  7017. }
  7018. }
  7019. }
  7020. }
  7021. SortedMapIterator.prototype.getNext = function () {
  7022. if (this.nodeStack_.length === 0) {
  7023. return null;
  7024. }
  7025. var node = this.nodeStack_.pop();
  7026. var result;
  7027. if (this.resultGenerator_) {
  7028. result = this.resultGenerator_(node.key, node.value);
  7029. }
  7030. else {
  7031. result = { key: node.key, value: node.value };
  7032. }
  7033. if (this.isReverse_) {
  7034. node = node.left;
  7035. while (!node.isEmpty()) {
  7036. this.nodeStack_.push(node);
  7037. node = node.right;
  7038. }
  7039. }
  7040. else {
  7041. node = node.right;
  7042. while (!node.isEmpty()) {
  7043. this.nodeStack_.push(node);
  7044. node = node.left;
  7045. }
  7046. }
  7047. return result;
  7048. };
  7049. SortedMapIterator.prototype.hasNext = function () {
  7050. return this.nodeStack_.length > 0;
  7051. };
  7052. SortedMapIterator.prototype.peek = function () {
  7053. if (this.nodeStack_.length === 0) {
  7054. return null;
  7055. }
  7056. var node = this.nodeStack_[this.nodeStack_.length - 1];
  7057. if (this.resultGenerator_) {
  7058. return this.resultGenerator_(node.key, node.value);
  7059. }
  7060. else {
  7061. return { key: node.key, value: node.value };
  7062. }
  7063. };
  7064. return SortedMapIterator;
  7065. }());
  7066. /**
  7067. * Represents a node in a Left-leaning Red-Black tree.
  7068. */
  7069. var LLRBNode = /** @class */ (function () {
  7070. /**
  7071. * @param key - Key associated with this node.
  7072. * @param value - Value associated with this node.
  7073. * @param color - Whether this node is red.
  7074. * @param left - Left child.
  7075. * @param right - Right child.
  7076. */
  7077. function LLRBNode(key, value, color, left, right) {
  7078. this.key = key;
  7079. this.value = value;
  7080. this.color = color != null ? color : LLRBNode.RED;
  7081. this.left =
  7082. left != null ? left : SortedMap.EMPTY_NODE;
  7083. this.right =
  7084. right != null ? right : SortedMap.EMPTY_NODE;
  7085. }
  7086. /**
  7087. * Returns a copy of the current node, optionally replacing pieces of it.
  7088. *
  7089. * @param key - New key for the node, or null.
  7090. * @param value - New value for the node, or null.
  7091. * @param color - New color for the node, or null.
  7092. * @param left - New left child for the node, or null.
  7093. * @param right - New right child for the node, or null.
  7094. * @returns The node copy.
  7095. */
  7096. LLRBNode.prototype.copy = function (key, value, color, left, right) {
  7097. return new LLRBNode(key != null ? key : this.key, value != null ? value : this.value, color != null ? color : this.color, left != null ? left : this.left, right != null ? right : this.right);
  7098. };
  7099. /**
  7100. * @returns The total number of nodes in the tree.
  7101. */
  7102. LLRBNode.prototype.count = function () {
  7103. return this.left.count() + 1 + this.right.count();
  7104. };
  7105. /**
  7106. * @returns True if the tree is empty.
  7107. */
  7108. LLRBNode.prototype.isEmpty = function () {
  7109. return false;
  7110. };
  7111. /**
  7112. * Traverses the tree in key order and calls the specified action function
  7113. * for each node.
  7114. *
  7115. * @param action - Callback function to be called for each
  7116. * node. If it returns true, traversal is aborted.
  7117. * @returns The first truthy value returned by action, or the last falsey
  7118. * value returned by action
  7119. */
  7120. LLRBNode.prototype.inorderTraversal = function (action) {
  7121. return (this.left.inorderTraversal(action) ||
  7122. !!action(this.key, this.value) ||
  7123. this.right.inorderTraversal(action));
  7124. };
  7125. /**
  7126. * Traverses the tree in reverse key order and calls the specified action function
  7127. * for each node.
  7128. *
  7129. * @param action - Callback function to be called for each
  7130. * node. If it returns true, traversal is aborted.
  7131. * @returns True if traversal was aborted.
  7132. */
  7133. LLRBNode.prototype.reverseTraversal = function (action) {
  7134. return (this.right.reverseTraversal(action) ||
  7135. action(this.key, this.value) ||
  7136. this.left.reverseTraversal(action));
  7137. };
  7138. /**
  7139. * @returns The minimum node in the tree.
  7140. */
  7141. LLRBNode.prototype.min_ = function () {
  7142. if (this.left.isEmpty()) {
  7143. return this;
  7144. }
  7145. else {
  7146. return this.left.min_();
  7147. }
  7148. };
  7149. /**
  7150. * @returns The maximum key in the tree.
  7151. */
  7152. LLRBNode.prototype.minKey = function () {
  7153. return this.min_().key;
  7154. };
  7155. /**
  7156. * @returns The maximum key in the tree.
  7157. */
  7158. LLRBNode.prototype.maxKey = function () {
  7159. if (this.right.isEmpty()) {
  7160. return this.key;
  7161. }
  7162. else {
  7163. return this.right.maxKey();
  7164. }
  7165. };
  7166. /**
  7167. * @param key - Key to insert.
  7168. * @param value - Value to insert.
  7169. * @param comparator - Comparator.
  7170. * @returns New tree, with the key/value added.
  7171. */
  7172. LLRBNode.prototype.insert = function (key, value, comparator) {
  7173. var n = this;
  7174. var cmp = comparator(key, n.key);
  7175. if (cmp < 0) {
  7176. n = n.copy(null, null, null, n.left.insert(key, value, comparator), null);
  7177. }
  7178. else if (cmp === 0) {
  7179. n = n.copy(null, value, null, null, null);
  7180. }
  7181. else {
  7182. n = n.copy(null, null, null, null, n.right.insert(key, value, comparator));
  7183. }
  7184. return n.fixUp_();
  7185. };
  7186. /**
  7187. * @returns New tree, with the minimum key removed.
  7188. */
  7189. LLRBNode.prototype.removeMin_ = function () {
  7190. if (this.left.isEmpty()) {
  7191. return SortedMap.EMPTY_NODE;
  7192. }
  7193. var n = this;
  7194. if (!n.left.isRed_() && !n.left.left.isRed_()) {
  7195. n = n.moveRedLeft_();
  7196. }
  7197. n = n.copy(null, null, null, n.left.removeMin_(), null);
  7198. return n.fixUp_();
  7199. };
  7200. /**
  7201. * @param key - The key of the item to remove.
  7202. * @param comparator - Comparator.
  7203. * @returns New tree, with the specified item removed.
  7204. */
  7205. LLRBNode.prototype.remove = function (key, comparator) {
  7206. var n, smallest;
  7207. n = this;
  7208. if (comparator(key, n.key) < 0) {
  7209. if (!n.left.isEmpty() && !n.left.isRed_() && !n.left.left.isRed_()) {
  7210. n = n.moveRedLeft_();
  7211. }
  7212. n = n.copy(null, null, null, n.left.remove(key, comparator), null);
  7213. }
  7214. else {
  7215. if (n.left.isRed_()) {
  7216. n = n.rotateRight_();
  7217. }
  7218. if (!n.right.isEmpty() && !n.right.isRed_() && !n.right.left.isRed_()) {
  7219. n = n.moveRedRight_();
  7220. }
  7221. if (comparator(key, n.key) === 0) {
  7222. if (n.right.isEmpty()) {
  7223. return SortedMap.EMPTY_NODE;
  7224. }
  7225. else {
  7226. smallest = n.right.min_();
  7227. n = n.copy(smallest.key, smallest.value, null, null, n.right.removeMin_());
  7228. }
  7229. }
  7230. n = n.copy(null, null, null, null, n.right.remove(key, comparator));
  7231. }
  7232. return n.fixUp_();
  7233. };
  7234. /**
  7235. * @returns Whether this is a RED node.
  7236. */
  7237. LLRBNode.prototype.isRed_ = function () {
  7238. return this.color;
  7239. };
  7240. /**
  7241. * @returns New tree after performing any needed rotations.
  7242. */
  7243. LLRBNode.prototype.fixUp_ = function () {
  7244. var n = this;
  7245. if (n.right.isRed_() && !n.left.isRed_()) {
  7246. n = n.rotateLeft_();
  7247. }
  7248. if (n.left.isRed_() && n.left.left.isRed_()) {
  7249. n = n.rotateRight_();
  7250. }
  7251. if (n.left.isRed_() && n.right.isRed_()) {
  7252. n = n.colorFlip_();
  7253. }
  7254. return n;
  7255. };
  7256. /**
  7257. * @returns New tree, after moveRedLeft.
  7258. */
  7259. LLRBNode.prototype.moveRedLeft_ = function () {
  7260. var n = this.colorFlip_();
  7261. if (n.right.left.isRed_()) {
  7262. n = n.copy(null, null, null, null, n.right.rotateRight_());
  7263. n = n.rotateLeft_();
  7264. n = n.colorFlip_();
  7265. }
  7266. return n;
  7267. };
  7268. /**
  7269. * @returns New tree, after moveRedRight.
  7270. */
  7271. LLRBNode.prototype.moveRedRight_ = function () {
  7272. var n = this.colorFlip_();
  7273. if (n.left.left.isRed_()) {
  7274. n = n.rotateRight_();
  7275. n = n.colorFlip_();
  7276. }
  7277. return n;
  7278. };
  7279. /**
  7280. * @returns New tree, after rotateLeft.
  7281. */
  7282. LLRBNode.prototype.rotateLeft_ = function () {
  7283. var nl = this.copy(null, null, LLRBNode.RED, null, this.right.left);
  7284. return this.right.copy(null, null, this.color, nl, null);
  7285. };
  7286. /**
  7287. * @returns New tree, after rotateRight.
  7288. */
  7289. LLRBNode.prototype.rotateRight_ = function () {
  7290. var nr = this.copy(null, null, LLRBNode.RED, this.left.right, null);
  7291. return this.left.copy(null, null, this.color, null, nr);
  7292. };
  7293. /**
  7294. * @returns Newt ree, after colorFlip.
  7295. */
  7296. LLRBNode.prototype.colorFlip_ = function () {
  7297. var left = this.left.copy(null, null, !this.left.color, null, null);
  7298. var right = this.right.copy(null, null, !this.right.color, null, null);
  7299. return this.copy(null, null, !this.color, left, right);
  7300. };
  7301. /**
  7302. * For testing.
  7303. *
  7304. * @returns True if all is well.
  7305. */
  7306. LLRBNode.prototype.checkMaxDepth_ = function () {
  7307. var blackDepth = this.check_();
  7308. return Math.pow(2.0, blackDepth) <= this.count() + 1;
  7309. };
  7310. LLRBNode.prototype.check_ = function () {
  7311. if (this.isRed_() && this.left.isRed_()) {
  7312. throw new Error('Red node has red child(' + this.key + ',' + this.value + ')');
  7313. }
  7314. if (this.right.isRed_()) {
  7315. throw new Error('Right child of (' + this.key + ',' + this.value + ') is red');
  7316. }
  7317. var blackDepth = this.left.check_();
  7318. if (blackDepth !== this.right.check_()) {
  7319. throw new Error('Black depths differ');
  7320. }
  7321. else {
  7322. return blackDepth + (this.isRed_() ? 0 : 1);
  7323. }
  7324. };
  7325. LLRBNode.RED = true;
  7326. LLRBNode.BLACK = false;
  7327. return LLRBNode;
  7328. }());
  7329. /**
  7330. * Represents an empty node (a leaf node in the Red-Black Tree).
  7331. */
  7332. var LLRBEmptyNode = /** @class */ (function () {
  7333. function LLRBEmptyNode() {
  7334. }
  7335. /**
  7336. * Returns a copy of the current node.
  7337. *
  7338. * @returns The node copy.
  7339. */
  7340. LLRBEmptyNode.prototype.copy = function (key, value, color, left, right) {
  7341. return this;
  7342. };
  7343. /**
  7344. * Returns a copy of the tree, with the specified key/value added.
  7345. *
  7346. * @param key - Key to be added.
  7347. * @param value - Value to be added.
  7348. * @param comparator - Comparator.
  7349. * @returns New tree, with item added.
  7350. */
  7351. LLRBEmptyNode.prototype.insert = function (key, value, comparator) {
  7352. return new LLRBNode(key, value, null);
  7353. };
  7354. /**
  7355. * Returns a copy of the tree, with the specified key removed.
  7356. *
  7357. * @param key - The key to remove.
  7358. * @param comparator - Comparator.
  7359. * @returns New tree, with item removed.
  7360. */
  7361. LLRBEmptyNode.prototype.remove = function (key, comparator) {
  7362. return this;
  7363. };
  7364. /**
  7365. * @returns The total number of nodes in the tree.
  7366. */
  7367. LLRBEmptyNode.prototype.count = function () {
  7368. return 0;
  7369. };
  7370. /**
  7371. * @returns True if the tree is empty.
  7372. */
  7373. LLRBEmptyNode.prototype.isEmpty = function () {
  7374. return true;
  7375. };
  7376. /**
  7377. * Traverses the tree in key order and calls the specified action function
  7378. * for each node.
  7379. *
  7380. * @param action - Callback function to be called for each
  7381. * node. If it returns true, traversal is aborted.
  7382. * @returns True if traversal was aborted.
  7383. */
  7384. LLRBEmptyNode.prototype.inorderTraversal = function (action) {
  7385. return false;
  7386. };
  7387. /**
  7388. * Traverses the tree in reverse key order and calls the specified action function
  7389. * for each node.
  7390. *
  7391. * @param action - Callback function to be called for each
  7392. * node. If it returns true, traversal is aborted.
  7393. * @returns True if traversal was aborted.
  7394. */
  7395. LLRBEmptyNode.prototype.reverseTraversal = function (action) {
  7396. return false;
  7397. };
  7398. LLRBEmptyNode.prototype.minKey = function () {
  7399. return null;
  7400. };
  7401. LLRBEmptyNode.prototype.maxKey = function () {
  7402. return null;
  7403. };
  7404. LLRBEmptyNode.prototype.check_ = function () {
  7405. return 0;
  7406. };
  7407. /**
  7408. * @returns Whether this node is red.
  7409. */
  7410. LLRBEmptyNode.prototype.isRed_ = function () {
  7411. return false;
  7412. };
  7413. return LLRBEmptyNode;
  7414. }());
  7415. /**
  7416. * An immutable sorted map implementation, based on a Left-leaning Red-Black
  7417. * tree.
  7418. */
  7419. var SortedMap = /** @class */ (function () {
  7420. /**
  7421. * @param comparator_ - Key comparator.
  7422. * @param root_ - Optional root node for the map.
  7423. */
  7424. function SortedMap(comparator_, root_) {
  7425. if (root_ === void 0) { root_ = SortedMap.EMPTY_NODE; }
  7426. this.comparator_ = comparator_;
  7427. this.root_ = root_;
  7428. }
  7429. /**
  7430. * Returns a copy of the map, with the specified key/value added or replaced.
  7431. * (TODO: We should perhaps rename this method to 'put')
  7432. *
  7433. * @param key - Key to be added.
  7434. * @param value - Value to be added.
  7435. * @returns New map, with item added.
  7436. */
  7437. SortedMap.prototype.insert = function (key, value) {
  7438. return new SortedMap(this.comparator_, this.root_
  7439. .insert(key, value, this.comparator_)
  7440. .copy(null, null, LLRBNode.BLACK, null, null));
  7441. };
  7442. /**
  7443. * Returns a copy of the map, with the specified key removed.
  7444. *
  7445. * @param key - The key to remove.
  7446. * @returns New map, with item removed.
  7447. */
  7448. SortedMap.prototype.remove = function (key) {
  7449. return new SortedMap(this.comparator_, this.root_
  7450. .remove(key, this.comparator_)
  7451. .copy(null, null, LLRBNode.BLACK, null, null));
  7452. };
  7453. /**
  7454. * Returns the value of the node with the given key, or null.
  7455. *
  7456. * @param key - The key to look up.
  7457. * @returns The value of the node with the given key, or null if the
  7458. * key doesn't exist.
  7459. */
  7460. SortedMap.prototype.get = function (key) {
  7461. var cmp;
  7462. var node = this.root_;
  7463. while (!node.isEmpty()) {
  7464. cmp = this.comparator_(key, node.key);
  7465. if (cmp === 0) {
  7466. return node.value;
  7467. }
  7468. else if (cmp < 0) {
  7469. node = node.left;
  7470. }
  7471. else if (cmp > 0) {
  7472. node = node.right;
  7473. }
  7474. }
  7475. return null;
  7476. };
  7477. /**
  7478. * Returns the key of the item *before* the specified key, or null if key is the first item.
  7479. * @param key - The key to find the predecessor of
  7480. * @returns The predecessor key.
  7481. */
  7482. SortedMap.prototype.getPredecessorKey = function (key) {
  7483. var cmp, node = this.root_, rightParent = null;
  7484. while (!node.isEmpty()) {
  7485. cmp = this.comparator_(key, node.key);
  7486. if (cmp === 0) {
  7487. if (!node.left.isEmpty()) {
  7488. node = node.left;
  7489. while (!node.right.isEmpty()) {
  7490. node = node.right;
  7491. }
  7492. return node.key;
  7493. }
  7494. else if (rightParent) {
  7495. return rightParent.key;
  7496. }
  7497. else {
  7498. return null; // first item.
  7499. }
  7500. }
  7501. else if (cmp < 0) {
  7502. node = node.left;
  7503. }
  7504. else if (cmp > 0) {
  7505. rightParent = node;
  7506. node = node.right;
  7507. }
  7508. }
  7509. throw new Error('Attempted to find predecessor key for a nonexistent key. What gives?');
  7510. };
  7511. /**
  7512. * @returns True if the map is empty.
  7513. */
  7514. SortedMap.prototype.isEmpty = function () {
  7515. return this.root_.isEmpty();
  7516. };
  7517. /**
  7518. * @returns The total number of nodes in the map.
  7519. */
  7520. SortedMap.prototype.count = function () {
  7521. return this.root_.count();
  7522. };
  7523. /**
  7524. * @returns The minimum key in the map.
  7525. */
  7526. SortedMap.prototype.minKey = function () {
  7527. return this.root_.minKey();
  7528. };
  7529. /**
  7530. * @returns The maximum key in the map.
  7531. */
  7532. SortedMap.prototype.maxKey = function () {
  7533. return this.root_.maxKey();
  7534. };
  7535. /**
  7536. * Traverses the map in key order and calls the specified action function
  7537. * for each key/value pair.
  7538. *
  7539. * @param action - Callback function to be called
  7540. * for each key/value pair. If action returns true, traversal is aborted.
  7541. * @returns The first truthy value returned by action, or the last falsey
  7542. * value returned by action
  7543. */
  7544. SortedMap.prototype.inorderTraversal = function (action) {
  7545. return this.root_.inorderTraversal(action);
  7546. };
  7547. /**
  7548. * Traverses the map in reverse key order and calls the specified action function
  7549. * for each key/value pair.
  7550. *
  7551. * @param action - Callback function to be called
  7552. * for each key/value pair. If action returns true, traversal is aborted.
  7553. * @returns True if the traversal was aborted.
  7554. */
  7555. SortedMap.prototype.reverseTraversal = function (action) {
  7556. return this.root_.reverseTraversal(action);
  7557. };
  7558. /**
  7559. * Returns an iterator over the SortedMap.
  7560. * @returns The iterator.
  7561. */
  7562. SortedMap.prototype.getIterator = function (resultGenerator) {
  7563. return new SortedMapIterator(this.root_, null, this.comparator_, false, resultGenerator);
  7564. };
  7565. SortedMap.prototype.getIteratorFrom = function (key, resultGenerator) {
  7566. return new SortedMapIterator(this.root_, key, this.comparator_, false, resultGenerator);
  7567. };
  7568. SortedMap.prototype.getReverseIteratorFrom = function (key, resultGenerator) {
  7569. return new SortedMapIterator(this.root_, key, this.comparator_, true, resultGenerator);
  7570. };
  7571. SortedMap.prototype.getReverseIterator = function (resultGenerator) {
  7572. return new SortedMapIterator(this.root_, null, this.comparator_, true, resultGenerator);
  7573. };
  7574. /**
  7575. * Always use the same empty node, to reduce memory.
  7576. */
  7577. SortedMap.EMPTY_NODE = new LLRBEmptyNode();
  7578. return SortedMap;
  7579. }());
  7580. /**
  7581. * @license
  7582. * Copyright 2017 Google LLC
  7583. *
  7584. * Licensed under the Apache License, Version 2.0 (the "License");
  7585. * you may not use this file except in compliance with the License.
  7586. * You may obtain a copy of the License at
  7587. *
  7588. * http://www.apache.org/licenses/LICENSE-2.0
  7589. *
  7590. * Unless required by applicable law or agreed to in writing, software
  7591. * distributed under the License is distributed on an "AS IS" BASIS,
  7592. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7593. * See the License for the specific language governing permissions and
  7594. * limitations under the License.
  7595. */
  7596. function NAME_ONLY_COMPARATOR(left, right) {
  7597. return nameCompare(left.name, right.name);
  7598. }
  7599. function NAME_COMPARATOR(left, right) {
  7600. return nameCompare(left, right);
  7601. }
  7602. /**
  7603. * @license
  7604. * Copyright 2017 Google LLC
  7605. *
  7606. * Licensed under the Apache License, Version 2.0 (the "License");
  7607. * you may not use this file except in compliance with the License.
  7608. * You may obtain a copy of the License at
  7609. *
  7610. * http://www.apache.org/licenses/LICENSE-2.0
  7611. *
  7612. * Unless required by applicable law or agreed to in writing, software
  7613. * distributed under the License is distributed on an "AS IS" BASIS,
  7614. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7615. * See the License for the specific language governing permissions and
  7616. * limitations under the License.
  7617. */
  7618. var MAX_NODE$2;
  7619. function setMaxNode$1(val) {
  7620. MAX_NODE$2 = val;
  7621. }
  7622. var priorityHashText = function (priority) {
  7623. if (typeof priority === 'number') {
  7624. return 'number:' + doubleToIEEE754String(priority);
  7625. }
  7626. else {
  7627. return 'string:' + priority;
  7628. }
  7629. };
  7630. /**
  7631. * Validates that a priority snapshot Node is valid.
  7632. */
  7633. var validatePriorityNode = function (priorityNode) {
  7634. if (priorityNode.isLeafNode()) {
  7635. var val = priorityNode.val();
  7636. util.assert(typeof val === 'string' ||
  7637. typeof val === 'number' ||
  7638. (typeof val === 'object' && util.contains(val, '.sv')), 'Priority must be a string or number.');
  7639. }
  7640. else {
  7641. util.assert(priorityNode === MAX_NODE$2 || priorityNode.isEmpty(), 'priority of unexpected type.');
  7642. }
  7643. // Don't call getPriority() on MAX_NODE to avoid hitting assertion.
  7644. util.assert(priorityNode === MAX_NODE$2 || priorityNode.getPriority().isEmpty(), "Priority nodes can't have a priority of their own.");
  7645. };
  7646. /**
  7647. * @license
  7648. * Copyright 2017 Google LLC
  7649. *
  7650. * Licensed under the Apache License, Version 2.0 (the "License");
  7651. * you may not use this file except in compliance with the License.
  7652. * You may obtain a copy of the License at
  7653. *
  7654. * http://www.apache.org/licenses/LICENSE-2.0
  7655. *
  7656. * Unless required by applicable law or agreed to in writing, software
  7657. * distributed under the License is distributed on an "AS IS" BASIS,
  7658. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7659. * See the License for the specific language governing permissions and
  7660. * limitations under the License.
  7661. */
  7662. var __childrenNodeConstructor;
  7663. /**
  7664. * LeafNode is a class for storing leaf nodes in a DataSnapshot. It
  7665. * implements Node and stores the value of the node (a string,
  7666. * number, or boolean) accessible via getValue().
  7667. */
  7668. var LeafNode = /** @class */ (function () {
  7669. /**
  7670. * @param value_ - The value to store in this leaf node. The object type is
  7671. * possible in the event of a deferred value
  7672. * @param priorityNode_ - The priority of this node.
  7673. */
  7674. function LeafNode(value_, priorityNode_) {
  7675. if (priorityNode_ === void 0) { priorityNode_ = LeafNode.__childrenNodeConstructor.EMPTY_NODE; }
  7676. this.value_ = value_;
  7677. this.priorityNode_ = priorityNode_;
  7678. this.lazyHash_ = null;
  7679. util.assert(this.value_ !== undefined && this.value_ !== null, "LeafNode shouldn't be created with null/undefined value.");
  7680. validatePriorityNode(this.priorityNode_);
  7681. }
  7682. Object.defineProperty(LeafNode, "__childrenNodeConstructor", {
  7683. get: function () {
  7684. return __childrenNodeConstructor;
  7685. },
  7686. set: function (val) {
  7687. __childrenNodeConstructor = val;
  7688. },
  7689. enumerable: false,
  7690. configurable: true
  7691. });
  7692. /** @inheritDoc */
  7693. LeafNode.prototype.isLeafNode = function () {
  7694. return true;
  7695. };
  7696. /** @inheritDoc */
  7697. LeafNode.prototype.getPriority = function () {
  7698. return this.priorityNode_;
  7699. };
  7700. /** @inheritDoc */
  7701. LeafNode.prototype.updatePriority = function (newPriorityNode) {
  7702. return new LeafNode(this.value_, newPriorityNode);
  7703. };
  7704. /** @inheritDoc */
  7705. LeafNode.prototype.getImmediateChild = function (childName) {
  7706. // Hack to treat priority as a regular child
  7707. if (childName === '.priority') {
  7708. return this.priorityNode_;
  7709. }
  7710. else {
  7711. return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
  7712. }
  7713. };
  7714. /** @inheritDoc */
  7715. LeafNode.prototype.getChild = function (path) {
  7716. if (pathIsEmpty(path)) {
  7717. return this;
  7718. }
  7719. else if (pathGetFront(path) === '.priority') {
  7720. return this.priorityNode_;
  7721. }
  7722. else {
  7723. return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
  7724. }
  7725. };
  7726. LeafNode.prototype.hasChild = function () {
  7727. return false;
  7728. };
  7729. /** @inheritDoc */
  7730. LeafNode.prototype.getPredecessorChildName = function (childName, childNode) {
  7731. return null;
  7732. };
  7733. /** @inheritDoc */
  7734. LeafNode.prototype.updateImmediateChild = function (childName, newChildNode) {
  7735. if (childName === '.priority') {
  7736. return this.updatePriority(newChildNode);
  7737. }
  7738. else if (newChildNode.isEmpty() && childName !== '.priority') {
  7739. return this;
  7740. }
  7741. else {
  7742. return LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateImmediateChild(childName, newChildNode).updatePriority(this.priorityNode_);
  7743. }
  7744. };
  7745. /** @inheritDoc */
  7746. LeafNode.prototype.updateChild = function (path, newChildNode) {
  7747. var front = pathGetFront(path);
  7748. if (front === null) {
  7749. return newChildNode;
  7750. }
  7751. else if (newChildNode.isEmpty() && front !== '.priority') {
  7752. return this;
  7753. }
  7754. else {
  7755. util.assert(front !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
  7756. return this.updateImmediateChild(front, LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateChild(pathPopFront(path), newChildNode));
  7757. }
  7758. };
  7759. /** @inheritDoc */
  7760. LeafNode.prototype.isEmpty = function () {
  7761. return false;
  7762. };
  7763. /** @inheritDoc */
  7764. LeafNode.prototype.numChildren = function () {
  7765. return 0;
  7766. };
  7767. /** @inheritDoc */
  7768. LeafNode.prototype.forEachChild = function (index, action) {
  7769. return false;
  7770. };
  7771. LeafNode.prototype.val = function (exportFormat) {
  7772. if (exportFormat && !this.getPriority().isEmpty()) {
  7773. return {
  7774. '.value': this.getValue(),
  7775. '.priority': this.getPriority().val()
  7776. };
  7777. }
  7778. else {
  7779. return this.getValue();
  7780. }
  7781. };
  7782. /** @inheritDoc */
  7783. LeafNode.prototype.hash = function () {
  7784. if (this.lazyHash_ === null) {
  7785. var toHash = '';
  7786. if (!this.priorityNode_.isEmpty()) {
  7787. toHash +=
  7788. 'priority:' +
  7789. priorityHashText(this.priorityNode_.val()) +
  7790. ':';
  7791. }
  7792. var type = typeof this.value_;
  7793. toHash += type + ':';
  7794. if (type === 'number') {
  7795. toHash += doubleToIEEE754String(this.value_);
  7796. }
  7797. else {
  7798. toHash += this.value_;
  7799. }
  7800. this.lazyHash_ = sha1(toHash);
  7801. }
  7802. return this.lazyHash_;
  7803. };
  7804. /**
  7805. * Returns the value of the leaf node.
  7806. * @returns The value of the node.
  7807. */
  7808. LeafNode.prototype.getValue = function () {
  7809. return this.value_;
  7810. };
  7811. LeafNode.prototype.compareTo = function (other) {
  7812. if (other === LeafNode.__childrenNodeConstructor.EMPTY_NODE) {
  7813. return 1;
  7814. }
  7815. else if (other instanceof LeafNode.__childrenNodeConstructor) {
  7816. return -1;
  7817. }
  7818. else {
  7819. util.assert(other.isLeafNode(), 'Unknown node type');
  7820. return this.compareToLeafNode_(other);
  7821. }
  7822. };
  7823. /**
  7824. * Comparison specifically for two leaf nodes
  7825. */
  7826. LeafNode.prototype.compareToLeafNode_ = function (otherLeaf) {
  7827. var otherLeafType = typeof otherLeaf.value_;
  7828. var thisLeafType = typeof this.value_;
  7829. var otherIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(otherLeafType);
  7830. var thisIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(thisLeafType);
  7831. util.assert(otherIndex >= 0, 'Unknown leaf type: ' + otherLeafType);
  7832. util.assert(thisIndex >= 0, 'Unknown leaf type: ' + thisLeafType);
  7833. if (otherIndex === thisIndex) {
  7834. // Same type, compare values
  7835. if (thisLeafType === 'object') {
  7836. // Deferred value nodes are all equal, but we should also never get to this point...
  7837. return 0;
  7838. }
  7839. else {
  7840. // Note that this works because true > false, all others are number or string comparisons
  7841. if (this.value_ < otherLeaf.value_) {
  7842. return -1;
  7843. }
  7844. else if (this.value_ === otherLeaf.value_) {
  7845. return 0;
  7846. }
  7847. else {
  7848. return 1;
  7849. }
  7850. }
  7851. }
  7852. else {
  7853. return thisIndex - otherIndex;
  7854. }
  7855. };
  7856. LeafNode.prototype.withIndex = function () {
  7857. return this;
  7858. };
  7859. LeafNode.prototype.isIndexed = function () {
  7860. return true;
  7861. };
  7862. LeafNode.prototype.equals = function (other) {
  7863. if (other === this) {
  7864. return true;
  7865. }
  7866. else if (other.isLeafNode()) {
  7867. var otherLeaf = other;
  7868. return (this.value_ === otherLeaf.value_ &&
  7869. this.priorityNode_.equals(otherLeaf.priorityNode_));
  7870. }
  7871. else {
  7872. return false;
  7873. }
  7874. };
  7875. /**
  7876. * The sort order for comparing leaf nodes of different types. If two leaf nodes have
  7877. * the same type, the comparison falls back to their value
  7878. */
  7879. LeafNode.VALUE_TYPE_ORDER = ['object', 'boolean', 'number', 'string'];
  7880. return LeafNode;
  7881. }());
  7882. /**
  7883. * @license
  7884. * Copyright 2017 Google LLC
  7885. *
  7886. * Licensed under the Apache License, Version 2.0 (the "License");
  7887. * you may not use this file except in compliance with the License.
  7888. * You may obtain a copy of the License at
  7889. *
  7890. * http://www.apache.org/licenses/LICENSE-2.0
  7891. *
  7892. * Unless required by applicable law or agreed to in writing, software
  7893. * distributed under the License is distributed on an "AS IS" BASIS,
  7894. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7895. * See the License for the specific language governing permissions and
  7896. * limitations under the License.
  7897. */
  7898. var nodeFromJSON$1;
  7899. var MAX_NODE$1;
  7900. function setNodeFromJSON(val) {
  7901. nodeFromJSON$1 = val;
  7902. }
  7903. function setMaxNode(val) {
  7904. MAX_NODE$1 = val;
  7905. }
  7906. var PriorityIndex = /** @class */ (function (_super) {
  7907. tslib.__extends(PriorityIndex, _super);
  7908. function PriorityIndex() {
  7909. return _super !== null && _super.apply(this, arguments) || this;
  7910. }
  7911. PriorityIndex.prototype.compare = function (a, b) {
  7912. var aPriority = a.node.getPriority();
  7913. var bPriority = b.node.getPriority();
  7914. var indexCmp = aPriority.compareTo(bPriority);
  7915. if (indexCmp === 0) {
  7916. return nameCompare(a.name, b.name);
  7917. }
  7918. else {
  7919. return indexCmp;
  7920. }
  7921. };
  7922. PriorityIndex.prototype.isDefinedOn = function (node) {
  7923. return !node.getPriority().isEmpty();
  7924. };
  7925. PriorityIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
  7926. return !oldNode.getPriority().equals(newNode.getPriority());
  7927. };
  7928. PriorityIndex.prototype.minPost = function () {
  7929. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  7930. return NamedNode.MIN;
  7931. };
  7932. PriorityIndex.prototype.maxPost = function () {
  7933. return new NamedNode(MAX_NAME, new LeafNode('[PRIORITY-POST]', MAX_NODE$1));
  7934. };
  7935. PriorityIndex.prototype.makePost = function (indexValue, name) {
  7936. var priorityNode = nodeFromJSON$1(indexValue);
  7937. return new NamedNode(name, new LeafNode('[PRIORITY-POST]', priorityNode));
  7938. };
  7939. /**
  7940. * @returns String representation for inclusion in a query spec
  7941. */
  7942. PriorityIndex.prototype.toString = function () {
  7943. return '.priority';
  7944. };
  7945. return PriorityIndex;
  7946. }(Index));
  7947. var PRIORITY_INDEX = new PriorityIndex();
  7948. /**
  7949. * @license
  7950. * Copyright 2017 Google LLC
  7951. *
  7952. * Licensed under the Apache License, Version 2.0 (the "License");
  7953. * you may not use this file except in compliance with the License.
  7954. * You may obtain a copy of the License at
  7955. *
  7956. * http://www.apache.org/licenses/LICENSE-2.0
  7957. *
  7958. * Unless required by applicable law or agreed to in writing, software
  7959. * distributed under the License is distributed on an "AS IS" BASIS,
  7960. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7961. * See the License for the specific language governing permissions and
  7962. * limitations under the License.
  7963. */
  7964. var LOG_2 = Math.log(2);
  7965. var Base12Num = /** @class */ (function () {
  7966. function Base12Num(length) {
  7967. var logBase2 = function (num) {
  7968. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  7969. return parseInt((Math.log(num) / LOG_2), 10);
  7970. };
  7971. var bitMask = function (bits) { return parseInt(Array(bits + 1).join('1'), 2); };
  7972. this.count = logBase2(length + 1);
  7973. this.current_ = this.count - 1;
  7974. var mask = bitMask(this.count);
  7975. this.bits_ = (length + 1) & mask;
  7976. }
  7977. Base12Num.prototype.nextBitIsOne = function () {
  7978. //noinspection JSBitwiseOperatorUsage
  7979. var result = !(this.bits_ & (0x1 << this.current_));
  7980. this.current_--;
  7981. return result;
  7982. };
  7983. return Base12Num;
  7984. }());
  7985. /**
  7986. * Takes a list of child nodes and constructs a SortedSet using the given comparison
  7987. * function
  7988. *
  7989. * Uses the algorithm described in the paper linked here:
  7990. * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.1458
  7991. *
  7992. * @param childList - Unsorted list of children
  7993. * @param cmp - The comparison method to be used
  7994. * @param keyFn - An optional function to extract K from a node wrapper, if K's
  7995. * type is not NamedNode
  7996. * @param mapSortFn - An optional override for comparator used by the generated sorted map
  7997. */
  7998. var buildChildSet = function (childList, cmp, keyFn, mapSortFn) {
  7999. childList.sort(cmp);
  8000. var buildBalancedTree = function (low, high) {
  8001. var length = high - low;
  8002. var namedNode;
  8003. var key;
  8004. if (length === 0) {
  8005. return null;
  8006. }
  8007. else if (length === 1) {
  8008. namedNode = childList[low];
  8009. key = keyFn ? keyFn(namedNode) : namedNode;
  8010. return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, null, null);
  8011. }
  8012. else {
  8013. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  8014. var middle = parseInt((length / 2), 10) + low;
  8015. var left = buildBalancedTree(low, middle);
  8016. var right = buildBalancedTree(middle + 1, high);
  8017. namedNode = childList[middle];
  8018. key = keyFn ? keyFn(namedNode) : namedNode;
  8019. return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, left, right);
  8020. }
  8021. };
  8022. var buildFrom12Array = function (base12) {
  8023. var node = null;
  8024. var root = null;
  8025. var index = childList.length;
  8026. var buildPennant = function (chunkSize, color) {
  8027. var low = index - chunkSize;
  8028. var high = index;
  8029. index -= chunkSize;
  8030. var childTree = buildBalancedTree(low + 1, high);
  8031. var namedNode = childList[low];
  8032. var key = keyFn ? keyFn(namedNode) : namedNode;
  8033. attachPennant(new LLRBNode(key, namedNode.node, color, null, childTree));
  8034. };
  8035. var attachPennant = function (pennant) {
  8036. if (node) {
  8037. node.left = pennant;
  8038. node = pennant;
  8039. }
  8040. else {
  8041. root = pennant;
  8042. node = pennant;
  8043. }
  8044. };
  8045. for (var i = 0; i < base12.count; ++i) {
  8046. var isOne = base12.nextBitIsOne();
  8047. // The number of nodes taken in each slice is 2^(arr.length - (i + 1))
  8048. var chunkSize = Math.pow(2, base12.count - (i + 1));
  8049. if (isOne) {
  8050. buildPennant(chunkSize, LLRBNode.BLACK);
  8051. }
  8052. else {
  8053. // current == 2
  8054. buildPennant(chunkSize, LLRBNode.BLACK);
  8055. buildPennant(chunkSize, LLRBNode.RED);
  8056. }
  8057. }
  8058. return root;
  8059. };
  8060. var base12 = new Base12Num(childList.length);
  8061. var root = buildFrom12Array(base12);
  8062. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  8063. return new SortedMap(mapSortFn || cmp, root);
  8064. };
  8065. /**
  8066. * @license
  8067. * Copyright 2017 Google LLC
  8068. *
  8069. * Licensed under the Apache License, Version 2.0 (the "License");
  8070. * you may not use this file except in compliance with the License.
  8071. * You may obtain a copy of the License at
  8072. *
  8073. * http://www.apache.org/licenses/LICENSE-2.0
  8074. *
  8075. * Unless required by applicable law or agreed to in writing, software
  8076. * distributed under the License is distributed on an "AS IS" BASIS,
  8077. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8078. * See the License for the specific language governing permissions and
  8079. * limitations under the License.
  8080. */
  8081. var _defaultIndexMap;
  8082. var fallbackObject = {};
  8083. var IndexMap = /** @class */ (function () {
  8084. function IndexMap(indexes_, indexSet_) {
  8085. this.indexes_ = indexes_;
  8086. this.indexSet_ = indexSet_;
  8087. }
  8088. Object.defineProperty(IndexMap, "Default", {
  8089. /**
  8090. * The default IndexMap for nodes without a priority
  8091. */
  8092. get: function () {
  8093. util.assert(fallbackObject && PRIORITY_INDEX, 'ChildrenNode.ts has not been loaded');
  8094. _defaultIndexMap =
  8095. _defaultIndexMap ||
  8096. new IndexMap({ '.priority': fallbackObject }, { '.priority': PRIORITY_INDEX });
  8097. return _defaultIndexMap;
  8098. },
  8099. enumerable: false,
  8100. configurable: true
  8101. });
  8102. IndexMap.prototype.get = function (indexKey) {
  8103. var sortedMap = util.safeGet(this.indexes_, indexKey);
  8104. if (!sortedMap) {
  8105. throw new Error('No index defined for ' + indexKey);
  8106. }
  8107. if (sortedMap instanceof SortedMap) {
  8108. return sortedMap;
  8109. }
  8110. else {
  8111. // The index exists, but it falls back to just name comparison. Return null so that the calling code uses the
  8112. // regular child map
  8113. return null;
  8114. }
  8115. };
  8116. IndexMap.prototype.hasIndex = function (indexDefinition) {
  8117. return util.contains(this.indexSet_, indexDefinition.toString());
  8118. };
  8119. IndexMap.prototype.addIndex = function (indexDefinition, existingChildren) {
  8120. util.assert(indexDefinition !== KEY_INDEX, "KeyIndex always exists and isn't meant to be added to the IndexMap.");
  8121. var childList = [];
  8122. var sawIndexedValue = false;
  8123. var iter = existingChildren.getIterator(NamedNode.Wrap);
  8124. var next = iter.getNext();
  8125. while (next) {
  8126. sawIndexedValue =
  8127. sawIndexedValue || indexDefinition.isDefinedOn(next.node);
  8128. childList.push(next);
  8129. next = iter.getNext();
  8130. }
  8131. var newIndex;
  8132. if (sawIndexedValue) {
  8133. newIndex = buildChildSet(childList, indexDefinition.getCompare());
  8134. }
  8135. else {
  8136. newIndex = fallbackObject;
  8137. }
  8138. var indexName = indexDefinition.toString();
  8139. var newIndexSet = tslib.__assign({}, this.indexSet_);
  8140. newIndexSet[indexName] = indexDefinition;
  8141. var newIndexes = tslib.__assign({}, this.indexes_);
  8142. newIndexes[indexName] = newIndex;
  8143. return new IndexMap(newIndexes, newIndexSet);
  8144. };
  8145. /**
  8146. * Ensure that this node is properly tracked in any indexes that we're maintaining
  8147. */
  8148. IndexMap.prototype.addToIndexes = function (namedNode, existingChildren) {
  8149. var _this = this;
  8150. var newIndexes = util.map(this.indexes_, function (indexedChildren, indexName) {
  8151. var index = util.safeGet(_this.indexSet_, indexName);
  8152. util.assert(index, 'Missing index implementation for ' + indexName);
  8153. if (indexedChildren === fallbackObject) {
  8154. // Check to see if we need to index everything
  8155. if (index.isDefinedOn(namedNode.node)) {
  8156. // We need to build this index
  8157. var childList = [];
  8158. var iter = existingChildren.getIterator(NamedNode.Wrap);
  8159. var next = iter.getNext();
  8160. while (next) {
  8161. if (next.name !== namedNode.name) {
  8162. childList.push(next);
  8163. }
  8164. next = iter.getNext();
  8165. }
  8166. childList.push(namedNode);
  8167. return buildChildSet(childList, index.getCompare());
  8168. }
  8169. else {
  8170. // No change, this remains a fallback
  8171. return fallbackObject;
  8172. }
  8173. }
  8174. else {
  8175. var existingSnap = existingChildren.get(namedNode.name);
  8176. var newChildren = indexedChildren;
  8177. if (existingSnap) {
  8178. newChildren = newChildren.remove(new NamedNode(namedNode.name, existingSnap));
  8179. }
  8180. return newChildren.insert(namedNode, namedNode.node);
  8181. }
  8182. });
  8183. return new IndexMap(newIndexes, this.indexSet_);
  8184. };
  8185. /**
  8186. * Create a new IndexMap instance with the given value removed
  8187. */
  8188. IndexMap.prototype.removeFromIndexes = function (namedNode, existingChildren) {
  8189. var newIndexes = util.map(this.indexes_, function (indexedChildren) {
  8190. if (indexedChildren === fallbackObject) {
  8191. // This is the fallback. Just return it, nothing to do in this case
  8192. return indexedChildren;
  8193. }
  8194. else {
  8195. var existingSnap = existingChildren.get(namedNode.name);
  8196. if (existingSnap) {
  8197. return indexedChildren.remove(new NamedNode(namedNode.name, existingSnap));
  8198. }
  8199. else {
  8200. // No record of this child
  8201. return indexedChildren;
  8202. }
  8203. }
  8204. });
  8205. return new IndexMap(newIndexes, this.indexSet_);
  8206. };
  8207. return IndexMap;
  8208. }());
  8209. /**
  8210. * @license
  8211. * Copyright 2017 Google LLC
  8212. *
  8213. * Licensed under the Apache License, Version 2.0 (the "License");
  8214. * you may not use this file except in compliance with the License.
  8215. * You may obtain a copy of the License at
  8216. *
  8217. * http://www.apache.org/licenses/LICENSE-2.0
  8218. *
  8219. * Unless required by applicable law or agreed to in writing, software
  8220. * distributed under the License is distributed on an "AS IS" BASIS,
  8221. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8222. * See the License for the specific language governing permissions and
  8223. * limitations under the License.
  8224. */
  8225. // TODO: For memory savings, don't store priorityNode_ if it's empty.
  8226. var EMPTY_NODE;
  8227. /**
  8228. * ChildrenNode is a class for storing internal nodes in a DataSnapshot
  8229. * (i.e. nodes with children). It implements Node and stores the
  8230. * list of children in the children property, sorted by child name.
  8231. */
  8232. var ChildrenNode = /** @class */ (function () {
  8233. /**
  8234. * @param children_ - List of children of this node..
  8235. * @param priorityNode_ - The priority of this node (as a snapshot node).
  8236. */
  8237. function ChildrenNode(children_, priorityNode_, indexMap_) {
  8238. this.children_ = children_;
  8239. this.priorityNode_ = priorityNode_;
  8240. this.indexMap_ = indexMap_;
  8241. this.lazyHash_ = null;
  8242. /**
  8243. * Note: The only reason we allow null priority is for EMPTY_NODE, since we can't use
  8244. * EMPTY_NODE as the priority of EMPTY_NODE. We might want to consider making EMPTY_NODE its own
  8245. * class instead of an empty ChildrenNode.
  8246. */
  8247. if (this.priorityNode_) {
  8248. validatePriorityNode(this.priorityNode_);
  8249. }
  8250. if (this.children_.isEmpty()) {
  8251. util.assert(!this.priorityNode_ || this.priorityNode_.isEmpty(), 'An empty node cannot have a priority');
  8252. }
  8253. }
  8254. Object.defineProperty(ChildrenNode, "EMPTY_NODE", {
  8255. get: function () {
  8256. return (EMPTY_NODE ||
  8257. (EMPTY_NODE = new ChildrenNode(new SortedMap(NAME_COMPARATOR), null, IndexMap.Default)));
  8258. },
  8259. enumerable: false,
  8260. configurable: true
  8261. });
  8262. /** @inheritDoc */
  8263. ChildrenNode.prototype.isLeafNode = function () {
  8264. return false;
  8265. };
  8266. /** @inheritDoc */
  8267. ChildrenNode.prototype.getPriority = function () {
  8268. return this.priorityNode_ || EMPTY_NODE;
  8269. };
  8270. /** @inheritDoc */
  8271. ChildrenNode.prototype.updatePriority = function (newPriorityNode) {
  8272. if (this.children_.isEmpty()) {
  8273. // Don't allow priorities on empty nodes
  8274. return this;
  8275. }
  8276. else {
  8277. return new ChildrenNode(this.children_, newPriorityNode, this.indexMap_);
  8278. }
  8279. };
  8280. /** @inheritDoc */
  8281. ChildrenNode.prototype.getImmediateChild = function (childName) {
  8282. // Hack to treat priority as a regular child
  8283. if (childName === '.priority') {
  8284. return this.getPriority();
  8285. }
  8286. else {
  8287. var child = this.children_.get(childName);
  8288. return child === null ? EMPTY_NODE : child;
  8289. }
  8290. };
  8291. /** @inheritDoc */
  8292. ChildrenNode.prototype.getChild = function (path) {
  8293. var front = pathGetFront(path);
  8294. if (front === null) {
  8295. return this;
  8296. }
  8297. return this.getImmediateChild(front).getChild(pathPopFront(path));
  8298. };
  8299. /** @inheritDoc */
  8300. ChildrenNode.prototype.hasChild = function (childName) {
  8301. return this.children_.get(childName) !== null;
  8302. };
  8303. /** @inheritDoc */
  8304. ChildrenNode.prototype.updateImmediateChild = function (childName, newChildNode) {
  8305. util.assert(newChildNode, 'We should always be passing snapshot nodes');
  8306. if (childName === '.priority') {
  8307. return this.updatePriority(newChildNode);
  8308. }
  8309. else {
  8310. var namedNode = new NamedNode(childName, newChildNode);
  8311. var newChildren = void 0, newIndexMap = void 0;
  8312. if (newChildNode.isEmpty()) {
  8313. newChildren = this.children_.remove(childName);
  8314. newIndexMap = this.indexMap_.removeFromIndexes(namedNode, this.children_);
  8315. }
  8316. else {
  8317. newChildren = this.children_.insert(childName, newChildNode);
  8318. newIndexMap = this.indexMap_.addToIndexes(namedNode, this.children_);
  8319. }
  8320. var newPriority = newChildren.isEmpty()
  8321. ? EMPTY_NODE
  8322. : this.priorityNode_;
  8323. return new ChildrenNode(newChildren, newPriority, newIndexMap);
  8324. }
  8325. };
  8326. /** @inheritDoc */
  8327. ChildrenNode.prototype.updateChild = function (path, newChildNode) {
  8328. var front = pathGetFront(path);
  8329. if (front === null) {
  8330. return newChildNode;
  8331. }
  8332. else {
  8333. util.assert(pathGetFront(path) !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
  8334. var newImmediateChild = this.getImmediateChild(front).updateChild(pathPopFront(path), newChildNode);
  8335. return this.updateImmediateChild(front, newImmediateChild);
  8336. }
  8337. };
  8338. /** @inheritDoc */
  8339. ChildrenNode.prototype.isEmpty = function () {
  8340. return this.children_.isEmpty();
  8341. };
  8342. /** @inheritDoc */
  8343. ChildrenNode.prototype.numChildren = function () {
  8344. return this.children_.count();
  8345. };
  8346. /** @inheritDoc */
  8347. ChildrenNode.prototype.val = function (exportFormat) {
  8348. if (this.isEmpty()) {
  8349. return null;
  8350. }
  8351. var obj = {};
  8352. var numKeys = 0, maxKey = 0, allIntegerKeys = true;
  8353. this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
  8354. obj[key] = childNode.val(exportFormat);
  8355. numKeys++;
  8356. if (allIntegerKeys && ChildrenNode.INTEGER_REGEXP_.test(key)) {
  8357. maxKey = Math.max(maxKey, Number(key));
  8358. }
  8359. else {
  8360. allIntegerKeys = false;
  8361. }
  8362. });
  8363. if (!exportFormat && allIntegerKeys && maxKey < 2 * numKeys) {
  8364. // convert to array.
  8365. var array = [];
  8366. // eslint-disable-next-line guard-for-in
  8367. for (var key in obj) {
  8368. array[key] = obj[key];
  8369. }
  8370. return array;
  8371. }
  8372. else {
  8373. if (exportFormat && !this.getPriority().isEmpty()) {
  8374. obj['.priority'] = this.getPriority().val();
  8375. }
  8376. return obj;
  8377. }
  8378. };
  8379. /** @inheritDoc */
  8380. ChildrenNode.prototype.hash = function () {
  8381. if (this.lazyHash_ === null) {
  8382. var toHash_1 = '';
  8383. if (!this.getPriority().isEmpty()) {
  8384. toHash_1 +=
  8385. 'priority:' +
  8386. priorityHashText(this.getPriority().val()) +
  8387. ':';
  8388. }
  8389. this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
  8390. var childHash = childNode.hash();
  8391. if (childHash !== '') {
  8392. toHash_1 += ':' + key + ':' + childHash;
  8393. }
  8394. });
  8395. this.lazyHash_ = toHash_1 === '' ? '' : sha1(toHash_1);
  8396. }
  8397. return this.lazyHash_;
  8398. };
  8399. /** @inheritDoc */
  8400. ChildrenNode.prototype.getPredecessorChildName = function (childName, childNode, index) {
  8401. var idx = this.resolveIndex_(index);
  8402. if (idx) {
  8403. var predecessor = idx.getPredecessorKey(new NamedNode(childName, childNode));
  8404. return predecessor ? predecessor.name : null;
  8405. }
  8406. else {
  8407. return this.children_.getPredecessorKey(childName);
  8408. }
  8409. };
  8410. ChildrenNode.prototype.getFirstChildName = function (indexDefinition) {
  8411. var idx = this.resolveIndex_(indexDefinition);
  8412. if (idx) {
  8413. var minKey = idx.minKey();
  8414. return minKey && minKey.name;
  8415. }
  8416. else {
  8417. return this.children_.minKey();
  8418. }
  8419. };
  8420. ChildrenNode.prototype.getFirstChild = function (indexDefinition) {
  8421. var minKey = this.getFirstChildName(indexDefinition);
  8422. if (minKey) {
  8423. return new NamedNode(minKey, this.children_.get(minKey));
  8424. }
  8425. else {
  8426. return null;
  8427. }
  8428. };
  8429. /**
  8430. * Given an index, return the key name of the largest value we have, according to that index
  8431. */
  8432. ChildrenNode.prototype.getLastChildName = function (indexDefinition) {
  8433. var idx = this.resolveIndex_(indexDefinition);
  8434. if (idx) {
  8435. var maxKey = idx.maxKey();
  8436. return maxKey && maxKey.name;
  8437. }
  8438. else {
  8439. return this.children_.maxKey();
  8440. }
  8441. };
  8442. ChildrenNode.prototype.getLastChild = function (indexDefinition) {
  8443. var maxKey = this.getLastChildName(indexDefinition);
  8444. if (maxKey) {
  8445. return new NamedNode(maxKey, this.children_.get(maxKey));
  8446. }
  8447. else {
  8448. return null;
  8449. }
  8450. };
  8451. ChildrenNode.prototype.forEachChild = function (index, action) {
  8452. var idx = this.resolveIndex_(index);
  8453. if (idx) {
  8454. return idx.inorderTraversal(function (wrappedNode) {
  8455. return action(wrappedNode.name, wrappedNode.node);
  8456. });
  8457. }
  8458. else {
  8459. return this.children_.inorderTraversal(action);
  8460. }
  8461. };
  8462. ChildrenNode.prototype.getIterator = function (indexDefinition) {
  8463. return this.getIteratorFrom(indexDefinition.minPost(), indexDefinition);
  8464. };
  8465. ChildrenNode.prototype.getIteratorFrom = function (startPost, indexDefinition) {
  8466. var idx = this.resolveIndex_(indexDefinition);
  8467. if (idx) {
  8468. return idx.getIteratorFrom(startPost, function (key) { return key; });
  8469. }
  8470. else {
  8471. var iterator = this.children_.getIteratorFrom(startPost.name, NamedNode.Wrap);
  8472. var next = iterator.peek();
  8473. while (next != null && indexDefinition.compare(next, startPost) < 0) {
  8474. iterator.getNext();
  8475. next = iterator.peek();
  8476. }
  8477. return iterator;
  8478. }
  8479. };
  8480. ChildrenNode.prototype.getReverseIterator = function (indexDefinition) {
  8481. return this.getReverseIteratorFrom(indexDefinition.maxPost(), indexDefinition);
  8482. };
  8483. ChildrenNode.prototype.getReverseIteratorFrom = function (endPost, indexDefinition) {
  8484. var idx = this.resolveIndex_(indexDefinition);
  8485. if (idx) {
  8486. return idx.getReverseIteratorFrom(endPost, function (key) {
  8487. return key;
  8488. });
  8489. }
  8490. else {
  8491. var iterator = this.children_.getReverseIteratorFrom(endPost.name, NamedNode.Wrap);
  8492. var next = iterator.peek();
  8493. while (next != null && indexDefinition.compare(next, endPost) > 0) {
  8494. iterator.getNext();
  8495. next = iterator.peek();
  8496. }
  8497. return iterator;
  8498. }
  8499. };
  8500. ChildrenNode.prototype.compareTo = function (other) {
  8501. if (this.isEmpty()) {
  8502. if (other.isEmpty()) {
  8503. return 0;
  8504. }
  8505. else {
  8506. return -1;
  8507. }
  8508. }
  8509. else if (other.isLeafNode() || other.isEmpty()) {
  8510. return 1;
  8511. }
  8512. else if (other === MAX_NODE) {
  8513. return -1;
  8514. }
  8515. else {
  8516. // Must be another node with children.
  8517. return 0;
  8518. }
  8519. };
  8520. ChildrenNode.prototype.withIndex = function (indexDefinition) {
  8521. if (indexDefinition === KEY_INDEX ||
  8522. this.indexMap_.hasIndex(indexDefinition)) {
  8523. return this;
  8524. }
  8525. else {
  8526. var newIndexMap = this.indexMap_.addIndex(indexDefinition, this.children_);
  8527. return new ChildrenNode(this.children_, this.priorityNode_, newIndexMap);
  8528. }
  8529. };
  8530. ChildrenNode.prototype.isIndexed = function (index) {
  8531. return index === KEY_INDEX || this.indexMap_.hasIndex(index);
  8532. };
  8533. ChildrenNode.prototype.equals = function (other) {
  8534. if (other === this) {
  8535. return true;
  8536. }
  8537. else if (other.isLeafNode()) {
  8538. return false;
  8539. }
  8540. else {
  8541. var otherChildrenNode = other;
  8542. if (!this.getPriority().equals(otherChildrenNode.getPriority())) {
  8543. return false;
  8544. }
  8545. else if (this.children_.count() === otherChildrenNode.children_.count()) {
  8546. var thisIter = this.getIterator(PRIORITY_INDEX);
  8547. var otherIter = otherChildrenNode.getIterator(PRIORITY_INDEX);
  8548. var thisCurrent = thisIter.getNext();
  8549. var otherCurrent = otherIter.getNext();
  8550. while (thisCurrent && otherCurrent) {
  8551. if (thisCurrent.name !== otherCurrent.name ||
  8552. !thisCurrent.node.equals(otherCurrent.node)) {
  8553. return false;
  8554. }
  8555. thisCurrent = thisIter.getNext();
  8556. otherCurrent = otherIter.getNext();
  8557. }
  8558. return thisCurrent === null && otherCurrent === null;
  8559. }
  8560. else {
  8561. return false;
  8562. }
  8563. }
  8564. };
  8565. /**
  8566. * Returns a SortedMap ordered by index, or null if the default (by-key) ordering can be used
  8567. * instead.
  8568. *
  8569. */
  8570. ChildrenNode.prototype.resolveIndex_ = function (indexDefinition) {
  8571. if (indexDefinition === KEY_INDEX) {
  8572. return null;
  8573. }
  8574. else {
  8575. return this.indexMap_.get(indexDefinition.toString());
  8576. }
  8577. };
  8578. ChildrenNode.INTEGER_REGEXP_ = /^(0|[1-9]\d*)$/;
  8579. return ChildrenNode;
  8580. }());
  8581. var MaxNode = /** @class */ (function (_super) {
  8582. tslib.__extends(MaxNode, _super);
  8583. function MaxNode() {
  8584. return _super.call(this, new SortedMap(NAME_COMPARATOR), ChildrenNode.EMPTY_NODE, IndexMap.Default) || this;
  8585. }
  8586. MaxNode.prototype.compareTo = function (other) {
  8587. if (other === this) {
  8588. return 0;
  8589. }
  8590. else {
  8591. return 1;
  8592. }
  8593. };
  8594. MaxNode.prototype.equals = function (other) {
  8595. // Not that we every compare it, but MAX_NODE is only ever equal to itself
  8596. return other === this;
  8597. };
  8598. MaxNode.prototype.getPriority = function () {
  8599. return this;
  8600. };
  8601. MaxNode.prototype.getImmediateChild = function (childName) {
  8602. return ChildrenNode.EMPTY_NODE;
  8603. };
  8604. MaxNode.prototype.isEmpty = function () {
  8605. return false;
  8606. };
  8607. return MaxNode;
  8608. }(ChildrenNode));
  8609. /**
  8610. * Marker that will sort higher than any other snapshot.
  8611. */
  8612. var MAX_NODE = new MaxNode();
  8613. Object.defineProperties(NamedNode, {
  8614. MIN: {
  8615. value: new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE)
  8616. },
  8617. MAX: {
  8618. value: new NamedNode(MAX_NAME, MAX_NODE)
  8619. }
  8620. });
  8621. /**
  8622. * Reference Extensions
  8623. */
  8624. KeyIndex.__EMPTY_NODE = ChildrenNode.EMPTY_NODE;
  8625. LeafNode.__childrenNodeConstructor = ChildrenNode;
  8626. setMaxNode$1(MAX_NODE);
  8627. setMaxNode(MAX_NODE);
  8628. /**
  8629. * @license
  8630. * Copyright 2017 Google LLC
  8631. *
  8632. * Licensed under the Apache License, Version 2.0 (the "License");
  8633. * you may not use this file except in compliance with the License.
  8634. * You may obtain a copy of the License at
  8635. *
  8636. * http://www.apache.org/licenses/LICENSE-2.0
  8637. *
  8638. * Unless required by applicable law or agreed to in writing, software
  8639. * distributed under the License is distributed on an "AS IS" BASIS,
  8640. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8641. * See the License for the specific language governing permissions and
  8642. * limitations under the License.
  8643. */
  8644. var USE_HINZE = true;
  8645. /**
  8646. * Constructs a snapshot node representing the passed JSON and returns it.
  8647. * @param json - JSON to create a node for.
  8648. * @param priority - Optional priority to use. This will be ignored if the
  8649. * passed JSON contains a .priority property.
  8650. */
  8651. function nodeFromJSON(json, priority) {
  8652. if (priority === void 0) { priority = null; }
  8653. if (json === null) {
  8654. return ChildrenNode.EMPTY_NODE;
  8655. }
  8656. if (typeof json === 'object' && '.priority' in json) {
  8657. priority = json['.priority'];
  8658. }
  8659. util.assert(priority === null ||
  8660. typeof priority === 'string' ||
  8661. typeof priority === 'number' ||
  8662. (typeof priority === 'object' && '.sv' in priority), 'Invalid priority type found: ' + typeof priority);
  8663. if (typeof json === 'object' && '.value' in json && json['.value'] !== null) {
  8664. json = json['.value'];
  8665. }
  8666. // Valid leaf nodes include non-objects or server-value wrapper objects
  8667. if (typeof json !== 'object' || '.sv' in json) {
  8668. var jsonLeaf = json;
  8669. return new LeafNode(jsonLeaf, nodeFromJSON(priority));
  8670. }
  8671. if (!(json instanceof Array) && USE_HINZE) {
  8672. var children_1 = [];
  8673. var childrenHavePriority_1 = false;
  8674. var hinzeJsonObj = json;
  8675. each(hinzeJsonObj, function (key, child) {
  8676. if (key.substring(0, 1) !== '.') {
  8677. // Ignore metadata nodes
  8678. var childNode = nodeFromJSON(child);
  8679. if (!childNode.isEmpty()) {
  8680. childrenHavePriority_1 =
  8681. childrenHavePriority_1 || !childNode.getPriority().isEmpty();
  8682. children_1.push(new NamedNode(key, childNode));
  8683. }
  8684. }
  8685. });
  8686. if (children_1.length === 0) {
  8687. return ChildrenNode.EMPTY_NODE;
  8688. }
  8689. var childSet = buildChildSet(children_1, NAME_ONLY_COMPARATOR, function (namedNode) { return namedNode.name; }, NAME_COMPARATOR);
  8690. if (childrenHavePriority_1) {
  8691. var sortedChildSet = buildChildSet(children_1, PRIORITY_INDEX.getCompare());
  8692. return new ChildrenNode(childSet, nodeFromJSON(priority), new IndexMap({ '.priority': sortedChildSet }, { '.priority': PRIORITY_INDEX }));
  8693. }
  8694. else {
  8695. return new ChildrenNode(childSet, nodeFromJSON(priority), IndexMap.Default);
  8696. }
  8697. }
  8698. else {
  8699. var node_1 = ChildrenNode.EMPTY_NODE;
  8700. each(json, function (key, childData) {
  8701. if (util.contains(json, key)) {
  8702. if (key.substring(0, 1) !== '.') {
  8703. // ignore metadata nodes.
  8704. var childNode = nodeFromJSON(childData);
  8705. if (childNode.isLeafNode() || !childNode.isEmpty()) {
  8706. node_1 = node_1.updateImmediateChild(key, childNode);
  8707. }
  8708. }
  8709. }
  8710. });
  8711. return node_1.updatePriority(nodeFromJSON(priority));
  8712. }
  8713. }
  8714. setNodeFromJSON(nodeFromJSON);
  8715. /**
  8716. * @license
  8717. * Copyright 2017 Google LLC
  8718. *
  8719. * Licensed under the Apache License, Version 2.0 (the "License");
  8720. * you may not use this file except in compliance with the License.
  8721. * You may obtain a copy of the License at
  8722. *
  8723. * http://www.apache.org/licenses/LICENSE-2.0
  8724. *
  8725. * Unless required by applicable law or agreed to in writing, software
  8726. * distributed under the License is distributed on an "AS IS" BASIS,
  8727. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8728. * See the License for the specific language governing permissions and
  8729. * limitations under the License.
  8730. */
  8731. var PathIndex = /** @class */ (function (_super) {
  8732. tslib.__extends(PathIndex, _super);
  8733. function PathIndex(indexPath_) {
  8734. var _this = _super.call(this) || this;
  8735. _this.indexPath_ = indexPath_;
  8736. util.assert(!pathIsEmpty(indexPath_) && pathGetFront(indexPath_) !== '.priority', "Can't create PathIndex with empty path or .priority key");
  8737. return _this;
  8738. }
  8739. PathIndex.prototype.extractChild = function (snap) {
  8740. return snap.getChild(this.indexPath_);
  8741. };
  8742. PathIndex.prototype.isDefinedOn = function (node) {
  8743. return !node.getChild(this.indexPath_).isEmpty();
  8744. };
  8745. PathIndex.prototype.compare = function (a, b) {
  8746. var aChild = this.extractChild(a.node);
  8747. var bChild = this.extractChild(b.node);
  8748. var indexCmp = aChild.compareTo(bChild);
  8749. if (indexCmp === 0) {
  8750. return nameCompare(a.name, b.name);
  8751. }
  8752. else {
  8753. return indexCmp;
  8754. }
  8755. };
  8756. PathIndex.prototype.makePost = function (indexValue, name) {
  8757. var valueNode = nodeFromJSON(indexValue);
  8758. var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, valueNode);
  8759. return new NamedNode(name, node);
  8760. };
  8761. PathIndex.prototype.maxPost = function () {
  8762. var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, MAX_NODE);
  8763. return new NamedNode(MAX_NAME, node);
  8764. };
  8765. PathIndex.prototype.toString = function () {
  8766. return pathSlice(this.indexPath_, 0).join('/');
  8767. };
  8768. return PathIndex;
  8769. }(Index));
  8770. /**
  8771. * @license
  8772. * Copyright 2017 Google LLC
  8773. *
  8774. * Licensed under the Apache License, Version 2.0 (the "License");
  8775. * you may not use this file except in compliance with the License.
  8776. * You may obtain a copy of the License at
  8777. *
  8778. * http://www.apache.org/licenses/LICENSE-2.0
  8779. *
  8780. * Unless required by applicable law or agreed to in writing, software
  8781. * distributed under the License is distributed on an "AS IS" BASIS,
  8782. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8783. * See the License for the specific language governing permissions and
  8784. * limitations under the License.
  8785. */
  8786. var ValueIndex = /** @class */ (function (_super) {
  8787. tslib.__extends(ValueIndex, _super);
  8788. function ValueIndex() {
  8789. return _super !== null && _super.apply(this, arguments) || this;
  8790. }
  8791. ValueIndex.prototype.compare = function (a, b) {
  8792. var indexCmp = a.node.compareTo(b.node);
  8793. if (indexCmp === 0) {
  8794. return nameCompare(a.name, b.name);
  8795. }
  8796. else {
  8797. return indexCmp;
  8798. }
  8799. };
  8800. ValueIndex.prototype.isDefinedOn = function (node) {
  8801. return true;
  8802. };
  8803. ValueIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
  8804. return !oldNode.equals(newNode);
  8805. };
  8806. ValueIndex.prototype.minPost = function () {
  8807. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  8808. return NamedNode.MIN;
  8809. };
  8810. ValueIndex.prototype.maxPost = function () {
  8811. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  8812. return NamedNode.MAX;
  8813. };
  8814. ValueIndex.prototype.makePost = function (indexValue, name) {
  8815. var valueNode = nodeFromJSON(indexValue);
  8816. return new NamedNode(name, valueNode);
  8817. };
  8818. /**
  8819. * @returns String representation for inclusion in a query spec
  8820. */
  8821. ValueIndex.prototype.toString = function () {
  8822. return '.value';
  8823. };
  8824. return ValueIndex;
  8825. }(Index));
  8826. var VALUE_INDEX = new ValueIndex();
  8827. /**
  8828. * @license
  8829. * Copyright 2017 Google LLC
  8830. *
  8831. * Licensed under the Apache License, Version 2.0 (the "License");
  8832. * you may not use this file except in compliance with the License.
  8833. * You may obtain a copy of the License at
  8834. *
  8835. * http://www.apache.org/licenses/LICENSE-2.0
  8836. *
  8837. * Unless required by applicable law or agreed to in writing, software
  8838. * distributed under the License is distributed on an "AS IS" BASIS,
  8839. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8840. * See the License for the specific language governing permissions and
  8841. * limitations under the License.
  8842. */
  8843. function changeValue(snapshotNode) {
  8844. return { type: "value" /* ChangeType.VALUE */, snapshotNode: snapshotNode };
  8845. }
  8846. function changeChildAdded(childName, snapshotNode) {
  8847. return { type: "child_added" /* ChangeType.CHILD_ADDED */, snapshotNode: snapshotNode, childName: childName };
  8848. }
  8849. function changeChildRemoved(childName, snapshotNode) {
  8850. return { type: "child_removed" /* ChangeType.CHILD_REMOVED */, snapshotNode: snapshotNode, childName: childName };
  8851. }
  8852. function changeChildChanged(childName, snapshotNode, oldSnap) {
  8853. return {
  8854. type: "child_changed" /* ChangeType.CHILD_CHANGED */,
  8855. snapshotNode: snapshotNode,
  8856. childName: childName,
  8857. oldSnap: oldSnap
  8858. };
  8859. }
  8860. function changeChildMoved(childName, snapshotNode) {
  8861. return { type: "child_moved" /* ChangeType.CHILD_MOVED */, snapshotNode: snapshotNode, childName: childName };
  8862. }
  8863. /**
  8864. * @license
  8865. * Copyright 2017 Google LLC
  8866. *
  8867. * Licensed under the Apache License, Version 2.0 (the "License");
  8868. * you may not use this file except in compliance with the License.
  8869. * You may obtain a copy of the License at
  8870. *
  8871. * http://www.apache.org/licenses/LICENSE-2.0
  8872. *
  8873. * Unless required by applicable law or agreed to in writing, software
  8874. * distributed under the License is distributed on an "AS IS" BASIS,
  8875. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8876. * See the License for the specific language governing permissions and
  8877. * limitations under the License.
  8878. */
  8879. /**
  8880. * Doesn't really filter nodes but applies an index to the node and keeps track of any changes
  8881. */
  8882. var IndexedFilter = /** @class */ (function () {
  8883. function IndexedFilter(index_) {
  8884. this.index_ = index_;
  8885. }
  8886. IndexedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
  8887. util.assert(snap.isIndexed(this.index_), 'A node must be indexed if only a child is updated');
  8888. var oldChild = snap.getImmediateChild(key);
  8889. // Check if anything actually changed.
  8890. if (oldChild.getChild(affectedPath).equals(newChild.getChild(affectedPath))) {
  8891. // There's an edge case where a child can enter or leave the view because affectedPath was set to null.
  8892. // In this case, affectedPath will appear null in both the old and new snapshots. So we need
  8893. // to avoid treating these cases as "nothing changed."
  8894. if (oldChild.isEmpty() === newChild.isEmpty()) {
  8895. // Nothing changed.
  8896. // This assert should be valid, but it's expensive (can dominate perf testing) so don't actually do it.
  8897. //assert(oldChild.equals(newChild), 'Old and new snapshots should be equal.');
  8898. return snap;
  8899. }
  8900. }
  8901. if (optChangeAccumulator != null) {
  8902. if (newChild.isEmpty()) {
  8903. if (snap.hasChild(key)) {
  8904. optChangeAccumulator.trackChildChange(changeChildRemoved(key, oldChild));
  8905. }
  8906. else {
  8907. util.assert(snap.isLeafNode(), 'A child remove without an old child only makes sense on a leaf node');
  8908. }
  8909. }
  8910. else if (oldChild.isEmpty()) {
  8911. optChangeAccumulator.trackChildChange(changeChildAdded(key, newChild));
  8912. }
  8913. else {
  8914. optChangeAccumulator.trackChildChange(changeChildChanged(key, newChild, oldChild));
  8915. }
  8916. }
  8917. if (snap.isLeafNode() && newChild.isEmpty()) {
  8918. return snap;
  8919. }
  8920. else {
  8921. // Make sure the node is indexed
  8922. return snap.updateImmediateChild(key, newChild).withIndex(this.index_);
  8923. }
  8924. };
  8925. IndexedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
  8926. if (optChangeAccumulator != null) {
  8927. if (!oldSnap.isLeafNode()) {
  8928. oldSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
  8929. if (!newSnap.hasChild(key)) {
  8930. optChangeAccumulator.trackChildChange(changeChildRemoved(key, childNode));
  8931. }
  8932. });
  8933. }
  8934. if (!newSnap.isLeafNode()) {
  8935. newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
  8936. if (oldSnap.hasChild(key)) {
  8937. var oldChild = oldSnap.getImmediateChild(key);
  8938. if (!oldChild.equals(childNode)) {
  8939. optChangeAccumulator.trackChildChange(changeChildChanged(key, childNode, oldChild));
  8940. }
  8941. }
  8942. else {
  8943. optChangeAccumulator.trackChildChange(changeChildAdded(key, childNode));
  8944. }
  8945. });
  8946. }
  8947. }
  8948. return newSnap.withIndex(this.index_);
  8949. };
  8950. IndexedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
  8951. if (oldSnap.isEmpty()) {
  8952. return ChildrenNode.EMPTY_NODE;
  8953. }
  8954. else {
  8955. return oldSnap.updatePriority(newPriority);
  8956. }
  8957. };
  8958. IndexedFilter.prototype.filtersNodes = function () {
  8959. return false;
  8960. };
  8961. IndexedFilter.prototype.getIndexedFilter = function () {
  8962. return this;
  8963. };
  8964. IndexedFilter.prototype.getIndex = function () {
  8965. return this.index_;
  8966. };
  8967. return IndexedFilter;
  8968. }());
  8969. /**
  8970. * @license
  8971. * Copyright 2017 Google LLC
  8972. *
  8973. * Licensed under the Apache License, Version 2.0 (the "License");
  8974. * you may not use this file except in compliance with the License.
  8975. * You may obtain a copy of the License at
  8976. *
  8977. * http://www.apache.org/licenses/LICENSE-2.0
  8978. *
  8979. * Unless required by applicable law or agreed to in writing, software
  8980. * distributed under the License is distributed on an "AS IS" BASIS,
  8981. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8982. * See the License for the specific language governing permissions and
  8983. * limitations under the License.
  8984. */
  8985. /**
  8986. * Filters nodes by range and uses an IndexFilter to track any changes after filtering the node
  8987. */
  8988. var RangedFilter = /** @class */ (function () {
  8989. function RangedFilter(params) {
  8990. this.indexedFilter_ = new IndexedFilter(params.getIndex());
  8991. this.index_ = params.getIndex();
  8992. this.startPost_ = RangedFilter.getStartPost_(params);
  8993. this.endPost_ = RangedFilter.getEndPost_(params);
  8994. this.startIsInclusive_ = !params.startAfterSet_;
  8995. this.endIsInclusive_ = !params.endBeforeSet_;
  8996. }
  8997. RangedFilter.prototype.getStartPost = function () {
  8998. return this.startPost_;
  8999. };
  9000. RangedFilter.prototype.getEndPost = function () {
  9001. return this.endPost_;
  9002. };
  9003. RangedFilter.prototype.matches = function (node) {
  9004. var isWithinStart = this.startIsInclusive_
  9005. ? this.index_.compare(this.getStartPost(), node) <= 0
  9006. : this.index_.compare(this.getStartPost(), node) < 0;
  9007. var isWithinEnd = this.endIsInclusive_
  9008. ? this.index_.compare(node, this.getEndPost()) <= 0
  9009. : this.index_.compare(node, this.getEndPost()) < 0;
  9010. return isWithinStart && isWithinEnd;
  9011. };
  9012. RangedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
  9013. if (!this.matches(new NamedNode(key, newChild))) {
  9014. newChild = ChildrenNode.EMPTY_NODE;
  9015. }
  9016. return this.indexedFilter_.updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
  9017. };
  9018. RangedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
  9019. if (newSnap.isLeafNode()) {
  9020. // Make sure we have a children node with the correct index, not a leaf node;
  9021. newSnap = ChildrenNode.EMPTY_NODE;
  9022. }
  9023. var filtered = newSnap.withIndex(this.index_);
  9024. // Don't support priorities on queries
  9025. filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
  9026. var self = this;
  9027. newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
  9028. if (!self.matches(new NamedNode(key, childNode))) {
  9029. filtered = filtered.updateImmediateChild(key, ChildrenNode.EMPTY_NODE);
  9030. }
  9031. });
  9032. return this.indexedFilter_.updateFullNode(oldSnap, filtered, optChangeAccumulator);
  9033. };
  9034. RangedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
  9035. // Don't support priorities on queries
  9036. return oldSnap;
  9037. };
  9038. RangedFilter.prototype.filtersNodes = function () {
  9039. return true;
  9040. };
  9041. RangedFilter.prototype.getIndexedFilter = function () {
  9042. return this.indexedFilter_;
  9043. };
  9044. RangedFilter.prototype.getIndex = function () {
  9045. return this.index_;
  9046. };
  9047. RangedFilter.getStartPost_ = function (params) {
  9048. if (params.hasStart()) {
  9049. var startName = params.getIndexStartName();
  9050. return params.getIndex().makePost(params.getIndexStartValue(), startName);
  9051. }
  9052. else {
  9053. return params.getIndex().minPost();
  9054. }
  9055. };
  9056. RangedFilter.getEndPost_ = function (params) {
  9057. if (params.hasEnd()) {
  9058. var endName = params.getIndexEndName();
  9059. return params.getIndex().makePost(params.getIndexEndValue(), endName);
  9060. }
  9061. else {
  9062. return params.getIndex().maxPost();
  9063. }
  9064. };
  9065. return RangedFilter;
  9066. }());
  9067. /**
  9068. * @license
  9069. * Copyright 2017 Google LLC
  9070. *
  9071. * Licensed under the Apache License, Version 2.0 (the "License");
  9072. * you may not use this file except in compliance with the License.
  9073. * You may obtain a copy of the License at
  9074. *
  9075. * http://www.apache.org/licenses/LICENSE-2.0
  9076. *
  9077. * Unless required by applicable law or agreed to in writing, software
  9078. * distributed under the License is distributed on an "AS IS" BASIS,
  9079. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9080. * See the License for the specific language governing permissions and
  9081. * limitations under the License.
  9082. */
  9083. /**
  9084. * Applies a limit and a range to a node and uses RangedFilter to do the heavy lifting where possible
  9085. */
  9086. var LimitedFilter = /** @class */ (function () {
  9087. function LimitedFilter(params) {
  9088. var _this = this;
  9089. this.withinDirectionalStart = function (node) {
  9090. return _this.reverse_ ? _this.withinEndPost(node) : _this.withinStartPost(node);
  9091. };
  9092. this.withinDirectionalEnd = function (node) {
  9093. return _this.reverse_ ? _this.withinStartPost(node) : _this.withinEndPost(node);
  9094. };
  9095. this.withinStartPost = function (node) {
  9096. var compareRes = _this.index_.compare(_this.rangedFilter_.getStartPost(), node);
  9097. return _this.startIsInclusive_ ? compareRes <= 0 : compareRes < 0;
  9098. };
  9099. this.withinEndPost = function (node) {
  9100. var compareRes = _this.index_.compare(node, _this.rangedFilter_.getEndPost());
  9101. return _this.endIsInclusive_ ? compareRes <= 0 : compareRes < 0;
  9102. };
  9103. this.rangedFilter_ = new RangedFilter(params);
  9104. this.index_ = params.getIndex();
  9105. this.limit_ = params.getLimit();
  9106. this.reverse_ = !params.isViewFromLeft();
  9107. this.startIsInclusive_ = !params.startAfterSet_;
  9108. this.endIsInclusive_ = !params.endBeforeSet_;
  9109. }
  9110. LimitedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
  9111. if (!this.rangedFilter_.matches(new NamedNode(key, newChild))) {
  9112. newChild = ChildrenNode.EMPTY_NODE;
  9113. }
  9114. if (snap.getImmediateChild(key).equals(newChild)) {
  9115. // No change
  9116. return snap;
  9117. }
  9118. else if (snap.numChildren() < this.limit_) {
  9119. return this.rangedFilter_
  9120. .getIndexedFilter()
  9121. .updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
  9122. }
  9123. else {
  9124. return this.fullLimitUpdateChild_(snap, key, newChild, source, optChangeAccumulator);
  9125. }
  9126. };
  9127. LimitedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
  9128. var filtered;
  9129. if (newSnap.isLeafNode() || newSnap.isEmpty()) {
  9130. // Make sure we have a children node with the correct index, not a leaf node;
  9131. filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
  9132. }
  9133. else {
  9134. if (this.limit_ * 2 < newSnap.numChildren() &&
  9135. newSnap.isIndexed(this.index_)) {
  9136. // Easier to build up a snapshot, since what we're given has more than twice the elements we want
  9137. filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
  9138. // anchor to the startPost, endPost, or last element as appropriate
  9139. var iterator = void 0;
  9140. if (this.reverse_) {
  9141. iterator = newSnap.getReverseIteratorFrom(this.rangedFilter_.getEndPost(), this.index_);
  9142. }
  9143. else {
  9144. iterator = newSnap.getIteratorFrom(this.rangedFilter_.getStartPost(), this.index_);
  9145. }
  9146. var count = 0;
  9147. while (iterator.hasNext() && count < this.limit_) {
  9148. var next = iterator.getNext();
  9149. if (!this.withinDirectionalStart(next)) {
  9150. // if we have not reached the start, skip to the next element
  9151. continue;
  9152. }
  9153. else if (!this.withinDirectionalEnd(next)) {
  9154. // if we have reached the end, stop adding elements
  9155. break;
  9156. }
  9157. else {
  9158. filtered = filtered.updateImmediateChild(next.name, next.node);
  9159. count++;
  9160. }
  9161. }
  9162. }
  9163. else {
  9164. // The snap contains less than twice the limit. Faster to delete from the snap than build up a new one
  9165. filtered = newSnap.withIndex(this.index_);
  9166. // Don't support priorities on queries
  9167. filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
  9168. var iterator = void 0;
  9169. if (this.reverse_) {
  9170. iterator = filtered.getReverseIterator(this.index_);
  9171. }
  9172. else {
  9173. iterator = filtered.getIterator(this.index_);
  9174. }
  9175. var count = 0;
  9176. while (iterator.hasNext()) {
  9177. var next = iterator.getNext();
  9178. var inRange = count < this.limit_ &&
  9179. this.withinDirectionalStart(next) &&
  9180. this.withinDirectionalEnd(next);
  9181. if (inRange) {
  9182. count++;
  9183. }
  9184. else {
  9185. filtered = filtered.updateImmediateChild(next.name, ChildrenNode.EMPTY_NODE);
  9186. }
  9187. }
  9188. }
  9189. }
  9190. return this.rangedFilter_
  9191. .getIndexedFilter()
  9192. .updateFullNode(oldSnap, filtered, optChangeAccumulator);
  9193. };
  9194. LimitedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
  9195. // Don't support priorities on queries
  9196. return oldSnap;
  9197. };
  9198. LimitedFilter.prototype.filtersNodes = function () {
  9199. return true;
  9200. };
  9201. LimitedFilter.prototype.getIndexedFilter = function () {
  9202. return this.rangedFilter_.getIndexedFilter();
  9203. };
  9204. LimitedFilter.prototype.getIndex = function () {
  9205. return this.index_;
  9206. };
  9207. LimitedFilter.prototype.fullLimitUpdateChild_ = function (snap, childKey, childSnap, source, changeAccumulator) {
  9208. // TODO: rename all cache stuff etc to general snap terminology
  9209. var cmp;
  9210. if (this.reverse_) {
  9211. var indexCmp_1 = this.index_.getCompare();
  9212. cmp = function (a, b) { return indexCmp_1(b, a); };
  9213. }
  9214. else {
  9215. cmp = this.index_.getCompare();
  9216. }
  9217. var oldEventCache = snap;
  9218. util.assert(oldEventCache.numChildren() === this.limit_, '');
  9219. var newChildNamedNode = new NamedNode(childKey, childSnap);
  9220. var windowBoundary = this.reverse_
  9221. ? oldEventCache.getFirstChild(this.index_)
  9222. : oldEventCache.getLastChild(this.index_);
  9223. var inRange = this.rangedFilter_.matches(newChildNamedNode);
  9224. if (oldEventCache.hasChild(childKey)) {
  9225. var oldChildSnap = oldEventCache.getImmediateChild(childKey);
  9226. var nextChild = source.getChildAfterChild(this.index_, windowBoundary, this.reverse_);
  9227. while (nextChild != null &&
  9228. (nextChild.name === childKey || oldEventCache.hasChild(nextChild.name))) {
  9229. // There is a weird edge case where a node is updated as part of a merge in the write tree, but hasn't
  9230. // been applied to the limited filter yet. Ignore this next child which will be updated later in
  9231. // the limited filter...
  9232. nextChild = source.getChildAfterChild(this.index_, nextChild, this.reverse_);
  9233. }
  9234. var compareNext = nextChild == null ? 1 : cmp(nextChild, newChildNamedNode);
  9235. var remainsInWindow = inRange && !childSnap.isEmpty() && compareNext >= 0;
  9236. if (remainsInWindow) {
  9237. if (changeAccumulator != null) {
  9238. changeAccumulator.trackChildChange(changeChildChanged(childKey, childSnap, oldChildSnap));
  9239. }
  9240. return oldEventCache.updateImmediateChild(childKey, childSnap);
  9241. }
  9242. else {
  9243. if (changeAccumulator != null) {
  9244. changeAccumulator.trackChildChange(changeChildRemoved(childKey, oldChildSnap));
  9245. }
  9246. var newEventCache = oldEventCache.updateImmediateChild(childKey, ChildrenNode.EMPTY_NODE);
  9247. var nextChildInRange = nextChild != null && this.rangedFilter_.matches(nextChild);
  9248. if (nextChildInRange) {
  9249. if (changeAccumulator != null) {
  9250. changeAccumulator.trackChildChange(changeChildAdded(nextChild.name, nextChild.node));
  9251. }
  9252. return newEventCache.updateImmediateChild(nextChild.name, nextChild.node);
  9253. }
  9254. else {
  9255. return newEventCache;
  9256. }
  9257. }
  9258. }
  9259. else if (childSnap.isEmpty()) {
  9260. // we're deleting a node, but it was not in the window, so ignore it
  9261. return snap;
  9262. }
  9263. else if (inRange) {
  9264. if (cmp(windowBoundary, newChildNamedNode) >= 0) {
  9265. if (changeAccumulator != null) {
  9266. changeAccumulator.trackChildChange(changeChildRemoved(windowBoundary.name, windowBoundary.node));
  9267. changeAccumulator.trackChildChange(changeChildAdded(childKey, childSnap));
  9268. }
  9269. return oldEventCache
  9270. .updateImmediateChild(childKey, childSnap)
  9271. .updateImmediateChild(windowBoundary.name, ChildrenNode.EMPTY_NODE);
  9272. }
  9273. else {
  9274. return snap;
  9275. }
  9276. }
  9277. else {
  9278. return snap;
  9279. }
  9280. };
  9281. return LimitedFilter;
  9282. }());
  9283. /**
  9284. * @license
  9285. * Copyright 2017 Google LLC
  9286. *
  9287. * Licensed under the Apache License, Version 2.0 (the "License");
  9288. * you may not use this file except in compliance with the License.
  9289. * You may obtain a copy of the License at
  9290. *
  9291. * http://www.apache.org/licenses/LICENSE-2.0
  9292. *
  9293. * Unless required by applicable law or agreed to in writing, software
  9294. * distributed under the License is distributed on an "AS IS" BASIS,
  9295. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9296. * See the License for the specific language governing permissions and
  9297. * limitations under the License.
  9298. */
  9299. /**
  9300. * This class is an immutable-from-the-public-api struct containing a set of query parameters defining a
  9301. * range to be returned for a particular location. It is assumed that validation of parameters is done at the
  9302. * user-facing API level, so it is not done here.
  9303. *
  9304. * @internal
  9305. */
  9306. var QueryParams = /** @class */ (function () {
  9307. function QueryParams() {
  9308. this.limitSet_ = false;
  9309. this.startSet_ = false;
  9310. this.startNameSet_ = false;
  9311. this.startAfterSet_ = false; // can only be true if startSet_ is true
  9312. this.endSet_ = false;
  9313. this.endNameSet_ = false;
  9314. this.endBeforeSet_ = false; // can only be true if endSet_ is true
  9315. this.limit_ = 0;
  9316. this.viewFrom_ = '';
  9317. this.indexStartValue_ = null;
  9318. this.indexStartName_ = '';
  9319. this.indexEndValue_ = null;
  9320. this.indexEndName_ = '';
  9321. this.index_ = PRIORITY_INDEX;
  9322. }
  9323. QueryParams.prototype.hasStart = function () {
  9324. return this.startSet_;
  9325. };
  9326. /**
  9327. * @returns True if it would return from left.
  9328. */
  9329. QueryParams.prototype.isViewFromLeft = function () {
  9330. if (this.viewFrom_ === '') {
  9331. // limit(), rather than limitToFirst or limitToLast was called.
  9332. // This means that only one of startSet_ and endSet_ is true. Use them
  9333. // to calculate which side of the view to anchor to. If neither is set,
  9334. // anchor to the end.
  9335. return this.startSet_;
  9336. }
  9337. else {
  9338. return this.viewFrom_ === "l" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_LEFT */;
  9339. }
  9340. };
  9341. /**
  9342. * Only valid to call if hasStart() returns true
  9343. */
  9344. QueryParams.prototype.getIndexStartValue = function () {
  9345. util.assert(this.startSet_, 'Only valid if start has been set');
  9346. return this.indexStartValue_;
  9347. };
  9348. /**
  9349. * Only valid to call if hasStart() returns true.
  9350. * Returns the starting key name for the range defined by these query parameters
  9351. */
  9352. QueryParams.prototype.getIndexStartName = function () {
  9353. util.assert(this.startSet_, 'Only valid if start has been set');
  9354. if (this.startNameSet_) {
  9355. return this.indexStartName_;
  9356. }
  9357. else {
  9358. return MIN_NAME;
  9359. }
  9360. };
  9361. QueryParams.prototype.hasEnd = function () {
  9362. return this.endSet_;
  9363. };
  9364. /**
  9365. * Only valid to call if hasEnd() returns true.
  9366. */
  9367. QueryParams.prototype.getIndexEndValue = function () {
  9368. util.assert(this.endSet_, 'Only valid if end has been set');
  9369. return this.indexEndValue_;
  9370. };
  9371. /**
  9372. * Only valid to call if hasEnd() returns true.
  9373. * Returns the end key name for the range defined by these query parameters
  9374. */
  9375. QueryParams.prototype.getIndexEndName = function () {
  9376. util.assert(this.endSet_, 'Only valid if end has been set');
  9377. if (this.endNameSet_) {
  9378. return this.indexEndName_;
  9379. }
  9380. else {
  9381. return MAX_NAME;
  9382. }
  9383. };
  9384. QueryParams.prototype.hasLimit = function () {
  9385. return this.limitSet_;
  9386. };
  9387. /**
  9388. * @returns True if a limit has been set and it has been explicitly anchored
  9389. */
  9390. QueryParams.prototype.hasAnchoredLimit = function () {
  9391. return this.limitSet_ && this.viewFrom_ !== '';
  9392. };
  9393. /**
  9394. * Only valid to call if hasLimit() returns true
  9395. */
  9396. QueryParams.prototype.getLimit = function () {
  9397. util.assert(this.limitSet_, 'Only valid if limit has been set');
  9398. return this.limit_;
  9399. };
  9400. QueryParams.prototype.getIndex = function () {
  9401. return this.index_;
  9402. };
  9403. QueryParams.prototype.loadsAllData = function () {
  9404. return !(this.startSet_ || this.endSet_ || this.limitSet_);
  9405. };
  9406. QueryParams.prototype.isDefault = function () {
  9407. return this.loadsAllData() && this.index_ === PRIORITY_INDEX;
  9408. };
  9409. QueryParams.prototype.copy = function () {
  9410. var copy = new QueryParams();
  9411. copy.limitSet_ = this.limitSet_;
  9412. copy.limit_ = this.limit_;
  9413. copy.startSet_ = this.startSet_;
  9414. copy.startAfterSet_ = this.startAfterSet_;
  9415. copy.indexStartValue_ = this.indexStartValue_;
  9416. copy.startNameSet_ = this.startNameSet_;
  9417. copy.indexStartName_ = this.indexStartName_;
  9418. copy.endSet_ = this.endSet_;
  9419. copy.endBeforeSet_ = this.endBeforeSet_;
  9420. copy.indexEndValue_ = this.indexEndValue_;
  9421. copy.endNameSet_ = this.endNameSet_;
  9422. copy.indexEndName_ = this.indexEndName_;
  9423. copy.index_ = this.index_;
  9424. copy.viewFrom_ = this.viewFrom_;
  9425. return copy;
  9426. };
  9427. return QueryParams;
  9428. }());
  9429. function queryParamsGetNodeFilter(queryParams) {
  9430. if (queryParams.loadsAllData()) {
  9431. return new IndexedFilter(queryParams.getIndex());
  9432. }
  9433. else if (queryParams.hasLimit()) {
  9434. return new LimitedFilter(queryParams);
  9435. }
  9436. else {
  9437. return new RangedFilter(queryParams);
  9438. }
  9439. }
  9440. function queryParamsLimitToFirst(queryParams, newLimit) {
  9441. var newParams = queryParams.copy();
  9442. newParams.limitSet_ = true;
  9443. newParams.limit_ = newLimit;
  9444. newParams.viewFrom_ = "l" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_LEFT */;
  9445. return newParams;
  9446. }
  9447. function queryParamsLimitToLast(queryParams, newLimit) {
  9448. var newParams = queryParams.copy();
  9449. newParams.limitSet_ = true;
  9450. newParams.limit_ = newLimit;
  9451. newParams.viewFrom_ = "r" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_RIGHT */;
  9452. return newParams;
  9453. }
  9454. function queryParamsStartAt(queryParams, indexValue, key) {
  9455. var newParams = queryParams.copy();
  9456. newParams.startSet_ = true;
  9457. if (indexValue === undefined) {
  9458. indexValue = null;
  9459. }
  9460. newParams.indexStartValue_ = indexValue;
  9461. if (key != null) {
  9462. newParams.startNameSet_ = true;
  9463. newParams.indexStartName_ = key;
  9464. }
  9465. else {
  9466. newParams.startNameSet_ = false;
  9467. newParams.indexStartName_ = '';
  9468. }
  9469. return newParams;
  9470. }
  9471. function queryParamsStartAfter(queryParams, indexValue, key) {
  9472. var params;
  9473. if (queryParams.index_ === KEY_INDEX || !!key) {
  9474. params = queryParamsStartAt(queryParams, indexValue, key);
  9475. }
  9476. else {
  9477. params = queryParamsStartAt(queryParams, indexValue, MAX_NAME);
  9478. }
  9479. params.startAfterSet_ = true;
  9480. return params;
  9481. }
  9482. function queryParamsEndAt(queryParams, indexValue, key) {
  9483. var newParams = queryParams.copy();
  9484. newParams.endSet_ = true;
  9485. if (indexValue === undefined) {
  9486. indexValue = null;
  9487. }
  9488. newParams.indexEndValue_ = indexValue;
  9489. if (key !== undefined) {
  9490. newParams.endNameSet_ = true;
  9491. newParams.indexEndName_ = key;
  9492. }
  9493. else {
  9494. newParams.endNameSet_ = false;
  9495. newParams.indexEndName_ = '';
  9496. }
  9497. return newParams;
  9498. }
  9499. function queryParamsEndBefore(queryParams, indexValue, key) {
  9500. var params;
  9501. if (queryParams.index_ === KEY_INDEX || !!key) {
  9502. params = queryParamsEndAt(queryParams, indexValue, key);
  9503. }
  9504. else {
  9505. params = queryParamsEndAt(queryParams, indexValue, MIN_NAME);
  9506. }
  9507. params.endBeforeSet_ = true;
  9508. return params;
  9509. }
  9510. function queryParamsOrderBy(queryParams, index) {
  9511. var newParams = queryParams.copy();
  9512. newParams.index_ = index;
  9513. return newParams;
  9514. }
  9515. /**
  9516. * Returns a set of REST query string parameters representing this query.
  9517. *
  9518. * @returns query string parameters
  9519. */
  9520. function queryParamsToRestQueryStringParameters(queryParams) {
  9521. var qs = {};
  9522. if (queryParams.isDefault()) {
  9523. return qs;
  9524. }
  9525. var orderBy;
  9526. if (queryParams.index_ === PRIORITY_INDEX) {
  9527. orderBy = "$priority" /* REST_QUERY_CONSTANTS.PRIORITY_INDEX */;
  9528. }
  9529. else if (queryParams.index_ === VALUE_INDEX) {
  9530. orderBy = "$value" /* REST_QUERY_CONSTANTS.VALUE_INDEX */;
  9531. }
  9532. else if (queryParams.index_ === KEY_INDEX) {
  9533. orderBy = "$key" /* REST_QUERY_CONSTANTS.KEY_INDEX */;
  9534. }
  9535. else {
  9536. util.assert(queryParams.index_ instanceof PathIndex, 'Unrecognized index type!');
  9537. orderBy = queryParams.index_.toString();
  9538. }
  9539. qs["orderBy" /* REST_QUERY_CONSTANTS.ORDER_BY */] = util.stringify(orderBy);
  9540. if (queryParams.startSet_) {
  9541. var startParam = queryParams.startAfterSet_
  9542. ? "startAfter" /* REST_QUERY_CONSTANTS.START_AFTER */
  9543. : "startAt" /* REST_QUERY_CONSTANTS.START_AT */;
  9544. qs[startParam] = util.stringify(queryParams.indexStartValue_);
  9545. if (queryParams.startNameSet_) {
  9546. qs[startParam] += ',' + util.stringify(queryParams.indexStartName_);
  9547. }
  9548. }
  9549. if (queryParams.endSet_) {
  9550. var endParam = queryParams.endBeforeSet_
  9551. ? "endBefore" /* REST_QUERY_CONSTANTS.END_BEFORE */
  9552. : "endAt" /* REST_QUERY_CONSTANTS.END_AT */;
  9553. qs[endParam] = util.stringify(queryParams.indexEndValue_);
  9554. if (queryParams.endNameSet_) {
  9555. qs[endParam] += ',' + util.stringify(queryParams.indexEndName_);
  9556. }
  9557. }
  9558. if (queryParams.limitSet_) {
  9559. if (queryParams.isViewFromLeft()) {
  9560. qs["limitToFirst" /* REST_QUERY_CONSTANTS.LIMIT_TO_FIRST */] = queryParams.limit_;
  9561. }
  9562. else {
  9563. qs["limitToLast" /* REST_QUERY_CONSTANTS.LIMIT_TO_LAST */] = queryParams.limit_;
  9564. }
  9565. }
  9566. return qs;
  9567. }
  9568. function queryParamsGetQueryObject(queryParams) {
  9569. var obj = {};
  9570. if (queryParams.startSet_) {
  9571. obj["sp" /* WIRE_PROTOCOL_CONSTANTS.INDEX_START_VALUE */] =
  9572. queryParams.indexStartValue_;
  9573. if (queryParams.startNameSet_) {
  9574. obj["sn" /* WIRE_PROTOCOL_CONSTANTS.INDEX_START_NAME */] =
  9575. queryParams.indexStartName_;
  9576. }
  9577. obj["sin" /* WIRE_PROTOCOL_CONSTANTS.INDEX_START_IS_INCLUSIVE */] =
  9578. !queryParams.startAfterSet_;
  9579. }
  9580. if (queryParams.endSet_) {
  9581. obj["ep" /* WIRE_PROTOCOL_CONSTANTS.INDEX_END_VALUE */] = queryParams.indexEndValue_;
  9582. if (queryParams.endNameSet_) {
  9583. obj["en" /* WIRE_PROTOCOL_CONSTANTS.INDEX_END_NAME */] = queryParams.indexEndName_;
  9584. }
  9585. obj["ein" /* WIRE_PROTOCOL_CONSTANTS.INDEX_END_IS_INCLUSIVE */] =
  9586. !queryParams.endBeforeSet_;
  9587. }
  9588. if (queryParams.limitSet_) {
  9589. obj["l" /* WIRE_PROTOCOL_CONSTANTS.LIMIT */] = queryParams.limit_;
  9590. var viewFrom = queryParams.viewFrom_;
  9591. if (viewFrom === '') {
  9592. if (queryParams.isViewFromLeft()) {
  9593. viewFrom = "l" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_LEFT */;
  9594. }
  9595. else {
  9596. viewFrom = "r" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_RIGHT */;
  9597. }
  9598. }
  9599. obj["vf" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM */] = viewFrom;
  9600. }
  9601. // For now, priority index is the default, so we only specify if it's some other index
  9602. if (queryParams.index_ !== PRIORITY_INDEX) {
  9603. obj["i" /* WIRE_PROTOCOL_CONSTANTS.INDEX */] = queryParams.index_.toString();
  9604. }
  9605. return obj;
  9606. }
  9607. /**
  9608. * @license
  9609. * Copyright 2017 Google LLC
  9610. *
  9611. * Licensed under the Apache License, Version 2.0 (the "License");
  9612. * you may not use this file except in compliance with the License.
  9613. * You may obtain a copy of the License at
  9614. *
  9615. * http://www.apache.org/licenses/LICENSE-2.0
  9616. *
  9617. * Unless required by applicable law or agreed to in writing, software
  9618. * distributed under the License is distributed on an "AS IS" BASIS,
  9619. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9620. * See the License for the specific language governing permissions and
  9621. * limitations under the License.
  9622. */
  9623. /**
  9624. * An implementation of ServerActions that communicates with the server via REST requests.
  9625. * This is mostly useful for compatibility with crawlers, where we don't want to spin up a full
  9626. * persistent connection (using WebSockets or long-polling)
  9627. */
  9628. var ReadonlyRestClient = /** @class */ (function (_super) {
  9629. tslib.__extends(ReadonlyRestClient, _super);
  9630. /**
  9631. * @param repoInfo_ - Data about the namespace we are connecting to
  9632. * @param onDataUpdate_ - A callback for new data from the server
  9633. */
  9634. function ReadonlyRestClient(repoInfo_, onDataUpdate_, authTokenProvider_, appCheckTokenProvider_) {
  9635. var _this = _super.call(this) || this;
  9636. _this.repoInfo_ = repoInfo_;
  9637. _this.onDataUpdate_ = onDataUpdate_;
  9638. _this.authTokenProvider_ = authTokenProvider_;
  9639. _this.appCheckTokenProvider_ = appCheckTokenProvider_;
  9640. /** @private {function(...[*])} */
  9641. _this.log_ = logWrapper('p:rest:');
  9642. /**
  9643. * We don't actually need to track listens, except to prevent us calling an onComplete for a listen
  9644. * that's been removed. :-/
  9645. */
  9646. _this.listens_ = {};
  9647. return _this;
  9648. }
  9649. ReadonlyRestClient.prototype.reportStats = function (stats) {
  9650. throw new Error('Method not implemented.');
  9651. };
  9652. ReadonlyRestClient.getListenId_ = function (query, tag) {
  9653. if (tag !== undefined) {
  9654. return 'tag$' + tag;
  9655. }
  9656. else {
  9657. util.assert(query._queryParams.isDefault(), "should have a tag if it's not a default query.");
  9658. return query._path.toString();
  9659. }
  9660. };
  9661. /** @inheritDoc */
  9662. ReadonlyRestClient.prototype.listen = function (query, currentHashFn, tag, onComplete) {
  9663. var _this = this;
  9664. var pathString = query._path.toString();
  9665. this.log_('Listen called for ' + pathString + ' ' + query._queryIdentifier);
  9666. // Mark this listener so we can tell if it's removed.
  9667. var listenId = ReadonlyRestClient.getListenId_(query, tag);
  9668. var thisListen = {};
  9669. this.listens_[listenId] = thisListen;
  9670. var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
  9671. this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
  9672. var data = result;
  9673. if (error === 404) {
  9674. data = null;
  9675. error = null;
  9676. }
  9677. if (error === null) {
  9678. _this.onDataUpdate_(pathString, data, /*isMerge=*/ false, tag);
  9679. }
  9680. if (util.safeGet(_this.listens_, listenId) === thisListen) {
  9681. var status_1;
  9682. if (!error) {
  9683. status_1 = 'ok';
  9684. }
  9685. else if (error === 401) {
  9686. status_1 = 'permission_denied';
  9687. }
  9688. else {
  9689. status_1 = 'rest_error:' + error;
  9690. }
  9691. onComplete(status_1, null);
  9692. }
  9693. });
  9694. };
  9695. /** @inheritDoc */
  9696. ReadonlyRestClient.prototype.unlisten = function (query, tag) {
  9697. var listenId = ReadonlyRestClient.getListenId_(query, tag);
  9698. delete this.listens_[listenId];
  9699. };
  9700. ReadonlyRestClient.prototype.get = function (query) {
  9701. var _this = this;
  9702. var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
  9703. var pathString = query._path.toString();
  9704. var deferred = new util.Deferred();
  9705. this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
  9706. var data = result;
  9707. if (error === 404) {
  9708. data = null;
  9709. error = null;
  9710. }
  9711. if (error === null) {
  9712. _this.onDataUpdate_(pathString, data,
  9713. /*isMerge=*/ false,
  9714. /*tag=*/ null);
  9715. deferred.resolve(data);
  9716. }
  9717. else {
  9718. deferred.reject(new Error(data));
  9719. }
  9720. });
  9721. return deferred.promise;
  9722. };
  9723. /** @inheritDoc */
  9724. ReadonlyRestClient.prototype.refreshAuthToken = function (token) {
  9725. // no-op since we just always call getToken.
  9726. };
  9727. /**
  9728. * Performs a REST request to the given path, with the provided query string parameters,
  9729. * and any auth credentials we have.
  9730. */
  9731. ReadonlyRestClient.prototype.restRequest_ = function (pathString, queryStringParameters, callback) {
  9732. var _this = this;
  9733. if (queryStringParameters === void 0) { queryStringParameters = {}; }
  9734. queryStringParameters['format'] = 'export';
  9735. return Promise.all([
  9736. this.authTokenProvider_.getToken(/*forceRefresh=*/ false),
  9737. this.appCheckTokenProvider_.getToken(/*forceRefresh=*/ false)
  9738. ]).then(function (_a) {
  9739. var _b = tslib.__read(_a, 2), authToken = _b[0], appCheckToken = _b[1];
  9740. if (authToken && authToken.accessToken) {
  9741. queryStringParameters['auth'] = authToken.accessToken;
  9742. }
  9743. if (appCheckToken && appCheckToken.token) {
  9744. queryStringParameters['ac'] = appCheckToken.token;
  9745. }
  9746. var url = (_this.repoInfo_.secure ? 'https://' : 'http://') +
  9747. _this.repoInfo_.host +
  9748. pathString +
  9749. '?' +
  9750. 'ns=' +
  9751. _this.repoInfo_.namespace +
  9752. util.querystring(queryStringParameters);
  9753. _this.log_('Sending REST request for ' + url);
  9754. var xhr = new XMLHttpRequest();
  9755. xhr.onreadystatechange = function () {
  9756. if (callback && xhr.readyState === 4) {
  9757. _this.log_('REST Response for ' + url + ' received. status:', xhr.status, 'response:', xhr.responseText);
  9758. var res = null;
  9759. if (xhr.status >= 200 && xhr.status < 300) {
  9760. try {
  9761. res = util.jsonEval(xhr.responseText);
  9762. }
  9763. catch (e) {
  9764. warn$1('Failed to parse JSON response for ' +
  9765. url +
  9766. ': ' +
  9767. xhr.responseText);
  9768. }
  9769. callback(null, res);
  9770. }
  9771. else {
  9772. // 401 and 404 are expected.
  9773. if (xhr.status !== 401 && xhr.status !== 404) {
  9774. warn$1('Got unsuccessful REST response for ' +
  9775. url +
  9776. ' Status: ' +
  9777. xhr.status);
  9778. }
  9779. callback(xhr.status);
  9780. }
  9781. callback = null;
  9782. }
  9783. };
  9784. xhr.open('GET', url, /*asynchronous=*/ true);
  9785. xhr.send();
  9786. });
  9787. };
  9788. return ReadonlyRestClient;
  9789. }(ServerActions));
  9790. /**
  9791. * @license
  9792. * Copyright 2017 Google LLC
  9793. *
  9794. * Licensed under the Apache License, Version 2.0 (the "License");
  9795. * you may not use this file except in compliance with the License.
  9796. * You may obtain a copy of the License at
  9797. *
  9798. * http://www.apache.org/licenses/LICENSE-2.0
  9799. *
  9800. * Unless required by applicable law or agreed to in writing, software
  9801. * distributed under the License is distributed on an "AS IS" BASIS,
  9802. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9803. * See the License for the specific language governing permissions and
  9804. * limitations under the License.
  9805. */
  9806. /**
  9807. * Mutable object which basically just stores a reference to the "latest" immutable snapshot.
  9808. */
  9809. var SnapshotHolder = /** @class */ (function () {
  9810. function SnapshotHolder() {
  9811. this.rootNode_ = ChildrenNode.EMPTY_NODE;
  9812. }
  9813. SnapshotHolder.prototype.getNode = function (path) {
  9814. return this.rootNode_.getChild(path);
  9815. };
  9816. SnapshotHolder.prototype.updateSnapshot = function (path, newSnapshotNode) {
  9817. this.rootNode_ = this.rootNode_.updateChild(path, newSnapshotNode);
  9818. };
  9819. return SnapshotHolder;
  9820. }());
  9821. /**
  9822. * @license
  9823. * Copyright 2017 Google LLC
  9824. *
  9825. * Licensed under the Apache License, Version 2.0 (the "License");
  9826. * you may not use this file except in compliance with the License.
  9827. * You may obtain a copy of the License at
  9828. *
  9829. * http://www.apache.org/licenses/LICENSE-2.0
  9830. *
  9831. * Unless required by applicable law or agreed to in writing, software
  9832. * distributed under the License is distributed on an "AS IS" BASIS,
  9833. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9834. * See the License for the specific language governing permissions and
  9835. * limitations under the License.
  9836. */
  9837. function newSparseSnapshotTree() {
  9838. return {
  9839. value: null,
  9840. children: new Map()
  9841. };
  9842. }
  9843. /**
  9844. * Stores the given node at the specified path. If there is already a node
  9845. * at a shallower path, it merges the new data into that snapshot node.
  9846. *
  9847. * @param path - Path to look up snapshot for.
  9848. * @param data - The new data, or null.
  9849. */
  9850. function sparseSnapshotTreeRemember(sparseSnapshotTree, path, data) {
  9851. if (pathIsEmpty(path)) {
  9852. sparseSnapshotTree.value = data;
  9853. sparseSnapshotTree.children.clear();
  9854. }
  9855. else if (sparseSnapshotTree.value !== null) {
  9856. sparseSnapshotTree.value = sparseSnapshotTree.value.updateChild(path, data);
  9857. }
  9858. else {
  9859. var childKey = pathGetFront(path);
  9860. if (!sparseSnapshotTree.children.has(childKey)) {
  9861. sparseSnapshotTree.children.set(childKey, newSparseSnapshotTree());
  9862. }
  9863. var child = sparseSnapshotTree.children.get(childKey);
  9864. path = pathPopFront(path);
  9865. sparseSnapshotTreeRemember(child, path, data);
  9866. }
  9867. }
  9868. /**
  9869. * Purge the data at path from the cache.
  9870. *
  9871. * @param path - Path to look up snapshot for.
  9872. * @returns True if this node should now be removed.
  9873. */
  9874. function sparseSnapshotTreeForget(sparseSnapshotTree, path) {
  9875. if (pathIsEmpty(path)) {
  9876. sparseSnapshotTree.value = null;
  9877. sparseSnapshotTree.children.clear();
  9878. return true;
  9879. }
  9880. else {
  9881. if (sparseSnapshotTree.value !== null) {
  9882. if (sparseSnapshotTree.value.isLeafNode()) {
  9883. // We're trying to forget a node that doesn't exist
  9884. return false;
  9885. }
  9886. else {
  9887. var value = sparseSnapshotTree.value;
  9888. sparseSnapshotTree.value = null;
  9889. value.forEachChild(PRIORITY_INDEX, function (key, tree) {
  9890. sparseSnapshotTreeRemember(sparseSnapshotTree, new Path(key), tree);
  9891. });
  9892. return sparseSnapshotTreeForget(sparseSnapshotTree, path);
  9893. }
  9894. }
  9895. else if (sparseSnapshotTree.children.size > 0) {
  9896. var childKey = pathGetFront(path);
  9897. path = pathPopFront(path);
  9898. if (sparseSnapshotTree.children.has(childKey)) {
  9899. var safeToRemove = sparseSnapshotTreeForget(sparseSnapshotTree.children.get(childKey), path);
  9900. if (safeToRemove) {
  9901. sparseSnapshotTree.children.delete(childKey);
  9902. }
  9903. }
  9904. return sparseSnapshotTree.children.size === 0;
  9905. }
  9906. else {
  9907. return true;
  9908. }
  9909. }
  9910. }
  9911. /**
  9912. * Recursively iterates through all of the stored tree and calls the
  9913. * callback on each one.
  9914. *
  9915. * @param prefixPath - Path to look up node for.
  9916. * @param func - The function to invoke for each tree.
  9917. */
  9918. function sparseSnapshotTreeForEachTree(sparseSnapshotTree, prefixPath, func) {
  9919. if (sparseSnapshotTree.value !== null) {
  9920. func(prefixPath, sparseSnapshotTree.value);
  9921. }
  9922. else {
  9923. sparseSnapshotTreeForEachChild(sparseSnapshotTree, function (key, tree) {
  9924. var path = new Path(prefixPath.toString() + '/' + key);
  9925. sparseSnapshotTreeForEachTree(tree, path, func);
  9926. });
  9927. }
  9928. }
  9929. /**
  9930. * Iterates through each immediate child and triggers the callback.
  9931. * Only seems to be used in tests.
  9932. *
  9933. * @param func - The function to invoke for each child.
  9934. */
  9935. function sparseSnapshotTreeForEachChild(sparseSnapshotTree, func) {
  9936. sparseSnapshotTree.children.forEach(function (tree, key) {
  9937. func(key, tree);
  9938. });
  9939. }
  9940. /**
  9941. * @license
  9942. * Copyright 2017 Google LLC
  9943. *
  9944. * Licensed under the Apache License, Version 2.0 (the "License");
  9945. * you may not use this file except in compliance with the License.
  9946. * You may obtain a copy of the License at
  9947. *
  9948. * http://www.apache.org/licenses/LICENSE-2.0
  9949. *
  9950. * Unless required by applicable law or agreed to in writing, software
  9951. * distributed under the License is distributed on an "AS IS" BASIS,
  9952. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9953. * See the License for the specific language governing permissions and
  9954. * limitations under the License.
  9955. */
  9956. /**
  9957. * Returns the delta from the previous call to get stats.
  9958. *
  9959. * @param collection_ - The collection to "listen" to.
  9960. */
  9961. var StatsListener = /** @class */ (function () {
  9962. function StatsListener(collection_) {
  9963. this.collection_ = collection_;
  9964. this.last_ = null;
  9965. }
  9966. StatsListener.prototype.get = function () {
  9967. var newStats = this.collection_.get();
  9968. var delta = tslib.__assign({}, newStats);
  9969. if (this.last_) {
  9970. each(this.last_, function (stat, value) {
  9971. delta[stat] = delta[stat] - value;
  9972. });
  9973. }
  9974. this.last_ = newStats;
  9975. return delta;
  9976. };
  9977. return StatsListener;
  9978. }());
  9979. /**
  9980. * @license
  9981. * Copyright 2017 Google LLC
  9982. *
  9983. * Licensed under the Apache License, Version 2.0 (the "License");
  9984. * you may not use this file except in compliance with the License.
  9985. * You may obtain a copy of the License at
  9986. *
  9987. * http://www.apache.org/licenses/LICENSE-2.0
  9988. *
  9989. * Unless required by applicable law or agreed to in writing, software
  9990. * distributed under the License is distributed on an "AS IS" BASIS,
  9991. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9992. * See the License for the specific language governing permissions and
  9993. * limitations under the License.
  9994. */
  9995. // Assuming some apps may have a short amount of time on page, and a bulk of firebase operations probably
  9996. // happen on page load, we try to report our first set of stats pretty quickly, but we wait at least 10
  9997. // seconds to try to ensure the Firebase connection is established / settled.
  9998. var FIRST_STATS_MIN_TIME = 10 * 1000;
  9999. var FIRST_STATS_MAX_TIME = 30 * 1000;
  10000. // We'll continue to report stats on average every 5 minutes.
  10001. var REPORT_STATS_INTERVAL = 5 * 60 * 1000;
  10002. var StatsReporter = /** @class */ (function () {
  10003. function StatsReporter(collection, server_) {
  10004. this.server_ = server_;
  10005. this.statsToReport_ = {};
  10006. this.statsListener_ = new StatsListener(collection);
  10007. var timeout = FIRST_STATS_MIN_TIME +
  10008. (FIRST_STATS_MAX_TIME - FIRST_STATS_MIN_TIME) * Math.random();
  10009. setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(timeout));
  10010. }
  10011. StatsReporter.prototype.reportStats_ = function () {
  10012. var _this = this;
  10013. var stats = this.statsListener_.get();
  10014. var reportedStats = {};
  10015. var haveStatsToReport = false;
  10016. each(stats, function (stat, value) {
  10017. if (value > 0 && util.contains(_this.statsToReport_, stat)) {
  10018. reportedStats[stat] = value;
  10019. haveStatsToReport = true;
  10020. }
  10021. });
  10022. if (haveStatsToReport) {
  10023. this.server_.reportStats(reportedStats);
  10024. }
  10025. // queue our next run.
  10026. setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(Math.random() * 2 * REPORT_STATS_INTERVAL));
  10027. };
  10028. return StatsReporter;
  10029. }());
  10030. /**
  10031. * @license
  10032. * Copyright 2017 Google LLC
  10033. *
  10034. * Licensed under the Apache License, Version 2.0 (the "License");
  10035. * you may not use this file except in compliance with the License.
  10036. * You may obtain a copy of the License at
  10037. *
  10038. * http://www.apache.org/licenses/LICENSE-2.0
  10039. *
  10040. * Unless required by applicable law or agreed to in writing, software
  10041. * distributed under the License is distributed on an "AS IS" BASIS,
  10042. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10043. * See the License for the specific language governing permissions and
  10044. * limitations under the License.
  10045. */
  10046. /**
  10047. *
  10048. * @enum
  10049. */
  10050. var OperationType;
  10051. (function (OperationType) {
  10052. OperationType[OperationType["OVERWRITE"] = 0] = "OVERWRITE";
  10053. OperationType[OperationType["MERGE"] = 1] = "MERGE";
  10054. OperationType[OperationType["ACK_USER_WRITE"] = 2] = "ACK_USER_WRITE";
  10055. OperationType[OperationType["LISTEN_COMPLETE"] = 3] = "LISTEN_COMPLETE";
  10056. })(OperationType || (OperationType = {}));
  10057. function newOperationSourceUser() {
  10058. return {
  10059. fromUser: true,
  10060. fromServer: false,
  10061. queryId: null,
  10062. tagged: false
  10063. };
  10064. }
  10065. function newOperationSourceServer() {
  10066. return {
  10067. fromUser: false,
  10068. fromServer: true,
  10069. queryId: null,
  10070. tagged: false
  10071. };
  10072. }
  10073. function newOperationSourceServerTaggedQuery(queryId) {
  10074. return {
  10075. fromUser: false,
  10076. fromServer: true,
  10077. queryId: queryId,
  10078. tagged: true
  10079. };
  10080. }
  10081. /**
  10082. * @license
  10083. * Copyright 2017 Google LLC
  10084. *
  10085. * Licensed under the Apache License, Version 2.0 (the "License");
  10086. * you may not use this file except in compliance with the License.
  10087. * You may obtain a copy of the License at
  10088. *
  10089. * http://www.apache.org/licenses/LICENSE-2.0
  10090. *
  10091. * Unless required by applicable law or agreed to in writing, software
  10092. * distributed under the License is distributed on an "AS IS" BASIS,
  10093. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10094. * See the License for the specific language governing permissions and
  10095. * limitations under the License.
  10096. */
  10097. var AckUserWrite = /** @class */ (function () {
  10098. /**
  10099. * @param affectedTree - A tree containing true for each affected path. Affected paths can't overlap.
  10100. */
  10101. function AckUserWrite(
  10102. /** @inheritDoc */ path,
  10103. /** @inheritDoc */ affectedTree,
  10104. /** @inheritDoc */ revert) {
  10105. this.path = path;
  10106. this.affectedTree = affectedTree;
  10107. this.revert = revert;
  10108. /** @inheritDoc */
  10109. this.type = OperationType.ACK_USER_WRITE;
  10110. /** @inheritDoc */
  10111. this.source = newOperationSourceUser();
  10112. }
  10113. AckUserWrite.prototype.operationForChild = function (childName) {
  10114. if (!pathIsEmpty(this.path)) {
  10115. util.assert(pathGetFront(this.path) === childName, 'operationForChild called for unrelated child.');
  10116. return new AckUserWrite(pathPopFront(this.path), this.affectedTree, this.revert);
  10117. }
  10118. else if (this.affectedTree.value != null) {
  10119. util.assert(this.affectedTree.children.isEmpty(), 'affectedTree should not have overlapping affected paths.');
  10120. // All child locations are affected as well; just return same operation.
  10121. return this;
  10122. }
  10123. else {
  10124. var childTree = this.affectedTree.subtree(new Path(childName));
  10125. return new AckUserWrite(newEmptyPath(), childTree, this.revert);
  10126. }
  10127. };
  10128. return AckUserWrite;
  10129. }());
  10130. /**
  10131. * @license
  10132. * Copyright 2017 Google LLC
  10133. *
  10134. * Licensed under the Apache License, Version 2.0 (the "License");
  10135. * you may not use this file except in compliance with the License.
  10136. * You may obtain a copy of the License at
  10137. *
  10138. * http://www.apache.org/licenses/LICENSE-2.0
  10139. *
  10140. * Unless required by applicable law or agreed to in writing, software
  10141. * distributed under the License is distributed on an "AS IS" BASIS,
  10142. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10143. * See the License for the specific language governing permissions and
  10144. * limitations under the License.
  10145. */
  10146. var ListenComplete = /** @class */ (function () {
  10147. function ListenComplete(source, path) {
  10148. this.source = source;
  10149. this.path = path;
  10150. /** @inheritDoc */
  10151. this.type = OperationType.LISTEN_COMPLETE;
  10152. }
  10153. ListenComplete.prototype.operationForChild = function (childName) {
  10154. if (pathIsEmpty(this.path)) {
  10155. return new ListenComplete(this.source, newEmptyPath());
  10156. }
  10157. else {
  10158. return new ListenComplete(this.source, pathPopFront(this.path));
  10159. }
  10160. };
  10161. return ListenComplete;
  10162. }());
  10163. /**
  10164. * @license
  10165. * Copyright 2017 Google LLC
  10166. *
  10167. * Licensed under the Apache License, Version 2.0 (the "License");
  10168. * you may not use this file except in compliance with the License.
  10169. * You may obtain a copy of the License at
  10170. *
  10171. * http://www.apache.org/licenses/LICENSE-2.0
  10172. *
  10173. * Unless required by applicable law or agreed to in writing, software
  10174. * distributed under the License is distributed on an "AS IS" BASIS,
  10175. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10176. * See the License for the specific language governing permissions and
  10177. * limitations under the License.
  10178. */
  10179. var Overwrite = /** @class */ (function () {
  10180. function Overwrite(source, path, snap) {
  10181. this.source = source;
  10182. this.path = path;
  10183. this.snap = snap;
  10184. /** @inheritDoc */
  10185. this.type = OperationType.OVERWRITE;
  10186. }
  10187. Overwrite.prototype.operationForChild = function (childName) {
  10188. if (pathIsEmpty(this.path)) {
  10189. return new Overwrite(this.source, newEmptyPath(), this.snap.getImmediateChild(childName));
  10190. }
  10191. else {
  10192. return new Overwrite(this.source, pathPopFront(this.path), this.snap);
  10193. }
  10194. };
  10195. return Overwrite;
  10196. }());
  10197. /**
  10198. * @license
  10199. * Copyright 2017 Google LLC
  10200. *
  10201. * Licensed under the Apache License, Version 2.0 (the "License");
  10202. * you may not use this file except in compliance with the License.
  10203. * You may obtain a copy of the License at
  10204. *
  10205. * http://www.apache.org/licenses/LICENSE-2.0
  10206. *
  10207. * Unless required by applicable law or agreed to in writing, software
  10208. * distributed under the License is distributed on an "AS IS" BASIS,
  10209. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10210. * See the License for the specific language governing permissions and
  10211. * limitations under the License.
  10212. */
  10213. var Merge = /** @class */ (function () {
  10214. function Merge(
  10215. /** @inheritDoc */ source,
  10216. /** @inheritDoc */ path,
  10217. /** @inheritDoc */ children) {
  10218. this.source = source;
  10219. this.path = path;
  10220. this.children = children;
  10221. /** @inheritDoc */
  10222. this.type = OperationType.MERGE;
  10223. }
  10224. Merge.prototype.operationForChild = function (childName) {
  10225. if (pathIsEmpty(this.path)) {
  10226. var childTree = this.children.subtree(new Path(childName));
  10227. if (childTree.isEmpty()) {
  10228. // This child is unaffected
  10229. return null;
  10230. }
  10231. else if (childTree.value) {
  10232. // We have a snapshot for the child in question. This becomes an overwrite of the child.
  10233. return new Overwrite(this.source, newEmptyPath(), childTree.value);
  10234. }
  10235. else {
  10236. // This is a merge at a deeper level
  10237. return new Merge(this.source, newEmptyPath(), childTree);
  10238. }
  10239. }
  10240. else {
  10241. util.assert(pathGetFront(this.path) === childName, "Can't get a merge for a child not on the path of the operation");
  10242. return new Merge(this.source, pathPopFront(this.path), this.children);
  10243. }
  10244. };
  10245. Merge.prototype.toString = function () {
  10246. return ('Operation(' +
  10247. this.path +
  10248. ': ' +
  10249. this.source.toString() +
  10250. ' merge: ' +
  10251. this.children.toString() +
  10252. ')');
  10253. };
  10254. return Merge;
  10255. }());
  10256. /**
  10257. * @license
  10258. * Copyright 2017 Google LLC
  10259. *
  10260. * Licensed under the Apache License, Version 2.0 (the "License");
  10261. * you may not use this file except in compliance with the License.
  10262. * You may obtain a copy of the License at
  10263. *
  10264. * http://www.apache.org/licenses/LICENSE-2.0
  10265. *
  10266. * Unless required by applicable law or agreed to in writing, software
  10267. * distributed under the License is distributed on an "AS IS" BASIS,
  10268. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10269. * See the License for the specific language governing permissions and
  10270. * limitations under the License.
  10271. */
  10272. /**
  10273. * A cache node only stores complete children. Additionally it holds a flag whether the node can be considered fully
  10274. * initialized in the sense that we know at one point in time this represented a valid state of the world, e.g.
  10275. * initialized with data from the server, or a complete overwrite by the client. The filtered flag also tracks
  10276. * whether a node potentially had children removed due to a filter.
  10277. */
  10278. var CacheNode = /** @class */ (function () {
  10279. function CacheNode(node_, fullyInitialized_, filtered_) {
  10280. this.node_ = node_;
  10281. this.fullyInitialized_ = fullyInitialized_;
  10282. this.filtered_ = filtered_;
  10283. }
  10284. /**
  10285. * Returns whether this node was fully initialized with either server data or a complete overwrite by the client
  10286. */
  10287. CacheNode.prototype.isFullyInitialized = function () {
  10288. return this.fullyInitialized_;
  10289. };
  10290. /**
  10291. * Returns whether this node is potentially missing children due to a filter applied to the node
  10292. */
  10293. CacheNode.prototype.isFiltered = function () {
  10294. return this.filtered_;
  10295. };
  10296. CacheNode.prototype.isCompleteForPath = function (path) {
  10297. if (pathIsEmpty(path)) {
  10298. return this.isFullyInitialized() && !this.filtered_;
  10299. }
  10300. var childKey = pathGetFront(path);
  10301. return this.isCompleteForChild(childKey);
  10302. };
  10303. CacheNode.prototype.isCompleteForChild = function (key) {
  10304. return ((this.isFullyInitialized() && !this.filtered_) || this.node_.hasChild(key));
  10305. };
  10306. CacheNode.prototype.getNode = function () {
  10307. return this.node_;
  10308. };
  10309. return CacheNode;
  10310. }());
  10311. /**
  10312. * @license
  10313. * Copyright 2017 Google LLC
  10314. *
  10315. * Licensed under the Apache License, Version 2.0 (the "License");
  10316. * you may not use this file except in compliance with the License.
  10317. * You may obtain a copy of the License at
  10318. *
  10319. * http://www.apache.org/licenses/LICENSE-2.0
  10320. *
  10321. * Unless required by applicable law or agreed to in writing, software
  10322. * distributed under the License is distributed on an "AS IS" BASIS,
  10323. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10324. * See the License for the specific language governing permissions and
  10325. * limitations under the License.
  10326. */
  10327. /**
  10328. * An EventGenerator is used to convert "raw" changes (Change) as computed by the
  10329. * CacheDiffer into actual events (Event) that can be raised. See generateEventsForChanges()
  10330. * for details.
  10331. *
  10332. */
  10333. var EventGenerator = /** @class */ (function () {
  10334. function EventGenerator(query_) {
  10335. this.query_ = query_;
  10336. this.index_ = this.query_._queryParams.getIndex();
  10337. }
  10338. return EventGenerator;
  10339. }());
  10340. /**
  10341. * Given a set of raw changes (no moved events and prevName not specified yet), and a set of
  10342. * EventRegistrations that should be notified of these changes, generate the actual events to be raised.
  10343. *
  10344. * Notes:
  10345. * - child_moved events will be synthesized at this time for any child_changed events that affect
  10346. * our index.
  10347. * - prevName will be calculated based on the index ordering.
  10348. */
  10349. function eventGeneratorGenerateEventsForChanges(eventGenerator, changes, eventCache, eventRegistrations) {
  10350. var events = [];
  10351. var moves = [];
  10352. changes.forEach(function (change) {
  10353. if (change.type === "child_changed" /* ChangeType.CHILD_CHANGED */ &&
  10354. eventGenerator.index_.indexedValueChanged(change.oldSnap, change.snapshotNode)) {
  10355. moves.push(changeChildMoved(change.childName, change.snapshotNode));
  10356. }
  10357. });
  10358. eventGeneratorGenerateEventsForType(eventGenerator, events, "child_removed" /* ChangeType.CHILD_REMOVED */, changes, eventRegistrations, eventCache);
  10359. eventGeneratorGenerateEventsForType(eventGenerator, events, "child_added" /* ChangeType.CHILD_ADDED */, changes, eventRegistrations, eventCache);
  10360. eventGeneratorGenerateEventsForType(eventGenerator, events, "child_moved" /* ChangeType.CHILD_MOVED */, moves, eventRegistrations, eventCache);
  10361. eventGeneratorGenerateEventsForType(eventGenerator, events, "child_changed" /* ChangeType.CHILD_CHANGED */, changes, eventRegistrations, eventCache);
  10362. eventGeneratorGenerateEventsForType(eventGenerator, events, "value" /* ChangeType.VALUE */, changes, eventRegistrations, eventCache);
  10363. return events;
  10364. }
  10365. /**
  10366. * Given changes of a single change type, generate the corresponding events.
  10367. */
  10368. function eventGeneratorGenerateEventsForType(eventGenerator, events, eventType, changes, registrations, eventCache) {
  10369. var filteredChanges = changes.filter(function (change) { return change.type === eventType; });
  10370. filteredChanges.sort(function (a, b) {
  10371. return eventGeneratorCompareChanges(eventGenerator, a, b);
  10372. });
  10373. filteredChanges.forEach(function (change) {
  10374. var materializedChange = eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache);
  10375. registrations.forEach(function (registration) {
  10376. if (registration.respondsTo(change.type)) {
  10377. events.push(registration.createEvent(materializedChange, eventGenerator.query_));
  10378. }
  10379. });
  10380. });
  10381. }
  10382. function eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache) {
  10383. if (change.type === 'value' || change.type === 'child_removed') {
  10384. return change;
  10385. }
  10386. else {
  10387. change.prevName = eventCache.getPredecessorChildName(change.childName, change.snapshotNode, eventGenerator.index_);
  10388. return change;
  10389. }
  10390. }
  10391. function eventGeneratorCompareChanges(eventGenerator, a, b) {
  10392. if (a.childName == null || b.childName == null) {
  10393. throw util.assertionError('Should only compare child_ events.');
  10394. }
  10395. var aWrapped = new NamedNode(a.childName, a.snapshotNode);
  10396. var bWrapped = new NamedNode(b.childName, b.snapshotNode);
  10397. return eventGenerator.index_.compare(aWrapped, bWrapped);
  10398. }
  10399. /**
  10400. * @license
  10401. * Copyright 2017 Google LLC
  10402. *
  10403. * Licensed under the Apache License, Version 2.0 (the "License");
  10404. * you may not use this file except in compliance with the License.
  10405. * You may obtain a copy of the License at
  10406. *
  10407. * http://www.apache.org/licenses/LICENSE-2.0
  10408. *
  10409. * Unless required by applicable law or agreed to in writing, software
  10410. * distributed under the License is distributed on an "AS IS" BASIS,
  10411. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10412. * See the License for the specific language governing permissions and
  10413. * limitations under the License.
  10414. */
  10415. function newViewCache(eventCache, serverCache) {
  10416. return { eventCache: eventCache, serverCache: serverCache };
  10417. }
  10418. function viewCacheUpdateEventSnap(viewCache, eventSnap, complete, filtered) {
  10419. return newViewCache(new CacheNode(eventSnap, complete, filtered), viewCache.serverCache);
  10420. }
  10421. function viewCacheUpdateServerSnap(viewCache, serverSnap, complete, filtered) {
  10422. return newViewCache(viewCache.eventCache, new CacheNode(serverSnap, complete, filtered));
  10423. }
  10424. function viewCacheGetCompleteEventSnap(viewCache) {
  10425. return viewCache.eventCache.isFullyInitialized()
  10426. ? viewCache.eventCache.getNode()
  10427. : null;
  10428. }
  10429. function viewCacheGetCompleteServerSnap(viewCache) {
  10430. return viewCache.serverCache.isFullyInitialized()
  10431. ? viewCache.serverCache.getNode()
  10432. : null;
  10433. }
  10434. /**
  10435. * @license
  10436. * Copyright 2017 Google LLC
  10437. *
  10438. * Licensed under the Apache License, Version 2.0 (the "License");
  10439. * you may not use this file except in compliance with the License.
  10440. * You may obtain a copy of the License at
  10441. *
  10442. * http://www.apache.org/licenses/LICENSE-2.0
  10443. *
  10444. * Unless required by applicable law or agreed to in writing, software
  10445. * distributed under the License is distributed on an "AS IS" BASIS,
  10446. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10447. * See the License for the specific language governing permissions and
  10448. * limitations under the License.
  10449. */
  10450. var emptyChildrenSingleton;
  10451. /**
  10452. * Singleton empty children collection.
  10453. *
  10454. */
  10455. var EmptyChildren = function () {
  10456. if (!emptyChildrenSingleton) {
  10457. emptyChildrenSingleton = new SortedMap(stringCompare);
  10458. }
  10459. return emptyChildrenSingleton;
  10460. };
  10461. /**
  10462. * A tree with immutable elements.
  10463. */
  10464. var ImmutableTree = /** @class */ (function () {
  10465. function ImmutableTree(value, children) {
  10466. if (children === void 0) { children = EmptyChildren(); }
  10467. this.value = value;
  10468. this.children = children;
  10469. }
  10470. ImmutableTree.fromObject = function (obj) {
  10471. var tree = new ImmutableTree(null);
  10472. each(obj, function (childPath, childSnap) {
  10473. tree = tree.set(new Path(childPath), childSnap);
  10474. });
  10475. return tree;
  10476. };
  10477. /**
  10478. * True if the value is empty and there are no children
  10479. */
  10480. ImmutableTree.prototype.isEmpty = function () {
  10481. return this.value === null && this.children.isEmpty();
  10482. };
  10483. /**
  10484. * Given a path and predicate, return the first node and the path to that node
  10485. * where the predicate returns true.
  10486. *
  10487. * TODO Do a perf test -- If we're creating a bunch of `{path: value:}`
  10488. * objects on the way back out, it may be better to pass down a pathSoFar obj.
  10489. *
  10490. * @param relativePath - The remainder of the path
  10491. * @param predicate - The predicate to satisfy to return a node
  10492. */
  10493. ImmutableTree.prototype.findRootMostMatchingPathAndValue = function (relativePath, predicate) {
  10494. if (this.value != null && predicate(this.value)) {
  10495. return { path: newEmptyPath(), value: this.value };
  10496. }
  10497. else {
  10498. if (pathIsEmpty(relativePath)) {
  10499. return null;
  10500. }
  10501. else {
  10502. var front = pathGetFront(relativePath);
  10503. var child = this.children.get(front);
  10504. if (child !== null) {
  10505. var childExistingPathAndValue = child.findRootMostMatchingPathAndValue(pathPopFront(relativePath), predicate);
  10506. if (childExistingPathAndValue != null) {
  10507. var fullPath = pathChild(new Path(front), childExistingPathAndValue.path);
  10508. return { path: fullPath, value: childExistingPathAndValue.value };
  10509. }
  10510. else {
  10511. return null;
  10512. }
  10513. }
  10514. else {
  10515. return null;
  10516. }
  10517. }
  10518. }
  10519. };
  10520. /**
  10521. * Find, if it exists, the shortest subpath of the given path that points a defined
  10522. * value in the tree
  10523. */
  10524. ImmutableTree.prototype.findRootMostValueAndPath = function (relativePath) {
  10525. return this.findRootMostMatchingPathAndValue(relativePath, function () { return true; });
  10526. };
  10527. /**
  10528. * @returns The subtree at the given path
  10529. */
  10530. ImmutableTree.prototype.subtree = function (relativePath) {
  10531. if (pathIsEmpty(relativePath)) {
  10532. return this;
  10533. }
  10534. else {
  10535. var front = pathGetFront(relativePath);
  10536. var childTree = this.children.get(front);
  10537. if (childTree !== null) {
  10538. return childTree.subtree(pathPopFront(relativePath));
  10539. }
  10540. else {
  10541. return new ImmutableTree(null);
  10542. }
  10543. }
  10544. };
  10545. /**
  10546. * Sets a value at the specified path.
  10547. *
  10548. * @param relativePath - Path to set value at.
  10549. * @param toSet - Value to set.
  10550. * @returns Resulting tree.
  10551. */
  10552. ImmutableTree.prototype.set = function (relativePath, toSet) {
  10553. if (pathIsEmpty(relativePath)) {
  10554. return new ImmutableTree(toSet, this.children);
  10555. }
  10556. else {
  10557. var front = pathGetFront(relativePath);
  10558. var child = this.children.get(front) || new ImmutableTree(null);
  10559. var newChild = child.set(pathPopFront(relativePath), toSet);
  10560. var newChildren = this.children.insert(front, newChild);
  10561. return new ImmutableTree(this.value, newChildren);
  10562. }
  10563. };
  10564. /**
  10565. * Removes the value at the specified path.
  10566. *
  10567. * @param relativePath - Path to value to remove.
  10568. * @returns Resulting tree.
  10569. */
  10570. ImmutableTree.prototype.remove = function (relativePath) {
  10571. if (pathIsEmpty(relativePath)) {
  10572. if (this.children.isEmpty()) {
  10573. return new ImmutableTree(null);
  10574. }
  10575. else {
  10576. return new ImmutableTree(null, this.children);
  10577. }
  10578. }
  10579. else {
  10580. var front = pathGetFront(relativePath);
  10581. var child = this.children.get(front);
  10582. if (child) {
  10583. var newChild = child.remove(pathPopFront(relativePath));
  10584. var newChildren = void 0;
  10585. if (newChild.isEmpty()) {
  10586. newChildren = this.children.remove(front);
  10587. }
  10588. else {
  10589. newChildren = this.children.insert(front, newChild);
  10590. }
  10591. if (this.value === null && newChildren.isEmpty()) {
  10592. return new ImmutableTree(null);
  10593. }
  10594. else {
  10595. return new ImmutableTree(this.value, newChildren);
  10596. }
  10597. }
  10598. else {
  10599. return this;
  10600. }
  10601. }
  10602. };
  10603. /**
  10604. * Gets a value from the tree.
  10605. *
  10606. * @param relativePath - Path to get value for.
  10607. * @returns Value at path, or null.
  10608. */
  10609. ImmutableTree.prototype.get = function (relativePath) {
  10610. if (pathIsEmpty(relativePath)) {
  10611. return this.value;
  10612. }
  10613. else {
  10614. var front = pathGetFront(relativePath);
  10615. var child = this.children.get(front);
  10616. if (child) {
  10617. return child.get(pathPopFront(relativePath));
  10618. }
  10619. else {
  10620. return null;
  10621. }
  10622. }
  10623. };
  10624. /**
  10625. * Replace the subtree at the specified path with the given new tree.
  10626. *
  10627. * @param relativePath - Path to replace subtree for.
  10628. * @param newTree - New tree.
  10629. * @returns Resulting tree.
  10630. */
  10631. ImmutableTree.prototype.setTree = function (relativePath, newTree) {
  10632. if (pathIsEmpty(relativePath)) {
  10633. return newTree;
  10634. }
  10635. else {
  10636. var front = pathGetFront(relativePath);
  10637. var child = this.children.get(front) || new ImmutableTree(null);
  10638. var newChild = child.setTree(pathPopFront(relativePath), newTree);
  10639. var newChildren = void 0;
  10640. if (newChild.isEmpty()) {
  10641. newChildren = this.children.remove(front);
  10642. }
  10643. else {
  10644. newChildren = this.children.insert(front, newChild);
  10645. }
  10646. return new ImmutableTree(this.value, newChildren);
  10647. }
  10648. };
  10649. /**
  10650. * Performs a depth first fold on this tree. Transforms a tree into a single
  10651. * value, given a function that operates on the path to a node, an optional
  10652. * current value, and a map of child names to folded subtrees
  10653. */
  10654. ImmutableTree.prototype.fold = function (fn) {
  10655. return this.fold_(newEmptyPath(), fn);
  10656. };
  10657. /**
  10658. * Recursive helper for public-facing fold() method
  10659. */
  10660. ImmutableTree.prototype.fold_ = function (pathSoFar, fn) {
  10661. var accum = {};
  10662. this.children.inorderTraversal(function (childKey, childTree) {
  10663. accum[childKey] = childTree.fold_(pathChild(pathSoFar, childKey), fn);
  10664. });
  10665. return fn(pathSoFar, this.value, accum);
  10666. };
  10667. /**
  10668. * Find the first matching value on the given path. Return the result of applying f to it.
  10669. */
  10670. ImmutableTree.prototype.findOnPath = function (path, f) {
  10671. return this.findOnPath_(path, newEmptyPath(), f);
  10672. };
  10673. ImmutableTree.prototype.findOnPath_ = function (pathToFollow, pathSoFar, f) {
  10674. var result = this.value ? f(pathSoFar, this.value) : false;
  10675. if (result) {
  10676. return result;
  10677. }
  10678. else {
  10679. if (pathIsEmpty(pathToFollow)) {
  10680. return null;
  10681. }
  10682. else {
  10683. var front = pathGetFront(pathToFollow);
  10684. var nextChild = this.children.get(front);
  10685. if (nextChild) {
  10686. return nextChild.findOnPath_(pathPopFront(pathToFollow), pathChild(pathSoFar, front), f);
  10687. }
  10688. else {
  10689. return null;
  10690. }
  10691. }
  10692. }
  10693. };
  10694. ImmutableTree.prototype.foreachOnPath = function (path, f) {
  10695. return this.foreachOnPath_(path, newEmptyPath(), f);
  10696. };
  10697. ImmutableTree.prototype.foreachOnPath_ = function (pathToFollow, currentRelativePath, f) {
  10698. if (pathIsEmpty(pathToFollow)) {
  10699. return this;
  10700. }
  10701. else {
  10702. if (this.value) {
  10703. f(currentRelativePath, this.value);
  10704. }
  10705. var front = pathGetFront(pathToFollow);
  10706. var nextChild = this.children.get(front);
  10707. if (nextChild) {
  10708. return nextChild.foreachOnPath_(pathPopFront(pathToFollow), pathChild(currentRelativePath, front), f);
  10709. }
  10710. else {
  10711. return new ImmutableTree(null);
  10712. }
  10713. }
  10714. };
  10715. /**
  10716. * Calls the given function for each node in the tree that has a value.
  10717. *
  10718. * @param f - A function to be called with the path from the root of the tree to
  10719. * a node, and the value at that node. Called in depth-first order.
  10720. */
  10721. ImmutableTree.prototype.foreach = function (f) {
  10722. this.foreach_(newEmptyPath(), f);
  10723. };
  10724. ImmutableTree.prototype.foreach_ = function (currentRelativePath, f) {
  10725. this.children.inorderTraversal(function (childName, childTree) {
  10726. childTree.foreach_(pathChild(currentRelativePath, childName), f);
  10727. });
  10728. if (this.value) {
  10729. f(currentRelativePath, this.value);
  10730. }
  10731. };
  10732. ImmutableTree.prototype.foreachChild = function (f) {
  10733. this.children.inorderTraversal(function (childName, childTree) {
  10734. if (childTree.value) {
  10735. f(childName, childTree.value);
  10736. }
  10737. });
  10738. };
  10739. return ImmutableTree;
  10740. }());
  10741. /**
  10742. * @license
  10743. * Copyright 2017 Google LLC
  10744. *
  10745. * Licensed under the Apache License, Version 2.0 (the "License");
  10746. * you may not use this file except in compliance with the License.
  10747. * You may obtain a copy of the License at
  10748. *
  10749. * http://www.apache.org/licenses/LICENSE-2.0
  10750. *
  10751. * Unless required by applicable law or agreed to in writing, software
  10752. * distributed under the License is distributed on an "AS IS" BASIS,
  10753. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10754. * See the License for the specific language governing permissions and
  10755. * limitations under the License.
  10756. */
  10757. /**
  10758. * This class holds a collection of writes that can be applied to nodes in unison. It abstracts away the logic with
  10759. * dealing with priority writes and multiple nested writes. At any given path there is only allowed to be one write
  10760. * modifying that path. Any write to an existing path or shadowing an existing path will modify that existing write
  10761. * to reflect the write added.
  10762. */
  10763. var CompoundWrite = /** @class */ (function () {
  10764. function CompoundWrite(writeTree_) {
  10765. this.writeTree_ = writeTree_;
  10766. }
  10767. CompoundWrite.empty = function () {
  10768. return new CompoundWrite(new ImmutableTree(null));
  10769. };
  10770. return CompoundWrite;
  10771. }());
  10772. function compoundWriteAddWrite(compoundWrite, path, node) {
  10773. if (pathIsEmpty(path)) {
  10774. return new CompoundWrite(new ImmutableTree(node));
  10775. }
  10776. else {
  10777. var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
  10778. if (rootmost != null) {
  10779. var rootMostPath = rootmost.path;
  10780. var value = rootmost.value;
  10781. var relativePath = newRelativePath(rootMostPath, path);
  10782. value = value.updateChild(relativePath, node);
  10783. return new CompoundWrite(compoundWrite.writeTree_.set(rootMostPath, value));
  10784. }
  10785. else {
  10786. var subtree = new ImmutableTree(node);
  10787. var newWriteTree = compoundWrite.writeTree_.setTree(path, subtree);
  10788. return new CompoundWrite(newWriteTree);
  10789. }
  10790. }
  10791. }
  10792. function compoundWriteAddWrites(compoundWrite, path, updates) {
  10793. var newWrite = compoundWrite;
  10794. each(updates, function (childKey, node) {
  10795. newWrite = compoundWriteAddWrite(newWrite, pathChild(path, childKey), node);
  10796. });
  10797. return newWrite;
  10798. }
  10799. /**
  10800. * Will remove a write at the given path and deeper paths. This will <em>not</em> modify a write at a higher
  10801. * location, which must be removed by calling this method with that path.
  10802. *
  10803. * @param compoundWrite - The CompoundWrite to remove.
  10804. * @param path - The path at which a write and all deeper writes should be removed
  10805. * @returns The new CompoundWrite with the removed path
  10806. */
  10807. function compoundWriteRemoveWrite(compoundWrite, path) {
  10808. if (pathIsEmpty(path)) {
  10809. return CompoundWrite.empty();
  10810. }
  10811. else {
  10812. var newWriteTree = compoundWrite.writeTree_.setTree(path, new ImmutableTree(null));
  10813. return new CompoundWrite(newWriteTree);
  10814. }
  10815. }
  10816. /**
  10817. * Returns whether this CompoundWrite will fully overwrite a node at a given location and can therefore be
  10818. * considered "complete".
  10819. *
  10820. * @param compoundWrite - The CompoundWrite to check.
  10821. * @param path - The path to check for
  10822. * @returns Whether there is a complete write at that path
  10823. */
  10824. function compoundWriteHasCompleteWrite(compoundWrite, path) {
  10825. return compoundWriteGetCompleteNode(compoundWrite, path) != null;
  10826. }
  10827. /**
  10828. * Returns a node for a path if and only if the node is a "complete" overwrite at that path. This will not aggregate
  10829. * writes from deeper paths, but will return child nodes from a more shallow path.
  10830. *
  10831. * @param compoundWrite - The CompoundWrite to get the node from.
  10832. * @param path - The path to get a complete write
  10833. * @returns The node if complete at that path, or null otherwise.
  10834. */
  10835. function compoundWriteGetCompleteNode(compoundWrite, path) {
  10836. var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
  10837. if (rootmost != null) {
  10838. return compoundWrite.writeTree_
  10839. .get(rootmost.path)
  10840. .getChild(newRelativePath(rootmost.path, path));
  10841. }
  10842. else {
  10843. return null;
  10844. }
  10845. }
  10846. /**
  10847. * Returns all children that are guaranteed to be a complete overwrite.
  10848. *
  10849. * @param compoundWrite - The CompoundWrite to get children from.
  10850. * @returns A list of all complete children.
  10851. */
  10852. function compoundWriteGetCompleteChildren(compoundWrite) {
  10853. var children = [];
  10854. var node = compoundWrite.writeTree_.value;
  10855. if (node != null) {
  10856. // If it's a leaf node, it has no children; so nothing to do.
  10857. if (!node.isLeafNode()) {
  10858. node.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
  10859. children.push(new NamedNode(childName, childNode));
  10860. });
  10861. }
  10862. }
  10863. else {
  10864. compoundWrite.writeTree_.children.inorderTraversal(function (childName, childTree) {
  10865. if (childTree.value != null) {
  10866. children.push(new NamedNode(childName, childTree.value));
  10867. }
  10868. });
  10869. }
  10870. return children;
  10871. }
  10872. function compoundWriteChildCompoundWrite(compoundWrite, path) {
  10873. if (pathIsEmpty(path)) {
  10874. return compoundWrite;
  10875. }
  10876. else {
  10877. var shadowingNode = compoundWriteGetCompleteNode(compoundWrite, path);
  10878. if (shadowingNode != null) {
  10879. return new CompoundWrite(new ImmutableTree(shadowingNode));
  10880. }
  10881. else {
  10882. return new CompoundWrite(compoundWrite.writeTree_.subtree(path));
  10883. }
  10884. }
  10885. }
  10886. /**
  10887. * Returns true if this CompoundWrite is empty and therefore does not modify any nodes.
  10888. * @returns Whether this CompoundWrite is empty
  10889. */
  10890. function compoundWriteIsEmpty(compoundWrite) {
  10891. return compoundWrite.writeTree_.isEmpty();
  10892. }
  10893. /**
  10894. * Applies this CompoundWrite to a node. The node is returned with all writes from this CompoundWrite applied to the
  10895. * node
  10896. * @param node - The node to apply this CompoundWrite to
  10897. * @returns The node with all writes applied
  10898. */
  10899. function compoundWriteApply(compoundWrite, node) {
  10900. return applySubtreeWrite(newEmptyPath(), compoundWrite.writeTree_, node);
  10901. }
  10902. function applySubtreeWrite(relativePath, writeTree, node) {
  10903. if (writeTree.value != null) {
  10904. // Since there a write is always a leaf, we're done here
  10905. return node.updateChild(relativePath, writeTree.value);
  10906. }
  10907. else {
  10908. var priorityWrite_1 = null;
  10909. writeTree.children.inorderTraversal(function (childKey, childTree) {
  10910. if (childKey === '.priority') {
  10911. // Apply priorities at the end so we don't update priorities for either empty nodes or forget
  10912. // to apply priorities to empty nodes that are later filled
  10913. util.assert(childTree.value !== null, 'Priority writes must always be leaf nodes');
  10914. priorityWrite_1 = childTree.value;
  10915. }
  10916. else {
  10917. node = applySubtreeWrite(pathChild(relativePath, childKey), childTree, node);
  10918. }
  10919. });
  10920. // If there was a priority write, we only apply it if the node is not empty
  10921. if (!node.getChild(relativePath).isEmpty() && priorityWrite_1 !== null) {
  10922. node = node.updateChild(pathChild(relativePath, '.priority'), priorityWrite_1);
  10923. }
  10924. return node;
  10925. }
  10926. }
  10927. /**
  10928. * @license
  10929. * Copyright 2017 Google LLC
  10930. *
  10931. * Licensed under the Apache License, Version 2.0 (the "License");
  10932. * you may not use this file except in compliance with the License.
  10933. * You may obtain a copy of the License at
  10934. *
  10935. * http://www.apache.org/licenses/LICENSE-2.0
  10936. *
  10937. * Unless required by applicable law or agreed to in writing, software
  10938. * distributed under the License is distributed on an "AS IS" BASIS,
  10939. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10940. * See the License for the specific language governing permissions and
  10941. * limitations under the License.
  10942. */
  10943. /**
  10944. * Create a new WriteTreeRef for the given path. For use with a new sync point at the given path.
  10945. *
  10946. */
  10947. function writeTreeChildWrites(writeTree, path) {
  10948. return newWriteTreeRef(path, writeTree);
  10949. }
  10950. /**
  10951. * Record a new overwrite from user code.
  10952. *
  10953. * @param visible - This is set to false by some transactions. It should be excluded from event caches
  10954. */
  10955. function writeTreeAddOverwrite(writeTree, path, snap, writeId, visible) {
  10956. util.assert(writeId > writeTree.lastWriteId, 'Stacking an older write on top of newer ones');
  10957. if (visible === undefined) {
  10958. visible = true;
  10959. }
  10960. writeTree.allWrites.push({
  10961. path: path,
  10962. snap: snap,
  10963. writeId: writeId,
  10964. visible: visible
  10965. });
  10966. if (visible) {
  10967. writeTree.visibleWrites = compoundWriteAddWrite(writeTree.visibleWrites, path, snap);
  10968. }
  10969. writeTree.lastWriteId = writeId;
  10970. }
  10971. /**
  10972. * Record a new merge from user code.
  10973. */
  10974. function writeTreeAddMerge(writeTree, path, changedChildren, writeId) {
  10975. util.assert(writeId > writeTree.lastWriteId, 'Stacking an older merge on top of newer ones');
  10976. writeTree.allWrites.push({
  10977. path: path,
  10978. children: changedChildren,
  10979. writeId: writeId,
  10980. visible: true
  10981. });
  10982. writeTree.visibleWrites = compoundWriteAddWrites(writeTree.visibleWrites, path, changedChildren);
  10983. writeTree.lastWriteId = writeId;
  10984. }
  10985. function writeTreeGetWrite(writeTree, writeId) {
  10986. for (var i = 0; i < writeTree.allWrites.length; i++) {
  10987. var record = writeTree.allWrites[i];
  10988. if (record.writeId === writeId) {
  10989. return record;
  10990. }
  10991. }
  10992. return null;
  10993. }
  10994. /**
  10995. * Remove a write (either an overwrite or merge) that has been successfully acknowledge by the server. Recalculates
  10996. * the tree if necessary. We return true if it may have been visible, meaning views need to reevaluate.
  10997. *
  10998. * @returns true if the write may have been visible (meaning we'll need to reevaluate / raise
  10999. * events as a result).
  11000. */
  11001. function writeTreeRemoveWrite(writeTree, writeId) {
  11002. // Note: disabling this check. It could be a transaction that preempted another transaction, and thus was applied
  11003. // out of order.
  11004. //const validClear = revert || this.allWrites_.length === 0 || writeId <= this.allWrites_[0].writeId;
  11005. //assert(validClear, "Either we don't have this write, or it's the first one in the queue");
  11006. var idx = writeTree.allWrites.findIndex(function (s) {
  11007. return s.writeId === writeId;
  11008. });
  11009. util.assert(idx >= 0, 'removeWrite called with nonexistent writeId.');
  11010. var writeToRemove = writeTree.allWrites[idx];
  11011. writeTree.allWrites.splice(idx, 1);
  11012. var removedWriteWasVisible = writeToRemove.visible;
  11013. var removedWriteOverlapsWithOtherWrites = false;
  11014. var i = writeTree.allWrites.length - 1;
  11015. while (removedWriteWasVisible && i >= 0) {
  11016. var currentWrite = writeTree.allWrites[i];
  11017. if (currentWrite.visible) {
  11018. if (i >= idx &&
  11019. writeTreeRecordContainsPath_(currentWrite, writeToRemove.path)) {
  11020. // The removed write was completely shadowed by a subsequent write.
  11021. removedWriteWasVisible = false;
  11022. }
  11023. else if (pathContains(writeToRemove.path, currentWrite.path)) {
  11024. // Either we're covering some writes or they're covering part of us (depending on which came first).
  11025. removedWriteOverlapsWithOtherWrites = true;
  11026. }
  11027. }
  11028. i--;
  11029. }
  11030. if (!removedWriteWasVisible) {
  11031. return false;
  11032. }
  11033. else if (removedWriteOverlapsWithOtherWrites) {
  11034. // There's some shadowing going on. Just rebuild the visible writes from scratch.
  11035. writeTreeResetTree_(writeTree);
  11036. return true;
  11037. }
  11038. else {
  11039. // There's no shadowing. We can safely just remove the write(s) from visibleWrites.
  11040. if (writeToRemove.snap) {
  11041. writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, writeToRemove.path);
  11042. }
  11043. else {
  11044. var children = writeToRemove.children;
  11045. each(children, function (childName) {
  11046. writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, pathChild(writeToRemove.path, childName));
  11047. });
  11048. }
  11049. return true;
  11050. }
  11051. }
  11052. function writeTreeRecordContainsPath_(writeRecord, path) {
  11053. if (writeRecord.snap) {
  11054. return pathContains(writeRecord.path, path);
  11055. }
  11056. else {
  11057. for (var childName in writeRecord.children) {
  11058. if (writeRecord.children.hasOwnProperty(childName) &&
  11059. pathContains(pathChild(writeRecord.path, childName), path)) {
  11060. return true;
  11061. }
  11062. }
  11063. return false;
  11064. }
  11065. }
  11066. /**
  11067. * Re-layer the writes and merges into a tree so we can efficiently calculate event snapshots
  11068. */
  11069. function writeTreeResetTree_(writeTree) {
  11070. writeTree.visibleWrites = writeTreeLayerTree_(writeTree.allWrites, writeTreeDefaultFilter_, newEmptyPath());
  11071. if (writeTree.allWrites.length > 0) {
  11072. writeTree.lastWriteId =
  11073. writeTree.allWrites[writeTree.allWrites.length - 1].writeId;
  11074. }
  11075. else {
  11076. writeTree.lastWriteId = -1;
  11077. }
  11078. }
  11079. /**
  11080. * The default filter used when constructing the tree. Keep everything that's visible.
  11081. */
  11082. function writeTreeDefaultFilter_(write) {
  11083. return write.visible;
  11084. }
  11085. /**
  11086. * Static method. Given an array of WriteRecords, a filter for which ones to include, and a path, construct the tree of
  11087. * event data at that path.
  11088. */
  11089. function writeTreeLayerTree_(writes, filter, treeRoot) {
  11090. var compoundWrite = CompoundWrite.empty();
  11091. for (var i = 0; i < writes.length; ++i) {
  11092. var write = writes[i];
  11093. // Theory, a later set will either:
  11094. // a) abort a relevant transaction, so no need to worry about excluding it from calculating that transaction
  11095. // b) not be relevant to a transaction (separate branch), so again will not affect the data for that transaction
  11096. if (filter(write)) {
  11097. var writePath = write.path;
  11098. var relativePath = void 0;
  11099. if (write.snap) {
  11100. if (pathContains(treeRoot, writePath)) {
  11101. relativePath = newRelativePath(treeRoot, writePath);
  11102. compoundWrite = compoundWriteAddWrite(compoundWrite, relativePath, write.snap);
  11103. }
  11104. else if (pathContains(writePath, treeRoot)) {
  11105. relativePath = newRelativePath(writePath, treeRoot);
  11106. compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), write.snap.getChild(relativePath));
  11107. }
  11108. else ;
  11109. }
  11110. else if (write.children) {
  11111. if (pathContains(treeRoot, writePath)) {
  11112. relativePath = newRelativePath(treeRoot, writePath);
  11113. compoundWrite = compoundWriteAddWrites(compoundWrite, relativePath, write.children);
  11114. }
  11115. else if (pathContains(writePath, treeRoot)) {
  11116. relativePath = newRelativePath(writePath, treeRoot);
  11117. if (pathIsEmpty(relativePath)) {
  11118. compoundWrite = compoundWriteAddWrites(compoundWrite, newEmptyPath(), write.children);
  11119. }
  11120. else {
  11121. var child = util.safeGet(write.children, pathGetFront(relativePath));
  11122. if (child) {
  11123. // There exists a child in this node that matches the root path
  11124. var deepNode = child.getChild(pathPopFront(relativePath));
  11125. compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), deepNode);
  11126. }
  11127. }
  11128. }
  11129. else ;
  11130. }
  11131. else {
  11132. throw util.assertionError('WriteRecord should have .snap or .children');
  11133. }
  11134. }
  11135. }
  11136. return compoundWrite;
  11137. }
  11138. /**
  11139. * Given optional, underlying server data, and an optional set of constraints (exclude some sets, include hidden
  11140. * writes), attempt to calculate a complete snapshot for the given path
  11141. *
  11142. * @param writeIdsToExclude - An optional set to be excluded
  11143. * @param includeHiddenWrites - Defaults to false, whether or not to layer on writes with visible set to false
  11144. */
  11145. function writeTreeCalcCompleteEventCache(writeTree, treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
  11146. if (!writeIdsToExclude && !includeHiddenWrites) {
  11147. var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
  11148. if (shadowingNode != null) {
  11149. return shadowingNode;
  11150. }
  11151. else {
  11152. var subMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
  11153. if (compoundWriteIsEmpty(subMerge)) {
  11154. return completeServerCache;
  11155. }
  11156. else if (completeServerCache == null &&
  11157. !compoundWriteHasCompleteWrite(subMerge, newEmptyPath())) {
  11158. // We wouldn't have a complete snapshot, since there's no underlying data and no complete shadow
  11159. return null;
  11160. }
  11161. else {
  11162. var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
  11163. return compoundWriteApply(subMerge, layeredCache);
  11164. }
  11165. }
  11166. }
  11167. else {
  11168. var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
  11169. if (!includeHiddenWrites && compoundWriteIsEmpty(merge)) {
  11170. return completeServerCache;
  11171. }
  11172. else {
  11173. // If the server cache is null, and we don't have a complete cache, we need to return null
  11174. if (!includeHiddenWrites &&
  11175. completeServerCache == null &&
  11176. !compoundWriteHasCompleteWrite(merge, newEmptyPath())) {
  11177. return null;
  11178. }
  11179. else {
  11180. var filter = function (write) {
  11181. return ((write.visible || includeHiddenWrites) &&
  11182. (!writeIdsToExclude ||
  11183. !~writeIdsToExclude.indexOf(write.writeId)) &&
  11184. (pathContains(write.path, treePath) ||
  11185. pathContains(treePath, write.path)));
  11186. };
  11187. var mergeAtPath = writeTreeLayerTree_(writeTree.allWrites, filter, treePath);
  11188. var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
  11189. return compoundWriteApply(mergeAtPath, layeredCache);
  11190. }
  11191. }
  11192. }
  11193. }
  11194. /**
  11195. * With optional, underlying server data, attempt to return a children node of children that we have complete data for.
  11196. * Used when creating new views, to pre-fill their complete event children snapshot.
  11197. */
  11198. function writeTreeCalcCompleteEventChildren(writeTree, treePath, completeServerChildren) {
  11199. var completeChildren = ChildrenNode.EMPTY_NODE;
  11200. var topLevelSet = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
  11201. if (topLevelSet) {
  11202. if (!topLevelSet.isLeafNode()) {
  11203. // we're shadowing everything. Return the children.
  11204. topLevelSet.forEachChild(PRIORITY_INDEX, function (childName, childSnap) {
  11205. completeChildren = completeChildren.updateImmediateChild(childName, childSnap);
  11206. });
  11207. }
  11208. return completeChildren;
  11209. }
  11210. else if (completeServerChildren) {
  11211. // Layer any children we have on top of this
  11212. // We know we don't have a top-level set, so just enumerate existing children
  11213. var merge_1 = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
  11214. completeServerChildren.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
  11215. var node = compoundWriteApply(compoundWriteChildCompoundWrite(merge_1, new Path(childName)), childNode);
  11216. completeChildren = completeChildren.updateImmediateChild(childName, node);
  11217. });
  11218. // Add any complete children we have from the set
  11219. compoundWriteGetCompleteChildren(merge_1).forEach(function (namedNode) {
  11220. completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
  11221. });
  11222. return completeChildren;
  11223. }
  11224. else {
  11225. // We don't have anything to layer on top of. Layer on any children we have
  11226. // Note that we can return an empty snap if we have a defined delete
  11227. var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
  11228. compoundWriteGetCompleteChildren(merge).forEach(function (namedNode) {
  11229. completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
  11230. });
  11231. return completeChildren;
  11232. }
  11233. }
  11234. /**
  11235. * Given that the underlying server data has updated, determine what, if anything, needs to be
  11236. * applied to the event cache.
  11237. *
  11238. * Possibilities:
  11239. *
  11240. * 1. No writes are shadowing. Events should be raised, the snap to be applied comes from the server data
  11241. *
  11242. * 2. Some write is completely shadowing. No events to be raised
  11243. *
  11244. * 3. Is partially shadowed. Events
  11245. *
  11246. * Either existingEventSnap or existingServerSnap must exist
  11247. */
  11248. function writeTreeCalcEventCacheAfterServerOverwrite(writeTree, treePath, childPath, existingEventSnap, existingServerSnap) {
  11249. util.assert(existingEventSnap || existingServerSnap, 'Either existingEventSnap or existingServerSnap must exist');
  11250. var path = pathChild(treePath, childPath);
  11251. if (compoundWriteHasCompleteWrite(writeTree.visibleWrites, path)) {
  11252. // At this point we can probably guarantee that we're in case 2, meaning no events
  11253. // May need to check visibility while doing the findRootMostValueAndPath call
  11254. return null;
  11255. }
  11256. else {
  11257. // No complete shadowing. We're either partially shadowing or not shadowing at all.
  11258. var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
  11259. if (compoundWriteIsEmpty(childMerge)) {
  11260. // We're not shadowing at all. Case 1
  11261. return existingServerSnap.getChild(childPath);
  11262. }
  11263. else {
  11264. // This could be more efficient if the serverNode + updates doesn't change the eventSnap
  11265. // However this is tricky to find out, since user updates don't necessary change the server
  11266. // snap, e.g. priority updates on empty nodes, or deep deletes. Another special case is if the server
  11267. // adds nodes, but doesn't change any existing writes. It is therefore not enough to
  11268. // only check if the updates change the serverNode.
  11269. // Maybe check if the merge tree contains these special cases and only do a full overwrite in that case?
  11270. return compoundWriteApply(childMerge, existingServerSnap.getChild(childPath));
  11271. }
  11272. }
  11273. }
  11274. /**
  11275. * Returns a complete child for a given server snap after applying all user writes or null if there is no
  11276. * complete child for this ChildKey.
  11277. */
  11278. function writeTreeCalcCompleteChild(writeTree, treePath, childKey, existingServerSnap) {
  11279. var path = pathChild(treePath, childKey);
  11280. var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
  11281. if (shadowingNode != null) {
  11282. return shadowingNode;
  11283. }
  11284. else {
  11285. if (existingServerSnap.isCompleteForChild(childKey)) {
  11286. var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
  11287. return compoundWriteApply(childMerge, existingServerSnap.getNode().getImmediateChild(childKey));
  11288. }
  11289. else {
  11290. return null;
  11291. }
  11292. }
  11293. }
  11294. /**
  11295. * Returns a node if there is a complete overwrite for this path. More specifically, if there is a write at
  11296. * a higher path, this will return the child of that write relative to the write and this path.
  11297. * Returns null if there is no write at this path.
  11298. */
  11299. function writeTreeShadowingWrite(writeTree, path) {
  11300. return compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
  11301. }
  11302. /**
  11303. * This method is used when processing child remove events on a query. If we can, we pull in children that were outside
  11304. * the window, but may now be in the window.
  11305. */
  11306. function writeTreeCalcIndexedSlice(writeTree, treePath, completeServerData, startPost, count, reverse, index) {
  11307. var toIterate;
  11308. var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
  11309. var shadowingNode = compoundWriteGetCompleteNode(merge, newEmptyPath());
  11310. if (shadowingNode != null) {
  11311. toIterate = shadowingNode;
  11312. }
  11313. else if (completeServerData != null) {
  11314. toIterate = compoundWriteApply(merge, completeServerData);
  11315. }
  11316. else {
  11317. // no children to iterate on
  11318. return [];
  11319. }
  11320. toIterate = toIterate.withIndex(index);
  11321. if (!toIterate.isEmpty() && !toIterate.isLeafNode()) {
  11322. var nodes = [];
  11323. var cmp = index.getCompare();
  11324. var iter = reverse
  11325. ? toIterate.getReverseIteratorFrom(startPost, index)
  11326. : toIterate.getIteratorFrom(startPost, index);
  11327. var next = iter.getNext();
  11328. while (next && nodes.length < count) {
  11329. if (cmp(next, startPost) !== 0) {
  11330. nodes.push(next);
  11331. }
  11332. next = iter.getNext();
  11333. }
  11334. return nodes;
  11335. }
  11336. else {
  11337. return [];
  11338. }
  11339. }
  11340. function newWriteTree() {
  11341. return {
  11342. visibleWrites: CompoundWrite.empty(),
  11343. allWrites: [],
  11344. lastWriteId: -1
  11345. };
  11346. }
  11347. /**
  11348. * If possible, returns a complete event cache, using the underlying server data if possible. In addition, can be used
  11349. * to get a cache that includes hidden writes, and excludes arbitrary writes. Note that customizing the returned node
  11350. * can lead to a more expensive calculation.
  11351. *
  11352. * @param writeIdsToExclude - Optional writes to exclude.
  11353. * @param includeHiddenWrites - Defaults to false, whether or not to layer on writes with visible set to false
  11354. */
  11355. function writeTreeRefCalcCompleteEventCache(writeTreeRef, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
  11356. return writeTreeCalcCompleteEventCache(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites);
  11357. }
  11358. /**
  11359. * If possible, returns a children node containing all of the complete children we have data for. The returned data is a
  11360. * mix of the given server data and write data.
  11361. *
  11362. */
  11363. function writeTreeRefCalcCompleteEventChildren(writeTreeRef, completeServerChildren) {
  11364. return writeTreeCalcCompleteEventChildren(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerChildren);
  11365. }
  11366. /**
  11367. * Given that either the underlying server data has updated or the outstanding writes have updated, determine what,
  11368. * if anything, needs to be applied to the event cache.
  11369. *
  11370. * Possibilities:
  11371. *
  11372. * 1. No writes are shadowing. Events should be raised, the snap to be applied comes from the server data
  11373. *
  11374. * 2. Some write is completely shadowing. No events to be raised
  11375. *
  11376. * 3. Is partially shadowed. Events should be raised
  11377. *
  11378. * Either existingEventSnap or existingServerSnap must exist, this is validated via an assert
  11379. *
  11380. *
  11381. */
  11382. function writeTreeRefCalcEventCacheAfterServerOverwrite(writeTreeRef, path, existingEventSnap, existingServerSnap) {
  11383. return writeTreeCalcEventCacheAfterServerOverwrite(writeTreeRef.writeTree, writeTreeRef.treePath, path, existingEventSnap, existingServerSnap);
  11384. }
  11385. /**
  11386. * Returns a node if there is a complete overwrite for this path. More specifically, if there is a write at
  11387. * a higher path, this will return the child of that write relative to the write and this path.
  11388. * Returns null if there is no write at this path.
  11389. *
  11390. */
  11391. function writeTreeRefShadowingWrite(writeTreeRef, path) {
  11392. return writeTreeShadowingWrite(writeTreeRef.writeTree, pathChild(writeTreeRef.treePath, path));
  11393. }
  11394. /**
  11395. * This method is used when processing child remove events on a query. If we can, we pull in children that were outside
  11396. * the window, but may now be in the window
  11397. */
  11398. function writeTreeRefCalcIndexedSlice(writeTreeRef, completeServerData, startPost, count, reverse, index) {
  11399. return writeTreeCalcIndexedSlice(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerData, startPost, count, reverse, index);
  11400. }
  11401. /**
  11402. * Returns a complete child for a given server snap after applying all user writes or null if there is no
  11403. * complete child for this ChildKey.
  11404. */
  11405. function writeTreeRefCalcCompleteChild(writeTreeRef, childKey, existingServerCache) {
  11406. return writeTreeCalcCompleteChild(writeTreeRef.writeTree, writeTreeRef.treePath, childKey, existingServerCache);
  11407. }
  11408. /**
  11409. * Return a WriteTreeRef for a child.
  11410. */
  11411. function writeTreeRefChild(writeTreeRef, childName) {
  11412. return newWriteTreeRef(pathChild(writeTreeRef.treePath, childName), writeTreeRef.writeTree);
  11413. }
  11414. function newWriteTreeRef(path, writeTree) {
  11415. return {
  11416. treePath: path,
  11417. writeTree: writeTree
  11418. };
  11419. }
  11420. /**
  11421. * @license
  11422. * Copyright 2017 Google LLC
  11423. *
  11424. * Licensed under the Apache License, Version 2.0 (the "License");
  11425. * you may not use this file except in compliance with the License.
  11426. * You may obtain a copy of the License at
  11427. *
  11428. * http://www.apache.org/licenses/LICENSE-2.0
  11429. *
  11430. * Unless required by applicable law or agreed to in writing, software
  11431. * distributed under the License is distributed on an "AS IS" BASIS,
  11432. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11433. * See the License for the specific language governing permissions and
  11434. * limitations under the License.
  11435. */
  11436. var ChildChangeAccumulator = /** @class */ (function () {
  11437. function ChildChangeAccumulator() {
  11438. this.changeMap = new Map();
  11439. }
  11440. ChildChangeAccumulator.prototype.trackChildChange = function (change) {
  11441. var type = change.type;
  11442. var childKey = change.childName;
  11443. util.assert(type === "child_added" /* ChangeType.CHILD_ADDED */ ||
  11444. type === "child_changed" /* ChangeType.CHILD_CHANGED */ ||
  11445. type === "child_removed" /* ChangeType.CHILD_REMOVED */, 'Only child changes supported for tracking');
  11446. util.assert(childKey !== '.priority', 'Only non-priority child changes can be tracked.');
  11447. var oldChange = this.changeMap.get(childKey);
  11448. if (oldChange) {
  11449. var oldType = oldChange.type;
  11450. if (type === "child_added" /* ChangeType.CHILD_ADDED */ &&
  11451. oldType === "child_removed" /* ChangeType.CHILD_REMOVED */) {
  11452. this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.snapshotNode));
  11453. }
  11454. else if (type === "child_removed" /* ChangeType.CHILD_REMOVED */ &&
  11455. oldType === "child_added" /* ChangeType.CHILD_ADDED */) {
  11456. this.changeMap.delete(childKey);
  11457. }
  11458. else if (type === "child_removed" /* ChangeType.CHILD_REMOVED */ &&
  11459. oldType === "child_changed" /* ChangeType.CHILD_CHANGED */) {
  11460. this.changeMap.set(childKey, changeChildRemoved(childKey, oldChange.oldSnap));
  11461. }
  11462. else if (type === "child_changed" /* ChangeType.CHILD_CHANGED */ &&
  11463. oldType === "child_added" /* ChangeType.CHILD_ADDED */) {
  11464. this.changeMap.set(childKey, changeChildAdded(childKey, change.snapshotNode));
  11465. }
  11466. else if (type === "child_changed" /* ChangeType.CHILD_CHANGED */ &&
  11467. oldType === "child_changed" /* ChangeType.CHILD_CHANGED */) {
  11468. this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.oldSnap));
  11469. }
  11470. else {
  11471. throw util.assertionError('Illegal combination of changes: ' +
  11472. change +
  11473. ' occurred after ' +
  11474. oldChange);
  11475. }
  11476. }
  11477. else {
  11478. this.changeMap.set(childKey, change);
  11479. }
  11480. };
  11481. ChildChangeAccumulator.prototype.getChanges = function () {
  11482. return Array.from(this.changeMap.values());
  11483. };
  11484. return ChildChangeAccumulator;
  11485. }());
  11486. /**
  11487. * @license
  11488. * Copyright 2017 Google LLC
  11489. *
  11490. * Licensed under the Apache License, Version 2.0 (the "License");
  11491. * you may not use this file except in compliance with the License.
  11492. * You may obtain a copy of the License at
  11493. *
  11494. * http://www.apache.org/licenses/LICENSE-2.0
  11495. *
  11496. * Unless required by applicable law or agreed to in writing, software
  11497. * distributed under the License is distributed on an "AS IS" BASIS,
  11498. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11499. * See the License for the specific language governing permissions and
  11500. * limitations under the License.
  11501. */
  11502. /**
  11503. * An implementation of CompleteChildSource that never returns any additional children
  11504. */
  11505. // eslint-disable-next-line @typescript-eslint/naming-convention
  11506. var NoCompleteChildSource_ = /** @class */ (function () {
  11507. function NoCompleteChildSource_() {
  11508. }
  11509. NoCompleteChildSource_.prototype.getCompleteChild = function (childKey) {
  11510. return null;
  11511. };
  11512. NoCompleteChildSource_.prototype.getChildAfterChild = function (index, child, reverse) {
  11513. return null;
  11514. };
  11515. return NoCompleteChildSource_;
  11516. }());
  11517. /**
  11518. * Singleton instance.
  11519. */
  11520. var NO_COMPLETE_CHILD_SOURCE = new NoCompleteChildSource_();
  11521. /**
  11522. * An implementation of CompleteChildSource that uses a WriteTree in addition to any other server data or
  11523. * old event caches available to calculate complete children.
  11524. */
  11525. var WriteTreeCompleteChildSource = /** @class */ (function () {
  11526. function WriteTreeCompleteChildSource(writes_, viewCache_, optCompleteServerCache_) {
  11527. if (optCompleteServerCache_ === void 0) { optCompleteServerCache_ = null; }
  11528. this.writes_ = writes_;
  11529. this.viewCache_ = viewCache_;
  11530. this.optCompleteServerCache_ = optCompleteServerCache_;
  11531. }
  11532. WriteTreeCompleteChildSource.prototype.getCompleteChild = function (childKey) {
  11533. var node = this.viewCache_.eventCache;
  11534. if (node.isCompleteForChild(childKey)) {
  11535. return node.getNode().getImmediateChild(childKey);
  11536. }
  11537. else {
  11538. var serverNode = this.optCompleteServerCache_ != null
  11539. ? new CacheNode(this.optCompleteServerCache_, true, false)
  11540. : this.viewCache_.serverCache;
  11541. return writeTreeRefCalcCompleteChild(this.writes_, childKey, serverNode);
  11542. }
  11543. };
  11544. WriteTreeCompleteChildSource.prototype.getChildAfterChild = function (index, child, reverse) {
  11545. var completeServerData = this.optCompleteServerCache_ != null
  11546. ? this.optCompleteServerCache_
  11547. : viewCacheGetCompleteServerSnap(this.viewCache_);
  11548. var nodes = writeTreeRefCalcIndexedSlice(this.writes_, completeServerData, child, 1, reverse, index);
  11549. if (nodes.length === 0) {
  11550. return null;
  11551. }
  11552. else {
  11553. return nodes[0];
  11554. }
  11555. };
  11556. return WriteTreeCompleteChildSource;
  11557. }());
  11558. /**
  11559. * @license
  11560. * Copyright 2017 Google LLC
  11561. *
  11562. * Licensed under the Apache License, Version 2.0 (the "License");
  11563. * you may not use this file except in compliance with the License.
  11564. * You may obtain a copy of the License at
  11565. *
  11566. * http://www.apache.org/licenses/LICENSE-2.0
  11567. *
  11568. * Unless required by applicable law or agreed to in writing, software
  11569. * distributed under the License is distributed on an "AS IS" BASIS,
  11570. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11571. * See the License for the specific language governing permissions and
  11572. * limitations under the License.
  11573. */
  11574. function newViewProcessor(filter) {
  11575. return { filter: filter };
  11576. }
  11577. function viewProcessorAssertIndexed(viewProcessor, viewCache) {
  11578. util.assert(viewCache.eventCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Event snap not indexed');
  11579. util.assert(viewCache.serverCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Server snap not indexed');
  11580. }
  11581. function viewProcessorApplyOperation(viewProcessor, oldViewCache, operation, writesCache, completeCache) {
  11582. var accumulator = new ChildChangeAccumulator();
  11583. var newViewCache, filterServerNode;
  11584. if (operation.type === OperationType.OVERWRITE) {
  11585. var overwrite = operation;
  11586. if (overwrite.source.fromUser) {
  11587. newViewCache = viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, accumulator);
  11588. }
  11589. else {
  11590. util.assert(overwrite.source.fromServer, 'Unknown source.');
  11591. // We filter the node if it's a tagged update or the node has been previously filtered and the
  11592. // update is not at the root in which case it is ok (and necessary) to mark the node unfiltered
  11593. // again
  11594. filterServerNode =
  11595. overwrite.source.tagged ||
  11596. (oldViewCache.serverCache.isFiltered() && !pathIsEmpty(overwrite.path));
  11597. newViewCache = viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, filterServerNode, accumulator);
  11598. }
  11599. }
  11600. else if (operation.type === OperationType.MERGE) {
  11601. var merge = operation;
  11602. if (merge.source.fromUser) {
  11603. newViewCache = viewProcessorApplyUserMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, accumulator);
  11604. }
  11605. else {
  11606. util.assert(merge.source.fromServer, 'Unknown source.');
  11607. // We filter the node if it's a tagged update or the node has been previously filtered
  11608. filterServerNode =
  11609. merge.source.tagged || oldViewCache.serverCache.isFiltered();
  11610. newViewCache = viewProcessorApplyServerMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, filterServerNode, accumulator);
  11611. }
  11612. }
  11613. else if (operation.type === OperationType.ACK_USER_WRITE) {
  11614. var ackUserWrite = operation;
  11615. if (!ackUserWrite.revert) {
  11616. newViewCache = viewProcessorAckUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, ackUserWrite.affectedTree, writesCache, completeCache, accumulator);
  11617. }
  11618. else {
  11619. newViewCache = viewProcessorRevertUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, writesCache, completeCache, accumulator);
  11620. }
  11621. }
  11622. else if (operation.type === OperationType.LISTEN_COMPLETE) {
  11623. newViewCache = viewProcessorListenComplete(viewProcessor, oldViewCache, operation.path, writesCache, accumulator);
  11624. }
  11625. else {
  11626. throw util.assertionError('Unknown operation type: ' + operation.type);
  11627. }
  11628. var changes = accumulator.getChanges();
  11629. viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, changes);
  11630. return { viewCache: newViewCache, changes: changes };
  11631. }
  11632. function viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, accumulator) {
  11633. var eventSnap = newViewCache.eventCache;
  11634. if (eventSnap.isFullyInitialized()) {
  11635. var isLeafOrEmpty = eventSnap.getNode().isLeafNode() || eventSnap.getNode().isEmpty();
  11636. var oldCompleteSnap = viewCacheGetCompleteEventSnap(oldViewCache);
  11637. if (accumulator.length > 0 ||
  11638. !oldViewCache.eventCache.isFullyInitialized() ||
  11639. (isLeafOrEmpty && !eventSnap.getNode().equals(oldCompleteSnap)) ||
  11640. !eventSnap.getNode().getPriority().equals(oldCompleteSnap.getPriority())) {
  11641. accumulator.push(changeValue(viewCacheGetCompleteEventSnap(newViewCache)));
  11642. }
  11643. }
  11644. }
  11645. function viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, viewCache, changePath, writesCache, source, accumulator) {
  11646. var oldEventSnap = viewCache.eventCache;
  11647. if (writeTreeRefShadowingWrite(writesCache, changePath) != null) {
  11648. // we have a shadowing write, ignore changes
  11649. return viewCache;
  11650. }
  11651. else {
  11652. var newEventCache = void 0, serverNode = void 0;
  11653. if (pathIsEmpty(changePath)) {
  11654. // TODO: figure out how this plays with "sliding ack windows"
  11655. util.assert(viewCache.serverCache.isFullyInitialized(), 'If change path is empty, we must have complete server data');
  11656. if (viewCache.serverCache.isFiltered()) {
  11657. // We need to special case this, because we need to only apply writes to complete children, or
  11658. // we might end up raising events for incomplete children. If the server data is filtered deep
  11659. // writes cannot be guaranteed to be complete
  11660. var serverCache = viewCacheGetCompleteServerSnap(viewCache);
  11661. var completeChildren = serverCache instanceof ChildrenNode
  11662. ? serverCache
  11663. : ChildrenNode.EMPTY_NODE;
  11664. var completeEventChildren = writeTreeRefCalcCompleteEventChildren(writesCache, completeChildren);
  11665. newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeEventChildren, accumulator);
  11666. }
  11667. else {
  11668. var completeNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
  11669. newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeNode, accumulator);
  11670. }
  11671. }
  11672. else {
  11673. var childKey = pathGetFront(changePath);
  11674. if (childKey === '.priority') {
  11675. util.assert(pathGetLength(changePath) === 1, "Can't have a priority with additional path components");
  11676. var oldEventNode = oldEventSnap.getNode();
  11677. serverNode = viewCache.serverCache.getNode();
  11678. // we might have overwrites for this priority
  11679. var updatedPriority = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventNode, serverNode);
  11680. if (updatedPriority != null) {
  11681. newEventCache = viewProcessor.filter.updatePriority(oldEventNode, updatedPriority);
  11682. }
  11683. else {
  11684. // priority didn't change, keep old node
  11685. newEventCache = oldEventSnap.getNode();
  11686. }
  11687. }
  11688. else {
  11689. var childChangePath = pathPopFront(changePath);
  11690. // update child
  11691. var newEventChild = void 0;
  11692. if (oldEventSnap.isCompleteForChild(childKey)) {
  11693. serverNode = viewCache.serverCache.getNode();
  11694. var eventChildUpdate = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventSnap.getNode(), serverNode);
  11695. if (eventChildUpdate != null) {
  11696. newEventChild = oldEventSnap
  11697. .getNode()
  11698. .getImmediateChild(childKey)
  11699. .updateChild(childChangePath, eventChildUpdate);
  11700. }
  11701. else {
  11702. // Nothing changed, just keep the old child
  11703. newEventChild = oldEventSnap.getNode().getImmediateChild(childKey);
  11704. }
  11705. }
  11706. else {
  11707. newEventChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
  11708. }
  11709. if (newEventChild != null) {
  11710. newEventCache = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newEventChild, childChangePath, source, accumulator);
  11711. }
  11712. else {
  11713. // no complete child available or no change
  11714. newEventCache = oldEventSnap.getNode();
  11715. }
  11716. }
  11717. }
  11718. return viewCacheUpdateEventSnap(viewCache, newEventCache, oldEventSnap.isFullyInitialized() || pathIsEmpty(changePath), viewProcessor.filter.filtersNodes());
  11719. }
  11720. }
  11721. function viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, filterServerNode, accumulator) {
  11722. var oldServerSnap = oldViewCache.serverCache;
  11723. var newServerCache;
  11724. var serverFilter = filterServerNode
  11725. ? viewProcessor.filter
  11726. : viewProcessor.filter.getIndexedFilter();
  11727. if (pathIsEmpty(changePath)) {
  11728. newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), changedSnap, null);
  11729. }
  11730. else if (serverFilter.filtersNodes() && !oldServerSnap.isFiltered()) {
  11731. // we want to filter the server node, but we didn't filter the server node yet, so simulate a full update
  11732. var newServerNode = oldServerSnap
  11733. .getNode()
  11734. .updateChild(changePath, changedSnap);
  11735. newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), newServerNode, null);
  11736. }
  11737. else {
  11738. var childKey = pathGetFront(changePath);
  11739. if (!oldServerSnap.isCompleteForPath(changePath) &&
  11740. pathGetLength(changePath) > 1) {
  11741. // We don't update incomplete nodes with updates intended for other listeners
  11742. return oldViewCache;
  11743. }
  11744. var childChangePath = pathPopFront(changePath);
  11745. var childNode = oldServerSnap.getNode().getImmediateChild(childKey);
  11746. var newChildNode = childNode.updateChild(childChangePath, changedSnap);
  11747. if (childKey === '.priority') {
  11748. newServerCache = serverFilter.updatePriority(oldServerSnap.getNode(), newChildNode);
  11749. }
  11750. else {
  11751. newServerCache = serverFilter.updateChild(oldServerSnap.getNode(), childKey, newChildNode, childChangePath, NO_COMPLETE_CHILD_SOURCE, null);
  11752. }
  11753. }
  11754. var newViewCache = viewCacheUpdateServerSnap(oldViewCache, newServerCache, oldServerSnap.isFullyInitialized() || pathIsEmpty(changePath), serverFilter.filtersNodes());
  11755. var source = new WriteTreeCompleteChildSource(writesCache, newViewCache, completeCache);
  11756. return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, changePath, writesCache, source, accumulator);
  11757. }
  11758. function viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, accumulator) {
  11759. var oldEventSnap = oldViewCache.eventCache;
  11760. var newViewCache, newEventCache;
  11761. var source = new WriteTreeCompleteChildSource(writesCache, oldViewCache, completeCache);
  11762. if (pathIsEmpty(changePath)) {
  11763. newEventCache = viewProcessor.filter.updateFullNode(oldViewCache.eventCache.getNode(), changedSnap, accumulator);
  11764. newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, true, viewProcessor.filter.filtersNodes());
  11765. }
  11766. else {
  11767. var childKey = pathGetFront(changePath);
  11768. if (childKey === '.priority') {
  11769. newEventCache = viewProcessor.filter.updatePriority(oldViewCache.eventCache.getNode(), changedSnap);
  11770. newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, oldEventSnap.isFullyInitialized(), oldEventSnap.isFiltered());
  11771. }
  11772. else {
  11773. var childChangePath = pathPopFront(changePath);
  11774. var oldChild = oldEventSnap.getNode().getImmediateChild(childKey);
  11775. var newChild = void 0;
  11776. if (pathIsEmpty(childChangePath)) {
  11777. // Child overwrite, we can replace the child
  11778. newChild = changedSnap;
  11779. }
  11780. else {
  11781. var childNode = source.getCompleteChild(childKey);
  11782. if (childNode != null) {
  11783. if (pathGetBack(childChangePath) === '.priority' &&
  11784. childNode.getChild(pathParent(childChangePath)).isEmpty()) {
  11785. // This is a priority update on an empty node. If this node exists on the server, the
  11786. // server will send down the priority in the update, so ignore for now
  11787. newChild = childNode;
  11788. }
  11789. else {
  11790. newChild = childNode.updateChild(childChangePath, changedSnap);
  11791. }
  11792. }
  11793. else {
  11794. // There is no complete child node available
  11795. newChild = ChildrenNode.EMPTY_NODE;
  11796. }
  11797. }
  11798. if (!oldChild.equals(newChild)) {
  11799. var newEventSnap = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newChild, childChangePath, source, accumulator);
  11800. newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventSnap, oldEventSnap.isFullyInitialized(), viewProcessor.filter.filtersNodes());
  11801. }
  11802. else {
  11803. newViewCache = oldViewCache;
  11804. }
  11805. }
  11806. }
  11807. return newViewCache;
  11808. }
  11809. function viewProcessorCacheHasChild(viewCache, childKey) {
  11810. return viewCache.eventCache.isCompleteForChild(childKey);
  11811. }
  11812. function viewProcessorApplyUserMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, accumulator) {
  11813. // HACK: In the case of a limit query, there may be some changes that bump things out of the
  11814. // window leaving room for new items. It's important we process these changes first, so we
  11815. // iterate the changes twice, first processing any that affect items currently in view.
  11816. // TODO: I consider an item "in view" if cacheHasChild is true, which checks both the server
  11817. // and event snap. I'm not sure if this will result in edge cases when a child is in one but
  11818. // not the other.
  11819. var curViewCache = viewCache;
  11820. changedChildren.foreach(function (relativePath, childNode) {
  11821. var writePath = pathChild(path, relativePath);
  11822. if (viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
  11823. curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
  11824. }
  11825. });
  11826. changedChildren.foreach(function (relativePath, childNode) {
  11827. var writePath = pathChild(path, relativePath);
  11828. if (!viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
  11829. curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
  11830. }
  11831. });
  11832. return curViewCache;
  11833. }
  11834. function viewProcessorApplyMerge(viewProcessor, node, merge) {
  11835. merge.foreach(function (relativePath, childNode) {
  11836. node = node.updateChild(relativePath, childNode);
  11837. });
  11838. return node;
  11839. }
  11840. function viewProcessorApplyServerMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, filterServerNode, accumulator) {
  11841. // If we don't have a cache yet, this merge was intended for a previously listen in the same location. Ignore it and
  11842. // wait for the complete data update coming soon.
  11843. if (viewCache.serverCache.getNode().isEmpty() &&
  11844. !viewCache.serverCache.isFullyInitialized()) {
  11845. return viewCache;
  11846. }
  11847. // HACK: In the case of a limit query, there may be some changes that bump things out of the
  11848. // window leaving room for new items. It's important we process these changes first, so we
  11849. // iterate the changes twice, first processing any that affect items currently in view.
  11850. // TODO: I consider an item "in view" if cacheHasChild is true, which checks both the server
  11851. // and event snap. I'm not sure if this will result in edge cases when a child is in one but
  11852. // not the other.
  11853. var curViewCache = viewCache;
  11854. var viewMergeTree;
  11855. if (pathIsEmpty(path)) {
  11856. viewMergeTree = changedChildren;
  11857. }
  11858. else {
  11859. viewMergeTree = new ImmutableTree(null).setTree(path, changedChildren);
  11860. }
  11861. var serverNode = viewCache.serverCache.getNode();
  11862. viewMergeTree.children.inorderTraversal(function (childKey, childTree) {
  11863. if (serverNode.hasChild(childKey)) {
  11864. var serverChild = viewCache.serverCache
  11865. .getNode()
  11866. .getImmediateChild(childKey);
  11867. var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childTree);
  11868. curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
  11869. }
  11870. });
  11871. viewMergeTree.children.inorderTraversal(function (childKey, childMergeTree) {
  11872. var isUnknownDeepMerge = !viewCache.serverCache.isCompleteForChild(childKey) &&
  11873. childMergeTree.value === null;
  11874. if (!serverNode.hasChild(childKey) && !isUnknownDeepMerge) {
  11875. var serverChild = viewCache.serverCache
  11876. .getNode()
  11877. .getImmediateChild(childKey);
  11878. var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childMergeTree);
  11879. curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
  11880. }
  11881. });
  11882. return curViewCache;
  11883. }
  11884. function viewProcessorAckUserWrite(viewProcessor, viewCache, ackPath, affectedTree, writesCache, completeCache, accumulator) {
  11885. if (writeTreeRefShadowingWrite(writesCache, ackPath) != null) {
  11886. return viewCache;
  11887. }
  11888. // Only filter server node if it is currently filtered
  11889. var filterServerNode = viewCache.serverCache.isFiltered();
  11890. // Essentially we'll just get our existing server cache for the affected paths and re-apply it as a server update
  11891. // now that it won't be shadowed.
  11892. var serverCache = viewCache.serverCache;
  11893. if (affectedTree.value != null) {
  11894. // This is an overwrite.
  11895. if ((pathIsEmpty(ackPath) && serverCache.isFullyInitialized()) ||
  11896. serverCache.isCompleteForPath(ackPath)) {
  11897. return viewProcessorApplyServerOverwrite(viewProcessor, viewCache, ackPath, serverCache.getNode().getChild(ackPath), writesCache, completeCache, filterServerNode, accumulator);
  11898. }
  11899. else if (pathIsEmpty(ackPath)) {
  11900. // This is a goofy edge case where we are acking data at this location but don't have full data. We
  11901. // should just re-apply whatever we have in our cache as a merge.
  11902. var changedChildren_1 = new ImmutableTree(null);
  11903. serverCache.getNode().forEachChild(KEY_INDEX, function (name, node) {
  11904. changedChildren_1 = changedChildren_1.set(new Path(name), node);
  11905. });
  11906. return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_1, writesCache, completeCache, filterServerNode, accumulator);
  11907. }
  11908. else {
  11909. return viewCache;
  11910. }
  11911. }
  11912. else {
  11913. // This is a merge.
  11914. var changedChildren_2 = new ImmutableTree(null);
  11915. affectedTree.foreach(function (mergePath, value) {
  11916. var serverCachePath = pathChild(ackPath, mergePath);
  11917. if (serverCache.isCompleteForPath(serverCachePath)) {
  11918. changedChildren_2 = changedChildren_2.set(mergePath, serverCache.getNode().getChild(serverCachePath));
  11919. }
  11920. });
  11921. return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_2, writesCache, completeCache, filterServerNode, accumulator);
  11922. }
  11923. }
  11924. function viewProcessorListenComplete(viewProcessor, viewCache, path, writesCache, accumulator) {
  11925. var oldServerNode = viewCache.serverCache;
  11926. var newViewCache = viewCacheUpdateServerSnap(viewCache, oldServerNode.getNode(), oldServerNode.isFullyInitialized() || pathIsEmpty(path), oldServerNode.isFiltered());
  11927. return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, path, writesCache, NO_COMPLETE_CHILD_SOURCE, accumulator);
  11928. }
  11929. function viewProcessorRevertUserWrite(viewProcessor, viewCache, path, writesCache, completeServerCache, accumulator) {
  11930. var complete;
  11931. if (writeTreeRefShadowingWrite(writesCache, path) != null) {
  11932. return viewCache;
  11933. }
  11934. else {
  11935. var source = new WriteTreeCompleteChildSource(writesCache, viewCache, completeServerCache);
  11936. var oldEventCache = viewCache.eventCache.getNode();
  11937. var newEventCache = void 0;
  11938. if (pathIsEmpty(path) || pathGetFront(path) === '.priority') {
  11939. var newNode = void 0;
  11940. if (viewCache.serverCache.isFullyInitialized()) {
  11941. newNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
  11942. }
  11943. else {
  11944. var serverChildren = viewCache.serverCache.getNode();
  11945. util.assert(serverChildren instanceof ChildrenNode, 'serverChildren would be complete if leaf node');
  11946. newNode = writeTreeRefCalcCompleteEventChildren(writesCache, serverChildren);
  11947. }
  11948. newNode = newNode;
  11949. newEventCache = viewProcessor.filter.updateFullNode(oldEventCache, newNode, accumulator);
  11950. }
  11951. else {
  11952. var childKey = pathGetFront(path);
  11953. var newChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
  11954. if (newChild == null &&
  11955. viewCache.serverCache.isCompleteForChild(childKey)) {
  11956. newChild = oldEventCache.getImmediateChild(childKey);
  11957. }
  11958. if (newChild != null) {
  11959. newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, newChild, pathPopFront(path), source, accumulator);
  11960. }
  11961. else if (viewCache.eventCache.getNode().hasChild(childKey)) {
  11962. // No complete child available, delete the existing one, if any
  11963. newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, ChildrenNode.EMPTY_NODE, pathPopFront(path), source, accumulator);
  11964. }
  11965. else {
  11966. newEventCache = oldEventCache;
  11967. }
  11968. if (newEventCache.isEmpty() &&
  11969. viewCache.serverCache.isFullyInitialized()) {
  11970. // We might have reverted all child writes. Maybe the old event was a leaf node
  11971. complete = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
  11972. if (complete.isLeafNode()) {
  11973. newEventCache = viewProcessor.filter.updateFullNode(newEventCache, complete, accumulator);
  11974. }
  11975. }
  11976. }
  11977. complete =
  11978. viewCache.serverCache.isFullyInitialized() ||
  11979. writeTreeRefShadowingWrite(writesCache, newEmptyPath()) != null;
  11980. return viewCacheUpdateEventSnap(viewCache, newEventCache, complete, viewProcessor.filter.filtersNodes());
  11981. }
  11982. }
  11983. /**
  11984. * @license
  11985. * Copyright 2017 Google LLC
  11986. *
  11987. * Licensed under the Apache License, Version 2.0 (the "License");
  11988. * you may not use this file except in compliance with the License.
  11989. * You may obtain a copy of the License at
  11990. *
  11991. * http://www.apache.org/licenses/LICENSE-2.0
  11992. *
  11993. * Unless required by applicable law or agreed to in writing, software
  11994. * distributed under the License is distributed on an "AS IS" BASIS,
  11995. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11996. * See the License for the specific language governing permissions and
  11997. * limitations under the License.
  11998. */
  11999. /**
  12000. * A view represents a specific location and query that has 1 or more event registrations.
  12001. *
  12002. * It does several things:
  12003. * - Maintains the list of event registrations for this location/query.
  12004. * - Maintains a cache of the data visible for this location/query.
  12005. * - Applies new operations (via applyOperation), updates the cache, and based on the event
  12006. * registrations returns the set of events to be raised.
  12007. */
  12008. var View = /** @class */ (function () {
  12009. function View(query_, initialViewCache) {
  12010. this.query_ = query_;
  12011. this.eventRegistrations_ = [];
  12012. var params = this.query_._queryParams;
  12013. var indexFilter = new IndexedFilter(params.getIndex());
  12014. var filter = queryParamsGetNodeFilter(params);
  12015. this.processor_ = newViewProcessor(filter);
  12016. var initialServerCache = initialViewCache.serverCache;
  12017. var initialEventCache = initialViewCache.eventCache;
  12018. // Don't filter server node with other filter than index, wait for tagged listen
  12019. var serverSnap = indexFilter.updateFullNode(ChildrenNode.EMPTY_NODE, initialServerCache.getNode(), null);
  12020. var eventSnap = filter.updateFullNode(ChildrenNode.EMPTY_NODE, initialEventCache.getNode(), null);
  12021. var newServerCache = new CacheNode(serverSnap, initialServerCache.isFullyInitialized(), indexFilter.filtersNodes());
  12022. var newEventCache = new CacheNode(eventSnap, initialEventCache.isFullyInitialized(), filter.filtersNodes());
  12023. this.viewCache_ = newViewCache(newEventCache, newServerCache);
  12024. this.eventGenerator_ = new EventGenerator(this.query_);
  12025. }
  12026. Object.defineProperty(View.prototype, "query", {
  12027. get: function () {
  12028. return this.query_;
  12029. },
  12030. enumerable: false,
  12031. configurable: true
  12032. });
  12033. return View;
  12034. }());
  12035. function viewGetServerCache(view) {
  12036. return view.viewCache_.serverCache.getNode();
  12037. }
  12038. function viewGetCompleteNode(view) {
  12039. return viewCacheGetCompleteEventSnap(view.viewCache_);
  12040. }
  12041. function viewGetCompleteServerCache(view, path) {
  12042. var cache = viewCacheGetCompleteServerSnap(view.viewCache_);
  12043. if (cache) {
  12044. // If this isn't a "loadsAllData" view, then cache isn't actually a complete cache and
  12045. // we need to see if it contains the child we're interested in.
  12046. if (view.query._queryParams.loadsAllData() ||
  12047. (!pathIsEmpty(path) &&
  12048. !cache.getImmediateChild(pathGetFront(path)).isEmpty())) {
  12049. return cache.getChild(path);
  12050. }
  12051. }
  12052. return null;
  12053. }
  12054. function viewIsEmpty(view) {
  12055. return view.eventRegistrations_.length === 0;
  12056. }
  12057. function viewAddEventRegistration(view, eventRegistration) {
  12058. view.eventRegistrations_.push(eventRegistration);
  12059. }
  12060. /**
  12061. * @param eventRegistration - If null, remove all callbacks.
  12062. * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
  12063. * @returns Cancel events, if cancelError was provided.
  12064. */
  12065. function viewRemoveEventRegistration(view, eventRegistration, cancelError) {
  12066. var cancelEvents = [];
  12067. if (cancelError) {
  12068. util.assert(eventRegistration == null, 'A cancel should cancel all event registrations.');
  12069. var path_1 = view.query._path;
  12070. view.eventRegistrations_.forEach(function (registration) {
  12071. var maybeEvent = registration.createCancelEvent(cancelError, path_1);
  12072. if (maybeEvent) {
  12073. cancelEvents.push(maybeEvent);
  12074. }
  12075. });
  12076. }
  12077. if (eventRegistration) {
  12078. var remaining = [];
  12079. for (var i = 0; i < view.eventRegistrations_.length; ++i) {
  12080. var existing = view.eventRegistrations_[i];
  12081. if (!existing.matches(eventRegistration)) {
  12082. remaining.push(existing);
  12083. }
  12084. else if (eventRegistration.hasAnyCallback()) {
  12085. // We're removing just this one
  12086. remaining = remaining.concat(view.eventRegistrations_.slice(i + 1));
  12087. break;
  12088. }
  12089. }
  12090. view.eventRegistrations_ = remaining;
  12091. }
  12092. else {
  12093. view.eventRegistrations_ = [];
  12094. }
  12095. return cancelEvents;
  12096. }
  12097. /**
  12098. * Applies the given Operation, updates our cache, and returns the appropriate events.
  12099. */
  12100. function viewApplyOperation(view, operation, writesCache, completeServerCache) {
  12101. if (operation.type === OperationType.MERGE &&
  12102. operation.source.queryId !== null) {
  12103. util.assert(viewCacheGetCompleteServerSnap(view.viewCache_), 'We should always have a full cache before handling merges');
  12104. util.assert(viewCacheGetCompleteEventSnap(view.viewCache_), 'Missing event cache, even though we have a server cache');
  12105. }
  12106. var oldViewCache = view.viewCache_;
  12107. var result = viewProcessorApplyOperation(view.processor_, oldViewCache, operation, writesCache, completeServerCache);
  12108. viewProcessorAssertIndexed(view.processor_, result.viewCache);
  12109. util.assert(result.viewCache.serverCache.isFullyInitialized() ||
  12110. !oldViewCache.serverCache.isFullyInitialized(), 'Once a server snap is complete, it should never go back');
  12111. view.viewCache_ = result.viewCache;
  12112. return viewGenerateEventsForChanges_(view, result.changes, result.viewCache.eventCache.getNode(), null);
  12113. }
  12114. function viewGetInitialEvents(view, registration) {
  12115. var eventSnap = view.viewCache_.eventCache;
  12116. var initialChanges = [];
  12117. if (!eventSnap.getNode().isLeafNode()) {
  12118. var eventNode = eventSnap.getNode();
  12119. eventNode.forEachChild(PRIORITY_INDEX, function (key, childNode) {
  12120. initialChanges.push(changeChildAdded(key, childNode));
  12121. });
  12122. }
  12123. if (eventSnap.isFullyInitialized()) {
  12124. initialChanges.push(changeValue(eventSnap.getNode()));
  12125. }
  12126. return viewGenerateEventsForChanges_(view, initialChanges, eventSnap.getNode(), registration);
  12127. }
  12128. function viewGenerateEventsForChanges_(view, changes, eventCache, eventRegistration) {
  12129. var registrations = eventRegistration
  12130. ? [eventRegistration]
  12131. : view.eventRegistrations_;
  12132. return eventGeneratorGenerateEventsForChanges(view.eventGenerator_, changes, eventCache, registrations);
  12133. }
  12134. /**
  12135. * @license
  12136. * Copyright 2017 Google LLC
  12137. *
  12138. * Licensed under the Apache License, Version 2.0 (the "License");
  12139. * you may not use this file except in compliance with the License.
  12140. * You may obtain a copy of the License at
  12141. *
  12142. * http://www.apache.org/licenses/LICENSE-2.0
  12143. *
  12144. * Unless required by applicable law or agreed to in writing, software
  12145. * distributed under the License is distributed on an "AS IS" BASIS,
  12146. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12147. * See the License for the specific language governing permissions and
  12148. * limitations under the License.
  12149. */
  12150. var referenceConstructor$1;
  12151. /**
  12152. * SyncPoint represents a single location in a SyncTree with 1 or more event registrations, meaning we need to
  12153. * maintain 1 or more Views at this location to cache server data and raise appropriate events for server changes
  12154. * and user writes (set, transaction, update).
  12155. *
  12156. * It's responsible for:
  12157. * - Maintaining the set of 1 or more views necessary at this location (a SyncPoint with 0 views should be removed).
  12158. * - Proxying user / server operations to the views as appropriate (i.e. applyServerOverwrite,
  12159. * applyUserOverwrite, etc.)
  12160. */
  12161. var SyncPoint = /** @class */ (function () {
  12162. function SyncPoint() {
  12163. /**
  12164. * The Views being tracked at this location in the tree, stored as a map where the key is a
  12165. * queryId and the value is the View for that query.
  12166. *
  12167. * NOTE: This list will be quite small (usually 1, but perhaps 2 or 3; any more is an odd use case).
  12168. */
  12169. this.views = new Map();
  12170. }
  12171. return SyncPoint;
  12172. }());
  12173. function syncPointSetReferenceConstructor(val) {
  12174. util.assert(!referenceConstructor$1, '__referenceConstructor has already been defined');
  12175. referenceConstructor$1 = val;
  12176. }
  12177. function syncPointGetReferenceConstructor() {
  12178. util.assert(referenceConstructor$1, 'Reference.ts has not been loaded');
  12179. return referenceConstructor$1;
  12180. }
  12181. function syncPointIsEmpty(syncPoint) {
  12182. return syncPoint.views.size === 0;
  12183. }
  12184. function syncPointApplyOperation(syncPoint, operation, writesCache, optCompleteServerCache) {
  12185. var e_1, _a;
  12186. var queryId = operation.source.queryId;
  12187. if (queryId !== null) {
  12188. var view = syncPoint.views.get(queryId);
  12189. util.assert(view != null, 'SyncTree gave us an op for an invalid query.');
  12190. return viewApplyOperation(view, operation, writesCache, optCompleteServerCache);
  12191. }
  12192. else {
  12193. var events = [];
  12194. try {
  12195. for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
  12196. var view = _c.value;
  12197. events = events.concat(viewApplyOperation(view, operation, writesCache, optCompleteServerCache));
  12198. }
  12199. }
  12200. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  12201. finally {
  12202. try {
  12203. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  12204. }
  12205. finally { if (e_1) throw e_1.error; }
  12206. }
  12207. return events;
  12208. }
  12209. }
  12210. /**
  12211. * Get a view for the specified query.
  12212. *
  12213. * @param query - The query to return a view for
  12214. * @param writesCache
  12215. * @param serverCache
  12216. * @param serverCacheComplete
  12217. * @returns Events to raise.
  12218. */
  12219. function syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete) {
  12220. var queryId = query._queryIdentifier;
  12221. var view = syncPoint.views.get(queryId);
  12222. if (!view) {
  12223. // TODO: make writesCache take flag for complete server node
  12224. var eventCache = writeTreeRefCalcCompleteEventCache(writesCache, serverCacheComplete ? serverCache : null);
  12225. var eventCacheComplete = false;
  12226. if (eventCache) {
  12227. eventCacheComplete = true;
  12228. }
  12229. else if (serverCache instanceof ChildrenNode) {
  12230. eventCache = writeTreeRefCalcCompleteEventChildren(writesCache, serverCache);
  12231. eventCacheComplete = false;
  12232. }
  12233. else {
  12234. eventCache = ChildrenNode.EMPTY_NODE;
  12235. eventCacheComplete = false;
  12236. }
  12237. var viewCache = newViewCache(new CacheNode(eventCache, eventCacheComplete, false), new CacheNode(serverCache, serverCacheComplete, false));
  12238. return new View(query, viewCache);
  12239. }
  12240. return view;
  12241. }
  12242. /**
  12243. * Add an event callback for the specified query.
  12244. *
  12245. * @param query
  12246. * @param eventRegistration
  12247. * @param writesCache
  12248. * @param serverCache - Complete server cache, if we have it.
  12249. * @param serverCacheComplete
  12250. * @returns Events to raise.
  12251. */
  12252. function syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete) {
  12253. var view = syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete);
  12254. if (!syncPoint.views.has(query._queryIdentifier)) {
  12255. syncPoint.views.set(query._queryIdentifier, view);
  12256. }
  12257. // This is guaranteed to exist now, we just created anything that was missing
  12258. viewAddEventRegistration(view, eventRegistration);
  12259. return viewGetInitialEvents(view, eventRegistration);
  12260. }
  12261. /**
  12262. * Remove event callback(s). Return cancelEvents if a cancelError is specified.
  12263. *
  12264. * If query is the default query, we'll check all views for the specified eventRegistration.
  12265. * If eventRegistration is null, we'll remove all callbacks for the specified view(s).
  12266. *
  12267. * @param eventRegistration - If null, remove all callbacks.
  12268. * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
  12269. * @returns removed queries and any cancel events
  12270. */
  12271. function syncPointRemoveEventRegistration(syncPoint, query, eventRegistration, cancelError) {
  12272. var e_2, _a;
  12273. var queryId = query._queryIdentifier;
  12274. var removed = [];
  12275. var cancelEvents = [];
  12276. var hadCompleteView = syncPointHasCompleteView(syncPoint);
  12277. if (queryId === 'default') {
  12278. try {
  12279. // When you do ref.off(...), we search all views for the registration to remove.
  12280. for (var _b = tslib.__values(syncPoint.views.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
  12281. var _d = tslib.__read(_c.value, 2), viewQueryId = _d[0], view = _d[1];
  12282. cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
  12283. if (viewIsEmpty(view)) {
  12284. syncPoint.views.delete(viewQueryId);
  12285. // We'll deal with complete views later.
  12286. if (!view.query._queryParams.loadsAllData()) {
  12287. removed.push(view.query);
  12288. }
  12289. }
  12290. }
  12291. }
  12292. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  12293. finally {
  12294. try {
  12295. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  12296. }
  12297. finally { if (e_2) throw e_2.error; }
  12298. }
  12299. }
  12300. else {
  12301. // remove the callback from the specific view.
  12302. var view = syncPoint.views.get(queryId);
  12303. if (view) {
  12304. cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
  12305. if (viewIsEmpty(view)) {
  12306. syncPoint.views.delete(queryId);
  12307. // We'll deal with complete views later.
  12308. if (!view.query._queryParams.loadsAllData()) {
  12309. removed.push(view.query);
  12310. }
  12311. }
  12312. }
  12313. }
  12314. if (hadCompleteView && !syncPointHasCompleteView(syncPoint)) {
  12315. // We removed our last complete view.
  12316. removed.push(new (syncPointGetReferenceConstructor())(query._repo, query._path));
  12317. }
  12318. return { removed: removed, events: cancelEvents };
  12319. }
  12320. function syncPointGetQueryViews(syncPoint) {
  12321. var e_3, _a;
  12322. var result = [];
  12323. try {
  12324. for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
  12325. var view = _c.value;
  12326. if (!view.query._queryParams.loadsAllData()) {
  12327. result.push(view);
  12328. }
  12329. }
  12330. }
  12331. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  12332. finally {
  12333. try {
  12334. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  12335. }
  12336. finally { if (e_3) throw e_3.error; }
  12337. }
  12338. return result;
  12339. }
  12340. /**
  12341. * @param path - The path to the desired complete snapshot
  12342. * @returns A complete cache, if it exists
  12343. */
  12344. function syncPointGetCompleteServerCache(syncPoint, path) {
  12345. var e_4, _a;
  12346. var serverCache = null;
  12347. try {
  12348. for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
  12349. var view = _c.value;
  12350. serverCache = serverCache || viewGetCompleteServerCache(view, path);
  12351. }
  12352. }
  12353. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  12354. finally {
  12355. try {
  12356. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  12357. }
  12358. finally { if (e_4) throw e_4.error; }
  12359. }
  12360. return serverCache;
  12361. }
  12362. function syncPointViewForQuery(syncPoint, query) {
  12363. var params = query._queryParams;
  12364. if (params.loadsAllData()) {
  12365. return syncPointGetCompleteView(syncPoint);
  12366. }
  12367. else {
  12368. var queryId = query._queryIdentifier;
  12369. return syncPoint.views.get(queryId);
  12370. }
  12371. }
  12372. function syncPointViewExistsForQuery(syncPoint, query) {
  12373. return syncPointViewForQuery(syncPoint, query) != null;
  12374. }
  12375. function syncPointHasCompleteView(syncPoint) {
  12376. return syncPointGetCompleteView(syncPoint) != null;
  12377. }
  12378. function syncPointGetCompleteView(syncPoint) {
  12379. var e_5, _a;
  12380. try {
  12381. for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
  12382. var view = _c.value;
  12383. if (view.query._queryParams.loadsAllData()) {
  12384. return view;
  12385. }
  12386. }
  12387. }
  12388. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  12389. finally {
  12390. try {
  12391. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  12392. }
  12393. finally { if (e_5) throw e_5.error; }
  12394. }
  12395. return null;
  12396. }
  12397. /**
  12398. * @license
  12399. * Copyright 2017 Google LLC
  12400. *
  12401. * Licensed under the Apache License, Version 2.0 (the "License");
  12402. * you may not use this file except in compliance with the License.
  12403. * You may obtain a copy of the License at
  12404. *
  12405. * http://www.apache.org/licenses/LICENSE-2.0
  12406. *
  12407. * Unless required by applicable law or agreed to in writing, software
  12408. * distributed under the License is distributed on an "AS IS" BASIS,
  12409. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12410. * See the License for the specific language governing permissions and
  12411. * limitations under the License.
  12412. */
  12413. var referenceConstructor;
  12414. function syncTreeSetReferenceConstructor(val) {
  12415. util.assert(!referenceConstructor, '__referenceConstructor has already been defined');
  12416. referenceConstructor = val;
  12417. }
  12418. function syncTreeGetReferenceConstructor() {
  12419. util.assert(referenceConstructor, 'Reference.ts has not been loaded');
  12420. return referenceConstructor;
  12421. }
  12422. /**
  12423. * Static tracker for next query tag.
  12424. */
  12425. var syncTreeNextQueryTag_ = 1;
  12426. /**
  12427. * SyncTree is the central class for managing event callback registration, data caching, views
  12428. * (query processing), and event generation. There are typically two SyncTree instances for
  12429. * each Repo, one for the normal Firebase data, and one for the .info data.
  12430. *
  12431. * It has a number of responsibilities, including:
  12432. * - Tracking all user event callbacks (registered via addEventRegistration() and removeEventRegistration()).
  12433. * - Applying and caching data changes for user set(), transaction(), and update() calls
  12434. * (applyUserOverwrite(), applyUserMerge()).
  12435. * - Applying and caching data changes for server data changes (applyServerOverwrite(),
  12436. * applyServerMerge()).
  12437. * - Generating user-facing events for server and user changes (all of the apply* methods
  12438. * return the set of events that need to be raised as a result).
  12439. * - Maintaining the appropriate set of server listens to ensure we are always subscribed
  12440. * to the correct set of paths and queries to satisfy the current set of user event
  12441. * callbacks (listens are started/stopped using the provided listenProvider).
  12442. *
  12443. * NOTE: Although SyncTree tracks event callbacks and calculates events to raise, the actual
  12444. * events are returned to the caller rather than raised synchronously.
  12445. *
  12446. */
  12447. var SyncTree = /** @class */ (function () {
  12448. /**
  12449. * @param listenProvider_ - Used by SyncTree to start / stop listening
  12450. * to server data.
  12451. */
  12452. function SyncTree(listenProvider_) {
  12453. this.listenProvider_ = listenProvider_;
  12454. /**
  12455. * Tree of SyncPoints. There's a SyncPoint at any location that has 1 or more views.
  12456. */
  12457. this.syncPointTree_ = new ImmutableTree(null);
  12458. /**
  12459. * A tree of all pending user writes (user-initiated set()'s, transaction()'s, update()'s, etc.).
  12460. */
  12461. this.pendingWriteTree_ = newWriteTree();
  12462. this.tagToQueryMap = new Map();
  12463. this.queryToTagMap = new Map();
  12464. }
  12465. return SyncTree;
  12466. }());
  12467. /**
  12468. * Apply the data changes for a user-generated set() or transaction() call.
  12469. *
  12470. * @returns Events to raise.
  12471. */
  12472. function syncTreeApplyUserOverwrite(syncTree, path, newData, writeId, visible) {
  12473. // Record pending write.
  12474. writeTreeAddOverwrite(syncTree.pendingWriteTree_, path, newData, writeId, visible);
  12475. if (!visible) {
  12476. return [];
  12477. }
  12478. else {
  12479. return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceUser(), path, newData));
  12480. }
  12481. }
  12482. /**
  12483. * Apply the data from a user-generated update() call
  12484. *
  12485. * @returns Events to raise.
  12486. */
  12487. function syncTreeApplyUserMerge(syncTree, path, changedChildren, writeId) {
  12488. // Record pending merge.
  12489. writeTreeAddMerge(syncTree.pendingWriteTree_, path, changedChildren, writeId);
  12490. var changeTree = ImmutableTree.fromObject(changedChildren);
  12491. return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceUser(), path, changeTree));
  12492. }
  12493. /**
  12494. * Acknowledge a pending user write that was previously registered with applyUserOverwrite() or applyUserMerge().
  12495. *
  12496. * @param revert - True if the given write failed and needs to be reverted
  12497. * @returns Events to raise.
  12498. */
  12499. function syncTreeAckUserWrite(syncTree, writeId, revert) {
  12500. if (revert === void 0) { revert = false; }
  12501. var write = writeTreeGetWrite(syncTree.pendingWriteTree_, writeId);
  12502. var needToReevaluate = writeTreeRemoveWrite(syncTree.pendingWriteTree_, writeId);
  12503. if (!needToReevaluate) {
  12504. return [];
  12505. }
  12506. else {
  12507. var affectedTree_1 = new ImmutableTree(null);
  12508. if (write.snap != null) {
  12509. // overwrite
  12510. affectedTree_1 = affectedTree_1.set(newEmptyPath(), true);
  12511. }
  12512. else {
  12513. each(write.children, function (pathString) {
  12514. affectedTree_1 = affectedTree_1.set(new Path(pathString), true);
  12515. });
  12516. }
  12517. return syncTreeApplyOperationToSyncPoints_(syncTree, new AckUserWrite(write.path, affectedTree_1, revert));
  12518. }
  12519. }
  12520. /**
  12521. * Apply new server data for the specified path..
  12522. *
  12523. * @returns Events to raise.
  12524. */
  12525. function syncTreeApplyServerOverwrite(syncTree, path, newData) {
  12526. return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceServer(), path, newData));
  12527. }
  12528. /**
  12529. * Apply new server data to be merged in at the specified path.
  12530. *
  12531. * @returns Events to raise.
  12532. */
  12533. function syncTreeApplyServerMerge(syncTree, path, changedChildren) {
  12534. var changeTree = ImmutableTree.fromObject(changedChildren);
  12535. return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceServer(), path, changeTree));
  12536. }
  12537. /**
  12538. * Apply a listen complete for a query
  12539. *
  12540. * @returns Events to raise.
  12541. */
  12542. function syncTreeApplyListenComplete(syncTree, path) {
  12543. return syncTreeApplyOperationToSyncPoints_(syncTree, new ListenComplete(newOperationSourceServer(), path));
  12544. }
  12545. /**
  12546. * Apply a listen complete for a tagged query
  12547. *
  12548. * @returns Events to raise.
  12549. */
  12550. function syncTreeApplyTaggedListenComplete(syncTree, path, tag) {
  12551. var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
  12552. if (queryKey) {
  12553. var r = syncTreeParseQueryKey_(queryKey);
  12554. var queryPath = r.path, queryId = r.queryId;
  12555. var relativePath = newRelativePath(queryPath, path);
  12556. var op = new ListenComplete(newOperationSourceServerTaggedQuery(queryId), relativePath);
  12557. return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
  12558. }
  12559. else {
  12560. // We've already removed the query. No big deal, ignore the update
  12561. return [];
  12562. }
  12563. }
  12564. /**
  12565. * Remove event callback(s).
  12566. *
  12567. * If query is the default query, we'll check all queries for the specified eventRegistration.
  12568. * If eventRegistration is null, we'll remove all callbacks for the specified query/queries.
  12569. *
  12570. * @param eventRegistration - If null, all callbacks are removed.
  12571. * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
  12572. * @param skipListenerDedup - When performing a `get()`, we don't add any new listeners, so no
  12573. * deduping needs to take place. This flag allows toggling of that behavior
  12574. * @returns Cancel events, if cancelError was provided.
  12575. */
  12576. function syncTreeRemoveEventRegistration(syncTree, query, eventRegistration, cancelError, skipListenerDedup) {
  12577. if (skipListenerDedup === void 0) { skipListenerDedup = false; }
  12578. // Find the syncPoint first. Then deal with whether or not it has matching listeners
  12579. var path = query._path;
  12580. var maybeSyncPoint = syncTree.syncPointTree_.get(path);
  12581. var cancelEvents = [];
  12582. // A removal on a default query affects all queries at that location. A removal on an indexed query, even one without
  12583. // other query constraints, does *not* affect all queries at that location. So this check must be for 'default', and
  12584. // not loadsAllData().
  12585. if (maybeSyncPoint &&
  12586. (query._queryIdentifier === 'default' ||
  12587. syncPointViewExistsForQuery(maybeSyncPoint, query))) {
  12588. var removedAndEvents = syncPointRemoveEventRegistration(maybeSyncPoint, query, eventRegistration, cancelError);
  12589. if (syncPointIsEmpty(maybeSyncPoint)) {
  12590. syncTree.syncPointTree_ = syncTree.syncPointTree_.remove(path);
  12591. }
  12592. var removed = removedAndEvents.removed;
  12593. cancelEvents = removedAndEvents.events;
  12594. if (!skipListenerDedup) {
  12595. /**
  12596. * We may have just removed one of many listeners and can short-circuit this whole process
  12597. * We may also not have removed a default listener, in which case all of the descendant listeners should already be
  12598. * properly set up.
  12599. */
  12600. // Since indexed queries can shadow if they don't have other query constraints, check for loadsAllData(), instead of
  12601. // queryId === 'default'
  12602. var removingDefault = -1 !==
  12603. removed.findIndex(function (query) {
  12604. return query._queryParams.loadsAllData();
  12605. });
  12606. var covered = syncTree.syncPointTree_.findOnPath(path, function (relativePath, parentSyncPoint) {
  12607. return syncPointHasCompleteView(parentSyncPoint);
  12608. });
  12609. if (removingDefault && !covered) {
  12610. var subtree = syncTree.syncPointTree_.subtree(path);
  12611. // There are potentially child listeners. Determine what if any listens we need to send before executing the
  12612. // removal
  12613. if (!subtree.isEmpty()) {
  12614. // We need to fold over our subtree and collect the listeners to send
  12615. var newViews = syncTreeCollectDistinctViewsForSubTree_(subtree);
  12616. // Ok, we've collected all the listens we need. Set them up.
  12617. for (var i = 0; i < newViews.length; ++i) {
  12618. var view = newViews[i], newQuery = view.query;
  12619. var listener = syncTreeCreateListenerForView_(syncTree, view);
  12620. syncTree.listenProvider_.startListening(syncTreeQueryForListening_(newQuery), syncTreeTagForQuery(syncTree, newQuery), listener.hashFn, listener.onComplete);
  12621. }
  12622. }
  12623. // Otherwise there's nothing below us, so nothing we need to start listening on
  12624. }
  12625. // If we removed anything and we're not covered by a higher up listen, we need to stop listening on this query
  12626. // The above block has us covered in terms of making sure we're set up on listens lower in the tree.
  12627. // Also, note that if we have a cancelError, it's already been removed at the provider level.
  12628. if (!covered && removed.length > 0 && !cancelError) {
  12629. // If we removed a default, then we weren't listening on any of the other queries here. Just cancel the one
  12630. // default. Otherwise, we need to iterate through and cancel each individual query
  12631. if (removingDefault) {
  12632. // We don't tag default listeners
  12633. var defaultTag = null;
  12634. syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(query), defaultTag);
  12635. }
  12636. else {
  12637. removed.forEach(function (queryToRemove) {
  12638. var tagToRemove = syncTree.queryToTagMap.get(syncTreeMakeQueryKey_(queryToRemove));
  12639. syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToRemove), tagToRemove);
  12640. });
  12641. }
  12642. }
  12643. }
  12644. // Now, clear all of the tags we're tracking for the removed listens
  12645. syncTreeRemoveTags_(syncTree, removed);
  12646. }
  12647. return cancelEvents;
  12648. }
  12649. /**
  12650. * Apply new server data for the specified tagged query.
  12651. *
  12652. * @returns Events to raise.
  12653. */
  12654. function syncTreeApplyTaggedQueryOverwrite(syncTree, path, snap, tag) {
  12655. var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
  12656. if (queryKey != null) {
  12657. var r = syncTreeParseQueryKey_(queryKey);
  12658. var queryPath = r.path, queryId = r.queryId;
  12659. var relativePath = newRelativePath(queryPath, path);
  12660. var op = new Overwrite(newOperationSourceServerTaggedQuery(queryId), relativePath, snap);
  12661. return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
  12662. }
  12663. else {
  12664. // Query must have been removed already
  12665. return [];
  12666. }
  12667. }
  12668. /**
  12669. * Apply server data to be merged in for the specified tagged query.
  12670. *
  12671. * @returns Events to raise.
  12672. */
  12673. function syncTreeApplyTaggedQueryMerge(syncTree, path, changedChildren, tag) {
  12674. var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
  12675. if (queryKey) {
  12676. var r = syncTreeParseQueryKey_(queryKey);
  12677. var queryPath = r.path, queryId = r.queryId;
  12678. var relativePath = newRelativePath(queryPath, path);
  12679. var changeTree = ImmutableTree.fromObject(changedChildren);
  12680. var op = new Merge(newOperationSourceServerTaggedQuery(queryId), relativePath, changeTree);
  12681. return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
  12682. }
  12683. else {
  12684. // We've already removed the query. No big deal, ignore the update
  12685. return [];
  12686. }
  12687. }
  12688. /**
  12689. * Add an event callback for the specified query.
  12690. *
  12691. * @returns Events to raise.
  12692. */
  12693. function syncTreeAddEventRegistration(syncTree, query, eventRegistration, skipSetupListener) {
  12694. if (skipSetupListener === void 0) { skipSetupListener = false; }
  12695. var path = query._path;
  12696. var serverCache = null;
  12697. var foundAncestorDefaultView = false;
  12698. // Any covering writes will necessarily be at the root, so really all we need to find is the server cache.
  12699. // Consider optimizing this once there's a better understanding of what actual behavior will be.
  12700. syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
  12701. var relativePath = newRelativePath(pathToSyncPoint, path);
  12702. serverCache =
  12703. serverCache || syncPointGetCompleteServerCache(sp, relativePath);
  12704. foundAncestorDefaultView =
  12705. foundAncestorDefaultView || syncPointHasCompleteView(sp);
  12706. });
  12707. var syncPoint = syncTree.syncPointTree_.get(path);
  12708. if (!syncPoint) {
  12709. syncPoint = new SyncPoint();
  12710. syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
  12711. }
  12712. else {
  12713. foundAncestorDefaultView =
  12714. foundAncestorDefaultView || syncPointHasCompleteView(syncPoint);
  12715. serverCache =
  12716. serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
  12717. }
  12718. var serverCacheComplete;
  12719. if (serverCache != null) {
  12720. serverCacheComplete = true;
  12721. }
  12722. else {
  12723. serverCacheComplete = false;
  12724. serverCache = ChildrenNode.EMPTY_NODE;
  12725. var subtree = syncTree.syncPointTree_.subtree(path);
  12726. subtree.foreachChild(function (childName, childSyncPoint) {
  12727. var completeCache = syncPointGetCompleteServerCache(childSyncPoint, newEmptyPath());
  12728. if (completeCache) {
  12729. serverCache = serverCache.updateImmediateChild(childName, completeCache);
  12730. }
  12731. });
  12732. }
  12733. var viewAlreadyExists = syncPointViewExistsForQuery(syncPoint, query);
  12734. if (!viewAlreadyExists && !query._queryParams.loadsAllData()) {
  12735. // We need to track a tag for this query
  12736. var queryKey = syncTreeMakeQueryKey_(query);
  12737. util.assert(!syncTree.queryToTagMap.has(queryKey), 'View does not exist, but we have a tag');
  12738. var tag = syncTreeGetNextQueryTag_();
  12739. syncTree.queryToTagMap.set(queryKey, tag);
  12740. syncTree.tagToQueryMap.set(tag, queryKey);
  12741. }
  12742. var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
  12743. var events = syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete);
  12744. if (!viewAlreadyExists && !foundAncestorDefaultView && !skipSetupListener) {
  12745. var view = syncPointViewForQuery(syncPoint, query);
  12746. events = events.concat(syncTreeSetupListener_(syncTree, query, view));
  12747. }
  12748. return events;
  12749. }
  12750. /**
  12751. * Returns a complete cache, if we have one, of the data at a particular path. If the location does not have a
  12752. * listener above it, we will get a false "null". This shouldn't be a problem because transactions will always
  12753. * have a listener above, and atomic operations would correctly show a jitter of <increment value> ->
  12754. * <incremented total> as the write is applied locally and then acknowledged at the server.
  12755. *
  12756. * Note: this method will *include* hidden writes from transaction with applyLocally set to false.
  12757. *
  12758. * @param path - The path to the data we want
  12759. * @param writeIdsToExclude - A specific set to be excluded
  12760. */
  12761. function syncTreeCalcCompleteEventCache(syncTree, path, writeIdsToExclude) {
  12762. var includeHiddenSets = true;
  12763. var writeTree = syncTree.pendingWriteTree_;
  12764. var serverCache = syncTree.syncPointTree_.findOnPath(path, function (pathSoFar, syncPoint) {
  12765. var relativePath = newRelativePath(pathSoFar, path);
  12766. var serverCache = syncPointGetCompleteServerCache(syncPoint, relativePath);
  12767. if (serverCache) {
  12768. return serverCache;
  12769. }
  12770. });
  12771. return writeTreeCalcCompleteEventCache(writeTree, path, serverCache, writeIdsToExclude, includeHiddenSets);
  12772. }
  12773. function syncTreeGetServerValue(syncTree, query) {
  12774. var path = query._path;
  12775. var serverCache = null;
  12776. // Any covering writes will necessarily be at the root, so really all we need to find is the server cache.
  12777. // Consider optimizing this once there's a better understanding of what actual behavior will be.
  12778. syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
  12779. var relativePath = newRelativePath(pathToSyncPoint, path);
  12780. serverCache =
  12781. serverCache || syncPointGetCompleteServerCache(sp, relativePath);
  12782. });
  12783. var syncPoint = syncTree.syncPointTree_.get(path);
  12784. if (!syncPoint) {
  12785. syncPoint = new SyncPoint();
  12786. syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
  12787. }
  12788. else {
  12789. serverCache =
  12790. serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
  12791. }
  12792. var serverCacheComplete = serverCache != null;
  12793. var serverCacheNode = serverCacheComplete
  12794. ? new CacheNode(serverCache, true, false)
  12795. : null;
  12796. var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, query._path);
  12797. var view = syncPointGetView(syncPoint, query, writesCache, serverCacheComplete ? serverCacheNode.getNode() : ChildrenNode.EMPTY_NODE, serverCacheComplete);
  12798. return viewGetCompleteNode(view);
  12799. }
  12800. /**
  12801. * A helper method that visits all descendant and ancestor SyncPoints, applying the operation.
  12802. *
  12803. * NOTES:
  12804. * - Descendant SyncPoints will be visited first (since we raise events depth-first).
  12805. *
  12806. * - We call applyOperation() on each SyncPoint passing three things:
  12807. * 1. A version of the Operation that has been made relative to the SyncPoint location.
  12808. * 2. A WriteTreeRef of any writes we have cached at the SyncPoint location.
  12809. * 3. A snapshot Node with cached server data, if we have it.
  12810. *
  12811. * - We concatenate all of the events returned by each SyncPoint and return the result.
  12812. */
  12813. function syncTreeApplyOperationToSyncPoints_(syncTree, operation) {
  12814. return syncTreeApplyOperationHelper_(operation, syncTree.syncPointTree_,
  12815. /*serverCache=*/ null, writeTreeChildWrites(syncTree.pendingWriteTree_, newEmptyPath()));
  12816. }
  12817. /**
  12818. * Recursive helper for applyOperationToSyncPoints_
  12819. */
  12820. function syncTreeApplyOperationHelper_(operation, syncPointTree, serverCache, writesCache) {
  12821. if (pathIsEmpty(operation.path)) {
  12822. return syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache);
  12823. }
  12824. else {
  12825. var syncPoint = syncPointTree.get(newEmptyPath());
  12826. // If we don't have cached server data, see if we can get it from this SyncPoint.
  12827. if (serverCache == null && syncPoint != null) {
  12828. serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
  12829. }
  12830. var events = [];
  12831. var childName = pathGetFront(operation.path);
  12832. var childOperation = operation.operationForChild(childName);
  12833. var childTree = syncPointTree.children.get(childName);
  12834. if (childTree && childOperation) {
  12835. var childServerCache = serverCache
  12836. ? serverCache.getImmediateChild(childName)
  12837. : null;
  12838. var childWritesCache = writeTreeRefChild(writesCache, childName);
  12839. events = events.concat(syncTreeApplyOperationHelper_(childOperation, childTree, childServerCache, childWritesCache));
  12840. }
  12841. if (syncPoint) {
  12842. events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
  12843. }
  12844. return events;
  12845. }
  12846. }
  12847. /**
  12848. * Recursive helper for applyOperationToSyncPoints_
  12849. */
  12850. function syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache) {
  12851. var syncPoint = syncPointTree.get(newEmptyPath());
  12852. // If we don't have cached server data, see if we can get it from this SyncPoint.
  12853. if (serverCache == null && syncPoint != null) {
  12854. serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
  12855. }
  12856. var events = [];
  12857. syncPointTree.children.inorderTraversal(function (childName, childTree) {
  12858. var childServerCache = serverCache
  12859. ? serverCache.getImmediateChild(childName)
  12860. : null;
  12861. var childWritesCache = writeTreeRefChild(writesCache, childName);
  12862. var childOperation = operation.operationForChild(childName);
  12863. if (childOperation) {
  12864. events = events.concat(syncTreeApplyOperationDescendantsHelper_(childOperation, childTree, childServerCache, childWritesCache));
  12865. }
  12866. });
  12867. if (syncPoint) {
  12868. events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
  12869. }
  12870. return events;
  12871. }
  12872. function syncTreeCreateListenerForView_(syncTree, view) {
  12873. var query = view.query;
  12874. var tag = syncTreeTagForQuery(syncTree, query);
  12875. return {
  12876. hashFn: function () {
  12877. var cache = viewGetServerCache(view) || ChildrenNode.EMPTY_NODE;
  12878. return cache.hash();
  12879. },
  12880. onComplete: function (status) {
  12881. if (status === 'ok') {
  12882. if (tag) {
  12883. return syncTreeApplyTaggedListenComplete(syncTree, query._path, tag);
  12884. }
  12885. else {
  12886. return syncTreeApplyListenComplete(syncTree, query._path);
  12887. }
  12888. }
  12889. else {
  12890. // If a listen failed, kill all of the listeners here, not just the one that triggered the error.
  12891. // Note that this may need to be scoped to just this listener if we change permissions on filtered children
  12892. var error = errorForServerCode(status, query);
  12893. return syncTreeRemoveEventRegistration(syncTree, query,
  12894. /*eventRegistration*/ null, error);
  12895. }
  12896. }
  12897. };
  12898. }
  12899. /**
  12900. * Return the tag associated with the given query.
  12901. */
  12902. function syncTreeTagForQuery(syncTree, query) {
  12903. var queryKey = syncTreeMakeQueryKey_(query);
  12904. return syncTree.queryToTagMap.get(queryKey);
  12905. }
  12906. /**
  12907. * Given a query, computes a "queryKey" suitable for use in our queryToTagMap_.
  12908. */
  12909. function syncTreeMakeQueryKey_(query) {
  12910. return query._path.toString() + '$' + query._queryIdentifier;
  12911. }
  12912. /**
  12913. * Return the query associated with the given tag, if we have one
  12914. */
  12915. function syncTreeQueryKeyForTag_(syncTree, tag) {
  12916. return syncTree.tagToQueryMap.get(tag);
  12917. }
  12918. /**
  12919. * Given a queryKey (created by makeQueryKey), parse it back into a path and queryId.
  12920. */
  12921. function syncTreeParseQueryKey_(queryKey) {
  12922. var splitIndex = queryKey.indexOf('$');
  12923. util.assert(splitIndex !== -1 && splitIndex < queryKey.length - 1, 'Bad queryKey.');
  12924. return {
  12925. queryId: queryKey.substr(splitIndex + 1),
  12926. path: new Path(queryKey.substr(0, splitIndex))
  12927. };
  12928. }
  12929. /**
  12930. * A helper method to apply tagged operations
  12931. */
  12932. function syncTreeApplyTaggedOperation_(syncTree, queryPath, operation) {
  12933. var syncPoint = syncTree.syncPointTree_.get(queryPath);
  12934. util.assert(syncPoint, "Missing sync point for query tag that we're tracking");
  12935. var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, queryPath);
  12936. return syncPointApplyOperation(syncPoint, operation, writesCache, null);
  12937. }
  12938. /**
  12939. * This collapses multiple unfiltered views into a single view, since we only need a single
  12940. * listener for them.
  12941. */
  12942. function syncTreeCollectDistinctViewsForSubTree_(subtree) {
  12943. return subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
  12944. if (maybeChildSyncPoint && syncPointHasCompleteView(maybeChildSyncPoint)) {
  12945. var completeView = syncPointGetCompleteView(maybeChildSyncPoint);
  12946. return [completeView];
  12947. }
  12948. else {
  12949. // No complete view here, flatten any deeper listens into an array
  12950. var views_1 = [];
  12951. if (maybeChildSyncPoint) {
  12952. views_1 = syncPointGetQueryViews(maybeChildSyncPoint);
  12953. }
  12954. each(childMap, function (_key, childViews) {
  12955. views_1 = views_1.concat(childViews);
  12956. });
  12957. return views_1;
  12958. }
  12959. });
  12960. }
  12961. /**
  12962. * Normalizes a query to a query we send the server for listening
  12963. *
  12964. * @returns The normalized query
  12965. */
  12966. function syncTreeQueryForListening_(query) {
  12967. if (query._queryParams.loadsAllData() && !query._queryParams.isDefault()) {
  12968. // We treat queries that load all data as default queries
  12969. // Cast is necessary because ref() technically returns Firebase which is actually fb.api.Firebase which inherits
  12970. // from Query
  12971. return new (syncTreeGetReferenceConstructor())(query._repo, query._path);
  12972. }
  12973. else {
  12974. return query;
  12975. }
  12976. }
  12977. function syncTreeRemoveTags_(syncTree, queries) {
  12978. for (var j = 0; j < queries.length; ++j) {
  12979. var removedQuery = queries[j];
  12980. if (!removedQuery._queryParams.loadsAllData()) {
  12981. // We should have a tag for this
  12982. var removedQueryKey = syncTreeMakeQueryKey_(removedQuery);
  12983. var removedQueryTag = syncTree.queryToTagMap.get(removedQueryKey);
  12984. syncTree.queryToTagMap.delete(removedQueryKey);
  12985. syncTree.tagToQueryMap.delete(removedQueryTag);
  12986. }
  12987. }
  12988. }
  12989. /**
  12990. * Static accessor for query tags.
  12991. */
  12992. function syncTreeGetNextQueryTag_() {
  12993. return syncTreeNextQueryTag_++;
  12994. }
  12995. /**
  12996. * For a given new listen, manage the de-duplication of outstanding subscriptions.
  12997. *
  12998. * @returns This method can return events to support synchronous data sources
  12999. */
  13000. function syncTreeSetupListener_(syncTree, query, view) {
  13001. var path = query._path;
  13002. var tag = syncTreeTagForQuery(syncTree, query);
  13003. var listener = syncTreeCreateListenerForView_(syncTree, view);
  13004. var events = syncTree.listenProvider_.startListening(syncTreeQueryForListening_(query), tag, listener.hashFn, listener.onComplete);
  13005. var subtree = syncTree.syncPointTree_.subtree(path);
  13006. // The root of this subtree has our query. We're here because we definitely need to send a listen for that, but we
  13007. // may need to shadow other listens as well.
  13008. if (tag) {
  13009. util.assert(!syncPointHasCompleteView(subtree.value), "If we're adding a query, it shouldn't be shadowed");
  13010. }
  13011. else {
  13012. // Shadow everything at or below this location, this is a default listener.
  13013. var queriesToStop = subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
  13014. if (!pathIsEmpty(relativePath) &&
  13015. maybeChildSyncPoint &&
  13016. syncPointHasCompleteView(maybeChildSyncPoint)) {
  13017. return [syncPointGetCompleteView(maybeChildSyncPoint).query];
  13018. }
  13019. else {
  13020. // No default listener here, flatten any deeper queries into an array
  13021. var queries_1 = [];
  13022. if (maybeChildSyncPoint) {
  13023. queries_1 = queries_1.concat(syncPointGetQueryViews(maybeChildSyncPoint).map(function (view) { return view.query; }));
  13024. }
  13025. each(childMap, function (_key, childQueries) {
  13026. queries_1 = queries_1.concat(childQueries);
  13027. });
  13028. return queries_1;
  13029. }
  13030. });
  13031. for (var i = 0; i < queriesToStop.length; ++i) {
  13032. var queryToStop = queriesToStop[i];
  13033. syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToStop), syncTreeTagForQuery(syncTree, queryToStop));
  13034. }
  13035. }
  13036. return events;
  13037. }
  13038. /**
  13039. * @license
  13040. * Copyright 2017 Google LLC
  13041. *
  13042. * Licensed under the Apache License, Version 2.0 (the "License");
  13043. * you may not use this file except in compliance with the License.
  13044. * You may obtain a copy of the License at
  13045. *
  13046. * http://www.apache.org/licenses/LICENSE-2.0
  13047. *
  13048. * Unless required by applicable law or agreed to in writing, software
  13049. * distributed under the License is distributed on an "AS IS" BASIS,
  13050. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13051. * See the License for the specific language governing permissions and
  13052. * limitations under the License.
  13053. */
  13054. var ExistingValueProvider = /** @class */ (function () {
  13055. function ExistingValueProvider(node_) {
  13056. this.node_ = node_;
  13057. }
  13058. ExistingValueProvider.prototype.getImmediateChild = function (childName) {
  13059. var child = this.node_.getImmediateChild(childName);
  13060. return new ExistingValueProvider(child);
  13061. };
  13062. ExistingValueProvider.prototype.node = function () {
  13063. return this.node_;
  13064. };
  13065. return ExistingValueProvider;
  13066. }());
  13067. var DeferredValueProvider = /** @class */ (function () {
  13068. function DeferredValueProvider(syncTree, path) {
  13069. this.syncTree_ = syncTree;
  13070. this.path_ = path;
  13071. }
  13072. DeferredValueProvider.prototype.getImmediateChild = function (childName) {
  13073. var childPath = pathChild(this.path_, childName);
  13074. return new DeferredValueProvider(this.syncTree_, childPath);
  13075. };
  13076. DeferredValueProvider.prototype.node = function () {
  13077. return syncTreeCalcCompleteEventCache(this.syncTree_, this.path_);
  13078. };
  13079. return DeferredValueProvider;
  13080. }());
  13081. /**
  13082. * Generate placeholders for deferred values.
  13083. */
  13084. var generateWithValues = function (values) {
  13085. values = values || {};
  13086. values['timestamp'] = values['timestamp'] || new Date().getTime();
  13087. return values;
  13088. };
  13089. /**
  13090. * Value to use when firing local events. When writing server values, fire
  13091. * local events with an approximate value, otherwise return value as-is.
  13092. */
  13093. var resolveDeferredLeafValue = function (value, existingVal, serverValues) {
  13094. if (!value || typeof value !== 'object') {
  13095. return value;
  13096. }
  13097. util.assert('.sv' in value, 'Unexpected leaf node or priority contents');
  13098. if (typeof value['.sv'] === 'string') {
  13099. return resolveScalarDeferredValue(value['.sv'], existingVal, serverValues);
  13100. }
  13101. else if (typeof value['.sv'] === 'object') {
  13102. return resolveComplexDeferredValue(value['.sv'], existingVal);
  13103. }
  13104. else {
  13105. util.assert(false, 'Unexpected server value: ' + JSON.stringify(value, null, 2));
  13106. }
  13107. };
  13108. var resolveScalarDeferredValue = function (op, existing, serverValues) {
  13109. switch (op) {
  13110. case 'timestamp':
  13111. return serverValues['timestamp'];
  13112. default:
  13113. util.assert(false, 'Unexpected server value: ' + op);
  13114. }
  13115. };
  13116. var resolveComplexDeferredValue = function (op, existing, unused) {
  13117. if (!op.hasOwnProperty('increment')) {
  13118. util.assert(false, 'Unexpected server value: ' + JSON.stringify(op, null, 2));
  13119. }
  13120. var delta = op['increment'];
  13121. if (typeof delta !== 'number') {
  13122. util.assert(false, 'Unexpected increment value: ' + delta);
  13123. }
  13124. var existingNode = existing.node();
  13125. util.assert(existingNode !== null && typeof existingNode !== 'undefined', 'Expected ChildrenNode.EMPTY_NODE for nulls');
  13126. // Incrementing a non-number sets the value to the incremented amount
  13127. if (!existingNode.isLeafNode()) {
  13128. return delta;
  13129. }
  13130. var leaf = existingNode;
  13131. var existingVal = leaf.getValue();
  13132. if (typeof existingVal !== 'number') {
  13133. return delta;
  13134. }
  13135. // No need to do over/underflow arithmetic here because JS only handles floats under the covers
  13136. return existingVal + delta;
  13137. };
  13138. /**
  13139. * Recursively replace all deferred values and priorities in the tree with the
  13140. * specified generated replacement values.
  13141. * @param path - path to which write is relative
  13142. * @param node - new data written at path
  13143. * @param syncTree - current data
  13144. */
  13145. var resolveDeferredValueTree = function (path, node, syncTree, serverValues) {
  13146. return resolveDeferredValue(node, new DeferredValueProvider(syncTree, path), serverValues);
  13147. };
  13148. /**
  13149. * Recursively replace all deferred values and priorities in the node with the
  13150. * specified generated replacement values. If there are no server values in the node,
  13151. * it'll be returned as-is.
  13152. */
  13153. var resolveDeferredValueSnapshot = function (node, existing, serverValues) {
  13154. return resolveDeferredValue(node, new ExistingValueProvider(existing), serverValues);
  13155. };
  13156. function resolveDeferredValue(node, existingVal, serverValues) {
  13157. var rawPri = node.getPriority().val();
  13158. var priority = resolveDeferredLeafValue(rawPri, existingVal.getImmediateChild('.priority'), serverValues);
  13159. var newNode;
  13160. if (node.isLeafNode()) {
  13161. var leafNode = node;
  13162. var value = resolveDeferredLeafValue(leafNode.getValue(), existingVal, serverValues);
  13163. if (value !== leafNode.getValue() ||
  13164. priority !== leafNode.getPriority().val()) {
  13165. return new LeafNode(value, nodeFromJSON(priority));
  13166. }
  13167. else {
  13168. return node;
  13169. }
  13170. }
  13171. else {
  13172. var childrenNode = node;
  13173. newNode = childrenNode;
  13174. if (priority !== childrenNode.getPriority().val()) {
  13175. newNode = newNode.updatePriority(new LeafNode(priority));
  13176. }
  13177. childrenNode.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
  13178. var newChildNode = resolveDeferredValue(childNode, existingVal.getImmediateChild(childName), serverValues);
  13179. if (newChildNode !== childNode) {
  13180. newNode = newNode.updateImmediateChild(childName, newChildNode);
  13181. }
  13182. });
  13183. return newNode;
  13184. }
  13185. }
  13186. /**
  13187. * @license
  13188. * Copyright 2017 Google LLC
  13189. *
  13190. * Licensed under the Apache License, Version 2.0 (the "License");
  13191. * you may not use this file except in compliance with the License.
  13192. * You may obtain a copy of the License at
  13193. *
  13194. * http://www.apache.org/licenses/LICENSE-2.0
  13195. *
  13196. * Unless required by applicable law or agreed to in writing, software
  13197. * distributed under the License is distributed on an "AS IS" BASIS,
  13198. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13199. * See the License for the specific language governing permissions and
  13200. * limitations under the License.
  13201. */
  13202. /**
  13203. * A light-weight tree, traversable by path. Nodes can have both values and children.
  13204. * Nodes are not enumerated (by forEachChild) unless they have a value or non-empty
  13205. * children.
  13206. */
  13207. var Tree = /** @class */ (function () {
  13208. /**
  13209. * @param name - Optional name of the node.
  13210. * @param parent - Optional parent node.
  13211. * @param node - Optional node to wrap.
  13212. */
  13213. function Tree(name, parent, node) {
  13214. if (name === void 0) { name = ''; }
  13215. if (parent === void 0) { parent = null; }
  13216. if (node === void 0) { node = { children: {}, childCount: 0 }; }
  13217. this.name = name;
  13218. this.parent = parent;
  13219. this.node = node;
  13220. }
  13221. return Tree;
  13222. }());
  13223. /**
  13224. * Returns a sub-Tree for the given path.
  13225. *
  13226. * @param pathObj - Path to look up.
  13227. * @returns Tree for path.
  13228. */
  13229. function treeSubTree(tree, pathObj) {
  13230. // TODO: Require pathObj to be Path?
  13231. var path = pathObj instanceof Path ? pathObj : new Path(pathObj);
  13232. var child = tree, next = pathGetFront(path);
  13233. while (next !== null) {
  13234. var childNode = util.safeGet(child.node.children, next) || {
  13235. children: {},
  13236. childCount: 0
  13237. };
  13238. child = new Tree(next, child, childNode);
  13239. path = pathPopFront(path);
  13240. next = pathGetFront(path);
  13241. }
  13242. return child;
  13243. }
  13244. /**
  13245. * Returns the data associated with this tree node.
  13246. *
  13247. * @returns The data or null if no data exists.
  13248. */
  13249. function treeGetValue(tree) {
  13250. return tree.node.value;
  13251. }
  13252. /**
  13253. * Sets data to this tree node.
  13254. *
  13255. * @param value - Value to set.
  13256. */
  13257. function treeSetValue(tree, value) {
  13258. tree.node.value = value;
  13259. treeUpdateParents(tree);
  13260. }
  13261. /**
  13262. * @returns Whether the tree has any children.
  13263. */
  13264. function treeHasChildren(tree) {
  13265. return tree.node.childCount > 0;
  13266. }
  13267. /**
  13268. * @returns Whethe rthe tree is empty (no value or children).
  13269. */
  13270. function treeIsEmpty(tree) {
  13271. return treeGetValue(tree) === undefined && !treeHasChildren(tree);
  13272. }
  13273. /**
  13274. * Calls action for each child of this tree node.
  13275. *
  13276. * @param action - Action to be called for each child.
  13277. */
  13278. function treeForEachChild(tree, action) {
  13279. each(tree.node.children, function (child, childTree) {
  13280. action(new Tree(child, tree, childTree));
  13281. });
  13282. }
  13283. /**
  13284. * Does a depth-first traversal of this node's descendants, calling action for each one.
  13285. *
  13286. * @param action - Action to be called for each child.
  13287. * @param includeSelf - Whether to call action on this node as well. Defaults to
  13288. * false.
  13289. * @param childrenFirst - Whether to call action on children before calling it on
  13290. * parent.
  13291. */
  13292. function treeForEachDescendant(tree, action, includeSelf, childrenFirst) {
  13293. if (includeSelf && !childrenFirst) {
  13294. action(tree);
  13295. }
  13296. treeForEachChild(tree, function (child) {
  13297. treeForEachDescendant(child, action, true, childrenFirst);
  13298. });
  13299. if (includeSelf && childrenFirst) {
  13300. action(tree);
  13301. }
  13302. }
  13303. /**
  13304. * Calls action on each ancestor node.
  13305. *
  13306. * @param action - Action to be called on each parent; return
  13307. * true to abort.
  13308. * @param includeSelf - Whether to call action on this node as well.
  13309. * @returns true if the action callback returned true.
  13310. */
  13311. function treeForEachAncestor(tree, action, includeSelf) {
  13312. var node = includeSelf ? tree : tree.parent;
  13313. while (node !== null) {
  13314. if (action(node)) {
  13315. return true;
  13316. }
  13317. node = node.parent;
  13318. }
  13319. return false;
  13320. }
  13321. /**
  13322. * @returns The path of this tree node, as a Path.
  13323. */
  13324. function treeGetPath(tree) {
  13325. return new Path(tree.parent === null
  13326. ? tree.name
  13327. : treeGetPath(tree.parent) + '/' + tree.name);
  13328. }
  13329. /**
  13330. * Adds or removes this child from its parent based on whether it's empty or not.
  13331. */
  13332. function treeUpdateParents(tree) {
  13333. if (tree.parent !== null) {
  13334. treeUpdateChild(tree.parent, tree.name, tree);
  13335. }
  13336. }
  13337. /**
  13338. * Adds or removes the passed child to this tree node, depending on whether it's empty.
  13339. *
  13340. * @param childName - The name of the child to update.
  13341. * @param child - The child to update.
  13342. */
  13343. function treeUpdateChild(tree, childName, child) {
  13344. var childEmpty = treeIsEmpty(child);
  13345. var childExists = util.contains(tree.node.children, childName);
  13346. if (childEmpty && childExists) {
  13347. delete tree.node.children[childName];
  13348. tree.node.childCount--;
  13349. treeUpdateParents(tree);
  13350. }
  13351. else if (!childEmpty && !childExists) {
  13352. tree.node.children[childName] = child.node;
  13353. tree.node.childCount++;
  13354. treeUpdateParents(tree);
  13355. }
  13356. }
  13357. /**
  13358. * @license
  13359. * Copyright 2017 Google LLC
  13360. *
  13361. * Licensed under the Apache License, Version 2.0 (the "License");
  13362. * you may not use this file except in compliance with the License.
  13363. * You may obtain a copy of the License at
  13364. *
  13365. * http://www.apache.org/licenses/LICENSE-2.0
  13366. *
  13367. * Unless required by applicable law or agreed to in writing, software
  13368. * distributed under the License is distributed on an "AS IS" BASIS,
  13369. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13370. * See the License for the specific language governing permissions and
  13371. * limitations under the License.
  13372. */
  13373. /**
  13374. * True for invalid Firebase keys
  13375. */
  13376. var INVALID_KEY_REGEX_ = /[\[\].#$\/\u0000-\u001F\u007F]/;
  13377. /**
  13378. * True for invalid Firebase paths.
  13379. * Allows '/' in paths.
  13380. */
  13381. var INVALID_PATH_REGEX_ = /[\[\].#$\u0000-\u001F\u007F]/;
  13382. /**
  13383. * Maximum number of characters to allow in leaf value
  13384. */
  13385. var MAX_LEAF_SIZE_ = 10 * 1024 * 1024;
  13386. var isValidKey = function (key) {
  13387. return (typeof key === 'string' && key.length !== 0 && !INVALID_KEY_REGEX_.test(key));
  13388. };
  13389. var isValidPathString = function (pathString) {
  13390. return (typeof pathString === 'string' &&
  13391. pathString.length !== 0 &&
  13392. !INVALID_PATH_REGEX_.test(pathString));
  13393. };
  13394. var isValidRootPathString = function (pathString) {
  13395. if (pathString) {
  13396. // Allow '/.info/' at the beginning.
  13397. pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
  13398. }
  13399. return isValidPathString(pathString);
  13400. };
  13401. var isValidPriority = function (priority) {
  13402. return (priority === null ||
  13403. typeof priority === 'string' ||
  13404. (typeof priority === 'number' && !isInvalidJSONNumber(priority)) ||
  13405. (priority &&
  13406. typeof priority === 'object' &&
  13407. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  13408. util.contains(priority, '.sv')));
  13409. };
  13410. /**
  13411. * Pre-validate a datum passed as an argument to Firebase function.
  13412. */
  13413. var validateFirebaseDataArg = function (fnName, value, path, optional) {
  13414. if (optional && value === undefined) {
  13415. return;
  13416. }
  13417. validateFirebaseData(util.errorPrefix(fnName, 'value'), value, path);
  13418. };
  13419. /**
  13420. * Validate a data object client-side before sending to server.
  13421. */
  13422. var validateFirebaseData = function (errorPrefix, data, path_) {
  13423. var path = path_ instanceof Path ? new ValidationPath(path_, errorPrefix) : path_;
  13424. if (data === undefined) {
  13425. throw new Error(errorPrefix + 'contains undefined ' + validationPathToErrorString(path));
  13426. }
  13427. if (typeof data === 'function') {
  13428. throw new Error(errorPrefix +
  13429. 'contains a function ' +
  13430. validationPathToErrorString(path) +
  13431. ' with contents = ' +
  13432. data.toString());
  13433. }
  13434. if (isInvalidJSONNumber(data)) {
  13435. throw new Error(errorPrefix +
  13436. 'contains ' +
  13437. data.toString() +
  13438. ' ' +
  13439. validationPathToErrorString(path));
  13440. }
  13441. // Check max leaf size, but try to avoid the utf8 conversion if we can.
  13442. if (typeof data === 'string' &&
  13443. data.length > MAX_LEAF_SIZE_ / 3 &&
  13444. util.stringLength(data) > MAX_LEAF_SIZE_) {
  13445. throw new Error(errorPrefix +
  13446. 'contains a string greater than ' +
  13447. MAX_LEAF_SIZE_ +
  13448. ' utf8 bytes ' +
  13449. validationPathToErrorString(path) +
  13450. " ('" +
  13451. data.substring(0, 50) +
  13452. "...')");
  13453. }
  13454. // TODO = Perf = Consider combining the recursive validation of keys into NodeFromJSON
  13455. // to save extra walking of large objects.
  13456. if (data && typeof data === 'object') {
  13457. var hasDotValue_1 = false;
  13458. var hasActualChild_1 = false;
  13459. each(data, function (key, value) {
  13460. if (key === '.value') {
  13461. hasDotValue_1 = true;
  13462. }
  13463. else if (key !== '.priority' && key !== '.sv') {
  13464. hasActualChild_1 = true;
  13465. if (!isValidKey(key)) {
  13466. throw new Error(errorPrefix +
  13467. ' contains an invalid key (' +
  13468. key +
  13469. ') ' +
  13470. validationPathToErrorString(path) +
  13471. '. Keys must be non-empty strings ' +
  13472. 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
  13473. }
  13474. }
  13475. validationPathPush(path, key);
  13476. validateFirebaseData(errorPrefix, value, path);
  13477. validationPathPop(path);
  13478. });
  13479. if (hasDotValue_1 && hasActualChild_1) {
  13480. throw new Error(errorPrefix +
  13481. ' contains ".value" child ' +
  13482. validationPathToErrorString(path) +
  13483. ' in addition to actual children.');
  13484. }
  13485. }
  13486. };
  13487. /**
  13488. * Pre-validate paths passed in the firebase function.
  13489. */
  13490. var validateFirebaseMergePaths = function (errorPrefix, mergePaths) {
  13491. var i, curPath;
  13492. for (i = 0; i < mergePaths.length; i++) {
  13493. curPath = mergePaths[i];
  13494. var keys = pathSlice(curPath);
  13495. for (var j = 0; j < keys.length; j++) {
  13496. if (keys[j] === '.priority' && j === keys.length - 1) ;
  13497. else if (!isValidKey(keys[j])) {
  13498. throw new Error(errorPrefix +
  13499. 'contains an invalid key (' +
  13500. keys[j] +
  13501. ') in path ' +
  13502. curPath.toString() +
  13503. '. Keys must be non-empty strings ' +
  13504. 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
  13505. }
  13506. }
  13507. }
  13508. // Check that update keys are not descendants of each other.
  13509. // We rely on the property that sorting guarantees that ancestors come
  13510. // right before descendants.
  13511. mergePaths.sort(pathCompare);
  13512. var prevPath = null;
  13513. for (i = 0; i < mergePaths.length; i++) {
  13514. curPath = mergePaths[i];
  13515. if (prevPath !== null && pathContains(prevPath, curPath)) {
  13516. throw new Error(errorPrefix +
  13517. 'contains a path ' +
  13518. prevPath.toString() +
  13519. ' that is ancestor of another path ' +
  13520. curPath.toString());
  13521. }
  13522. prevPath = curPath;
  13523. }
  13524. };
  13525. /**
  13526. * pre-validate an object passed as an argument to firebase function (
  13527. * must be an object - e.g. for firebase.update()).
  13528. */
  13529. var validateFirebaseMergeDataArg = function (fnName, data, path, optional) {
  13530. if (optional && data === undefined) {
  13531. return;
  13532. }
  13533. var errorPrefix = util.errorPrefix(fnName, 'values');
  13534. if (!(data && typeof data === 'object') || Array.isArray(data)) {
  13535. throw new Error(errorPrefix + ' must be an object containing the children to replace.');
  13536. }
  13537. var mergePaths = [];
  13538. each(data, function (key, value) {
  13539. var curPath = new Path(key);
  13540. validateFirebaseData(errorPrefix, value, pathChild(path, curPath));
  13541. if (pathGetBack(curPath) === '.priority') {
  13542. if (!isValidPriority(value)) {
  13543. throw new Error(errorPrefix +
  13544. "contains an invalid value for '" +
  13545. curPath.toString() +
  13546. "', which must be a valid " +
  13547. 'Firebase priority (a string, finite number, server value, or null).');
  13548. }
  13549. }
  13550. mergePaths.push(curPath);
  13551. });
  13552. validateFirebaseMergePaths(errorPrefix, mergePaths);
  13553. };
  13554. var validatePriority = function (fnName, priority, optional) {
  13555. if (optional && priority === undefined) {
  13556. return;
  13557. }
  13558. if (isInvalidJSONNumber(priority)) {
  13559. throw new Error(util.errorPrefix(fnName, 'priority') +
  13560. 'is ' +
  13561. priority.toString() +
  13562. ', but must be a valid Firebase priority (a string, finite number, ' +
  13563. 'server value, or null).');
  13564. }
  13565. // Special case to allow importing data with a .sv.
  13566. if (!isValidPriority(priority)) {
  13567. throw new Error(util.errorPrefix(fnName, 'priority') +
  13568. 'must be a valid Firebase priority ' +
  13569. '(a string, finite number, server value, or null).');
  13570. }
  13571. };
  13572. var validateKey = function (fnName, argumentName, key, optional) {
  13573. if (optional && key === undefined) {
  13574. return;
  13575. }
  13576. if (!isValidKey(key)) {
  13577. throw new Error(util.errorPrefix(fnName, argumentName) +
  13578. 'was an invalid key = "' +
  13579. key +
  13580. '". Firebase keys must be non-empty strings and ' +
  13581. 'can\'t contain ".", "#", "$", "/", "[", or "]").');
  13582. }
  13583. };
  13584. /**
  13585. * @internal
  13586. */
  13587. var validatePathString = function (fnName, argumentName, pathString, optional) {
  13588. if (optional && pathString === undefined) {
  13589. return;
  13590. }
  13591. if (!isValidPathString(pathString)) {
  13592. throw new Error(util.errorPrefix(fnName, argumentName) +
  13593. 'was an invalid path = "' +
  13594. pathString +
  13595. '". Paths must be non-empty strings and ' +
  13596. 'can\'t contain ".", "#", "$", "[", or "]"');
  13597. }
  13598. };
  13599. var validateRootPathString = function (fnName, argumentName, pathString, optional) {
  13600. if (pathString) {
  13601. // Allow '/.info/' at the beginning.
  13602. pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
  13603. }
  13604. validatePathString(fnName, argumentName, pathString, optional);
  13605. };
  13606. /**
  13607. * @internal
  13608. */
  13609. var validateWritablePath = function (fnName, path) {
  13610. if (pathGetFront(path) === '.info') {
  13611. throw new Error(fnName + " failed = Can't modify data under /.info/");
  13612. }
  13613. };
  13614. var validateUrl = function (fnName, parsedUrl) {
  13615. // TODO = Validate server better.
  13616. var pathString = parsedUrl.path.toString();
  13617. if (!(typeof parsedUrl.repoInfo.host === 'string') ||
  13618. parsedUrl.repoInfo.host.length === 0 ||
  13619. (!isValidKey(parsedUrl.repoInfo.namespace) &&
  13620. parsedUrl.repoInfo.host.split(':')[0] !== 'localhost') ||
  13621. (pathString.length !== 0 && !isValidRootPathString(pathString))) {
  13622. throw new Error(util.errorPrefix(fnName, 'url') +
  13623. 'must be a valid firebase URL and ' +
  13624. 'the path can\'t contain ".", "#", "$", "[", or "]".');
  13625. }
  13626. };
  13627. /**
  13628. * @license
  13629. * Copyright 2017 Google LLC
  13630. *
  13631. * Licensed under the Apache License, Version 2.0 (the "License");
  13632. * you may not use this file except in compliance with the License.
  13633. * You may obtain a copy of the License at
  13634. *
  13635. * http://www.apache.org/licenses/LICENSE-2.0
  13636. *
  13637. * Unless required by applicable law or agreed to in writing, software
  13638. * distributed under the License is distributed on an "AS IS" BASIS,
  13639. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13640. * See the License for the specific language governing permissions and
  13641. * limitations under the License.
  13642. */
  13643. /**
  13644. * The event queue serves a few purposes:
  13645. * 1. It ensures we maintain event order in the face of event callbacks doing operations that result in more
  13646. * events being queued.
  13647. * 2. raiseQueuedEvents() handles being called reentrantly nicely. That is, if in the course of raising events,
  13648. * raiseQueuedEvents() is called again, the "inner" call will pick up raising events where the "outer" call
  13649. * left off, ensuring that the events are still raised synchronously and in order.
  13650. * 3. You can use raiseEventsAtPath and raiseEventsForChangedPath to ensure only relevant previously-queued
  13651. * events are raised synchronously.
  13652. *
  13653. * NOTE: This can all go away if/when we move to async events.
  13654. *
  13655. */
  13656. var EventQueue = /** @class */ (function () {
  13657. function EventQueue() {
  13658. this.eventLists_ = [];
  13659. /**
  13660. * Tracks recursion depth of raiseQueuedEvents_, for debugging purposes.
  13661. */
  13662. this.recursionDepth_ = 0;
  13663. }
  13664. return EventQueue;
  13665. }());
  13666. /**
  13667. * @param eventDataList - The new events to queue.
  13668. */
  13669. function eventQueueQueueEvents(eventQueue, eventDataList) {
  13670. // We group events by path, storing them in a single EventList, to make it easier to skip over them quickly.
  13671. var currList = null;
  13672. for (var i = 0; i < eventDataList.length; i++) {
  13673. var data = eventDataList[i];
  13674. var path = data.getPath();
  13675. if (currList !== null && !pathEquals(path, currList.path)) {
  13676. eventQueue.eventLists_.push(currList);
  13677. currList = null;
  13678. }
  13679. if (currList === null) {
  13680. currList = { events: [], path: path };
  13681. }
  13682. currList.events.push(data);
  13683. }
  13684. if (currList) {
  13685. eventQueue.eventLists_.push(currList);
  13686. }
  13687. }
  13688. /**
  13689. * Queues the specified events and synchronously raises all events (including previously queued ones)
  13690. * for the specified path.
  13691. *
  13692. * It is assumed that the new events are all for the specified path.
  13693. *
  13694. * @param path - The path to raise events for.
  13695. * @param eventDataList - The new events to raise.
  13696. */
  13697. function eventQueueRaiseEventsAtPath(eventQueue, path, eventDataList) {
  13698. eventQueueQueueEvents(eventQueue, eventDataList);
  13699. eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
  13700. return pathEquals(eventPath, path);
  13701. });
  13702. }
  13703. /**
  13704. * Queues the specified events and synchronously raises all events (including previously queued ones) for
  13705. * locations related to the specified change path (i.e. all ancestors and descendants).
  13706. *
  13707. * It is assumed that the new events are all related (ancestor or descendant) to the specified path.
  13708. *
  13709. * @param changedPath - The path to raise events for.
  13710. * @param eventDataList - The events to raise
  13711. */
  13712. function eventQueueRaiseEventsForChangedPath(eventQueue, changedPath, eventDataList) {
  13713. eventQueueQueueEvents(eventQueue, eventDataList);
  13714. eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
  13715. return pathContains(eventPath, changedPath) ||
  13716. pathContains(changedPath, eventPath);
  13717. });
  13718. }
  13719. function eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, predicate) {
  13720. eventQueue.recursionDepth_++;
  13721. var sentAll = true;
  13722. for (var i = 0; i < eventQueue.eventLists_.length; i++) {
  13723. var eventList = eventQueue.eventLists_[i];
  13724. if (eventList) {
  13725. var eventPath = eventList.path;
  13726. if (predicate(eventPath)) {
  13727. eventListRaise(eventQueue.eventLists_[i]);
  13728. eventQueue.eventLists_[i] = null;
  13729. }
  13730. else {
  13731. sentAll = false;
  13732. }
  13733. }
  13734. }
  13735. if (sentAll) {
  13736. eventQueue.eventLists_ = [];
  13737. }
  13738. eventQueue.recursionDepth_--;
  13739. }
  13740. /**
  13741. * Iterates through the list and raises each event
  13742. */
  13743. function eventListRaise(eventList) {
  13744. for (var i = 0; i < eventList.events.length; i++) {
  13745. var eventData = eventList.events[i];
  13746. if (eventData !== null) {
  13747. eventList.events[i] = null;
  13748. var eventFn = eventData.getEventRunner();
  13749. if (logger) {
  13750. log('event: ' + eventData.toString());
  13751. }
  13752. exceptionGuard(eventFn);
  13753. }
  13754. }
  13755. }
  13756. /**
  13757. * @license
  13758. * Copyright 2017 Google LLC
  13759. *
  13760. * Licensed under the Apache License, Version 2.0 (the "License");
  13761. * you may not use this file except in compliance with the License.
  13762. * You may obtain a copy of the License at
  13763. *
  13764. * http://www.apache.org/licenses/LICENSE-2.0
  13765. *
  13766. * Unless required by applicable law or agreed to in writing, software
  13767. * distributed under the License is distributed on an "AS IS" BASIS,
  13768. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13769. * See the License for the specific language governing permissions and
  13770. * limitations under the License.
  13771. */
  13772. var INTERRUPT_REASON = 'repo_interrupt';
  13773. /**
  13774. * If a transaction does not succeed after 25 retries, we abort it. Among other
  13775. * things this ensure that if there's ever a bug causing a mismatch between
  13776. * client / server hashes for some data, we won't retry indefinitely.
  13777. */
  13778. var MAX_TRANSACTION_RETRIES = 25;
  13779. /**
  13780. * A connection to a single data repository.
  13781. */
  13782. var Repo = /** @class */ (function () {
  13783. function Repo(repoInfo_, forceRestClient_, authTokenProvider_, appCheckProvider_) {
  13784. this.repoInfo_ = repoInfo_;
  13785. this.forceRestClient_ = forceRestClient_;
  13786. this.authTokenProvider_ = authTokenProvider_;
  13787. this.appCheckProvider_ = appCheckProvider_;
  13788. this.dataUpdateCount = 0;
  13789. this.statsListener_ = null;
  13790. this.eventQueue_ = new EventQueue();
  13791. this.nextWriteId_ = 1;
  13792. this.interceptServerDataCallback_ = null;
  13793. /** A list of data pieces and paths to be set when this client disconnects. */
  13794. this.onDisconnect_ = newSparseSnapshotTree();
  13795. /** Stores queues of outstanding transactions for Firebase locations. */
  13796. this.transactionQueueTree_ = new Tree();
  13797. // TODO: This should be @private but it's used by test_access.js and internal.js
  13798. this.persistentConnection_ = null;
  13799. // This key is intentionally not updated if RepoInfo is later changed or replaced
  13800. this.key = this.repoInfo_.toURLString();
  13801. }
  13802. /**
  13803. * @returns The URL corresponding to the root of this Firebase.
  13804. */
  13805. Repo.prototype.toString = function () {
  13806. return ((this.repoInfo_.secure ? 'https://' : 'http://') + this.repoInfo_.host);
  13807. };
  13808. return Repo;
  13809. }());
  13810. function repoStart(repo, appId, authOverride) {
  13811. repo.stats_ = statsManagerGetCollection(repo.repoInfo_);
  13812. if (repo.forceRestClient_ || beingCrawled()) {
  13813. repo.server_ = new ReadonlyRestClient(repo.repoInfo_, function (pathString, data, isMerge, tag) {
  13814. repoOnDataUpdate(repo, pathString, data, isMerge, tag);
  13815. }, repo.authTokenProvider_, repo.appCheckProvider_);
  13816. // Minor hack: Fire onConnect immediately, since there's no actual connection.
  13817. setTimeout(function () { return repoOnConnectStatus(repo, /* connectStatus= */ true); }, 0);
  13818. }
  13819. else {
  13820. // Validate authOverride
  13821. if (typeof authOverride !== 'undefined' && authOverride !== null) {
  13822. if (typeof authOverride !== 'object') {
  13823. throw new Error('Only objects are supported for option databaseAuthVariableOverride');
  13824. }
  13825. try {
  13826. util.stringify(authOverride);
  13827. }
  13828. catch (e) {
  13829. throw new Error('Invalid authOverride provided: ' + e);
  13830. }
  13831. }
  13832. repo.persistentConnection_ = new PersistentConnection(repo.repoInfo_, appId, function (pathString, data, isMerge, tag) {
  13833. repoOnDataUpdate(repo, pathString, data, isMerge, tag);
  13834. }, function (connectStatus) {
  13835. repoOnConnectStatus(repo, connectStatus);
  13836. }, function (updates) {
  13837. repoOnServerInfoUpdate(repo, updates);
  13838. }, repo.authTokenProvider_, repo.appCheckProvider_, authOverride);
  13839. repo.server_ = repo.persistentConnection_;
  13840. }
  13841. repo.authTokenProvider_.addTokenChangeListener(function (token) {
  13842. repo.server_.refreshAuthToken(token);
  13843. });
  13844. repo.appCheckProvider_.addTokenChangeListener(function (result) {
  13845. repo.server_.refreshAppCheckToken(result.token);
  13846. });
  13847. // In the case of multiple Repos for the same repoInfo (i.e. there are multiple Firebase.Contexts being used),
  13848. // we only want to create one StatsReporter. As such, we'll report stats over the first Repo created.
  13849. repo.statsReporter_ = statsManagerGetOrCreateReporter(repo.repoInfo_, function () { return new StatsReporter(repo.stats_, repo.server_); });
  13850. // Used for .info.
  13851. repo.infoData_ = new SnapshotHolder();
  13852. repo.infoSyncTree_ = new SyncTree({
  13853. startListening: function (query, tag, currentHashFn, onComplete) {
  13854. var infoEvents = [];
  13855. var node = repo.infoData_.getNode(query._path);
  13856. // This is possibly a hack, but we have different semantics for .info endpoints. We don't raise null events
  13857. // on initial data...
  13858. if (!node.isEmpty()) {
  13859. infoEvents = syncTreeApplyServerOverwrite(repo.infoSyncTree_, query._path, node);
  13860. setTimeout(function () {
  13861. onComplete('ok');
  13862. }, 0);
  13863. }
  13864. return infoEvents;
  13865. },
  13866. stopListening: function () { }
  13867. });
  13868. repoUpdateInfo(repo, 'connected', false);
  13869. repo.serverSyncTree_ = new SyncTree({
  13870. startListening: function (query, tag, currentHashFn, onComplete) {
  13871. repo.server_.listen(query, currentHashFn, tag, function (status, data) {
  13872. var events = onComplete(status, data);
  13873. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, query._path, events);
  13874. });
  13875. // No synchronous events for network-backed sync trees
  13876. return [];
  13877. },
  13878. stopListening: function (query, tag) {
  13879. repo.server_.unlisten(query, tag);
  13880. }
  13881. });
  13882. }
  13883. /**
  13884. * @returns The time in milliseconds, taking the server offset into account if we have one.
  13885. */
  13886. function repoServerTime(repo) {
  13887. var offsetNode = repo.infoData_.getNode(new Path('.info/serverTimeOffset'));
  13888. var offset = offsetNode.val() || 0;
  13889. return new Date().getTime() + offset;
  13890. }
  13891. /**
  13892. * Generate ServerValues using some variables from the repo object.
  13893. */
  13894. function repoGenerateServerValues(repo) {
  13895. return generateWithValues({
  13896. timestamp: repoServerTime(repo)
  13897. });
  13898. }
  13899. /**
  13900. * Called by realtime when we get new messages from the server.
  13901. */
  13902. function repoOnDataUpdate(repo, pathString, data, isMerge, tag) {
  13903. // For testing.
  13904. repo.dataUpdateCount++;
  13905. var path = new Path(pathString);
  13906. data = repo.interceptServerDataCallback_
  13907. ? repo.interceptServerDataCallback_(pathString, data)
  13908. : data;
  13909. var events = [];
  13910. if (tag) {
  13911. if (isMerge) {
  13912. var taggedChildren = util.map(data, function (raw) { return nodeFromJSON(raw); });
  13913. events = syncTreeApplyTaggedQueryMerge(repo.serverSyncTree_, path, taggedChildren, tag);
  13914. }
  13915. else {
  13916. var taggedSnap = nodeFromJSON(data);
  13917. events = syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, path, taggedSnap, tag);
  13918. }
  13919. }
  13920. else if (isMerge) {
  13921. var changedChildren = util.map(data, function (raw) { return nodeFromJSON(raw); });
  13922. events = syncTreeApplyServerMerge(repo.serverSyncTree_, path, changedChildren);
  13923. }
  13924. else {
  13925. var snap = nodeFromJSON(data);
  13926. events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap);
  13927. }
  13928. var affectedPath = path;
  13929. if (events.length > 0) {
  13930. // Since we have a listener outstanding for each transaction, receiving any events
  13931. // is a proxy for some change having occurred.
  13932. affectedPath = repoRerunTransactions(repo, path);
  13933. }
  13934. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, events);
  13935. }
  13936. function repoOnConnectStatus(repo, connectStatus) {
  13937. repoUpdateInfo(repo, 'connected', connectStatus);
  13938. if (connectStatus === false) {
  13939. repoRunOnDisconnectEvents(repo);
  13940. }
  13941. }
  13942. function repoOnServerInfoUpdate(repo, updates) {
  13943. each(updates, function (key, value) {
  13944. repoUpdateInfo(repo, key, value);
  13945. });
  13946. }
  13947. function repoUpdateInfo(repo, pathString, value) {
  13948. var path = new Path('/.info/' + pathString);
  13949. var newNode = nodeFromJSON(value);
  13950. repo.infoData_.updateSnapshot(path, newNode);
  13951. var events = syncTreeApplyServerOverwrite(repo.infoSyncTree_, path, newNode);
  13952. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
  13953. }
  13954. function repoGetNextWriteId(repo) {
  13955. return repo.nextWriteId_++;
  13956. }
  13957. /**
  13958. * The purpose of `getValue` is to return the latest known value
  13959. * satisfying `query`.
  13960. *
  13961. * This method will first check for in-memory cached values
  13962. * belonging to active listeners. If they are found, such values
  13963. * are considered to be the most up-to-date.
  13964. *
  13965. * If the client is not connected, this method will wait until the
  13966. * repo has established a connection and then request the value for `query`.
  13967. * If the client is not able to retrieve the query result for another reason,
  13968. * it reports an error.
  13969. *
  13970. * @param query - The query to surface a value for.
  13971. */
  13972. function repoGetValue(repo, query, eventRegistration) {
  13973. // Only active queries are cached. There is no persisted cache.
  13974. var cached = syncTreeGetServerValue(repo.serverSyncTree_, query);
  13975. if (cached != null) {
  13976. return Promise.resolve(cached);
  13977. }
  13978. return repo.server_.get(query).then(function (payload) {
  13979. var node = nodeFromJSON(payload).withIndex(query._queryParams.getIndex());
  13980. /**
  13981. * Below we simulate the actions of an `onlyOnce` `onValue()` event where:
  13982. * Add an event registration,
  13983. * Update data at the path,
  13984. * Raise any events,
  13985. * Cleanup the SyncTree
  13986. */
  13987. syncTreeAddEventRegistration(repo.serverSyncTree_, query, eventRegistration, true);
  13988. var events;
  13989. if (query._queryParams.loadsAllData()) {
  13990. events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, query._path, node);
  13991. }
  13992. else {
  13993. var tag = syncTreeTagForQuery(repo.serverSyncTree_, query);
  13994. events = syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, query._path, node, tag);
  13995. }
  13996. /*
  13997. * We need to raise events in the scenario where `get()` is called at a parent path, and
  13998. * while the `get()` is pending, `onValue` is called at a child location. While get() is waiting
  13999. * for the data, `onValue` will register a new event. Then, get() will come back, and update the syncTree
  14000. * and its corresponding serverCache, including the child location where `onValue` is called. Then,
  14001. * `onValue` will receive the event from the server, but look at the syncTree and see that the data received
  14002. * from the server is already at the SyncPoint, and so the `onValue` callback will never get fired.
  14003. * Calling `eventQueueRaiseEventsForChangedPath()` is the correct way to propagate the events and
  14004. * ensure the corresponding child events will get fired.
  14005. */
  14006. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, query._path, events);
  14007. syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, eventRegistration, null, true);
  14008. return node;
  14009. }, function (err) {
  14010. repoLog(repo, 'get for query ' + util.stringify(query) + ' failed: ' + err);
  14011. return Promise.reject(new Error(err));
  14012. });
  14013. }
  14014. function repoSetWithPriority(repo, path, newVal, newPriority, onComplete) {
  14015. repoLog(repo, 'set', {
  14016. path: path.toString(),
  14017. value: newVal,
  14018. priority: newPriority
  14019. });
  14020. // TODO: Optimize this behavior to either (a) store flag to skip resolving where possible and / or
  14021. // (b) store unresolved paths on JSON parse
  14022. var serverValues = repoGenerateServerValues(repo);
  14023. var newNodeUnresolved = nodeFromJSON(newVal, newPriority);
  14024. var existing = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path);
  14025. var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, existing, serverValues);
  14026. var writeId = repoGetNextWriteId(repo);
  14027. var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, writeId, true);
  14028. eventQueueQueueEvents(repo.eventQueue_, events);
  14029. repo.server_.put(path.toString(), newNodeUnresolved.val(/*export=*/ true), function (status, errorReason) {
  14030. var success = status === 'ok';
  14031. if (!success) {
  14032. warn$1('set at ' + path + ' failed: ' + status);
  14033. }
  14034. var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId, !success);
  14035. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, clearEvents);
  14036. repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
  14037. });
  14038. var affectedPath = repoAbortTransactions(repo, path);
  14039. repoRerunTransactions(repo, affectedPath);
  14040. // We queued the events above, so just flush the queue here
  14041. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, []);
  14042. }
  14043. function repoUpdate(repo, path, childrenToMerge, onComplete) {
  14044. repoLog(repo, 'update', { path: path.toString(), value: childrenToMerge });
  14045. // Start with our existing data and merge each child into it.
  14046. var empty = true;
  14047. var serverValues = repoGenerateServerValues(repo);
  14048. var changedChildren = {};
  14049. each(childrenToMerge, function (changedKey, changedValue) {
  14050. empty = false;
  14051. changedChildren[changedKey] = resolveDeferredValueTree(pathChild(path, changedKey), nodeFromJSON(changedValue), repo.serverSyncTree_, serverValues);
  14052. });
  14053. if (!empty) {
  14054. var writeId_1 = repoGetNextWriteId(repo);
  14055. var events = syncTreeApplyUserMerge(repo.serverSyncTree_, path, changedChildren, writeId_1);
  14056. eventQueueQueueEvents(repo.eventQueue_, events);
  14057. repo.server_.merge(path.toString(), childrenToMerge, function (status, errorReason) {
  14058. var success = status === 'ok';
  14059. if (!success) {
  14060. warn$1('update at ' + path + ' failed: ' + status);
  14061. }
  14062. var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId_1, !success);
  14063. var affectedPath = clearEvents.length > 0 ? repoRerunTransactions(repo, path) : path;
  14064. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, clearEvents);
  14065. repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
  14066. });
  14067. each(childrenToMerge, function (changedPath) {
  14068. var affectedPath = repoAbortTransactions(repo, pathChild(path, changedPath));
  14069. repoRerunTransactions(repo, affectedPath);
  14070. });
  14071. // We queued the events above, so just flush the queue here
  14072. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, []);
  14073. }
  14074. else {
  14075. log("update() called with empty data. Don't do anything.");
  14076. repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
  14077. }
  14078. }
  14079. /**
  14080. * Applies all of the changes stored up in the onDisconnect_ tree.
  14081. */
  14082. function repoRunOnDisconnectEvents(repo) {
  14083. repoLog(repo, 'onDisconnectEvents');
  14084. var serverValues = repoGenerateServerValues(repo);
  14085. var resolvedOnDisconnectTree = newSparseSnapshotTree();
  14086. sparseSnapshotTreeForEachTree(repo.onDisconnect_, newEmptyPath(), function (path, node) {
  14087. var resolved = resolveDeferredValueTree(path, node, repo.serverSyncTree_, serverValues);
  14088. sparseSnapshotTreeRemember(resolvedOnDisconnectTree, path, resolved);
  14089. });
  14090. var events = [];
  14091. sparseSnapshotTreeForEachTree(resolvedOnDisconnectTree, newEmptyPath(), function (path, snap) {
  14092. events = events.concat(syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap));
  14093. var affectedPath = repoAbortTransactions(repo, path);
  14094. repoRerunTransactions(repo, affectedPath);
  14095. });
  14096. repo.onDisconnect_ = newSparseSnapshotTree();
  14097. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, newEmptyPath(), events);
  14098. }
  14099. function repoOnDisconnectCancel(repo, path, onComplete) {
  14100. repo.server_.onDisconnectCancel(path.toString(), function (status, errorReason) {
  14101. if (status === 'ok') {
  14102. sparseSnapshotTreeForget(repo.onDisconnect_, path);
  14103. }
  14104. repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
  14105. });
  14106. }
  14107. function repoOnDisconnectSet(repo, path, value, onComplete) {
  14108. var newNode = nodeFromJSON(value);
  14109. repo.server_.onDisconnectPut(path.toString(), newNode.val(/*export=*/ true), function (status, errorReason) {
  14110. if (status === 'ok') {
  14111. sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
  14112. }
  14113. repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
  14114. });
  14115. }
  14116. function repoOnDisconnectSetWithPriority(repo, path, value, priority, onComplete) {
  14117. var newNode = nodeFromJSON(value, priority);
  14118. repo.server_.onDisconnectPut(path.toString(), newNode.val(/*export=*/ true), function (status, errorReason) {
  14119. if (status === 'ok') {
  14120. sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
  14121. }
  14122. repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
  14123. });
  14124. }
  14125. function repoOnDisconnectUpdate(repo, path, childrenToMerge, onComplete) {
  14126. if (util.isEmpty(childrenToMerge)) {
  14127. log("onDisconnect().update() called with empty data. Don't do anything.");
  14128. repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
  14129. return;
  14130. }
  14131. repo.server_.onDisconnectMerge(path.toString(), childrenToMerge, function (status, errorReason) {
  14132. if (status === 'ok') {
  14133. each(childrenToMerge, function (childName, childNode) {
  14134. var newChildNode = nodeFromJSON(childNode);
  14135. sparseSnapshotTreeRemember(repo.onDisconnect_, pathChild(path, childName), newChildNode);
  14136. });
  14137. }
  14138. repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
  14139. });
  14140. }
  14141. function repoAddEventCallbackForQuery(repo, query, eventRegistration) {
  14142. var events;
  14143. if (pathGetFront(query._path) === '.info') {
  14144. events = syncTreeAddEventRegistration(repo.infoSyncTree_, query, eventRegistration);
  14145. }
  14146. else {
  14147. events = syncTreeAddEventRegistration(repo.serverSyncTree_, query, eventRegistration);
  14148. }
  14149. eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
  14150. }
  14151. function repoRemoveEventCallbackForQuery(repo, query, eventRegistration) {
  14152. // These are guaranteed not to raise events, since we're not passing in a cancelError. However, we can future-proof
  14153. // a little bit by handling the return values anyways.
  14154. var events;
  14155. if (pathGetFront(query._path) === '.info') {
  14156. events = syncTreeRemoveEventRegistration(repo.infoSyncTree_, query, eventRegistration);
  14157. }
  14158. else {
  14159. events = syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, eventRegistration);
  14160. }
  14161. eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
  14162. }
  14163. function repoInterrupt(repo) {
  14164. if (repo.persistentConnection_) {
  14165. repo.persistentConnection_.interrupt(INTERRUPT_REASON);
  14166. }
  14167. }
  14168. function repoResume(repo) {
  14169. if (repo.persistentConnection_) {
  14170. repo.persistentConnection_.resume(INTERRUPT_REASON);
  14171. }
  14172. }
  14173. function repoLog(repo) {
  14174. var varArgs = [];
  14175. for (var _i = 1; _i < arguments.length; _i++) {
  14176. varArgs[_i - 1] = arguments[_i];
  14177. }
  14178. var prefix = '';
  14179. if (repo.persistentConnection_) {
  14180. prefix = repo.persistentConnection_.id + ':';
  14181. }
  14182. log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs), false));
  14183. }
  14184. function repoCallOnCompleteCallback(repo, callback, status, errorReason) {
  14185. if (callback) {
  14186. exceptionGuard(function () {
  14187. if (status === 'ok') {
  14188. callback(null);
  14189. }
  14190. else {
  14191. var code = (status || 'error').toUpperCase();
  14192. var message = code;
  14193. if (errorReason) {
  14194. message += ': ' + errorReason;
  14195. }
  14196. var error = new Error(message);
  14197. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  14198. error.code = code;
  14199. callback(error);
  14200. }
  14201. });
  14202. }
  14203. }
  14204. /**
  14205. * Creates a new transaction, adds it to the transactions we're tracking, and
  14206. * sends it to the server if possible.
  14207. *
  14208. * @param path - Path at which to do transaction.
  14209. * @param transactionUpdate - Update callback.
  14210. * @param onComplete - Completion callback.
  14211. * @param unwatcher - Function that will be called when the transaction no longer
  14212. * need data updates for `path`.
  14213. * @param applyLocally - Whether or not to make intermediate results visible
  14214. */
  14215. function repoStartTransaction(repo, path, transactionUpdate, onComplete, unwatcher, applyLocally) {
  14216. repoLog(repo, 'transaction on ' + path);
  14217. // Initialize transaction.
  14218. var transaction = {
  14219. path: path,
  14220. update: transactionUpdate,
  14221. onComplete: onComplete,
  14222. // One of TransactionStatus enums.
  14223. status: null,
  14224. // Used when combining transactions at different locations to figure out
  14225. // which one goes first.
  14226. order: LUIDGenerator(),
  14227. // Whether to raise local events for this transaction.
  14228. applyLocally: applyLocally,
  14229. // Count of how many times we've retried the transaction.
  14230. retryCount: 0,
  14231. // Function to call to clean up our .on() listener.
  14232. unwatcher: unwatcher,
  14233. // Stores why a transaction was aborted.
  14234. abortReason: null,
  14235. currentWriteId: null,
  14236. currentInputSnapshot: null,
  14237. currentOutputSnapshotRaw: null,
  14238. currentOutputSnapshotResolved: null
  14239. };
  14240. // Run transaction initially.
  14241. var currentState = repoGetLatestState(repo, path, undefined);
  14242. transaction.currentInputSnapshot = currentState;
  14243. var newVal = transaction.update(currentState.val());
  14244. if (newVal === undefined) {
  14245. // Abort transaction.
  14246. transaction.unwatcher();
  14247. transaction.currentOutputSnapshotRaw = null;
  14248. transaction.currentOutputSnapshotResolved = null;
  14249. if (transaction.onComplete) {
  14250. transaction.onComplete(null, false, transaction.currentInputSnapshot);
  14251. }
  14252. }
  14253. else {
  14254. validateFirebaseData('transaction failed: Data returned ', newVal, transaction.path);
  14255. // Mark as run and add to our queue.
  14256. transaction.status = 0 /* TransactionStatus.RUN */;
  14257. var queueNode = treeSubTree(repo.transactionQueueTree_, path);
  14258. var nodeQueue = treeGetValue(queueNode) || [];
  14259. nodeQueue.push(transaction);
  14260. treeSetValue(queueNode, nodeQueue);
  14261. // Update visibleData and raise events
  14262. // Note: We intentionally raise events after updating all of our
  14263. // transaction state, since the user could start new transactions from the
  14264. // event callbacks.
  14265. var priorityForNode = void 0;
  14266. if (typeof newVal === 'object' &&
  14267. newVal !== null &&
  14268. util.contains(newVal, '.priority')) {
  14269. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  14270. priorityForNode = util.safeGet(newVal, '.priority');
  14271. util.assert(isValidPriority(priorityForNode), 'Invalid priority returned by transaction. ' +
  14272. 'Priority must be a valid string, finite number, server value, or null.');
  14273. }
  14274. else {
  14275. var currentNode = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path) ||
  14276. ChildrenNode.EMPTY_NODE;
  14277. priorityForNode = currentNode.getPriority().val();
  14278. }
  14279. var serverValues = repoGenerateServerValues(repo);
  14280. var newNodeUnresolved = nodeFromJSON(newVal, priorityForNode);
  14281. var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, currentState, serverValues);
  14282. transaction.currentOutputSnapshotRaw = newNodeUnresolved;
  14283. transaction.currentOutputSnapshotResolved = newNode;
  14284. transaction.currentWriteId = repoGetNextWriteId(repo);
  14285. var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, transaction.currentWriteId, transaction.applyLocally);
  14286. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
  14287. repoSendReadyTransactions(repo, repo.transactionQueueTree_);
  14288. }
  14289. }
  14290. /**
  14291. * @param excludeSets - A specific set to exclude
  14292. */
  14293. function repoGetLatestState(repo, path, excludeSets) {
  14294. return (syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path, excludeSets) ||
  14295. ChildrenNode.EMPTY_NODE);
  14296. }
  14297. /**
  14298. * Sends any already-run transactions that aren't waiting for outstanding
  14299. * transactions to complete.
  14300. *
  14301. * Externally it's called with no arguments, but it calls itself recursively
  14302. * with a particular transactionQueueTree node to recurse through the tree.
  14303. *
  14304. * @param node - transactionQueueTree node to start at.
  14305. */
  14306. function repoSendReadyTransactions(repo, node) {
  14307. if (node === void 0) { node = repo.transactionQueueTree_; }
  14308. // Before recursing, make sure any completed transactions are removed.
  14309. if (!node) {
  14310. repoPruneCompletedTransactionsBelowNode(repo, node);
  14311. }
  14312. if (treeGetValue(node)) {
  14313. var queue = repoBuildTransactionQueue(repo, node);
  14314. util.assert(queue.length > 0, 'Sending zero length transaction queue');
  14315. var allRun = queue.every(function (transaction) { return transaction.status === 0 /* TransactionStatus.RUN */; });
  14316. // If they're all run (and not sent), we can send them. Else, we must wait.
  14317. if (allRun) {
  14318. repoSendTransactionQueue(repo, treeGetPath(node), queue);
  14319. }
  14320. }
  14321. else if (treeHasChildren(node)) {
  14322. treeForEachChild(node, function (childNode) {
  14323. repoSendReadyTransactions(repo, childNode);
  14324. });
  14325. }
  14326. }
  14327. /**
  14328. * Given a list of run transactions, send them to the server and then handle
  14329. * the result (success or failure).
  14330. *
  14331. * @param path - The location of the queue.
  14332. * @param queue - Queue of transactions under the specified location.
  14333. */
  14334. function repoSendTransactionQueue(repo, path, queue) {
  14335. // Mark transactions as sent and increment retry count!
  14336. var setsToIgnore = queue.map(function (txn) {
  14337. return txn.currentWriteId;
  14338. });
  14339. var latestState = repoGetLatestState(repo, path, setsToIgnore);
  14340. var snapToSend = latestState;
  14341. var latestHash = latestState.hash();
  14342. for (var i = 0; i < queue.length; i++) {
  14343. var txn = queue[i];
  14344. util.assert(txn.status === 0 /* TransactionStatus.RUN */, 'tryToSendTransactionQueue_: items in queue should all be run.');
  14345. txn.status = 1 /* TransactionStatus.SENT */;
  14346. txn.retryCount++;
  14347. var relativePath = newRelativePath(path, txn.path);
  14348. // If we've gotten to this point, the output snapshot must be defined.
  14349. snapToSend = snapToSend.updateChild(relativePath /** @type {!Node} */, txn.currentOutputSnapshotRaw);
  14350. }
  14351. var dataToSend = snapToSend.val(true);
  14352. var pathToSend = path;
  14353. // Send the put.
  14354. repo.server_.put(pathToSend.toString(), dataToSend, function (status) {
  14355. repoLog(repo, 'transaction put response', {
  14356. path: pathToSend.toString(),
  14357. status: status
  14358. });
  14359. var events = [];
  14360. if (status === 'ok') {
  14361. // Queue up the callbacks and fire them after cleaning up all of our
  14362. // transaction state, since the callback could trigger more
  14363. // transactions or sets.
  14364. var callbacks = [];
  14365. var _loop_1 = function (i) {
  14366. queue[i].status = 2 /* TransactionStatus.COMPLETED */;
  14367. events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId));
  14368. if (queue[i].onComplete) {
  14369. // We never unset the output snapshot, and given that this
  14370. // transaction is complete, it should be set
  14371. callbacks.push(function () {
  14372. return queue[i].onComplete(null, true, queue[i].currentOutputSnapshotResolved);
  14373. });
  14374. }
  14375. queue[i].unwatcher();
  14376. };
  14377. for (var i = 0; i < queue.length; i++) {
  14378. _loop_1(i);
  14379. }
  14380. // Now remove the completed transactions.
  14381. repoPruneCompletedTransactionsBelowNode(repo, treeSubTree(repo.transactionQueueTree_, path));
  14382. // There may be pending transactions that we can now send.
  14383. repoSendReadyTransactions(repo, repo.transactionQueueTree_);
  14384. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
  14385. // Finally, trigger onComplete callbacks.
  14386. for (var i = 0; i < callbacks.length; i++) {
  14387. exceptionGuard(callbacks[i]);
  14388. }
  14389. }
  14390. else {
  14391. // transactions are no longer sent. Update their status appropriately.
  14392. if (status === 'datastale') {
  14393. for (var i = 0; i < queue.length; i++) {
  14394. if (queue[i].status === 3 /* TransactionStatus.SENT_NEEDS_ABORT */) {
  14395. queue[i].status = 4 /* TransactionStatus.NEEDS_ABORT */;
  14396. }
  14397. else {
  14398. queue[i].status = 0 /* TransactionStatus.RUN */;
  14399. }
  14400. }
  14401. }
  14402. else {
  14403. warn$1('transaction at ' + pathToSend.toString() + ' failed: ' + status);
  14404. for (var i = 0; i < queue.length; i++) {
  14405. queue[i].status = 4 /* TransactionStatus.NEEDS_ABORT */;
  14406. queue[i].abortReason = status;
  14407. }
  14408. }
  14409. repoRerunTransactions(repo, path);
  14410. }
  14411. }, latestHash);
  14412. }
  14413. /**
  14414. * Finds all transactions dependent on the data at changedPath and reruns them.
  14415. *
  14416. * Should be called any time cached data changes.
  14417. *
  14418. * Return the highest path that was affected by rerunning transactions. This
  14419. * is the path at which events need to be raised for.
  14420. *
  14421. * @param changedPath - The path in mergedData that changed.
  14422. * @returns The rootmost path that was affected by rerunning transactions.
  14423. */
  14424. function repoRerunTransactions(repo, changedPath) {
  14425. var rootMostTransactionNode = repoGetAncestorTransactionNode(repo, changedPath);
  14426. var path = treeGetPath(rootMostTransactionNode);
  14427. var queue = repoBuildTransactionQueue(repo, rootMostTransactionNode);
  14428. repoRerunTransactionQueue(repo, queue, path);
  14429. return path;
  14430. }
  14431. /**
  14432. * Does all the work of rerunning transactions (as well as cleans up aborted
  14433. * transactions and whatnot).
  14434. *
  14435. * @param queue - The queue of transactions to run.
  14436. * @param path - The path the queue is for.
  14437. */
  14438. function repoRerunTransactionQueue(repo, queue, path) {
  14439. if (queue.length === 0) {
  14440. return; // Nothing to do!
  14441. }
  14442. // Queue up the callbacks and fire them after cleaning up all of our
  14443. // transaction state, since the callback could trigger more transactions or
  14444. // sets.
  14445. var callbacks = [];
  14446. var events = [];
  14447. // Ignore all of the sets we're going to re-run.
  14448. var txnsToRerun = queue.filter(function (q) {
  14449. return q.status === 0 /* TransactionStatus.RUN */;
  14450. });
  14451. var setsToIgnore = txnsToRerun.map(function (q) {
  14452. return q.currentWriteId;
  14453. });
  14454. var _loop_2 = function (i) {
  14455. var transaction = queue[i];
  14456. var relativePath = newRelativePath(path, transaction.path);
  14457. var abortTransaction = false, abortReason;
  14458. util.assert(relativePath !== null, 'rerunTransactionsUnderNode_: relativePath should not be null.');
  14459. if (transaction.status === 4 /* TransactionStatus.NEEDS_ABORT */) {
  14460. abortTransaction = true;
  14461. abortReason = transaction.abortReason;
  14462. events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
  14463. }
  14464. else if (transaction.status === 0 /* TransactionStatus.RUN */) {
  14465. if (transaction.retryCount >= MAX_TRANSACTION_RETRIES) {
  14466. abortTransaction = true;
  14467. abortReason = 'maxretry';
  14468. events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
  14469. }
  14470. else {
  14471. // This code reruns a transaction
  14472. var currentNode = repoGetLatestState(repo, transaction.path, setsToIgnore);
  14473. transaction.currentInputSnapshot = currentNode;
  14474. var newData = queue[i].update(currentNode.val());
  14475. if (newData !== undefined) {
  14476. validateFirebaseData('transaction failed: Data returned ', newData, transaction.path);
  14477. var newDataNode = nodeFromJSON(newData);
  14478. var hasExplicitPriority = typeof newData === 'object' &&
  14479. newData != null &&
  14480. util.contains(newData, '.priority');
  14481. if (!hasExplicitPriority) {
  14482. // Keep the old priority if there wasn't a priority explicitly specified.
  14483. newDataNode = newDataNode.updatePriority(currentNode.getPriority());
  14484. }
  14485. var oldWriteId = transaction.currentWriteId;
  14486. var serverValues = repoGenerateServerValues(repo);
  14487. var newNodeResolved = resolveDeferredValueSnapshot(newDataNode, currentNode, serverValues);
  14488. transaction.currentOutputSnapshotRaw = newDataNode;
  14489. transaction.currentOutputSnapshotResolved = newNodeResolved;
  14490. transaction.currentWriteId = repoGetNextWriteId(repo);
  14491. // Mutates setsToIgnore in place
  14492. setsToIgnore.splice(setsToIgnore.indexOf(oldWriteId), 1);
  14493. events = events.concat(syncTreeApplyUserOverwrite(repo.serverSyncTree_, transaction.path, newNodeResolved, transaction.currentWriteId, transaction.applyLocally));
  14494. events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, oldWriteId, true));
  14495. }
  14496. else {
  14497. abortTransaction = true;
  14498. abortReason = 'nodata';
  14499. events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
  14500. }
  14501. }
  14502. }
  14503. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
  14504. events = [];
  14505. if (abortTransaction) {
  14506. // Abort.
  14507. queue[i].status = 2 /* TransactionStatus.COMPLETED */;
  14508. // Removing a listener can trigger pruning which can muck with
  14509. // mergedData/visibleData (as it prunes data). So defer the unwatcher
  14510. // until we're done.
  14511. (function (unwatcher) {
  14512. setTimeout(unwatcher, Math.floor(0));
  14513. })(queue[i].unwatcher);
  14514. if (queue[i].onComplete) {
  14515. if (abortReason === 'nodata') {
  14516. callbacks.push(function () {
  14517. return queue[i].onComplete(null, false, queue[i].currentInputSnapshot);
  14518. });
  14519. }
  14520. else {
  14521. callbacks.push(function () {
  14522. return queue[i].onComplete(new Error(abortReason), false, null);
  14523. });
  14524. }
  14525. }
  14526. }
  14527. };
  14528. for (var i = 0; i < queue.length; i++) {
  14529. _loop_2(i);
  14530. }
  14531. // Clean up completed transactions.
  14532. repoPruneCompletedTransactionsBelowNode(repo, repo.transactionQueueTree_);
  14533. // Now fire callbacks, now that we're in a good, known state.
  14534. for (var i = 0; i < callbacks.length; i++) {
  14535. exceptionGuard(callbacks[i]);
  14536. }
  14537. // Try to send the transaction result to the server.
  14538. repoSendReadyTransactions(repo, repo.transactionQueueTree_);
  14539. }
  14540. /**
  14541. * Returns the rootmost ancestor node of the specified path that has a pending
  14542. * transaction on it, or just returns the node for the given path if there are
  14543. * no pending transactions on any ancestor.
  14544. *
  14545. * @param path - The location to start at.
  14546. * @returns The rootmost node with a transaction.
  14547. */
  14548. function repoGetAncestorTransactionNode(repo, path) {
  14549. var front;
  14550. // Start at the root and walk deeper into the tree towards path until we
  14551. // find a node with pending transactions.
  14552. var transactionNode = repo.transactionQueueTree_;
  14553. front = pathGetFront(path);
  14554. while (front !== null && treeGetValue(transactionNode) === undefined) {
  14555. transactionNode = treeSubTree(transactionNode, front);
  14556. path = pathPopFront(path);
  14557. front = pathGetFront(path);
  14558. }
  14559. return transactionNode;
  14560. }
  14561. /**
  14562. * Builds the queue of all transactions at or below the specified
  14563. * transactionNode.
  14564. *
  14565. * @param transactionNode
  14566. * @returns The generated queue.
  14567. */
  14568. function repoBuildTransactionQueue(repo, transactionNode) {
  14569. // Walk any child transaction queues and aggregate them into a single queue.
  14570. var transactionQueue = [];
  14571. repoAggregateTransactionQueuesForNode(repo, transactionNode, transactionQueue);
  14572. // Sort them by the order the transactions were created.
  14573. transactionQueue.sort(function (a, b) { return a.order - b.order; });
  14574. return transactionQueue;
  14575. }
  14576. function repoAggregateTransactionQueuesForNode(repo, node, queue) {
  14577. var nodeQueue = treeGetValue(node);
  14578. if (nodeQueue) {
  14579. for (var i = 0; i < nodeQueue.length; i++) {
  14580. queue.push(nodeQueue[i]);
  14581. }
  14582. }
  14583. treeForEachChild(node, function (child) {
  14584. repoAggregateTransactionQueuesForNode(repo, child, queue);
  14585. });
  14586. }
  14587. /**
  14588. * Remove COMPLETED transactions at or below this node in the transactionQueueTree_.
  14589. */
  14590. function repoPruneCompletedTransactionsBelowNode(repo, node) {
  14591. var queue = treeGetValue(node);
  14592. if (queue) {
  14593. var to = 0;
  14594. for (var from = 0; from < queue.length; from++) {
  14595. if (queue[from].status !== 2 /* TransactionStatus.COMPLETED */) {
  14596. queue[to] = queue[from];
  14597. to++;
  14598. }
  14599. }
  14600. queue.length = to;
  14601. treeSetValue(node, queue.length > 0 ? queue : undefined);
  14602. }
  14603. treeForEachChild(node, function (childNode) {
  14604. repoPruneCompletedTransactionsBelowNode(repo, childNode);
  14605. });
  14606. }
  14607. /**
  14608. * Aborts all transactions on ancestors or descendants of the specified path.
  14609. * Called when doing a set() or update() since we consider them incompatible
  14610. * with transactions.
  14611. *
  14612. * @param path - Path for which we want to abort related transactions.
  14613. */
  14614. function repoAbortTransactions(repo, path) {
  14615. var affectedPath = treeGetPath(repoGetAncestorTransactionNode(repo, path));
  14616. var transactionNode = treeSubTree(repo.transactionQueueTree_, path);
  14617. treeForEachAncestor(transactionNode, function (node) {
  14618. repoAbortTransactionsOnNode(repo, node);
  14619. });
  14620. repoAbortTransactionsOnNode(repo, transactionNode);
  14621. treeForEachDescendant(transactionNode, function (node) {
  14622. repoAbortTransactionsOnNode(repo, node);
  14623. });
  14624. return affectedPath;
  14625. }
  14626. /**
  14627. * Abort transactions stored in this transaction queue node.
  14628. *
  14629. * @param node - Node to abort transactions for.
  14630. */
  14631. function repoAbortTransactionsOnNode(repo, node) {
  14632. var queue = treeGetValue(node);
  14633. if (queue) {
  14634. // Queue up the callbacks and fire them after cleaning up all of our
  14635. // transaction state, since the callback could trigger more transactions
  14636. // or sets.
  14637. var callbacks = [];
  14638. // Go through queue. Any already-sent transactions must be marked for
  14639. // abort, while the unsent ones can be immediately aborted and removed.
  14640. var events = [];
  14641. var lastSent = -1;
  14642. for (var i = 0; i < queue.length; i++) {
  14643. if (queue[i].status === 3 /* TransactionStatus.SENT_NEEDS_ABORT */) ;
  14644. else if (queue[i].status === 1 /* TransactionStatus.SENT */) {
  14645. util.assert(lastSent === i - 1, 'All SENT items should be at beginning of queue.');
  14646. lastSent = i;
  14647. // Mark transaction for abort when it comes back.
  14648. queue[i].status = 3 /* TransactionStatus.SENT_NEEDS_ABORT */;
  14649. queue[i].abortReason = 'set';
  14650. }
  14651. else {
  14652. util.assert(queue[i].status === 0 /* TransactionStatus.RUN */, 'Unexpected transaction status in abort');
  14653. // We can abort it immediately.
  14654. queue[i].unwatcher();
  14655. events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId, true));
  14656. if (queue[i].onComplete) {
  14657. callbacks.push(queue[i].onComplete.bind(null, new Error('set'), false, null));
  14658. }
  14659. }
  14660. }
  14661. if (lastSent === -1) {
  14662. // We're not waiting for any sent transactions. We can clear the queue.
  14663. treeSetValue(node, undefined);
  14664. }
  14665. else {
  14666. // Remove the transactions we aborted.
  14667. queue.length = lastSent + 1;
  14668. }
  14669. // Now fire the callbacks.
  14670. eventQueueRaiseEventsForChangedPath(repo.eventQueue_, treeGetPath(node), events);
  14671. for (var i = 0; i < callbacks.length; i++) {
  14672. exceptionGuard(callbacks[i]);
  14673. }
  14674. }
  14675. }
  14676. /**
  14677. * @license
  14678. * Copyright 2017 Google LLC
  14679. *
  14680. * Licensed under the Apache License, Version 2.0 (the "License");
  14681. * you may not use this file except in compliance with the License.
  14682. * You may obtain a copy of the License at
  14683. *
  14684. * http://www.apache.org/licenses/LICENSE-2.0
  14685. *
  14686. * Unless required by applicable law or agreed to in writing, software
  14687. * distributed under the License is distributed on an "AS IS" BASIS,
  14688. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14689. * See the License for the specific language governing permissions and
  14690. * limitations under the License.
  14691. */
  14692. function decodePath(pathString) {
  14693. var pathStringDecoded = '';
  14694. var pieces = pathString.split('/');
  14695. for (var i = 0; i < pieces.length; i++) {
  14696. if (pieces[i].length > 0) {
  14697. var piece = pieces[i];
  14698. try {
  14699. piece = decodeURIComponent(piece.replace(/\+/g, ' '));
  14700. }
  14701. catch (e) { }
  14702. pathStringDecoded += '/' + piece;
  14703. }
  14704. }
  14705. return pathStringDecoded;
  14706. }
  14707. /**
  14708. * @returns key value hash
  14709. */
  14710. function decodeQuery(queryString) {
  14711. var e_1, _a;
  14712. var results = {};
  14713. if (queryString.charAt(0) === '?') {
  14714. queryString = queryString.substring(1);
  14715. }
  14716. try {
  14717. for (var _b = tslib.__values(queryString.split('&')), _c = _b.next(); !_c.done; _c = _b.next()) {
  14718. var segment = _c.value;
  14719. if (segment.length === 0) {
  14720. continue;
  14721. }
  14722. var kv = segment.split('=');
  14723. if (kv.length === 2) {
  14724. results[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
  14725. }
  14726. else {
  14727. warn$1("Invalid query segment '".concat(segment, "' in query '").concat(queryString, "'"));
  14728. }
  14729. }
  14730. }
  14731. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  14732. finally {
  14733. try {
  14734. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  14735. }
  14736. finally { if (e_1) throw e_1.error; }
  14737. }
  14738. return results;
  14739. }
  14740. var parseRepoInfo = function (dataURL, nodeAdmin) {
  14741. var parsedUrl = parseDatabaseURL(dataURL), namespace = parsedUrl.namespace;
  14742. if (parsedUrl.domain === 'firebase.com') {
  14743. fatal(parsedUrl.host +
  14744. ' is no longer supported. ' +
  14745. 'Please use <YOUR FIREBASE>.firebaseio.com instead');
  14746. }
  14747. // Catch common error of uninitialized namespace value.
  14748. if ((!namespace || namespace === 'undefined') &&
  14749. parsedUrl.domain !== 'localhost') {
  14750. fatal('Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com');
  14751. }
  14752. if (!parsedUrl.secure) {
  14753. warnIfPageIsSecure();
  14754. }
  14755. var webSocketOnly = parsedUrl.scheme === 'ws' || parsedUrl.scheme === 'wss';
  14756. return {
  14757. repoInfo: new RepoInfo(parsedUrl.host, parsedUrl.secure, namespace, webSocketOnly, nodeAdmin,
  14758. /*persistenceKey=*/ '',
  14759. /*includeNamespaceInQueryParams=*/ namespace !== parsedUrl.subdomain),
  14760. path: new Path(parsedUrl.pathString)
  14761. };
  14762. };
  14763. var parseDatabaseURL = function (dataURL) {
  14764. // Default to empty strings in the event of a malformed string.
  14765. var host = '', domain = '', subdomain = '', pathString = '', namespace = '';
  14766. // Always default to SSL, unless otherwise specified.
  14767. var secure = true, scheme = 'https', port = 443;
  14768. // Don't do any validation here. The caller is responsible for validating the result of parsing.
  14769. if (typeof dataURL === 'string') {
  14770. // Parse scheme.
  14771. var colonInd = dataURL.indexOf('//');
  14772. if (colonInd >= 0) {
  14773. scheme = dataURL.substring(0, colonInd - 1);
  14774. dataURL = dataURL.substring(colonInd + 2);
  14775. }
  14776. // Parse host, path, and query string.
  14777. var slashInd = dataURL.indexOf('/');
  14778. if (slashInd === -1) {
  14779. slashInd = dataURL.length;
  14780. }
  14781. var questionMarkInd = dataURL.indexOf('?');
  14782. if (questionMarkInd === -1) {
  14783. questionMarkInd = dataURL.length;
  14784. }
  14785. host = dataURL.substring(0, Math.min(slashInd, questionMarkInd));
  14786. if (slashInd < questionMarkInd) {
  14787. // For pathString, questionMarkInd will always come after slashInd
  14788. pathString = decodePath(dataURL.substring(slashInd, questionMarkInd));
  14789. }
  14790. var queryParams = decodeQuery(dataURL.substring(Math.min(dataURL.length, questionMarkInd)));
  14791. // If we have a port, use scheme for determining if it's secure.
  14792. colonInd = host.indexOf(':');
  14793. if (colonInd >= 0) {
  14794. secure = scheme === 'https' || scheme === 'wss';
  14795. port = parseInt(host.substring(colonInd + 1), 10);
  14796. }
  14797. else {
  14798. colonInd = host.length;
  14799. }
  14800. var hostWithoutPort = host.slice(0, colonInd);
  14801. if (hostWithoutPort.toLowerCase() === 'localhost') {
  14802. domain = 'localhost';
  14803. }
  14804. else if (hostWithoutPort.split('.').length <= 2) {
  14805. domain = hostWithoutPort;
  14806. }
  14807. else {
  14808. // Interpret the subdomain of a 3 or more component URL as the namespace name.
  14809. var dotInd = host.indexOf('.');
  14810. subdomain = host.substring(0, dotInd).toLowerCase();
  14811. domain = host.substring(dotInd + 1);
  14812. // Normalize namespaces to lowercase to share storage / connection.
  14813. namespace = subdomain;
  14814. }
  14815. // Always treat the value of the `ns` as the namespace name if it is present.
  14816. if ('ns' in queryParams) {
  14817. namespace = queryParams['ns'];
  14818. }
  14819. }
  14820. return {
  14821. host: host,
  14822. port: port,
  14823. domain: domain,
  14824. subdomain: subdomain,
  14825. secure: secure,
  14826. scheme: scheme,
  14827. pathString: pathString,
  14828. namespace: namespace
  14829. };
  14830. };
  14831. /**
  14832. * @license
  14833. * Copyright 2017 Google LLC
  14834. *
  14835. * Licensed under the Apache License, Version 2.0 (the "License");
  14836. * you may not use this file except in compliance with the License.
  14837. * You may obtain a copy of the License at
  14838. *
  14839. * http://www.apache.org/licenses/LICENSE-2.0
  14840. *
  14841. * Unless required by applicable law or agreed to in writing, software
  14842. * distributed under the License is distributed on an "AS IS" BASIS,
  14843. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14844. * See the License for the specific language governing permissions and
  14845. * limitations under the License.
  14846. */
  14847. // Modeled after base64 web-safe chars, but ordered by ASCII.
  14848. var PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
  14849. /**
  14850. * Fancy ID generator that creates 20-character string identifiers with the
  14851. * following properties:
  14852. *
  14853. * 1. They're based on timestamp so that they sort *after* any existing ids.
  14854. * 2. They contain 72-bits of random data after the timestamp so that IDs won't
  14855. * collide with other clients' IDs.
  14856. * 3. They sort *lexicographically* (so the timestamp is converted to characters
  14857. * that will sort properly).
  14858. * 4. They're monotonically increasing. Even if you generate more than one in
  14859. * the same timestamp, the latter ones will sort after the former ones. We do
  14860. * this by using the previous random bits but "incrementing" them by 1 (only
  14861. * in the case of a timestamp collision).
  14862. */
  14863. var nextPushId = (function () {
  14864. // Timestamp of last push, used to prevent local collisions if you push twice
  14865. // in one ms.
  14866. var lastPushTime = 0;
  14867. // We generate 72-bits of randomness which get turned into 12 characters and
  14868. // appended to the timestamp to prevent collisions with other clients. We
  14869. // store the last characters we generated because in the event of a collision,
  14870. // we'll use those same characters except "incremented" by one.
  14871. var lastRandChars = [];
  14872. return function (now) {
  14873. var duplicateTime = now === lastPushTime;
  14874. lastPushTime = now;
  14875. var i;
  14876. var timeStampChars = new Array(8);
  14877. for (i = 7; i >= 0; i--) {
  14878. timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
  14879. // NOTE: Can't use << here because javascript will convert to int and lose
  14880. // the upper bits.
  14881. now = Math.floor(now / 64);
  14882. }
  14883. util.assert(now === 0, 'Cannot push at time == 0');
  14884. var id = timeStampChars.join('');
  14885. if (!duplicateTime) {
  14886. for (i = 0; i < 12; i++) {
  14887. lastRandChars[i] = Math.floor(Math.random() * 64);
  14888. }
  14889. }
  14890. else {
  14891. // If the timestamp hasn't changed since last push, use the same random
  14892. // number, except incremented by 1.
  14893. for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
  14894. lastRandChars[i] = 0;
  14895. }
  14896. lastRandChars[i]++;
  14897. }
  14898. for (i = 0; i < 12; i++) {
  14899. id += PUSH_CHARS.charAt(lastRandChars[i]);
  14900. }
  14901. util.assert(id.length === 20, 'nextPushId: Length should be 20.');
  14902. return id;
  14903. };
  14904. })();
  14905. /**
  14906. * @license
  14907. * Copyright 2017 Google LLC
  14908. *
  14909. * Licensed under the Apache License, Version 2.0 (the "License");
  14910. * you may not use this file except in compliance with the License.
  14911. * You may obtain a copy of the License at
  14912. *
  14913. * http://www.apache.org/licenses/LICENSE-2.0
  14914. *
  14915. * Unless required by applicable law or agreed to in writing, software
  14916. * distributed under the License is distributed on an "AS IS" BASIS,
  14917. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14918. * See the License for the specific language governing permissions and
  14919. * limitations under the License.
  14920. */
  14921. /**
  14922. * Encapsulates the data needed to raise an event
  14923. */
  14924. var DataEvent = /** @class */ (function () {
  14925. /**
  14926. * @param eventType - One of: value, child_added, child_changed, child_moved, child_removed
  14927. * @param eventRegistration - The function to call to with the event data. User provided
  14928. * @param snapshot - The data backing the event
  14929. * @param prevName - Optional, the name of the previous child for child_* events.
  14930. */
  14931. function DataEvent(eventType, eventRegistration, snapshot, prevName) {
  14932. this.eventType = eventType;
  14933. this.eventRegistration = eventRegistration;
  14934. this.snapshot = snapshot;
  14935. this.prevName = prevName;
  14936. }
  14937. DataEvent.prototype.getPath = function () {
  14938. var ref = this.snapshot.ref;
  14939. if (this.eventType === 'value') {
  14940. return ref._path;
  14941. }
  14942. else {
  14943. return ref.parent._path;
  14944. }
  14945. };
  14946. DataEvent.prototype.getEventType = function () {
  14947. return this.eventType;
  14948. };
  14949. DataEvent.prototype.getEventRunner = function () {
  14950. return this.eventRegistration.getEventRunner(this);
  14951. };
  14952. DataEvent.prototype.toString = function () {
  14953. return (this.getPath().toString() +
  14954. ':' +
  14955. this.eventType +
  14956. ':' +
  14957. util.stringify(this.snapshot.exportVal()));
  14958. };
  14959. return DataEvent;
  14960. }());
  14961. var CancelEvent = /** @class */ (function () {
  14962. function CancelEvent(eventRegistration, error, path) {
  14963. this.eventRegistration = eventRegistration;
  14964. this.error = error;
  14965. this.path = path;
  14966. }
  14967. CancelEvent.prototype.getPath = function () {
  14968. return this.path;
  14969. };
  14970. CancelEvent.prototype.getEventType = function () {
  14971. return 'cancel';
  14972. };
  14973. CancelEvent.prototype.getEventRunner = function () {
  14974. return this.eventRegistration.getEventRunner(this);
  14975. };
  14976. CancelEvent.prototype.toString = function () {
  14977. return this.path.toString() + ':cancel';
  14978. };
  14979. return CancelEvent;
  14980. }());
  14981. /**
  14982. * @license
  14983. * Copyright 2017 Google LLC
  14984. *
  14985. * Licensed under the Apache License, Version 2.0 (the "License");
  14986. * you may not use this file except in compliance with the License.
  14987. * You may obtain a copy of the License at
  14988. *
  14989. * http://www.apache.org/licenses/LICENSE-2.0
  14990. *
  14991. * Unless required by applicable law or agreed to in writing, software
  14992. * distributed under the License is distributed on an "AS IS" BASIS,
  14993. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14994. * See the License for the specific language governing permissions and
  14995. * limitations under the License.
  14996. */
  14997. /**
  14998. * A wrapper class that converts events from the database@exp SDK to the legacy
  14999. * Database SDK. Events are not converted directly as event registration relies
  15000. * on reference comparison of the original user callback (see `matches()`) and
  15001. * relies on equality of the legacy SDK's `context` object.
  15002. */
  15003. var CallbackContext = /** @class */ (function () {
  15004. function CallbackContext(snapshotCallback, cancelCallback) {
  15005. this.snapshotCallback = snapshotCallback;
  15006. this.cancelCallback = cancelCallback;
  15007. }
  15008. CallbackContext.prototype.onValue = function (expDataSnapshot, previousChildName) {
  15009. this.snapshotCallback.call(null, expDataSnapshot, previousChildName);
  15010. };
  15011. CallbackContext.prototype.onCancel = function (error) {
  15012. util.assert(this.hasCancelCallback, 'Raising a cancel event on a listener with no cancel callback');
  15013. return this.cancelCallback.call(null, error);
  15014. };
  15015. Object.defineProperty(CallbackContext.prototype, "hasCancelCallback", {
  15016. get: function () {
  15017. return !!this.cancelCallback;
  15018. },
  15019. enumerable: false,
  15020. configurable: true
  15021. });
  15022. CallbackContext.prototype.matches = function (other) {
  15023. return (this.snapshotCallback === other.snapshotCallback ||
  15024. (this.snapshotCallback.userCallback !== undefined &&
  15025. this.snapshotCallback.userCallback ===
  15026. other.snapshotCallback.userCallback &&
  15027. this.snapshotCallback.context === other.snapshotCallback.context));
  15028. };
  15029. return CallbackContext;
  15030. }());
  15031. /**
  15032. * @license
  15033. * Copyright 2021 Google LLC
  15034. *
  15035. * Licensed under the Apache License, Version 2.0 (the "License");
  15036. * you may not use this file except in compliance with the License.
  15037. * You may obtain a copy of the License at
  15038. *
  15039. * http://www.apache.org/licenses/LICENSE-2.0
  15040. *
  15041. * Unless required by applicable law or agreed to in writing, software
  15042. * distributed under the License is distributed on an "AS IS" BASIS,
  15043. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15044. * See the License for the specific language governing permissions and
  15045. * limitations under the License.
  15046. */
  15047. /**
  15048. * The `onDisconnect` class allows you to write or clear data when your client
  15049. * disconnects from the Database server. These updates occur whether your
  15050. * client disconnects cleanly or not, so you can rely on them to clean up data
  15051. * even if a connection is dropped or a client crashes.
  15052. *
  15053. * The `onDisconnect` class is most commonly used to manage presence in
  15054. * applications where it is useful to detect how many clients are connected and
  15055. * when other clients disconnect. See
  15056. * {@link https://firebase.google.com/docs/database/web/offline-capabilities | Enabling Offline Capabilities in JavaScript}
  15057. * for more information.
  15058. *
  15059. * To avoid problems when a connection is dropped before the requests can be
  15060. * transferred to the Database server, these functions should be called before
  15061. * writing any data.
  15062. *
  15063. * Note that `onDisconnect` operations are only triggered once. If you want an
  15064. * operation to occur each time a disconnect occurs, you'll need to re-establish
  15065. * the `onDisconnect` operations each time you reconnect.
  15066. */
  15067. var OnDisconnect$1 = /** @class */ (function () {
  15068. /** @hideconstructor */
  15069. function OnDisconnect(_repo, _path) {
  15070. this._repo = _repo;
  15071. this._path = _path;
  15072. }
  15073. /**
  15074. * Cancels all previously queued `onDisconnect()` set or update events for this
  15075. * location and all children.
  15076. *
  15077. * If a write has been queued for this location via a `set()` or `update()` at a
  15078. * parent location, the write at this location will be canceled, though writes
  15079. * to sibling locations will still occur.
  15080. *
  15081. * @returns Resolves when synchronization to the server is complete.
  15082. */
  15083. OnDisconnect.prototype.cancel = function () {
  15084. var deferred = new util.Deferred();
  15085. repoOnDisconnectCancel(this._repo, this._path, deferred.wrapCallback(function () { }));
  15086. return deferred.promise;
  15087. };
  15088. /**
  15089. * Ensures the data at this location is deleted when the client is disconnected
  15090. * (due to closing the browser, navigating to a new page, or network issues).
  15091. *
  15092. * @returns Resolves when synchronization to the server is complete.
  15093. */
  15094. OnDisconnect.prototype.remove = function () {
  15095. validateWritablePath('OnDisconnect.remove', this._path);
  15096. var deferred = new util.Deferred();
  15097. repoOnDisconnectSet(this._repo, this._path, null, deferred.wrapCallback(function () { }));
  15098. return deferred.promise;
  15099. };
  15100. /**
  15101. * Ensures the data at this location is set to the specified value when the
  15102. * client is disconnected (due to closing the browser, navigating to a new page,
  15103. * or network issues).
  15104. *
  15105. * `set()` is especially useful for implementing "presence" systems, where a
  15106. * value should be changed or cleared when a user disconnects so that they
  15107. * appear "offline" to other users. See
  15108. * {@link https://firebase.google.com/docs/database/web/offline-capabilities | Enabling Offline Capabilities in JavaScript}
  15109. * for more information.
  15110. *
  15111. * Note that `onDisconnect` operations are only triggered once. If you want an
  15112. * operation to occur each time a disconnect occurs, you'll need to re-establish
  15113. * the `onDisconnect` operations each time.
  15114. *
  15115. * @param value - The value to be written to this location on disconnect (can
  15116. * be an object, array, string, number, boolean, or null).
  15117. * @returns Resolves when synchronization to the Database is complete.
  15118. */
  15119. OnDisconnect.prototype.set = function (value) {
  15120. validateWritablePath('OnDisconnect.set', this._path);
  15121. validateFirebaseDataArg('OnDisconnect.set', value, this._path, false);
  15122. var deferred = new util.Deferred();
  15123. repoOnDisconnectSet(this._repo, this._path, value, deferred.wrapCallback(function () { }));
  15124. return deferred.promise;
  15125. };
  15126. /**
  15127. * Ensures the data at this location is set to the specified value and priority
  15128. * when the client is disconnected (due to closing the browser, navigating to a
  15129. * new page, or network issues).
  15130. *
  15131. * @param value - The value to be written to this location on disconnect (can
  15132. * be an object, array, string, number, boolean, or null).
  15133. * @param priority - The priority to be written (string, number, or null).
  15134. * @returns Resolves when synchronization to the Database is complete.
  15135. */
  15136. OnDisconnect.prototype.setWithPriority = function (value, priority) {
  15137. validateWritablePath('OnDisconnect.setWithPriority', this._path);
  15138. validateFirebaseDataArg('OnDisconnect.setWithPriority', value, this._path, false);
  15139. validatePriority('OnDisconnect.setWithPriority', priority, false);
  15140. var deferred = new util.Deferred();
  15141. repoOnDisconnectSetWithPriority(this._repo, this._path, value, priority, deferred.wrapCallback(function () { }));
  15142. return deferred.promise;
  15143. };
  15144. /**
  15145. * Writes multiple values at this location when the client is disconnected (due
  15146. * to closing the browser, navigating to a new page, or network issues).
  15147. *
  15148. * The `values` argument contains multiple property-value pairs that will be
  15149. * written to the Database together. Each child property can either be a simple
  15150. * property (for example, "name") or a relative path (for example, "name/first")
  15151. * from the current location to the data to update.
  15152. *
  15153. * As opposed to the `set()` method, `update()` can be use to selectively update
  15154. * only the referenced properties at the current location (instead of replacing
  15155. * all the child properties at the current location).
  15156. *
  15157. * @param values - Object containing multiple values.
  15158. * @returns Resolves when synchronization to the Database is complete.
  15159. */
  15160. OnDisconnect.prototype.update = function (values) {
  15161. validateWritablePath('OnDisconnect.update', this._path);
  15162. validateFirebaseMergeDataArg('OnDisconnect.update', values, this._path, false);
  15163. var deferred = new util.Deferred();
  15164. repoOnDisconnectUpdate(this._repo, this._path, values, deferred.wrapCallback(function () { }));
  15165. return deferred.promise;
  15166. };
  15167. return OnDisconnect;
  15168. }());
  15169. /**
  15170. * @license
  15171. * Copyright 2020 Google LLC
  15172. *
  15173. * Licensed under the Apache License, Version 2.0 (the "License");
  15174. * you may not use this file except in compliance with the License.
  15175. * You may obtain a copy of the License at
  15176. *
  15177. * http://www.apache.org/licenses/LICENSE-2.0
  15178. *
  15179. * Unless required by applicable law or agreed to in writing, software
  15180. * distributed under the License is distributed on an "AS IS" BASIS,
  15181. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15182. * See the License for the specific language governing permissions and
  15183. * limitations under the License.
  15184. */
  15185. /**
  15186. * @internal
  15187. */
  15188. var QueryImpl = /** @class */ (function () {
  15189. /**
  15190. * @hideconstructor
  15191. */
  15192. function QueryImpl(_repo, _path, _queryParams, _orderByCalled) {
  15193. this._repo = _repo;
  15194. this._path = _path;
  15195. this._queryParams = _queryParams;
  15196. this._orderByCalled = _orderByCalled;
  15197. }
  15198. Object.defineProperty(QueryImpl.prototype, "key", {
  15199. get: function () {
  15200. if (pathIsEmpty(this._path)) {
  15201. return null;
  15202. }
  15203. else {
  15204. return pathGetBack(this._path);
  15205. }
  15206. },
  15207. enumerable: false,
  15208. configurable: true
  15209. });
  15210. Object.defineProperty(QueryImpl.prototype, "ref", {
  15211. get: function () {
  15212. return new ReferenceImpl(this._repo, this._path);
  15213. },
  15214. enumerable: false,
  15215. configurable: true
  15216. });
  15217. Object.defineProperty(QueryImpl.prototype, "_queryIdentifier", {
  15218. get: function () {
  15219. var obj = queryParamsGetQueryObject(this._queryParams);
  15220. var id = ObjectToUniqueKey(obj);
  15221. return id === '{}' ? 'default' : id;
  15222. },
  15223. enumerable: false,
  15224. configurable: true
  15225. });
  15226. Object.defineProperty(QueryImpl.prototype, "_queryObject", {
  15227. /**
  15228. * An object representation of the query parameters used by this Query.
  15229. */
  15230. get: function () {
  15231. return queryParamsGetQueryObject(this._queryParams);
  15232. },
  15233. enumerable: false,
  15234. configurable: true
  15235. });
  15236. QueryImpl.prototype.isEqual = function (other) {
  15237. other = util.getModularInstance(other);
  15238. if (!(other instanceof QueryImpl)) {
  15239. return false;
  15240. }
  15241. var sameRepo = this._repo === other._repo;
  15242. var samePath = pathEquals(this._path, other._path);
  15243. var sameQueryIdentifier = this._queryIdentifier === other._queryIdentifier;
  15244. return sameRepo && samePath && sameQueryIdentifier;
  15245. };
  15246. QueryImpl.prototype.toJSON = function () {
  15247. return this.toString();
  15248. };
  15249. QueryImpl.prototype.toString = function () {
  15250. return this._repo.toString() + pathToUrlEncodedString(this._path);
  15251. };
  15252. return QueryImpl;
  15253. }());
  15254. /**
  15255. * Validates that no other order by call has been made
  15256. */
  15257. function validateNoPreviousOrderByCall(query, fnName) {
  15258. if (query._orderByCalled === true) {
  15259. throw new Error(fnName + ": You can't combine multiple orderBy calls.");
  15260. }
  15261. }
  15262. /**
  15263. * Validates start/end values for queries.
  15264. */
  15265. function validateQueryEndpoints(params) {
  15266. var startNode = null;
  15267. var endNode = null;
  15268. if (params.hasStart()) {
  15269. startNode = params.getIndexStartValue();
  15270. }
  15271. if (params.hasEnd()) {
  15272. endNode = params.getIndexEndValue();
  15273. }
  15274. if (params.getIndex() === KEY_INDEX) {
  15275. var tooManyArgsError = 'Query: When ordering by key, you may only pass one argument to ' +
  15276. 'startAt(), endAt(), or equalTo().';
  15277. var wrongArgTypeError = 'Query: When ordering by key, the argument passed to startAt(), startAfter(), ' +
  15278. 'endAt(), endBefore(), or equalTo() must be a string.';
  15279. if (params.hasStart()) {
  15280. var startName = params.getIndexStartName();
  15281. if (startName !== MIN_NAME) {
  15282. throw new Error(tooManyArgsError);
  15283. }
  15284. else if (typeof startNode !== 'string') {
  15285. throw new Error(wrongArgTypeError);
  15286. }
  15287. }
  15288. if (params.hasEnd()) {
  15289. var endName = params.getIndexEndName();
  15290. if (endName !== MAX_NAME) {
  15291. throw new Error(tooManyArgsError);
  15292. }
  15293. else if (typeof endNode !== 'string') {
  15294. throw new Error(wrongArgTypeError);
  15295. }
  15296. }
  15297. }
  15298. else if (params.getIndex() === PRIORITY_INDEX) {
  15299. if ((startNode != null && !isValidPriority(startNode)) ||
  15300. (endNode != null && !isValidPriority(endNode))) {
  15301. throw new Error('Query: When ordering by priority, the first argument passed to startAt(), ' +
  15302. 'startAfter() endAt(), endBefore(), or equalTo() must be a valid priority value ' +
  15303. '(null, a number, or a string).');
  15304. }
  15305. }
  15306. else {
  15307. util.assert(params.getIndex() instanceof PathIndex ||
  15308. params.getIndex() === VALUE_INDEX, 'unknown index type.');
  15309. if ((startNode != null && typeof startNode === 'object') ||
  15310. (endNode != null && typeof endNode === 'object')) {
  15311. throw new Error('Query: First argument passed to startAt(), startAfter(), endAt(), endBefore(), or ' +
  15312. 'equalTo() cannot be an object.');
  15313. }
  15314. }
  15315. }
  15316. /**
  15317. * Validates that limit* has been called with the correct combination of parameters
  15318. */
  15319. function validateLimit(params) {
  15320. if (params.hasStart() &&
  15321. params.hasEnd() &&
  15322. params.hasLimit() &&
  15323. !params.hasAnchoredLimit()) {
  15324. throw new Error("Query: Can't combine startAt(), startAfter(), endAt(), endBefore(), and limit(). Use " +
  15325. 'limitToFirst() or limitToLast() instead.');
  15326. }
  15327. }
  15328. /**
  15329. * @internal
  15330. */
  15331. var ReferenceImpl = /** @class */ (function (_super) {
  15332. tslib.__extends(ReferenceImpl, _super);
  15333. /** @hideconstructor */
  15334. function ReferenceImpl(repo, path) {
  15335. return _super.call(this, repo, path, new QueryParams(), false) || this;
  15336. }
  15337. Object.defineProperty(ReferenceImpl.prototype, "parent", {
  15338. get: function () {
  15339. var parentPath = pathParent(this._path);
  15340. return parentPath === null
  15341. ? null
  15342. : new ReferenceImpl(this._repo, parentPath);
  15343. },
  15344. enumerable: false,
  15345. configurable: true
  15346. });
  15347. Object.defineProperty(ReferenceImpl.prototype, "root", {
  15348. get: function () {
  15349. var ref = this;
  15350. while (ref.parent !== null) {
  15351. ref = ref.parent;
  15352. }
  15353. return ref;
  15354. },
  15355. enumerable: false,
  15356. configurable: true
  15357. });
  15358. return ReferenceImpl;
  15359. }(QueryImpl));
  15360. /**
  15361. * A `DataSnapshot` contains data from a Database location.
  15362. *
  15363. * Any time you read data from the Database, you receive the data as a
  15364. * `DataSnapshot`. A `DataSnapshot` is passed to the event callbacks you attach
  15365. * with `on()` or `once()`. You can extract the contents of the snapshot as a
  15366. * JavaScript object by calling the `val()` method. Alternatively, you can
  15367. * traverse into the snapshot by calling `child()` to return child snapshots
  15368. * (which you could then call `val()` on).
  15369. *
  15370. * A `DataSnapshot` is an efficiently generated, immutable copy of the data at
  15371. * a Database location. It cannot be modified and will never change (to modify
  15372. * data, you always call the `set()` method on a `Reference` directly).
  15373. */
  15374. var DataSnapshot$1 = /** @class */ (function () {
  15375. /**
  15376. * @param _node - A SnapshotNode to wrap.
  15377. * @param ref - The location this snapshot came from.
  15378. * @param _index - The iteration order for this snapshot
  15379. * @hideconstructor
  15380. */
  15381. function DataSnapshot(_node,
  15382. /**
  15383. * The location of this DataSnapshot.
  15384. */
  15385. ref, _index) {
  15386. this._node = _node;
  15387. this.ref = ref;
  15388. this._index = _index;
  15389. }
  15390. Object.defineProperty(DataSnapshot.prototype, "priority", {
  15391. /**
  15392. * Gets the priority value of the data in this `DataSnapshot`.
  15393. *
  15394. * Applications need not use priority but can order collections by
  15395. * ordinary properties (see
  15396. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data |Sorting and filtering data}
  15397. * ).
  15398. */
  15399. get: function () {
  15400. // typecast here because we never return deferred values or internal priorities (MAX_PRIORITY)
  15401. return this._node.getPriority().val();
  15402. },
  15403. enumerable: false,
  15404. configurable: true
  15405. });
  15406. Object.defineProperty(DataSnapshot.prototype, "key", {
  15407. /**
  15408. * The key (last part of the path) of the location of this `DataSnapshot`.
  15409. *
  15410. * The last token in a Database location is considered its key. For example,
  15411. * "ada" is the key for the /users/ada/ node. Accessing the key on any
  15412. * `DataSnapshot` will return the key for the location that generated it.
  15413. * However, accessing the key on the root URL of a Database will return
  15414. * `null`.
  15415. */
  15416. get: function () {
  15417. return this.ref.key;
  15418. },
  15419. enumerable: false,
  15420. configurable: true
  15421. });
  15422. Object.defineProperty(DataSnapshot.prototype, "size", {
  15423. /** Returns the number of child properties of this `DataSnapshot`. */
  15424. get: function () {
  15425. return this._node.numChildren();
  15426. },
  15427. enumerable: false,
  15428. configurable: true
  15429. });
  15430. /**
  15431. * Gets another `DataSnapshot` for the location at the specified relative path.
  15432. *
  15433. * Passing a relative path to the `child()` method of a DataSnapshot returns
  15434. * another `DataSnapshot` for the location at the specified relative path. The
  15435. * relative path can either be a simple child name (for example, "ada") or a
  15436. * deeper, slash-separated path (for example, "ada/name/first"). If the child
  15437. * location has no data, an empty `DataSnapshot` (that is, a `DataSnapshot`
  15438. * whose value is `null`) is returned.
  15439. *
  15440. * @param path - A relative path to the location of child data.
  15441. */
  15442. DataSnapshot.prototype.child = function (path) {
  15443. var childPath = new Path(path);
  15444. var childRef = child(this.ref, path);
  15445. return new DataSnapshot(this._node.getChild(childPath), childRef, PRIORITY_INDEX);
  15446. };
  15447. /**
  15448. * Returns true if this `DataSnapshot` contains any data. It is slightly more
  15449. * efficient than using `snapshot.val() !== null`.
  15450. */
  15451. DataSnapshot.prototype.exists = function () {
  15452. return !this._node.isEmpty();
  15453. };
  15454. /**
  15455. * Exports the entire contents of the DataSnapshot as a JavaScript object.
  15456. *
  15457. * The `exportVal()` method is similar to `val()`, except priority information
  15458. * is included (if available), making it suitable for backing up your data.
  15459. *
  15460. * @returns The DataSnapshot's contents as a JavaScript value (Object,
  15461. * Array, string, number, boolean, or `null`).
  15462. */
  15463. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  15464. DataSnapshot.prototype.exportVal = function () {
  15465. return this._node.val(true);
  15466. };
  15467. /**
  15468. * Enumerates the top-level children in the `DataSnapshot`.
  15469. *
  15470. * Because of the way JavaScript objects work, the ordering of data in the
  15471. * JavaScript object returned by `val()` is not guaranteed to match the
  15472. * ordering on the server nor the ordering of `onChildAdded()` events. That is
  15473. * where `forEach()` comes in handy. It guarantees the children of a
  15474. * `DataSnapshot` will be iterated in their query order.
  15475. *
  15476. * If no explicit `orderBy*()` method is used, results are returned
  15477. * ordered by key (unless priorities are used, in which case, results are
  15478. * returned by priority).
  15479. *
  15480. * @param action - A function that will be called for each child DataSnapshot.
  15481. * The callback can return true to cancel further enumeration.
  15482. * @returns true if enumeration was canceled due to your callback returning
  15483. * true.
  15484. */
  15485. DataSnapshot.prototype.forEach = function (action) {
  15486. var _this = this;
  15487. if (this._node.isLeafNode()) {
  15488. return false;
  15489. }
  15490. var childrenNode = this._node;
  15491. // Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
  15492. return !!childrenNode.forEachChild(this._index, function (key, node) {
  15493. return action(new DataSnapshot(node, child(_this.ref, key), PRIORITY_INDEX));
  15494. });
  15495. };
  15496. /**
  15497. * Returns true if the specified child path has (non-null) data.
  15498. *
  15499. * @param path - A relative path to the location of a potential child.
  15500. * @returns `true` if data exists at the specified child path; else
  15501. * `false`.
  15502. */
  15503. DataSnapshot.prototype.hasChild = function (path) {
  15504. var childPath = new Path(path);
  15505. return !this._node.getChild(childPath).isEmpty();
  15506. };
  15507. /**
  15508. * Returns whether or not the `DataSnapshot` has any non-`null` child
  15509. * properties.
  15510. *
  15511. * You can use `hasChildren()` to determine if a `DataSnapshot` has any
  15512. * children. If it does, you can enumerate them using `forEach()`. If it
  15513. * doesn't, then either this snapshot contains a primitive value (which can be
  15514. * retrieved with `val()`) or it is empty (in which case, `val()` will return
  15515. * `null`).
  15516. *
  15517. * @returns true if this snapshot has any children; else false.
  15518. */
  15519. DataSnapshot.prototype.hasChildren = function () {
  15520. if (this._node.isLeafNode()) {
  15521. return false;
  15522. }
  15523. else {
  15524. return !this._node.isEmpty();
  15525. }
  15526. };
  15527. /**
  15528. * Returns a JSON-serializable representation of this object.
  15529. */
  15530. DataSnapshot.prototype.toJSON = function () {
  15531. return this.exportVal();
  15532. };
  15533. /**
  15534. * Extracts a JavaScript value from a `DataSnapshot`.
  15535. *
  15536. * Depending on the data in a `DataSnapshot`, the `val()` method may return a
  15537. * scalar type (string, number, or boolean), an array, or an object. It may
  15538. * also return null, indicating that the `DataSnapshot` is empty (contains no
  15539. * data).
  15540. *
  15541. * @returns The DataSnapshot's contents as a JavaScript value (Object,
  15542. * Array, string, number, boolean, or `null`).
  15543. */
  15544. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  15545. DataSnapshot.prototype.val = function () {
  15546. return this._node.val();
  15547. };
  15548. return DataSnapshot;
  15549. }());
  15550. /**
  15551. *
  15552. * Returns a `Reference` representing the location in the Database
  15553. * corresponding to the provided path. If no path is provided, the `Reference`
  15554. * will point to the root of the Database.
  15555. *
  15556. * @param db - The database instance to obtain a reference for.
  15557. * @param path - Optional path representing the location the returned
  15558. * `Reference` will point. If not provided, the returned `Reference` will
  15559. * point to the root of the Database.
  15560. * @returns If a path is provided, a `Reference`
  15561. * pointing to the provided path. Otherwise, a `Reference` pointing to the
  15562. * root of the Database.
  15563. */
  15564. function ref(db, path) {
  15565. db = util.getModularInstance(db);
  15566. db._checkNotDeleted('ref');
  15567. return path !== undefined ? child(db._root, path) : db._root;
  15568. }
  15569. /**
  15570. * Returns a `Reference` representing the location in the Database
  15571. * corresponding to the provided Firebase URL.
  15572. *
  15573. * An exception is thrown if the URL is not a valid Firebase Database URL or it
  15574. * has a different domain than the current `Database` instance.
  15575. *
  15576. * Note that all query parameters (`orderBy`, `limitToLast`, etc.) are ignored
  15577. * and are not applied to the returned `Reference`.
  15578. *
  15579. * @param db - The database instance to obtain a reference for.
  15580. * @param url - The Firebase URL at which the returned `Reference` will
  15581. * point.
  15582. * @returns A `Reference` pointing to the provided
  15583. * Firebase URL.
  15584. */
  15585. function refFromURL(db, url) {
  15586. db = util.getModularInstance(db);
  15587. db._checkNotDeleted('refFromURL');
  15588. var parsedURL = parseRepoInfo(url, db._repo.repoInfo_.nodeAdmin);
  15589. validateUrl('refFromURL', parsedURL);
  15590. var repoInfo = parsedURL.repoInfo;
  15591. if (!db._repo.repoInfo_.isCustomHost() &&
  15592. repoInfo.host !== db._repo.repoInfo_.host) {
  15593. fatal('refFromURL' +
  15594. ': Host name does not match the current database: ' +
  15595. '(found ' +
  15596. repoInfo.host +
  15597. ' but expected ' +
  15598. db._repo.repoInfo_.host +
  15599. ')');
  15600. }
  15601. return ref(db, parsedURL.path.toString());
  15602. }
  15603. /**
  15604. * Gets a `Reference` for the location at the specified relative path.
  15605. *
  15606. * The relative path can either be a simple child name (for example, "ada") or
  15607. * a deeper slash-separated path (for example, "ada/name/first").
  15608. *
  15609. * @param parent - The parent location.
  15610. * @param path - A relative path from this location to the desired child
  15611. * location.
  15612. * @returns The specified child location.
  15613. */
  15614. function child(parent, path) {
  15615. parent = util.getModularInstance(parent);
  15616. if (pathGetFront(parent._path) === null) {
  15617. validateRootPathString('child', 'path', path, false);
  15618. }
  15619. else {
  15620. validatePathString('child', 'path', path, false);
  15621. }
  15622. return new ReferenceImpl(parent._repo, pathChild(parent._path, path));
  15623. }
  15624. /**
  15625. * Returns an `OnDisconnect` object - see
  15626. * {@link https://firebase.google.com/docs/database/web/offline-capabilities | Enabling Offline Capabilities in JavaScript}
  15627. * for more information on how to use it.
  15628. *
  15629. * @param ref - The reference to add OnDisconnect triggers for.
  15630. */
  15631. function onDisconnect(ref) {
  15632. ref = util.getModularInstance(ref);
  15633. return new OnDisconnect$1(ref._repo, ref._path);
  15634. }
  15635. /**
  15636. * Generates a new child location using a unique key and returns its
  15637. * `Reference`.
  15638. *
  15639. * This is the most common pattern for adding data to a collection of items.
  15640. *
  15641. * If you provide a value to `push()`, the value is written to the
  15642. * generated location. If you don't pass a value, nothing is written to the
  15643. * database and the child remains empty (but you can use the `Reference`
  15644. * elsewhere).
  15645. *
  15646. * The unique keys generated by `push()` are ordered by the current time, so the
  15647. * resulting list of items is chronologically sorted. The keys are also
  15648. * designed to be unguessable (they contain 72 random bits of entropy).
  15649. *
  15650. * See {@link https://firebase.google.com/docs/database/web/lists-of-data#append_to_a_list_of_data | Append to a list of data}.
  15651. * See {@link https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html | The 2^120 Ways to Ensure Unique Identifiers}.
  15652. *
  15653. * @param parent - The parent location.
  15654. * @param value - Optional value to be written at the generated location.
  15655. * @returns Combined `Promise` and `Reference`; resolves when write is complete,
  15656. * but can be used immediately as the `Reference` to the child location.
  15657. */
  15658. function push(parent, value) {
  15659. parent = util.getModularInstance(parent);
  15660. validateWritablePath('push', parent._path);
  15661. validateFirebaseDataArg('push', value, parent._path, true);
  15662. var now = repoServerTime(parent._repo);
  15663. var name = nextPushId(now);
  15664. // push() returns a ThennableReference whose promise is fulfilled with a
  15665. // regular Reference. We use child() to create handles to two different
  15666. // references. The first is turned into a ThennableReference below by adding
  15667. // then() and catch() methods and is used as the return value of push(). The
  15668. // second remains a regular Reference and is used as the fulfilled value of
  15669. // the first ThennableReference.
  15670. var thennablePushRef = child(parent, name);
  15671. var pushRef = child(parent, name);
  15672. var promise;
  15673. if (value != null) {
  15674. promise = set(pushRef, value).then(function () { return pushRef; });
  15675. }
  15676. else {
  15677. promise = Promise.resolve(pushRef);
  15678. }
  15679. thennablePushRef.then = promise.then.bind(promise);
  15680. thennablePushRef.catch = promise.then.bind(promise, undefined);
  15681. return thennablePushRef;
  15682. }
  15683. /**
  15684. * Removes the data at this Database location.
  15685. *
  15686. * Any data at child locations will also be deleted.
  15687. *
  15688. * The effect of the remove will be visible immediately and the corresponding
  15689. * event 'value' will be triggered. Synchronization of the remove to the
  15690. * Firebase servers will also be started, and the returned Promise will resolve
  15691. * when complete. If provided, the onComplete callback will be called
  15692. * asynchronously after synchronization has finished.
  15693. *
  15694. * @param ref - The location to remove.
  15695. * @returns Resolves when remove on server is complete.
  15696. */
  15697. function remove(ref) {
  15698. validateWritablePath('remove', ref._path);
  15699. return set(ref, null);
  15700. }
  15701. /**
  15702. * Writes data to this Database location.
  15703. *
  15704. * This will overwrite any data at this location and all child locations.
  15705. *
  15706. * The effect of the write will be visible immediately, and the corresponding
  15707. * events ("value", "child_added", etc.) will be triggered. Synchronization of
  15708. * the data to the Firebase servers will also be started, and the returned
  15709. * Promise will resolve when complete. If provided, the `onComplete` callback
  15710. * will be called asynchronously after synchronization has finished.
  15711. *
  15712. * Passing `null` for the new value is equivalent to calling `remove()`; namely,
  15713. * all data at this location and all child locations will be deleted.
  15714. *
  15715. * `set()` will remove any priority stored at this location, so if priority is
  15716. * meant to be preserved, you need to use `setWithPriority()` instead.
  15717. *
  15718. * Note that modifying data with `set()` will cancel any pending transactions
  15719. * at that location, so extreme care should be taken if mixing `set()` and
  15720. * `transaction()` to modify the same data.
  15721. *
  15722. * A single `set()` will generate a single "value" event at the location where
  15723. * the `set()` was performed.
  15724. *
  15725. * @param ref - The location to write to.
  15726. * @param value - The value to be written (string, number, boolean, object,
  15727. * array, or null).
  15728. * @returns Resolves when write to server is complete.
  15729. */
  15730. function set(ref, value) {
  15731. ref = util.getModularInstance(ref);
  15732. validateWritablePath('set', ref._path);
  15733. validateFirebaseDataArg('set', value, ref._path, false);
  15734. var deferred = new util.Deferred();
  15735. repoSetWithPriority(ref._repo, ref._path, value,
  15736. /*priority=*/ null, deferred.wrapCallback(function () { }));
  15737. return deferred.promise;
  15738. }
  15739. /**
  15740. * Sets a priority for the data at this Database location.
  15741. *
  15742. * Applications need not use priority but can order collections by
  15743. * ordinary properties (see
  15744. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data | Sorting and filtering data}
  15745. * ).
  15746. *
  15747. * @param ref - The location to write to.
  15748. * @param priority - The priority to be written (string, number, or null).
  15749. * @returns Resolves when write to server is complete.
  15750. */
  15751. function setPriority(ref, priority) {
  15752. ref = util.getModularInstance(ref);
  15753. validateWritablePath('setPriority', ref._path);
  15754. validatePriority('setPriority', priority, false);
  15755. var deferred = new util.Deferred();
  15756. repoSetWithPriority(ref._repo, pathChild(ref._path, '.priority'), priority, null, deferred.wrapCallback(function () { }));
  15757. return deferred.promise;
  15758. }
  15759. /**
  15760. * Writes data the Database location. Like `set()` but also specifies the
  15761. * priority for that data.
  15762. *
  15763. * Applications need not use priority but can order collections by
  15764. * ordinary properties (see
  15765. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data | Sorting and filtering data}
  15766. * ).
  15767. *
  15768. * @param ref - The location to write to.
  15769. * @param value - The value to be written (string, number, boolean, object,
  15770. * array, or null).
  15771. * @param priority - The priority to be written (string, number, or null).
  15772. * @returns Resolves when write to server is complete.
  15773. */
  15774. function setWithPriority(ref, value, priority) {
  15775. validateWritablePath('setWithPriority', ref._path);
  15776. validateFirebaseDataArg('setWithPriority', value, ref._path, false);
  15777. validatePriority('setWithPriority', priority, false);
  15778. if (ref.key === '.length' || ref.key === '.keys') {
  15779. throw 'setWithPriority failed: ' + ref.key + ' is a read-only object.';
  15780. }
  15781. var deferred = new util.Deferred();
  15782. repoSetWithPriority(ref._repo, ref._path, value, priority, deferred.wrapCallback(function () { }));
  15783. return deferred.promise;
  15784. }
  15785. /**
  15786. * Writes multiple values to the Database at once.
  15787. *
  15788. * The `values` argument contains multiple property-value pairs that will be
  15789. * written to the Database together. Each child property can either be a simple
  15790. * property (for example, "name") or a relative path (for example,
  15791. * "name/first") from the current location to the data to update.
  15792. *
  15793. * As opposed to the `set()` method, `update()` can be use to selectively update
  15794. * only the referenced properties at the current location (instead of replacing
  15795. * all the child properties at the current location).
  15796. *
  15797. * The effect of the write will be visible immediately, and the corresponding
  15798. * events ('value', 'child_added', etc.) will be triggered. Synchronization of
  15799. * the data to the Firebase servers will also be started, and the returned
  15800. * Promise will resolve when complete. If provided, the `onComplete` callback
  15801. * will be called asynchronously after synchronization has finished.
  15802. *
  15803. * A single `update()` will generate a single "value" event at the location
  15804. * where the `update()` was performed, regardless of how many children were
  15805. * modified.
  15806. *
  15807. * Note that modifying data with `update()` will cancel any pending
  15808. * transactions at that location, so extreme care should be taken if mixing
  15809. * `update()` and `transaction()` to modify the same data.
  15810. *
  15811. * Passing `null` to `update()` will remove the data at this location.
  15812. *
  15813. * See
  15814. * {@link https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html | Introducing multi-location updates and more}.
  15815. *
  15816. * @param ref - The location to write to.
  15817. * @param values - Object containing multiple values.
  15818. * @returns Resolves when update on server is complete.
  15819. */
  15820. function update(ref, values) {
  15821. validateFirebaseMergeDataArg('update', values, ref._path, false);
  15822. var deferred = new util.Deferred();
  15823. repoUpdate(ref._repo, ref._path, values, deferred.wrapCallback(function () { }));
  15824. return deferred.promise;
  15825. }
  15826. /**
  15827. * Gets the most up-to-date result for this query.
  15828. *
  15829. * @param query - The query to run.
  15830. * @returns A `Promise` which resolves to the resulting DataSnapshot if a value is
  15831. * available, or rejects if the client is unable to return a value (e.g., if the
  15832. * server is unreachable and there is nothing cached).
  15833. */
  15834. function get(query) {
  15835. query = util.getModularInstance(query);
  15836. var callbackContext = new CallbackContext(function () { });
  15837. var container = new ValueEventRegistration(callbackContext);
  15838. return repoGetValue(query._repo, query, container).then(function (node) {
  15839. return new DataSnapshot$1(node, new ReferenceImpl(query._repo, query._path), query._queryParams.getIndex());
  15840. });
  15841. }
  15842. /**
  15843. * Represents registration for 'value' events.
  15844. */
  15845. var ValueEventRegistration = /** @class */ (function () {
  15846. function ValueEventRegistration(callbackContext) {
  15847. this.callbackContext = callbackContext;
  15848. }
  15849. ValueEventRegistration.prototype.respondsTo = function (eventType) {
  15850. return eventType === 'value';
  15851. };
  15852. ValueEventRegistration.prototype.createEvent = function (change, query) {
  15853. var index = query._queryParams.getIndex();
  15854. return new DataEvent('value', this, new DataSnapshot$1(change.snapshotNode, new ReferenceImpl(query._repo, query._path), index));
  15855. };
  15856. ValueEventRegistration.prototype.getEventRunner = function (eventData) {
  15857. var _this = this;
  15858. if (eventData.getEventType() === 'cancel') {
  15859. return function () {
  15860. return _this.callbackContext.onCancel(eventData.error);
  15861. };
  15862. }
  15863. else {
  15864. return function () {
  15865. return _this.callbackContext.onValue(eventData.snapshot, null);
  15866. };
  15867. }
  15868. };
  15869. ValueEventRegistration.prototype.createCancelEvent = function (error, path) {
  15870. if (this.callbackContext.hasCancelCallback) {
  15871. return new CancelEvent(this, error, path);
  15872. }
  15873. else {
  15874. return null;
  15875. }
  15876. };
  15877. ValueEventRegistration.prototype.matches = function (other) {
  15878. if (!(other instanceof ValueEventRegistration)) {
  15879. return false;
  15880. }
  15881. else if (!other.callbackContext || !this.callbackContext) {
  15882. // If no callback specified, we consider it to match any callback.
  15883. return true;
  15884. }
  15885. else {
  15886. return other.callbackContext.matches(this.callbackContext);
  15887. }
  15888. };
  15889. ValueEventRegistration.prototype.hasAnyCallback = function () {
  15890. return this.callbackContext !== null;
  15891. };
  15892. return ValueEventRegistration;
  15893. }());
  15894. /**
  15895. * Represents the registration of a child_x event.
  15896. */
  15897. var ChildEventRegistration = /** @class */ (function () {
  15898. function ChildEventRegistration(eventType, callbackContext) {
  15899. this.eventType = eventType;
  15900. this.callbackContext = callbackContext;
  15901. }
  15902. ChildEventRegistration.prototype.respondsTo = function (eventType) {
  15903. var eventToCheck = eventType === 'children_added' ? 'child_added' : eventType;
  15904. eventToCheck =
  15905. eventToCheck === 'children_removed' ? 'child_removed' : eventToCheck;
  15906. return this.eventType === eventToCheck;
  15907. };
  15908. ChildEventRegistration.prototype.createCancelEvent = function (error, path) {
  15909. if (this.callbackContext.hasCancelCallback) {
  15910. return new CancelEvent(this, error, path);
  15911. }
  15912. else {
  15913. return null;
  15914. }
  15915. };
  15916. ChildEventRegistration.prototype.createEvent = function (change, query) {
  15917. util.assert(change.childName != null, 'Child events should have a childName.');
  15918. var childRef = child(new ReferenceImpl(query._repo, query._path), change.childName);
  15919. var index = query._queryParams.getIndex();
  15920. return new DataEvent(change.type, this, new DataSnapshot$1(change.snapshotNode, childRef, index), change.prevName);
  15921. };
  15922. ChildEventRegistration.prototype.getEventRunner = function (eventData) {
  15923. var _this = this;
  15924. if (eventData.getEventType() === 'cancel') {
  15925. return function () {
  15926. return _this.callbackContext.onCancel(eventData.error);
  15927. };
  15928. }
  15929. else {
  15930. return function () {
  15931. return _this.callbackContext.onValue(eventData.snapshot, eventData.prevName);
  15932. };
  15933. }
  15934. };
  15935. ChildEventRegistration.prototype.matches = function (other) {
  15936. if (other instanceof ChildEventRegistration) {
  15937. return (this.eventType === other.eventType &&
  15938. (!this.callbackContext ||
  15939. !other.callbackContext ||
  15940. this.callbackContext.matches(other.callbackContext)));
  15941. }
  15942. return false;
  15943. };
  15944. ChildEventRegistration.prototype.hasAnyCallback = function () {
  15945. return !!this.callbackContext;
  15946. };
  15947. return ChildEventRegistration;
  15948. }());
  15949. function addEventListener(query, eventType, callback, cancelCallbackOrListenOptions, options) {
  15950. var cancelCallback;
  15951. if (typeof cancelCallbackOrListenOptions === 'object') {
  15952. cancelCallback = undefined;
  15953. options = cancelCallbackOrListenOptions;
  15954. }
  15955. if (typeof cancelCallbackOrListenOptions === 'function') {
  15956. cancelCallback = cancelCallbackOrListenOptions;
  15957. }
  15958. if (options && options.onlyOnce) {
  15959. var userCallback_1 = callback;
  15960. var onceCallback = function (dataSnapshot, previousChildName) {
  15961. repoRemoveEventCallbackForQuery(query._repo, query, container);
  15962. userCallback_1(dataSnapshot, previousChildName);
  15963. };
  15964. onceCallback.userCallback = callback.userCallback;
  15965. onceCallback.context = callback.context;
  15966. callback = onceCallback;
  15967. }
  15968. var callbackContext = new CallbackContext(callback, cancelCallback || undefined);
  15969. var container = eventType === 'value'
  15970. ? new ValueEventRegistration(callbackContext)
  15971. : new ChildEventRegistration(eventType, callbackContext);
  15972. repoAddEventCallbackForQuery(query._repo, query, container);
  15973. return function () { return repoRemoveEventCallbackForQuery(query._repo, query, container); };
  15974. }
  15975. function onValue(query, callback, cancelCallbackOrListenOptions, options) {
  15976. return addEventListener(query, 'value', callback, cancelCallbackOrListenOptions, options);
  15977. }
  15978. function onChildAdded(query, callback, cancelCallbackOrListenOptions, options) {
  15979. return addEventListener(query, 'child_added', callback, cancelCallbackOrListenOptions, options);
  15980. }
  15981. function onChildChanged(query, callback, cancelCallbackOrListenOptions, options) {
  15982. return addEventListener(query, 'child_changed', callback, cancelCallbackOrListenOptions, options);
  15983. }
  15984. function onChildMoved(query, callback, cancelCallbackOrListenOptions, options) {
  15985. return addEventListener(query, 'child_moved', callback, cancelCallbackOrListenOptions, options);
  15986. }
  15987. function onChildRemoved(query, callback, cancelCallbackOrListenOptions, options) {
  15988. return addEventListener(query, 'child_removed', callback, cancelCallbackOrListenOptions, options);
  15989. }
  15990. /**
  15991. * Detaches a callback previously attached with the corresponding `on*()` (`onValue`, `onChildAdded`) listener.
  15992. * Note: This is not the recommended way to remove a listener. Instead, please use the returned callback function from
  15993. * the respective `on*` callbacks.
  15994. *
  15995. * Detach a callback previously attached with `on*()`. Calling `off()` on a parent listener
  15996. * will not automatically remove listeners registered on child nodes, `off()`
  15997. * must also be called on any child listeners to remove the callback.
  15998. *
  15999. * If a callback is not specified, all callbacks for the specified eventType
  16000. * will be removed. Similarly, if no eventType is specified, all callbacks
  16001. * for the `Reference` will be removed.
  16002. *
  16003. * Individual listeners can also be removed by invoking their unsubscribe
  16004. * callbacks.
  16005. *
  16006. * @param query - The query that the listener was registered with.
  16007. * @param eventType - One of the following strings: "value", "child_added",
  16008. * "child_changed", "child_removed", or "child_moved." If omitted, all callbacks
  16009. * for the `Reference` will be removed.
  16010. * @param callback - The callback function that was passed to `on()` or
  16011. * `undefined` to remove all callbacks.
  16012. */
  16013. function off(query, eventType, callback) {
  16014. var container = null;
  16015. var expCallback = callback ? new CallbackContext(callback) : null;
  16016. if (eventType === 'value') {
  16017. container = new ValueEventRegistration(expCallback);
  16018. }
  16019. else if (eventType) {
  16020. container = new ChildEventRegistration(eventType, expCallback);
  16021. }
  16022. repoRemoveEventCallbackForQuery(query._repo, query, container);
  16023. }
  16024. /**
  16025. * A `QueryConstraint` is used to narrow the set of documents returned by a
  16026. * Database query. `QueryConstraint`s are created by invoking {@link endAt},
  16027. * {@link endBefore}, {@link startAt}, {@link startAfter}, {@link
  16028. * limitToFirst}, {@link limitToLast}, {@link orderByChild},
  16029. * {@link orderByChild}, {@link orderByKey} , {@link orderByPriority} ,
  16030. * {@link orderByValue} or {@link equalTo} and
  16031. * can then be passed to {@link query} to create a new query instance that
  16032. * also contains this `QueryConstraint`.
  16033. */
  16034. var QueryConstraint = /** @class */ (function () {
  16035. function QueryConstraint() {
  16036. }
  16037. return QueryConstraint;
  16038. }());
  16039. var QueryEndAtConstraint = /** @class */ (function (_super) {
  16040. tslib.__extends(QueryEndAtConstraint, _super);
  16041. function QueryEndAtConstraint(_value, _key) {
  16042. var _this = _super.call(this) || this;
  16043. _this._value = _value;
  16044. _this._key = _key;
  16045. return _this;
  16046. }
  16047. QueryEndAtConstraint.prototype._apply = function (query) {
  16048. validateFirebaseDataArg('endAt', this._value, query._path, true);
  16049. var newParams = queryParamsEndAt(query._queryParams, this._value, this._key);
  16050. validateLimit(newParams);
  16051. validateQueryEndpoints(newParams);
  16052. if (query._queryParams.hasEnd()) {
  16053. throw new Error('endAt: Starting point was already set (by another call to endAt, ' +
  16054. 'endBefore or equalTo).');
  16055. }
  16056. return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
  16057. };
  16058. return QueryEndAtConstraint;
  16059. }(QueryConstraint));
  16060. /**
  16061. * Creates a `QueryConstraint` with the specified ending point.
  16062. *
  16063. * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
  16064. * allows you to choose arbitrary starting and ending points for your queries.
  16065. *
  16066. * The ending point is inclusive, so children with exactly the specified value
  16067. * will be included in the query. The optional key argument can be used to
  16068. * further limit the range of the query. If it is specified, then children that
  16069. * have exactly the specified value must also have a key name less than or equal
  16070. * to the specified key.
  16071. *
  16072. * You can read more about `endAt()` in
  16073. * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
  16074. *
  16075. * @param value - The value to end at. The argument type depends on which
  16076. * `orderBy*()` function was used in this query. Specify a value that matches
  16077. * the `orderBy*()` type. When used in combination with `orderByKey()`, the
  16078. * value must be a string.
  16079. * @param key - The child key to end at, among the children with the previously
  16080. * specified priority. This argument is only allowed if ordering by child,
  16081. * value, or priority.
  16082. */
  16083. function endAt(value, key) {
  16084. validateKey('endAt', 'key', key, true);
  16085. return new QueryEndAtConstraint(value, key);
  16086. }
  16087. var QueryEndBeforeConstraint = /** @class */ (function (_super) {
  16088. tslib.__extends(QueryEndBeforeConstraint, _super);
  16089. function QueryEndBeforeConstraint(_value, _key) {
  16090. var _this = _super.call(this) || this;
  16091. _this._value = _value;
  16092. _this._key = _key;
  16093. return _this;
  16094. }
  16095. QueryEndBeforeConstraint.prototype._apply = function (query) {
  16096. validateFirebaseDataArg('endBefore', this._value, query._path, false);
  16097. var newParams = queryParamsEndBefore(query._queryParams, this._value, this._key);
  16098. validateLimit(newParams);
  16099. validateQueryEndpoints(newParams);
  16100. if (query._queryParams.hasEnd()) {
  16101. throw new Error('endBefore: Starting point was already set (by another call to endAt, ' +
  16102. 'endBefore or equalTo).');
  16103. }
  16104. return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
  16105. };
  16106. return QueryEndBeforeConstraint;
  16107. }(QueryConstraint));
  16108. /**
  16109. * Creates a `QueryConstraint` with the specified ending point (exclusive).
  16110. *
  16111. * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
  16112. * allows you to choose arbitrary starting and ending points for your queries.
  16113. *
  16114. * The ending point is exclusive. If only a value is provided, children
  16115. * with a value less than the specified value will be included in the query.
  16116. * If a key is specified, then children must have a value less than or equal
  16117. * to the specified value and a key name less than the specified key.
  16118. *
  16119. * @param value - The value to end before. The argument type depends on which
  16120. * `orderBy*()` function was used in this query. Specify a value that matches
  16121. * the `orderBy*()` type. When used in combination with `orderByKey()`, the
  16122. * value must be a string.
  16123. * @param key - The child key to end before, among the children with the
  16124. * previously specified priority. This argument is only allowed if ordering by
  16125. * child, value, or priority.
  16126. */
  16127. function endBefore(value, key) {
  16128. validateKey('endBefore', 'key', key, true);
  16129. return new QueryEndBeforeConstraint(value, key);
  16130. }
  16131. var QueryStartAtConstraint = /** @class */ (function (_super) {
  16132. tslib.__extends(QueryStartAtConstraint, _super);
  16133. function QueryStartAtConstraint(_value, _key) {
  16134. var _this = _super.call(this) || this;
  16135. _this._value = _value;
  16136. _this._key = _key;
  16137. return _this;
  16138. }
  16139. QueryStartAtConstraint.prototype._apply = function (query) {
  16140. validateFirebaseDataArg('startAt', this._value, query._path, true);
  16141. var newParams = queryParamsStartAt(query._queryParams, this._value, this._key);
  16142. validateLimit(newParams);
  16143. validateQueryEndpoints(newParams);
  16144. if (query._queryParams.hasStart()) {
  16145. throw new Error('startAt: Starting point was already set (by another call to startAt, ' +
  16146. 'startBefore or equalTo).');
  16147. }
  16148. return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
  16149. };
  16150. return QueryStartAtConstraint;
  16151. }(QueryConstraint));
  16152. /**
  16153. * Creates a `QueryConstraint` with the specified starting point.
  16154. *
  16155. * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
  16156. * allows you to choose arbitrary starting and ending points for your queries.
  16157. *
  16158. * The starting point is inclusive, so children with exactly the specified value
  16159. * will be included in the query. The optional key argument can be used to
  16160. * further limit the range of the query. If it is specified, then children that
  16161. * have exactly the specified value must also have a key name greater than or
  16162. * equal to the specified key.
  16163. *
  16164. * You can read more about `startAt()` in
  16165. * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
  16166. *
  16167. * @param value - The value to start at. The argument type depends on which
  16168. * `orderBy*()` function was used in this query. Specify a value that matches
  16169. * the `orderBy*()` type. When used in combination with `orderByKey()`, the
  16170. * value must be a string.
  16171. * @param key - The child key to start at. This argument is only allowed if
  16172. * ordering by child, value, or priority.
  16173. */
  16174. function startAt(value, key) {
  16175. if (value === void 0) { value = null; }
  16176. validateKey('startAt', 'key', key, true);
  16177. return new QueryStartAtConstraint(value, key);
  16178. }
  16179. var QueryStartAfterConstraint = /** @class */ (function (_super) {
  16180. tslib.__extends(QueryStartAfterConstraint, _super);
  16181. function QueryStartAfterConstraint(_value, _key) {
  16182. var _this = _super.call(this) || this;
  16183. _this._value = _value;
  16184. _this._key = _key;
  16185. return _this;
  16186. }
  16187. QueryStartAfterConstraint.prototype._apply = function (query) {
  16188. validateFirebaseDataArg('startAfter', this._value, query._path, false);
  16189. var newParams = queryParamsStartAfter(query._queryParams, this._value, this._key);
  16190. validateLimit(newParams);
  16191. validateQueryEndpoints(newParams);
  16192. if (query._queryParams.hasStart()) {
  16193. throw new Error('startAfter: Starting point was already set (by another call to startAt, ' +
  16194. 'startAfter, or equalTo).');
  16195. }
  16196. return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
  16197. };
  16198. return QueryStartAfterConstraint;
  16199. }(QueryConstraint));
  16200. /**
  16201. * Creates a `QueryConstraint` with the specified starting point (exclusive).
  16202. *
  16203. * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
  16204. * allows you to choose arbitrary starting and ending points for your queries.
  16205. *
  16206. * The starting point is exclusive. If only a value is provided, children
  16207. * with a value greater than the specified value will be included in the query.
  16208. * If a key is specified, then children must have a value greater than or equal
  16209. * to the specified value and a a key name greater than the specified key.
  16210. *
  16211. * @param value - The value to start after. The argument type depends on which
  16212. * `orderBy*()` function was used in this query. Specify a value that matches
  16213. * the `orderBy*()` type. When used in combination with `orderByKey()`, the
  16214. * value must be a string.
  16215. * @param key - The child key to start after. This argument is only allowed if
  16216. * ordering by child, value, or priority.
  16217. */
  16218. function startAfter(value, key) {
  16219. validateKey('startAfter', 'key', key, true);
  16220. return new QueryStartAfterConstraint(value, key);
  16221. }
  16222. var QueryLimitToFirstConstraint = /** @class */ (function (_super) {
  16223. tslib.__extends(QueryLimitToFirstConstraint, _super);
  16224. function QueryLimitToFirstConstraint(_limit) {
  16225. var _this = _super.call(this) || this;
  16226. _this._limit = _limit;
  16227. return _this;
  16228. }
  16229. QueryLimitToFirstConstraint.prototype._apply = function (query) {
  16230. if (query._queryParams.hasLimit()) {
  16231. throw new Error('limitToFirst: Limit was already set (by another call to limitToFirst ' +
  16232. 'or limitToLast).');
  16233. }
  16234. return new QueryImpl(query._repo, query._path, queryParamsLimitToFirst(query._queryParams, this._limit), query._orderByCalled);
  16235. };
  16236. return QueryLimitToFirstConstraint;
  16237. }(QueryConstraint));
  16238. /**
  16239. * Creates a new `QueryConstraint` that if limited to the first specific number
  16240. * of children.
  16241. *
  16242. * The `limitToFirst()` method is used to set a maximum number of children to be
  16243. * synced for a given callback. If we set a limit of 100, we will initially only
  16244. * receive up to 100 `child_added` events. If we have fewer than 100 messages
  16245. * stored in our Database, a `child_added` event will fire for each message.
  16246. * However, if we have over 100 messages, we will only receive a `child_added`
  16247. * event for the first 100 ordered messages. As items change, we will receive
  16248. * `child_removed` events for each item that drops out of the active list so
  16249. * that the total number stays at 100.
  16250. *
  16251. * You can read more about `limitToFirst()` in
  16252. * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
  16253. *
  16254. * @param limit - The maximum number of nodes to include in this query.
  16255. */
  16256. function limitToFirst(limit) {
  16257. if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
  16258. throw new Error('limitToFirst: First argument must be a positive integer.');
  16259. }
  16260. return new QueryLimitToFirstConstraint(limit);
  16261. }
  16262. var QueryLimitToLastConstraint = /** @class */ (function (_super) {
  16263. tslib.__extends(QueryLimitToLastConstraint, _super);
  16264. function QueryLimitToLastConstraint(_limit) {
  16265. var _this = _super.call(this) || this;
  16266. _this._limit = _limit;
  16267. return _this;
  16268. }
  16269. QueryLimitToLastConstraint.prototype._apply = function (query) {
  16270. if (query._queryParams.hasLimit()) {
  16271. throw new Error('limitToLast: Limit was already set (by another call to limitToFirst ' +
  16272. 'or limitToLast).');
  16273. }
  16274. return new QueryImpl(query._repo, query._path, queryParamsLimitToLast(query._queryParams, this._limit), query._orderByCalled);
  16275. };
  16276. return QueryLimitToLastConstraint;
  16277. }(QueryConstraint));
  16278. /**
  16279. * Creates a new `QueryConstraint` that is limited to return only the last
  16280. * specified number of children.
  16281. *
  16282. * The `limitToLast()` method is used to set a maximum number of children to be
  16283. * synced for a given callback. If we set a limit of 100, we will initially only
  16284. * receive up to 100 `child_added` events. If we have fewer than 100 messages
  16285. * stored in our Database, a `child_added` event will fire for each message.
  16286. * However, if we have over 100 messages, we will only receive a `child_added`
  16287. * event for the last 100 ordered messages. As items change, we will receive
  16288. * `child_removed` events for each item that drops out of the active list so
  16289. * that the total number stays at 100.
  16290. *
  16291. * You can read more about `limitToLast()` in
  16292. * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
  16293. *
  16294. * @param limit - The maximum number of nodes to include in this query.
  16295. */
  16296. function limitToLast(limit) {
  16297. if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
  16298. throw new Error('limitToLast: First argument must be a positive integer.');
  16299. }
  16300. return new QueryLimitToLastConstraint(limit);
  16301. }
  16302. var QueryOrderByChildConstraint = /** @class */ (function (_super) {
  16303. tslib.__extends(QueryOrderByChildConstraint, _super);
  16304. function QueryOrderByChildConstraint(_path) {
  16305. var _this = _super.call(this) || this;
  16306. _this._path = _path;
  16307. return _this;
  16308. }
  16309. QueryOrderByChildConstraint.prototype._apply = function (query) {
  16310. validateNoPreviousOrderByCall(query, 'orderByChild');
  16311. var parsedPath = new Path(this._path);
  16312. if (pathIsEmpty(parsedPath)) {
  16313. throw new Error('orderByChild: cannot pass in empty path. Use orderByValue() instead.');
  16314. }
  16315. var index = new PathIndex(parsedPath);
  16316. var newParams = queryParamsOrderBy(query._queryParams, index);
  16317. validateQueryEndpoints(newParams);
  16318. return new QueryImpl(query._repo, query._path, newParams,
  16319. /*orderByCalled=*/ true);
  16320. };
  16321. return QueryOrderByChildConstraint;
  16322. }(QueryConstraint));
  16323. /**
  16324. * Creates a new `QueryConstraint` that orders by the specified child key.
  16325. *
  16326. * Queries can only order by one key at a time. Calling `orderByChild()`
  16327. * multiple times on the same query is an error.
  16328. *
  16329. * Firebase queries allow you to order your data by any child key on the fly.
  16330. * However, if you know in advance what your indexes will be, you can define
  16331. * them via the .indexOn rule in your Security Rules for better performance. See
  16332. * the{@link https://firebase.google.com/docs/database/security/indexing-data}
  16333. * rule for more information.
  16334. *
  16335. * You can read more about `orderByChild()` in
  16336. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}.
  16337. *
  16338. * @param path - The path to order by.
  16339. */
  16340. function orderByChild(path) {
  16341. if (path === '$key') {
  16342. throw new Error('orderByChild: "$key" is invalid. Use orderByKey() instead.');
  16343. }
  16344. else if (path === '$priority') {
  16345. throw new Error('orderByChild: "$priority" is invalid. Use orderByPriority() instead.');
  16346. }
  16347. else if (path === '$value') {
  16348. throw new Error('orderByChild: "$value" is invalid. Use orderByValue() instead.');
  16349. }
  16350. validatePathString('orderByChild', 'path', path, false);
  16351. return new QueryOrderByChildConstraint(path);
  16352. }
  16353. var QueryOrderByKeyConstraint = /** @class */ (function (_super) {
  16354. tslib.__extends(QueryOrderByKeyConstraint, _super);
  16355. function QueryOrderByKeyConstraint() {
  16356. return _super !== null && _super.apply(this, arguments) || this;
  16357. }
  16358. QueryOrderByKeyConstraint.prototype._apply = function (query) {
  16359. validateNoPreviousOrderByCall(query, 'orderByKey');
  16360. var newParams = queryParamsOrderBy(query._queryParams, KEY_INDEX);
  16361. validateQueryEndpoints(newParams);
  16362. return new QueryImpl(query._repo, query._path, newParams,
  16363. /*orderByCalled=*/ true);
  16364. };
  16365. return QueryOrderByKeyConstraint;
  16366. }(QueryConstraint));
  16367. /**
  16368. * Creates a new `QueryConstraint` that orders by the key.
  16369. *
  16370. * Sorts the results of a query by their (ascending) key values.
  16371. *
  16372. * You can read more about `orderByKey()` in
  16373. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}.
  16374. */
  16375. function orderByKey() {
  16376. return new QueryOrderByKeyConstraint();
  16377. }
  16378. var QueryOrderByPriorityConstraint = /** @class */ (function (_super) {
  16379. tslib.__extends(QueryOrderByPriorityConstraint, _super);
  16380. function QueryOrderByPriorityConstraint() {
  16381. return _super !== null && _super.apply(this, arguments) || this;
  16382. }
  16383. QueryOrderByPriorityConstraint.prototype._apply = function (query) {
  16384. validateNoPreviousOrderByCall(query, 'orderByPriority');
  16385. var newParams = queryParamsOrderBy(query._queryParams, PRIORITY_INDEX);
  16386. validateQueryEndpoints(newParams);
  16387. return new QueryImpl(query._repo, query._path, newParams,
  16388. /*orderByCalled=*/ true);
  16389. };
  16390. return QueryOrderByPriorityConstraint;
  16391. }(QueryConstraint));
  16392. /**
  16393. * Creates a new `QueryConstraint` that orders by priority.
  16394. *
  16395. * Applications need not use priority but can order collections by
  16396. * ordinary properties (see
  16397. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}
  16398. * for alternatives to priority.
  16399. */
  16400. function orderByPriority() {
  16401. return new QueryOrderByPriorityConstraint();
  16402. }
  16403. var QueryOrderByValueConstraint = /** @class */ (function (_super) {
  16404. tslib.__extends(QueryOrderByValueConstraint, _super);
  16405. function QueryOrderByValueConstraint() {
  16406. return _super !== null && _super.apply(this, arguments) || this;
  16407. }
  16408. QueryOrderByValueConstraint.prototype._apply = function (query) {
  16409. validateNoPreviousOrderByCall(query, 'orderByValue');
  16410. var newParams = queryParamsOrderBy(query._queryParams, VALUE_INDEX);
  16411. validateQueryEndpoints(newParams);
  16412. return new QueryImpl(query._repo, query._path, newParams,
  16413. /*orderByCalled=*/ true);
  16414. };
  16415. return QueryOrderByValueConstraint;
  16416. }(QueryConstraint));
  16417. /**
  16418. * Creates a new `QueryConstraint` that orders by value.
  16419. *
  16420. * If the children of a query are all scalar values (string, number, or
  16421. * boolean), you can order the results by their (ascending) values.
  16422. *
  16423. * You can read more about `orderByValue()` in
  16424. * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}.
  16425. */
  16426. function orderByValue() {
  16427. return new QueryOrderByValueConstraint();
  16428. }
  16429. var QueryEqualToValueConstraint = /** @class */ (function (_super) {
  16430. tslib.__extends(QueryEqualToValueConstraint, _super);
  16431. function QueryEqualToValueConstraint(_value, _key) {
  16432. var _this = _super.call(this) || this;
  16433. _this._value = _value;
  16434. _this._key = _key;
  16435. return _this;
  16436. }
  16437. QueryEqualToValueConstraint.prototype._apply = function (query) {
  16438. validateFirebaseDataArg('equalTo', this._value, query._path, false);
  16439. if (query._queryParams.hasStart()) {
  16440. throw new Error('equalTo: Starting point was already set (by another call to startAt/startAfter or ' +
  16441. 'equalTo).');
  16442. }
  16443. if (query._queryParams.hasEnd()) {
  16444. throw new Error('equalTo: Ending point was already set (by another call to endAt/endBefore or ' +
  16445. 'equalTo).');
  16446. }
  16447. return new QueryEndAtConstraint(this._value, this._key)._apply(new QueryStartAtConstraint(this._value, this._key)._apply(query));
  16448. };
  16449. return QueryEqualToValueConstraint;
  16450. }(QueryConstraint));
  16451. /**
  16452. * Creates a `QueryConstraint` that includes children that match the specified
  16453. * value.
  16454. *
  16455. * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
  16456. * allows you to choose arbitrary starting and ending points for your queries.
  16457. *
  16458. * The optional key argument can be used to further limit the range of the
  16459. * query. If it is specified, then children that have exactly the specified
  16460. * value must also have exactly the specified key as their key name. This can be
  16461. * used to filter result sets with many matches for the same value.
  16462. *
  16463. * You can read more about `equalTo()` in
  16464. * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
  16465. *
  16466. * @param value - The value to match for. The argument type depends on which
  16467. * `orderBy*()` function was used in this query. Specify a value that matches
  16468. * the `orderBy*()` type. When used in combination with `orderByKey()`, the
  16469. * value must be a string.
  16470. * @param key - The child key to start at, among the children with the
  16471. * previously specified priority. This argument is only allowed if ordering by
  16472. * child, value, or priority.
  16473. */
  16474. function equalTo(value, key) {
  16475. validateKey('equalTo', 'key', key, true);
  16476. return new QueryEqualToValueConstraint(value, key);
  16477. }
  16478. /**
  16479. * Creates a new immutable instance of `Query` that is extended to also include
  16480. * additional query constraints.
  16481. *
  16482. * @param query - The Query instance to use as a base for the new constraints.
  16483. * @param queryConstraints - The list of `QueryConstraint`s to apply.
  16484. * @throws if any of the provided query constraints cannot be combined with the
  16485. * existing or new constraints.
  16486. */
  16487. function query(query) {
  16488. var e_1, _a;
  16489. var queryConstraints = [];
  16490. for (var _i = 1; _i < arguments.length; _i++) {
  16491. queryConstraints[_i - 1] = arguments[_i];
  16492. }
  16493. var queryImpl = util.getModularInstance(query);
  16494. try {
  16495. for (var queryConstraints_1 = tslib.__values(queryConstraints), queryConstraints_1_1 = queryConstraints_1.next(); !queryConstraints_1_1.done; queryConstraints_1_1 = queryConstraints_1.next()) {
  16496. var constraint = queryConstraints_1_1.value;
  16497. queryImpl = constraint._apply(queryImpl);
  16498. }
  16499. }
  16500. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  16501. finally {
  16502. try {
  16503. if (queryConstraints_1_1 && !queryConstraints_1_1.done && (_a = queryConstraints_1.return)) _a.call(queryConstraints_1);
  16504. }
  16505. finally { if (e_1) throw e_1.error; }
  16506. }
  16507. return queryImpl;
  16508. }
  16509. /**
  16510. * Define reference constructor in various modules
  16511. *
  16512. * We are doing this here to avoid several circular
  16513. * dependency issues
  16514. */
  16515. syncPointSetReferenceConstructor(ReferenceImpl);
  16516. syncTreeSetReferenceConstructor(ReferenceImpl);
  16517. /**
  16518. * This variable is also defined in the firebase Node.js Admin SDK. Before
  16519. * modifying this definition, consult the definition in:
  16520. *
  16521. * https://github.com/firebase/firebase-admin-node
  16522. *
  16523. * and make sure the two are consistent.
  16524. */
  16525. var FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
  16526. /**
  16527. * Creates and caches `Repo` instances.
  16528. */
  16529. var repos = {};
  16530. /**
  16531. * If true, any new `Repo` will be created to use `ReadonlyRestClient` (for testing purposes).
  16532. */
  16533. var useRestClient = false;
  16534. /**
  16535. * Update an existing `Repo` in place to point to a new host/port.
  16536. */
  16537. function repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider) {
  16538. repo.repoInfo_ = new RepoInfo("".concat(host, ":").concat(port),
  16539. /* secure= */ false, repo.repoInfo_.namespace, repo.repoInfo_.webSocketOnly, repo.repoInfo_.nodeAdmin, repo.repoInfo_.persistenceKey, repo.repoInfo_.includeNamespaceInQueryParams,
  16540. /*isUsingEmulator=*/ true);
  16541. if (tokenProvider) {
  16542. repo.authTokenProvider_ = tokenProvider;
  16543. }
  16544. }
  16545. /**
  16546. * This function should only ever be called to CREATE a new database instance.
  16547. * @internal
  16548. */
  16549. function repoManagerDatabaseFromApp(app, authProvider, appCheckProvider, url, nodeAdmin) {
  16550. var dbUrl = url || app.options.databaseURL;
  16551. if (dbUrl === undefined) {
  16552. if (!app.options.projectId) {
  16553. fatal("Can't determine Firebase Database URL. Be sure to include " +
  16554. ' a Project ID when calling firebase.initializeApp().');
  16555. }
  16556. log('Using default host for project ', app.options.projectId);
  16557. dbUrl = "".concat(app.options.projectId, "-default-rtdb.firebaseio.com");
  16558. }
  16559. var parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
  16560. var repoInfo = parsedUrl.repoInfo;
  16561. var isEmulator;
  16562. var dbEmulatorHost = undefined;
  16563. if (typeof process !== 'undefined' && process.env) {
  16564. dbEmulatorHost = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
  16565. }
  16566. if (dbEmulatorHost) {
  16567. isEmulator = true;
  16568. dbUrl = "http://".concat(dbEmulatorHost, "?ns=").concat(repoInfo.namespace);
  16569. parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
  16570. repoInfo = parsedUrl.repoInfo;
  16571. }
  16572. else {
  16573. isEmulator = !parsedUrl.repoInfo.secure;
  16574. }
  16575. var authTokenProvider = nodeAdmin && isEmulator
  16576. ? new EmulatorTokenProvider(EmulatorTokenProvider.OWNER)
  16577. : new FirebaseAuthTokenProvider(app.name, app.options, authProvider);
  16578. validateUrl('Invalid Firebase Database URL', parsedUrl);
  16579. if (!pathIsEmpty(parsedUrl.path)) {
  16580. fatal('Database URL must point to the root of a Firebase Database ' +
  16581. '(not including a child path).');
  16582. }
  16583. var repo = repoManagerCreateRepo(repoInfo, app, authTokenProvider, new AppCheckTokenProvider(app.name, appCheckProvider));
  16584. return new Database$1(repo, app);
  16585. }
  16586. /**
  16587. * Remove the repo and make sure it is disconnected.
  16588. *
  16589. */
  16590. function repoManagerDeleteRepo(repo, appName) {
  16591. var appRepos = repos[appName];
  16592. // This should never happen...
  16593. if (!appRepos || appRepos[repo.key] !== repo) {
  16594. fatal("Database ".concat(appName, "(").concat(repo.repoInfo_, ") has already been deleted."));
  16595. }
  16596. repoInterrupt(repo);
  16597. delete appRepos[repo.key];
  16598. }
  16599. /**
  16600. * Ensures a repo doesn't already exist and then creates one using the
  16601. * provided app.
  16602. *
  16603. * @param repoInfo - The metadata about the Repo
  16604. * @returns The Repo object for the specified server / repoName.
  16605. */
  16606. function repoManagerCreateRepo(repoInfo, app, authTokenProvider, appCheckProvider) {
  16607. var appRepos = repos[app.name];
  16608. if (!appRepos) {
  16609. appRepos = {};
  16610. repos[app.name] = appRepos;
  16611. }
  16612. var repo = appRepos[repoInfo.toURLString()];
  16613. if (repo) {
  16614. fatal('Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.');
  16615. }
  16616. repo = new Repo(repoInfo, useRestClient, authTokenProvider, appCheckProvider);
  16617. appRepos[repoInfo.toURLString()] = repo;
  16618. return repo;
  16619. }
  16620. /**
  16621. * Forces us to use ReadonlyRestClient instead of PersistentConnection for new Repos.
  16622. */
  16623. function repoManagerForceRestClient(forceRestClient) {
  16624. useRestClient = forceRestClient;
  16625. }
  16626. /**
  16627. * Class representing a Firebase Realtime Database.
  16628. */
  16629. var Database$1 = /** @class */ (function () {
  16630. /** @hideconstructor */
  16631. function Database(_repoInternal,
  16632. /** The {@link @firebase/app#FirebaseApp} associated with this Realtime Database instance. */
  16633. app) {
  16634. this._repoInternal = _repoInternal;
  16635. this.app = app;
  16636. /** Represents a `Database` instance. */
  16637. this['type'] = 'database';
  16638. /** Track if the instance has been used (root or repo accessed) */
  16639. this._instanceStarted = false;
  16640. }
  16641. Object.defineProperty(Database.prototype, "_repo", {
  16642. get: function () {
  16643. if (!this._instanceStarted) {
  16644. repoStart(this._repoInternal, this.app.options.appId, this.app.options['databaseAuthVariableOverride']);
  16645. this._instanceStarted = true;
  16646. }
  16647. return this._repoInternal;
  16648. },
  16649. enumerable: false,
  16650. configurable: true
  16651. });
  16652. Object.defineProperty(Database.prototype, "_root", {
  16653. get: function () {
  16654. if (!this._rootInternal) {
  16655. this._rootInternal = new ReferenceImpl(this._repo, newEmptyPath());
  16656. }
  16657. return this._rootInternal;
  16658. },
  16659. enumerable: false,
  16660. configurable: true
  16661. });
  16662. Database.prototype._delete = function () {
  16663. if (this._rootInternal !== null) {
  16664. repoManagerDeleteRepo(this._repo, this.app.name);
  16665. this._repoInternal = null;
  16666. this._rootInternal = null;
  16667. }
  16668. return Promise.resolve();
  16669. };
  16670. Database.prototype._checkNotDeleted = function (apiName) {
  16671. if (this._rootInternal === null) {
  16672. fatal('Cannot call ' + apiName + ' on a deleted database.');
  16673. }
  16674. };
  16675. return Database;
  16676. }());
  16677. function checkTransportInit() {
  16678. if (TransportManager.IS_TRANSPORT_INITIALIZED) {
  16679. warn$1('Transport has already been initialized. Please call this function before calling ref or setting up a listener');
  16680. }
  16681. }
  16682. /**
  16683. * Force the use of websockets instead of longPolling.
  16684. */
  16685. function forceWebSockets() {
  16686. checkTransportInit();
  16687. BrowserPollConnection.forceDisallow();
  16688. }
  16689. /**
  16690. * Force the use of longPolling instead of websockets. This will be ignored if websocket protocol is used in databaseURL.
  16691. */
  16692. function forceLongPolling() {
  16693. checkTransportInit();
  16694. WebSocketConnection.forceDisallow();
  16695. BrowserPollConnection.forceAllow();
  16696. }
  16697. /**
  16698. * Modify the provided instance to communicate with the Realtime Database
  16699. * emulator.
  16700. *
  16701. * <p>Note: This method must be called before performing any other operation.
  16702. *
  16703. * @param db - The instance to modify.
  16704. * @param host - The emulator host (ex: localhost)
  16705. * @param port - The emulator port (ex: 8080)
  16706. * @param options.mockUserToken - the mock auth token to use for unit testing Security Rules
  16707. */
  16708. function connectDatabaseEmulator(db, host, port, options) {
  16709. if (options === void 0) { options = {}; }
  16710. db = util.getModularInstance(db);
  16711. db._checkNotDeleted('useEmulator');
  16712. if (db._instanceStarted) {
  16713. fatal('Cannot call useEmulator() after instance has already been initialized.');
  16714. }
  16715. var repo = db._repoInternal;
  16716. var tokenProvider = undefined;
  16717. if (repo.repoInfo_.nodeAdmin) {
  16718. if (options.mockUserToken) {
  16719. fatal('mockUserToken is not supported by the Admin SDK. For client access with mock users, please use the "firebase" package instead of "firebase-admin".');
  16720. }
  16721. tokenProvider = new EmulatorTokenProvider(EmulatorTokenProvider.OWNER);
  16722. }
  16723. else if (options.mockUserToken) {
  16724. var token = typeof options.mockUserToken === 'string'
  16725. ? options.mockUserToken
  16726. : util.createMockUserToken(options.mockUserToken, db.app.options.projectId);
  16727. tokenProvider = new EmulatorTokenProvider(token);
  16728. }
  16729. // Modify the repo to apply emulator settings
  16730. repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
  16731. }
  16732. /**
  16733. * Disconnects from the server (all Database operations will be completed
  16734. * offline).
  16735. *
  16736. * The client automatically maintains a persistent connection to the Database
  16737. * server, which will remain active indefinitely and reconnect when
  16738. * disconnected. However, the `goOffline()` and `goOnline()` methods may be used
  16739. * to control the client connection in cases where a persistent connection is
  16740. * undesirable.
  16741. *
  16742. * While offline, the client will no longer receive data updates from the
  16743. * Database. However, all Database operations performed locally will continue to
  16744. * immediately fire events, allowing your application to continue behaving
  16745. * normally. Additionally, each operation performed locally will automatically
  16746. * be queued and retried upon reconnection to the Database server.
  16747. *
  16748. * To reconnect to the Database and begin receiving remote events, see
  16749. * `goOnline()`.
  16750. *
  16751. * @param db - The instance to disconnect.
  16752. */
  16753. function goOffline(db) {
  16754. db = util.getModularInstance(db);
  16755. db._checkNotDeleted('goOffline');
  16756. repoInterrupt(db._repo);
  16757. }
  16758. /**
  16759. * Reconnects to the server and synchronizes the offline Database state
  16760. * with the server state.
  16761. *
  16762. * This method should be used after disabling the active connection with
  16763. * `goOffline()`. Once reconnected, the client will transmit the proper data
  16764. * and fire the appropriate events so that your client "catches up"
  16765. * automatically.
  16766. *
  16767. * @param db - The instance to reconnect.
  16768. */
  16769. function goOnline(db) {
  16770. db = util.getModularInstance(db);
  16771. db._checkNotDeleted('goOnline');
  16772. repoResume(db._repo);
  16773. }
  16774. function enableLogging(logger, persistent) {
  16775. enableLogging$1(logger, persistent);
  16776. }
  16777. /**
  16778. * @license
  16779. * Copyright 2020 Google LLC
  16780. *
  16781. * Licensed under the Apache License, Version 2.0 (the "License");
  16782. * you may not use this file except in compliance with the License.
  16783. * You may obtain a copy of the License at
  16784. *
  16785. * http://www.apache.org/licenses/LICENSE-2.0
  16786. *
  16787. * Unless required by applicable law or agreed to in writing, software
  16788. * distributed under the License is distributed on an "AS IS" BASIS,
  16789. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16790. * See the License for the specific language governing permissions and
  16791. * limitations under the License.
  16792. */
  16793. var SERVER_TIMESTAMP = {
  16794. '.sv': 'timestamp'
  16795. };
  16796. /**
  16797. * Returns a placeholder value for auto-populating the current timestamp (time
  16798. * since the Unix epoch, in milliseconds) as determined by the Firebase
  16799. * servers.
  16800. */
  16801. function serverTimestamp() {
  16802. return SERVER_TIMESTAMP;
  16803. }
  16804. /**
  16805. * Returns a placeholder value that can be used to atomically increment the
  16806. * current database value by the provided delta.
  16807. *
  16808. * @param delta - the amount to modify the current value atomically.
  16809. * @returns A placeholder value for modifying data atomically server-side.
  16810. */
  16811. function increment(delta) {
  16812. return {
  16813. '.sv': {
  16814. 'increment': delta
  16815. }
  16816. };
  16817. }
  16818. /**
  16819. * @license
  16820. * Copyright 2020 Google LLC
  16821. *
  16822. * Licensed under the Apache License, Version 2.0 (the "License");
  16823. * you may not use this file except in compliance with the License.
  16824. * You may obtain a copy of the License at
  16825. *
  16826. * http://www.apache.org/licenses/LICENSE-2.0
  16827. *
  16828. * Unless required by applicable law or agreed to in writing, software
  16829. * distributed under the License is distributed on an "AS IS" BASIS,
  16830. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16831. * See the License for the specific language governing permissions and
  16832. * limitations under the License.
  16833. */
  16834. /**
  16835. * A type for the resolve value of {@link runTransaction}.
  16836. */
  16837. var TransactionResult$1 = /** @class */ (function () {
  16838. /** @hideconstructor */
  16839. function TransactionResult(
  16840. /** Whether the transaction was successfully committed. */
  16841. committed,
  16842. /** The resulting data snapshot. */
  16843. snapshot) {
  16844. this.committed = committed;
  16845. this.snapshot = snapshot;
  16846. }
  16847. /** Returns a JSON-serializable representation of this object. */
  16848. TransactionResult.prototype.toJSON = function () {
  16849. return { committed: this.committed, snapshot: this.snapshot.toJSON() };
  16850. };
  16851. return TransactionResult;
  16852. }());
  16853. /**
  16854. * Atomically modifies the data at this location.
  16855. *
  16856. * Atomically modify the data at this location. Unlike a normal `set()`, which
  16857. * just overwrites the data regardless of its previous value, `runTransaction()` is
  16858. * used to modify the existing value to a new value, ensuring there are no
  16859. * conflicts with other clients writing to the same location at the same time.
  16860. *
  16861. * To accomplish this, you pass `runTransaction()` an update function which is
  16862. * used to transform the current value into a new value. If another client
  16863. * writes to the location before your new value is successfully written, your
  16864. * update function will be called again with the new current value, and the
  16865. * write will be retried. This will happen repeatedly until your write succeeds
  16866. * without conflict or you abort the transaction by not returning a value from
  16867. * your update function.
  16868. *
  16869. * Note: Modifying data with `set()` will cancel any pending transactions at
  16870. * that location, so extreme care should be taken if mixing `set()` and
  16871. * `runTransaction()` to update the same data.
  16872. *
  16873. * Note: When using transactions with Security and Firebase Rules in place, be
  16874. * aware that a client needs `.read` access in addition to `.write` access in
  16875. * order to perform a transaction. This is because the client-side nature of
  16876. * transactions requires the client to read the data in order to transactionally
  16877. * update it.
  16878. *
  16879. * @param ref - The location to atomically modify.
  16880. * @param transactionUpdate - A developer-supplied function which will be passed
  16881. * the current data stored at this location (as a JavaScript object). The
  16882. * function should return the new value it would like written (as a JavaScript
  16883. * object). If `undefined` is returned (i.e. you return with no arguments) the
  16884. * transaction will be aborted and the data at this location will not be
  16885. * modified.
  16886. * @param options - An options object to configure transactions.
  16887. * @returns A `Promise` that can optionally be used instead of the `onComplete`
  16888. * callback to handle success and failure.
  16889. */
  16890. function runTransaction(ref,
  16891. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  16892. transactionUpdate, options) {
  16893. var _a;
  16894. ref = util.getModularInstance(ref);
  16895. validateWritablePath('Reference.transaction', ref._path);
  16896. if (ref.key === '.length' || ref.key === '.keys') {
  16897. throw ('Reference.transaction failed: ' + ref.key + ' is a read-only object.');
  16898. }
  16899. var applyLocally = (_a = options === null || options === void 0 ? void 0 : options.applyLocally) !== null && _a !== void 0 ? _a : true;
  16900. var deferred = new util.Deferred();
  16901. var promiseComplete = function (error, committed, node) {
  16902. var dataSnapshot = null;
  16903. if (error) {
  16904. deferred.reject(error);
  16905. }
  16906. else {
  16907. dataSnapshot = new DataSnapshot$1(node, new ReferenceImpl(ref._repo, ref._path), PRIORITY_INDEX);
  16908. deferred.resolve(new TransactionResult$1(committed, dataSnapshot));
  16909. }
  16910. };
  16911. // Add a watch to make sure we get server updates.
  16912. var unwatcher = onValue(ref, function () { });
  16913. repoStartTransaction(ref._repo, ref._path, transactionUpdate, promiseComplete, unwatcher, applyLocally);
  16914. return deferred.promise;
  16915. }
  16916. /**
  16917. * @license
  16918. * Copyright 2017 Google LLC
  16919. *
  16920. * Licensed under the Apache License, Version 2.0 (the "License");
  16921. * you may not use this file except in compliance with the License.
  16922. * You may obtain a copy of the License at
  16923. *
  16924. * http://www.apache.org/licenses/LICENSE-2.0
  16925. *
  16926. * Unless required by applicable law or agreed to in writing, software
  16927. * distributed under the License is distributed on an "AS IS" BASIS,
  16928. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16929. * See the License for the specific language governing permissions and
  16930. * limitations under the License.
  16931. */
  16932. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  16933. PersistentConnection.prototype.simpleListen = function (pathString, onComplete) {
  16934. this.sendRequest('q', { p: pathString }, onComplete);
  16935. };
  16936. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  16937. PersistentConnection.prototype.echo = function (data, onEcho) {
  16938. this.sendRequest('echo', { d: data }, onEcho);
  16939. };
  16940. /**
  16941. * @internal
  16942. */
  16943. var hijackHash = function (newHash) {
  16944. var oldPut = PersistentConnection.prototype.put;
  16945. PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
  16946. if (hash !== undefined) {
  16947. hash = newHash();
  16948. }
  16949. oldPut.call(this, pathString, data, onComplete, hash);
  16950. };
  16951. return function () {
  16952. PersistentConnection.prototype.put = oldPut;
  16953. };
  16954. };
  16955. /**
  16956. * Forces the RepoManager to create Repos that use ReadonlyRestClient instead of PersistentConnection.
  16957. * @internal
  16958. */
  16959. var forceRestClient = function (forceRestClient) {
  16960. repoManagerForceRestClient(forceRestClient);
  16961. };
  16962. /**
  16963. * @license
  16964. * Copyright 2021 Google LLC
  16965. *
  16966. * Licensed under the Apache License, Version 2.0 (the "License");
  16967. * you may not use this file except in compliance with the License.
  16968. * You may obtain a copy of the License at
  16969. *
  16970. * http://www.apache.org/licenses/LICENSE-2.0
  16971. *
  16972. * Unless required by applicable law or agreed to in writing, software
  16973. * distributed under the License is distributed on an "AS IS" BASIS,
  16974. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16975. * See the License for the specific language governing permissions and
  16976. * limitations under the License.
  16977. */
  16978. setWebSocketImpl(Websocket__default["default"].Client);
  16979. index_standalone.DataSnapshot = DataSnapshot$1;
  16980. index_standalone.Database = Database$1;
  16981. var OnDisconnect_1 = index_standalone.OnDisconnect = OnDisconnect$1;
  16982. index_standalone.QueryConstraint = QueryConstraint;
  16983. index_standalone.TransactionResult = TransactionResult$1;
  16984. var _QueryImpl = index_standalone._QueryImpl = QueryImpl;
  16985. var _QueryParams = index_standalone._QueryParams = QueryParams;
  16986. var _ReferenceImpl = index_standalone._ReferenceImpl = ReferenceImpl;
  16987. index_standalone._TEST_ACCESS_forceRestClient = forceRestClient;
  16988. index_standalone._TEST_ACCESS_hijackHash = hijackHash;
  16989. var _repoManagerDatabaseFromApp = index_standalone._repoManagerDatabaseFromApp = repoManagerDatabaseFromApp;
  16990. var _setSDKVersion = index_standalone._setSDKVersion = setSDKVersion;
  16991. var _validatePathString = index_standalone._validatePathString = validatePathString;
  16992. var _validateWritablePath = index_standalone._validateWritablePath = validateWritablePath;
  16993. var child_1 = index_standalone.child = child;
  16994. var connectDatabaseEmulator_1 = index_standalone.connectDatabaseEmulator = connectDatabaseEmulator;
  16995. var enableLogging_1 = index_standalone.enableLogging = enableLogging;
  16996. var endAt_1 = index_standalone.endAt = endAt;
  16997. var endBefore_1 = index_standalone.endBefore = endBefore;
  16998. var equalTo_1 = index_standalone.equalTo = equalTo;
  16999. var forceLongPolling_1 = index_standalone.forceLongPolling = forceLongPolling;
  17000. var forceWebSockets_1 = index_standalone.forceWebSockets = forceWebSockets;
  17001. var get_1 = index_standalone.get = get;
  17002. var goOffline_1 = index_standalone.goOffline = goOffline;
  17003. var goOnline_1 = index_standalone.goOnline = goOnline;
  17004. var increment_1 = index_standalone.increment = increment;
  17005. var limitToFirst_1 = index_standalone.limitToFirst = limitToFirst;
  17006. var limitToLast_1 = index_standalone.limitToLast = limitToLast;
  17007. var off_1 = index_standalone.off = off;
  17008. var onChildAdded_1 = index_standalone.onChildAdded = onChildAdded;
  17009. var onChildChanged_1 = index_standalone.onChildChanged = onChildChanged;
  17010. var onChildMoved_1 = index_standalone.onChildMoved = onChildMoved;
  17011. var onChildRemoved_1 = index_standalone.onChildRemoved = onChildRemoved;
  17012. index_standalone.onDisconnect = onDisconnect;
  17013. var onValue_1 = index_standalone.onValue = onValue;
  17014. var orderByChild_1 = index_standalone.orderByChild = orderByChild;
  17015. var orderByKey_1 = index_standalone.orderByKey = orderByKey;
  17016. var orderByPriority_1 = index_standalone.orderByPriority = orderByPriority;
  17017. var orderByValue_1 = index_standalone.orderByValue = orderByValue;
  17018. var push_1 = index_standalone.push = push;
  17019. var query_1 = index_standalone.query = query;
  17020. var ref_1 = index_standalone.ref = ref;
  17021. var refFromURL_1 = index_standalone.refFromURL = refFromURL;
  17022. var remove_1 = index_standalone.remove = remove;
  17023. var runTransaction_1 = index_standalone.runTransaction = runTransaction;
  17024. var serverTimestamp_1 = index_standalone.serverTimestamp = serverTimestamp;
  17025. var set_1 = index_standalone.set = set;
  17026. var setPriority_1 = index_standalone.setPriority = setPriority;
  17027. var setWithPriority_1 = index_standalone.setWithPriority = setWithPriority;
  17028. var startAfter_1 = index_standalone.startAfter = startAfter;
  17029. var startAt_1 = index_standalone.startAt = startAt;
  17030. var update_1 = index_standalone.update = update;
  17031. /**
  17032. * @license
  17033. * Copyright 2021 Google LLC
  17034. *
  17035. * Licensed under the Apache License, Version 2.0 (the "License");
  17036. * you may not use this file except in compliance with the License.
  17037. * You may obtain a copy of the License at
  17038. *
  17039. * http://www.apache.org/licenses/LICENSE-2.0
  17040. *
  17041. * Unless required by applicable law or agreed to in writing, software
  17042. * distributed under the License is distributed on an "AS IS" BASIS,
  17043. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17044. * See the License for the specific language governing permissions and
  17045. * limitations under the License.
  17046. */
  17047. var logClient = new require$$3.Logger('@firebase/database-compat');
  17048. var warn = function (msg) {
  17049. var message = 'FIREBASE WARNING: ' + msg;
  17050. logClient.warn(message);
  17051. };
  17052. /**
  17053. * @license
  17054. * Copyright 2021 Google LLC
  17055. *
  17056. * Licensed under the Apache License, Version 2.0 (the "License");
  17057. * you may not use this file except in compliance with the License.
  17058. * You may obtain a copy of the License at
  17059. *
  17060. * http://www.apache.org/licenses/LICENSE-2.0
  17061. *
  17062. * Unless required by applicable law or agreed to in writing, software
  17063. * distributed under the License is distributed on an "AS IS" BASIS,
  17064. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17065. * See the License for the specific language governing permissions and
  17066. * limitations under the License.
  17067. */
  17068. var validateBoolean = function (fnName, argumentName, bool, optional) {
  17069. if (optional && bool === undefined) {
  17070. return;
  17071. }
  17072. if (typeof bool !== 'boolean') {
  17073. throw new Error(require$$1$3.errorPrefix(fnName, argumentName) + 'must be a boolean.');
  17074. }
  17075. };
  17076. var validateEventType = function (fnName, eventType, optional) {
  17077. if (optional && eventType === undefined) {
  17078. return;
  17079. }
  17080. switch (eventType) {
  17081. case 'value':
  17082. case 'child_added':
  17083. case 'child_removed':
  17084. case 'child_changed':
  17085. case 'child_moved':
  17086. break;
  17087. default:
  17088. throw new Error(require$$1$3.errorPrefix(fnName, 'eventType') +
  17089. 'must be a valid event type = "value", "child_added", "child_removed", ' +
  17090. '"child_changed", or "child_moved".');
  17091. }
  17092. };
  17093. /**
  17094. * @license
  17095. * Copyright 2017 Google LLC
  17096. *
  17097. * Licensed under the Apache License, Version 2.0 (the "License");
  17098. * you may not use this file except in compliance with the License.
  17099. * You may obtain a copy of the License at
  17100. *
  17101. * http://www.apache.org/licenses/LICENSE-2.0
  17102. *
  17103. * Unless required by applicable law or agreed to in writing, software
  17104. * distributed under the License is distributed on an "AS IS" BASIS,
  17105. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17106. * See the License for the specific language governing permissions and
  17107. * limitations under the License.
  17108. */
  17109. var OnDisconnect = /** @class */ (function () {
  17110. function OnDisconnect(_delegate) {
  17111. this._delegate = _delegate;
  17112. }
  17113. OnDisconnect.prototype.cancel = function (onComplete) {
  17114. require$$1$3.validateArgCount('OnDisconnect.cancel', 0, 1, arguments.length);
  17115. require$$1$3.validateCallback('OnDisconnect.cancel', 'onComplete', onComplete, true);
  17116. var result = this._delegate.cancel();
  17117. if (onComplete) {
  17118. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17119. }
  17120. return result;
  17121. };
  17122. OnDisconnect.prototype.remove = function (onComplete) {
  17123. require$$1$3.validateArgCount('OnDisconnect.remove', 0, 1, arguments.length);
  17124. require$$1$3.validateCallback('OnDisconnect.remove', 'onComplete', onComplete, true);
  17125. var result = this._delegate.remove();
  17126. if (onComplete) {
  17127. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17128. }
  17129. return result;
  17130. };
  17131. OnDisconnect.prototype.set = function (value, onComplete) {
  17132. require$$1$3.validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
  17133. require$$1$3.validateCallback('OnDisconnect.set', 'onComplete', onComplete, true);
  17134. var result = this._delegate.set(value);
  17135. if (onComplete) {
  17136. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17137. }
  17138. return result;
  17139. };
  17140. OnDisconnect.prototype.setWithPriority = function (value, priority, onComplete) {
  17141. require$$1$3.validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
  17142. require$$1$3.validateCallback('OnDisconnect.setWithPriority', 'onComplete', onComplete, true);
  17143. var result = this._delegate.setWithPriority(value, priority);
  17144. if (onComplete) {
  17145. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17146. }
  17147. return result;
  17148. };
  17149. OnDisconnect.prototype.update = function (objectToMerge, onComplete) {
  17150. require$$1$3.validateArgCount('OnDisconnect.update', 1, 2, arguments.length);
  17151. if (Array.isArray(objectToMerge)) {
  17152. var newObjectToMerge = {};
  17153. for (var i = 0; i < objectToMerge.length; ++i) {
  17154. newObjectToMerge['' + i] = objectToMerge[i];
  17155. }
  17156. objectToMerge = newObjectToMerge;
  17157. warn('Passing an Array to firebase.database.onDisconnect().update() is deprecated. Use set() if you want to overwrite the ' +
  17158. 'existing data, or an Object with integer keys if you really do want to only update some of the children.');
  17159. }
  17160. require$$1$3.validateCallback('OnDisconnect.update', 'onComplete', onComplete, true);
  17161. var result = this._delegate.update(objectToMerge);
  17162. if (onComplete) {
  17163. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17164. }
  17165. return result;
  17166. };
  17167. return OnDisconnect;
  17168. }());
  17169. /**
  17170. * @license
  17171. * Copyright 2017 Google LLC
  17172. *
  17173. * Licensed under the Apache License, Version 2.0 (the "License");
  17174. * you may not use this file except in compliance with the License.
  17175. * You may obtain a copy of the License at
  17176. *
  17177. * http://www.apache.org/licenses/LICENSE-2.0
  17178. *
  17179. * Unless required by applicable law or agreed to in writing, software
  17180. * distributed under the License is distributed on an "AS IS" BASIS,
  17181. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17182. * See the License for the specific language governing permissions and
  17183. * limitations under the License.
  17184. */
  17185. var TransactionResult = /** @class */ (function () {
  17186. /**
  17187. * A type for the resolve value of Firebase.transaction.
  17188. */
  17189. function TransactionResult(committed, snapshot) {
  17190. this.committed = committed;
  17191. this.snapshot = snapshot;
  17192. }
  17193. // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  17194. // for end-users
  17195. TransactionResult.prototype.toJSON = function () {
  17196. require$$1$3.validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
  17197. return { committed: this.committed, snapshot: this.snapshot.toJSON() };
  17198. };
  17199. return TransactionResult;
  17200. }());
  17201. /**
  17202. * @license
  17203. * Copyright 2017 Google LLC
  17204. *
  17205. * Licensed under the Apache License, Version 2.0 (the "License");
  17206. * you may not use this file except in compliance with the License.
  17207. * You may obtain a copy of the License at
  17208. *
  17209. * http://www.apache.org/licenses/LICENSE-2.0
  17210. *
  17211. * Unless required by applicable law or agreed to in writing, software
  17212. * distributed under the License is distributed on an "AS IS" BASIS,
  17213. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17214. * See the License for the specific language governing permissions and
  17215. * limitations under the License.
  17216. */
  17217. /**
  17218. * Class representing a firebase data snapshot. It wraps a SnapshotNode and
  17219. * surfaces the public methods (val, forEach, etc.) we want to expose.
  17220. */
  17221. var DataSnapshot = /** @class */ (function () {
  17222. function DataSnapshot(_database, _delegate) {
  17223. this._database = _database;
  17224. this._delegate = _delegate;
  17225. }
  17226. /**
  17227. * Retrieves the snapshot contents as JSON. Returns null if the snapshot is
  17228. * empty.
  17229. *
  17230. * @returns JSON representation of the DataSnapshot contents, or null if empty.
  17231. */
  17232. DataSnapshot.prototype.val = function () {
  17233. require$$1$3.validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
  17234. return this._delegate.val();
  17235. };
  17236. /**
  17237. * Returns the snapshot contents as JSON, including priorities of node. Suitable for exporting
  17238. * the entire node contents.
  17239. * @returns JSON representation of the DataSnapshot contents, or null if empty.
  17240. */
  17241. DataSnapshot.prototype.exportVal = function () {
  17242. require$$1$3.validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
  17243. return this._delegate.exportVal();
  17244. };
  17245. // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  17246. // for end-users
  17247. DataSnapshot.prototype.toJSON = function () {
  17248. // Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
  17249. require$$1$3.validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
  17250. return this._delegate.toJSON();
  17251. };
  17252. /**
  17253. * Returns whether the snapshot contains a non-null value.
  17254. *
  17255. * @returns Whether the snapshot contains a non-null value, or is empty.
  17256. */
  17257. DataSnapshot.prototype.exists = function () {
  17258. require$$1$3.validateArgCount('DataSnapshot.exists', 0, 0, arguments.length);
  17259. return this._delegate.exists();
  17260. };
  17261. /**
  17262. * Returns a DataSnapshot of the specified child node's contents.
  17263. *
  17264. * @param path - Path to a child.
  17265. * @returns DataSnapshot for child node.
  17266. */
  17267. DataSnapshot.prototype.child = function (path) {
  17268. require$$1$3.validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
  17269. // Ensure the childPath is a string (can be a number)
  17270. path = String(path);
  17271. _validatePathString('DataSnapshot.child', 'path', path, false);
  17272. return new DataSnapshot(this._database, this._delegate.child(path));
  17273. };
  17274. /**
  17275. * Returns whether the snapshot contains a child at the specified path.
  17276. *
  17277. * @param path - Path to a child.
  17278. * @returns Whether the child exists.
  17279. */
  17280. DataSnapshot.prototype.hasChild = function (path) {
  17281. require$$1$3.validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
  17282. _validatePathString('DataSnapshot.hasChild', 'path', path, false);
  17283. return this._delegate.hasChild(path);
  17284. };
  17285. /**
  17286. * Returns the priority of the object, or null if no priority was set.
  17287. *
  17288. * @returns The priority.
  17289. */
  17290. DataSnapshot.prototype.getPriority = function () {
  17291. require$$1$3.validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
  17292. return this._delegate.priority;
  17293. };
  17294. /**
  17295. * Iterates through child nodes and calls the specified action for each one.
  17296. *
  17297. * @param action - Callback function to be called
  17298. * for each child.
  17299. * @returns True if forEach was canceled by action returning true for
  17300. * one of the child nodes.
  17301. */
  17302. DataSnapshot.prototype.forEach = function (action) {
  17303. var _this = this;
  17304. require$$1$3.validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
  17305. require$$1$3.validateCallback('DataSnapshot.forEach', 'action', action, false);
  17306. return this._delegate.forEach(function (expDataSnapshot) {
  17307. return action(new DataSnapshot(_this._database, expDataSnapshot));
  17308. });
  17309. };
  17310. /**
  17311. * Returns whether this DataSnapshot has children.
  17312. * @returns True if the DataSnapshot contains 1 or more child nodes.
  17313. */
  17314. DataSnapshot.prototype.hasChildren = function () {
  17315. require$$1$3.validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
  17316. return this._delegate.hasChildren();
  17317. };
  17318. Object.defineProperty(DataSnapshot.prototype, "key", {
  17319. get: function () {
  17320. return this._delegate.key;
  17321. },
  17322. enumerable: false,
  17323. configurable: true
  17324. });
  17325. /**
  17326. * Returns the number of children for this DataSnapshot.
  17327. * @returns The number of children that this DataSnapshot contains.
  17328. */
  17329. DataSnapshot.prototype.numChildren = function () {
  17330. require$$1$3.validateArgCount('DataSnapshot.numChildren', 0, 0, arguments.length);
  17331. return this._delegate.size;
  17332. };
  17333. /**
  17334. * @returns The Firebase reference for the location this snapshot's data came
  17335. * from.
  17336. */
  17337. DataSnapshot.prototype.getRef = function () {
  17338. require$$1$3.validateArgCount('DataSnapshot.ref', 0, 0, arguments.length);
  17339. return new Reference(this._database, this._delegate.ref);
  17340. };
  17341. Object.defineProperty(DataSnapshot.prototype, "ref", {
  17342. get: function () {
  17343. return this.getRef();
  17344. },
  17345. enumerable: false,
  17346. configurable: true
  17347. });
  17348. return DataSnapshot;
  17349. }());
  17350. /**
  17351. * A Query represents a filter to be applied to a firebase location. This object purely represents the
  17352. * query expression (and exposes our public API to build the query). The actual query logic is in ViewBase.js.
  17353. *
  17354. * Since every Firebase reference is a query, Firebase inherits from this object.
  17355. */
  17356. var Query = /** @class */ (function () {
  17357. function Query(database, _delegate) {
  17358. this.database = database;
  17359. this._delegate = _delegate;
  17360. }
  17361. Query.prototype.on = function (eventType, callback, cancelCallbackOrContext, context) {
  17362. var _this = this;
  17363. var _a;
  17364. require$$1$3.validateArgCount('Query.on', 2, 4, arguments.length);
  17365. require$$1$3.validateCallback('Query.on', 'callback', callback, false);
  17366. var ret = Query.getCancelAndContextArgs_('Query.on', cancelCallbackOrContext, context);
  17367. var valueCallback = function (expSnapshot, previousChildName) {
  17368. callback.call(ret.context, new DataSnapshot(_this.database, expSnapshot), previousChildName);
  17369. };
  17370. valueCallback.userCallback = callback;
  17371. valueCallback.context = ret.context;
  17372. var cancelCallback = (_a = ret.cancel) === null || _a === void 0 ? void 0 : _a.bind(ret.context);
  17373. switch (eventType) {
  17374. case 'value':
  17375. onValue_1(this._delegate, valueCallback, cancelCallback);
  17376. return callback;
  17377. case 'child_added':
  17378. onChildAdded_1(this._delegate, valueCallback, cancelCallback);
  17379. return callback;
  17380. case 'child_removed':
  17381. onChildRemoved_1(this._delegate, valueCallback, cancelCallback);
  17382. return callback;
  17383. case 'child_changed':
  17384. onChildChanged_1(this._delegate, valueCallback, cancelCallback);
  17385. return callback;
  17386. case 'child_moved':
  17387. onChildMoved_1(this._delegate, valueCallback, cancelCallback);
  17388. return callback;
  17389. default:
  17390. throw new Error(require$$1$3.errorPrefix('Query.on', 'eventType') +
  17391. 'must be a valid event type = "value", "child_added", "child_removed", ' +
  17392. '"child_changed", or "child_moved".');
  17393. }
  17394. };
  17395. Query.prototype.off = function (eventType, callback, context) {
  17396. require$$1$3.validateArgCount('Query.off', 0, 3, arguments.length);
  17397. validateEventType('Query.off', eventType, true);
  17398. require$$1$3.validateCallback('Query.off', 'callback', callback, true);
  17399. require$$1$3.validateContextObject('Query.off', 'context', context, true);
  17400. if (callback) {
  17401. var valueCallback = function () { };
  17402. valueCallback.userCallback = callback;
  17403. valueCallback.context = context;
  17404. off_1(this._delegate, eventType, valueCallback);
  17405. }
  17406. else {
  17407. off_1(this._delegate, eventType);
  17408. }
  17409. };
  17410. /**
  17411. * Get the server-value for this query, or return a cached value if not connected.
  17412. */
  17413. Query.prototype.get = function () {
  17414. var _this = this;
  17415. return get_1(this._delegate).then(function (expSnapshot) {
  17416. return new DataSnapshot(_this.database, expSnapshot);
  17417. });
  17418. };
  17419. /**
  17420. * Attaches a listener, waits for the first event, and then removes the listener
  17421. */
  17422. Query.prototype.once = function (eventType, callback, failureCallbackOrContext, context) {
  17423. var _this = this;
  17424. require$$1$3.validateArgCount('Query.once', 1, 4, arguments.length);
  17425. require$$1$3.validateCallback('Query.once', 'callback', callback, true);
  17426. var ret = Query.getCancelAndContextArgs_('Query.once', failureCallbackOrContext, context);
  17427. var deferred = new require$$1$3.Deferred();
  17428. var valueCallback = function (expSnapshot, previousChildName) {
  17429. var result = new DataSnapshot(_this.database, expSnapshot);
  17430. if (callback) {
  17431. callback.call(ret.context, result, previousChildName);
  17432. }
  17433. deferred.resolve(result);
  17434. };
  17435. valueCallback.userCallback = callback;
  17436. valueCallback.context = ret.context;
  17437. var cancelCallback = function (error) {
  17438. if (ret.cancel) {
  17439. ret.cancel.call(ret.context, error);
  17440. }
  17441. deferred.reject(error);
  17442. };
  17443. switch (eventType) {
  17444. case 'value':
  17445. onValue_1(this._delegate, valueCallback, cancelCallback, {
  17446. onlyOnce: true
  17447. });
  17448. break;
  17449. case 'child_added':
  17450. onChildAdded_1(this._delegate, valueCallback, cancelCallback, {
  17451. onlyOnce: true
  17452. });
  17453. break;
  17454. case 'child_removed':
  17455. onChildRemoved_1(this._delegate, valueCallback, cancelCallback, {
  17456. onlyOnce: true
  17457. });
  17458. break;
  17459. case 'child_changed':
  17460. onChildChanged_1(this._delegate, valueCallback, cancelCallback, {
  17461. onlyOnce: true
  17462. });
  17463. break;
  17464. case 'child_moved':
  17465. onChildMoved_1(this._delegate, valueCallback, cancelCallback, {
  17466. onlyOnce: true
  17467. });
  17468. break;
  17469. default:
  17470. throw new Error(require$$1$3.errorPrefix('Query.once', 'eventType') +
  17471. 'must be a valid event type = "value", "child_added", "child_removed", ' +
  17472. '"child_changed", or "child_moved".');
  17473. }
  17474. return deferred.promise;
  17475. };
  17476. /**
  17477. * Set a limit and anchor it to the start of the window.
  17478. */
  17479. Query.prototype.limitToFirst = function (limit) {
  17480. require$$1$3.validateArgCount('Query.limitToFirst', 1, 1, arguments.length);
  17481. return new Query(this.database, query_1(this._delegate, limitToFirst_1(limit)));
  17482. };
  17483. /**
  17484. * Set a limit and anchor it to the end of the window.
  17485. */
  17486. Query.prototype.limitToLast = function (limit) {
  17487. require$$1$3.validateArgCount('Query.limitToLast', 1, 1, arguments.length);
  17488. return new Query(this.database, query_1(this._delegate, limitToLast_1(limit)));
  17489. };
  17490. /**
  17491. * Given a child path, return a new query ordered by the specified grandchild path.
  17492. */
  17493. Query.prototype.orderByChild = function (path) {
  17494. require$$1$3.validateArgCount('Query.orderByChild', 1, 1, arguments.length);
  17495. return new Query(this.database, query_1(this._delegate, orderByChild_1(path)));
  17496. };
  17497. /**
  17498. * Return a new query ordered by the KeyIndex
  17499. */
  17500. Query.prototype.orderByKey = function () {
  17501. require$$1$3.validateArgCount('Query.orderByKey', 0, 0, arguments.length);
  17502. return new Query(this.database, query_1(this._delegate, orderByKey_1()));
  17503. };
  17504. /**
  17505. * Return a new query ordered by the PriorityIndex
  17506. */
  17507. Query.prototype.orderByPriority = function () {
  17508. require$$1$3.validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
  17509. return new Query(this.database, query_1(this._delegate, orderByPriority_1()));
  17510. };
  17511. /**
  17512. * Return a new query ordered by the ValueIndex
  17513. */
  17514. Query.prototype.orderByValue = function () {
  17515. require$$1$3.validateArgCount('Query.orderByValue', 0, 0, arguments.length);
  17516. return new Query(this.database, query_1(this._delegate, orderByValue_1()));
  17517. };
  17518. Query.prototype.startAt = function (value, name) {
  17519. if (value === void 0) { value = null; }
  17520. require$$1$3.validateArgCount('Query.startAt', 0, 2, arguments.length);
  17521. return new Query(this.database, query_1(this._delegate, startAt_1(value, name)));
  17522. };
  17523. Query.prototype.startAfter = function (value, name) {
  17524. if (value === void 0) { value = null; }
  17525. require$$1$3.validateArgCount('Query.startAfter', 0, 2, arguments.length);
  17526. return new Query(this.database, query_1(this._delegate, startAfter_1(value, name)));
  17527. };
  17528. Query.prototype.endAt = function (value, name) {
  17529. if (value === void 0) { value = null; }
  17530. require$$1$3.validateArgCount('Query.endAt', 0, 2, arguments.length);
  17531. return new Query(this.database, query_1(this._delegate, endAt_1(value, name)));
  17532. };
  17533. Query.prototype.endBefore = function (value, name) {
  17534. if (value === void 0) { value = null; }
  17535. require$$1$3.validateArgCount('Query.endBefore', 0, 2, arguments.length);
  17536. return new Query(this.database, query_1(this._delegate, endBefore_1(value, name)));
  17537. };
  17538. /**
  17539. * Load the selection of children with exactly the specified value, and, optionally,
  17540. * the specified name.
  17541. */
  17542. Query.prototype.equalTo = function (value, name) {
  17543. require$$1$3.validateArgCount('Query.equalTo', 1, 2, arguments.length);
  17544. return new Query(this.database, query_1(this._delegate, equalTo_1(value, name)));
  17545. };
  17546. /**
  17547. * @returns URL for this location.
  17548. */
  17549. Query.prototype.toString = function () {
  17550. require$$1$3.validateArgCount('Query.toString', 0, 0, arguments.length);
  17551. return this._delegate.toString();
  17552. };
  17553. // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
  17554. // for end-users.
  17555. Query.prototype.toJSON = function () {
  17556. // An optional spacer argument is unnecessary for a string.
  17557. require$$1$3.validateArgCount('Query.toJSON', 0, 1, arguments.length);
  17558. return this._delegate.toJSON();
  17559. };
  17560. /**
  17561. * Return true if this query and the provided query are equivalent; otherwise, return false.
  17562. */
  17563. Query.prototype.isEqual = function (other) {
  17564. require$$1$3.validateArgCount('Query.isEqual', 1, 1, arguments.length);
  17565. if (!(other instanceof Query)) {
  17566. var error = 'Query.isEqual failed: First argument must be an instance of firebase.database.Query.';
  17567. throw new Error(error);
  17568. }
  17569. return this._delegate.isEqual(other._delegate);
  17570. };
  17571. /**
  17572. * Helper used by .on and .once to extract the context and or cancel arguments.
  17573. * @param fnName - The function name (on or once)
  17574. *
  17575. */
  17576. Query.getCancelAndContextArgs_ = function (fnName, cancelOrContext, context) {
  17577. var ret = { cancel: undefined, context: undefined };
  17578. if (cancelOrContext && context) {
  17579. ret.cancel = cancelOrContext;
  17580. require$$1$3.validateCallback(fnName, 'cancel', ret.cancel, true);
  17581. ret.context = context;
  17582. require$$1$3.validateContextObject(fnName, 'context', ret.context, true);
  17583. }
  17584. else if (cancelOrContext) {
  17585. // we have either a cancel callback or a context.
  17586. if (typeof cancelOrContext === 'object' && cancelOrContext !== null) {
  17587. // it's a context!
  17588. ret.context = cancelOrContext;
  17589. }
  17590. else if (typeof cancelOrContext === 'function') {
  17591. ret.cancel = cancelOrContext;
  17592. }
  17593. else {
  17594. throw new Error(require$$1$3.errorPrefix(fnName, 'cancelOrContext') +
  17595. ' must either be a cancel callback or a context object.');
  17596. }
  17597. }
  17598. return ret;
  17599. };
  17600. Object.defineProperty(Query.prototype, "ref", {
  17601. get: function () {
  17602. return new Reference(this.database, new _ReferenceImpl(this._delegate._repo, this._delegate._path));
  17603. },
  17604. enumerable: false,
  17605. configurable: true
  17606. });
  17607. return Query;
  17608. }());
  17609. var Reference = /** @class */ (function (_super) {
  17610. require$$2$3.__extends(Reference, _super);
  17611. /**
  17612. * Call options:
  17613. * new Reference(Repo, Path) or
  17614. * new Reference(url: string, string|RepoManager)
  17615. *
  17616. * Externally - this is the firebase.database.Reference type.
  17617. */
  17618. function Reference(database, _delegate) {
  17619. var _this = _super.call(this, database, new _QueryImpl(_delegate._repo, _delegate._path, new _QueryParams(), false)) || this;
  17620. _this.database = database;
  17621. _this._delegate = _delegate;
  17622. return _this;
  17623. }
  17624. /** @returns {?string} */
  17625. Reference.prototype.getKey = function () {
  17626. require$$1$3.validateArgCount('Reference.key', 0, 0, arguments.length);
  17627. return this._delegate.key;
  17628. };
  17629. Reference.prototype.child = function (pathString) {
  17630. require$$1$3.validateArgCount('Reference.child', 1, 1, arguments.length);
  17631. if (typeof pathString === 'number') {
  17632. pathString = String(pathString);
  17633. }
  17634. return new Reference(this.database, child_1(this._delegate, pathString));
  17635. };
  17636. /** @returns {?Reference} */
  17637. Reference.prototype.getParent = function () {
  17638. require$$1$3.validateArgCount('Reference.parent', 0, 0, arguments.length);
  17639. var parent = this._delegate.parent;
  17640. return parent ? new Reference(this.database, parent) : null;
  17641. };
  17642. /** @returns {!Reference} */
  17643. Reference.prototype.getRoot = function () {
  17644. require$$1$3.validateArgCount('Reference.root', 0, 0, arguments.length);
  17645. return new Reference(this.database, this._delegate.root);
  17646. };
  17647. Reference.prototype.set = function (newVal, onComplete) {
  17648. require$$1$3.validateArgCount('Reference.set', 1, 2, arguments.length);
  17649. require$$1$3.validateCallback('Reference.set', 'onComplete', onComplete, true);
  17650. var result = set_1(this._delegate, newVal);
  17651. if (onComplete) {
  17652. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17653. }
  17654. return result;
  17655. };
  17656. Reference.prototype.update = function (values, onComplete) {
  17657. require$$1$3.validateArgCount('Reference.update', 1, 2, arguments.length);
  17658. if (Array.isArray(values)) {
  17659. var newObjectToMerge = {};
  17660. for (var i = 0; i < values.length; ++i) {
  17661. newObjectToMerge['' + i] = values[i];
  17662. }
  17663. values = newObjectToMerge;
  17664. warn('Passing an Array to Firebase.update() is deprecated. ' +
  17665. 'Use set() if you want to overwrite the existing data, or ' +
  17666. 'an Object with integer keys if you really do want to ' +
  17667. 'only update some of the children.');
  17668. }
  17669. _validateWritablePath('Reference.update', this._delegate._path);
  17670. require$$1$3.validateCallback('Reference.update', 'onComplete', onComplete, true);
  17671. var result = update_1(this._delegate, values);
  17672. if (onComplete) {
  17673. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17674. }
  17675. return result;
  17676. };
  17677. Reference.prototype.setWithPriority = function (newVal, newPriority, onComplete) {
  17678. require$$1$3.validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
  17679. require$$1$3.validateCallback('Reference.setWithPriority', 'onComplete', onComplete, true);
  17680. var result = setWithPriority_1(this._delegate, newVal, newPriority);
  17681. if (onComplete) {
  17682. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17683. }
  17684. return result;
  17685. };
  17686. Reference.prototype.remove = function (onComplete) {
  17687. require$$1$3.validateArgCount('Reference.remove', 0, 1, arguments.length);
  17688. require$$1$3.validateCallback('Reference.remove', 'onComplete', onComplete, true);
  17689. var result = remove_1(this._delegate);
  17690. if (onComplete) {
  17691. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17692. }
  17693. return result;
  17694. };
  17695. Reference.prototype.transaction = function (transactionUpdate, onComplete, applyLocally) {
  17696. var _this = this;
  17697. require$$1$3.validateArgCount('Reference.transaction', 1, 3, arguments.length);
  17698. require$$1$3.validateCallback('Reference.transaction', 'transactionUpdate', transactionUpdate, false);
  17699. require$$1$3.validateCallback('Reference.transaction', 'onComplete', onComplete, true);
  17700. validateBoolean('Reference.transaction', 'applyLocally', applyLocally, true);
  17701. var result = runTransaction_1(this._delegate, transactionUpdate, {
  17702. applyLocally: applyLocally
  17703. }).then(function (transactionResult) {
  17704. return new TransactionResult(transactionResult.committed, new DataSnapshot(_this.database, transactionResult.snapshot));
  17705. });
  17706. if (onComplete) {
  17707. result.then(function (transactionResult) {
  17708. return onComplete(null, transactionResult.committed, transactionResult.snapshot);
  17709. }, function (error) { return onComplete(error, false, null); });
  17710. }
  17711. return result;
  17712. };
  17713. Reference.prototype.setPriority = function (priority, onComplete) {
  17714. require$$1$3.validateArgCount('Reference.setPriority', 1, 2, arguments.length);
  17715. require$$1$3.validateCallback('Reference.setPriority', 'onComplete', onComplete, true);
  17716. var result = setPriority_1(this._delegate, priority);
  17717. if (onComplete) {
  17718. result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17719. }
  17720. return result;
  17721. };
  17722. Reference.prototype.push = function (value, onComplete) {
  17723. var _this = this;
  17724. require$$1$3.validateArgCount('Reference.push', 0, 2, arguments.length);
  17725. require$$1$3.validateCallback('Reference.push', 'onComplete', onComplete, true);
  17726. var expPromise = push_1(this._delegate, value);
  17727. var promise = expPromise.then(function (expRef) { return new Reference(_this.database, expRef); });
  17728. if (onComplete) {
  17729. promise.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
  17730. }
  17731. var result = new Reference(this.database, expPromise);
  17732. result.then = promise.then.bind(promise);
  17733. result.catch = promise.catch.bind(promise, undefined);
  17734. return result;
  17735. };
  17736. Reference.prototype.onDisconnect = function () {
  17737. _validateWritablePath('Reference.onDisconnect', this._delegate._path);
  17738. return new OnDisconnect(new OnDisconnect_1(this._delegate._repo, this._delegate._path));
  17739. };
  17740. Object.defineProperty(Reference.prototype, "key", {
  17741. get: function () {
  17742. return this.getKey();
  17743. },
  17744. enumerable: false,
  17745. configurable: true
  17746. });
  17747. Object.defineProperty(Reference.prototype, "parent", {
  17748. get: function () {
  17749. return this.getParent();
  17750. },
  17751. enumerable: false,
  17752. configurable: true
  17753. });
  17754. Object.defineProperty(Reference.prototype, "root", {
  17755. get: function () {
  17756. return this.getRoot();
  17757. },
  17758. enumerable: false,
  17759. configurable: true
  17760. });
  17761. return Reference;
  17762. }(Query));
  17763. /**
  17764. * @license
  17765. * Copyright 2017 Google LLC
  17766. *
  17767. * Licensed under the Apache License, Version 2.0 (the "License");
  17768. * you may not use this file except in compliance with the License.
  17769. * You may obtain a copy of the License at
  17770. *
  17771. * http://www.apache.org/licenses/LICENSE-2.0
  17772. *
  17773. * Unless required by applicable law or agreed to in writing, software
  17774. * distributed under the License is distributed on an "AS IS" BASIS,
  17775. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17776. * See the License for the specific language governing permissions and
  17777. * limitations under the License.
  17778. */
  17779. /**
  17780. * Class representing a firebase database.
  17781. */
  17782. var Database = /** @class */ (function () {
  17783. /**
  17784. * The constructor should not be called by users of our public API.
  17785. */
  17786. function Database(_delegate, app) {
  17787. var _this = this;
  17788. this._delegate = _delegate;
  17789. this.app = app;
  17790. this.INTERNAL = {
  17791. delete: function () { return _this._delegate._delete(); },
  17792. forceWebSockets: forceWebSockets_1,
  17793. forceLongPolling: forceLongPolling_1
  17794. };
  17795. }
  17796. /**
  17797. * Modify this instance to communicate with the Realtime Database emulator.
  17798. *
  17799. * <p>Note: This method must be called before performing any other operation.
  17800. *
  17801. * @param host - the emulator host (ex: localhost)
  17802. * @param port - the emulator port (ex: 8080)
  17803. * @param options.mockUserToken - the mock auth token to use for unit testing Security Rules
  17804. */
  17805. Database.prototype.useEmulator = function (host, port, options) {
  17806. if (options === void 0) { options = {}; }
  17807. connectDatabaseEmulator_1(this._delegate, host, port, options);
  17808. };
  17809. Database.prototype.ref = function (path) {
  17810. require$$1$3.validateArgCount('database.ref', 0, 1, arguments.length);
  17811. if (path instanceof Reference) {
  17812. var childRef = refFromURL_1(this._delegate, path.toString());
  17813. return new Reference(this, childRef);
  17814. }
  17815. else {
  17816. var childRef = ref_1(this._delegate, path);
  17817. return new Reference(this, childRef);
  17818. }
  17819. };
  17820. /**
  17821. * Returns a reference to the root or the path specified in url.
  17822. * We throw a exception if the url is not in the same domain as the
  17823. * current repo.
  17824. * @returns Firebase reference.
  17825. */
  17826. Database.prototype.refFromURL = function (url) {
  17827. var apiName = 'database.refFromURL';
  17828. require$$1$3.validateArgCount(apiName, 1, 1, arguments.length);
  17829. var childRef = refFromURL_1(this._delegate, url);
  17830. return new Reference(this, childRef);
  17831. };
  17832. // Make individual repo go offline.
  17833. Database.prototype.goOffline = function () {
  17834. require$$1$3.validateArgCount('database.goOffline', 0, 0, arguments.length);
  17835. return goOffline_1(this._delegate);
  17836. };
  17837. Database.prototype.goOnline = function () {
  17838. require$$1$3.validateArgCount('database.goOnline', 0, 0, arguments.length);
  17839. return goOnline_1(this._delegate);
  17840. };
  17841. Database.ServerValue = {
  17842. TIMESTAMP: serverTimestamp_1(),
  17843. increment: function (delta) { return increment_1(delta); }
  17844. };
  17845. return Database;
  17846. }());
  17847. /**
  17848. * @license
  17849. * Copyright 2017 Google LLC
  17850. *
  17851. * Licensed under the Apache License, Version 2.0 (the "License");
  17852. * you may not use this file except in compliance with the License.
  17853. * You may obtain a copy of the License at
  17854. *
  17855. * http://www.apache.org/licenses/LICENSE-2.0
  17856. *
  17857. * Unless required by applicable law or agreed to in writing, software
  17858. * distributed under the License is distributed on an "AS IS" BASIS,
  17859. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17860. * See the License for the specific language governing permissions and
  17861. * limitations under the License.
  17862. */
  17863. /**
  17864. * Used by console to create a database based on the app,
  17865. * passed database URL and a custom auth implementation.
  17866. *
  17867. * @param app - A valid FirebaseApp-like object
  17868. * @param url - A valid Firebase databaseURL
  17869. * @param version - custom version e.g. firebase-admin version
  17870. * @param customAuthImpl - custom auth implementation
  17871. */
  17872. function initStandalone$1(_a) {
  17873. var app = _a.app, url = _a.url, version = _a.version, customAuthImpl = _a.customAuthImpl, namespace = _a.namespace, _b = _a.nodeAdmin, nodeAdmin = _b === void 0 ? false : _b;
  17874. _setSDKVersion(version);
  17875. /**
  17876. * ComponentContainer('database-standalone') is just a placeholder that doesn't perform
  17877. * any actual function.
  17878. */
  17879. var authProvider = new component.Provider('auth-internal', new component.ComponentContainer('database-standalone'));
  17880. authProvider.setComponent(new component.Component('auth-internal', function () { return customAuthImpl; }, "PRIVATE" /* ComponentType.PRIVATE */));
  17881. return {
  17882. instance: new Database(_repoManagerDatabaseFromApp(app, authProvider,
  17883. /* appCheckProvider= */ undefined, url, nodeAdmin), app),
  17884. namespace: namespace
  17885. };
  17886. }
  17887. var INTERNAL = /*#__PURE__*/Object.freeze({
  17888. __proto__: null,
  17889. initStandalone: initStandalone$1
  17890. });
  17891. /**
  17892. * @license
  17893. * Copyright 2021 Google LLC
  17894. *
  17895. * Licensed under the Apache License, Version 2.0 (the "License");
  17896. * you may not use this file except in compliance with the License.
  17897. * You may obtain a copy of the License at
  17898. *
  17899. * http://www.apache.org/licenses/LICENSE-2.0
  17900. *
  17901. * Unless required by applicable law or agreed to in writing, software
  17902. * distributed under the License is distributed on an "AS IS" BASIS,
  17903. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17904. * See the License for the specific language governing permissions and
  17905. * limitations under the License.
  17906. */
  17907. var ServerValue = Database.ServerValue;
  17908. /**
  17909. * A one off register function which returns a database based on the app and
  17910. * passed database URL. (Used by the Admin SDK)
  17911. *
  17912. * @param app - A valid FirebaseApp-like object
  17913. * @param url - A valid Firebase databaseURL
  17914. * @param version - custom version e.g. firebase-admin version
  17915. * @param nodeAdmin - true if the SDK is being initialized from Firebase Admin.
  17916. */
  17917. function initStandalone(app, url, version, nodeAdmin) {
  17918. if (nodeAdmin === void 0) { nodeAdmin = true; }
  17919. require$$1$3.CONSTANTS.NODE_ADMIN = nodeAdmin;
  17920. return initStandalone$1({
  17921. app: app,
  17922. url: url,
  17923. version: version,
  17924. // firebase-admin-node's app.INTERNAL implements FirebaseAuthInternal interface
  17925. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  17926. customAuthImpl: app.INTERNAL,
  17927. namespace: {
  17928. Reference: Reference,
  17929. Query: Query,
  17930. Database: Database,
  17931. DataSnapshot: DataSnapshot,
  17932. enableLogging: enableLogging_1,
  17933. INTERNAL: INTERNAL,
  17934. ServerValue: ServerValue
  17935. },
  17936. nodeAdmin: nodeAdmin
  17937. });
  17938. }
  17939. exports.DataSnapshot = DataSnapshot;
  17940. exports.Database = Database;
  17941. exports.OnDisconnect = OnDisconnect_1;
  17942. exports.Query = Query;
  17943. exports.Reference = Reference;
  17944. exports.ServerValue = ServerValue;
  17945. exports.enableLogging = enableLogging_1;
  17946. exports.initStandalone = initStandalone;
  17947. //# sourceMappingURL=index.standalone.js.map