12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', { value: true });
-
- var require$$2 = require('util');
- var require$$0 = require('buffer');
- var require$$1 = require('events');
- var require$$0$1 = require('stream');
- var require$$1$1 = require('crypto');
- var require$$2$1 = require('url');
- var require$$0$2 = require('assert');
- var require$$1$2 = require('net');
- var require$$2$2 = require('tls');
- var require$$1$3 = require('@firebase/util');
- var require$$2$3 = require('tslib');
- var require$$3 = require('@firebase/logger');
- var component = require('@firebase/component');
-
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
-
- var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2);
- var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
- var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
- var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
- var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
- var require$$2__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$2$1);
- var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
- var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
- var require$$2__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$2$2);
- var require$$1__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$1$3);
- var require$$2__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$2$3);
- var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3);
-
- var index_standalone = {};
-
- var safeBuffer = {exports: {}};
-
- /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
-
- (function (module, exports) {
- /* eslint-disable node/no-deprecated-api */
- var buffer = require$$0__default["default"];
- var Buffer = buffer.Buffer;
-
- // alternative to using Object.keys for old browsers
- function copyProps (src, dst) {
- for (var key in src) {
- dst[key] = src[key];
- }
- }
- if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
- module.exports = buffer;
- } else {
- // Copy properties from require('buffer')
- copyProps(buffer, exports);
- exports.Buffer = SafeBuffer;
- }
-
- function SafeBuffer (arg, encodingOrOffset, length) {
- return Buffer(arg, encodingOrOffset, length)
- }
-
- SafeBuffer.prototype = Object.create(Buffer.prototype);
-
- // Copy static methods from Buffer
- copyProps(Buffer, SafeBuffer);
-
- SafeBuffer.from = function (arg, encodingOrOffset, length) {
- if (typeof arg === 'number') {
- throw new TypeError('Argument must not be a number')
- }
- return Buffer(arg, encodingOrOffset, length)
- };
-
- SafeBuffer.alloc = function (size, fill, encoding) {
- if (typeof size !== 'number') {
- throw new TypeError('Argument must be a number')
- }
- var buf = Buffer(size);
- if (fill !== undefined) {
- if (typeof encoding === 'string') {
- buf.fill(fill, encoding);
- } else {
- buf.fill(fill);
- }
- } else {
- buf.fill(0);
- }
- return buf
- };
-
- SafeBuffer.allocUnsafe = function (size) {
- if (typeof size !== 'number') {
- throw new TypeError('Argument must be a number')
- }
- return Buffer(size)
- };
-
- SafeBuffer.allocUnsafeSlow = function (size) {
- if (typeof size !== 'number') {
- throw new TypeError('Argument must be a number')
- }
- return buffer.SlowBuffer(size)
- };
- }(safeBuffer, safeBuffer.exports));
-
- var streams$1 = {};
-
- /**
-
- Streams in a WebSocket connection
- ---------------------------------
-
- We model a WebSocket as two duplex streams: one stream is for the wire protocol
- over an I/O socket, and the other is for incoming/outgoing messages.
-
-
- +----------+ +---------+ +----------+
- [1] write(chunk) -->| ~~~~~~~~ +----->| parse() +----->| ~~~~~~~~ +--> emit('data') [2]
- | | +----+----+ | |
- | | | | |
- | IO | | [5] | Messages |
- | | V | |
- | | +---------+ | |
- [4] emit('data') <--+ ~~~~~~~~ |<-----+ frame() |<-----+ ~~~~~~~~ |<-- write(chunk) [3]
- +----------+ +---------+ +----------+
-
-
- Message transfer in each direction is simple: IO receives a byte stream [1] and
- sends this stream for parsing. The parser will periodically emit a complete
- message text on the Messages stream [2]. Similarly, when messages are written
- to the Messages stream [3], they are framed using the WebSocket wire format and
- emitted via IO [4].
-
- There is a feedback loop via [5] since some input from [1] will be things like
- ping, pong and close frames. In these cases the protocol responds by emitting
- responses directly back to [4] rather than emitting messages via [2].
-
- For the purposes of flow control, we consider the sources of each Readable
- stream to be as follows:
-
- * [2] receives input from [1]
- * [4] receives input from [1] and [3]
-
- The classes below express the relationships described above without prescribing
- anything about how parse() and frame() work, other than assuming they emit
- 'data' events to the IO and Messages streams. They will work with any protocol
- driver having these two methods.
- **/
-
-
- var Stream$3 = require$$0__default$1["default"].Stream,
- util$c = require$$2__default["default"];
-
-
- var IO = function(driver) {
- this.readable = this.writable = true;
- this._paused = false;
- this._driver = driver;
- };
- util$c.inherits(IO, Stream$3);
-
- // The IO pause() and resume() methods will be called when the socket we are
- // piping to gets backed up and drains. Since IO output [4] comes from IO input
- // [1] and Messages input [3], we need to tell both of those to return false
- // from write() when this stream is paused.
-
- IO.prototype.pause = function() {
- this._paused = true;
- this._driver.messages._paused = true;
- };
-
- IO.prototype.resume = function() {
- this._paused = false;
- this.emit('drain');
-
- var messages = this._driver.messages;
- messages._paused = false;
- messages.emit('drain');
- };
-
- // When we receive input from a socket, send it to the parser and tell the
- // source whether to back off.
- IO.prototype.write = function(chunk) {
- if (!this.writable) return false;
- this._driver.parse(chunk);
- return !this._paused;
- };
-
- // The IO end() method will be called when the socket piping into it emits
- // 'close' or 'end', i.e. the socket is closed. In this situation the Messages
- // stream will not emit any more data so we emit 'end'.
- IO.prototype.end = function(chunk) {
- if (!this.writable) return;
- if (chunk !== undefined) this.write(chunk);
- this.writable = false;
-
- var messages = this._driver.messages;
- if (messages.readable) {
- messages.readable = messages.writable = false;
- messages.emit('end');
- }
- };
-
- IO.prototype.destroy = function() {
- this.end();
- };
-
-
- var Messages = function(driver) {
- this.readable = this.writable = true;
- this._paused = false;
- this._driver = driver;
- };
- util$c.inherits(Messages, Stream$3);
-
- // The Messages pause() and resume() methods will be called when the app that's
- // processing the messages gets backed up and drains. If we're emitting
- // messages too fast we should tell the source to slow down. Message output [2]
- // comes from IO input [1].
-
- Messages.prototype.pause = function() {
- this._driver.io._paused = true;
- };
-
- Messages.prototype.resume = function() {
- this._driver.io._paused = false;
- this._driver.io.emit('drain');
- };
-
- // When we receive messages from the user, send them to the formatter and tell
- // the source whether to back off.
- Messages.prototype.write = function(message) {
- if (!this.writable) return false;
- if (typeof message === 'string') this._driver.text(message);
- else this._driver.binary(message);
- return !this._paused;
- };
-
- // The Messages end() method will be called when a stream piping into it emits
- // 'end'. Many streams may be piped into the WebSocket and one of them ending
- // does not mean the whole socket is done, so just process the input and move
- // on leaving the socket open.
- Messages.prototype.end = function(message) {
- if (message !== undefined) this.write(message);
- };
-
- Messages.prototype.destroy = function() {};
-
-
- streams$1.IO = IO;
- streams$1.Messages = Messages;
-
- var Headers$3 = function() {
- this.clear();
- };
-
- Headers$3.prototype.ALLOWED_DUPLICATES = ['set-cookie', 'set-cookie2', 'warning', 'www-authenticate'];
-
- Headers$3.prototype.clear = function() {
- this._sent = {};
- this._lines = [];
- };
-
- Headers$3.prototype.set = function(name, value) {
- if (value === undefined) return;
-
- name = this._strip(name);
- value = this._strip(value);
-
- var key = name.toLowerCase();
- if (!this._sent.hasOwnProperty(key) || this.ALLOWED_DUPLICATES.indexOf(key) >= 0) {
- this._sent[key] = true;
- this._lines.push(name + ': ' + value + '\r\n');
- }
- };
-
- Headers$3.prototype.toString = function() {
- return this._lines.join('');
- };
-
- Headers$3.prototype._strip = function(string) {
- return string.toString().replace(/^ */, '').replace(/ *$/, '');
- };
-
- var headers = Headers$3;
-
- var Buffer$9 = safeBuffer.exports.Buffer;
-
- var StreamReader = function() {
- this._queue = [];
- this._queueSize = 0;
- this._offset = 0;
- };
-
- StreamReader.prototype.put = function(buffer) {
- if (!buffer || buffer.length === 0) return;
- if (!Buffer$9.isBuffer(buffer)) buffer = Buffer$9.from(buffer);
- this._queue.push(buffer);
- this._queueSize += buffer.length;
- };
-
- StreamReader.prototype.read = function(length) {
- if (length > this._queueSize) return null;
- if (length === 0) return Buffer$9.alloc(0);
-
- this._queueSize -= length;
-
- var queue = this._queue,
- remain = length,
- first = queue[0],
- buffers, buffer;
-
- if (first.length >= length) {
- if (first.length === length) {
- return queue.shift();
- } else {
- buffer = first.slice(0, length);
- queue[0] = first.slice(length);
- return buffer;
- }
- }
-
- for (var i = 0, n = queue.length; i < n; i++) {
- if (remain < queue[i].length) break;
- remain -= queue[i].length;
- }
- buffers = queue.splice(0, i);
-
- if (remain > 0 && queue.length > 0) {
- buffers.push(queue[0].slice(0, remain));
- queue[0] = queue[0].slice(remain);
- }
- return Buffer$9.concat(buffers, length);
- };
-
- StreamReader.prototype.eachByte = function(callback, context) {
- var buffer, n, index;
-
- while (this._queue.length > 0) {
- buffer = this._queue[0];
- n = buffer.length;
-
- while (this._offset < n) {
- index = this._offset;
- this._offset += 1;
- callback.call(context, buffer[index]);
- }
- this._offset = 0;
- this._queue.shift();
- }
- };
-
- var stream_reader = StreamReader;
-
- var Buffer$8 = safeBuffer.exports.Buffer,
- Emitter = require$$1__default["default"].EventEmitter,
- util$b = require$$2__default["default"],
- streams = streams$1,
- Headers$2 = headers,
- Reader = stream_reader;
-
- var Base$7 = function(request, url, options) {
- Emitter.call(this);
- Base$7.validateOptions(options || {}, ['maxLength', 'masking', 'requireMasking', 'protocols']);
-
- this._request = request;
- this._reader = new Reader();
- this._options = options || {};
- this._maxLength = this._options.maxLength || this.MAX_LENGTH;
- this._headers = new Headers$2();
- this.__queue = [];
- this.readyState = 0;
- this.url = url;
-
- this.io = new streams.IO(this);
- this.messages = new streams.Messages(this);
- this._bindEventListeners();
- };
- util$b.inherits(Base$7, Emitter);
-
- Base$7.isWebSocket = function(request) {
- var connection = request.headers.connection || '',
- upgrade = request.headers.upgrade || '';
-
- return request.method === 'GET' &&
- connection.toLowerCase().split(/ *, */).indexOf('upgrade') >= 0 &&
- upgrade.toLowerCase() === 'websocket';
- };
-
- Base$7.validateOptions = function(options, validKeys) {
- for (var key in options) {
- if (validKeys.indexOf(key) < 0)
- throw new Error('Unrecognized option: ' + key);
- }
- };
-
- var instance$b = {
- // This is 64MB, small enough for an average VPS to handle without
- // crashing from process out of memory
- MAX_LENGTH: 0x3ffffff,
-
- STATES: ['connecting', 'open', 'closing', 'closed'],
-
- _bindEventListeners: function() {
- var self = this;
-
- // Protocol errors are informational and do not have to be handled
- this.messages.on('error', function() {});
-
- this.on('message', function(event) {
- var messages = self.messages;
- if (messages.readable) messages.emit('data', event.data);
- });
-
- this.on('error', function(error) {
- var messages = self.messages;
- if (messages.readable) messages.emit('error', error);
- });
-
- this.on('close', function() {
- var messages = self.messages;
- if (!messages.readable) return;
- messages.readable = messages.writable = false;
- messages.emit('end');
- });
- },
-
- getState: function() {
- return this.STATES[this.readyState] || null;
- },
-
- addExtension: function(extension) {
- return false;
- },
-
- setHeader: function(name, value) {
- if (this.readyState > 0) return false;
- this._headers.set(name, value);
- return true;
- },
-
- start: function() {
- if (this.readyState !== 0) return false;
-
- if (!Base$7.isWebSocket(this._request))
- return this._failHandshake(new Error('Not a WebSocket request'));
-
- var response;
-
- try {
- response = this._handshakeResponse();
- } catch (error) {
- return this._failHandshake(error);
- }
-
- this._write(response);
- if (this._stage !== -1) this._open();
- return true;
- },
-
- _failHandshake: function(error) {
- var headers = new Headers$2();
- headers.set('Content-Type', 'text/plain');
- headers.set('Content-Length', Buffer$8.byteLength(error.message, 'utf8'));
-
- headers = ['HTTP/1.1 400 Bad Request', headers.toString(), error.message];
- this._write(Buffer$8.from(headers.join('\r\n'), 'utf8'));
- this._fail('protocol_error', error.message);
-
- return false;
- },
-
- text: function(message) {
- return this.frame(message);
- },
-
- binary: function(message) {
- return false;
- },
-
- ping: function() {
- return false;
- },
-
- pong: function() {
- return false;
- },
-
- close: function(reason, code) {
- if (this.readyState !== 1) return false;
- this.readyState = 3;
- this.emit('close', new Base$7.CloseEvent(null, null));
- return true;
- },
-
- _open: function() {
- this.readyState = 1;
- this.__queue.forEach(function(args) { this.frame.apply(this, args); }, this);
- this.__queue = [];
- this.emit('open', new Base$7.OpenEvent());
- },
-
- _queue: function(message) {
- this.__queue.push(message);
- return true;
- },
-
- _write: function(chunk) {
- var io = this.io;
- if (io.readable) io.emit('data', chunk);
- },
-
- _fail: function(type, message) {
- this.readyState = 2;
- this.emit('error', new Error(message));
- this.close();
- }
- };
-
- for (var key$b in instance$b)
- Base$7.prototype[key$b] = instance$b[key$b];
-
-
- Base$7.ConnectEvent = function() {};
-
- Base$7.OpenEvent = function() {};
-
- Base$7.CloseEvent = function(code, reason) {
- this.code = code;
- this.reason = reason;
- };
-
- Base$7.MessageEvent = function(data) {
- this.data = data;
- };
-
- Base$7.PingEvent = function(data) {
- this.data = data;
- };
-
- Base$7.PongEvent = function(data) {
- this.data = data;
- };
-
- var base = Base$7;
-
- var httpParser = {};
-
- /*jshint node:true */
- var assert = require$$0__default$2["default"];
-
- httpParser.HTTPParser = HTTPParser;
- function HTTPParser(type) {
- assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE || type === undefined);
- if (type === undefined) ; else {
- this.initialize(type);
- }
- }
- HTTPParser.prototype.initialize = function (type, async_resource) {
- assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE);
- this.type = type;
- this.state = type + '_LINE';
- this.info = {
- headers: [],
- upgrade: false
- };
- this.trailers = [];
- this.line = '';
- this.isChunked = false;
- this.connection = '';
- this.headerSize = 0; // for preventing too big headers
- this.body_bytes = null;
- this.isUserCall = false;
- this.hadError = false;
- };
-
- HTTPParser.encoding = 'ascii';
- HTTPParser.maxHeaderSize = 80 * 1024; // maxHeaderSize (in bytes) is configurable, but 80kb by default;
- HTTPParser.REQUEST = 'REQUEST';
- HTTPParser.RESPONSE = 'RESPONSE';
-
- // Note: *not* starting with kOnHeaders=0 line the Node parser, because any
- // newly added constants (kOnTimeout in Node v12.19.0) will overwrite 0!
- var kOnHeaders = HTTPParser.kOnHeaders = 1;
- var kOnHeadersComplete = HTTPParser.kOnHeadersComplete = 2;
- var kOnBody = HTTPParser.kOnBody = 3;
- var kOnMessageComplete = HTTPParser.kOnMessageComplete = 4;
-
- // Some handler stubs, needed for compatibility
- HTTPParser.prototype[kOnHeaders] =
- HTTPParser.prototype[kOnHeadersComplete] =
- HTTPParser.prototype[kOnBody] =
- HTTPParser.prototype[kOnMessageComplete] = function () {};
-
- var compatMode0_12 = true;
- Object.defineProperty(HTTPParser, 'kOnExecute', {
- get: function () {
- // hack for backward compatibility
- compatMode0_12 = false;
- return 99;
- }
- });
-
- var methods = httpParser.methods = HTTPParser.methods = [
- 'DELETE',
- 'GET',
- 'HEAD',
- 'POST',
- 'PUT',
- 'CONNECT',
- 'OPTIONS',
- 'TRACE',
- 'COPY',
- 'LOCK',
- 'MKCOL',
- 'MOVE',
- 'PROPFIND',
- 'PROPPATCH',
- 'SEARCH',
- 'UNLOCK',
- 'BIND',
- 'REBIND',
- 'UNBIND',
- 'ACL',
- 'REPORT',
- 'MKACTIVITY',
- 'CHECKOUT',
- 'MERGE',
- 'M-SEARCH',
- 'NOTIFY',
- 'SUBSCRIBE',
- 'UNSUBSCRIBE',
- 'PATCH',
- 'PURGE',
- 'MKCALENDAR',
- 'LINK',
- 'UNLINK'
- ];
- var method_connect = methods.indexOf('CONNECT');
- HTTPParser.prototype.reinitialize = HTTPParser;
- HTTPParser.prototype.close =
- HTTPParser.prototype.pause =
- HTTPParser.prototype.resume =
- HTTPParser.prototype.free = function () {};
- HTTPParser.prototype._compatMode0_11 = false;
- HTTPParser.prototype.getAsyncId = function() { return 0; };
-
- var headerState = {
- REQUEST_LINE: true,
- RESPONSE_LINE: true,
- HEADER: true
- };
- HTTPParser.prototype.execute = function (chunk, start, length) {
- if (!(this instanceof HTTPParser)) {
- throw new TypeError('not a HTTPParser');
- }
-
- // backward compat to node < 0.11.4
- // Note: the start and length params were removed in newer version
- start = start || 0;
- length = typeof length === 'number' ? length : chunk.length;
-
- this.chunk = chunk;
- this.offset = start;
- var end = this.end = start + length;
- try {
- while (this.offset < end) {
- if (this[this.state]()) {
- break;
- }
- }
- } catch (err) {
- if (this.isUserCall) {
- throw err;
- }
- this.hadError = true;
- return err;
- }
- this.chunk = null;
- length = this.offset - start;
- if (headerState[this.state]) {
- this.headerSize += length;
- if (this.headerSize > HTTPParser.maxHeaderSize) {
- return new Error('max header size exceeded');
- }
- }
- return length;
- };
-
- var stateFinishAllowed = {
- REQUEST_LINE: true,
- RESPONSE_LINE: true,
- BODY_RAW: true
- };
- HTTPParser.prototype.finish = function () {
- if (this.hadError) {
- return;
- }
- if (!stateFinishAllowed[this.state]) {
- return new Error('invalid state for EOF');
- }
- if (this.state === 'BODY_RAW') {
- this.userCall()(this[kOnMessageComplete]());
- }
- };
-
- // These three methods are used for an internal speed optimization, and it also
- // works if theses are noops. Basically consume() asks us to read the bytes
- // ourselves, but if we don't do it we get them through execute().
- HTTPParser.prototype.consume =
- HTTPParser.prototype.unconsume =
- HTTPParser.prototype.getCurrentBuffer = function () {};
-
- //For correct error handling - see HTTPParser#execute
- //Usage: this.userCall()(userFunction('arg'));
- HTTPParser.prototype.userCall = function () {
- this.isUserCall = true;
- var self = this;
- return function (ret) {
- self.isUserCall = false;
- return ret;
- };
- };
-
- HTTPParser.prototype.nextRequest = function () {
- this.userCall()(this[kOnMessageComplete]());
- this.reinitialize(this.type);
- };
-
- HTTPParser.prototype.consumeLine = function () {
- var end = this.end,
- chunk = this.chunk;
- for (var i = this.offset; i < end; i++) {
- if (chunk[i] === 0x0a) { // \n
- var line = this.line + chunk.toString(HTTPParser.encoding, this.offset, i);
- if (line.charAt(line.length - 1) === '\r') {
- line = line.substr(0, line.length - 1);
- }
- this.line = '';
- this.offset = i + 1;
- return line;
- }
- }
- //line split over multiple chunks
- this.line += chunk.toString(HTTPParser.encoding, this.offset, this.end);
- this.offset = this.end;
- };
-
- var headerExp = /^([^: \t]+):[ \t]*((?:.*[^ \t])|)/;
- var headerContinueExp = /^[ \t]+(.*[^ \t])/;
- HTTPParser.prototype.parseHeader = function (line, headers) {
- if (line.indexOf('\r') !== -1) {
- throw parseErrorCode('HPE_LF_EXPECTED');
- }
-
- var match = headerExp.exec(line);
- var k = match && match[1];
- if (k) { // skip empty string (malformed header)
- headers.push(k);
- headers.push(match[2]);
- } else {
- var matchContinue = headerContinueExp.exec(line);
- if (matchContinue && headers.length) {
- if (headers[headers.length - 1]) {
- headers[headers.length - 1] += ' ';
- }
- headers[headers.length - 1] += matchContinue[1];
- }
- }
- };
-
- var requestExp = /^([A-Z-]+) ([^ ]+) HTTP\/(\d)\.(\d)$/;
- HTTPParser.prototype.REQUEST_LINE = function () {
- var line = this.consumeLine();
- if (!line) {
- return;
- }
- var match = requestExp.exec(line);
- if (match === null) {
- throw parseErrorCode('HPE_INVALID_CONSTANT');
- }
- this.info.method = this._compatMode0_11 ? match[1] : methods.indexOf(match[1]);
- if (this.info.method === -1) {
- throw new Error('invalid request method');
- }
- this.info.url = match[2];
- this.info.versionMajor = +match[3];
- this.info.versionMinor = +match[4];
- this.body_bytes = 0;
- this.state = 'HEADER';
- };
-
- var responseExp = /^HTTP\/(\d)\.(\d) (\d{3}) ?(.*)$/;
- HTTPParser.prototype.RESPONSE_LINE = function () {
- var line = this.consumeLine();
- if (!line) {
- return;
- }
- var match = responseExp.exec(line);
- if (match === null) {
- throw parseErrorCode('HPE_INVALID_CONSTANT');
- }
- this.info.versionMajor = +match[1];
- this.info.versionMinor = +match[2];
- var statusCode = this.info.statusCode = +match[3];
- this.info.statusMessage = match[4];
- // Implied zero length.
- if ((statusCode / 100 | 0) === 1 || statusCode === 204 || statusCode === 304) {
- this.body_bytes = 0;
- }
- this.state = 'HEADER';
- };
-
- HTTPParser.prototype.shouldKeepAlive = function () {
- if (this.info.versionMajor > 0 && this.info.versionMinor > 0) {
- if (this.connection.indexOf('close') !== -1) {
- return false;
- }
- } else if (this.connection.indexOf('keep-alive') === -1) {
- return false;
- }
- if (this.body_bytes !== null || this.isChunked) { // || skipBody
- return true;
- }
- return false;
- };
-
- HTTPParser.prototype.HEADER = function () {
- var line = this.consumeLine();
- if (line === undefined) {
- return;
- }
- var info = this.info;
- if (line) {
- this.parseHeader(line, info.headers);
- } else {
- var headers = info.headers;
- var hasContentLength = false;
- var currentContentLengthValue;
- var hasUpgradeHeader = false;
- for (var i = 0; i < headers.length; i += 2) {
- switch (headers[i].toLowerCase()) {
- case 'transfer-encoding':
- this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
- break;
- case 'content-length':
- currentContentLengthValue = +headers[i + 1];
- if (hasContentLength) {
- // Fix duplicate Content-Length header with same values.
- // Throw error only if values are different.
- // Known issues:
- // https://github.com/request/request/issues/2091#issuecomment-328715113
- // https://github.com/nodejs/node/issues/6517#issuecomment-216263771
- if (currentContentLengthValue !== this.body_bytes) {
- throw parseErrorCode('HPE_UNEXPECTED_CONTENT_LENGTH');
- }
- } else {
- hasContentLength = true;
- this.body_bytes = currentContentLengthValue;
- }
- break;
- case 'connection':
- this.connection += headers[i + 1].toLowerCase();
- break;
- case 'upgrade':
- hasUpgradeHeader = true;
- break;
- }
- }
-
- // if both isChunked and hasContentLength, isChunked wins
- // This is required so the body is parsed using the chunked method, and matches
- // Chrome's behavior. We could, maybe, ignore them both (would get chunked
- // encoding into the body), and/or disable shouldKeepAlive to be more
- // resilient.
- if (this.isChunked && hasContentLength) {
- hasContentLength = false;
- this.body_bytes = null;
- }
-
- // Logic from https://github.com/nodejs/http-parser/blob/921d5585515a153fa00e411cf144280c59b41f90/http_parser.c#L1727-L1737
- // "For responses, "Upgrade: foo" and "Connection: upgrade" are
- // mandatory only when it is a 101 Switching Protocols response,
- // otherwise it is purely informational, to announce support.
- if (hasUpgradeHeader && this.connection.indexOf('upgrade') != -1) {
- info.upgrade = this.type === HTTPParser.REQUEST || info.statusCode === 101;
- } else {
- info.upgrade = info.method === method_connect;
- }
-
- if (this.isChunked && info.upgrade) {
- this.isChunked = false;
- }
-
- info.shouldKeepAlive = this.shouldKeepAlive();
- //problem which also exists in original node: we should know skipBody before calling onHeadersComplete
- var skipBody;
- if (compatMode0_12) {
- skipBody = this.userCall()(this[kOnHeadersComplete](info));
- } else {
- skipBody = this.userCall()(this[kOnHeadersComplete](info.versionMajor,
- info.versionMinor, info.headers, info.method, info.url, info.statusCode,
- info.statusMessage, info.upgrade, info.shouldKeepAlive));
- }
- if (skipBody === 2) {
- this.nextRequest();
- return true;
- } else if (this.isChunked && !skipBody) {
- this.state = 'BODY_CHUNKHEAD';
- } else if (skipBody || this.body_bytes === 0) {
- this.nextRequest();
- // For older versions of node (v6.x and older?), that return skipBody=1 or skipBody=true,
- // need this "return true;" if it's an upgrade request.
- return info.upgrade;
- } else if (this.body_bytes === null) {
- this.state = 'BODY_RAW';
- } else {
- this.state = 'BODY_SIZED';
- }
- }
- };
-
- HTTPParser.prototype.BODY_CHUNKHEAD = function () {
- var line = this.consumeLine();
- if (line === undefined) {
- return;
- }
- this.body_bytes = parseInt(line, 16);
- if (!this.body_bytes) {
- this.state = 'BODY_CHUNKTRAILERS';
- } else {
- this.state = 'BODY_CHUNK';
- }
- };
-
- HTTPParser.prototype.BODY_CHUNK = function () {
- var length = Math.min(this.end - this.offset, this.body_bytes);
- this.userCall()(this[kOnBody](this.chunk, this.offset, length));
- this.offset += length;
- this.body_bytes -= length;
- if (!this.body_bytes) {
- this.state = 'BODY_CHUNKEMPTYLINE';
- }
- };
-
- HTTPParser.prototype.BODY_CHUNKEMPTYLINE = function () {
- var line = this.consumeLine();
- if (line === undefined) {
- return;
- }
- assert.equal(line, '');
- this.state = 'BODY_CHUNKHEAD';
- };
-
- HTTPParser.prototype.BODY_CHUNKTRAILERS = function () {
- var line = this.consumeLine();
- if (line === undefined) {
- return;
- }
- if (line) {
- this.parseHeader(line, this.trailers);
- } else {
- if (this.trailers.length) {
- this.userCall()(this[kOnHeaders](this.trailers, ''));
- }
- this.nextRequest();
- }
- };
-
- HTTPParser.prototype.BODY_RAW = function () {
- var length = this.end - this.offset;
- this.userCall()(this[kOnBody](this.chunk, this.offset, length));
- this.offset = this.end;
- };
-
- HTTPParser.prototype.BODY_SIZED = function () {
- var length = Math.min(this.end - this.offset, this.body_bytes);
- this.userCall()(this[kOnBody](this.chunk, this.offset, length));
- this.offset += length;
- this.body_bytes -= length;
- if (!this.body_bytes) {
- this.nextRequest();
- }
- };
-
- // backward compat to node < 0.11.6
- ['Headers', 'HeadersComplete', 'Body', 'MessageComplete'].forEach(function (name) {
- var k = HTTPParser['kOn' + name];
- Object.defineProperty(HTTPParser.prototype, 'on' + name, {
- get: function () {
- return this[k];
- },
- set: function (to) {
- // hack for backward compatibility
- this._compatMode0_11 = true;
- method_connect = 'CONNECT';
- return (this[k] = to);
- }
- });
- });
-
- function parseErrorCode(code) {
- var err = new Error('Parse Error');
- err.code = code;
- return err;
- }
-
- var NodeHTTPParser = httpParser.HTTPParser,
- Buffer$7 = safeBuffer.exports.Buffer;
-
- var TYPES = {
- request: NodeHTTPParser.REQUEST || 'request',
- response: NodeHTTPParser.RESPONSE || 'response'
- };
-
- var HttpParser$3 = function(type) {
- this._type = type;
- this._parser = new NodeHTTPParser(TYPES[type]);
- this._complete = false;
- this.headers = {};
-
- var current = null,
- self = this;
-
- this._parser.onHeaderField = function(b, start, length) {
- current = b.toString('utf8', start, start + length).toLowerCase();
- };
-
- this._parser.onHeaderValue = function(b, start, length) {
- var value = b.toString('utf8', start, start + length);
-
- if (self.headers.hasOwnProperty(current))
- self.headers[current] += ', ' + value;
- else
- self.headers[current] = value;
- };
-
- this._parser.onHeadersComplete = this._parser[NodeHTTPParser.kOnHeadersComplete] =
- function(majorVersion, minorVersion, headers, method, pathname, statusCode) {
- var info = arguments[0];
-
- if (typeof info === 'object') {
- method = info.method;
- pathname = info.url;
- statusCode = info.statusCode;
- headers = info.headers;
- }
-
- self.method = (typeof method === 'number') ? HttpParser$3.METHODS[method] : method;
- self.statusCode = statusCode;
- self.url = pathname;
-
- if (!headers) return;
-
- for (var i = 0, n = headers.length, key, value; i < n; i += 2) {
- key = headers[i].toLowerCase();
- value = headers[i+1];
- if (self.headers.hasOwnProperty(key))
- self.headers[key] += ', ' + value;
- else
- self.headers[key] = value;
- }
-
- self._complete = true;
- };
- };
-
- HttpParser$3.METHODS = {
- 0: 'DELETE',
- 1: 'GET',
- 2: 'HEAD',
- 3: 'POST',
- 4: 'PUT',
- 5: 'CONNECT',
- 6: 'OPTIONS',
- 7: 'TRACE',
- 8: 'COPY',
- 9: 'LOCK',
- 10: 'MKCOL',
- 11: 'MOVE',
- 12: 'PROPFIND',
- 13: 'PROPPATCH',
- 14: 'SEARCH',
- 15: 'UNLOCK',
- 16: 'BIND',
- 17: 'REBIND',
- 18: 'UNBIND',
- 19: 'ACL',
- 20: 'REPORT',
- 21: 'MKACTIVITY',
- 22: 'CHECKOUT',
- 23: 'MERGE',
- 24: 'M-SEARCH',
- 25: 'NOTIFY',
- 26: 'SUBSCRIBE',
- 27: 'UNSUBSCRIBE',
- 28: 'PATCH',
- 29: 'PURGE',
- 30: 'MKCALENDAR',
- 31: 'LINK',
- 32: 'UNLINK'
- };
-
- var VERSION = process.version
- ? process.version.match(/[0-9]+/g).map(function(n) { return parseInt(n, 10) })
- : [];
-
- if (VERSION[0] === 0 && VERSION[1] === 12) {
- HttpParser$3.METHODS[16] = 'REPORT';
- HttpParser$3.METHODS[17] = 'MKACTIVITY';
- HttpParser$3.METHODS[18] = 'CHECKOUT';
- HttpParser$3.METHODS[19] = 'MERGE';
- HttpParser$3.METHODS[20] = 'M-SEARCH';
- HttpParser$3.METHODS[21] = 'NOTIFY';
- HttpParser$3.METHODS[22] = 'SUBSCRIBE';
- HttpParser$3.METHODS[23] = 'UNSUBSCRIBE';
- HttpParser$3.METHODS[24] = 'PATCH';
- HttpParser$3.METHODS[25] = 'PURGE';
- }
-
- HttpParser$3.prototype.isComplete = function() {
- return this._complete;
- };
-
- HttpParser$3.prototype.parse = function(chunk) {
- var consumed = this._parser.execute(chunk, 0, chunk.length);
-
- if (typeof consumed !== 'number') {
- this.error = consumed;
- this._complete = true;
- return;
- }
-
- if (this._complete)
- this.body = (consumed < chunk.length)
- ? chunk.slice(consumed)
- : Buffer$7.alloc(0);
- };
-
- var http_parser = HttpParser$3;
-
- var TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/,
- NOTOKEN = /([^!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z])/g,
- QUOTED = /"((?:\\[\x00-\x7f]|[^\x00-\x08\x0a-\x1f\x7f"\\])*)"/,
- PARAM = new RegExp(TOKEN.source + '(?:=(?:' + TOKEN.source + '|' + QUOTED.source + '))?'),
- EXT = new RegExp(TOKEN.source + '(?: *; *' + PARAM.source + ')*', 'g'),
- EXT_LIST = new RegExp('^' + EXT.source + '(?: *, *' + EXT.source + ')*$'),
- NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/;
-
- var hasOwnProperty = Object.prototype.hasOwnProperty;
-
- var Parser$1 = {
- parseHeader: function(header) {
- var offers = new Offers();
- if (header === '' || header === undefined) return offers;
-
- if (!EXT_LIST.test(header))
- throw new SyntaxError('Invalid Sec-WebSocket-Extensions header: ' + header);
-
- var values = header.match(EXT);
-
- values.forEach(function(value) {
- var params = value.match(new RegExp(PARAM.source, 'g')),
- name = params.shift(),
- offer = {};
-
- params.forEach(function(param) {
- var args = param.match(PARAM), key = args[1], data;
-
- if (args[2] !== undefined) {
- data = args[2];
- } else if (args[3] !== undefined) {
- data = args[3].replace(/\\/g, '');
- } else {
- data = true;
- }
- if (NUMBER.test(data)) data = parseFloat(data);
-
- if (hasOwnProperty.call(offer, key)) {
- offer[key] = [].concat(offer[key]);
- offer[key].push(data);
- } else {
- offer[key] = data;
- }
- }, this);
- offers.push(name, offer);
- }, this);
-
- return offers;
- },
-
- serializeParams: function(name, params) {
- var values = [];
-
- var print = function(key, value) {
- if (value instanceof Array) {
- value.forEach(function(v) { print(key, v); });
- } else if (value === true) {
- values.push(key);
- } else if (typeof value === 'number') {
- values.push(key + '=' + value);
- } else if (NOTOKEN.test(value)) {
- values.push(key + '="' + value.replace(/"/g, '\\"') + '"');
- } else {
- values.push(key + '=' + value);
- }
- };
-
- for (var key in params) print(key, params[key]);
-
- return [name].concat(values).join('; ');
- }
- };
-
- var Offers = function() {
- this._byName = {};
- this._inOrder = [];
- };
-
- Offers.prototype.push = function(name, params) {
- if (!hasOwnProperty.call(this._byName, name))
- this._byName[name] = [];
-
- this._byName[name].push(params);
- this._inOrder.push({ name: name, params: params });
- };
-
- Offers.prototype.eachOffer = function(callback, context) {
- var list = this._inOrder;
- for (var i = 0, n = list.length; i < n; i++)
- callback.call(context, list[i].name, list[i].params);
- };
-
- Offers.prototype.byName = function(name) {
- return this._byName[name] || [];
- };
-
- Offers.prototype.toArray = function() {
- return this._inOrder.slice();
- };
-
- var parser = Parser$1;
-
- var RingBuffer$2 = function(bufferSize) {
- this._bufferSize = bufferSize;
- this.clear();
- };
-
- RingBuffer$2.prototype.clear = function() {
- this._buffer = new Array(this._bufferSize);
- this._ringOffset = 0;
- this._ringSize = this._bufferSize;
- this._head = 0;
- this._tail = 0;
- this.length = 0;
- };
-
- RingBuffer$2.prototype.push = function(value) {
- var expandBuffer = false,
- expandRing = false;
-
- if (this._ringSize < this._bufferSize) {
- expandBuffer = (this._tail === 0);
- } else if (this._ringOffset === this._ringSize) {
- expandBuffer = true;
- expandRing = (this._tail === 0);
- }
-
- if (expandBuffer) {
- this._tail = this._bufferSize;
- this._buffer = this._buffer.concat(new Array(this._bufferSize));
- this._bufferSize = this._buffer.length;
-
- if (expandRing)
- this._ringSize = this._bufferSize;
- }
-
- this._buffer[this._tail] = value;
- this.length += 1;
- if (this._tail < this._ringSize) this._ringOffset += 1;
- this._tail = (this._tail + 1) % this._bufferSize;
- };
-
- RingBuffer$2.prototype.peek = function() {
- if (this.length === 0) return void 0;
- return this._buffer[this._head];
- };
-
- RingBuffer$2.prototype.shift = function() {
- if (this.length === 0) return void 0;
-
- var value = this._buffer[this._head];
- this._buffer[this._head] = void 0;
- this.length -= 1;
- this._ringOffset -= 1;
-
- if (this._ringOffset === 0 && this.length > 0) {
- this._head = this._ringSize;
- this._ringOffset = this.length;
- this._ringSize = this._bufferSize;
- } else {
- this._head = (this._head + 1) % this._ringSize;
- }
- return value;
- };
-
- var ring_buffer = RingBuffer$2;
-
- var RingBuffer$1 = ring_buffer;
-
- var Functor$1 = function(session, method) {
- this._session = session;
- this._method = method;
- this._queue = new RingBuffer$1(Functor$1.QUEUE_SIZE);
- this._stopped = false;
- this.pending = 0;
- };
-
- Functor$1.QUEUE_SIZE = 8;
-
- Functor$1.prototype.call = function(error, message, callback, context) {
- if (this._stopped) return;
-
- var record = { error: error, message: message, callback: callback, context: context, done: false },
- called = false,
- self = this;
-
- this._queue.push(record);
-
- if (record.error) {
- record.done = true;
- this._stop();
- return this._flushQueue();
- }
-
- var handler = function(err, msg) {
- if (!(called ^ (called = true))) return;
-
- if (err) {
- self._stop();
- record.error = err;
- record.message = null;
- } else {
- record.message = msg;
- }
-
- record.done = true;
- self._flushQueue();
- };
-
- try {
- this._session[this._method](message, handler);
- } catch (err) {
- handler(err);
- }
- };
-
- Functor$1.prototype._stop = function() {
- this.pending = this._queue.length;
- this._stopped = true;
- };
-
- Functor$1.prototype._flushQueue = function() {
- var queue = this._queue, record;
-
- while (queue.length > 0 && queue.peek().done) {
- record = queue.shift();
- if (record.error) {
- this.pending = 0;
- queue.clear();
- } else {
- this.pending -= 1;
- }
- record.callback.call(record.context, record.error, record.message);
- }
- };
-
- var functor = Functor$1;
-
- var RingBuffer = ring_buffer;
-
- var Pledge$2 = function() {
- this._complete = false;
- this._callbacks = new RingBuffer(Pledge$2.QUEUE_SIZE);
- };
-
- Pledge$2.QUEUE_SIZE = 4;
-
- Pledge$2.all = function(list) {
- var pledge = new Pledge$2(),
- pending = list.length,
- n = pending;
-
- if (pending === 0) pledge.done();
-
- while (n--) list[n].then(function() {
- pending -= 1;
- if (pending === 0) pledge.done();
- });
- return pledge;
- };
-
- Pledge$2.prototype.then = function(callback) {
- if (this._complete) callback();
- else this._callbacks.push(callback);
- };
-
- Pledge$2.prototype.done = function() {
- this._complete = true;
- var callbacks = this._callbacks, callback;
- while (callback = callbacks.shift()) callback();
- };
-
- var pledge = Pledge$2;
-
- var Functor = functor,
- Pledge$1 = pledge;
-
- var Cell$1 = function(tuple) {
- this._ext = tuple[0];
- this._session = tuple[1];
-
- this._functors = {
- incoming: new Functor(this._session, 'processIncomingMessage'),
- outgoing: new Functor(this._session, 'processOutgoingMessage')
- };
- };
-
- Cell$1.prototype.pending = function(direction) {
- var functor = this._functors[direction];
- if (!functor._stopped) functor.pending += 1;
- };
-
- Cell$1.prototype.incoming = function(error, message, callback, context) {
- this._exec('incoming', error, message, callback, context);
- };
-
- Cell$1.prototype.outgoing = function(error, message, callback, context) {
- this._exec('outgoing', error, message, callback, context);
- };
-
- Cell$1.prototype.close = function() {
- this._closed = this._closed || new Pledge$1();
- this._doClose();
- return this._closed;
- };
-
- Cell$1.prototype._exec = function(direction, error, message, callback, context) {
- this._functors[direction].call(error, message, function(err, msg) {
- if (err) err.message = this._ext.name + ': ' + err.message;
- callback.call(context, err, msg);
- this._doClose();
- }, this);
- };
-
- Cell$1.prototype._doClose = function() {
- var fin = this._functors.incoming,
- fout = this._functors.outgoing;
-
- if (!this._closed || fin.pending + fout.pending !== 0) return;
- if (this._session) this._session.close();
- this._session = null;
- this._closed.done();
- };
-
- var cell = Cell$1;
-
- var Cell = cell,
- Pledge = pledge;
-
- var Pipeline$1 = function(sessions) {
- this._cells = sessions.map(function(session) { return new Cell(session) });
- this._stopped = { incoming: false, outgoing: false };
- };
-
- Pipeline$1.prototype.processIncomingMessage = function(message, callback, context) {
- if (this._stopped.incoming) return;
- this._loop('incoming', this._cells.length - 1, -1, -1, message, callback, context);
- };
-
- Pipeline$1.prototype.processOutgoingMessage = function(message, callback, context) {
- if (this._stopped.outgoing) return;
- this._loop('outgoing', 0, this._cells.length, 1, message, callback, context);
- };
-
- Pipeline$1.prototype.close = function(callback, context) {
- this._stopped = { incoming: true, outgoing: true };
-
- var closed = this._cells.map(function(a) { return a.close() });
- if (callback)
- Pledge.all(closed).then(function() { callback.call(context); });
- };
-
- Pipeline$1.prototype._loop = function(direction, start, end, step, message, callback, context) {
- var cells = this._cells,
- n = cells.length,
- self = this;
-
- while (n--) cells[n].pending(direction);
-
- var pipe = function(index, error, msg) {
- if (index === end) return callback.call(context, error, msg);
-
- cells[index][direction](error, msg, function(err, m) {
- if (err) self._stopped[direction] = true;
- pipe(index + step, err, m);
- });
- };
- pipe(start, null, message);
- };
-
- var pipeline = Pipeline$1;
-
- var Parser = parser,
- Pipeline = pipeline;
-
- var Extensions$1 = function() {
- this._rsv1 = this._rsv2 = this._rsv3 = null;
-
- this._byName = {};
- this._inOrder = [];
- this._sessions = [];
- this._index = {};
- };
-
- Extensions$1.MESSAGE_OPCODES = [1, 2];
-
- var instance$a = {
- add: function(ext) {
- if (typeof ext.name !== 'string') throw new TypeError('extension.name must be a string');
- if (ext.type !== 'permessage') throw new TypeError('extension.type must be "permessage"');
-
- if (typeof ext.rsv1 !== 'boolean') throw new TypeError('extension.rsv1 must be true or false');
- if (typeof ext.rsv2 !== 'boolean') throw new TypeError('extension.rsv2 must be true or false');
- if (typeof ext.rsv3 !== 'boolean') throw new TypeError('extension.rsv3 must be true or false');
-
- if (this._byName.hasOwnProperty(ext.name))
- throw new TypeError('An extension with name "' + ext.name + '" is already registered');
-
- this._byName[ext.name] = ext;
- this._inOrder.push(ext);
- },
-
- generateOffer: function() {
- var sessions = [],
- offer = [],
- index = {};
-
- this._inOrder.forEach(function(ext) {
- var session = ext.createClientSession();
- if (!session) return;
-
- var record = [ext, session];
- sessions.push(record);
- index[ext.name] = record;
-
- var offers = session.generateOffer();
- offers = offers ? [].concat(offers) : [];
-
- offers.forEach(function(off) {
- offer.push(Parser.serializeParams(ext.name, off));
- }, this);
- }, this);
-
- this._sessions = sessions;
- this._index = index;
-
- return offer.length > 0 ? offer.join(', ') : null;
- },
-
- activate: function(header) {
- var responses = Parser.parseHeader(header),
- sessions = [];
-
- responses.eachOffer(function(name, params) {
- var record = this._index[name];
-
- if (!record)
- throw new Error('Server sent an extension response for unknown extension "' + name + '"');
-
- var ext = record[0],
- session = record[1],
- reserved = this._reserved(ext);
-
- if (reserved)
- throw new Error('Server sent two extension responses that use the RSV' +
- reserved[0] + ' bit: "' +
- reserved[1] + '" and "' + ext.name + '"');
-
- if (session.activate(params) !== true)
- throw new Error('Server sent unacceptable extension parameters: ' +
- Parser.serializeParams(name, params));
-
- this._reserve(ext);
- sessions.push(record);
- }, this);
-
- this._sessions = sessions;
- this._pipeline = new Pipeline(sessions);
- },
-
- generateResponse: function(header) {
- var sessions = [],
- response = [],
- offers = Parser.parseHeader(header);
-
- this._inOrder.forEach(function(ext) {
- var offer = offers.byName(ext.name);
- if (offer.length === 0 || this._reserved(ext)) return;
-
- var session = ext.createServerSession(offer);
- if (!session) return;
-
- this._reserve(ext);
- sessions.push([ext, session]);
- response.push(Parser.serializeParams(ext.name, session.generateResponse()));
- }, this);
-
- this._sessions = sessions;
- this._pipeline = new Pipeline(sessions);
-
- return response.length > 0 ? response.join(', ') : null;
- },
-
- validFrameRsv: function(frame) {
- var allowed = { rsv1: false, rsv2: false, rsv3: false },
- ext;
-
- if (Extensions$1.MESSAGE_OPCODES.indexOf(frame.opcode) >= 0) {
- for (var i = 0, n = this._sessions.length; i < n; i++) {
- ext = this._sessions[i][0];
- allowed.rsv1 = allowed.rsv1 || ext.rsv1;
- allowed.rsv2 = allowed.rsv2 || ext.rsv2;
- allowed.rsv3 = allowed.rsv3 || ext.rsv3;
- }
- }
-
- return (allowed.rsv1 || !frame.rsv1) &&
- (allowed.rsv2 || !frame.rsv2) &&
- (allowed.rsv3 || !frame.rsv3);
- },
-
- processIncomingMessage: function(message, callback, context) {
- this._pipeline.processIncomingMessage(message, callback, context);
- },
-
- processOutgoingMessage: function(message, callback, context) {
- this._pipeline.processOutgoingMessage(message, callback, context);
- },
-
- close: function(callback, context) {
- if (!this._pipeline) return callback.call(context);
- this._pipeline.close(callback, context);
- },
-
- _reserve: function(ext) {
- this._rsv1 = this._rsv1 || (ext.rsv1 && ext.name);
- this._rsv2 = this._rsv2 || (ext.rsv2 && ext.name);
- this._rsv3 = this._rsv3 || (ext.rsv3 && ext.name);
- },
-
- _reserved: function(ext) {
- if (this._rsv1 && ext.rsv1) return [1, this._rsv1];
- if (this._rsv2 && ext.rsv2) return [2, this._rsv2];
- if (this._rsv3 && ext.rsv3) return [3, this._rsv3];
- return false;
- }
- };
-
- for (var key$a in instance$a)
- Extensions$1.prototype[key$a] = instance$a[key$a];
-
- var websocket_extensions = Extensions$1;
-
- var Frame$1 = function() {};
-
- var instance$9 = {
- final: false,
- rsv1: false,
- rsv2: false,
- rsv3: false,
- opcode: null,
- masked: false,
- maskingKey: null,
- lengthBytes: 1,
- length: 0,
- payload: null
- };
-
- for (var key$9 in instance$9)
- Frame$1.prototype[key$9] = instance$9[key$9];
-
- var frame = Frame$1;
-
- var Buffer$6 = safeBuffer.exports.Buffer;
-
- var Message$1 = function() {
- this.rsv1 = false;
- this.rsv2 = false;
- this.rsv3 = false;
- this.opcode = null;
- this.length = 0;
- this._chunks = [];
- };
-
- var instance$8 = {
- read: function() {
- return this.data = this.data || Buffer$6.concat(this._chunks, this.length);
- },
-
- pushFrame: function(frame) {
- this.rsv1 = this.rsv1 || frame.rsv1;
- this.rsv2 = this.rsv2 || frame.rsv2;
- this.rsv3 = this.rsv3 || frame.rsv3;
-
- if (this.opcode === null) this.opcode = frame.opcode;
-
- this._chunks.push(frame.payload);
- this.length += frame.length;
- }
- };
-
- for (var key$8 in instance$8)
- Message$1.prototype[key$8] = instance$8[key$8];
-
- var message = Message$1;
-
- var Buffer$5 = safeBuffer.exports.Buffer,
- crypto$2 = require$$1__default$1["default"],
- util$a = require$$2__default["default"],
- Extensions = websocket_extensions,
- Base$6 = base,
- Frame = frame,
- Message = message;
-
- var Hybi$2 = function(request, url, options) {
- Base$6.apply(this, arguments);
-
- this._extensions = new Extensions();
- this._stage = 0;
- this._masking = this._options.masking;
- this._protocols = this._options.protocols || [];
- this._requireMasking = this._options.requireMasking;
- this._pingCallbacks = {};
-
- if (typeof this._protocols === 'string')
- this._protocols = this._protocols.split(/ *, */);
-
- if (!this._request) return;
-
- var protos = this._request.headers['sec-websocket-protocol'],
- supported = this._protocols;
-
- if (protos !== undefined) {
- if (typeof protos === 'string') protos = protos.split(/ *, */);
- this.protocol = protos.filter(function(p) { return supported.indexOf(p) >= 0 })[0];
- }
-
- this.version = 'hybi-' + Hybi$2.VERSION;
- };
- util$a.inherits(Hybi$2, Base$6);
-
- Hybi$2.VERSION = '13';
-
- Hybi$2.mask = function(payload, mask, offset) {
- if (!mask || mask.length === 0) return payload;
- offset = offset || 0;
-
- for (var i = 0, n = payload.length - offset; i < n; i++) {
- payload[offset + i] = payload[offset + i] ^ mask[i % 4];
- }
- return payload;
- };
-
- Hybi$2.generateAccept = function(key) {
- var sha1 = crypto$2.createHash('sha1');
- sha1.update(key + Hybi$2.GUID);
- return sha1.digest('base64');
- };
-
- Hybi$2.GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
-
- var instance$7 = {
- FIN: 0x80,
- MASK: 0x80,
- RSV1: 0x40,
- RSV2: 0x20,
- RSV3: 0x10,
- OPCODE: 0x0F,
- LENGTH: 0x7F,
-
- OPCODES: {
- continuation: 0,
- text: 1,
- binary: 2,
- close: 8,
- ping: 9,
- pong: 10
- },
-
- OPCODE_CODES: [0, 1, 2, 8, 9, 10],
- MESSAGE_OPCODES: [0, 1, 2],
- OPENING_OPCODES: [1, 2],
-
- ERRORS: {
- normal_closure: 1000,
- going_away: 1001,
- protocol_error: 1002,
- unacceptable: 1003,
- encoding_error: 1007,
- policy_violation: 1008,
- too_large: 1009,
- extension_error: 1010,
- unexpected_condition: 1011
- },
-
- ERROR_CODES: [1000, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1011],
- DEFAULT_ERROR_CODE: 1000,
- MIN_RESERVED_ERROR: 3000,
- MAX_RESERVED_ERROR: 4999,
-
- // http://www.w3.org/International/questions/qa-forms-utf-8.en.php
- 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})*$/,
-
- addExtension: function(extension) {
- this._extensions.add(extension);
- return true;
- },
-
- parse: function(chunk) {
- this._reader.put(chunk);
- var buffer = true;
- while (buffer) {
- switch (this._stage) {
- case 0:
- buffer = this._reader.read(1);
- if (buffer) this._parseOpcode(buffer[0]);
- break;
-
- case 1:
- buffer = this._reader.read(1);
- if (buffer) this._parseLength(buffer[0]);
- break;
-
- case 2:
- buffer = this._reader.read(this._frame.lengthBytes);
- if (buffer) this._parseExtendedLength(buffer);
- break;
-
- case 3:
- buffer = this._reader.read(4);
- if (buffer) {
- this._stage = 4;
- this._frame.maskingKey = buffer;
- }
- break;
-
- case 4:
- buffer = this._reader.read(this._frame.length);
- if (buffer) {
- this._stage = 0;
- this._emitFrame(buffer);
- }
- break;
-
- default:
- buffer = null;
- }
- }
- },
-
- text: function(message) {
- if (this.readyState > 1) return false;
- return this.frame(message, 'text');
- },
-
- binary: function(message) {
- if (this.readyState > 1) return false;
- return this.frame(message, 'binary');
- },
-
- ping: function(message, callback) {
- if (this.readyState > 1) return false;
- message = message || '';
- if (callback) this._pingCallbacks[message] = callback;
- return this.frame(message, 'ping');
- },
-
- pong: function(message) {
- if (this.readyState > 1) return false;
- message = message ||'';
- return this.frame(message, 'pong');
- },
-
- close: function(reason, code) {
- reason = reason || '';
- code = code || this.ERRORS.normal_closure;
-
- if (this.readyState <= 0) {
- this.readyState = 3;
- this.emit('close', new Base$6.CloseEvent(code, reason));
- return true;
- } else if (this.readyState === 1) {
- this.readyState = 2;
- this._extensions.close(function() { this.frame(reason, 'close', code); }, this);
- return true;
- } else {
- return false;
- }
- },
-
- frame: function(buffer, type, code) {
- if (this.readyState <= 0) return this._queue([buffer, type, code]);
- if (this.readyState > 2) return false;
-
- if (buffer instanceof Array) buffer = Buffer$5.from(buffer);
- if (typeof buffer === 'number') buffer = buffer.toString();
-
- var message = new Message(),
- isText = (typeof buffer === 'string'),
- payload, copy;
-
- message.rsv1 = message.rsv2 = message.rsv3 = false;
- message.opcode = this.OPCODES[type || (isText ? 'text' : 'binary')];
-
- payload = isText ? Buffer$5.from(buffer, 'utf8') : buffer;
-
- if (code) {
- copy = payload;
- payload = Buffer$5.allocUnsafe(2 + copy.length);
- payload.writeUInt16BE(code, 0);
- copy.copy(payload, 2);
- }
- message.data = payload;
-
- var onMessageReady = function(message) {
- var frame = new Frame();
-
- frame.final = true;
- frame.rsv1 = message.rsv1;
- frame.rsv2 = message.rsv2;
- frame.rsv3 = message.rsv3;
- frame.opcode = message.opcode;
- frame.masked = !!this._masking;
- frame.length = message.data.length;
- frame.payload = message.data;
-
- if (frame.masked) frame.maskingKey = crypto$2.randomBytes(4);
-
- this._sendFrame(frame);
- };
-
- if (this.MESSAGE_OPCODES.indexOf(message.opcode) >= 0)
- this._extensions.processOutgoingMessage(message, function(error, message) {
- if (error) return this._fail('extension_error', error.message);
- onMessageReady.call(this, message);
- }, this);
- else
- onMessageReady.call(this, message);
-
- return true;
- },
-
- _sendFrame: function(frame) {
- var length = frame.length,
- header = (length <= 125) ? 2 : (length <= 65535 ? 4 : 10),
- offset = header + (frame.masked ? 4 : 0),
- buffer = Buffer$5.allocUnsafe(offset + length),
- masked = frame.masked ? this.MASK : 0;
-
- buffer[0] = (frame.final ? this.FIN : 0) |
- (frame.rsv1 ? this.RSV1 : 0) |
- (frame.rsv2 ? this.RSV2 : 0) |
- (frame.rsv3 ? this.RSV3 : 0) |
- frame.opcode;
-
- if (length <= 125) {
- buffer[1] = masked | length;
- } else if (length <= 65535) {
- buffer[1] = masked | 126;
- buffer.writeUInt16BE(length, 2);
- } else {
- buffer[1] = masked | 127;
- buffer.writeUInt32BE(Math.floor(length / 0x100000000), 2);
- buffer.writeUInt32BE(length % 0x100000000, 6);
- }
-
- frame.payload.copy(buffer, offset);
-
- if (frame.masked) {
- frame.maskingKey.copy(buffer, header);
- Hybi$2.mask(buffer, frame.maskingKey, offset);
- }
-
- this._write(buffer);
- },
-
- _handshakeResponse: function() {
- var secKey = this._request.headers['sec-websocket-key'],
- version = this._request.headers['sec-websocket-version'];
-
- if (version !== Hybi$2.VERSION)
- throw new Error('Unsupported WebSocket version: ' + version);
-
- if (typeof secKey !== 'string')
- throw new Error('Missing handshake request header: Sec-WebSocket-Key');
-
- this._headers.set('Upgrade', 'websocket');
- this._headers.set('Connection', 'Upgrade');
- this._headers.set('Sec-WebSocket-Accept', Hybi$2.generateAccept(secKey));
-
- if (this.protocol) this._headers.set('Sec-WebSocket-Protocol', this.protocol);
-
- var extensions = this._extensions.generateResponse(this._request.headers['sec-websocket-extensions']);
- if (extensions) this._headers.set('Sec-WebSocket-Extensions', extensions);
-
- var start = 'HTTP/1.1 101 Switching Protocols',
- headers = [start, this._headers.toString(), ''];
-
- return Buffer$5.from(headers.join('\r\n'), 'utf8');
- },
-
- _shutdown: function(code, reason, error) {
- delete this._frame;
- delete this._message;
- this._stage = 5;
-
- var sendCloseFrame = (this.readyState === 1);
- this.readyState = 2;
-
- this._extensions.close(function() {
- if (sendCloseFrame) this.frame(reason, 'close', code);
- this.readyState = 3;
- if (error) this.emit('error', new Error(reason));
- this.emit('close', new Base$6.CloseEvent(code, reason));
- }, this);
- },
-
- _fail: function(type, message) {
- if (this.readyState > 1) return;
- this._shutdown(this.ERRORS[type], message, true);
- },
-
- _parseOpcode: function(octet) {
- var rsvs = [this.RSV1, this.RSV2, this.RSV3].map(function(rsv) {
- return (octet & rsv) === rsv;
- });
-
- var frame = this._frame = new Frame();
-
- frame.final = (octet & this.FIN) === this.FIN;
- frame.rsv1 = rsvs[0];
- frame.rsv2 = rsvs[1];
- frame.rsv3 = rsvs[2];
- frame.opcode = (octet & this.OPCODE);
-
- this._stage = 1;
-
- if (!this._extensions.validFrameRsv(frame))
- return this._fail('protocol_error',
- 'One or more reserved bits are on: reserved1 = ' + (frame.rsv1 ? 1 : 0) +
- ', reserved2 = ' + (frame.rsv2 ? 1 : 0) +
- ', reserved3 = ' + (frame.rsv3 ? 1 : 0));
-
- if (this.OPCODE_CODES.indexOf(frame.opcode) < 0)
- return this._fail('protocol_error', 'Unrecognized frame opcode: ' + frame.opcode);
-
- if (this.MESSAGE_OPCODES.indexOf(frame.opcode) < 0 && !frame.final)
- return this._fail('protocol_error', 'Received fragmented control frame: opcode = ' + frame.opcode);
-
- if (this._message && this.OPENING_OPCODES.indexOf(frame.opcode) >= 0)
- return this._fail('protocol_error', 'Received new data frame but previous continuous frame is unfinished');
- },
-
- _parseLength: function(octet) {
- var frame = this._frame;
- frame.masked = (octet & this.MASK) === this.MASK;
- frame.length = (octet & this.LENGTH);
-
- if (frame.length >= 0 && frame.length <= 125) {
- this._stage = frame.masked ? 3 : 4;
- if (!this._checkFrameLength()) return;
- } else {
- this._stage = 2;
- frame.lengthBytes = (frame.length === 126 ? 2 : 8);
- }
-
- if (this._requireMasking && !frame.masked)
- return this._fail('unacceptable', 'Received unmasked frame but masking is required');
- },
-
- _parseExtendedLength: function(buffer) {
- var frame = this._frame;
- frame.length = this._readUInt(buffer);
-
- this._stage = frame.masked ? 3 : 4;
-
- if (this.MESSAGE_OPCODES.indexOf(frame.opcode) < 0 && frame.length > 125)
- return this._fail('protocol_error', 'Received control frame having too long payload: ' + frame.length);
-
- if (!this._checkFrameLength()) return;
- },
-
- _checkFrameLength: function() {
- var length = this._message ? this._message.length : 0;
-
- if (length + this._frame.length > this._maxLength) {
- this._fail('too_large', 'WebSocket frame length too large');
- return false;
- } else {
- return true;
- }
- },
-
- _emitFrame: function(buffer) {
- var frame = this._frame,
- payload = frame.payload = Hybi$2.mask(buffer, frame.maskingKey),
- opcode = frame.opcode,
- message,
- code, reason,
- callbacks, callback;
-
- delete this._frame;
-
- if (opcode === this.OPCODES.continuation) {
- if (!this._message) return this._fail('protocol_error', 'Received unexpected continuation frame');
- this._message.pushFrame(frame);
- }
-
- if (opcode === this.OPCODES.text || opcode === this.OPCODES.binary) {
- this._message = new Message();
- this._message.pushFrame(frame);
- }
-
- if (frame.final && this.MESSAGE_OPCODES.indexOf(opcode) >= 0)
- return this._emitMessage(this._message);
-
- if (opcode === this.OPCODES.close) {
- code = (payload.length >= 2) ? payload.readUInt16BE(0) : null;
- reason = (payload.length > 2) ? this._encode(payload.slice(2)) : null;
-
- if (!(payload.length === 0) &&
- !(code !== null && code >= this.MIN_RESERVED_ERROR && code <= this.MAX_RESERVED_ERROR) &&
- this.ERROR_CODES.indexOf(code) < 0)
- code = this.ERRORS.protocol_error;
-
- if (payload.length > 125 || (payload.length > 2 && !reason))
- code = this.ERRORS.protocol_error;
-
- this._shutdown(code || this.DEFAULT_ERROR_CODE, reason || '');
- }
-
- if (opcode === this.OPCODES.ping) {
- this.frame(payload, 'pong');
- this.emit('ping', new Base$6.PingEvent(payload.toString()));
- }
-
- if (opcode === this.OPCODES.pong) {
- callbacks = this._pingCallbacks;
- message = this._encode(payload);
- callback = callbacks[message];
-
- delete callbacks[message];
- if (callback) callback();
-
- this.emit('pong', new Base$6.PongEvent(payload.toString()));
- }
- },
-
- _emitMessage: function(message) {
- var message = this._message;
- message.read();
-
- delete this._message;
-
- this._extensions.processIncomingMessage(message, function(error, message) {
- if (error) return this._fail('extension_error', error.message);
-
- var payload = message.data;
- if (message.opcode === this.OPCODES.text) payload = this._encode(payload);
-
- if (payload === null)
- return this._fail('encoding_error', 'Could not decode a text frame as UTF-8');
- else
- this.emit('message', new Base$6.MessageEvent(payload));
- }, this);
- },
-
- _encode: function(buffer) {
- try {
- var string = buffer.toString('binary', 0, buffer.length);
- if (!this.UTF8_MATCH.test(string)) return null;
- } catch (e) {}
- return buffer.toString('utf8', 0, buffer.length);
- },
-
- _readUInt: function(buffer) {
- if (buffer.length === 2) return buffer.readUInt16BE(0);
-
- return buffer.readUInt32BE(0) * 0x100000000 +
- buffer.readUInt32BE(4);
- }
- };
-
- for (var key$7 in instance$7)
- Hybi$2.prototype[key$7] = instance$7[key$7];
-
- var hybi = Hybi$2;
-
- var Buffer$4 = safeBuffer.exports.Buffer,
- Stream$2 = require$$0__default$1["default"].Stream,
- url$2 = require$$2__default$1["default"],
- util$9 = require$$2__default["default"],
- Base$5 = base,
- Headers$1 = headers,
- HttpParser$2 = http_parser;
-
- var PORTS = { 'ws:': 80, 'wss:': 443 };
-
- var Proxy$1 = function(client, origin, options) {
- this._client = client;
- this._http = new HttpParser$2('response');
- this._origin = (typeof client.url === 'object') ? client.url : url$2.parse(client.url);
- this._url = (typeof origin === 'object') ? origin : url$2.parse(origin);
- this._options = options || {};
- this._state = 0;
-
- this.readable = this.writable = true;
- this._paused = false;
-
- this._headers = new Headers$1();
- this._headers.set('Host', this._origin.host);
- this._headers.set('Connection', 'keep-alive');
- this._headers.set('Proxy-Connection', 'keep-alive');
-
- var auth = this._url.auth && Buffer$4.from(this._url.auth, 'utf8').toString('base64');
- if (auth) this._headers.set('Proxy-Authorization', 'Basic ' + auth);
- };
- util$9.inherits(Proxy$1, Stream$2);
-
- var instance$6 = {
- setHeader: function(name, value) {
- if (this._state !== 0) return false;
- this._headers.set(name, value);
- return true;
- },
-
- start: function() {
- if (this._state !== 0) return false;
- this._state = 1;
-
- var origin = this._origin,
- port = origin.port || PORTS[origin.protocol],
- start = 'CONNECT ' + origin.hostname + ':' + port + ' HTTP/1.1';
-
- var headers = [start, this._headers.toString(), ''];
-
- this.emit('data', Buffer$4.from(headers.join('\r\n'), 'utf8'));
- return true;
- },
-
- pause: function() {
- this._paused = true;
- },
-
- resume: function() {
- this._paused = false;
- this.emit('drain');
- },
-
- write: function(chunk) {
- if (!this.writable) return false;
-
- this._http.parse(chunk);
- if (!this._http.isComplete()) return !this._paused;
-
- this.statusCode = this._http.statusCode;
- this.headers = this._http.headers;
-
- if (this.statusCode === 200) {
- this.emit('connect', new Base$5.ConnectEvent());
- } else {
- var message = "Can't establish a connection to the server at " + this._origin.href;
- this.emit('error', new Error(message));
- }
- this.end();
- return !this._paused;
- },
-
- end: function(chunk) {
- if (!this.writable) return;
- if (chunk !== undefined) this.write(chunk);
- this.readable = this.writable = false;
- this.emit('close');
- this.emit('end');
- },
-
- destroy: function() {
- this.end();
- }
- };
-
- for (var key$6 in instance$6)
- Proxy$1.prototype[key$6] = instance$6[key$6];
-
- var proxy = Proxy$1;
-
- var Buffer$3 = safeBuffer.exports.Buffer,
- crypto$1 = require$$1__default$1["default"],
- url$1 = require$$2__default$1["default"],
- util$8 = require$$2__default["default"],
- HttpParser$1 = http_parser,
- Base$4 = base,
- Hybi$1 = hybi,
- Proxy = proxy;
-
- var Client$2 = function(_url, options) {
- this.version = 'hybi-' + Hybi$1.VERSION;
- Hybi$1.call(this, null, _url, options);
-
- this.readyState = -1;
- this._key = Client$2.generateKey();
- this._accept = Hybi$1.generateAccept(this._key);
- this._http = new HttpParser$1('response');
-
- var uri = url$1.parse(this.url),
- auth = uri.auth && Buffer$3.from(uri.auth, 'utf8').toString('base64');
-
- if (this.VALID_PROTOCOLS.indexOf(uri.protocol) < 0)
- throw new Error(this.url + ' is not a valid WebSocket URL');
-
- this._pathname = (uri.pathname || '/') + (uri.search || '');
-
- this._headers.set('Host', uri.host);
- this._headers.set('Upgrade', 'websocket');
- this._headers.set('Connection', 'Upgrade');
- this._headers.set('Sec-WebSocket-Key', this._key);
- this._headers.set('Sec-WebSocket-Version', Hybi$1.VERSION);
-
- if (this._protocols.length > 0)
- this._headers.set('Sec-WebSocket-Protocol', this._protocols.join(', '));
-
- if (auth)
- this._headers.set('Authorization', 'Basic ' + auth);
- };
- util$8.inherits(Client$2, Hybi$1);
-
- Client$2.generateKey = function() {
- return crypto$1.randomBytes(16).toString('base64');
- };
-
- var instance$5 = {
- VALID_PROTOCOLS: ['ws:', 'wss:'],
-
- proxy: function(origin, options) {
- return new Proxy(this, origin, options);
- },
-
- start: function() {
- if (this.readyState !== -1) return false;
- this._write(this._handshakeRequest());
- this.readyState = 0;
- return true;
- },
-
- parse: function(chunk) {
- if (this.readyState === 3) return;
- if (this.readyState > 0) return Hybi$1.prototype.parse.call(this, chunk);
-
- this._http.parse(chunk);
- if (!this._http.isComplete()) return;
-
- this._validateHandshake();
- if (this.readyState === 3) return;
-
- this._open();
- this.parse(this._http.body);
- },
-
- _handshakeRequest: function() {
- var extensions = this._extensions.generateOffer();
- if (extensions)
- this._headers.set('Sec-WebSocket-Extensions', extensions);
-
- var start = 'GET ' + this._pathname + ' HTTP/1.1',
- headers = [start, this._headers.toString(), ''];
-
- return Buffer$3.from(headers.join('\r\n'), 'utf8');
- },
-
- _failHandshake: function(message) {
- message = 'Error during WebSocket handshake: ' + message;
- this.readyState = 3;
- this.emit('error', new Error(message));
- this.emit('close', new Base$4.CloseEvent(this.ERRORS.protocol_error, message));
- },
-
- _validateHandshake: function() {
- this.statusCode = this._http.statusCode;
- this.headers = this._http.headers;
-
- if (this._http.error)
- return this._failHandshake(this._http.error.message);
-
- if (this._http.statusCode !== 101)
- return this._failHandshake('Unexpected response code: ' + this._http.statusCode);
-
- var headers = this._http.headers,
- upgrade = headers['upgrade'] || '',
- connection = headers['connection'] || '',
- accept = headers['sec-websocket-accept'] || '',
- protocol = headers['sec-websocket-protocol'] || '';
-
- if (upgrade === '')
- return this._failHandshake("'Upgrade' header is missing");
- if (upgrade.toLowerCase() !== 'websocket')
- return this._failHandshake("'Upgrade' header value is not 'WebSocket'");
-
- if (connection === '')
- return this._failHandshake("'Connection' header is missing");
- if (connection.toLowerCase() !== 'upgrade')
- return this._failHandshake("'Connection' header value is not 'Upgrade'");
-
- if (accept !== this._accept)
- return this._failHandshake('Sec-WebSocket-Accept mismatch');
-
- this.protocol = null;
-
- if (protocol !== '') {
- if (this._protocols.indexOf(protocol) < 0)
- return this._failHandshake('Sec-WebSocket-Protocol mismatch');
- else
- this.protocol = protocol;
- }
-
- try {
- this._extensions.activate(this.headers['sec-websocket-extensions']);
- } catch (e) {
- return this._failHandshake(e.message);
- }
- }
- };
-
- for (var key$5 in instance$5)
- Client$2.prototype[key$5] = instance$5[key$5];
-
- var client$1 = Client$2;
-
- var Buffer$2 = safeBuffer.exports.Buffer,
- Base$3 = base,
- util$7 = require$$2__default["default"];
-
- var Draft75$2 = function(request, url, options) {
- Base$3.apply(this, arguments);
- this._stage = 0;
- this.version = 'hixie-75';
-
- this._headers.set('Upgrade', 'WebSocket');
- this._headers.set('Connection', 'Upgrade');
- this._headers.set('WebSocket-Origin', this._request.headers.origin);
- this._headers.set('WebSocket-Location', this.url);
- };
- util$7.inherits(Draft75$2, Base$3);
-
- var instance$4 = {
- close: function() {
- if (this.readyState === 3) return false;
- this.readyState = 3;
- this.emit('close', new Base$3.CloseEvent(null, null));
- return true;
- },
-
- parse: function(chunk) {
- if (this.readyState > 1) return;
-
- this._reader.put(chunk);
-
- this._reader.eachByte(function(octet) {
- var message;
-
- switch (this._stage) {
- case -1:
- this._body.push(octet);
- this._sendHandshakeBody();
- break;
-
- case 0:
- this._parseLeadingByte(octet);
- break;
-
- case 1:
- this._length = (octet & 0x7F) + 128 * this._length;
-
- if (this._closing && this._length === 0) {
- return this.close();
- }
- else if ((octet & 0x80) !== 0x80) {
- if (this._length === 0) {
- this._stage = 0;
- }
- else {
- this._skipped = 0;
- this._stage = 2;
- }
- }
- break;
-
- case 2:
- if (octet === 0xFF) {
- this._stage = 0;
- message = Buffer$2.from(this._buffer).toString('utf8', 0, this._buffer.length);
- this.emit('message', new Base$3.MessageEvent(message));
- }
- else {
- if (this._length) {
- this._skipped += 1;
- if (this._skipped === this._length)
- this._stage = 0;
- } else {
- this._buffer.push(octet);
- if (this._buffer.length > this._maxLength) return this.close();
- }
- }
- break;
- }
- }, this);
- },
-
- frame: function(buffer) {
- if (this.readyState === 0) return this._queue([buffer]);
- if (this.readyState > 1) return false;
-
- if (typeof buffer !== 'string') buffer = buffer.toString();
-
- var length = Buffer$2.byteLength(buffer),
- frame = Buffer$2.allocUnsafe(length + 2);
-
- frame[0] = 0x00;
- frame.write(buffer, 1);
- frame[frame.length - 1] = 0xFF;
-
- this._write(frame);
- return true;
- },
-
- _handshakeResponse: function() {
- var start = 'HTTP/1.1 101 Web Socket Protocol Handshake',
- headers = [start, this._headers.toString(), ''];
-
- return Buffer$2.from(headers.join('\r\n'), 'utf8');
- },
-
- _parseLeadingByte: function(octet) {
- if ((octet & 0x80) === 0x80) {
- this._length = 0;
- this._stage = 1;
- } else {
- delete this._length;
- delete this._skipped;
- this._buffer = [];
- this._stage = 2;
- }
- }
- };
-
- for (var key$4 in instance$4)
- Draft75$2.prototype[key$4] = instance$4[key$4];
-
- var draft75 = Draft75$2;
-
- var Buffer$1 = safeBuffer.exports.Buffer,
- Base$2 = base,
- Draft75$1 = draft75,
- crypto = require$$1__default$1["default"],
- util$6 = require$$2__default["default"];
-
-
- var numberFromKey = function(key) {
- return parseInt((key.match(/[0-9]/g) || []).join(''), 10);
- };
-
- var spacesInKey = function(key) {
- return (key.match(/ /g) || []).length;
- };
-
-
- var Draft76$1 = function(request, url, options) {
- Draft75$1.apply(this, arguments);
- this._stage = -1;
- this._body = [];
- this.version = 'hixie-76';
-
- this._headers.clear();
-
- this._headers.set('Upgrade', 'WebSocket');
- this._headers.set('Connection', 'Upgrade');
- this._headers.set('Sec-WebSocket-Origin', this._request.headers.origin);
- this._headers.set('Sec-WebSocket-Location', this.url);
- };
- util$6.inherits(Draft76$1, Draft75$1);
-
- var instance$3 = {
- BODY_SIZE: 8,
-
- start: function() {
- if (!Draft75$1.prototype.start.call(this)) return false;
- this._started = true;
- this._sendHandshakeBody();
- return true;
- },
-
- close: function() {
- if (this.readyState === 3) return false;
- if (this.readyState === 1) this._write(Buffer$1.from([0xFF, 0x00]));
- this.readyState = 3;
- this.emit('close', new Base$2.CloseEvent(null, null));
- return true;
- },
-
- _handshakeResponse: function() {
- var headers = this._request.headers,
- key1 = headers['sec-websocket-key1'],
- key2 = headers['sec-websocket-key2'];
-
- if (!key1) throw new Error('Missing required header: Sec-WebSocket-Key1');
- if (!key2) throw new Error('Missing required header: Sec-WebSocket-Key2');
-
- var number1 = numberFromKey(key1),
- spaces1 = spacesInKey(key1),
-
- number2 = numberFromKey(key2),
- spaces2 = spacesInKey(key2);
-
- if (number1 % spaces1 !== 0 || number2 % spaces2 !== 0)
- throw new Error('Client sent invalid Sec-WebSocket-Key headers');
-
- this._keyValues = [number1 / spaces1, number2 / spaces2];
-
- var start = 'HTTP/1.1 101 WebSocket Protocol Handshake',
- headers = [start, this._headers.toString(), ''];
-
- return Buffer$1.from(headers.join('\r\n'), 'binary');
- },
-
- _handshakeSignature: function() {
- if (this._body.length < this.BODY_SIZE) return null;
-
- var md5 = crypto.createHash('md5'),
- buffer = Buffer$1.allocUnsafe(8 + this.BODY_SIZE);
-
- buffer.writeUInt32BE(this._keyValues[0], 0);
- buffer.writeUInt32BE(this._keyValues[1], 4);
- Buffer$1.from(this._body).copy(buffer, 8, 0, this.BODY_SIZE);
-
- md5.update(buffer);
- return Buffer$1.from(md5.digest('binary'), 'binary');
- },
-
- _sendHandshakeBody: function() {
- if (!this._started) return;
- var signature = this._handshakeSignature();
- if (!signature) return;
-
- this._write(signature);
- this._stage = 0;
- this._open();
-
- if (this._body.length > this.BODY_SIZE)
- this.parse(this._body.slice(this.BODY_SIZE));
- },
-
- _parseLeadingByte: function(octet) {
- if (octet !== 0xFF)
- return Draft75$1.prototype._parseLeadingByte.call(this, octet);
-
- this._closing = true;
- this._length = 0;
- this._stage = 1;
- }
- };
-
- for (var key$3 in instance$3)
- Draft76$1.prototype[key$3] = instance$3[key$3];
-
- var draft76 = Draft76$1;
-
- var util$5 = require$$2__default["default"],
- HttpParser = http_parser,
- Base$1 = base,
- Draft75 = draft75,
- Draft76 = draft76,
- Hybi = hybi;
-
- var Server$1 = function(options) {
- Base$1.call(this, null, null, options);
- this._http = new HttpParser('request');
- };
- util$5.inherits(Server$1, Base$1);
-
- var instance$2 = {
- EVENTS: ['open', 'message', 'error', 'close', 'ping', 'pong'],
-
- _bindEventListeners: function() {
- this.messages.on('error', function() {});
- this.on('error', function() {});
- },
-
- parse: function(chunk) {
- if (this._delegate) return this._delegate.parse(chunk);
-
- this._http.parse(chunk);
- if (!this._http.isComplete()) return;
-
- this.method = this._http.method;
- this.url = this._http.url;
- this.headers = this._http.headers;
- this.body = this._http.body;
-
- var self = this;
- this._delegate = Server$1.http(this, this._options);
- this._delegate.messages = this.messages;
- this._delegate.io = this.io;
- this._open();
-
- this.EVENTS.forEach(function(event) {
- this._delegate.on(event, function(e) { self.emit(event, e); });
- }, this);
-
- this.protocol = this._delegate.protocol;
- this.version = this._delegate.version;
-
- this.parse(this._http.body);
- this.emit('connect', new Base$1.ConnectEvent());
- },
-
- _open: function() {
- this.__queue.forEach(function(msg) {
- this._delegate[msg[0]].apply(this._delegate, msg[1]);
- }, this);
- this.__queue = [];
- }
- };
-
- ['addExtension', 'setHeader', 'start', 'frame', 'text', 'binary', 'ping', 'close'].forEach(function(method) {
- instance$2[method] = function() {
- if (this._delegate) {
- return this._delegate[method].apply(this._delegate, arguments);
- } else {
- this.__queue.push([method, arguments]);
- return true;
- }
- };
- });
-
- for (var key$2 in instance$2)
- Server$1.prototype[key$2] = instance$2[key$2];
-
- Server$1.isSecureRequest = function(request) {
- if (request.connection && request.connection.authorized !== undefined) return true;
- if (request.socket && request.socket.secure) return true;
-
- var headers = request.headers;
- if (!headers) return false;
- if (headers['https'] === 'on') return true;
- if (headers['x-forwarded-ssl'] === 'on') return true;
- if (headers['x-forwarded-scheme'] === 'https') return true;
- if (headers['x-forwarded-proto'] === 'https') return true;
-
- return false;
- };
-
- Server$1.determineUrl = function(request) {
- var scheme = this.isSecureRequest(request) ? 'wss:' : 'ws:';
- return scheme + '//' + request.headers.host + request.url;
- };
-
- Server$1.http = function(request, options) {
- options = options || {};
- if (options.requireMasking === undefined) options.requireMasking = true;
-
- var headers = request.headers,
- version = headers['sec-websocket-version'],
- key = headers['sec-websocket-key'],
- key1 = headers['sec-websocket-key1'],
- key2 = headers['sec-websocket-key2'],
- url = this.determineUrl(request);
-
- if (version || key)
- return new Hybi(request, url, options);
- else if (key1 || key2)
- return new Draft76(request, url, options);
- else
- return new Draft75(request, url, options);
- };
-
- var server = Server$1;
-
- // Protocol references:
- //
- // * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75
- // * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
- // * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
-
- var Base = base,
- Client$1 = client$1,
- Server = server;
-
- var Driver = {
- client: function(url, options) {
- options = options || {};
- if (options.masking === undefined) options.masking = true;
- return new Client$1(url, options);
- },
-
- server: function(options) {
- options = options || {};
- if (options.requireMasking === undefined) options.requireMasking = true;
- return new Server(options);
- },
-
- http: function() {
- return Server.http.apply(Server, arguments);
- },
-
- isSecureRequest: function(request) {
- return Server.isSecureRequest(request);
- },
-
- isWebSocket: function(request) {
- return Base.isWebSocket(request);
- },
-
- validateOptions: function(options, validKeys) {
- Base.validateOptions(options, validKeys);
- }
- };
-
- var driver$4 = Driver;
-
- var Event$3 = function(eventType, options) {
- this.type = eventType;
- for (var key in options)
- this[key] = options[key];
- };
-
- Event$3.prototype.initEvent = function(eventType, canBubble, cancelable) {
- this.type = eventType;
- this.bubbles = canBubble;
- this.cancelable = cancelable;
- };
-
- Event$3.prototype.stopPropagation = function() {};
- Event$3.prototype.preventDefault = function() {};
-
- Event$3.CAPTURING_PHASE = 1;
- Event$3.AT_TARGET = 2;
- Event$3.BUBBLING_PHASE = 3;
-
- var event = Event$3;
-
- var Event$2 = event;
-
- var EventTarget$2 = {
- onopen: null,
- onmessage: null,
- onerror: null,
- onclose: null,
-
- addEventListener: function(eventType, listener, useCapture) {
- this.on(eventType, listener);
- },
-
- removeEventListener: function(eventType, listener, useCapture) {
- this.removeListener(eventType, listener);
- },
-
- dispatchEvent: function(event) {
- event.target = event.currentTarget = this;
- event.eventPhase = Event$2.AT_TARGET;
-
- if (this['on' + event.type])
- this['on' + event.type](event);
-
- this.emit(event.type, event);
- }
- };
-
- var event_target = EventTarget$2;
-
- var Stream$1 = require$$0__default$1["default"].Stream,
- util$4 = require$$2__default["default"],
- driver$3 = driver$4,
- EventTarget$1 = event_target,
- Event$1 = event;
-
- var API$3 = function(options) {
- options = options || {};
- driver$3.validateOptions(options, ['headers', 'extensions', 'maxLength', 'ping', 'proxy', 'tls', 'ca']);
-
- this.readable = this.writable = true;
-
- var headers = options.headers;
- if (headers) {
- for (var name in headers) this._driver.setHeader(name, headers[name]);
- }
-
- var extensions = options.extensions;
- if (extensions) {
- [].concat(extensions).forEach(this._driver.addExtension, this._driver);
- }
-
- this._ping = options.ping;
- this._pingId = 0;
- this.readyState = API$3.CONNECTING;
- this.bufferedAmount = 0;
- this.protocol = '';
- this.url = this._driver.url;
- this.version = this._driver.version;
-
- var self = this;
-
- this._driver.on('open', function(e) { self._open(); });
- this._driver.on('message', function(e) { self._receiveMessage(e.data); });
- this._driver.on('close', function(e) { self._beginClose(e.reason, e.code); });
-
- this._driver.on('error', function(error) {
- self._emitError(error.message);
- });
- this.on('error', function() {});
-
- this._driver.messages.on('drain', function() {
- self.emit('drain');
- });
-
- if (this._ping)
- this._pingTimer = setInterval(function() {
- self._pingId += 1;
- self.ping(self._pingId.toString());
- }, this._ping * 1000);
-
- this._configureStream();
-
- if (!this._proxy) {
- this._stream.pipe(this._driver.io);
- this._driver.io.pipe(this._stream);
- }
- };
- util$4.inherits(API$3, Stream$1);
-
- API$3.CONNECTING = 0;
- API$3.OPEN = 1;
- API$3.CLOSING = 2;
- API$3.CLOSED = 3;
-
- API$3.CLOSE_TIMEOUT = 30000;
-
- var instance$1 = {
- write: function(data) {
- return this.send(data);
- },
-
- end: function(data) {
- if (data !== undefined) this.send(data);
- this.close();
- },
-
- pause: function() {
- return this._driver.messages.pause();
- },
-
- resume: function() {
- return this._driver.messages.resume();
- },
-
- send: function(data) {
- if (this.readyState > API$3.OPEN) return false;
- if (!(data instanceof Buffer)) data = String(data);
- return this._driver.messages.write(data);
- },
-
- ping: function(message, callback) {
- if (this.readyState > API$3.OPEN) return false;
- return this._driver.ping(message, callback);
- },
-
- close: function(code, reason) {
- if (code === undefined) code = 1000;
- if (reason === undefined) reason = '';
-
- if (code !== 1000 && (code < 3000 || code > 4999))
- throw new Error("Failed to execute 'close' on WebSocket: " +
- "The code must be either 1000, or between 3000 and 4999. " +
- code + " is neither.");
-
- if (this.readyState < API$3.CLOSING) {
- var self = this;
- this._closeTimer = setTimeout(function() {
- self._beginClose('', 1006);
- }, API$3.CLOSE_TIMEOUT);
- }
-
- if (this.readyState !== API$3.CLOSED) this.readyState = API$3.CLOSING;
-
- this._driver.close(reason, code);
- },
-
- _configureStream: function() {
- var self = this;
-
- this._stream.setTimeout(0);
- this._stream.setNoDelay(true);
-
- ['close', 'end'].forEach(function(event) {
- this._stream.on(event, function() { self._finalizeClose(); });
- }, this);
-
- this._stream.on('error', function(error) {
- self._emitError('Network error: ' + self.url + ': ' + error.message);
- self._finalizeClose();
- });
- },
-
- _open: function() {
- if (this.readyState !== API$3.CONNECTING) return;
-
- this.readyState = API$3.OPEN;
- this.protocol = this._driver.protocol || '';
-
- var event = new Event$1('open');
- event.initEvent('open', false, false);
- this.dispatchEvent(event);
- },
-
- _receiveMessage: function(data) {
- if (this.readyState > API$3.OPEN) return false;
-
- if (this.readable) this.emit('data', data);
-
- var event = new Event$1('message', { data: data });
- event.initEvent('message', false, false);
- this.dispatchEvent(event);
- },
-
- _emitError: function(message) {
- if (this.readyState >= API$3.CLOSING) return;
-
- var event = new Event$1('error', { message: message });
- event.initEvent('error', false, false);
- this.dispatchEvent(event);
- },
-
- _beginClose: function(reason, code) {
- if (this.readyState === API$3.CLOSED) return;
- this.readyState = API$3.CLOSING;
- this._closeParams = [reason, code];
-
- if (this._stream) {
- this._stream.destroy();
- if (!this._stream.readable) this._finalizeClose();
- }
- },
-
- _finalizeClose: function() {
- if (this.readyState === API$3.CLOSED) return;
- this.readyState = API$3.CLOSED;
-
- if (this._closeTimer) clearTimeout(this._closeTimer);
- if (this._pingTimer) clearInterval(this._pingTimer);
- if (this._stream) this._stream.end();
-
- if (this.readable) this.emit('end');
- this.readable = this.writable = false;
-
- var reason = this._closeParams ? this._closeParams[0] : '',
- code = this._closeParams ? this._closeParams[1] : 1006;
-
- var event = new Event$1('close', { code: code, reason: reason });
- event.initEvent('close', false, false);
- this.dispatchEvent(event);
- }
- };
-
- for (var method$1 in instance$1) API$3.prototype[method$1] = instance$1[method$1];
- for (var key$1 in EventTarget$1) API$3.prototype[key$1] = EventTarget$1[key$1];
-
- var api = API$3;
-
- var util$3 = require$$2__default["default"],
- net = require$$1__default$2["default"],
- tls = require$$2__default$2["default"],
- url = require$$2__default$1["default"],
- driver$2 = driver$4,
- API$2 = api;
-
- var DEFAULT_PORTS = { 'http:': 80, 'https:': 443, 'ws:':80, 'wss:': 443 },
- SECURE_PROTOCOLS = ['https:', 'wss:'];
-
- var Client = function(_url, protocols, options) {
- options = options || {};
-
- this.url = _url;
- this._driver = driver$2.client(this.url, { maxLength: options.maxLength, protocols: protocols });
-
- ['open', 'error'].forEach(function(event) {
- this._driver.on(event, function() {
- self.headers = self._driver.headers;
- self.statusCode = self._driver.statusCode;
- });
- }, this);
-
- var proxy = options.proxy || {},
- endpoint = url.parse(proxy.origin || this.url),
- port = endpoint.port || DEFAULT_PORTS[endpoint.protocol],
- secure = SECURE_PROTOCOLS.indexOf(endpoint.protocol) >= 0,
- onConnect = function() { self._onConnect(); },
- netOptions = options.net || {},
- originTLS = options.tls || {},
- socketTLS = proxy.origin ? (proxy.tls || {}) : originTLS,
- self = this;
-
- netOptions.host = socketTLS.host = endpoint.hostname;
- netOptions.port = socketTLS.port = port;
-
- originTLS.ca = originTLS.ca || options.ca;
- socketTLS.servername = socketTLS.servername || endpoint.hostname;
-
- this._stream = secure
- ? tls.connect(socketTLS, onConnect)
- : net.connect(netOptions, onConnect);
-
- if (proxy.origin) this._configureProxy(proxy, originTLS);
-
- API$2.call(this, options);
- };
- util$3.inherits(Client, API$2);
-
- Client.prototype._onConnect = function() {
- var worker = this._proxy || this._driver;
- worker.start();
- };
-
- Client.prototype._configureProxy = function(proxy, originTLS) {
- var uri = url.parse(this.url),
- secure = SECURE_PROTOCOLS.indexOf(uri.protocol) >= 0,
- self = this,
- name;
-
- this._proxy = this._driver.proxy(proxy.origin);
-
- if (proxy.headers) {
- for (name in proxy.headers) this._proxy.setHeader(name, proxy.headers[name]);
- }
-
- this._proxy.pipe(this._stream, { end: false });
- this._stream.pipe(this._proxy);
-
- this._proxy.on('connect', function() {
- if (secure) {
- var options = { socket: self._stream, servername: uri.hostname };
- for (name in originTLS) options[name] = originTLS[name];
- self._stream = tls.connect(options);
- self._configureStream();
- }
- self._driver.io.pipe(self._stream);
- self._stream.pipe(self._driver.io);
- self._driver.start();
- });
-
- this._proxy.on('error', function(error) {
- self._driver.emit('error', error);
- });
- };
-
- var client = Client;
-
- var Stream = require$$0__default$1["default"].Stream,
- util$2 = require$$2__default["default"],
- driver$1 = driver$4,
- Headers = headers,
- API$1 = api,
- EventTarget = event_target,
- Event = event;
-
- var EventSource = function(request, response, options) {
- this.writable = true;
- options = options || {};
-
- this._stream = response.socket;
- this._ping = options.ping || this.DEFAULT_PING;
- this._retry = options.retry || this.DEFAULT_RETRY;
-
- var scheme = driver$1.isSecureRequest(request) ? 'https:' : 'http:';
- this.url = scheme + '//' + request.headers.host + request.url;
- this.lastEventId = request.headers['last-event-id'] || '';
- this.readyState = API$1.CONNECTING;
-
- var headers = new Headers(),
- self = this;
-
- if (options.headers) {
- for (var key in options.headers) headers.set(key, options.headers[key]);
- }
-
- if (!this._stream || !this._stream.writable) return;
- process.nextTick(function() { self._open(); });
-
- this._stream.setTimeout(0);
- this._stream.setNoDelay(true);
-
- var handshake = 'HTTP/1.1 200 OK\r\n' +
- 'Content-Type: text/event-stream\r\n' +
- 'Cache-Control: no-cache, no-store\r\n' +
- 'Connection: close\r\n' +
- headers.toString() +
- '\r\n' +
- 'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n';
-
- this._write(handshake);
-
- this._stream.on('drain', function() { self.emit('drain'); });
-
- if (this._ping)
- this._pingTimer = setInterval(function() { self.ping(); }, this._ping * 1000);
-
- ['error', 'end'].forEach(function(event) {
- self._stream.on(event, function() { self.close(); });
- });
- };
- util$2.inherits(EventSource, Stream);
-
- EventSource.isEventSource = function(request) {
- if (request.method !== 'GET') return false;
- var accept = (request.headers.accept || '').split(/\s*,\s*/);
- return accept.indexOf('text/event-stream') >= 0;
- };
-
- var instance = {
- DEFAULT_PING: 10,
- DEFAULT_RETRY: 5,
-
- _write: function(chunk) {
- if (!this.writable) return false;
- try {
- return this._stream.write(chunk, 'utf8');
- } catch (e) {
- return false;
- }
- },
-
- _open: function() {
- if (this.readyState !== API$1.CONNECTING) return;
-
- this.readyState = API$1.OPEN;
-
- var event = new Event('open');
- event.initEvent('open', false, false);
- this.dispatchEvent(event);
- },
-
- write: function(message) {
- return this.send(message);
- },
-
- end: function(message) {
- if (message !== undefined) this.write(message);
- this.close();
- },
-
- send: function(message, options) {
- if (this.readyState > API$1.OPEN) return false;
-
- message = String(message).replace(/(\r\n|\r|\n)/g, '$1data: ');
- options = options || {};
-
- var frame = '';
- if (options.event) frame += 'event: ' + options.event + '\r\n';
- if (options.id) frame += 'id: ' + options.id + '\r\n';
- frame += 'data: ' + message + '\r\n\r\n';
-
- return this._write(frame);
- },
-
- ping: function() {
- return this._write(':\r\n\r\n');
- },
-
- close: function() {
- if (this.readyState > API$1.OPEN) return false;
-
- this.readyState = API$1.CLOSED;
- this.writable = false;
- if (this._pingTimer) clearInterval(this._pingTimer);
- if (this._stream) this._stream.end();
-
- var event = new Event('close');
- event.initEvent('close', false, false);
- this.dispatchEvent(event);
-
- return true;
- }
- };
-
- for (var method in instance) EventSource.prototype[method] = instance[method];
- for (var key in EventTarget) EventSource.prototype[key] = EventTarget[key];
-
- var eventsource = EventSource;
-
- var util$1 = require$$2__default["default"],
- driver = driver$4,
- API = api;
-
- var WebSocket$1 = function(request, socket, body, protocols, options) {
- options = options || {};
-
- this._stream = socket;
- this._driver = driver.http(request, { maxLength: options.maxLength, protocols: protocols });
-
- var self = this;
- if (!this._stream || !this._stream.writable) return;
- if (!this._stream.readable) return this._stream.end();
-
- var catchup = function() { self._stream.removeListener('data', catchup); };
- this._stream.on('data', catchup);
-
- API.call(this, options);
-
- process.nextTick(function() {
- self._driver.start();
- self._driver.io.write(body);
- });
- };
- util$1.inherits(WebSocket$1, API);
-
- WebSocket$1.isWebSocket = function(request) {
- return driver.isWebSocket(request);
- };
-
- WebSocket$1.validateOptions = function(options, validKeys) {
- driver.validateOptions(options, validKeys);
- };
-
- WebSocket$1.WebSocket = WebSocket$1;
- WebSocket$1.Client = client;
- WebSocket$1.EventSource = eventsource;
-
- var websocket = WebSocket$1;
-
- Object.defineProperty(index_standalone, '__esModule', { value: true });
-
- var Websocket = websocket;
- var util = require$$1__default$3["default"];
- var tslib = require$$2__default$3["default"];
- var logger$1 = require$$3__default["default"];
-
- function _interopDefaultLegacy$1 (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
-
- var Websocket__default = /*#__PURE__*/_interopDefaultLegacy$1(Websocket);
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var PROTOCOL_VERSION = '5';
- var VERSION_PARAM = 'v';
- var TRANSPORT_SESSION_PARAM = 's';
- var REFERER_PARAM = 'r';
- var FORGE_REF = 'f';
- // Matches console.firebase.google.com, firebase-console-*.corp.google.com and
- // firebase.corp.google.com
- var FORGE_DOMAIN_RE = /(console\.firebase|firebase-console-\w+\.corp|firebase\.corp)\.google\.com/;
- var LAST_SESSION_PARAM = 'ls';
- var APPLICATION_ID_PARAM = 'p';
- var APP_CHECK_TOKEN_PARAM = 'ac';
- var WEBSOCKET = 'websocket';
- var LONG_POLLING = 'long_polling';
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Wraps a DOM Storage object and:
- * - automatically encode objects as JSON strings before storing them to allow us to store arbitrary types.
- * - prefixes names with "firebase:" to avoid collisions with app data.
- *
- * We automatically (see storage.js) create two such wrappers, one for sessionStorage,
- * and one for localStorage.
- *
- */
- var DOMStorageWrapper = /** @class */ (function () {
- /**
- * @param domStorage_ - The underlying storage object (e.g. localStorage or sessionStorage)
- */
- function DOMStorageWrapper(domStorage_) {
- this.domStorage_ = domStorage_;
- // Use a prefix to avoid collisions with other stuff saved by the app.
- this.prefix_ = 'firebase:';
- }
- /**
- * @param key - The key to save the value under
- * @param value - The value being stored, or null to remove the key.
- */
- DOMStorageWrapper.prototype.set = function (key, value) {
- if (value == null) {
- this.domStorage_.removeItem(this.prefixedName_(key));
- }
- else {
- this.domStorage_.setItem(this.prefixedName_(key), util.stringify(value));
- }
- };
- /**
- * @returns The value that was stored under this key, or null
- */
- DOMStorageWrapper.prototype.get = function (key) {
- var storedVal = this.domStorage_.getItem(this.prefixedName_(key));
- if (storedVal == null) {
- return null;
- }
- else {
- return util.jsonEval(storedVal);
- }
- };
- DOMStorageWrapper.prototype.remove = function (key) {
- this.domStorage_.removeItem(this.prefixedName_(key));
- };
- DOMStorageWrapper.prototype.prefixedName_ = function (name) {
- return this.prefix_ + name;
- };
- DOMStorageWrapper.prototype.toString = function () {
- return this.domStorage_.toString();
- };
- return DOMStorageWrapper;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An in-memory storage implementation that matches the API of DOMStorageWrapper
- * (TODO: create interface for both to implement).
- */
- var MemoryStorage = /** @class */ (function () {
- function MemoryStorage() {
- this.cache_ = {};
- this.isInMemoryStorage = true;
- }
- MemoryStorage.prototype.set = function (key, value) {
- if (value == null) {
- delete this.cache_[key];
- }
- else {
- this.cache_[key] = value;
- }
- };
- MemoryStorage.prototype.get = function (key) {
- if (util.contains(this.cache_, key)) {
- return this.cache_[key];
- }
- return null;
- };
- MemoryStorage.prototype.remove = function (key) {
- delete this.cache_[key];
- };
- return MemoryStorage;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Helper to create a DOMStorageWrapper or else fall back to MemoryStorage.
- * TODO: Once MemoryStorage and DOMStorageWrapper have a shared interface this method annotation should change
- * to reflect this type
- *
- * @param domStorageName - Name of the underlying storage object
- * (e.g. 'localStorage' or 'sessionStorage').
- * @returns Turning off type information until a common interface is defined.
- */
- var createStoragefor = function (domStorageName) {
- try {
- // NOTE: just accessing "localStorage" or "window['localStorage']" may throw a security exception,
- // so it must be inside the try/catch.
- if (typeof window !== 'undefined' &&
- typeof window[domStorageName] !== 'undefined') {
- // Need to test cache. Just because it's here doesn't mean it works
- var domStorage = window[domStorageName];
- domStorage.setItem('firebase:sentinel', 'cache');
- domStorage.removeItem('firebase:sentinel');
- return new DOMStorageWrapper(domStorage);
- }
- }
- catch (e) { }
- // Failed to create wrapper. Just return in-memory storage.
- // TODO: log?
- return new MemoryStorage();
- };
- /** A storage object that lasts across sessions */
- var PersistentStorage = createStoragefor('localStorage');
- /** A storage object that only lasts one session */
- var SessionStorage = createStoragefor('sessionStorage');
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var logClient$1 = new logger$1.Logger('@firebase/database');
- /**
- * Returns a locally-unique ID (generated by just incrementing up from 0 each time its called).
- */
- var LUIDGenerator = (function () {
- var id = 1;
- return function () {
- return id++;
- };
- })();
- /**
- * Sha1 hash of the input string
- * @param str - The string to hash
- * @returns {!string} The resulting hash
- */
- var sha1 = function (str) {
- var utf8Bytes = util.stringToByteArray(str);
- var sha1 = new util.Sha1();
- sha1.update(utf8Bytes);
- var sha1Bytes = sha1.digest();
- return util.base64.encodeByteArray(sha1Bytes);
- };
- var buildLogMessage_ = function () {
- var varArgs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- varArgs[_i] = arguments[_i];
- }
- var message = '';
- for (var i = 0; i < varArgs.length; i++) {
- var arg = varArgs[i];
- if (Array.isArray(arg) ||
- (arg &&
- typeof arg === 'object' &&
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- typeof arg.length === 'number')) {
- message += buildLogMessage_.apply(null, arg);
- }
- else if (typeof arg === 'object') {
- message += util.stringify(arg);
- }
- else {
- message += arg;
- }
- message += ' ';
- }
- return message;
- };
- /**
- * Use this for all debug messages in Firebase.
- */
- var logger = null;
- /**
- * Flag to check for log availability on first log message
- */
- var firstLog_ = true;
- /**
- * The implementation of Firebase.enableLogging (defined here to break dependencies)
- * @param logger_ - A flag to turn on logging, or a custom logger
- * @param persistent - Whether or not to persist logging settings across refreshes
- */
- var enableLogging$1 = function (logger_, persistent) {
- util.assert(!persistent || logger_ === true || logger_ === false, "Can't turn on custom loggers persistently.");
- if (logger_ === true) {
- logClient$1.logLevel = logger$1.LogLevel.VERBOSE;
- logger = logClient$1.log.bind(logClient$1);
- if (persistent) {
- SessionStorage.set('logging_enabled', true);
- }
- }
- else if (typeof logger_ === 'function') {
- logger = logger_;
- }
- else {
- logger = null;
- SessionStorage.remove('logging_enabled');
- }
- };
- var log = function () {
- var varArgs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- varArgs[_i] = arguments[_i];
- }
- if (firstLog_ === true) {
- firstLog_ = false;
- if (logger === null && SessionStorage.get('logging_enabled') === true) {
- enableLogging$1(true);
- }
- }
- if (logger) {
- var message = buildLogMessage_.apply(null, varArgs);
- logger(message);
- }
- };
- var logWrapper = function (prefix) {
- return function () {
- var varArgs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- varArgs[_i] = arguments[_i];
- }
- log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs), false));
- };
- };
- var error = function () {
- var varArgs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- varArgs[_i] = arguments[_i];
- }
- var message = 'FIREBASE INTERNAL ERROR: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs), false));
- logClient$1.error(message);
- };
- var fatal = function () {
- var varArgs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- varArgs[_i] = arguments[_i];
- }
- var message = "FIREBASE FATAL ERROR: ".concat(buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs), false)));
- logClient$1.error(message);
- throw new Error(message);
- };
- var warn$1 = function () {
- var varArgs = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- varArgs[_i] = arguments[_i];
- }
- var message = 'FIREBASE WARNING: ' + buildLogMessage_.apply(void 0, tslib.__spreadArray([], tslib.__read(varArgs), false));
- logClient$1.warn(message);
- };
- /**
- * Logs a warning if the containing page uses https. Called when a call to new Firebase
- * does not use https.
- */
- var warnIfPageIsSecure = function () {
- // Be very careful accessing browser globals. Who knows what may or may not exist.
- if (typeof window !== 'undefined' &&
- window.location &&
- window.location.protocol &&
- window.location.protocol.indexOf('https:') !== -1) {
- warn$1('Insecure Firebase access from a secure page. ' +
- 'Please use https in calls to new Firebase().');
- }
- };
- /**
- * Returns true if data is NaN, or +/- Infinity.
- */
- var isInvalidJSONNumber = function (data) {
- return (typeof data === 'number' &&
- (data !== data || // NaN
- data === Number.POSITIVE_INFINITY ||
- data === Number.NEGATIVE_INFINITY));
- };
- var executeWhenDOMReady = function (fn) {
- if (util.isNodeSdk() || document.readyState === 'complete') {
- fn();
- }
- else {
- // Modeled after jQuery. Try DOMContentLoaded and onreadystatechange (which
- // fire before onload), but fall back to onload.
- var called_1 = false;
- var wrappedFn_1 = function () {
- if (!document.body) {
- setTimeout(wrappedFn_1, Math.floor(10));
- return;
- }
- if (!called_1) {
- called_1 = true;
- fn();
- }
- };
- if (document.addEventListener) {
- document.addEventListener('DOMContentLoaded', wrappedFn_1, false);
- // fallback to onload.
- window.addEventListener('load', wrappedFn_1, false);
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }
- else if (document.attachEvent) {
- // IE.
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- document.attachEvent('onreadystatechange', function () {
- if (document.readyState === 'complete') {
- wrappedFn_1();
- }
- });
- // fallback to onload.
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- window.attachEvent('onload', wrappedFn_1);
- // jQuery has an extra hack for IE that we could employ (based on
- // http://javascript.nwbox.com/IEContentLoaded/) But it looks really old.
- // I'm hoping we don't need it.
- }
- }
- };
- /**
- * Minimum key name. Invalid for actual data, used as a marker to sort before any valid names
- */
- var MIN_NAME = '[MIN_NAME]';
- /**
- * Maximum key name. Invalid for actual data, used as a marker to sort above any valid names
- */
- var MAX_NAME = '[MAX_NAME]';
- /**
- * Compares valid Firebase key names, plus min and max name
- */
- var nameCompare = function (a, b) {
- if (a === b) {
- return 0;
- }
- else if (a === MIN_NAME || b === MAX_NAME) {
- return -1;
- }
- else if (b === MIN_NAME || a === MAX_NAME) {
- return 1;
- }
- else {
- var aAsInt = tryParseInt(a), bAsInt = tryParseInt(b);
- if (aAsInt !== null) {
- if (bAsInt !== null) {
- return aAsInt - bAsInt === 0 ? a.length - b.length : aAsInt - bAsInt;
- }
- else {
- return -1;
- }
- }
- else if (bAsInt !== null) {
- return 1;
- }
- else {
- return a < b ? -1 : 1;
- }
- }
- };
- /**
- * @returns {!number} comparison result.
- */
- var stringCompare = function (a, b) {
- if (a === b) {
- return 0;
- }
- else if (a < b) {
- return -1;
- }
- else {
- return 1;
- }
- };
- var requireKey = function (key, obj) {
- if (obj && key in obj) {
- return obj[key];
- }
- else {
- throw new Error('Missing required key (' + key + ') in object: ' + util.stringify(obj));
- }
- };
- var ObjectToUniqueKey = function (obj) {
- if (typeof obj !== 'object' || obj === null) {
- return util.stringify(obj);
- }
- var keys = [];
- // eslint-disable-next-line guard-for-in
- for (var k in obj) {
- keys.push(k);
- }
- // Export as json, but with the keys sorted.
- keys.sort();
- var key = '{';
- for (var i = 0; i < keys.length; i++) {
- if (i !== 0) {
- key += ',';
- }
- key += util.stringify(keys[i]);
- key += ':';
- key += ObjectToUniqueKey(obj[keys[i]]);
- }
- key += '}';
- return key;
- };
- /**
- * Splits a string into a number of smaller segments of maximum size
- * @param str - The string
- * @param segsize - The maximum number of chars in the string.
- * @returns The string, split into appropriately-sized chunks
- */
- var splitStringBySize = function (str, segsize) {
- var len = str.length;
- if (len <= segsize) {
- return [str];
- }
- var dataSegs = [];
- for (var c = 0; c < len; c += segsize) {
- if (c + segsize > len) {
- dataSegs.push(str.substring(c, len));
- }
- else {
- dataSegs.push(str.substring(c, c + segsize));
- }
- }
- return dataSegs;
- };
- /**
- * Apply a function to each (key, value) pair in an object or
- * apply a function to each (index, value) pair in an array
- * @param obj - The object or array to iterate over
- * @param fn - The function to apply
- */
- function each(obj, fn) {
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- fn(key, obj[key]);
- }
- }
- }
- /**
- * Borrowed from http://hg.secondlife.com/llsd/src/tip/js/typedarray.js (MIT License)
- * I made one modification at the end and removed the NaN / Infinity
- * handling (since it seemed broken [caused an overflow] and we don't need it). See MJL comments.
- * @param v - A double
- *
- */
- var doubleToIEEE754String = function (v) {
- util.assert(!isInvalidJSONNumber(v), 'Invalid JSON number'); // MJL
- var ebits = 11, fbits = 52;
- var bias = (1 << (ebits - 1)) - 1;
- var s, e, f, ln, i;
- // Compute sign, exponent, fraction
- // Skip NaN / Infinity handling --MJL.
- if (v === 0) {
- e = 0;
- f = 0;
- s = 1 / v === -Infinity ? 1 : 0;
- }
- else {
- s = v < 0;
- v = Math.abs(v);
- if (v >= Math.pow(2, 1 - bias)) {
- // Normalized
- ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
- e = ln + bias;
- f = Math.round(v * Math.pow(2, fbits - ln) - Math.pow(2, fbits));
- }
- else {
- // Denormalized
- e = 0;
- f = Math.round(v / Math.pow(2, 1 - bias - fbits));
- }
- }
- // Pack sign, exponent, fraction
- var bits = [];
- for (i = fbits; i; i -= 1) {
- bits.push(f % 2 ? 1 : 0);
- f = Math.floor(f / 2);
- }
- for (i = ebits; i; i -= 1) {
- bits.push(e % 2 ? 1 : 0);
- e = Math.floor(e / 2);
- }
- bits.push(s ? 1 : 0);
- bits.reverse();
- var str = bits.join('');
- // Return the data as a hex string. --MJL
- var hexByteString = '';
- for (i = 0; i < 64; i += 8) {
- var hexByte = parseInt(str.substr(i, 8), 2).toString(16);
- if (hexByte.length === 1) {
- hexByte = '0' + hexByte;
- }
- hexByteString = hexByteString + hexByte;
- }
- return hexByteString.toLowerCase();
- };
- /**
- * Used to detect if we're in a Chrome content script (which executes in an
- * isolated environment where long-polling doesn't work).
- */
- var isChromeExtensionContentScript = function () {
- return !!(typeof window === 'object' &&
- window['chrome'] &&
- window['chrome']['extension'] &&
- !/^chrome/.test(window.location.href));
- };
- /**
- * Used to detect if we're in a Windows 8 Store app.
- */
- var isWindowsStoreApp = function () {
- // Check for the presence of a couple WinRT globals
- return typeof Windows === 'object' && typeof Windows.UI === 'object';
- };
- /**
- * Converts a server error code to a Javascript Error
- */
- function errorForServerCode(code, query) {
- var reason = 'Unknown Error';
- if (code === 'too_big') {
- reason =
- 'The data requested exceeds the maximum size ' +
- 'that can be accessed with a single request.';
- }
- else if (code === 'permission_denied') {
- reason = "Client doesn't have permission to access the desired data.";
- }
- else if (code === 'unavailable') {
- reason = 'The service is unavailable';
- }
- var error = new Error(code + ' at ' + query._path.toString() + ': ' + reason);
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- error.code = code.toUpperCase();
- return error;
- }
- /**
- * Used to test for integer-looking strings
- */
- var INTEGER_REGEXP_ = new RegExp('^-?(0*)\\d{1,10}$');
- /**
- * For use in keys, the minimum possible 32-bit integer.
- */
- var INTEGER_32_MIN = -2147483648;
- /**
- * For use in kyes, the maximum possible 32-bit integer.
- */
- var INTEGER_32_MAX = 2147483647;
- /**
- * If the string contains a 32-bit integer, return it. Else return null.
- */
- var tryParseInt = function (str) {
- if (INTEGER_REGEXP_.test(str)) {
- var intVal = Number(str);
- if (intVal >= INTEGER_32_MIN && intVal <= INTEGER_32_MAX) {
- return intVal;
- }
- }
- return null;
- };
- /**
- * Helper to run some code but catch any exceptions and re-throw them later.
- * Useful for preventing user callbacks from breaking internal code.
- *
- * Re-throwing the exception from a setTimeout is a little evil, but it's very
- * convenient (we don't have to try to figure out when is a safe point to
- * re-throw it), and the behavior seems reasonable:
- *
- * * If you aren't pausing on exceptions, you get an error in the console with
- * the correct stack trace.
- * * If you're pausing on all exceptions, the debugger will pause on your
- * exception and then again when we rethrow it.
- * * If you're only pausing on uncaught exceptions, the debugger will only pause
- * on us re-throwing it.
- *
- * @param fn - The code to guard.
- */
- var exceptionGuard = function (fn) {
- try {
- fn();
- }
- catch (e) {
- // Re-throw exception when it's safe.
- setTimeout(function () {
- // It used to be that "throw e" would result in a good console error with
- // relevant context, but as of Chrome 39, you just get the firebase.js
- // file/line number where we re-throw it, which is useless. So we log
- // e.stack explicitly.
- var stack = e.stack || '';
- warn$1('Exception was thrown by user callback.', stack);
- throw e;
- }, Math.floor(0));
- }
- };
- /**
- * @returns {boolean} true if we think we're currently being crawled.
- */
- var beingCrawled = function () {
- var userAgent = (typeof window === 'object' &&
- window['navigator'] &&
- window['navigator']['userAgent']) ||
- '';
- // For now we whitelist the most popular crawlers. We should refine this to be the set of crawlers we
- // believe to support JavaScript/AJAX rendering.
- // NOTE: Google Webmaster Tools doesn't really belong, but their "This is how a visitor to your website
- // would have seen the page" is flaky if we don't treat it as a crawler.
- return (userAgent.search(/googlebot|google webmaster tools|bingbot|yahoo! slurp|baiduspider|yandexbot|duckduckbot/i) >= 0);
- };
- /**
- * Same as setTimeout() except on Node.JS it will /not/ prevent the process from exiting.
- *
- * It is removed with clearTimeout() as normal.
- *
- * @param fn - Function to run.
- * @param time - Milliseconds to wait before running.
- * @returns The setTimeout() return value.
- */
- var setTimeoutNonBlocking = function (fn, time) {
- var timeout = setTimeout(fn, time);
- // Note: at the time of this comment, unrefTimer is under the unstable set of APIs. Run with --unstable to enable the API.
- if (typeof timeout === 'number' &&
- // @ts-ignore Is only defined in Deno environments.
- typeof Deno !== 'undefined' &&
- // @ts-ignore Deno and unrefTimer are only defined in Deno environments.
- Deno['unrefTimer']) {
- // @ts-ignore Deno and unrefTimer are only defined in Deno environments.
- Deno.unrefTimer(timeout);
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }
- else if (typeof timeout === 'object' && timeout['unref']) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- timeout['unref']();
- }
- return timeout;
- };
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * A class that holds metadata about a Repo object
- */
- var RepoInfo = /** @class */ (function () {
- /**
- * @param host - Hostname portion of the url for the repo
- * @param secure - Whether or not this repo is accessed over ssl
- * @param namespace - The namespace represented by the repo
- * @param webSocketOnly - Whether to prefer websockets over all other transports (used by Nest).
- * @param nodeAdmin - Whether this instance uses Admin SDK credentials
- * @param persistenceKey - Override the default session persistence storage key
- */
- function RepoInfo(host, secure, namespace, webSocketOnly, nodeAdmin, persistenceKey, includeNamespaceInQueryParams) {
- if (nodeAdmin === void 0) { nodeAdmin = false; }
- if (persistenceKey === void 0) { persistenceKey = ''; }
- if (includeNamespaceInQueryParams === void 0) { includeNamespaceInQueryParams = false; }
- this.secure = secure;
- this.namespace = namespace;
- this.webSocketOnly = webSocketOnly;
- this.nodeAdmin = nodeAdmin;
- this.persistenceKey = persistenceKey;
- this.includeNamespaceInQueryParams = includeNamespaceInQueryParams;
- this._host = host.toLowerCase();
- this._domain = this._host.substr(this._host.indexOf('.') + 1);
- this.internalHost =
- PersistentStorage.get('host:' + host) || this._host;
- }
- RepoInfo.prototype.isCacheableHost = function () {
- return this.internalHost.substr(0, 2) === 's-';
- };
- RepoInfo.prototype.isCustomHost = function () {
- return (this._domain !== 'firebaseio.com' &&
- this._domain !== 'firebaseio-demo.com');
- };
- Object.defineProperty(RepoInfo.prototype, "host", {
- get: function () {
- return this._host;
- },
- set: function (newHost) {
- if (newHost !== this.internalHost) {
- this.internalHost = newHost;
- if (this.isCacheableHost()) {
- PersistentStorage.set('host:' + this._host, this.internalHost);
- }
- }
- },
- enumerable: false,
- configurable: true
- });
- RepoInfo.prototype.toString = function () {
- var str = this.toURLString();
- if (this.persistenceKey) {
- str += '<' + this.persistenceKey + '>';
- }
- return str;
- };
- RepoInfo.prototype.toURLString = function () {
- var protocol = this.secure ? 'https://' : 'http://';
- var query = this.includeNamespaceInQueryParams
- ? "?ns=".concat(this.namespace)
- : '';
- return "".concat(protocol).concat(this.host, "/").concat(query);
- };
- return RepoInfo;
- }());
- function repoInfoNeedsQueryParam(repoInfo) {
- return (repoInfo.host !== repoInfo.internalHost ||
- repoInfo.isCustomHost() ||
- repoInfo.includeNamespaceInQueryParams);
- }
- /**
- * Returns the websocket URL for this repo
- * @param repoInfo - RepoInfo object
- * @param type - of connection
- * @param params - list
- * @returns The URL for this repo
- */
- function repoInfoConnectionURL(repoInfo, type, params) {
- util.assert(typeof type === 'string', 'typeof type must == string');
- util.assert(typeof params === 'object', 'typeof params must == object');
- var connURL;
- if (type === WEBSOCKET) {
- connURL =
- (repoInfo.secure ? 'wss://' : 'ws://') + repoInfo.internalHost + '/.ws?';
- }
- else if (type === LONG_POLLING) {
- connURL =
- (repoInfo.secure ? 'https://' : 'http://') +
- repoInfo.internalHost +
- '/.lp?';
- }
- else {
- throw new Error('Unknown connection type: ' + type);
- }
- if (repoInfoNeedsQueryParam(repoInfo)) {
- params['ns'] = repoInfo.namespace;
- }
- var pairs = [];
- each(params, function (key, value) {
- pairs.push(key + '=' + value);
- });
- return connURL + pairs.join('&');
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Tracks a collection of stats.
- */
- var StatsCollection = /** @class */ (function () {
- function StatsCollection() {
- this.counters_ = {};
- }
- StatsCollection.prototype.incrementCounter = function (name, amount) {
- if (amount === void 0) { amount = 1; }
- if (!util.contains(this.counters_, name)) {
- this.counters_[name] = 0;
- }
- this.counters_[name] += amount;
- };
- StatsCollection.prototype.get = function () {
- return util.deepCopy(this.counters_);
- };
- return StatsCollection;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var collections = {};
- var reporters = {};
- function statsManagerGetCollection(repoInfo) {
- var hashString = repoInfo.toString();
- if (!collections[hashString]) {
- collections[hashString] = new StatsCollection();
- }
- return collections[hashString];
- }
- function statsManagerGetOrCreateReporter(repoInfo, creatorFunction) {
- var hashString = repoInfo.toString();
- if (!reporters[hashString]) {
- reporters[hashString] = creatorFunction();
- }
- return reporters[hashString];
- }
-
- /**
- * @license
- * Copyright 2019 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /** The semver (www.semver.org) version of the SDK. */
- var SDK_VERSION = '';
- /**
- * SDK_VERSION should be set before any database instance is created
- * @internal
- */
- function setSDKVersion(version) {
- SDK_VERSION = version;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var WEBSOCKET_MAX_FRAME_SIZE = 16384;
- var WEBSOCKET_KEEPALIVE_INTERVAL = 45000;
- var WebSocketImpl = null;
- if (typeof MozWebSocket !== 'undefined') {
- WebSocketImpl = MozWebSocket;
- }
- else if (typeof WebSocket !== 'undefined') {
- WebSocketImpl = WebSocket;
- }
- function setWebSocketImpl(impl) {
- WebSocketImpl = impl;
- }
- /**
- * Create a new websocket connection with the given callbacks.
- */
- var WebSocketConnection = /** @class */ (function () {
- /**
- * @param connId identifier for this transport
- * @param repoInfo The info for the websocket endpoint.
- * @param applicationId The Firebase App ID for this project.
- * @param appCheckToken The App Check Token for this client.
- * @param authToken The Auth Token for this client.
- * @param transportSessionId Optional transportSessionId if this is connecting
- * to an existing transport session
- * @param lastSessionId Optional lastSessionId if there was a previous
- * connection
- */
- function WebSocketConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
- this.connId = connId;
- this.applicationId = applicationId;
- this.appCheckToken = appCheckToken;
- this.authToken = authToken;
- this.keepaliveTimer = null;
- this.frames = null;
- this.totalFrames = 0;
- this.bytesSent = 0;
- this.bytesReceived = 0;
- this.log_ = logWrapper(this.connId);
- this.stats_ = statsManagerGetCollection(repoInfo);
- this.connURL = WebSocketConnection.connectionURL_(repoInfo, transportSessionId, lastSessionId, appCheckToken, applicationId);
- this.nodeAdmin = repoInfo.nodeAdmin;
- }
- /**
- * @param repoInfo - The info for the websocket endpoint.
- * @param transportSessionId - Optional transportSessionId if this is connecting to an existing transport
- * session
- * @param lastSessionId - Optional lastSessionId if there was a previous connection
- * @returns connection url
- */
- WebSocketConnection.connectionURL_ = function (repoInfo, transportSessionId, lastSessionId, appCheckToken, applicationId) {
- var urlParams = {};
- urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
- if (!util.isNodeSdk() &&
- typeof location !== 'undefined' &&
- location.hostname &&
- FORGE_DOMAIN_RE.test(location.hostname)) {
- urlParams[REFERER_PARAM] = FORGE_REF;
- }
- if (transportSessionId) {
- urlParams[TRANSPORT_SESSION_PARAM] = transportSessionId;
- }
- if (lastSessionId) {
- urlParams[LAST_SESSION_PARAM] = lastSessionId;
- }
- if (appCheckToken) {
- urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
- }
- if (applicationId) {
- urlParams[APPLICATION_ID_PARAM] = applicationId;
- }
- return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
- };
- /**
- * @param onMessage - Callback when messages arrive
- * @param onDisconnect - Callback with connection lost.
- */
- WebSocketConnection.prototype.open = function (onMessage, onDisconnect) {
- var _this = this;
- this.onDisconnect = onDisconnect;
- this.onMessage = onMessage;
- this.log_('Websocket connecting to ' + this.connURL);
- this.everConnected_ = false;
- // Assume failure until proven otherwise.
- PersistentStorage.set('previous_websocket_failure', true);
- try {
- var options = void 0;
- if (util.isNodeSdk()) {
- var device = this.nodeAdmin ? 'AdminNode' : 'Node';
- // UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
- options = {
- headers: {
- 'User-Agent': "Firebase/".concat(PROTOCOL_VERSION, "/").concat(SDK_VERSION, "/").concat(process.platform, "/").concat(device),
- 'X-Firebase-GMPID': this.applicationId || ''
- }
- };
- // If using Node with admin creds, AppCheck-related checks are unnecessary.
- // Note that we send the credentials here even if they aren't admin credentials, which is
- // not a problem.
- // Note that this header is just used to bypass appcheck, and the token should still be sent
- // through the websocket connection once it is established.
- if (this.authToken) {
- options.headers['Authorization'] = "Bearer ".concat(this.authToken);
- }
- if (this.appCheckToken) {
- options.headers['X-Firebase-AppCheck'] = this.appCheckToken;
- }
- // Plumb appropriate http_proxy environment variable into faye-websocket if it exists.
- var env = process['env'];
- var proxy = this.connURL.indexOf('wss://') === 0
- ? env['HTTPS_PROXY'] || env['https_proxy']
- : env['HTTP_PROXY'] || env['http_proxy'];
- if (proxy) {
- options['proxy'] = { origin: proxy };
- }
- }
- this.mySock = new WebSocketImpl(this.connURL, [], options);
- }
- catch (e) {
- this.log_('Error instantiating WebSocket.');
- var error = e.message || e.data;
- if (error) {
- this.log_(error);
- }
- this.onClosed_();
- return;
- }
- this.mySock.onopen = function () {
- _this.log_('Websocket connected.');
- _this.everConnected_ = true;
- };
- this.mySock.onclose = function () {
- _this.log_('Websocket connection was disconnected.');
- _this.mySock = null;
- _this.onClosed_();
- };
- this.mySock.onmessage = function (m) {
- _this.handleIncomingFrame(m);
- };
- this.mySock.onerror = function (e) {
- _this.log_('WebSocket error. Closing connection.');
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- var error = e.message || e.data;
- if (error) {
- _this.log_(error);
- }
- _this.onClosed_();
- };
- };
- /**
- * No-op for websockets, we don't need to do anything once the connection is confirmed as open
- */
- WebSocketConnection.prototype.start = function () { };
- WebSocketConnection.forceDisallow = function () {
- WebSocketConnection.forceDisallow_ = true;
- };
- WebSocketConnection.isAvailable = function () {
- var isOldAndroid = false;
- if (typeof navigator !== 'undefined' && navigator.userAgent) {
- var oldAndroidRegex = /Android ([0-9]{0,}\.[0-9]{0,})/;
- var oldAndroidMatch = navigator.userAgent.match(oldAndroidRegex);
- if (oldAndroidMatch && oldAndroidMatch.length > 1) {
- if (parseFloat(oldAndroidMatch[1]) < 4.4) {
- isOldAndroid = true;
- }
- }
- }
- return (!isOldAndroid &&
- WebSocketImpl !== null &&
- !WebSocketConnection.forceDisallow_);
- };
- /**
- * Returns true if we previously failed to connect with this transport.
- */
- WebSocketConnection.previouslyFailed = function () {
- // If our persistent storage is actually only in-memory storage,
- // we default to assuming that it previously failed to be safe.
- return (PersistentStorage.isInMemoryStorage ||
- PersistentStorage.get('previous_websocket_failure') === true);
- };
- WebSocketConnection.prototype.markConnectionHealthy = function () {
- PersistentStorage.remove('previous_websocket_failure');
- };
- WebSocketConnection.prototype.appendFrame_ = function (data) {
- this.frames.push(data);
- if (this.frames.length === this.totalFrames) {
- var fullMess = this.frames.join('');
- this.frames = null;
- var jsonMess = util.jsonEval(fullMess);
- //handle the message
- this.onMessage(jsonMess);
- }
- };
- /**
- * @param frameCount - The number of frames we are expecting from the server
- */
- WebSocketConnection.prototype.handleNewFrameCount_ = function (frameCount) {
- this.totalFrames = frameCount;
- this.frames = [];
- };
- /**
- * Attempts to parse a frame count out of some text. If it can't, assumes a value of 1
- * @returns Any remaining data to be process, or null if there is none
- */
- WebSocketConnection.prototype.extractFrameCount_ = function (data) {
- util.assert(this.frames === null, 'We already have a frame buffer');
- // TODO: The server is only supposed to send up to 9999 frames (i.e. length <= 4), but that isn't being enforced
- // currently. So allowing larger frame counts (length <= 6). See https://app.asana.com/0/search/8688598998380/8237608042508
- if (data.length <= 6) {
- var frameCount = Number(data);
- if (!isNaN(frameCount)) {
- this.handleNewFrameCount_(frameCount);
- return null;
- }
- }
- this.handleNewFrameCount_(1);
- return data;
- };
- /**
- * Process a websocket frame that has arrived from the server.
- * @param mess - The frame data
- */
- WebSocketConnection.prototype.handleIncomingFrame = function (mess) {
- if (this.mySock === null) {
- return; // Chrome apparently delivers incoming packets even after we .close() the connection sometimes.
- }
- var data = mess['data'];
- this.bytesReceived += data.length;
- this.stats_.incrementCounter('bytes_received', data.length);
- this.resetKeepAlive();
- if (this.frames !== null) {
- // we're buffering
- this.appendFrame_(data);
- }
- else {
- // try to parse out a frame count, otherwise, assume 1 and process it
- var remainingData = this.extractFrameCount_(data);
- if (remainingData !== null) {
- this.appendFrame_(remainingData);
- }
- }
- };
- /**
- * Send a message to the server
- * @param data - The JSON object to transmit
- */
- WebSocketConnection.prototype.send = function (data) {
- this.resetKeepAlive();
- var dataStr = util.stringify(data);
- this.bytesSent += dataStr.length;
- this.stats_.incrementCounter('bytes_sent', dataStr.length);
- //We can only fit a certain amount in each websocket frame, so we need to split this request
- //up into multiple pieces if it doesn't fit in one request.
- var dataSegs = splitStringBySize(dataStr, WEBSOCKET_MAX_FRAME_SIZE);
- //Send the length header
- if (dataSegs.length > 1) {
- this.sendString_(String(dataSegs.length));
- }
- //Send the actual data in segments.
- for (var i = 0; i < dataSegs.length; i++) {
- this.sendString_(dataSegs[i]);
- }
- };
- WebSocketConnection.prototype.shutdown_ = function () {
- this.isClosed_ = true;
- if (this.keepaliveTimer) {
- clearInterval(this.keepaliveTimer);
- this.keepaliveTimer = null;
- }
- if (this.mySock) {
- this.mySock.close();
- this.mySock = null;
- }
- };
- WebSocketConnection.prototype.onClosed_ = function () {
- if (!this.isClosed_) {
- this.log_('WebSocket is closing itself');
- this.shutdown_();
- // since this is an internal close, trigger the close listener
- if (this.onDisconnect) {
- this.onDisconnect(this.everConnected_);
- this.onDisconnect = null;
- }
- }
- };
- /**
- * External-facing close handler.
- * Close the websocket and kill the connection.
- */
- WebSocketConnection.prototype.close = function () {
- if (!this.isClosed_) {
- this.log_('WebSocket is being closed');
- this.shutdown_();
- }
- };
- /**
- * Kill the current keepalive timer and start a new one, to ensure that it always fires N seconds after
- * the last activity.
- */
- WebSocketConnection.prototype.resetKeepAlive = function () {
- var _this = this;
- clearInterval(this.keepaliveTimer);
- this.keepaliveTimer = setInterval(function () {
- //If there has been no websocket activity for a while, send a no-op
- if (_this.mySock) {
- _this.sendString_('0');
- }
- _this.resetKeepAlive();
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }, Math.floor(WEBSOCKET_KEEPALIVE_INTERVAL));
- };
- /**
- * Send a string over the websocket.
- *
- * @param str - String to send.
- */
- WebSocketConnection.prototype.sendString_ = function (str) {
- // Firefox seems to sometimes throw exceptions (NS_ERROR_UNEXPECTED) from websocket .send()
- // calls for some unknown reason. We treat these as an error and disconnect.
- // See https://app.asana.com/0/58926111402292/68021340250410
- try {
- this.mySock.send(str);
- }
- catch (e) {
- this.log_('Exception thrown from WebSocket.send():', e.message || e.data, 'Closing connection.');
- setTimeout(this.onClosed_.bind(this), 0);
- }
- };
- /**
- * Number of response before we consider the connection "healthy."
- */
- WebSocketConnection.responsesRequiredToBeHealthy = 2;
- /**
- * Time to wait for the connection te become healthy before giving up.
- */
- WebSocketConnection.healthyTimeout = 30000;
- return WebSocketConnection;
- }());
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Abstraction around AppCheck's token fetching capabilities.
- */
- var AppCheckTokenProvider = /** @class */ (function () {
- function AppCheckTokenProvider(appName_, appCheckProvider) {
- var _this = this;
- this.appName_ = appName_;
- this.appCheckProvider = appCheckProvider;
- this.appCheck = appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.getImmediate({ optional: true });
- if (!this.appCheck) {
- appCheckProvider === null || appCheckProvider === void 0 ? void 0 : appCheckProvider.get().then(function (appCheck) { return (_this.appCheck = appCheck); });
- }
- }
- AppCheckTokenProvider.prototype.getToken = function (forceRefresh) {
- var _this = this;
- if (!this.appCheck) {
- return new Promise(function (resolve, reject) {
- // Support delayed initialization of FirebaseAppCheck. This allows our
- // customers to initialize the RTDB SDK before initializing Firebase
- // AppCheck and ensures that all requests are authenticated if a token
- // becomes available before the timoeout below expires.
- setTimeout(function () {
- if (_this.appCheck) {
- _this.getToken(forceRefresh).then(resolve, reject);
- }
- else {
- resolve(null);
- }
- }, 0);
- });
- }
- return this.appCheck.getToken(forceRefresh);
- };
- AppCheckTokenProvider.prototype.addTokenChangeListener = function (listener) {
- var _a;
- (_a = this.appCheckProvider) === null || _a === void 0 ? void 0 : _a.get().then(function (appCheck) { return appCheck.addTokenListener(listener); });
- };
- AppCheckTokenProvider.prototype.notifyForInvalidToken = function () {
- warn$1("Provided AppCheck credentials for the app named \"".concat(this.appName_, "\" ") +
- 'are invalid. This usually indicates your app was not initialized correctly.');
- };
- return AppCheckTokenProvider;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Abstraction around FirebaseApp's token fetching capabilities.
- */
- var FirebaseAuthTokenProvider = /** @class */ (function () {
- function FirebaseAuthTokenProvider(appName_, firebaseOptions_, authProvider_) {
- var _this = this;
- this.appName_ = appName_;
- this.firebaseOptions_ = firebaseOptions_;
- this.authProvider_ = authProvider_;
- this.auth_ = null;
- this.auth_ = authProvider_.getImmediate({ optional: true });
- if (!this.auth_) {
- authProvider_.onInit(function (auth) { return (_this.auth_ = auth); });
- }
- }
- FirebaseAuthTokenProvider.prototype.getToken = function (forceRefresh) {
- var _this = this;
- if (!this.auth_) {
- return new Promise(function (resolve, reject) {
- // Support delayed initialization of FirebaseAuth. This allows our
- // customers to initialize the RTDB SDK before initializing Firebase
- // Auth and ensures that all requests are authenticated if a token
- // becomes available before the timoeout below expires.
- setTimeout(function () {
- if (_this.auth_) {
- _this.getToken(forceRefresh).then(resolve, reject);
- }
- else {
- resolve(null);
- }
- }, 0);
- });
- }
- return this.auth_.getToken(forceRefresh).catch(function (error) {
- // TODO: Need to figure out all the cases this is raised and whether
- // this makes sense.
- if (error && error.code === 'auth/token-not-initialized') {
- log('Got auth/token-not-initialized error. Treating as null token.');
- return null;
- }
- else {
- return Promise.reject(error);
- }
- });
- };
- FirebaseAuthTokenProvider.prototype.addTokenChangeListener = function (listener) {
- // TODO: We might want to wrap the listener and call it with no args to
- // avoid a leaky abstraction, but that makes removing the listener harder.
- if (this.auth_) {
- this.auth_.addAuthTokenListener(listener);
- }
- else {
- this.authProvider_
- .get()
- .then(function (auth) { return auth.addAuthTokenListener(listener); });
- }
- };
- FirebaseAuthTokenProvider.prototype.removeTokenChangeListener = function (listener) {
- this.authProvider_
- .get()
- .then(function (auth) { return auth.removeAuthTokenListener(listener); });
- };
- FirebaseAuthTokenProvider.prototype.notifyForInvalidToken = function () {
- var errorMessage = 'Provided authentication credentials for the app named "' +
- this.appName_ +
- '" are invalid. This usually indicates your app was not ' +
- 'initialized correctly. ';
- if ('credential' in this.firebaseOptions_) {
- errorMessage +=
- 'Make sure the "credential" property provided to initializeApp() ' +
- 'is authorized to access the specified "databaseURL" and is from the correct ' +
- 'project.';
- }
- else if ('serviceAccount' in this.firebaseOptions_) {
- errorMessage +=
- 'Make sure the "serviceAccount" property provided to initializeApp() ' +
- 'is authorized to access the specified "databaseURL" and is from the correct ' +
- 'project.';
- }
- else {
- errorMessage +=
- 'Make sure the "apiKey" and "databaseURL" properties provided to ' +
- 'initializeApp() match the values provided for your app at ' +
- 'https://console.firebase.google.com/.';
- }
- warn$1(errorMessage);
- };
- return FirebaseAuthTokenProvider;
- }());
- /* AuthTokenProvider that supplies a constant token. Used by Admin SDK or mockUserToken with emulators. */
- var EmulatorTokenProvider = /** @class */ (function () {
- function EmulatorTokenProvider(accessToken) {
- this.accessToken = accessToken;
- }
- EmulatorTokenProvider.prototype.getToken = function (forceRefresh) {
- return Promise.resolve({
- accessToken: this.accessToken
- });
- };
- EmulatorTokenProvider.prototype.addTokenChangeListener = function (listener) {
- // Invoke the listener immediately to match the behavior in Firebase Auth
- // (see packages/auth/src/auth.js#L1807)
- listener(this.accessToken);
- };
- EmulatorTokenProvider.prototype.removeTokenChangeListener = function (listener) { };
- EmulatorTokenProvider.prototype.notifyForInvalidToken = function () { };
- /** A string that is treated as an admin access token by the RTDB emulator. Used by Admin SDK. */
- EmulatorTokenProvider.OWNER = 'owner';
- return EmulatorTokenProvider;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * This class ensures the packets from the server arrive in order
- * This class takes data from the server and ensures it gets passed into the callbacks in order.
- */
- var PacketReceiver = /** @class */ (function () {
- /**
- * @param onMessage_
- */
- function PacketReceiver(onMessage_) {
- this.onMessage_ = onMessage_;
- this.pendingResponses = [];
- this.currentResponseNum = 0;
- this.closeAfterResponse = -1;
- this.onClose = null;
- }
- PacketReceiver.prototype.closeAfter = function (responseNum, callback) {
- this.closeAfterResponse = responseNum;
- this.onClose = callback;
- if (this.closeAfterResponse < this.currentResponseNum) {
- this.onClose();
- this.onClose = null;
- }
- };
- /**
- * Each message from the server comes with a response number, and an array of data. The responseNumber
- * allows us to ensure that we process them in the right order, since we can't be guaranteed that all
- * browsers will respond in the same order as the requests we sent
- */
- PacketReceiver.prototype.handleResponse = function (requestNum, data) {
- var _this = this;
- this.pendingResponses[requestNum] = data;
- var _loop_1 = function () {
- var toProcess = this_1.pendingResponses[this_1.currentResponseNum];
- delete this_1.pendingResponses[this_1.currentResponseNum];
- var _loop_2 = function (i) {
- if (toProcess[i]) {
- exceptionGuard(function () {
- _this.onMessage_(toProcess[i]);
- });
- }
- };
- for (var i = 0; i < toProcess.length; ++i) {
- _loop_2(i);
- }
- if (this_1.currentResponseNum === this_1.closeAfterResponse) {
- if (this_1.onClose) {
- this_1.onClose();
- this_1.onClose = null;
- }
- return "break";
- }
- this_1.currentResponseNum++;
- };
- var this_1 = this;
- while (this.pendingResponses[this.currentResponseNum]) {
- var state_1 = _loop_1();
- if (state_1 === "break")
- break;
- }
- };
- return PacketReceiver;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // URL query parameters associated with longpolling
- var FIREBASE_LONGPOLL_START_PARAM = 'start';
- var FIREBASE_LONGPOLL_CLOSE_COMMAND = 'close';
- var FIREBASE_LONGPOLL_COMMAND_CB_NAME = 'pLPCommand';
- var FIREBASE_LONGPOLL_DATA_CB_NAME = 'pRTLPCB';
- var FIREBASE_LONGPOLL_ID_PARAM = 'id';
- var FIREBASE_LONGPOLL_PW_PARAM = 'pw';
- var FIREBASE_LONGPOLL_SERIAL_PARAM = 'ser';
- var FIREBASE_LONGPOLL_CALLBACK_ID_PARAM = 'cb';
- var FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM = 'seg';
- var FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET = 'ts';
- var FIREBASE_LONGPOLL_DATA_PARAM = 'd';
- var FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM = 'dframe';
- //Data size constants.
- //TODO: Perf: the maximum length actually differs from browser to browser.
- // We should check what browser we're on and set accordingly.
- var MAX_URL_DATA_SIZE = 1870;
- var SEG_HEADER_SIZE = 30; //ie: &seg=8299234&ts=982389123&d=
- var MAX_PAYLOAD_SIZE = MAX_URL_DATA_SIZE - SEG_HEADER_SIZE;
- /**
- * Keepalive period
- * send a fresh request at minimum every 25 seconds. Opera has a maximum request
- * length of 30 seconds that we can't exceed.
- */
- var KEEPALIVE_REQUEST_INTERVAL = 25000;
- /**
- * How long to wait before aborting a long-polling connection attempt.
- */
- var LP_CONNECT_TIMEOUT = 30000;
- /**
- * This class manages a single long-polling connection.
- */
- var BrowserPollConnection = /** @class */ (function () {
- /**
- * @param connId An identifier for this connection, used for logging
- * @param repoInfo The info for the endpoint to send data to.
- * @param applicationId The Firebase App ID for this project.
- * @param appCheckToken The AppCheck token for this client.
- * @param authToken The AuthToken to use for this connection.
- * @param transportSessionId Optional transportSessionid if we are
- * reconnecting for an existing transport session
- * @param lastSessionId Optional lastSessionId if the PersistentConnection has
- * already created a connection previously
- */
- function BrowserPollConnection(connId, repoInfo, applicationId, appCheckToken, authToken, transportSessionId, lastSessionId) {
- var _this = this;
- this.connId = connId;
- this.repoInfo = repoInfo;
- this.applicationId = applicationId;
- this.appCheckToken = appCheckToken;
- this.authToken = authToken;
- this.transportSessionId = transportSessionId;
- this.lastSessionId = lastSessionId;
- this.bytesSent = 0;
- this.bytesReceived = 0;
- this.everConnected_ = false;
- this.log_ = logWrapper(connId);
- this.stats_ = statsManagerGetCollection(repoInfo);
- this.urlFn = function (params) {
- // Always add the token if we have one.
- if (_this.appCheckToken) {
- params[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
- }
- return repoInfoConnectionURL(repoInfo, LONG_POLLING, params);
- };
- }
- /**
- * @param onMessage - Callback when messages arrive
- * @param onDisconnect - Callback with connection lost.
- */
- BrowserPollConnection.prototype.open = function (onMessage, onDisconnect) {
- var _this = this;
- this.curSegmentNum = 0;
- this.onDisconnect_ = onDisconnect;
- this.myPacketOrderer = new PacketReceiver(onMessage);
- this.isClosed_ = false;
- this.connectTimeoutTimer_ = setTimeout(function () {
- _this.log_('Timed out trying to connect.');
- // Make sure we clear the host cache
- _this.onClosed_();
- _this.connectTimeoutTimer_ = null;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }, Math.floor(LP_CONNECT_TIMEOUT));
- // Ensure we delay the creation of the iframe until the DOM is loaded.
- executeWhenDOMReady(function () {
- if (_this.isClosed_) {
- return;
- }
- //Set up a callback that gets triggered once a connection is set up.
- _this.scriptTagHolder = new FirebaseIFrameScriptHolder(function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- var _a = tslib.__read(args, 5), command = _a[0], arg1 = _a[1], arg2 = _a[2]; _a[3]; _a[4];
- _this.incrementIncomingBytes_(args);
- if (!_this.scriptTagHolder) {
- return; // we closed the connection.
- }
- if (_this.connectTimeoutTimer_) {
- clearTimeout(_this.connectTimeoutTimer_);
- _this.connectTimeoutTimer_ = null;
- }
- _this.everConnected_ = true;
- if (command === FIREBASE_LONGPOLL_START_PARAM) {
- _this.id = arg1;
- _this.password = arg2;
- }
- else if (command === FIREBASE_LONGPOLL_CLOSE_COMMAND) {
- // Don't clear the host cache. We got a response from the server, so we know it's reachable
- if (arg1) {
- // We aren't expecting any more data (other than what the server's already in the process of sending us
- // through our already open polls), so don't send any more.
- _this.scriptTagHolder.sendNewPolls = false;
- // arg1 in this case is the last response number sent by the server. We should try to receive
- // all of the responses up to this one before closing
- _this.myPacketOrderer.closeAfter(arg1, function () {
- _this.onClosed_();
- });
- }
- else {
- _this.onClosed_();
- }
- }
- else {
- throw new Error('Unrecognized command received: ' + command);
- }
- }, function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- var _a = tslib.__read(args, 2), pN = _a[0], data = _a[1];
- _this.incrementIncomingBytes_(args);
- _this.myPacketOrderer.handleResponse(pN, data);
- }, function () {
- _this.onClosed_();
- }, _this.urlFn);
- //Send the initial request to connect. The serial number is simply to keep the browser from pulling previous results
- //from cache.
- var urlParams = {};
- urlParams[FIREBASE_LONGPOLL_START_PARAM] = 't';
- urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = Math.floor(Math.random() * 100000000);
- if (_this.scriptTagHolder.uniqueCallbackIdentifier) {
- urlParams[FIREBASE_LONGPOLL_CALLBACK_ID_PARAM] =
- _this.scriptTagHolder.uniqueCallbackIdentifier;
- }
- urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
- if (_this.transportSessionId) {
- urlParams[TRANSPORT_SESSION_PARAM] = _this.transportSessionId;
- }
- if (_this.lastSessionId) {
- urlParams[LAST_SESSION_PARAM] = _this.lastSessionId;
- }
- if (_this.applicationId) {
- urlParams[APPLICATION_ID_PARAM] = _this.applicationId;
- }
- if (_this.appCheckToken) {
- urlParams[APP_CHECK_TOKEN_PARAM] = _this.appCheckToken;
- }
- if (typeof location !== 'undefined' &&
- location.hostname &&
- FORGE_DOMAIN_RE.test(location.hostname)) {
- urlParams[REFERER_PARAM] = FORGE_REF;
- }
- var connectURL = _this.urlFn(urlParams);
- _this.log_('Connecting via long-poll to ' + connectURL);
- _this.scriptTagHolder.addTag(connectURL, function () {
- /* do nothing */
- });
- });
- };
- /**
- * Call this when a handshake has completed successfully and we want to consider the connection established
- */
- BrowserPollConnection.prototype.start = function () {
- this.scriptTagHolder.startLongPoll(this.id, this.password);
- this.addDisconnectPingFrame(this.id, this.password);
- };
- /**
- * Forces long polling to be considered as a potential transport
- */
- BrowserPollConnection.forceAllow = function () {
- BrowserPollConnection.forceAllow_ = true;
- };
- /**
- * Forces longpolling to not be considered as a potential transport
- */
- BrowserPollConnection.forceDisallow = function () {
- BrowserPollConnection.forceDisallow_ = true;
- };
- // Static method, use string literal so it can be accessed in a generic way
- BrowserPollConnection.isAvailable = function () {
- if (util.isNodeSdk()) {
- return false;
- }
- else if (BrowserPollConnection.forceAllow_) {
- return true;
- }
- else {
- // NOTE: In React-Native there's normally no 'document', but if you debug a React-Native app in
- // the Chrome debugger, 'document' is defined, but document.createElement is null (2015/06/08).
- return (!BrowserPollConnection.forceDisallow_ &&
- typeof document !== 'undefined' &&
- document.createElement != null &&
- !isChromeExtensionContentScript() &&
- !isWindowsStoreApp());
- }
- };
- /**
- * No-op for polling
- */
- BrowserPollConnection.prototype.markConnectionHealthy = function () { };
- /**
- * Stops polling and cleans up the iframe
- */
- BrowserPollConnection.prototype.shutdown_ = function () {
- this.isClosed_ = true;
- if (this.scriptTagHolder) {
- this.scriptTagHolder.close();
- this.scriptTagHolder = null;
- }
- //remove the disconnect frame, which will trigger an XHR call to the server to tell it we're leaving.
- if (this.myDisconnFrame) {
- document.body.removeChild(this.myDisconnFrame);
- this.myDisconnFrame = null;
- }
- if (this.connectTimeoutTimer_) {
- clearTimeout(this.connectTimeoutTimer_);
- this.connectTimeoutTimer_ = null;
- }
- };
- /**
- * Triggered when this transport is closed
- */
- BrowserPollConnection.prototype.onClosed_ = function () {
- if (!this.isClosed_) {
- this.log_('Longpoll is closing itself');
- this.shutdown_();
- if (this.onDisconnect_) {
- this.onDisconnect_(this.everConnected_);
- this.onDisconnect_ = null;
- }
- }
- };
- /**
- * External-facing close handler. RealTime has requested we shut down. Kill our connection and tell the server
- * that we've left.
- */
- BrowserPollConnection.prototype.close = function () {
- if (!this.isClosed_) {
- this.log_('Longpoll is being closed.');
- this.shutdown_();
- }
- };
- /**
- * Send the JSON object down to the server. It will need to be stringified, base64 encoded, and then
- * broken into chunks (since URLs have a small maximum length).
- * @param data - The JSON data to transmit.
- */
- BrowserPollConnection.prototype.send = function (data) {
- var dataStr = util.stringify(data);
- this.bytesSent += dataStr.length;
- this.stats_.incrementCounter('bytes_sent', dataStr.length);
- //first, lets get the base64-encoded data
- var base64data = util.base64Encode(dataStr);
- //We can only fit a certain amount in each URL, so we need to split this request
- //up into multiple pieces if it doesn't fit in one request.
- var dataSegs = splitStringBySize(base64data, MAX_PAYLOAD_SIZE);
- //Enqueue each segment for transmission. We assign each chunk a sequential ID and a total number
- //of segments so that we can reassemble the packet on the server.
- for (var i = 0; i < dataSegs.length; i++) {
- this.scriptTagHolder.enqueueSegment(this.curSegmentNum, dataSegs.length, dataSegs[i]);
- this.curSegmentNum++;
- }
- };
- /**
- * This is how we notify the server that we're leaving.
- * We aren't able to send requests with DHTML on a window close event, but we can
- * trigger XHR requests in some browsers (everything but Opera basically).
- */
- BrowserPollConnection.prototype.addDisconnectPingFrame = function (id, pw) {
- if (util.isNodeSdk()) {
- return;
- }
- this.myDisconnFrame = document.createElement('iframe');
- var urlParams = {};
- urlParams[FIREBASE_LONGPOLL_DISCONN_FRAME_REQUEST_PARAM] = 't';
- urlParams[FIREBASE_LONGPOLL_ID_PARAM] = id;
- urlParams[FIREBASE_LONGPOLL_PW_PARAM] = pw;
- this.myDisconnFrame.src = this.urlFn(urlParams);
- this.myDisconnFrame.style.display = 'none';
- document.body.appendChild(this.myDisconnFrame);
- };
- /**
- * Used to track the bytes received by this client
- */
- BrowserPollConnection.prototype.incrementIncomingBytes_ = function (args) {
- // TODO: This is an annoying perf hit just to track the number of incoming bytes. Maybe it should be opt-in.
- var bytesReceived = util.stringify(args).length;
- this.bytesReceived += bytesReceived;
- this.stats_.incrementCounter('bytes_received', bytesReceived);
- };
- return BrowserPollConnection;
- }());
- /*********************************************************************************************
- * A wrapper around an iframe that is used as a long-polling script holder.
- *********************************************************************************************/
- var FirebaseIFrameScriptHolder = /** @class */ (function () {
- /**
- * @param commandCB - The callback to be called when control commands are recevied from the server.
- * @param onMessageCB - The callback to be triggered when responses arrive from the server.
- * @param onDisconnect - The callback to be triggered when this tag holder is closed
- * @param urlFn - A function that provides the URL of the endpoint to send data to.
- */
- function FirebaseIFrameScriptHolder(commandCB, onMessageCB, onDisconnect, urlFn) {
- this.onDisconnect = onDisconnect;
- this.urlFn = urlFn;
- //We maintain a count of all of the outstanding requests, because if we have too many active at once it can cause
- //problems in some browsers.
- this.outstandingRequests = new Set();
- //A queue of the pending segments waiting for transmission to the server.
- this.pendingSegs = [];
- //A serial number. We use this for two things:
- // 1) A way to ensure the browser doesn't cache responses to polls
- // 2) A way to make the server aware when long-polls arrive in a different order than we started them. The
- // server needs to release both polls in this case or it will cause problems in Opera since Opera can only execute
- // JSONP code in the order it was added to the iframe.
- this.currentSerial = Math.floor(Math.random() * 100000000);
- // This gets set to false when we're "closing down" the connection (e.g. we're switching transports but there's still
- // incoming data from the server that we're waiting for).
- this.sendNewPolls = true;
- if (!util.isNodeSdk()) {
- //Each script holder registers a couple of uniquely named callbacks with the window. These are called from the
- //iframes where we put the long-polling script tags. We have two callbacks:
- // 1) Command Callback - Triggered for control issues, like starting a connection.
- // 2) Message Callback - Triggered when new data arrives.
- this.uniqueCallbackIdentifier = LUIDGenerator();
- window[FIREBASE_LONGPOLL_COMMAND_CB_NAME + this.uniqueCallbackIdentifier] = commandCB;
- window[FIREBASE_LONGPOLL_DATA_CB_NAME + this.uniqueCallbackIdentifier] =
- onMessageCB;
- //Create an iframe for us to add script tags to.
- this.myIFrame = FirebaseIFrameScriptHolder.createIFrame_();
- // Set the iframe's contents.
- var script = '';
- // if we set a javascript url, it's IE and we need to set the document domain. The javascript url is sufficient
- // for ie9, but ie8 needs to do it again in the document itself.
- if (this.myIFrame.src &&
- this.myIFrame.src.substr(0, 'javascript:'.length) === 'javascript:') {
- var currentDomain = document.domain;
- script = '<script>document.domain="' + currentDomain + '";</script>';
- }
- var iframeContents = '<html><body>' + script + '</body></html>';
- try {
- this.myIFrame.doc.open();
- this.myIFrame.doc.write(iframeContents);
- this.myIFrame.doc.close();
- }
- catch (e) {
- log('frame writing exception');
- if (e.stack) {
- log(e.stack);
- }
- log(e);
- }
- }
- else {
- this.commandCB = commandCB;
- this.onMessageCB = onMessageCB;
- }
- }
- /**
- * Each browser has its own funny way to handle iframes. Here we mush them all together into one object that I can
- * actually use.
- */
- FirebaseIFrameScriptHolder.createIFrame_ = function () {
- var iframe = document.createElement('iframe');
- iframe.style.display = 'none';
- // This is necessary in order to initialize the document inside the iframe
- if (document.body) {
- document.body.appendChild(iframe);
- try {
- // If document.domain has been modified in IE, this will throw an error, and we need to set the
- // domain of the iframe's document manually. We can do this via a javascript: url as the src attribute
- // Also note that we must do this *after* the iframe has been appended to the page. Otherwise it doesn't work.
- var a = iframe.contentWindow.document;
- if (!a) {
- // Apologies for the log-spam, I need to do something to keep closure from optimizing out the assignment above.
- log('No IE domain setting required');
- }
- }
- catch (e) {
- var domain = document.domain;
- iframe.src =
- "javascript:void((function(){document.open();document.domain='" +
- domain +
- "';document.close();})())";
- }
- }
- else {
- // LongPollConnection attempts to delay initialization until the document is ready, so hopefully this
- // never gets hit.
- throw 'Document body has not initialized. Wait to initialize Firebase until after the document is ready.';
- }
- // Get the document of the iframe in a browser-specific way.
- if (iframe.contentDocument) {
- iframe.doc = iframe.contentDocument; // Firefox, Opera, Safari
- }
- else if (iframe.contentWindow) {
- iframe.doc = iframe.contentWindow.document; // Internet Explorer
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }
- else if (iframe.document) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- iframe.doc = iframe.document; //others?
- }
- return iframe;
- };
- /**
- * Cancel all outstanding queries and remove the frame.
- */
- FirebaseIFrameScriptHolder.prototype.close = function () {
- var _this = this;
- //Mark this iframe as dead, so no new requests are sent.
- this.alive = false;
- if (this.myIFrame) {
- //We have to actually remove all of the html inside this iframe before removing it from the
- //window, or IE will continue loading and executing the script tags we've already added, which
- //can lead to some errors being thrown. Setting textContent seems to be the safest way to do this.
- this.myIFrame.doc.body.textContent = '';
- setTimeout(function () {
- if (_this.myIFrame !== null) {
- document.body.removeChild(_this.myIFrame);
- _this.myIFrame = null;
- }
- }, Math.floor(0));
- }
- // Protect from being called recursively.
- var onDisconnect = this.onDisconnect;
- if (onDisconnect) {
- this.onDisconnect = null;
- onDisconnect();
- }
- };
- /**
- * Actually start the long-polling session by adding the first script tag(s) to the iframe.
- * @param id - The ID of this connection
- * @param pw - The password for this connection
- */
- FirebaseIFrameScriptHolder.prototype.startLongPoll = function (id, pw) {
- this.myID = id;
- this.myPW = pw;
- this.alive = true;
- //send the initial request. If there are requests queued, make sure that we transmit as many as we are currently able to.
- while (this.newRequest_()) { }
- };
- /**
- * This is called any time someone might want a script tag to be added. It adds a script tag when there aren't
- * too many outstanding requests and we are still alive.
- *
- * If there are outstanding packet segments to send, it sends one. If there aren't, it sends a long-poll anyways if
- * needed.
- */
- FirebaseIFrameScriptHolder.prototype.newRequest_ = function () {
- // We keep one outstanding request open all the time to receive data, but if we need to send data
- // (pendingSegs.length > 0) then we create a new request to send the data. The server will automatically
- // close the old request.
- if (this.alive &&
- this.sendNewPolls &&
- this.outstandingRequests.size < (this.pendingSegs.length > 0 ? 2 : 1)) {
- //construct our url
- this.currentSerial++;
- var urlParams = {};
- urlParams[FIREBASE_LONGPOLL_ID_PARAM] = this.myID;
- urlParams[FIREBASE_LONGPOLL_PW_PARAM] = this.myPW;
- urlParams[FIREBASE_LONGPOLL_SERIAL_PARAM] = this.currentSerial;
- var theURL = this.urlFn(urlParams);
- //Now add as much data as we can.
- var curDataString = '';
- var i = 0;
- while (this.pendingSegs.length > 0) {
- //first, lets see if the next segment will fit.
- var nextSeg = this.pendingSegs[0];
- if (nextSeg.d.length +
- SEG_HEADER_SIZE +
- curDataString.length <=
- MAX_URL_DATA_SIZE) {
- //great, the segment will fit. Lets append it.
- var theSeg = this.pendingSegs.shift();
- curDataString =
- curDataString +
- '&' +
- FIREBASE_LONGPOLL_SEGMENT_NUM_PARAM +
- i +
- '=' +
- theSeg.seg +
- '&' +
- FIREBASE_LONGPOLL_SEGMENTS_IN_PACKET +
- i +
- '=' +
- theSeg.ts +
- '&' +
- FIREBASE_LONGPOLL_DATA_PARAM +
- i +
- '=' +
- theSeg.d;
- i++;
- }
- else {
- break;
- }
- }
- theURL = theURL + curDataString;
- this.addLongPollTag_(theURL, this.currentSerial);
- return true;
- }
- else {
- return false;
- }
- };
- /**
- * Queue a packet for transmission to the server.
- * @param segnum - A sequential id for this packet segment used for reassembly
- * @param totalsegs - The total number of segments in this packet
- * @param data - The data for this segment.
- */
- FirebaseIFrameScriptHolder.prototype.enqueueSegment = function (segnum, totalsegs, data) {
- //add this to the queue of segments to send.
- this.pendingSegs.push({ seg: segnum, ts: totalsegs, d: data });
- //send the data immediately if there isn't already data being transmitted, unless
- //startLongPoll hasn't been called yet.
- if (this.alive) {
- this.newRequest_();
- }
- };
- /**
- * Add a script tag for a regular long-poll request.
- * @param url - The URL of the script tag.
- * @param serial - The serial number of the request.
- */
- FirebaseIFrameScriptHolder.prototype.addLongPollTag_ = function (url, serial) {
- var _this = this;
- //remember that we sent this request.
- this.outstandingRequests.add(serial);
- var doNewRequest = function () {
- _this.outstandingRequests.delete(serial);
- _this.newRequest_();
- };
- // If this request doesn't return on its own accord (by the server sending us some data), we'll
- // create a new one after the KEEPALIVE interval to make sure we always keep a fresh request open.
- var keepaliveTimeout = setTimeout(doNewRequest, Math.floor(KEEPALIVE_REQUEST_INTERVAL));
- var readyStateCB = function () {
- // Request completed. Cancel the keepalive.
- clearTimeout(keepaliveTimeout);
- // Trigger a new request so we can continue receiving data.
- doNewRequest();
- };
- this.addTag(url, readyStateCB);
- };
- /**
- * Add an arbitrary script tag to the iframe.
- * @param url - The URL for the script tag source.
- * @param loadCB - A callback to be triggered once the script has loaded.
- */
- FirebaseIFrameScriptHolder.prototype.addTag = function (url, loadCB) {
- var _this = this;
- if (util.isNodeSdk()) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- this.doNodeLongPoll(url, loadCB);
- }
- else {
- setTimeout(function () {
- try {
- // if we're already closed, don't add this poll
- if (!_this.sendNewPolls) {
- return;
- }
- var newScript_1 = _this.myIFrame.doc.createElement('script');
- newScript_1.type = 'text/javascript';
- newScript_1.async = true;
- newScript_1.src = url;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- newScript_1.onload = newScript_1.onreadystatechange =
- function () {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- var rstate = newScript_1.readyState;
- if (!rstate || rstate === 'loaded' || rstate === 'complete') {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- newScript_1.onload = newScript_1.onreadystatechange = null;
- if (newScript_1.parentNode) {
- newScript_1.parentNode.removeChild(newScript_1);
- }
- loadCB();
- }
- };
- newScript_1.onerror = function () {
- log('Long-poll script failed to load: ' + url);
- _this.sendNewPolls = false;
- _this.close();
- };
- _this.myIFrame.doc.body.appendChild(newScript_1);
- }
- catch (e) {
- // TODO: we should make this error visible somehow
- }
- }, Math.floor(1));
- }
- };
- return FirebaseIFrameScriptHolder;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Currently simplistic, this class manages what transport a Connection should use at various stages of its
- * lifecycle.
- *
- * It starts with longpolling in a browser, and httppolling on node. It then upgrades to websockets if
- * they are available.
- */
- var TransportManager = /** @class */ (function () {
- /**
- * @param repoInfo - Metadata around the namespace we're connecting to
- */
- function TransportManager(repoInfo) {
- this.initTransports_(repoInfo);
- }
- Object.defineProperty(TransportManager, "ALL_TRANSPORTS", {
- get: function () {
- return [BrowserPollConnection, WebSocketConnection];
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(TransportManager, "IS_TRANSPORT_INITIALIZED", {
- /**
- * Returns whether transport has been selected to ensure WebSocketConnection or BrowserPollConnection are not called after
- * TransportManager has already set up transports_
- */
- get: function () {
- return this.globalTransportInitialized_;
- },
- enumerable: false,
- configurable: true
- });
- TransportManager.prototype.initTransports_ = function (repoInfo) {
- var e_1, _a;
- var isWebSocketsAvailable = WebSocketConnection && WebSocketConnection['isAvailable']();
- var isSkipPollConnection = isWebSocketsAvailable && !WebSocketConnection.previouslyFailed();
- if (repoInfo.webSocketOnly) {
- if (!isWebSocketsAvailable) {
- warn$1("wss:// URL used, but browser isn't known to support websockets. Trying anyway.");
- }
- isSkipPollConnection = true;
- }
- if (isSkipPollConnection) {
- this.transports_ = [WebSocketConnection];
- }
- else {
- var transports = (this.transports_ = []);
- try {
- for (var _b = tslib.__values(TransportManager.ALL_TRANSPORTS), _c = _b.next(); !_c.done; _c = _b.next()) {
- var transport = _c.value;
- if (transport && transport['isAvailable']()) {
- transports.push(transport);
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- TransportManager.globalTransportInitialized_ = true;
- }
- };
- /**
- * @returns The constructor for the initial transport to use
- */
- TransportManager.prototype.initialTransport = function () {
- if (this.transports_.length > 0) {
- return this.transports_[0];
- }
- else {
- throw new Error('No transports available');
- }
- };
- /**
- * @returns The constructor for the next transport, or null
- */
- TransportManager.prototype.upgradeTransport = function () {
- if (this.transports_.length > 1) {
- return this.transports_[1];
- }
- else {
- return null;
- }
- };
- // Keeps track of whether the TransportManager has already chosen a transport to use
- TransportManager.globalTransportInitialized_ = false;
- return TransportManager;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // Abort upgrade attempt if it takes longer than 60s.
- var UPGRADE_TIMEOUT = 60000;
- // For some transports (WebSockets), we need to "validate" the transport by exchanging a few requests and responses.
- // If we haven't sent enough requests within 5s, we'll start sending noop ping requests.
- var DELAY_BEFORE_SENDING_EXTRA_REQUESTS = 5000;
- // 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)
- // then we may not be able to exchange our ping/pong requests within the healthy timeout. So if we reach the timeout
- // but we've sent/received enough bytes, we don't cancel the connection.
- var BYTES_SENT_HEALTHY_OVERRIDE = 10 * 1024;
- var BYTES_RECEIVED_HEALTHY_OVERRIDE = 100 * 1024;
- var MESSAGE_TYPE = 't';
- var MESSAGE_DATA = 'd';
- var CONTROL_SHUTDOWN = 's';
- var CONTROL_RESET = 'r';
- var CONTROL_ERROR = 'e';
- var CONTROL_PONG = 'o';
- var SWITCH_ACK = 'a';
- var END_TRANSMISSION = 'n';
- var PING = 'p';
- var SERVER_HELLO = 'h';
- /**
- * Creates a new real-time connection to the server using whichever method works
- * best in the current browser.
- */
- var Connection = /** @class */ (function () {
- /**
- * @param id - an id for this connection
- * @param repoInfo_ - the info for the endpoint to connect to
- * @param applicationId_ - the Firebase App ID for this project
- * @param appCheckToken_ - The App Check Token for this device.
- * @param authToken_ - The auth token for this session.
- * @param onMessage_ - the callback to be triggered when a server-push message arrives
- * @param onReady_ - the callback to be triggered when this connection is ready to send messages.
- * @param onDisconnect_ - the callback to be triggered when a connection was lost
- * @param onKill_ - the callback to be triggered when this connection has permanently shut down.
- * @param lastSessionId - last session id in persistent connection. is used to clean up old session in real-time server
- */
- function Connection(id, repoInfo_, applicationId_, appCheckToken_, authToken_, onMessage_, onReady_, onDisconnect_, onKill_, lastSessionId) {
- this.id = id;
- this.repoInfo_ = repoInfo_;
- this.applicationId_ = applicationId_;
- this.appCheckToken_ = appCheckToken_;
- this.authToken_ = authToken_;
- this.onMessage_ = onMessage_;
- this.onReady_ = onReady_;
- this.onDisconnect_ = onDisconnect_;
- this.onKill_ = onKill_;
- this.lastSessionId = lastSessionId;
- this.connectionCount = 0;
- this.pendingDataMessages = [];
- this.state_ = 0 /* RealtimeState.CONNECTING */;
- this.log_ = logWrapper('c:' + this.id + ':');
- this.transportManager_ = new TransportManager(repoInfo_);
- this.log_('Connection created');
- this.start_();
- }
- /**
- * Starts a connection attempt
- */
- Connection.prototype.start_ = function () {
- var _this = this;
- var conn = this.transportManager_.initialTransport();
- this.conn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, null, this.lastSessionId);
- // For certain transports (WebSockets), we need to send and receive several messages back and forth before we
- // can consider the transport healthy.
- this.primaryResponsesRequired_ = conn['responsesRequiredToBeHealthy'] || 0;
- var onMessageReceived = this.connReceiver_(this.conn_);
- var onConnectionLost = this.disconnReceiver_(this.conn_);
- this.tx_ = this.conn_;
- this.rx_ = this.conn_;
- this.secondaryConn_ = null;
- this.isHealthy_ = false;
- /*
- * Firefox doesn't like when code from one iframe tries to create another iframe by way of the parent frame.
- * This can occur in the case of a redirect, i.e. we guessed wrong on what server to connect to and received a reset.
- * Somehow, setTimeout seems to make this ok. That doesn't make sense from a security perspective, since you should
- * still have the context of your originating frame.
- */
- setTimeout(function () {
- // this.conn_ gets set to null in some of the tests. Check to make sure it still exists before using it
- _this.conn_ && _this.conn_.open(onMessageReceived, onConnectionLost);
- }, Math.floor(0));
- var healthyTimeoutMS = conn['healthyTimeout'] || 0;
- if (healthyTimeoutMS > 0) {
- this.healthyTimeout_ = setTimeoutNonBlocking(function () {
- _this.healthyTimeout_ = null;
- if (!_this.isHealthy_) {
- if (_this.conn_ &&
- _this.conn_.bytesReceived > BYTES_RECEIVED_HEALTHY_OVERRIDE) {
- _this.log_('Connection exceeded healthy timeout but has received ' +
- _this.conn_.bytesReceived +
- ' bytes. Marking connection healthy.');
- _this.isHealthy_ = true;
- _this.conn_.markConnectionHealthy();
- }
- else if (_this.conn_ &&
- _this.conn_.bytesSent > BYTES_SENT_HEALTHY_OVERRIDE) {
- _this.log_('Connection exceeded healthy timeout but has sent ' +
- _this.conn_.bytesSent +
- ' bytes. Leaving connection alive.');
- // NOTE: We don't want to mark it healthy, since we have no guarantee that the bytes have made it to
- // the server.
- }
- else {
- _this.log_('Closing unhealthy connection after timeout.');
- _this.close();
- }
- }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }, Math.floor(healthyTimeoutMS));
- }
- };
- Connection.prototype.nextTransportId_ = function () {
- return 'c:' + this.id + ':' + this.connectionCount++;
- };
- Connection.prototype.disconnReceiver_ = function (conn) {
- var _this = this;
- return function (everConnected) {
- if (conn === _this.conn_) {
- _this.onConnectionLost_(everConnected);
- }
- else if (conn === _this.secondaryConn_) {
- _this.log_('Secondary connection lost.');
- _this.onSecondaryConnectionLost_();
- }
- else {
- _this.log_('closing an old connection');
- }
- };
- };
- Connection.prototype.connReceiver_ = function (conn) {
- var _this = this;
- return function (message) {
- if (_this.state_ !== 2 /* RealtimeState.DISCONNECTED */) {
- if (conn === _this.rx_) {
- _this.onPrimaryMessageReceived_(message);
- }
- else if (conn === _this.secondaryConn_) {
- _this.onSecondaryMessageReceived_(message);
- }
- else {
- _this.log_('message on old connection');
- }
- }
- };
- };
- /**
- * @param dataMsg - An arbitrary data message to be sent to the server
- */
- Connection.prototype.sendRequest = function (dataMsg) {
- // wrap in a data message envelope and send it on
- var msg = { t: 'd', d: dataMsg };
- this.sendData_(msg);
- };
- Connection.prototype.tryCleanupConnection = function () {
- if (this.tx_ === this.secondaryConn_ && this.rx_ === this.secondaryConn_) {
- this.log_('cleaning up and promoting a connection: ' + this.secondaryConn_.connId);
- this.conn_ = this.secondaryConn_;
- this.secondaryConn_ = null;
- // the server will shutdown the old connection
- }
- };
- Connection.prototype.onSecondaryControl_ = function (controlData) {
- if (MESSAGE_TYPE in controlData) {
- var cmd = controlData[MESSAGE_TYPE];
- if (cmd === SWITCH_ACK) {
- this.upgradeIfSecondaryHealthy_();
- }
- else if (cmd === CONTROL_RESET) {
- // Most likely the session wasn't valid. Abandon the switch attempt
- this.log_('Got a reset on secondary, closing it');
- this.secondaryConn_.close();
- // If we were already using this connection for something, than we need to fully close
- if (this.tx_ === this.secondaryConn_ ||
- this.rx_ === this.secondaryConn_) {
- this.close();
- }
- }
- else if (cmd === CONTROL_PONG) {
- this.log_('got pong on secondary.');
- this.secondaryResponsesRequired_--;
- this.upgradeIfSecondaryHealthy_();
- }
- }
- };
- Connection.prototype.onSecondaryMessageReceived_ = function (parsedData) {
- var layer = requireKey('t', parsedData);
- var data = requireKey('d', parsedData);
- if (layer === 'c') {
- this.onSecondaryControl_(data);
- }
- else if (layer === 'd') {
- // got a data message, but we're still second connection. Need to buffer it up
- this.pendingDataMessages.push(data);
- }
- else {
- throw new Error('Unknown protocol layer: ' + layer);
- }
- };
- Connection.prototype.upgradeIfSecondaryHealthy_ = function () {
- if (this.secondaryResponsesRequired_ <= 0) {
- this.log_('Secondary connection is healthy.');
- this.isHealthy_ = true;
- this.secondaryConn_.markConnectionHealthy();
- this.proceedWithUpgrade_();
- }
- else {
- // Send a ping to make sure the connection is healthy.
- this.log_('sending ping on secondary.');
- this.secondaryConn_.send({ t: 'c', d: { t: PING, d: {} } });
- }
- };
- Connection.prototype.proceedWithUpgrade_ = function () {
- // tell this connection to consider itself open
- this.secondaryConn_.start();
- // send ack
- this.log_('sending client ack on secondary');
- this.secondaryConn_.send({ t: 'c', d: { t: SWITCH_ACK, d: {} } });
- // send end packet on primary transport, switch to sending on this one
- // can receive on this one, buffer responses until end received on primary transport
- this.log_('Ending transmission on primary');
- this.conn_.send({ t: 'c', d: { t: END_TRANSMISSION, d: {} } });
- this.tx_ = this.secondaryConn_;
- this.tryCleanupConnection();
- };
- Connection.prototype.onPrimaryMessageReceived_ = function (parsedData) {
- // Must refer to parsedData properties in quotes, so closure doesn't touch them.
- var layer = requireKey('t', parsedData);
- var data = requireKey('d', parsedData);
- if (layer === 'c') {
- this.onControl_(data);
- }
- else if (layer === 'd') {
- this.onDataMessage_(data);
- }
- };
- Connection.prototype.onDataMessage_ = function (message) {
- this.onPrimaryResponse_();
- // We don't do anything with data messages, just kick them up a level
- this.onMessage_(message);
- };
- Connection.prototype.onPrimaryResponse_ = function () {
- if (!this.isHealthy_) {
- this.primaryResponsesRequired_--;
- if (this.primaryResponsesRequired_ <= 0) {
- this.log_('Primary connection is healthy.');
- this.isHealthy_ = true;
- this.conn_.markConnectionHealthy();
- }
- }
- };
- Connection.prototype.onControl_ = function (controlData) {
- var cmd = requireKey(MESSAGE_TYPE, controlData);
- if (MESSAGE_DATA in controlData) {
- var payload = controlData[MESSAGE_DATA];
- if (cmd === SERVER_HELLO) {
- this.onHandshake_(payload);
- }
- else if (cmd === END_TRANSMISSION) {
- this.log_('recvd end transmission on primary');
- this.rx_ = this.secondaryConn_;
- for (var i = 0; i < this.pendingDataMessages.length; ++i) {
- this.onDataMessage_(this.pendingDataMessages[i]);
- }
- this.pendingDataMessages = [];
- this.tryCleanupConnection();
- }
- else if (cmd === CONTROL_SHUTDOWN) {
- // This was previously the 'onKill' callback passed to the lower-level connection
- // payload in this case is the reason for the shutdown. Generally a human-readable error
- this.onConnectionShutdown_(payload);
- }
- else if (cmd === CONTROL_RESET) {
- // payload in this case is the host we should contact
- this.onReset_(payload);
- }
- else if (cmd === CONTROL_ERROR) {
- error('Server Error: ' + payload);
- }
- else if (cmd === CONTROL_PONG) {
- this.log_('got pong on primary.');
- this.onPrimaryResponse_();
- this.sendPingOnPrimaryIfNecessary_();
- }
- else {
- error('Unknown control packet command: ' + cmd);
- }
- }
- };
- /**
- * @param handshake - The handshake data returned from the server
- */
- Connection.prototype.onHandshake_ = function (handshake) {
- var timestamp = handshake.ts;
- var version = handshake.v;
- var host = handshake.h;
- this.sessionId = handshake.s;
- this.repoInfo_.host = host;
- // if we've already closed the connection, then don't bother trying to progress further
- if (this.state_ === 0 /* RealtimeState.CONNECTING */) {
- this.conn_.start();
- this.onConnectionEstablished_(this.conn_, timestamp);
- if (PROTOCOL_VERSION !== version) {
- warn$1('Protocol version mismatch detected');
- }
- // TODO: do we want to upgrade? when? maybe a delay?
- this.tryStartUpgrade_();
- }
- };
- Connection.prototype.tryStartUpgrade_ = function () {
- var conn = this.transportManager_.upgradeTransport();
- if (conn) {
- this.startUpgrade_(conn);
- }
- };
- Connection.prototype.startUpgrade_ = function (conn) {
- var _this = this;
- this.secondaryConn_ = new conn(this.nextTransportId_(), this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, this.sessionId);
- // For certain transports (WebSockets), we need to send and receive several messages back and forth before we
- // can consider the transport healthy.
- this.secondaryResponsesRequired_ =
- conn['responsesRequiredToBeHealthy'] || 0;
- var onMessage = this.connReceiver_(this.secondaryConn_);
- var onDisconnect = this.disconnReceiver_(this.secondaryConn_);
- this.secondaryConn_.open(onMessage, onDisconnect);
- // If we haven't successfully upgraded after UPGRADE_TIMEOUT, give up and kill the secondary.
- setTimeoutNonBlocking(function () {
- if (_this.secondaryConn_) {
- _this.log_('Timed out trying to upgrade.');
- _this.secondaryConn_.close();
- }
- }, Math.floor(UPGRADE_TIMEOUT));
- };
- Connection.prototype.onReset_ = function (host) {
- this.log_('Reset packet received. New host: ' + host);
- this.repoInfo_.host = host;
- // TODO: if we're already "connected", we need to trigger a disconnect at the next layer up.
- // We don't currently support resets after the connection has already been established
- if (this.state_ === 1 /* RealtimeState.CONNECTED */) {
- this.close();
- }
- else {
- // Close whatever connections we have open and start again.
- this.closeConnections_();
- this.start_();
- }
- };
- Connection.prototype.onConnectionEstablished_ = function (conn, timestamp) {
- var _this = this;
- this.log_('Realtime connection established.');
- this.conn_ = conn;
- this.state_ = 1 /* RealtimeState.CONNECTED */;
- if (this.onReady_) {
- this.onReady_(timestamp, this.sessionId);
- this.onReady_ = null;
- }
- // If after 5 seconds we haven't sent enough requests to the server to get the connection healthy,
- // send some pings.
- if (this.primaryResponsesRequired_ === 0) {
- this.log_('Primary connection is healthy.');
- this.isHealthy_ = true;
- }
- else {
- setTimeoutNonBlocking(function () {
- _this.sendPingOnPrimaryIfNecessary_();
- }, Math.floor(DELAY_BEFORE_SENDING_EXTRA_REQUESTS));
- }
- };
- Connection.prototype.sendPingOnPrimaryIfNecessary_ = function () {
- // If the connection isn't considered healthy yet, we'll send a noop ping packet request.
- if (!this.isHealthy_ && this.state_ === 1 /* RealtimeState.CONNECTED */) {
- this.log_('sending ping on primary.');
- this.sendData_({ t: 'c', d: { t: PING, d: {} } });
- }
- };
- Connection.prototype.onSecondaryConnectionLost_ = function () {
- var conn = this.secondaryConn_;
- this.secondaryConn_ = null;
- if (this.tx_ === conn || this.rx_ === conn) {
- // we are relying on this connection already in some capacity. Therefore, a failure is real
- this.close();
- }
- };
- /**
- * @param everConnected - Whether or not the connection ever reached a server. Used to determine if
- * we should flush the host cache
- */
- Connection.prototype.onConnectionLost_ = function (everConnected) {
- this.conn_ = null;
- // NOTE: IF you're seeing a Firefox error for this line, I think it might be because it's getting
- // called on window close and RealtimeState.CONNECTING is no longer defined. Just a guess.
- if (!everConnected && this.state_ === 0 /* RealtimeState.CONNECTING */) {
- this.log_('Realtime connection failed.');
- // Since we failed to connect at all, clear any cached entry for this namespace in case the machine went away
- if (this.repoInfo_.isCacheableHost()) {
- PersistentStorage.remove('host:' + this.repoInfo_.host);
- // reset the internal host to what we would show the user, i.e. <ns>.firebaseio.com
- this.repoInfo_.internalHost = this.repoInfo_.host;
- }
- }
- else if (this.state_ === 1 /* RealtimeState.CONNECTED */) {
- this.log_('Realtime connection lost.');
- }
- this.close();
- };
- Connection.prototype.onConnectionShutdown_ = function (reason) {
- this.log_('Connection shutdown command received. Shutting down...');
- if (this.onKill_) {
- this.onKill_(reason);
- this.onKill_ = null;
- }
- // We intentionally don't want to fire onDisconnect (kill is a different case),
- // so clear the callback.
- this.onDisconnect_ = null;
- this.close();
- };
- Connection.prototype.sendData_ = function (data) {
- if (this.state_ !== 1 /* RealtimeState.CONNECTED */) {
- throw 'Connection is not connected';
- }
- else {
- this.tx_.send(data);
- }
- };
- /**
- * Cleans up this connection, calling the appropriate callbacks
- */
- Connection.prototype.close = function () {
- if (this.state_ !== 2 /* RealtimeState.DISCONNECTED */) {
- this.log_('Closing realtime connection.');
- this.state_ = 2 /* RealtimeState.DISCONNECTED */;
- this.closeConnections_();
- if (this.onDisconnect_) {
- this.onDisconnect_();
- this.onDisconnect_ = null;
- }
- }
- };
- Connection.prototype.closeConnections_ = function () {
- this.log_('Shutting down all connections');
- if (this.conn_) {
- this.conn_.close();
- this.conn_ = null;
- }
- if (this.secondaryConn_) {
- this.secondaryConn_.close();
- this.secondaryConn_ = null;
- }
- if (this.healthyTimeout_) {
- clearTimeout(this.healthyTimeout_);
- this.healthyTimeout_ = null;
- }
- };
- return Connection;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Interface defining the set of actions that can be performed against the Firebase server
- * (basically corresponds to our wire protocol).
- *
- * @interface
- */
- var ServerActions = /** @class */ (function () {
- function ServerActions() {
- }
- ServerActions.prototype.put = function (pathString, data, onComplete, hash) { };
- ServerActions.prototype.merge = function (pathString, data, onComplete, hash) { };
- /**
- * Refreshes the auth token for the current connection.
- * @param token - The authentication token
- */
- ServerActions.prototype.refreshAuthToken = function (token) { };
- /**
- * Refreshes the app check token for the current connection.
- * @param token The app check token
- */
- ServerActions.prototype.refreshAppCheckToken = function (token) { };
- ServerActions.prototype.onDisconnectPut = function (pathString, data, onComplete) { };
- ServerActions.prototype.onDisconnectMerge = function (pathString, data, onComplete) { };
- ServerActions.prototype.onDisconnectCancel = function (pathString, onComplete) { };
- ServerActions.prototype.reportStats = function (stats) { };
- return ServerActions;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Base class to be used if you want to emit events. Call the constructor with
- * the set of allowed event names.
- */
- var EventEmitter = /** @class */ (function () {
- function EventEmitter(allowedEvents_) {
- this.allowedEvents_ = allowedEvents_;
- this.listeners_ = {};
- util.assert(Array.isArray(allowedEvents_) && allowedEvents_.length > 0, 'Requires a non-empty array');
- }
- /**
- * To be called by derived classes to trigger events.
- */
- EventEmitter.prototype.trigger = function (eventType) {
- var varArgs = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- varArgs[_i - 1] = arguments[_i];
- }
- if (Array.isArray(this.listeners_[eventType])) {
- // Clone the list, since callbacks could add/remove listeners.
- var listeners = tslib.__spreadArray([], tslib.__read(this.listeners_[eventType]), false);
- for (var i = 0; i < listeners.length; i++) {
- listeners[i].callback.apply(listeners[i].context, varArgs);
- }
- }
- };
- EventEmitter.prototype.on = function (eventType, callback, context) {
- this.validateEventType_(eventType);
- this.listeners_[eventType] = this.listeners_[eventType] || [];
- this.listeners_[eventType].push({ callback: callback, context: context });
- var eventData = this.getInitialEvent(eventType);
- if (eventData) {
- callback.apply(context, eventData);
- }
- };
- EventEmitter.prototype.off = function (eventType, callback, context) {
- this.validateEventType_(eventType);
- var listeners = this.listeners_[eventType] || [];
- for (var i = 0; i < listeners.length; i++) {
- if (listeners[i].callback === callback &&
- (!context || context === listeners[i].context)) {
- listeners.splice(i, 1);
- return;
- }
- }
- };
- EventEmitter.prototype.validateEventType_ = function (eventType) {
- util.assert(this.allowedEvents_.find(function (et) {
- return et === eventType;
- }), 'Unknown event: ' + eventType);
- };
- return EventEmitter;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Monitors online state (as reported by window.online/offline events).
- *
- * The expectation is that this could have many false positives (thinks we are online
- * when we're not), but no false negatives. So we can safely use it to determine when
- * we definitely cannot reach the internet.
- */
- var OnlineMonitor = /** @class */ (function (_super) {
- tslib.__extends(OnlineMonitor, _super);
- function OnlineMonitor() {
- var _this = _super.call(this, ['online']) || this;
- _this.online_ = true;
- // We've had repeated complaints that Cordova apps can get stuck "offline", e.g.
- // https://forum.ionicframework.com/t/firebase-connection-is-lost-and-never-come-back/43810
- // It would seem that the 'online' event does not always fire consistently. So we disable it
- // for Cordova.
- if (typeof window !== 'undefined' &&
- typeof window.addEventListener !== 'undefined' &&
- !util.isMobileCordova()) {
- window.addEventListener('online', function () {
- if (!_this.online_) {
- _this.online_ = true;
- _this.trigger('online', true);
- }
- }, false);
- window.addEventListener('offline', function () {
- if (_this.online_) {
- _this.online_ = false;
- _this.trigger('online', false);
- }
- }, false);
- }
- return _this;
- }
- OnlineMonitor.getInstance = function () {
- return new OnlineMonitor();
- };
- OnlineMonitor.prototype.getInitialEvent = function (eventType) {
- util.assert(eventType === 'online', 'Unknown event type: ' + eventType);
- return [this.online_];
- };
- OnlineMonitor.prototype.currentlyOnline = function () {
- return this.online_;
- };
- return OnlineMonitor;
- }(EventEmitter));
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /** Maximum key depth. */
- var MAX_PATH_DEPTH = 32;
- /** Maximum number of (UTF8) bytes in a Firebase path. */
- var MAX_PATH_LENGTH_BYTES = 768;
- /**
- * An immutable object representing a parsed path. It's immutable so that you
- * can pass them around to other functions without worrying about them changing
- * it.
- */
- var Path = /** @class */ (function () {
- /**
- * @param pathOrString - Path string to parse, or another path, or the raw
- * tokens array
- */
- function Path(pathOrString, pieceNum) {
- if (pieceNum === void 0) {
- this.pieces_ = pathOrString.split('/');
- // Remove empty pieces.
- var copyTo = 0;
- for (var i = 0; i < this.pieces_.length; i++) {
- if (this.pieces_[i].length > 0) {
- this.pieces_[copyTo] = this.pieces_[i];
- copyTo++;
- }
- }
- this.pieces_.length = copyTo;
- this.pieceNum_ = 0;
- }
- else {
- this.pieces_ = pathOrString;
- this.pieceNum_ = pieceNum;
- }
- }
- Path.prototype.toString = function () {
- var pathString = '';
- for (var i = this.pieceNum_; i < this.pieces_.length; i++) {
- if (this.pieces_[i] !== '') {
- pathString += '/' + this.pieces_[i];
- }
- }
- return pathString || '/';
- };
- return Path;
- }());
- function newEmptyPath() {
- return new Path('');
- }
- function pathGetFront(path) {
- if (path.pieceNum_ >= path.pieces_.length) {
- return null;
- }
- return path.pieces_[path.pieceNum_];
- }
- /**
- * @returns The number of segments in this path
- */
- function pathGetLength(path) {
- return path.pieces_.length - path.pieceNum_;
- }
- function pathPopFront(path) {
- var pieceNum = path.pieceNum_;
- if (pieceNum < path.pieces_.length) {
- pieceNum++;
- }
- return new Path(path.pieces_, pieceNum);
- }
- function pathGetBack(path) {
- if (path.pieceNum_ < path.pieces_.length) {
- return path.pieces_[path.pieces_.length - 1];
- }
- return null;
- }
- function pathToUrlEncodedString(path) {
- var pathString = '';
- for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
- if (path.pieces_[i] !== '') {
- pathString += '/' + encodeURIComponent(String(path.pieces_[i]));
- }
- }
- return pathString || '/';
- }
- /**
- * Shallow copy of the parts of the path.
- *
- */
- function pathSlice(path, begin) {
- if (begin === void 0) { begin = 0; }
- return path.pieces_.slice(path.pieceNum_ + begin);
- }
- function pathParent(path) {
- if (path.pieceNum_ >= path.pieces_.length) {
- return null;
- }
- var pieces = [];
- for (var i = path.pieceNum_; i < path.pieces_.length - 1; i++) {
- pieces.push(path.pieces_[i]);
- }
- return new Path(pieces, 0);
- }
- function pathChild(path, childPathObj) {
- var pieces = [];
- for (var i = path.pieceNum_; i < path.pieces_.length; i++) {
- pieces.push(path.pieces_[i]);
- }
- if (childPathObj instanceof Path) {
- for (var i = childPathObj.pieceNum_; i < childPathObj.pieces_.length; i++) {
- pieces.push(childPathObj.pieces_[i]);
- }
- }
- else {
- var childPieces = childPathObj.split('/');
- for (var i = 0; i < childPieces.length; i++) {
- if (childPieces[i].length > 0) {
- pieces.push(childPieces[i]);
- }
- }
- }
- return new Path(pieces, 0);
- }
- /**
- * @returns True if there are no segments in this path
- */
- function pathIsEmpty(path) {
- return path.pieceNum_ >= path.pieces_.length;
- }
- /**
- * @returns The path from outerPath to innerPath
- */
- function newRelativePath(outerPath, innerPath) {
- var outer = pathGetFront(outerPath), inner = pathGetFront(innerPath);
- if (outer === null) {
- return innerPath;
- }
- else if (outer === inner) {
- return newRelativePath(pathPopFront(outerPath), pathPopFront(innerPath));
- }
- else {
- throw new Error('INTERNAL ERROR: innerPath (' +
- innerPath +
- ') is not within ' +
- 'outerPath (' +
- outerPath +
- ')');
- }
- }
- /**
- * @returns -1, 0, 1 if left is less, equal, or greater than the right.
- */
- function pathCompare(left, right) {
- var leftKeys = pathSlice(left, 0);
- var rightKeys = pathSlice(right, 0);
- for (var i = 0; i < leftKeys.length && i < rightKeys.length; i++) {
- var cmp = nameCompare(leftKeys[i], rightKeys[i]);
- if (cmp !== 0) {
- return cmp;
- }
- }
- if (leftKeys.length === rightKeys.length) {
- return 0;
- }
- return leftKeys.length < rightKeys.length ? -1 : 1;
- }
- /**
- * @returns true if paths are the same.
- */
- function pathEquals(path, other) {
- if (pathGetLength(path) !== pathGetLength(other)) {
- return false;
- }
- for (var i = path.pieceNum_, j = other.pieceNum_; i <= path.pieces_.length; i++, j++) {
- if (path.pieces_[i] !== other.pieces_[j]) {
- return false;
- }
- }
- return true;
- }
- /**
- * @returns True if this path is a parent of (or the same as) other
- */
- function pathContains(path, other) {
- var i = path.pieceNum_;
- var j = other.pieceNum_;
- if (pathGetLength(path) > pathGetLength(other)) {
- return false;
- }
- while (i < path.pieces_.length) {
- if (path.pieces_[i] !== other.pieces_[j]) {
- return false;
- }
- ++i;
- ++j;
- }
- return true;
- }
- /**
- * Dynamic (mutable) path used to count path lengths.
- *
- * This class is used to efficiently check paths for valid
- * length (in UTF8 bytes) and depth (used in path validation).
- *
- * Throws Error exception if path is ever invalid.
- *
- * The definition of a path always begins with '/'.
- */
- var ValidationPath = /** @class */ (function () {
- /**
- * @param path - Initial Path.
- * @param errorPrefix_ - Prefix for any error messages.
- */
- function ValidationPath(path, errorPrefix_) {
- this.errorPrefix_ = errorPrefix_;
- this.parts_ = pathSlice(path, 0);
- /** Initialize to number of '/' chars needed in path. */
- this.byteLength_ = Math.max(1, this.parts_.length);
- for (var i = 0; i < this.parts_.length; i++) {
- this.byteLength_ += util.stringLength(this.parts_[i]);
- }
- validationPathCheckValid(this);
- }
- return ValidationPath;
- }());
- function validationPathPush(validationPath, child) {
- // Count the needed '/'
- if (validationPath.parts_.length > 0) {
- validationPath.byteLength_ += 1;
- }
- validationPath.parts_.push(child);
- validationPath.byteLength_ += util.stringLength(child);
- validationPathCheckValid(validationPath);
- }
- function validationPathPop(validationPath) {
- var last = validationPath.parts_.pop();
- validationPath.byteLength_ -= util.stringLength(last);
- // Un-count the previous '/'
- if (validationPath.parts_.length > 0) {
- validationPath.byteLength_ -= 1;
- }
- }
- function validationPathCheckValid(validationPath) {
- if (validationPath.byteLength_ > MAX_PATH_LENGTH_BYTES) {
- throw new Error(validationPath.errorPrefix_ +
- 'has a key path longer than ' +
- MAX_PATH_LENGTH_BYTES +
- ' bytes (' +
- validationPath.byteLength_ +
- ').');
- }
- if (validationPath.parts_.length > MAX_PATH_DEPTH) {
- throw new Error(validationPath.errorPrefix_ +
- 'path specified exceeds the maximum depth that can be written (' +
- MAX_PATH_DEPTH +
- ') or object contains a cycle ' +
- validationPathToErrorString(validationPath));
- }
- }
- /**
- * String for use in error messages - uses '.' notation for path.
- */
- function validationPathToErrorString(validationPath) {
- if (validationPath.parts_.length === 0) {
- return '';
- }
- return "in property '" + validationPath.parts_.join('.') + "'";
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var VisibilityMonitor = /** @class */ (function (_super) {
- tslib.__extends(VisibilityMonitor, _super);
- function VisibilityMonitor() {
- var _this = _super.call(this, ['visible']) || this;
- var hidden;
- var visibilityChange;
- if (typeof document !== 'undefined' &&
- typeof document.addEventListener !== 'undefined') {
- if (typeof document['hidden'] !== 'undefined') {
- // Opera 12.10 and Firefox 18 and later support
- visibilityChange = 'visibilitychange';
- hidden = 'hidden';
- }
- else if (typeof document['mozHidden'] !== 'undefined') {
- visibilityChange = 'mozvisibilitychange';
- hidden = 'mozHidden';
- }
- else if (typeof document['msHidden'] !== 'undefined') {
- visibilityChange = 'msvisibilitychange';
- hidden = 'msHidden';
- }
- else if (typeof document['webkitHidden'] !== 'undefined') {
- visibilityChange = 'webkitvisibilitychange';
- hidden = 'webkitHidden';
- }
- }
- // Initially, we always assume we are visible. This ensures that in browsers
- // without page visibility support or in cases where we are never visible
- // (e.g. chrome extension), we act as if we are visible, i.e. don't delay
- // reconnects
- _this.visible_ = true;
- if (visibilityChange) {
- document.addEventListener(visibilityChange, function () {
- var visible = !document[hidden];
- if (visible !== _this.visible_) {
- _this.visible_ = visible;
- _this.trigger('visible', visible);
- }
- }, false);
- }
- return _this;
- }
- VisibilityMonitor.getInstance = function () {
- return new VisibilityMonitor();
- };
- VisibilityMonitor.prototype.getInitialEvent = function (eventType) {
- util.assert(eventType === 'visible', 'Unknown event type: ' + eventType);
- return [this.visible_];
- };
- return VisibilityMonitor;
- }(EventEmitter));
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var RECONNECT_MIN_DELAY = 1000;
- var RECONNECT_MAX_DELAY_DEFAULT = 60 * 5 * 1000; // 5 minutes in milliseconds (Case: 1858)
- var RECONNECT_MAX_DELAY_FOR_ADMINS = 30 * 1000; // 30 seconds for admin clients (likely to be a backend server)
- var RECONNECT_DELAY_MULTIPLIER = 1.3;
- var RECONNECT_DELAY_RESET_TIMEOUT = 30000; // Reset delay back to MIN_DELAY after being connected for 30sec.
- var SERVER_KILL_INTERRUPT_REASON = 'server_kill';
- // If auth fails repeatedly, we'll assume something is wrong and log a warning / back off.
- var INVALID_TOKEN_THRESHOLD = 3;
- /**
- * Firebase connection. Abstracts wire protocol and handles reconnecting.
- *
- * NOTE: All JSON objects sent to the realtime connection must have property names enclosed
- * in quotes to make sure the closure compiler does not minify them.
- */
- var PersistentConnection = /** @class */ (function (_super) {
- tslib.__extends(PersistentConnection, _super);
- /**
- * @param repoInfo_ - Data about the namespace we are connecting to
- * @param applicationId_ - The Firebase App ID for this project
- * @param onDataUpdate_ - A callback for new data from the server
- */
- function PersistentConnection(repoInfo_, applicationId_, onDataUpdate_, onConnectStatus_, onServerInfoUpdate_, authTokenProvider_, appCheckTokenProvider_, authOverride_) {
- var _this = _super.call(this) || this;
- _this.repoInfo_ = repoInfo_;
- _this.applicationId_ = applicationId_;
- _this.onDataUpdate_ = onDataUpdate_;
- _this.onConnectStatus_ = onConnectStatus_;
- _this.onServerInfoUpdate_ = onServerInfoUpdate_;
- _this.authTokenProvider_ = authTokenProvider_;
- _this.appCheckTokenProvider_ = appCheckTokenProvider_;
- _this.authOverride_ = authOverride_;
- // Used for diagnostic logging.
- _this.id = PersistentConnection.nextPersistentConnectionId_++;
- _this.log_ = logWrapper('p:' + _this.id + ':');
- _this.interruptReasons_ = {};
- _this.listens = new Map();
- _this.outstandingPuts_ = [];
- _this.outstandingGets_ = [];
- _this.outstandingPutCount_ = 0;
- _this.outstandingGetCount_ = 0;
- _this.onDisconnectRequestQueue_ = [];
- _this.connected_ = false;
- _this.reconnectDelay_ = RECONNECT_MIN_DELAY;
- _this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_DEFAULT;
- _this.securityDebugCallback_ = null;
- _this.lastSessionId = null;
- _this.establishConnectionTimer_ = null;
- _this.visible_ = false;
- // Before we get connected, we keep a queue of pending messages to send.
- _this.requestCBHash_ = {};
- _this.requestNumber_ = 0;
- _this.realtime_ = null;
- _this.authToken_ = null;
- _this.appCheckToken_ = null;
- _this.forceTokenRefresh_ = false;
- _this.invalidAuthTokenCount_ = 0;
- _this.invalidAppCheckTokenCount_ = 0;
- _this.firstConnection_ = true;
- _this.lastConnectionAttemptTime_ = null;
- _this.lastConnectionEstablishedTime_ = null;
- if (authOverride_ && !util.isNodeSdk()) {
- throw new Error('Auth override specified in options, but not supported on non Node.js platforms');
- }
- VisibilityMonitor.getInstance().on('visible', _this.onVisible_, _this);
- if (repoInfo_.host.indexOf('fblocal') === -1) {
- OnlineMonitor.getInstance().on('online', _this.onOnline_, _this);
- }
- return _this;
- }
- PersistentConnection.prototype.sendRequest = function (action, body, onResponse) {
- var curReqNum = ++this.requestNumber_;
- var msg = { r: curReqNum, a: action, b: body };
- this.log_(util.stringify(msg));
- util.assert(this.connected_, "sendRequest call when we're not connected not allowed.");
- this.realtime_.sendRequest(msg);
- if (onResponse) {
- this.requestCBHash_[curReqNum] = onResponse;
- }
- };
- PersistentConnection.prototype.get = function (query) {
- this.initConnection_();
- var deferred = new util.Deferred();
- var request = {
- p: query._path.toString(),
- q: query._queryObject
- };
- var outstandingGet = {
- action: 'g',
- request: request,
- onComplete: function (message) {
- var payload = message['d'];
- if (message['s'] === 'ok') {
- deferred.resolve(payload);
- }
- else {
- deferred.reject(payload);
- }
- }
- };
- this.outstandingGets_.push(outstandingGet);
- this.outstandingGetCount_++;
- var index = this.outstandingGets_.length - 1;
- if (this.connected_) {
- this.sendGet_(index);
- }
- return deferred.promise;
- };
- PersistentConnection.prototype.listen = function (query, currentHashFn, tag, onComplete) {
- this.initConnection_();
- var queryId = query._queryIdentifier;
- var pathString = query._path.toString();
- this.log_('Listen called for ' + pathString + ' ' + queryId);
- if (!this.listens.has(pathString)) {
- this.listens.set(pathString, new Map());
- }
- util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'listen() called for non-default but complete query');
- util.assert(!this.listens.get(pathString).has(queryId), "listen() called twice for same path/queryId.");
- var listenSpec = {
- onComplete: onComplete,
- hashFn: currentHashFn,
- query: query,
- tag: tag
- };
- this.listens.get(pathString).set(queryId, listenSpec);
- if (this.connected_) {
- this.sendListen_(listenSpec);
- }
- };
- PersistentConnection.prototype.sendGet_ = function (index) {
- var _this = this;
- var get = this.outstandingGets_[index];
- this.sendRequest('g', get.request, function (message) {
- delete _this.outstandingGets_[index];
- _this.outstandingGetCount_--;
- if (_this.outstandingGetCount_ === 0) {
- _this.outstandingGets_ = [];
- }
- if (get.onComplete) {
- get.onComplete(message);
- }
- });
- };
- PersistentConnection.prototype.sendListen_ = function (listenSpec) {
- var _this = this;
- var query = listenSpec.query;
- var pathString = query._path.toString();
- var queryId = query._queryIdentifier;
- this.log_('Listen on ' + pathString + ' for ' + queryId);
- var req = { /*path*/ p: pathString };
- var action = 'q';
- // Only bother to send query if it's non-default.
- if (listenSpec.tag) {
- req['q'] = query._queryObject;
- req['t'] = listenSpec.tag;
- }
- req[ /*hash*/'h'] = listenSpec.hashFn();
- this.sendRequest(action, req, function (message) {
- var payload = message[ /*data*/'d'];
- var status = message[ /*status*/'s'];
- // print warnings in any case...
- PersistentConnection.warnOnListenWarnings_(payload, query);
- var currentListenSpec = _this.listens.get(pathString) &&
- _this.listens.get(pathString).get(queryId);
- // only trigger actions if the listen hasn't been removed and readded
- if (currentListenSpec === listenSpec) {
- _this.log_('listen response', message);
- if (status !== 'ok') {
- _this.removeListen_(pathString, queryId);
- }
- if (listenSpec.onComplete) {
- listenSpec.onComplete(status, payload);
- }
- }
- });
- };
- PersistentConnection.warnOnListenWarnings_ = function (payload, query) {
- if (payload && typeof payload === 'object' && util.contains(payload, 'w')) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- var warnings = util.safeGet(payload, 'w');
- if (Array.isArray(warnings) && ~warnings.indexOf('no_index')) {
- var indexSpec = '".indexOn": "' + query._queryParams.getIndex().toString() + '"';
- var indexPath = query._path.toString();
- warn$1("Using an unspecified index. Your data will be downloaded and " +
- "filtered on the client. Consider adding ".concat(indexSpec, " at ") +
- "".concat(indexPath, " to your security rules for better performance."));
- }
- }
- };
- PersistentConnection.prototype.refreshAuthToken = function (token) {
- this.authToken_ = token;
- this.log_('Auth token refreshed');
- if (this.authToken_) {
- this.tryAuth();
- }
- else {
- //If we're connected we want to let the server know to unauthenticate us. If we're not connected, simply delete
- //the credential so we dont become authenticated next time we connect.
- if (this.connected_) {
- this.sendRequest('unauth', {}, function () { });
- }
- }
- this.reduceReconnectDelayIfAdminCredential_(token);
- };
- PersistentConnection.prototype.reduceReconnectDelayIfAdminCredential_ = function (credential) {
- // NOTE: This isn't intended to be bulletproof (a malicious developer can always just modify the client).
- // Additionally, we don't bother resetting the max delay back to the default if auth fails / expires.
- var isFirebaseSecret = credential && credential.length === 40;
- if (isFirebaseSecret || util.isAdmin(credential)) {
- this.log_('Admin auth credential detected. Reducing max reconnect time.');
- this.maxReconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
- }
- };
- PersistentConnection.prototype.refreshAppCheckToken = function (token) {
- this.appCheckToken_ = token;
- this.log_('App check token refreshed');
- if (this.appCheckToken_) {
- this.tryAppCheck();
- }
- else {
- //If we're connected we want to let the server know to unauthenticate us.
- //If we're not connected, simply delete the credential so we dont become
- // authenticated next time we connect.
- if (this.connected_) {
- this.sendRequest('unappeck', {}, function () { });
- }
- }
- };
- /**
- * Attempts to authenticate with the given credentials. If the authentication attempt fails, it's triggered like
- * a auth revoked (the connection is closed).
- */
- PersistentConnection.prototype.tryAuth = function () {
- var _this = this;
- if (this.connected_ && this.authToken_) {
- var token_1 = this.authToken_;
- var authMethod = util.isValidFormat(token_1) ? 'auth' : 'gauth';
- var requestData = { cred: token_1 };
- if (this.authOverride_ === null) {
- requestData['noauth'] = true;
- }
- else if (typeof this.authOverride_ === 'object') {
- requestData['authvar'] = this.authOverride_;
- }
- this.sendRequest(authMethod, requestData, function (res) {
- var status = res[ /*status*/'s'];
- var data = res[ /*data*/'d'] || 'error';
- if (_this.authToken_ === token_1) {
- if (status === 'ok') {
- _this.invalidAuthTokenCount_ = 0;
- }
- else {
- // Triggers reconnect and force refresh for auth token
- _this.onAuthRevoked_(status, data);
- }
- }
- });
- }
- };
- /**
- * Attempts to authenticate with the given token. If the authentication
- * attempt fails, it's triggered like the token was revoked (the connection is
- * closed).
- */
- PersistentConnection.prototype.tryAppCheck = function () {
- var _this = this;
- if (this.connected_ && this.appCheckToken_) {
- this.sendRequest('appcheck', { 'token': this.appCheckToken_ }, function (res) {
- var status = res[ /*status*/'s'];
- var data = res[ /*data*/'d'] || 'error';
- if (status === 'ok') {
- _this.invalidAppCheckTokenCount_ = 0;
- }
- else {
- _this.onAppCheckRevoked_(status, data);
- }
- });
- }
- };
- /**
- * @inheritDoc
- */
- PersistentConnection.prototype.unlisten = function (query, tag) {
- var pathString = query._path.toString();
- var queryId = query._queryIdentifier;
- this.log_('Unlisten called for ' + pathString + ' ' + queryId);
- util.assert(query._queryParams.isDefault() || !query._queryParams.loadsAllData(), 'unlisten() called for non-default but complete query');
- var listen = this.removeListen_(pathString, queryId);
- if (listen && this.connected_) {
- this.sendUnlisten_(pathString, queryId, query._queryObject, tag);
- }
- };
- PersistentConnection.prototype.sendUnlisten_ = function (pathString, queryId, queryObj, tag) {
- this.log_('Unlisten on ' + pathString + ' for ' + queryId);
- var req = { /*path*/ p: pathString };
- var action = 'n';
- // Only bother sending queryId if it's non-default.
- if (tag) {
- req['q'] = queryObj;
- req['t'] = tag;
- }
- this.sendRequest(action, req);
- };
- PersistentConnection.prototype.onDisconnectPut = function (pathString, data, onComplete) {
- this.initConnection_();
- if (this.connected_) {
- this.sendOnDisconnect_('o', pathString, data, onComplete);
- }
- else {
- this.onDisconnectRequestQueue_.push({
- pathString: pathString,
- action: 'o',
- data: data,
- onComplete: onComplete
- });
- }
- };
- PersistentConnection.prototype.onDisconnectMerge = function (pathString, data, onComplete) {
- this.initConnection_();
- if (this.connected_) {
- this.sendOnDisconnect_('om', pathString, data, onComplete);
- }
- else {
- this.onDisconnectRequestQueue_.push({
- pathString: pathString,
- action: 'om',
- data: data,
- onComplete: onComplete
- });
- }
- };
- PersistentConnection.prototype.onDisconnectCancel = function (pathString, onComplete) {
- this.initConnection_();
- if (this.connected_) {
- this.sendOnDisconnect_('oc', pathString, null, onComplete);
- }
- else {
- this.onDisconnectRequestQueue_.push({
- pathString: pathString,
- action: 'oc',
- data: null,
- onComplete: onComplete
- });
- }
- };
- PersistentConnection.prototype.sendOnDisconnect_ = function (action, pathString, data, onComplete) {
- var request = { /*path*/ p: pathString, /*data*/ d: data };
- this.log_('onDisconnect ' + action, request);
- this.sendRequest(action, request, function (response) {
- if (onComplete) {
- setTimeout(function () {
- onComplete(response[ /*status*/'s'], response[ /* data */'d']);
- }, Math.floor(0));
- }
- });
- };
- PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
- this.putInternal('p', pathString, data, onComplete, hash);
- };
- PersistentConnection.prototype.merge = function (pathString, data, onComplete, hash) {
- this.putInternal('m', pathString, data, onComplete, hash);
- };
- PersistentConnection.prototype.putInternal = function (action, pathString, data, onComplete, hash) {
- this.initConnection_();
- var request = {
- /*path*/ p: pathString,
- /*data*/ d: data
- };
- if (hash !== undefined) {
- request[ /*hash*/'h'] = hash;
- }
- // TODO: Only keep track of the most recent put for a given path?
- this.outstandingPuts_.push({
- action: action,
- request: request,
- onComplete: onComplete
- });
- this.outstandingPutCount_++;
- var index = this.outstandingPuts_.length - 1;
- if (this.connected_) {
- this.sendPut_(index);
- }
- else {
- this.log_('Buffering put: ' + pathString);
- }
- };
- PersistentConnection.prototype.sendPut_ = function (index) {
- var _this = this;
- var action = this.outstandingPuts_[index].action;
- var request = this.outstandingPuts_[index].request;
- var onComplete = this.outstandingPuts_[index].onComplete;
- this.outstandingPuts_[index].queued = this.connected_;
- this.sendRequest(action, request, function (message) {
- _this.log_(action + ' response', message);
- delete _this.outstandingPuts_[index];
- _this.outstandingPutCount_--;
- // Clean up array occasionally.
- if (_this.outstandingPutCount_ === 0) {
- _this.outstandingPuts_ = [];
- }
- if (onComplete) {
- onComplete(message[ /*status*/'s'], message[ /* data */'d']);
- }
- });
- };
- PersistentConnection.prototype.reportStats = function (stats) {
- var _this = this;
- // If we're not connected, we just drop the stats.
- if (this.connected_) {
- var request = { /*counters*/ c: stats };
- this.log_('reportStats', request);
- this.sendRequest(/*stats*/ 's', request, function (result) {
- var status = result[ /*status*/'s'];
- if (status !== 'ok') {
- var errorReason = result[ /* data */'d'];
- _this.log_('reportStats', 'Error sending stats: ' + errorReason);
- }
- });
- }
- };
- PersistentConnection.prototype.onDataMessage_ = function (message) {
- if ('r' in message) {
- // this is a response
- this.log_('from server: ' + util.stringify(message));
- var reqNum = message['r'];
- var onResponse = this.requestCBHash_[reqNum];
- if (onResponse) {
- delete this.requestCBHash_[reqNum];
- onResponse(message[ /*body*/'b']);
- }
- }
- else if ('error' in message) {
- throw 'A server-side error has occurred: ' + message['error'];
- }
- else if ('a' in message) {
- // a and b are action and body, respectively
- this.onDataPush_(message['a'], message['b']);
- }
- };
- PersistentConnection.prototype.onDataPush_ = function (action, body) {
- this.log_('handleServerMessage', action, body);
- if (action === 'd') {
- this.onDataUpdate_(body[ /*path*/'p'], body[ /*data*/'d'],
- /*isMerge*/ false, body['t']);
- }
- else if (action === 'm') {
- this.onDataUpdate_(body[ /*path*/'p'], body[ /*data*/'d'],
- /*isMerge=*/ true, body['t']);
- }
- else if (action === 'c') {
- this.onListenRevoked_(body[ /*path*/'p'], body[ /*query*/'q']);
- }
- else if (action === 'ac') {
- this.onAuthRevoked_(body[ /*status code*/'s'], body[ /* explanation */'d']);
- }
- else if (action === 'apc') {
- this.onAppCheckRevoked_(body[ /*status code*/'s'], body[ /* explanation */'d']);
- }
- else if (action === 'sd') {
- this.onSecurityDebugPacket_(body);
- }
- else {
- error('Unrecognized action received from server: ' +
- util.stringify(action) +
- '\nAre you using the latest client?');
- }
- };
- PersistentConnection.prototype.onReady_ = function (timestamp, sessionId) {
- this.log_('connection ready');
- this.connected_ = true;
- this.lastConnectionEstablishedTime_ = new Date().getTime();
- this.handleTimestamp_(timestamp);
- this.lastSessionId = sessionId;
- if (this.firstConnection_) {
- this.sendConnectStats_();
- }
- this.restoreState_();
- this.firstConnection_ = false;
- this.onConnectStatus_(true);
- };
- PersistentConnection.prototype.scheduleConnect_ = function (timeout) {
- var _this = this;
- util.assert(!this.realtime_, "Scheduling a connect when we're already connected/ing?");
- if (this.establishConnectionTimer_) {
- clearTimeout(this.establishConnectionTimer_);
- }
- // NOTE: Even when timeout is 0, it's important to do a setTimeout to work around an infuriating "Security Error" in
- // Firefox when trying to write to our long-polling iframe in some scenarios (e.g. Forge or our unit tests).
- this.establishConnectionTimer_ = setTimeout(function () {
- _this.establishConnectionTimer_ = null;
- _this.establishConnection_();
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }, Math.floor(timeout));
- };
- PersistentConnection.prototype.initConnection_ = function () {
- if (!this.realtime_ && this.firstConnection_) {
- this.scheduleConnect_(0);
- }
- };
- PersistentConnection.prototype.onVisible_ = function (visible) {
- // NOTE: Tabbing away and back to a window will defeat our reconnect backoff, but I think that's fine.
- if (visible &&
- !this.visible_ &&
- this.reconnectDelay_ === this.maxReconnectDelay_) {
- this.log_('Window became visible. Reducing delay.');
- this.reconnectDelay_ = RECONNECT_MIN_DELAY;
- if (!this.realtime_) {
- this.scheduleConnect_(0);
- }
- }
- this.visible_ = visible;
- };
- PersistentConnection.prototype.onOnline_ = function (online) {
- if (online) {
- this.log_('Browser went online.');
- this.reconnectDelay_ = RECONNECT_MIN_DELAY;
- if (!this.realtime_) {
- this.scheduleConnect_(0);
- }
- }
- else {
- this.log_('Browser went offline. Killing connection.');
- if (this.realtime_) {
- this.realtime_.close();
- }
- }
- };
- PersistentConnection.prototype.onRealtimeDisconnect_ = function () {
- this.log_('data client disconnected');
- this.connected_ = false;
- this.realtime_ = null;
- // Since we don't know if our sent transactions succeeded or not, we need to cancel them.
- this.cancelSentTransactions_();
- // Clear out the pending requests.
- this.requestCBHash_ = {};
- if (this.shouldReconnect_()) {
- if (!this.visible_) {
- this.log_("Window isn't visible. Delaying reconnect.");
- this.reconnectDelay_ = this.maxReconnectDelay_;
- this.lastConnectionAttemptTime_ = new Date().getTime();
- }
- else if (this.lastConnectionEstablishedTime_) {
- // If we've been connected long enough, reset reconnect delay to minimum.
- var timeSinceLastConnectSucceeded = new Date().getTime() - this.lastConnectionEstablishedTime_;
- if (timeSinceLastConnectSucceeded > RECONNECT_DELAY_RESET_TIMEOUT) {
- this.reconnectDelay_ = RECONNECT_MIN_DELAY;
- }
- this.lastConnectionEstablishedTime_ = null;
- }
- var timeSinceLastConnectAttempt = new Date().getTime() - this.lastConnectionAttemptTime_;
- var reconnectDelay = Math.max(0, this.reconnectDelay_ - timeSinceLastConnectAttempt);
- reconnectDelay = Math.random() * reconnectDelay;
- this.log_('Trying to reconnect in ' + reconnectDelay + 'ms');
- this.scheduleConnect_(reconnectDelay);
- // Adjust reconnect delay for next time.
- this.reconnectDelay_ = Math.min(this.maxReconnectDelay_, this.reconnectDelay_ * RECONNECT_DELAY_MULTIPLIER);
- }
- this.onConnectStatus_(false);
- };
- PersistentConnection.prototype.establishConnection_ = function () {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var onDataMessage, onReady, onDisconnect_1, connId, lastSessionId, canceled_1, connection_1, closeFn, sendRequestFn, forceRefresh, _a, authToken, appCheckToken, error_1;
- var _this = this;
- return tslib.__generator(this, function (_b) {
- switch (_b.label) {
- case 0:
- if (!this.shouldReconnect_()) return [3 /*break*/, 4];
- this.log_('Making a connection attempt');
- this.lastConnectionAttemptTime_ = new Date().getTime();
- this.lastConnectionEstablishedTime_ = null;
- onDataMessage = this.onDataMessage_.bind(this);
- onReady = this.onReady_.bind(this);
- onDisconnect_1 = this.onRealtimeDisconnect_.bind(this);
- connId = this.id + ':' + PersistentConnection.nextConnectionId_++;
- lastSessionId = this.lastSessionId;
- canceled_1 = false;
- connection_1 = null;
- closeFn = function () {
- if (connection_1) {
- connection_1.close();
- }
- else {
- canceled_1 = true;
- onDisconnect_1();
- }
- };
- sendRequestFn = function (msg) {
- util.assert(connection_1, "sendRequest call when we're not connected not allowed.");
- connection_1.sendRequest(msg);
- };
- this.realtime_ = {
- close: closeFn,
- sendRequest: sendRequestFn
- };
- forceRefresh = this.forceTokenRefresh_;
- this.forceTokenRefresh_ = false;
- _b.label = 1;
- case 1:
- _b.trys.push([1, 3, , 4]);
- return [4 /*yield*/, Promise.all([
- this.authTokenProvider_.getToken(forceRefresh),
- this.appCheckTokenProvider_.getToken(forceRefresh)
- ])];
- case 2:
- _a = tslib.__read.apply(void 0, [_b.sent(), 2]), authToken = _a[0], appCheckToken = _a[1];
- if (!canceled_1) {
- log('getToken() completed. Creating connection.');
- this.authToken_ = authToken && authToken.accessToken;
- this.appCheckToken_ = appCheckToken && appCheckToken.token;
- connection_1 = new Connection(connId, this.repoInfo_, this.applicationId_, this.appCheckToken_, this.authToken_, onDataMessage, onReady, onDisconnect_1,
- /* onKill= */ function (reason) {
- warn$1(reason + ' (' + _this.repoInfo_.toString() + ')');
- _this.interrupt(SERVER_KILL_INTERRUPT_REASON);
- }, lastSessionId);
- }
- else {
- log('getToken() completed but was canceled');
- }
- return [3 /*break*/, 4];
- case 3:
- error_1 = _b.sent();
- this.log_('Failed to get token: ' + error_1);
- if (!canceled_1) {
- if (this.repoInfo_.nodeAdmin) {
- // This may be a critical error for the Admin Node.js SDK, so log a warning.
- // But getToken() may also just have temporarily failed, so we still want to
- // continue retrying.
- warn$1(error_1);
- }
- closeFn();
- }
- return [3 /*break*/, 4];
- case 4: return [2 /*return*/];
- }
- });
- });
- };
- PersistentConnection.prototype.interrupt = function (reason) {
- log('Interrupting connection for reason: ' + reason);
- this.interruptReasons_[reason] = true;
- if (this.realtime_) {
- this.realtime_.close();
- }
- else {
- if (this.establishConnectionTimer_) {
- clearTimeout(this.establishConnectionTimer_);
- this.establishConnectionTimer_ = null;
- }
- if (this.connected_) {
- this.onRealtimeDisconnect_();
- }
- }
- };
- PersistentConnection.prototype.resume = function (reason) {
- log('Resuming connection for reason: ' + reason);
- delete this.interruptReasons_[reason];
- if (util.isEmpty(this.interruptReasons_)) {
- this.reconnectDelay_ = RECONNECT_MIN_DELAY;
- if (!this.realtime_) {
- this.scheduleConnect_(0);
- }
- }
- };
- PersistentConnection.prototype.handleTimestamp_ = function (timestamp) {
- var delta = timestamp - new Date().getTime();
- this.onServerInfoUpdate_({ serverTimeOffset: delta });
- };
- PersistentConnection.prototype.cancelSentTransactions_ = function () {
- for (var i = 0; i < this.outstandingPuts_.length; i++) {
- var put = this.outstandingPuts_[i];
- if (put && /*hash*/ 'h' in put.request && put.queued) {
- if (put.onComplete) {
- put.onComplete('disconnect');
- }
- delete this.outstandingPuts_[i];
- this.outstandingPutCount_--;
- }
- }
- // Clean up array occasionally.
- if (this.outstandingPutCount_ === 0) {
- this.outstandingPuts_ = [];
- }
- };
- PersistentConnection.prototype.onListenRevoked_ = function (pathString, query) {
- // Remove the listen and manufacture a "permission_denied" error for the failed listen.
- var queryId;
- if (!query) {
- queryId = 'default';
- }
- else {
- queryId = query.map(function (q) { return ObjectToUniqueKey(q); }).join('$');
- }
- var listen = this.removeListen_(pathString, queryId);
- if (listen && listen.onComplete) {
- listen.onComplete('permission_denied');
- }
- };
- PersistentConnection.prototype.removeListen_ = function (pathString, queryId) {
- var normalizedPathString = new Path(pathString).toString(); // normalize path.
- var listen;
- if (this.listens.has(normalizedPathString)) {
- var map = this.listens.get(normalizedPathString);
- listen = map.get(queryId);
- map.delete(queryId);
- if (map.size === 0) {
- this.listens.delete(normalizedPathString);
- }
- }
- else {
- // all listens for this path has already been removed
- listen = undefined;
- }
- return listen;
- };
- PersistentConnection.prototype.onAuthRevoked_ = function (statusCode, explanation) {
- log('Auth token revoked: ' + statusCode + '/' + explanation);
- this.authToken_ = null;
- this.forceTokenRefresh_ = true;
- this.realtime_.close();
- if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
- // We'll wait a couple times before logging the warning / increasing the
- // retry period since oauth tokens will report as "invalid" if they're
- // just expired. Plus there may be transient issues that resolve themselves.
- this.invalidAuthTokenCount_++;
- if (this.invalidAuthTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
- // Set a long reconnect delay because recovery is unlikely
- this.reconnectDelay_ = RECONNECT_MAX_DELAY_FOR_ADMINS;
- // Notify the auth token provider that the token is invalid, which will log
- // a warning
- this.authTokenProvider_.notifyForInvalidToken();
- }
- }
- };
- PersistentConnection.prototype.onAppCheckRevoked_ = function (statusCode, explanation) {
- log('App check token revoked: ' + statusCode + '/' + explanation);
- this.appCheckToken_ = null;
- this.forceTokenRefresh_ = true;
- // Note: We don't close the connection as the developer may not have
- // enforcement enabled. The backend closes connections with enforcements.
- if (statusCode === 'invalid_token' || statusCode === 'permission_denied') {
- // We'll wait a couple times before logging the warning / increasing the
- // retry period since oauth tokens will report as "invalid" if they're
- // just expired. Plus there may be transient issues that resolve themselves.
- this.invalidAppCheckTokenCount_++;
- if (this.invalidAppCheckTokenCount_ >= INVALID_TOKEN_THRESHOLD) {
- this.appCheckTokenProvider_.notifyForInvalidToken();
- }
- }
- };
- PersistentConnection.prototype.onSecurityDebugPacket_ = function (body) {
- if (this.securityDebugCallback_) {
- this.securityDebugCallback_(body);
- }
- else {
- if ('msg' in body) {
- console.log('FIREBASE: ' + body['msg'].replace('\n', '\nFIREBASE: '));
- }
- }
- };
- PersistentConnection.prototype.restoreState_ = function () {
- var e_1, _a, e_2, _b;
- //Re-authenticate ourselves if we have a credential stored.
- this.tryAuth();
- this.tryAppCheck();
- try {
- // Puts depend on having received the corresponding data update from the server before they complete, so we must
- // make sure to send listens before puts.
- for (var _c = tslib.__values(this.listens.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
- var queries = _d.value;
- try {
- for (var _e = (e_2 = void 0, tslib.__values(queries.values())), _f = _e.next(); !_f.done; _f = _e.next()) {
- var listenSpec = _f.value;
- this.sendListen_(listenSpec);
- }
- }
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
- finally {
- try {
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
- }
- finally { if (e_2) throw e_2.error; }
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
- }
- finally { if (e_1) throw e_1.error; }
- }
- for (var i = 0; i < this.outstandingPuts_.length; i++) {
- if (this.outstandingPuts_[i]) {
- this.sendPut_(i);
- }
- }
- while (this.onDisconnectRequestQueue_.length) {
- var request = this.onDisconnectRequestQueue_.shift();
- this.sendOnDisconnect_(request.action, request.pathString, request.data, request.onComplete);
- }
- for (var i = 0; i < this.outstandingGets_.length; i++) {
- if (this.outstandingGets_[i]) {
- this.sendGet_(i);
- }
- }
- };
- /**
- * Sends client stats for first connection
- */
- PersistentConnection.prototype.sendConnectStats_ = function () {
- var stats = {};
- var clientName = 'js';
- if (util.isNodeSdk()) {
- if (this.repoInfo_.nodeAdmin) {
- clientName = 'admin_node';
- }
- else {
- clientName = 'node';
- }
- }
- stats['sdk.' + clientName + '.' + SDK_VERSION.replace(/\./g, '-')] = 1;
- if (util.isMobileCordova()) {
- stats['framework.cordova'] = 1;
- }
- else if (util.isReactNative()) {
- stats['framework.reactnative'] = 1;
- }
- this.reportStats(stats);
- };
- PersistentConnection.prototype.shouldReconnect_ = function () {
- var online = OnlineMonitor.getInstance().currentlyOnline();
- return util.isEmpty(this.interruptReasons_) && online;
- };
- PersistentConnection.nextPersistentConnectionId_ = 0;
- /**
- * Counter for number of connections created. Mainly used for tagging in the logs
- */
- PersistentConnection.nextConnectionId_ = 0;
- return PersistentConnection;
- }(ServerActions));
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var NamedNode = /** @class */ (function () {
- function NamedNode(name, node) {
- this.name = name;
- this.node = node;
- }
- NamedNode.Wrap = function (name, node) {
- return new NamedNode(name, node);
- };
- return NamedNode;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var Index = /** @class */ (function () {
- function Index() {
- }
- /**
- * @returns A standalone comparison function for
- * this index
- */
- Index.prototype.getCompare = function () {
- return this.compare.bind(this);
- };
- /**
- * Given a before and after value for a node, determine if the indexed value has changed. Even if they are different,
- * it's possible that the changes are isolated to parts of the snapshot that are not indexed.
- *
- *
- * @returns True if the portion of the snapshot being indexed changed between oldNode and newNode
- */
- Index.prototype.indexedValueChanged = function (oldNode, newNode) {
- var oldWrapped = new NamedNode(MIN_NAME, oldNode);
- var newWrapped = new NamedNode(MIN_NAME, newNode);
- return this.compare(oldWrapped, newWrapped) !== 0;
- };
- /**
- * @returns a node wrapper that will sort equal to or less than
- * any other node wrapper, using this index
- */
- Index.prototype.minPost = function () {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return NamedNode.MIN;
- };
- return Index;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var __EMPTY_NODE;
- var KeyIndex = /** @class */ (function (_super) {
- tslib.__extends(KeyIndex, _super);
- function KeyIndex() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- Object.defineProperty(KeyIndex, "__EMPTY_NODE", {
- get: function () {
- return __EMPTY_NODE;
- },
- set: function (val) {
- __EMPTY_NODE = val;
- },
- enumerable: false,
- configurable: true
- });
- KeyIndex.prototype.compare = function (a, b) {
- return nameCompare(a.name, b.name);
- };
- KeyIndex.prototype.isDefinedOn = function (node) {
- // We could probably return true here (since every node has a key), but it's never called
- // so just leaving unimplemented for now.
- throw util.assertionError('KeyIndex.isDefinedOn not expected to be called.');
- };
- KeyIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
- return false; // The key for a node never changes.
- };
- KeyIndex.prototype.minPost = function () {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return NamedNode.MIN;
- };
- KeyIndex.prototype.maxPost = function () {
- // TODO: This should really be created once and cached in a static property, but
- // NamedNode isn't defined yet, so I can't use it in a static. Bleh.
- return new NamedNode(MAX_NAME, __EMPTY_NODE);
- };
- KeyIndex.prototype.makePost = function (indexValue, name) {
- util.assert(typeof indexValue === 'string', 'KeyIndex indexValue must always be a string.');
- // We just use empty node, but it'll never be compared, since our comparator only looks at name.
- return new NamedNode(indexValue, __EMPTY_NODE);
- };
- /**
- * @returns String representation for inclusion in a query spec
- */
- KeyIndex.prototype.toString = function () {
- return '.key';
- };
- return KeyIndex;
- }(Index));
- var KEY_INDEX = new KeyIndex();
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An iterator over an LLRBNode.
- */
- var SortedMapIterator = /** @class */ (function () {
- /**
- * @param node - Node to iterate.
- * @param isReverse_ - Whether or not to iterate in reverse
- */
- function SortedMapIterator(node, startKey, comparator, isReverse_, resultGenerator_) {
- if (resultGenerator_ === void 0) { resultGenerator_ = null; }
- this.isReverse_ = isReverse_;
- this.resultGenerator_ = resultGenerator_;
- this.nodeStack_ = [];
- var cmp = 1;
- while (!node.isEmpty()) {
- node = node;
- cmp = startKey ? comparator(node.key, startKey) : 1;
- // flip the comparison if we're going in reverse
- if (isReverse_) {
- cmp *= -1;
- }
- if (cmp < 0) {
- // This node is less than our start key. ignore it
- if (this.isReverse_) {
- node = node.left;
- }
- else {
- node = node.right;
- }
- }
- else if (cmp === 0) {
- // This node is exactly equal to our start key. Push it on the stack, but stop iterating;
- this.nodeStack_.push(node);
- break;
- }
- else {
- // This node is greater than our start key, add it to the stack and move to the next one
- this.nodeStack_.push(node);
- if (this.isReverse_) {
- node = node.right;
- }
- else {
- node = node.left;
- }
- }
- }
- }
- SortedMapIterator.prototype.getNext = function () {
- if (this.nodeStack_.length === 0) {
- return null;
- }
- var node = this.nodeStack_.pop();
- var result;
- if (this.resultGenerator_) {
- result = this.resultGenerator_(node.key, node.value);
- }
- else {
- result = { key: node.key, value: node.value };
- }
- if (this.isReverse_) {
- node = node.left;
- while (!node.isEmpty()) {
- this.nodeStack_.push(node);
- node = node.right;
- }
- }
- else {
- node = node.right;
- while (!node.isEmpty()) {
- this.nodeStack_.push(node);
- node = node.left;
- }
- }
- return result;
- };
- SortedMapIterator.prototype.hasNext = function () {
- return this.nodeStack_.length > 0;
- };
- SortedMapIterator.prototype.peek = function () {
- if (this.nodeStack_.length === 0) {
- return null;
- }
- var node = this.nodeStack_[this.nodeStack_.length - 1];
- if (this.resultGenerator_) {
- return this.resultGenerator_(node.key, node.value);
- }
- else {
- return { key: node.key, value: node.value };
- }
- };
- return SortedMapIterator;
- }());
- /**
- * Represents a node in a Left-leaning Red-Black tree.
- */
- var LLRBNode = /** @class */ (function () {
- /**
- * @param key - Key associated with this node.
- * @param value - Value associated with this node.
- * @param color - Whether this node is red.
- * @param left - Left child.
- * @param right - Right child.
- */
- function LLRBNode(key, value, color, left, right) {
- this.key = key;
- this.value = value;
- this.color = color != null ? color : LLRBNode.RED;
- this.left =
- left != null ? left : SortedMap.EMPTY_NODE;
- this.right =
- right != null ? right : SortedMap.EMPTY_NODE;
- }
- /**
- * Returns a copy of the current node, optionally replacing pieces of it.
- *
- * @param key - New key for the node, or null.
- * @param value - New value for the node, or null.
- * @param color - New color for the node, or null.
- * @param left - New left child for the node, or null.
- * @param right - New right child for the node, or null.
- * @returns The node copy.
- */
- LLRBNode.prototype.copy = function (key, value, color, left, right) {
- 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);
- };
- /**
- * @returns The total number of nodes in the tree.
- */
- LLRBNode.prototype.count = function () {
- return this.left.count() + 1 + this.right.count();
- };
- /**
- * @returns True if the tree is empty.
- */
- LLRBNode.prototype.isEmpty = function () {
- return false;
- };
- /**
- * Traverses the tree in key order and calls the specified action function
- * for each node.
- *
- * @param action - Callback function to be called for each
- * node. If it returns true, traversal is aborted.
- * @returns The first truthy value returned by action, or the last falsey
- * value returned by action
- */
- LLRBNode.prototype.inorderTraversal = function (action) {
- return (this.left.inorderTraversal(action) ||
- !!action(this.key, this.value) ||
- this.right.inorderTraversal(action));
- };
- /**
- * Traverses the tree in reverse key order and calls the specified action function
- * for each node.
- *
- * @param action - Callback function to be called for each
- * node. If it returns true, traversal is aborted.
- * @returns True if traversal was aborted.
- */
- LLRBNode.prototype.reverseTraversal = function (action) {
- return (this.right.reverseTraversal(action) ||
- action(this.key, this.value) ||
- this.left.reverseTraversal(action));
- };
- /**
- * @returns The minimum node in the tree.
- */
- LLRBNode.prototype.min_ = function () {
- if (this.left.isEmpty()) {
- return this;
- }
- else {
- return this.left.min_();
- }
- };
- /**
- * @returns The maximum key in the tree.
- */
- LLRBNode.prototype.minKey = function () {
- return this.min_().key;
- };
- /**
- * @returns The maximum key in the tree.
- */
- LLRBNode.prototype.maxKey = function () {
- if (this.right.isEmpty()) {
- return this.key;
- }
- else {
- return this.right.maxKey();
- }
- };
- /**
- * @param key - Key to insert.
- * @param value - Value to insert.
- * @param comparator - Comparator.
- * @returns New tree, with the key/value added.
- */
- LLRBNode.prototype.insert = function (key, value, comparator) {
- var n = this;
- var cmp = comparator(key, n.key);
- if (cmp < 0) {
- n = n.copy(null, null, null, n.left.insert(key, value, comparator), null);
- }
- else if (cmp === 0) {
- n = n.copy(null, value, null, null, null);
- }
- else {
- n = n.copy(null, null, null, null, n.right.insert(key, value, comparator));
- }
- return n.fixUp_();
- };
- /**
- * @returns New tree, with the minimum key removed.
- */
- LLRBNode.prototype.removeMin_ = function () {
- if (this.left.isEmpty()) {
- return SortedMap.EMPTY_NODE;
- }
- var n = this;
- if (!n.left.isRed_() && !n.left.left.isRed_()) {
- n = n.moveRedLeft_();
- }
- n = n.copy(null, null, null, n.left.removeMin_(), null);
- return n.fixUp_();
- };
- /**
- * @param key - The key of the item to remove.
- * @param comparator - Comparator.
- * @returns New tree, with the specified item removed.
- */
- LLRBNode.prototype.remove = function (key, comparator) {
- var n, smallest;
- n = this;
- if (comparator(key, n.key) < 0) {
- if (!n.left.isEmpty() && !n.left.isRed_() && !n.left.left.isRed_()) {
- n = n.moveRedLeft_();
- }
- n = n.copy(null, null, null, n.left.remove(key, comparator), null);
- }
- else {
- if (n.left.isRed_()) {
- n = n.rotateRight_();
- }
- if (!n.right.isEmpty() && !n.right.isRed_() && !n.right.left.isRed_()) {
- n = n.moveRedRight_();
- }
- if (comparator(key, n.key) === 0) {
- if (n.right.isEmpty()) {
- return SortedMap.EMPTY_NODE;
- }
- else {
- smallest = n.right.min_();
- n = n.copy(smallest.key, smallest.value, null, null, n.right.removeMin_());
- }
- }
- n = n.copy(null, null, null, null, n.right.remove(key, comparator));
- }
- return n.fixUp_();
- };
- /**
- * @returns Whether this is a RED node.
- */
- LLRBNode.prototype.isRed_ = function () {
- return this.color;
- };
- /**
- * @returns New tree after performing any needed rotations.
- */
- LLRBNode.prototype.fixUp_ = function () {
- var n = this;
- if (n.right.isRed_() && !n.left.isRed_()) {
- n = n.rotateLeft_();
- }
- if (n.left.isRed_() && n.left.left.isRed_()) {
- n = n.rotateRight_();
- }
- if (n.left.isRed_() && n.right.isRed_()) {
- n = n.colorFlip_();
- }
- return n;
- };
- /**
- * @returns New tree, after moveRedLeft.
- */
- LLRBNode.prototype.moveRedLeft_ = function () {
- var n = this.colorFlip_();
- if (n.right.left.isRed_()) {
- n = n.copy(null, null, null, null, n.right.rotateRight_());
- n = n.rotateLeft_();
- n = n.colorFlip_();
- }
- return n;
- };
- /**
- * @returns New tree, after moveRedRight.
- */
- LLRBNode.prototype.moveRedRight_ = function () {
- var n = this.colorFlip_();
- if (n.left.left.isRed_()) {
- n = n.rotateRight_();
- n = n.colorFlip_();
- }
- return n;
- };
- /**
- * @returns New tree, after rotateLeft.
- */
- LLRBNode.prototype.rotateLeft_ = function () {
- var nl = this.copy(null, null, LLRBNode.RED, null, this.right.left);
- return this.right.copy(null, null, this.color, nl, null);
- };
- /**
- * @returns New tree, after rotateRight.
- */
- LLRBNode.prototype.rotateRight_ = function () {
- var nr = this.copy(null, null, LLRBNode.RED, this.left.right, null);
- return this.left.copy(null, null, this.color, null, nr);
- };
- /**
- * @returns Newt ree, after colorFlip.
- */
- LLRBNode.prototype.colorFlip_ = function () {
- var left = this.left.copy(null, null, !this.left.color, null, null);
- var right = this.right.copy(null, null, !this.right.color, null, null);
- return this.copy(null, null, !this.color, left, right);
- };
- /**
- * For testing.
- *
- * @returns True if all is well.
- */
- LLRBNode.prototype.checkMaxDepth_ = function () {
- var blackDepth = this.check_();
- return Math.pow(2.0, blackDepth) <= this.count() + 1;
- };
- LLRBNode.prototype.check_ = function () {
- if (this.isRed_() && this.left.isRed_()) {
- throw new Error('Red node has red child(' + this.key + ',' + this.value + ')');
- }
- if (this.right.isRed_()) {
- throw new Error('Right child of (' + this.key + ',' + this.value + ') is red');
- }
- var blackDepth = this.left.check_();
- if (blackDepth !== this.right.check_()) {
- throw new Error('Black depths differ');
- }
- else {
- return blackDepth + (this.isRed_() ? 0 : 1);
- }
- };
- LLRBNode.RED = true;
- LLRBNode.BLACK = false;
- return LLRBNode;
- }());
- /**
- * Represents an empty node (a leaf node in the Red-Black Tree).
- */
- var LLRBEmptyNode = /** @class */ (function () {
- function LLRBEmptyNode() {
- }
- /**
- * Returns a copy of the current node.
- *
- * @returns The node copy.
- */
- LLRBEmptyNode.prototype.copy = function (key, value, color, left, right) {
- return this;
- };
- /**
- * Returns a copy of the tree, with the specified key/value added.
- *
- * @param key - Key to be added.
- * @param value - Value to be added.
- * @param comparator - Comparator.
- * @returns New tree, with item added.
- */
- LLRBEmptyNode.prototype.insert = function (key, value, comparator) {
- return new LLRBNode(key, value, null);
- };
- /**
- * Returns a copy of the tree, with the specified key removed.
- *
- * @param key - The key to remove.
- * @param comparator - Comparator.
- * @returns New tree, with item removed.
- */
- LLRBEmptyNode.prototype.remove = function (key, comparator) {
- return this;
- };
- /**
- * @returns The total number of nodes in the tree.
- */
- LLRBEmptyNode.prototype.count = function () {
- return 0;
- };
- /**
- * @returns True if the tree is empty.
- */
- LLRBEmptyNode.prototype.isEmpty = function () {
- return true;
- };
- /**
- * Traverses the tree in key order and calls the specified action function
- * for each node.
- *
- * @param action - Callback function to be called for each
- * node. If it returns true, traversal is aborted.
- * @returns True if traversal was aborted.
- */
- LLRBEmptyNode.prototype.inorderTraversal = function (action) {
- return false;
- };
- /**
- * Traverses the tree in reverse key order and calls the specified action function
- * for each node.
- *
- * @param action - Callback function to be called for each
- * node. If it returns true, traversal is aborted.
- * @returns True if traversal was aborted.
- */
- LLRBEmptyNode.prototype.reverseTraversal = function (action) {
- return false;
- };
- LLRBEmptyNode.prototype.minKey = function () {
- return null;
- };
- LLRBEmptyNode.prototype.maxKey = function () {
- return null;
- };
- LLRBEmptyNode.prototype.check_ = function () {
- return 0;
- };
- /**
- * @returns Whether this node is red.
- */
- LLRBEmptyNode.prototype.isRed_ = function () {
- return false;
- };
- return LLRBEmptyNode;
- }());
- /**
- * An immutable sorted map implementation, based on a Left-leaning Red-Black
- * tree.
- */
- var SortedMap = /** @class */ (function () {
- /**
- * @param comparator_ - Key comparator.
- * @param root_ - Optional root node for the map.
- */
- function SortedMap(comparator_, root_) {
- if (root_ === void 0) { root_ = SortedMap.EMPTY_NODE; }
- this.comparator_ = comparator_;
- this.root_ = root_;
- }
- /**
- * Returns a copy of the map, with the specified key/value added or replaced.
- * (TODO: We should perhaps rename this method to 'put')
- *
- * @param key - Key to be added.
- * @param value - Value to be added.
- * @returns New map, with item added.
- */
- SortedMap.prototype.insert = function (key, value) {
- return new SortedMap(this.comparator_, this.root_
- .insert(key, value, this.comparator_)
- .copy(null, null, LLRBNode.BLACK, null, null));
- };
- /**
- * Returns a copy of the map, with the specified key removed.
- *
- * @param key - The key to remove.
- * @returns New map, with item removed.
- */
- SortedMap.prototype.remove = function (key) {
- return new SortedMap(this.comparator_, this.root_
- .remove(key, this.comparator_)
- .copy(null, null, LLRBNode.BLACK, null, null));
- };
- /**
- * Returns the value of the node with the given key, or null.
- *
- * @param key - The key to look up.
- * @returns The value of the node with the given key, or null if the
- * key doesn't exist.
- */
- SortedMap.prototype.get = function (key) {
- var cmp;
- var node = this.root_;
- while (!node.isEmpty()) {
- cmp = this.comparator_(key, node.key);
- if (cmp === 0) {
- return node.value;
- }
- else if (cmp < 0) {
- node = node.left;
- }
- else if (cmp > 0) {
- node = node.right;
- }
- }
- return null;
- };
- /**
- * Returns the key of the item *before* the specified key, or null if key is the first item.
- * @param key - The key to find the predecessor of
- * @returns The predecessor key.
- */
- SortedMap.prototype.getPredecessorKey = function (key) {
- var cmp, node = this.root_, rightParent = null;
- while (!node.isEmpty()) {
- cmp = this.comparator_(key, node.key);
- if (cmp === 0) {
- if (!node.left.isEmpty()) {
- node = node.left;
- while (!node.right.isEmpty()) {
- node = node.right;
- }
- return node.key;
- }
- else if (rightParent) {
- return rightParent.key;
- }
- else {
- return null; // first item.
- }
- }
- else if (cmp < 0) {
- node = node.left;
- }
- else if (cmp > 0) {
- rightParent = node;
- node = node.right;
- }
- }
- throw new Error('Attempted to find predecessor key for a nonexistent key. What gives?');
- };
- /**
- * @returns True if the map is empty.
- */
- SortedMap.prototype.isEmpty = function () {
- return this.root_.isEmpty();
- };
- /**
- * @returns The total number of nodes in the map.
- */
- SortedMap.prototype.count = function () {
- return this.root_.count();
- };
- /**
- * @returns The minimum key in the map.
- */
- SortedMap.prototype.minKey = function () {
- return this.root_.minKey();
- };
- /**
- * @returns The maximum key in the map.
- */
- SortedMap.prototype.maxKey = function () {
- return this.root_.maxKey();
- };
- /**
- * Traverses the map in key order and calls the specified action function
- * for each key/value pair.
- *
- * @param action - Callback function to be called
- * for each key/value pair. If action returns true, traversal is aborted.
- * @returns The first truthy value returned by action, or the last falsey
- * value returned by action
- */
- SortedMap.prototype.inorderTraversal = function (action) {
- return this.root_.inorderTraversal(action);
- };
- /**
- * Traverses the map in reverse key order and calls the specified action function
- * for each key/value pair.
- *
- * @param action - Callback function to be called
- * for each key/value pair. If action returns true, traversal is aborted.
- * @returns True if the traversal was aborted.
- */
- SortedMap.prototype.reverseTraversal = function (action) {
- return this.root_.reverseTraversal(action);
- };
- /**
- * Returns an iterator over the SortedMap.
- * @returns The iterator.
- */
- SortedMap.prototype.getIterator = function (resultGenerator) {
- return new SortedMapIterator(this.root_, null, this.comparator_, false, resultGenerator);
- };
- SortedMap.prototype.getIteratorFrom = function (key, resultGenerator) {
- return new SortedMapIterator(this.root_, key, this.comparator_, false, resultGenerator);
- };
- SortedMap.prototype.getReverseIteratorFrom = function (key, resultGenerator) {
- return new SortedMapIterator(this.root_, key, this.comparator_, true, resultGenerator);
- };
- SortedMap.prototype.getReverseIterator = function (resultGenerator) {
- return new SortedMapIterator(this.root_, null, this.comparator_, true, resultGenerator);
- };
- /**
- * Always use the same empty node, to reduce memory.
- */
- SortedMap.EMPTY_NODE = new LLRBEmptyNode();
- return SortedMap;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function NAME_ONLY_COMPARATOR(left, right) {
- return nameCompare(left.name, right.name);
- }
- function NAME_COMPARATOR(left, right) {
- return nameCompare(left, right);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var MAX_NODE$2;
- function setMaxNode$1(val) {
- MAX_NODE$2 = val;
- }
- var priorityHashText = function (priority) {
- if (typeof priority === 'number') {
- return 'number:' + doubleToIEEE754String(priority);
- }
- else {
- return 'string:' + priority;
- }
- };
- /**
- * Validates that a priority snapshot Node is valid.
- */
- var validatePriorityNode = function (priorityNode) {
- if (priorityNode.isLeafNode()) {
- var val = priorityNode.val();
- util.assert(typeof val === 'string' ||
- typeof val === 'number' ||
- (typeof val === 'object' && util.contains(val, '.sv')), 'Priority must be a string or number.');
- }
- else {
- util.assert(priorityNode === MAX_NODE$2 || priorityNode.isEmpty(), 'priority of unexpected type.');
- }
- // Don't call getPriority() on MAX_NODE to avoid hitting assertion.
- util.assert(priorityNode === MAX_NODE$2 || priorityNode.getPriority().isEmpty(), "Priority nodes can't have a priority of their own.");
- };
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var __childrenNodeConstructor;
- /**
- * LeafNode is a class for storing leaf nodes in a DataSnapshot. It
- * implements Node and stores the value of the node (a string,
- * number, or boolean) accessible via getValue().
- */
- var LeafNode = /** @class */ (function () {
- /**
- * @param value_ - The value to store in this leaf node. The object type is
- * possible in the event of a deferred value
- * @param priorityNode_ - The priority of this node.
- */
- function LeafNode(value_, priorityNode_) {
- if (priorityNode_ === void 0) { priorityNode_ = LeafNode.__childrenNodeConstructor.EMPTY_NODE; }
- this.value_ = value_;
- this.priorityNode_ = priorityNode_;
- this.lazyHash_ = null;
- util.assert(this.value_ !== undefined && this.value_ !== null, "LeafNode shouldn't be created with null/undefined value.");
- validatePriorityNode(this.priorityNode_);
- }
- Object.defineProperty(LeafNode, "__childrenNodeConstructor", {
- get: function () {
- return __childrenNodeConstructor;
- },
- set: function (val) {
- __childrenNodeConstructor = val;
- },
- enumerable: false,
- configurable: true
- });
- /** @inheritDoc */
- LeafNode.prototype.isLeafNode = function () {
- return true;
- };
- /** @inheritDoc */
- LeafNode.prototype.getPriority = function () {
- return this.priorityNode_;
- };
- /** @inheritDoc */
- LeafNode.prototype.updatePriority = function (newPriorityNode) {
- return new LeafNode(this.value_, newPriorityNode);
- };
- /** @inheritDoc */
- LeafNode.prototype.getImmediateChild = function (childName) {
- // Hack to treat priority as a regular child
- if (childName === '.priority') {
- return this.priorityNode_;
- }
- else {
- return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
- }
- };
- /** @inheritDoc */
- LeafNode.prototype.getChild = function (path) {
- if (pathIsEmpty(path)) {
- return this;
- }
- else if (pathGetFront(path) === '.priority') {
- return this.priorityNode_;
- }
- else {
- return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
- }
- };
- LeafNode.prototype.hasChild = function () {
- return false;
- };
- /** @inheritDoc */
- LeafNode.prototype.getPredecessorChildName = function (childName, childNode) {
- return null;
- };
- /** @inheritDoc */
- LeafNode.prototype.updateImmediateChild = function (childName, newChildNode) {
- if (childName === '.priority') {
- return this.updatePriority(newChildNode);
- }
- else if (newChildNode.isEmpty() && childName !== '.priority') {
- return this;
- }
- else {
- return LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateImmediateChild(childName, newChildNode).updatePriority(this.priorityNode_);
- }
- };
- /** @inheritDoc */
- LeafNode.prototype.updateChild = function (path, newChildNode) {
- var front = pathGetFront(path);
- if (front === null) {
- return newChildNode;
- }
- else if (newChildNode.isEmpty() && front !== '.priority') {
- return this;
- }
- else {
- util.assert(front !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
- return this.updateImmediateChild(front, LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateChild(pathPopFront(path), newChildNode));
- }
- };
- /** @inheritDoc */
- LeafNode.prototype.isEmpty = function () {
- return false;
- };
- /** @inheritDoc */
- LeafNode.prototype.numChildren = function () {
- return 0;
- };
- /** @inheritDoc */
- LeafNode.prototype.forEachChild = function (index, action) {
- return false;
- };
- LeafNode.prototype.val = function (exportFormat) {
- if (exportFormat && !this.getPriority().isEmpty()) {
- return {
- '.value': this.getValue(),
- '.priority': this.getPriority().val()
- };
- }
- else {
- return this.getValue();
- }
- };
- /** @inheritDoc */
- LeafNode.prototype.hash = function () {
- if (this.lazyHash_ === null) {
- var toHash = '';
- if (!this.priorityNode_.isEmpty()) {
- toHash +=
- 'priority:' +
- priorityHashText(this.priorityNode_.val()) +
- ':';
- }
- var type = typeof this.value_;
- toHash += type + ':';
- if (type === 'number') {
- toHash += doubleToIEEE754String(this.value_);
- }
- else {
- toHash += this.value_;
- }
- this.lazyHash_ = sha1(toHash);
- }
- return this.lazyHash_;
- };
- /**
- * Returns the value of the leaf node.
- * @returns The value of the node.
- */
- LeafNode.prototype.getValue = function () {
- return this.value_;
- };
- LeafNode.prototype.compareTo = function (other) {
- if (other === LeafNode.__childrenNodeConstructor.EMPTY_NODE) {
- return 1;
- }
- else if (other instanceof LeafNode.__childrenNodeConstructor) {
- return -1;
- }
- else {
- util.assert(other.isLeafNode(), 'Unknown node type');
- return this.compareToLeafNode_(other);
- }
- };
- /**
- * Comparison specifically for two leaf nodes
- */
- LeafNode.prototype.compareToLeafNode_ = function (otherLeaf) {
- var otherLeafType = typeof otherLeaf.value_;
- var thisLeafType = typeof this.value_;
- var otherIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(otherLeafType);
- var thisIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(thisLeafType);
- util.assert(otherIndex >= 0, 'Unknown leaf type: ' + otherLeafType);
- util.assert(thisIndex >= 0, 'Unknown leaf type: ' + thisLeafType);
- if (otherIndex === thisIndex) {
- // Same type, compare values
- if (thisLeafType === 'object') {
- // Deferred value nodes are all equal, but we should also never get to this point...
- return 0;
- }
- else {
- // Note that this works because true > false, all others are number or string comparisons
- if (this.value_ < otherLeaf.value_) {
- return -1;
- }
- else if (this.value_ === otherLeaf.value_) {
- return 0;
- }
- else {
- return 1;
- }
- }
- }
- else {
- return thisIndex - otherIndex;
- }
- };
- LeafNode.prototype.withIndex = function () {
- return this;
- };
- LeafNode.prototype.isIndexed = function () {
- return true;
- };
- LeafNode.prototype.equals = function (other) {
- if (other === this) {
- return true;
- }
- else if (other.isLeafNode()) {
- var otherLeaf = other;
- return (this.value_ === otherLeaf.value_ &&
- this.priorityNode_.equals(otherLeaf.priorityNode_));
- }
- else {
- return false;
- }
- };
- /**
- * The sort order for comparing leaf nodes of different types. If two leaf nodes have
- * the same type, the comparison falls back to their value
- */
- LeafNode.VALUE_TYPE_ORDER = ['object', 'boolean', 'number', 'string'];
- return LeafNode;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var nodeFromJSON$1;
- var MAX_NODE$1;
- function setNodeFromJSON(val) {
- nodeFromJSON$1 = val;
- }
- function setMaxNode(val) {
- MAX_NODE$1 = val;
- }
- var PriorityIndex = /** @class */ (function (_super) {
- tslib.__extends(PriorityIndex, _super);
- function PriorityIndex() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- PriorityIndex.prototype.compare = function (a, b) {
- var aPriority = a.node.getPriority();
- var bPriority = b.node.getPriority();
- var indexCmp = aPriority.compareTo(bPriority);
- if (indexCmp === 0) {
- return nameCompare(a.name, b.name);
- }
- else {
- return indexCmp;
- }
- };
- PriorityIndex.prototype.isDefinedOn = function (node) {
- return !node.getPriority().isEmpty();
- };
- PriorityIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
- return !oldNode.getPriority().equals(newNode.getPriority());
- };
- PriorityIndex.prototype.minPost = function () {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return NamedNode.MIN;
- };
- PriorityIndex.prototype.maxPost = function () {
- return new NamedNode(MAX_NAME, new LeafNode('[PRIORITY-POST]', MAX_NODE$1));
- };
- PriorityIndex.prototype.makePost = function (indexValue, name) {
- var priorityNode = nodeFromJSON$1(indexValue);
- return new NamedNode(name, new LeafNode('[PRIORITY-POST]', priorityNode));
- };
- /**
- * @returns String representation for inclusion in a query spec
- */
- PriorityIndex.prototype.toString = function () {
- return '.priority';
- };
- return PriorityIndex;
- }(Index));
- var PRIORITY_INDEX = new PriorityIndex();
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var LOG_2 = Math.log(2);
- var Base12Num = /** @class */ (function () {
- function Base12Num(length) {
- var logBase2 = function (num) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return parseInt((Math.log(num) / LOG_2), 10);
- };
- var bitMask = function (bits) { return parseInt(Array(bits + 1).join('1'), 2); };
- this.count = logBase2(length + 1);
- this.current_ = this.count - 1;
- var mask = bitMask(this.count);
- this.bits_ = (length + 1) & mask;
- }
- Base12Num.prototype.nextBitIsOne = function () {
- //noinspection JSBitwiseOperatorUsage
- var result = !(this.bits_ & (0x1 << this.current_));
- this.current_--;
- return result;
- };
- return Base12Num;
- }());
- /**
- * Takes a list of child nodes and constructs a SortedSet using the given comparison
- * function
- *
- * Uses the algorithm described in the paper linked here:
- * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.1458
- *
- * @param childList - Unsorted list of children
- * @param cmp - The comparison method to be used
- * @param keyFn - An optional function to extract K from a node wrapper, if K's
- * type is not NamedNode
- * @param mapSortFn - An optional override for comparator used by the generated sorted map
- */
- var buildChildSet = function (childList, cmp, keyFn, mapSortFn) {
- childList.sort(cmp);
- var buildBalancedTree = function (low, high) {
- var length = high - low;
- var namedNode;
- var key;
- if (length === 0) {
- return null;
- }
- else if (length === 1) {
- namedNode = childList[low];
- key = keyFn ? keyFn(namedNode) : namedNode;
- return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, null, null);
- }
- else {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- var middle = parseInt((length / 2), 10) + low;
- var left = buildBalancedTree(low, middle);
- var right = buildBalancedTree(middle + 1, high);
- namedNode = childList[middle];
- key = keyFn ? keyFn(namedNode) : namedNode;
- return new LLRBNode(key, namedNode.node, LLRBNode.BLACK, left, right);
- }
- };
- var buildFrom12Array = function (base12) {
- var node = null;
- var root = null;
- var index = childList.length;
- var buildPennant = function (chunkSize, color) {
- var low = index - chunkSize;
- var high = index;
- index -= chunkSize;
- var childTree = buildBalancedTree(low + 1, high);
- var namedNode = childList[low];
- var key = keyFn ? keyFn(namedNode) : namedNode;
- attachPennant(new LLRBNode(key, namedNode.node, color, null, childTree));
- };
- var attachPennant = function (pennant) {
- if (node) {
- node.left = pennant;
- node = pennant;
- }
- else {
- root = pennant;
- node = pennant;
- }
- };
- for (var i = 0; i < base12.count; ++i) {
- var isOne = base12.nextBitIsOne();
- // The number of nodes taken in each slice is 2^(arr.length - (i + 1))
- var chunkSize = Math.pow(2, base12.count - (i + 1));
- if (isOne) {
- buildPennant(chunkSize, LLRBNode.BLACK);
- }
- else {
- // current == 2
- buildPennant(chunkSize, LLRBNode.BLACK);
- buildPennant(chunkSize, LLRBNode.RED);
- }
- }
- return root;
- };
- var base12 = new Base12Num(childList.length);
- var root = buildFrom12Array(base12);
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return new SortedMap(mapSortFn || cmp, root);
- };
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var _defaultIndexMap;
- var fallbackObject = {};
- var IndexMap = /** @class */ (function () {
- function IndexMap(indexes_, indexSet_) {
- this.indexes_ = indexes_;
- this.indexSet_ = indexSet_;
- }
- Object.defineProperty(IndexMap, "Default", {
- /**
- * The default IndexMap for nodes without a priority
- */
- get: function () {
- util.assert(fallbackObject && PRIORITY_INDEX, 'ChildrenNode.ts has not been loaded');
- _defaultIndexMap =
- _defaultIndexMap ||
- new IndexMap({ '.priority': fallbackObject }, { '.priority': PRIORITY_INDEX });
- return _defaultIndexMap;
- },
- enumerable: false,
- configurable: true
- });
- IndexMap.prototype.get = function (indexKey) {
- var sortedMap = util.safeGet(this.indexes_, indexKey);
- if (!sortedMap) {
- throw new Error('No index defined for ' + indexKey);
- }
- if (sortedMap instanceof SortedMap) {
- return sortedMap;
- }
- else {
- // The index exists, but it falls back to just name comparison. Return null so that the calling code uses the
- // regular child map
- return null;
- }
- };
- IndexMap.prototype.hasIndex = function (indexDefinition) {
- return util.contains(this.indexSet_, indexDefinition.toString());
- };
- IndexMap.prototype.addIndex = function (indexDefinition, existingChildren) {
- util.assert(indexDefinition !== KEY_INDEX, "KeyIndex always exists and isn't meant to be added to the IndexMap.");
- var childList = [];
- var sawIndexedValue = false;
- var iter = existingChildren.getIterator(NamedNode.Wrap);
- var next = iter.getNext();
- while (next) {
- sawIndexedValue =
- sawIndexedValue || indexDefinition.isDefinedOn(next.node);
- childList.push(next);
- next = iter.getNext();
- }
- var newIndex;
- if (sawIndexedValue) {
- newIndex = buildChildSet(childList, indexDefinition.getCompare());
- }
- else {
- newIndex = fallbackObject;
- }
- var indexName = indexDefinition.toString();
- var newIndexSet = tslib.__assign({}, this.indexSet_);
- newIndexSet[indexName] = indexDefinition;
- var newIndexes = tslib.__assign({}, this.indexes_);
- newIndexes[indexName] = newIndex;
- return new IndexMap(newIndexes, newIndexSet);
- };
- /**
- * Ensure that this node is properly tracked in any indexes that we're maintaining
- */
- IndexMap.prototype.addToIndexes = function (namedNode, existingChildren) {
- var _this = this;
- var newIndexes = util.map(this.indexes_, function (indexedChildren, indexName) {
- var index = util.safeGet(_this.indexSet_, indexName);
- util.assert(index, 'Missing index implementation for ' + indexName);
- if (indexedChildren === fallbackObject) {
- // Check to see if we need to index everything
- if (index.isDefinedOn(namedNode.node)) {
- // We need to build this index
- var childList = [];
- var iter = existingChildren.getIterator(NamedNode.Wrap);
- var next = iter.getNext();
- while (next) {
- if (next.name !== namedNode.name) {
- childList.push(next);
- }
- next = iter.getNext();
- }
- childList.push(namedNode);
- return buildChildSet(childList, index.getCompare());
- }
- else {
- // No change, this remains a fallback
- return fallbackObject;
- }
- }
- else {
- var existingSnap = existingChildren.get(namedNode.name);
- var newChildren = indexedChildren;
- if (existingSnap) {
- newChildren = newChildren.remove(new NamedNode(namedNode.name, existingSnap));
- }
- return newChildren.insert(namedNode, namedNode.node);
- }
- });
- return new IndexMap(newIndexes, this.indexSet_);
- };
- /**
- * Create a new IndexMap instance with the given value removed
- */
- IndexMap.prototype.removeFromIndexes = function (namedNode, existingChildren) {
- var newIndexes = util.map(this.indexes_, function (indexedChildren) {
- if (indexedChildren === fallbackObject) {
- // This is the fallback. Just return it, nothing to do in this case
- return indexedChildren;
- }
- else {
- var existingSnap = existingChildren.get(namedNode.name);
- if (existingSnap) {
- return indexedChildren.remove(new NamedNode(namedNode.name, existingSnap));
- }
- else {
- // No record of this child
- return indexedChildren;
- }
- }
- });
- return new IndexMap(newIndexes, this.indexSet_);
- };
- return IndexMap;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // TODO: For memory savings, don't store priorityNode_ if it's empty.
- var EMPTY_NODE;
- /**
- * ChildrenNode is a class for storing internal nodes in a DataSnapshot
- * (i.e. nodes with children). It implements Node and stores the
- * list of children in the children property, sorted by child name.
- */
- var ChildrenNode = /** @class */ (function () {
- /**
- * @param children_ - List of children of this node..
- * @param priorityNode_ - The priority of this node (as a snapshot node).
- */
- function ChildrenNode(children_, priorityNode_, indexMap_) {
- this.children_ = children_;
- this.priorityNode_ = priorityNode_;
- this.indexMap_ = indexMap_;
- this.lazyHash_ = null;
- /**
- * Note: The only reason we allow null priority is for EMPTY_NODE, since we can't use
- * EMPTY_NODE as the priority of EMPTY_NODE. We might want to consider making EMPTY_NODE its own
- * class instead of an empty ChildrenNode.
- */
- if (this.priorityNode_) {
- validatePriorityNode(this.priorityNode_);
- }
- if (this.children_.isEmpty()) {
- util.assert(!this.priorityNode_ || this.priorityNode_.isEmpty(), 'An empty node cannot have a priority');
- }
- }
- Object.defineProperty(ChildrenNode, "EMPTY_NODE", {
- get: function () {
- return (EMPTY_NODE ||
- (EMPTY_NODE = new ChildrenNode(new SortedMap(NAME_COMPARATOR), null, IndexMap.Default)));
- },
- enumerable: false,
- configurable: true
- });
- /** @inheritDoc */
- ChildrenNode.prototype.isLeafNode = function () {
- return false;
- };
- /** @inheritDoc */
- ChildrenNode.prototype.getPriority = function () {
- return this.priorityNode_ || EMPTY_NODE;
- };
- /** @inheritDoc */
- ChildrenNode.prototype.updatePriority = function (newPriorityNode) {
- if (this.children_.isEmpty()) {
- // Don't allow priorities on empty nodes
- return this;
- }
- else {
- return new ChildrenNode(this.children_, newPriorityNode, this.indexMap_);
- }
- };
- /** @inheritDoc */
- ChildrenNode.prototype.getImmediateChild = function (childName) {
- // Hack to treat priority as a regular child
- if (childName === '.priority') {
- return this.getPriority();
- }
- else {
- var child = this.children_.get(childName);
- return child === null ? EMPTY_NODE : child;
- }
- };
- /** @inheritDoc */
- ChildrenNode.prototype.getChild = function (path) {
- var front = pathGetFront(path);
- if (front === null) {
- return this;
- }
- return this.getImmediateChild(front).getChild(pathPopFront(path));
- };
- /** @inheritDoc */
- ChildrenNode.prototype.hasChild = function (childName) {
- return this.children_.get(childName) !== null;
- };
- /** @inheritDoc */
- ChildrenNode.prototype.updateImmediateChild = function (childName, newChildNode) {
- util.assert(newChildNode, 'We should always be passing snapshot nodes');
- if (childName === '.priority') {
- return this.updatePriority(newChildNode);
- }
- else {
- var namedNode = new NamedNode(childName, newChildNode);
- var newChildren = void 0, newIndexMap = void 0;
- if (newChildNode.isEmpty()) {
- newChildren = this.children_.remove(childName);
- newIndexMap = this.indexMap_.removeFromIndexes(namedNode, this.children_);
- }
- else {
- newChildren = this.children_.insert(childName, newChildNode);
- newIndexMap = this.indexMap_.addToIndexes(namedNode, this.children_);
- }
- var newPriority = newChildren.isEmpty()
- ? EMPTY_NODE
- : this.priorityNode_;
- return new ChildrenNode(newChildren, newPriority, newIndexMap);
- }
- };
- /** @inheritDoc */
- ChildrenNode.prototype.updateChild = function (path, newChildNode) {
- var front = pathGetFront(path);
- if (front === null) {
- return newChildNode;
- }
- else {
- util.assert(pathGetFront(path) !== '.priority' || pathGetLength(path) === 1, '.priority must be the last token in a path');
- var newImmediateChild = this.getImmediateChild(front).updateChild(pathPopFront(path), newChildNode);
- return this.updateImmediateChild(front, newImmediateChild);
- }
- };
- /** @inheritDoc */
- ChildrenNode.prototype.isEmpty = function () {
- return this.children_.isEmpty();
- };
- /** @inheritDoc */
- ChildrenNode.prototype.numChildren = function () {
- return this.children_.count();
- };
- /** @inheritDoc */
- ChildrenNode.prototype.val = function (exportFormat) {
- if (this.isEmpty()) {
- return null;
- }
- var obj = {};
- var numKeys = 0, maxKey = 0, allIntegerKeys = true;
- this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
- obj[key] = childNode.val(exportFormat);
- numKeys++;
- if (allIntegerKeys && ChildrenNode.INTEGER_REGEXP_.test(key)) {
- maxKey = Math.max(maxKey, Number(key));
- }
- else {
- allIntegerKeys = false;
- }
- });
- if (!exportFormat && allIntegerKeys && maxKey < 2 * numKeys) {
- // convert to array.
- var array = [];
- // eslint-disable-next-line guard-for-in
- for (var key in obj) {
- array[key] = obj[key];
- }
- return array;
- }
- else {
- if (exportFormat && !this.getPriority().isEmpty()) {
- obj['.priority'] = this.getPriority().val();
- }
- return obj;
- }
- };
- /** @inheritDoc */
- ChildrenNode.prototype.hash = function () {
- if (this.lazyHash_ === null) {
- var toHash_1 = '';
- if (!this.getPriority().isEmpty()) {
- toHash_1 +=
- 'priority:' +
- priorityHashText(this.getPriority().val()) +
- ':';
- }
- this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
- var childHash = childNode.hash();
- if (childHash !== '') {
- toHash_1 += ':' + key + ':' + childHash;
- }
- });
- this.lazyHash_ = toHash_1 === '' ? '' : sha1(toHash_1);
- }
- return this.lazyHash_;
- };
- /** @inheritDoc */
- ChildrenNode.prototype.getPredecessorChildName = function (childName, childNode, index) {
- var idx = this.resolveIndex_(index);
- if (idx) {
- var predecessor = idx.getPredecessorKey(new NamedNode(childName, childNode));
- return predecessor ? predecessor.name : null;
- }
- else {
- return this.children_.getPredecessorKey(childName);
- }
- };
- ChildrenNode.prototype.getFirstChildName = function (indexDefinition) {
- var idx = this.resolveIndex_(indexDefinition);
- if (idx) {
- var minKey = idx.minKey();
- return minKey && minKey.name;
- }
- else {
- return this.children_.minKey();
- }
- };
- ChildrenNode.prototype.getFirstChild = function (indexDefinition) {
- var minKey = this.getFirstChildName(indexDefinition);
- if (minKey) {
- return new NamedNode(minKey, this.children_.get(minKey));
- }
- else {
- return null;
- }
- };
- /**
- * Given an index, return the key name of the largest value we have, according to that index
- */
- ChildrenNode.prototype.getLastChildName = function (indexDefinition) {
- var idx = this.resolveIndex_(indexDefinition);
- if (idx) {
- var maxKey = idx.maxKey();
- return maxKey && maxKey.name;
- }
- else {
- return this.children_.maxKey();
- }
- };
- ChildrenNode.prototype.getLastChild = function (indexDefinition) {
- var maxKey = this.getLastChildName(indexDefinition);
- if (maxKey) {
- return new NamedNode(maxKey, this.children_.get(maxKey));
- }
- else {
- return null;
- }
- };
- ChildrenNode.prototype.forEachChild = function (index, action) {
- var idx = this.resolveIndex_(index);
- if (idx) {
- return idx.inorderTraversal(function (wrappedNode) {
- return action(wrappedNode.name, wrappedNode.node);
- });
- }
- else {
- return this.children_.inorderTraversal(action);
- }
- };
- ChildrenNode.prototype.getIterator = function (indexDefinition) {
- return this.getIteratorFrom(indexDefinition.minPost(), indexDefinition);
- };
- ChildrenNode.prototype.getIteratorFrom = function (startPost, indexDefinition) {
- var idx = this.resolveIndex_(indexDefinition);
- if (idx) {
- return idx.getIteratorFrom(startPost, function (key) { return key; });
- }
- else {
- var iterator = this.children_.getIteratorFrom(startPost.name, NamedNode.Wrap);
- var next = iterator.peek();
- while (next != null && indexDefinition.compare(next, startPost) < 0) {
- iterator.getNext();
- next = iterator.peek();
- }
- return iterator;
- }
- };
- ChildrenNode.prototype.getReverseIterator = function (indexDefinition) {
- return this.getReverseIteratorFrom(indexDefinition.maxPost(), indexDefinition);
- };
- ChildrenNode.prototype.getReverseIteratorFrom = function (endPost, indexDefinition) {
- var idx = this.resolveIndex_(indexDefinition);
- if (idx) {
- return idx.getReverseIteratorFrom(endPost, function (key) {
- return key;
- });
- }
- else {
- var iterator = this.children_.getReverseIteratorFrom(endPost.name, NamedNode.Wrap);
- var next = iterator.peek();
- while (next != null && indexDefinition.compare(next, endPost) > 0) {
- iterator.getNext();
- next = iterator.peek();
- }
- return iterator;
- }
- };
- ChildrenNode.prototype.compareTo = function (other) {
- if (this.isEmpty()) {
- if (other.isEmpty()) {
- return 0;
- }
- else {
- return -1;
- }
- }
- else if (other.isLeafNode() || other.isEmpty()) {
- return 1;
- }
- else if (other === MAX_NODE) {
- return -1;
- }
- else {
- // Must be another node with children.
- return 0;
- }
- };
- ChildrenNode.prototype.withIndex = function (indexDefinition) {
- if (indexDefinition === KEY_INDEX ||
- this.indexMap_.hasIndex(indexDefinition)) {
- return this;
- }
- else {
- var newIndexMap = this.indexMap_.addIndex(indexDefinition, this.children_);
- return new ChildrenNode(this.children_, this.priorityNode_, newIndexMap);
- }
- };
- ChildrenNode.prototype.isIndexed = function (index) {
- return index === KEY_INDEX || this.indexMap_.hasIndex(index);
- };
- ChildrenNode.prototype.equals = function (other) {
- if (other === this) {
- return true;
- }
- else if (other.isLeafNode()) {
- return false;
- }
- else {
- var otherChildrenNode = other;
- if (!this.getPriority().equals(otherChildrenNode.getPriority())) {
- return false;
- }
- else if (this.children_.count() === otherChildrenNode.children_.count()) {
- var thisIter = this.getIterator(PRIORITY_INDEX);
- var otherIter = otherChildrenNode.getIterator(PRIORITY_INDEX);
- var thisCurrent = thisIter.getNext();
- var otherCurrent = otherIter.getNext();
- while (thisCurrent && otherCurrent) {
- if (thisCurrent.name !== otherCurrent.name ||
- !thisCurrent.node.equals(otherCurrent.node)) {
- return false;
- }
- thisCurrent = thisIter.getNext();
- otherCurrent = otherIter.getNext();
- }
- return thisCurrent === null && otherCurrent === null;
- }
- else {
- return false;
- }
- }
- };
- /**
- * Returns a SortedMap ordered by index, or null if the default (by-key) ordering can be used
- * instead.
- *
- */
- ChildrenNode.prototype.resolveIndex_ = function (indexDefinition) {
- if (indexDefinition === KEY_INDEX) {
- return null;
- }
- else {
- return this.indexMap_.get(indexDefinition.toString());
- }
- };
- ChildrenNode.INTEGER_REGEXP_ = /^(0|[1-9]\d*)$/;
- return ChildrenNode;
- }());
- var MaxNode = /** @class */ (function (_super) {
- tslib.__extends(MaxNode, _super);
- function MaxNode() {
- return _super.call(this, new SortedMap(NAME_COMPARATOR), ChildrenNode.EMPTY_NODE, IndexMap.Default) || this;
- }
- MaxNode.prototype.compareTo = function (other) {
- if (other === this) {
- return 0;
- }
- else {
- return 1;
- }
- };
- MaxNode.prototype.equals = function (other) {
- // Not that we every compare it, but MAX_NODE is only ever equal to itself
- return other === this;
- };
- MaxNode.prototype.getPriority = function () {
- return this;
- };
- MaxNode.prototype.getImmediateChild = function (childName) {
- return ChildrenNode.EMPTY_NODE;
- };
- MaxNode.prototype.isEmpty = function () {
- return false;
- };
- return MaxNode;
- }(ChildrenNode));
- /**
- * Marker that will sort higher than any other snapshot.
- */
- var MAX_NODE = new MaxNode();
- Object.defineProperties(NamedNode, {
- MIN: {
- value: new NamedNode(MIN_NAME, ChildrenNode.EMPTY_NODE)
- },
- MAX: {
- value: new NamedNode(MAX_NAME, MAX_NODE)
- }
- });
- /**
- * Reference Extensions
- */
- KeyIndex.__EMPTY_NODE = ChildrenNode.EMPTY_NODE;
- LeafNode.__childrenNodeConstructor = ChildrenNode;
- setMaxNode$1(MAX_NODE);
- setMaxNode(MAX_NODE);
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var USE_HINZE = true;
- /**
- * Constructs a snapshot node representing the passed JSON and returns it.
- * @param json - JSON to create a node for.
- * @param priority - Optional priority to use. This will be ignored if the
- * passed JSON contains a .priority property.
- */
- function nodeFromJSON(json, priority) {
- if (priority === void 0) { priority = null; }
- if (json === null) {
- return ChildrenNode.EMPTY_NODE;
- }
- if (typeof json === 'object' && '.priority' in json) {
- priority = json['.priority'];
- }
- util.assert(priority === null ||
- typeof priority === 'string' ||
- typeof priority === 'number' ||
- (typeof priority === 'object' && '.sv' in priority), 'Invalid priority type found: ' + typeof priority);
- if (typeof json === 'object' && '.value' in json && json['.value'] !== null) {
- json = json['.value'];
- }
- // Valid leaf nodes include non-objects or server-value wrapper objects
- if (typeof json !== 'object' || '.sv' in json) {
- var jsonLeaf = json;
- return new LeafNode(jsonLeaf, nodeFromJSON(priority));
- }
- if (!(json instanceof Array) && USE_HINZE) {
- var children_1 = [];
- var childrenHavePriority_1 = false;
- var hinzeJsonObj = json;
- each(hinzeJsonObj, function (key, child) {
- if (key.substring(0, 1) !== '.') {
- // Ignore metadata nodes
- var childNode = nodeFromJSON(child);
- if (!childNode.isEmpty()) {
- childrenHavePriority_1 =
- childrenHavePriority_1 || !childNode.getPriority().isEmpty();
- children_1.push(new NamedNode(key, childNode));
- }
- }
- });
- if (children_1.length === 0) {
- return ChildrenNode.EMPTY_NODE;
- }
- var childSet = buildChildSet(children_1, NAME_ONLY_COMPARATOR, function (namedNode) { return namedNode.name; }, NAME_COMPARATOR);
- if (childrenHavePriority_1) {
- var sortedChildSet = buildChildSet(children_1, PRIORITY_INDEX.getCompare());
- return new ChildrenNode(childSet, nodeFromJSON(priority), new IndexMap({ '.priority': sortedChildSet }, { '.priority': PRIORITY_INDEX }));
- }
- else {
- return new ChildrenNode(childSet, nodeFromJSON(priority), IndexMap.Default);
- }
- }
- else {
- var node_1 = ChildrenNode.EMPTY_NODE;
- each(json, function (key, childData) {
- if (util.contains(json, key)) {
- if (key.substring(0, 1) !== '.') {
- // ignore metadata nodes.
- var childNode = nodeFromJSON(childData);
- if (childNode.isLeafNode() || !childNode.isEmpty()) {
- node_1 = node_1.updateImmediateChild(key, childNode);
- }
- }
- }
- });
- return node_1.updatePriority(nodeFromJSON(priority));
- }
- }
- setNodeFromJSON(nodeFromJSON);
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var PathIndex = /** @class */ (function (_super) {
- tslib.__extends(PathIndex, _super);
- function PathIndex(indexPath_) {
- var _this = _super.call(this) || this;
- _this.indexPath_ = indexPath_;
- util.assert(!pathIsEmpty(indexPath_) && pathGetFront(indexPath_) !== '.priority', "Can't create PathIndex with empty path or .priority key");
- return _this;
- }
- PathIndex.prototype.extractChild = function (snap) {
- return snap.getChild(this.indexPath_);
- };
- PathIndex.prototype.isDefinedOn = function (node) {
- return !node.getChild(this.indexPath_).isEmpty();
- };
- PathIndex.prototype.compare = function (a, b) {
- var aChild = this.extractChild(a.node);
- var bChild = this.extractChild(b.node);
- var indexCmp = aChild.compareTo(bChild);
- if (indexCmp === 0) {
- return nameCompare(a.name, b.name);
- }
- else {
- return indexCmp;
- }
- };
- PathIndex.prototype.makePost = function (indexValue, name) {
- var valueNode = nodeFromJSON(indexValue);
- var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, valueNode);
- return new NamedNode(name, node);
- };
- PathIndex.prototype.maxPost = function () {
- var node = ChildrenNode.EMPTY_NODE.updateChild(this.indexPath_, MAX_NODE);
- return new NamedNode(MAX_NAME, node);
- };
- PathIndex.prototype.toString = function () {
- return pathSlice(this.indexPath_, 0).join('/');
- };
- return PathIndex;
- }(Index));
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var ValueIndex = /** @class */ (function (_super) {
- tslib.__extends(ValueIndex, _super);
- function ValueIndex() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- ValueIndex.prototype.compare = function (a, b) {
- var indexCmp = a.node.compareTo(b.node);
- if (indexCmp === 0) {
- return nameCompare(a.name, b.name);
- }
- else {
- return indexCmp;
- }
- };
- ValueIndex.prototype.isDefinedOn = function (node) {
- return true;
- };
- ValueIndex.prototype.indexedValueChanged = function (oldNode, newNode) {
- return !oldNode.equals(newNode);
- };
- ValueIndex.prototype.minPost = function () {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return NamedNode.MIN;
- };
- ValueIndex.prototype.maxPost = function () {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return NamedNode.MAX;
- };
- ValueIndex.prototype.makePost = function (indexValue, name) {
- var valueNode = nodeFromJSON(indexValue);
- return new NamedNode(name, valueNode);
- };
- /**
- * @returns String representation for inclusion in a query spec
- */
- ValueIndex.prototype.toString = function () {
- return '.value';
- };
- return ValueIndex;
- }(Index));
- var VALUE_INDEX = new ValueIndex();
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function changeValue(snapshotNode) {
- return { type: "value" /* ChangeType.VALUE */, snapshotNode: snapshotNode };
- }
- function changeChildAdded(childName, snapshotNode) {
- return { type: "child_added" /* ChangeType.CHILD_ADDED */, snapshotNode: snapshotNode, childName: childName };
- }
- function changeChildRemoved(childName, snapshotNode) {
- return { type: "child_removed" /* ChangeType.CHILD_REMOVED */, snapshotNode: snapshotNode, childName: childName };
- }
- function changeChildChanged(childName, snapshotNode, oldSnap) {
- return {
- type: "child_changed" /* ChangeType.CHILD_CHANGED */,
- snapshotNode: snapshotNode,
- childName: childName,
- oldSnap: oldSnap
- };
- }
- function changeChildMoved(childName, snapshotNode) {
- return { type: "child_moved" /* ChangeType.CHILD_MOVED */, snapshotNode: snapshotNode, childName: childName };
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Doesn't really filter nodes but applies an index to the node and keeps track of any changes
- */
- var IndexedFilter = /** @class */ (function () {
- function IndexedFilter(index_) {
- this.index_ = index_;
- }
- IndexedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
- util.assert(snap.isIndexed(this.index_), 'A node must be indexed if only a child is updated');
- var oldChild = snap.getImmediateChild(key);
- // Check if anything actually changed.
- if (oldChild.getChild(affectedPath).equals(newChild.getChild(affectedPath))) {
- // There's an edge case where a child can enter or leave the view because affectedPath was set to null.
- // In this case, affectedPath will appear null in both the old and new snapshots. So we need
- // to avoid treating these cases as "nothing changed."
- if (oldChild.isEmpty() === newChild.isEmpty()) {
- // Nothing changed.
- // This assert should be valid, but it's expensive (can dominate perf testing) so don't actually do it.
- //assert(oldChild.equals(newChild), 'Old and new snapshots should be equal.');
- return snap;
- }
- }
- if (optChangeAccumulator != null) {
- if (newChild.isEmpty()) {
- if (snap.hasChild(key)) {
- optChangeAccumulator.trackChildChange(changeChildRemoved(key, oldChild));
- }
- else {
- util.assert(snap.isLeafNode(), 'A child remove without an old child only makes sense on a leaf node');
- }
- }
- else if (oldChild.isEmpty()) {
- optChangeAccumulator.trackChildChange(changeChildAdded(key, newChild));
- }
- else {
- optChangeAccumulator.trackChildChange(changeChildChanged(key, newChild, oldChild));
- }
- }
- if (snap.isLeafNode() && newChild.isEmpty()) {
- return snap;
- }
- else {
- // Make sure the node is indexed
- return snap.updateImmediateChild(key, newChild).withIndex(this.index_);
- }
- };
- IndexedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
- if (optChangeAccumulator != null) {
- if (!oldSnap.isLeafNode()) {
- oldSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
- if (!newSnap.hasChild(key)) {
- optChangeAccumulator.trackChildChange(changeChildRemoved(key, childNode));
- }
- });
- }
- if (!newSnap.isLeafNode()) {
- newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
- if (oldSnap.hasChild(key)) {
- var oldChild = oldSnap.getImmediateChild(key);
- if (!oldChild.equals(childNode)) {
- optChangeAccumulator.trackChildChange(changeChildChanged(key, childNode, oldChild));
- }
- }
- else {
- optChangeAccumulator.trackChildChange(changeChildAdded(key, childNode));
- }
- });
- }
- }
- return newSnap.withIndex(this.index_);
- };
- IndexedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
- if (oldSnap.isEmpty()) {
- return ChildrenNode.EMPTY_NODE;
- }
- else {
- return oldSnap.updatePriority(newPriority);
- }
- };
- IndexedFilter.prototype.filtersNodes = function () {
- return false;
- };
- IndexedFilter.prototype.getIndexedFilter = function () {
- return this;
- };
- IndexedFilter.prototype.getIndex = function () {
- return this.index_;
- };
- return IndexedFilter;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Filters nodes by range and uses an IndexFilter to track any changes after filtering the node
- */
- var RangedFilter = /** @class */ (function () {
- function RangedFilter(params) {
- this.indexedFilter_ = new IndexedFilter(params.getIndex());
- this.index_ = params.getIndex();
- this.startPost_ = RangedFilter.getStartPost_(params);
- this.endPost_ = RangedFilter.getEndPost_(params);
- this.startIsInclusive_ = !params.startAfterSet_;
- this.endIsInclusive_ = !params.endBeforeSet_;
- }
- RangedFilter.prototype.getStartPost = function () {
- return this.startPost_;
- };
- RangedFilter.prototype.getEndPost = function () {
- return this.endPost_;
- };
- RangedFilter.prototype.matches = function (node) {
- var isWithinStart = this.startIsInclusive_
- ? this.index_.compare(this.getStartPost(), node) <= 0
- : this.index_.compare(this.getStartPost(), node) < 0;
- var isWithinEnd = this.endIsInclusive_
- ? this.index_.compare(node, this.getEndPost()) <= 0
- : this.index_.compare(node, this.getEndPost()) < 0;
- return isWithinStart && isWithinEnd;
- };
- RangedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
- if (!this.matches(new NamedNode(key, newChild))) {
- newChild = ChildrenNode.EMPTY_NODE;
- }
- return this.indexedFilter_.updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
- };
- RangedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
- if (newSnap.isLeafNode()) {
- // Make sure we have a children node with the correct index, not a leaf node;
- newSnap = ChildrenNode.EMPTY_NODE;
- }
- var filtered = newSnap.withIndex(this.index_);
- // Don't support priorities on queries
- filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
- var self = this;
- newSnap.forEachChild(PRIORITY_INDEX, function (key, childNode) {
- if (!self.matches(new NamedNode(key, childNode))) {
- filtered = filtered.updateImmediateChild(key, ChildrenNode.EMPTY_NODE);
- }
- });
- return this.indexedFilter_.updateFullNode(oldSnap, filtered, optChangeAccumulator);
- };
- RangedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
- // Don't support priorities on queries
- return oldSnap;
- };
- RangedFilter.prototype.filtersNodes = function () {
- return true;
- };
- RangedFilter.prototype.getIndexedFilter = function () {
- return this.indexedFilter_;
- };
- RangedFilter.prototype.getIndex = function () {
- return this.index_;
- };
- RangedFilter.getStartPost_ = function (params) {
- if (params.hasStart()) {
- var startName = params.getIndexStartName();
- return params.getIndex().makePost(params.getIndexStartValue(), startName);
- }
- else {
- return params.getIndex().minPost();
- }
- };
- RangedFilter.getEndPost_ = function (params) {
- if (params.hasEnd()) {
- var endName = params.getIndexEndName();
- return params.getIndex().makePost(params.getIndexEndValue(), endName);
- }
- else {
- return params.getIndex().maxPost();
- }
- };
- return RangedFilter;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Applies a limit and a range to a node and uses RangedFilter to do the heavy lifting where possible
- */
- var LimitedFilter = /** @class */ (function () {
- function LimitedFilter(params) {
- var _this = this;
- this.withinDirectionalStart = function (node) {
- return _this.reverse_ ? _this.withinEndPost(node) : _this.withinStartPost(node);
- };
- this.withinDirectionalEnd = function (node) {
- return _this.reverse_ ? _this.withinStartPost(node) : _this.withinEndPost(node);
- };
- this.withinStartPost = function (node) {
- var compareRes = _this.index_.compare(_this.rangedFilter_.getStartPost(), node);
- return _this.startIsInclusive_ ? compareRes <= 0 : compareRes < 0;
- };
- this.withinEndPost = function (node) {
- var compareRes = _this.index_.compare(node, _this.rangedFilter_.getEndPost());
- return _this.endIsInclusive_ ? compareRes <= 0 : compareRes < 0;
- };
- this.rangedFilter_ = new RangedFilter(params);
- this.index_ = params.getIndex();
- this.limit_ = params.getLimit();
- this.reverse_ = !params.isViewFromLeft();
- this.startIsInclusive_ = !params.startAfterSet_;
- this.endIsInclusive_ = !params.endBeforeSet_;
- }
- LimitedFilter.prototype.updateChild = function (snap, key, newChild, affectedPath, source, optChangeAccumulator) {
- if (!this.rangedFilter_.matches(new NamedNode(key, newChild))) {
- newChild = ChildrenNode.EMPTY_NODE;
- }
- if (snap.getImmediateChild(key).equals(newChild)) {
- // No change
- return snap;
- }
- else if (snap.numChildren() < this.limit_) {
- return this.rangedFilter_
- .getIndexedFilter()
- .updateChild(snap, key, newChild, affectedPath, source, optChangeAccumulator);
- }
- else {
- return this.fullLimitUpdateChild_(snap, key, newChild, source, optChangeAccumulator);
- }
- };
- LimitedFilter.prototype.updateFullNode = function (oldSnap, newSnap, optChangeAccumulator) {
- var filtered;
- if (newSnap.isLeafNode() || newSnap.isEmpty()) {
- // Make sure we have a children node with the correct index, not a leaf node;
- filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
- }
- else {
- if (this.limit_ * 2 < newSnap.numChildren() &&
- newSnap.isIndexed(this.index_)) {
- // Easier to build up a snapshot, since what we're given has more than twice the elements we want
- filtered = ChildrenNode.EMPTY_NODE.withIndex(this.index_);
- // anchor to the startPost, endPost, or last element as appropriate
- var iterator = void 0;
- if (this.reverse_) {
- iterator = newSnap.getReverseIteratorFrom(this.rangedFilter_.getEndPost(), this.index_);
- }
- else {
- iterator = newSnap.getIteratorFrom(this.rangedFilter_.getStartPost(), this.index_);
- }
- var count = 0;
- while (iterator.hasNext() && count < this.limit_) {
- var next = iterator.getNext();
- if (!this.withinDirectionalStart(next)) {
- // if we have not reached the start, skip to the next element
- continue;
- }
- else if (!this.withinDirectionalEnd(next)) {
- // if we have reached the end, stop adding elements
- break;
- }
- else {
- filtered = filtered.updateImmediateChild(next.name, next.node);
- count++;
- }
- }
- }
- else {
- // The snap contains less than twice the limit. Faster to delete from the snap than build up a new one
- filtered = newSnap.withIndex(this.index_);
- // Don't support priorities on queries
- filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
- var iterator = void 0;
- if (this.reverse_) {
- iterator = filtered.getReverseIterator(this.index_);
- }
- else {
- iterator = filtered.getIterator(this.index_);
- }
- var count = 0;
- while (iterator.hasNext()) {
- var next = iterator.getNext();
- var inRange = count < this.limit_ &&
- this.withinDirectionalStart(next) &&
- this.withinDirectionalEnd(next);
- if (inRange) {
- count++;
- }
- else {
- filtered = filtered.updateImmediateChild(next.name, ChildrenNode.EMPTY_NODE);
- }
- }
- }
- }
- return this.rangedFilter_
- .getIndexedFilter()
- .updateFullNode(oldSnap, filtered, optChangeAccumulator);
- };
- LimitedFilter.prototype.updatePriority = function (oldSnap, newPriority) {
- // Don't support priorities on queries
- return oldSnap;
- };
- LimitedFilter.prototype.filtersNodes = function () {
- return true;
- };
- LimitedFilter.prototype.getIndexedFilter = function () {
- return this.rangedFilter_.getIndexedFilter();
- };
- LimitedFilter.prototype.getIndex = function () {
- return this.index_;
- };
- LimitedFilter.prototype.fullLimitUpdateChild_ = function (snap, childKey, childSnap, source, changeAccumulator) {
- // TODO: rename all cache stuff etc to general snap terminology
- var cmp;
- if (this.reverse_) {
- var indexCmp_1 = this.index_.getCompare();
- cmp = function (a, b) { return indexCmp_1(b, a); };
- }
- else {
- cmp = this.index_.getCompare();
- }
- var oldEventCache = snap;
- util.assert(oldEventCache.numChildren() === this.limit_, '');
- var newChildNamedNode = new NamedNode(childKey, childSnap);
- var windowBoundary = this.reverse_
- ? oldEventCache.getFirstChild(this.index_)
- : oldEventCache.getLastChild(this.index_);
- var inRange = this.rangedFilter_.matches(newChildNamedNode);
- if (oldEventCache.hasChild(childKey)) {
- var oldChildSnap = oldEventCache.getImmediateChild(childKey);
- var nextChild = source.getChildAfterChild(this.index_, windowBoundary, this.reverse_);
- while (nextChild != null &&
- (nextChild.name === childKey || oldEventCache.hasChild(nextChild.name))) {
- // There is a weird edge case where a node is updated as part of a merge in the write tree, but hasn't
- // been applied to the limited filter yet. Ignore this next child which will be updated later in
- // the limited filter...
- nextChild = source.getChildAfterChild(this.index_, nextChild, this.reverse_);
- }
- var compareNext = nextChild == null ? 1 : cmp(nextChild, newChildNamedNode);
- var remainsInWindow = inRange && !childSnap.isEmpty() && compareNext >= 0;
- if (remainsInWindow) {
- if (changeAccumulator != null) {
- changeAccumulator.trackChildChange(changeChildChanged(childKey, childSnap, oldChildSnap));
- }
- return oldEventCache.updateImmediateChild(childKey, childSnap);
- }
- else {
- if (changeAccumulator != null) {
- changeAccumulator.trackChildChange(changeChildRemoved(childKey, oldChildSnap));
- }
- var newEventCache = oldEventCache.updateImmediateChild(childKey, ChildrenNode.EMPTY_NODE);
- var nextChildInRange = nextChild != null && this.rangedFilter_.matches(nextChild);
- if (nextChildInRange) {
- if (changeAccumulator != null) {
- changeAccumulator.trackChildChange(changeChildAdded(nextChild.name, nextChild.node));
- }
- return newEventCache.updateImmediateChild(nextChild.name, nextChild.node);
- }
- else {
- return newEventCache;
- }
- }
- }
- else if (childSnap.isEmpty()) {
- // we're deleting a node, but it was not in the window, so ignore it
- return snap;
- }
- else if (inRange) {
- if (cmp(windowBoundary, newChildNamedNode) >= 0) {
- if (changeAccumulator != null) {
- changeAccumulator.trackChildChange(changeChildRemoved(windowBoundary.name, windowBoundary.node));
- changeAccumulator.trackChildChange(changeChildAdded(childKey, childSnap));
- }
- return oldEventCache
- .updateImmediateChild(childKey, childSnap)
- .updateImmediateChild(windowBoundary.name, ChildrenNode.EMPTY_NODE);
- }
- else {
- return snap;
- }
- }
- else {
- return snap;
- }
- };
- return LimitedFilter;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * This class is an immutable-from-the-public-api struct containing a set of query parameters defining a
- * range to be returned for a particular location. It is assumed that validation of parameters is done at the
- * user-facing API level, so it is not done here.
- *
- * @internal
- */
- var QueryParams = /** @class */ (function () {
- function QueryParams() {
- this.limitSet_ = false;
- this.startSet_ = false;
- this.startNameSet_ = false;
- this.startAfterSet_ = false; // can only be true if startSet_ is true
- this.endSet_ = false;
- this.endNameSet_ = false;
- this.endBeforeSet_ = false; // can only be true if endSet_ is true
- this.limit_ = 0;
- this.viewFrom_ = '';
- this.indexStartValue_ = null;
- this.indexStartName_ = '';
- this.indexEndValue_ = null;
- this.indexEndName_ = '';
- this.index_ = PRIORITY_INDEX;
- }
- QueryParams.prototype.hasStart = function () {
- return this.startSet_;
- };
- /**
- * @returns True if it would return from left.
- */
- QueryParams.prototype.isViewFromLeft = function () {
- if (this.viewFrom_ === '') {
- // limit(), rather than limitToFirst or limitToLast was called.
- // This means that only one of startSet_ and endSet_ is true. Use them
- // to calculate which side of the view to anchor to. If neither is set,
- // anchor to the end.
- return this.startSet_;
- }
- else {
- return this.viewFrom_ === "l" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_LEFT */;
- }
- };
- /**
- * Only valid to call if hasStart() returns true
- */
- QueryParams.prototype.getIndexStartValue = function () {
- util.assert(this.startSet_, 'Only valid if start has been set');
- return this.indexStartValue_;
- };
- /**
- * Only valid to call if hasStart() returns true.
- * Returns the starting key name for the range defined by these query parameters
- */
- QueryParams.prototype.getIndexStartName = function () {
- util.assert(this.startSet_, 'Only valid if start has been set');
- if (this.startNameSet_) {
- return this.indexStartName_;
- }
- else {
- return MIN_NAME;
- }
- };
- QueryParams.prototype.hasEnd = function () {
- return this.endSet_;
- };
- /**
- * Only valid to call if hasEnd() returns true.
- */
- QueryParams.prototype.getIndexEndValue = function () {
- util.assert(this.endSet_, 'Only valid if end has been set');
- return this.indexEndValue_;
- };
- /**
- * Only valid to call if hasEnd() returns true.
- * Returns the end key name for the range defined by these query parameters
- */
- QueryParams.prototype.getIndexEndName = function () {
- util.assert(this.endSet_, 'Only valid if end has been set');
- if (this.endNameSet_) {
- return this.indexEndName_;
- }
- else {
- return MAX_NAME;
- }
- };
- QueryParams.prototype.hasLimit = function () {
- return this.limitSet_;
- };
- /**
- * @returns True if a limit has been set and it has been explicitly anchored
- */
- QueryParams.prototype.hasAnchoredLimit = function () {
- return this.limitSet_ && this.viewFrom_ !== '';
- };
- /**
- * Only valid to call if hasLimit() returns true
- */
- QueryParams.prototype.getLimit = function () {
- util.assert(this.limitSet_, 'Only valid if limit has been set');
- return this.limit_;
- };
- QueryParams.prototype.getIndex = function () {
- return this.index_;
- };
- QueryParams.prototype.loadsAllData = function () {
- return !(this.startSet_ || this.endSet_ || this.limitSet_);
- };
- QueryParams.prototype.isDefault = function () {
- return this.loadsAllData() && this.index_ === PRIORITY_INDEX;
- };
- QueryParams.prototype.copy = function () {
- var copy = new QueryParams();
- copy.limitSet_ = this.limitSet_;
- copy.limit_ = this.limit_;
- copy.startSet_ = this.startSet_;
- copy.startAfterSet_ = this.startAfterSet_;
- copy.indexStartValue_ = this.indexStartValue_;
- copy.startNameSet_ = this.startNameSet_;
- copy.indexStartName_ = this.indexStartName_;
- copy.endSet_ = this.endSet_;
- copy.endBeforeSet_ = this.endBeforeSet_;
- copy.indexEndValue_ = this.indexEndValue_;
- copy.endNameSet_ = this.endNameSet_;
- copy.indexEndName_ = this.indexEndName_;
- copy.index_ = this.index_;
- copy.viewFrom_ = this.viewFrom_;
- return copy;
- };
- return QueryParams;
- }());
- function queryParamsGetNodeFilter(queryParams) {
- if (queryParams.loadsAllData()) {
- return new IndexedFilter(queryParams.getIndex());
- }
- else if (queryParams.hasLimit()) {
- return new LimitedFilter(queryParams);
- }
- else {
- return new RangedFilter(queryParams);
- }
- }
- function queryParamsLimitToFirst(queryParams, newLimit) {
- var newParams = queryParams.copy();
- newParams.limitSet_ = true;
- newParams.limit_ = newLimit;
- newParams.viewFrom_ = "l" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_LEFT */;
- return newParams;
- }
- function queryParamsLimitToLast(queryParams, newLimit) {
- var newParams = queryParams.copy();
- newParams.limitSet_ = true;
- newParams.limit_ = newLimit;
- newParams.viewFrom_ = "r" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_RIGHT */;
- return newParams;
- }
- function queryParamsStartAt(queryParams, indexValue, key) {
- var newParams = queryParams.copy();
- newParams.startSet_ = true;
- if (indexValue === undefined) {
- indexValue = null;
- }
- newParams.indexStartValue_ = indexValue;
- if (key != null) {
- newParams.startNameSet_ = true;
- newParams.indexStartName_ = key;
- }
- else {
- newParams.startNameSet_ = false;
- newParams.indexStartName_ = '';
- }
- return newParams;
- }
- function queryParamsStartAfter(queryParams, indexValue, key) {
- var params;
- if (queryParams.index_ === KEY_INDEX || !!key) {
- params = queryParamsStartAt(queryParams, indexValue, key);
- }
- else {
- params = queryParamsStartAt(queryParams, indexValue, MAX_NAME);
- }
- params.startAfterSet_ = true;
- return params;
- }
- function queryParamsEndAt(queryParams, indexValue, key) {
- var newParams = queryParams.copy();
- newParams.endSet_ = true;
- if (indexValue === undefined) {
- indexValue = null;
- }
- newParams.indexEndValue_ = indexValue;
- if (key !== undefined) {
- newParams.endNameSet_ = true;
- newParams.indexEndName_ = key;
- }
- else {
- newParams.endNameSet_ = false;
- newParams.indexEndName_ = '';
- }
- return newParams;
- }
- function queryParamsEndBefore(queryParams, indexValue, key) {
- var params;
- if (queryParams.index_ === KEY_INDEX || !!key) {
- params = queryParamsEndAt(queryParams, indexValue, key);
- }
- else {
- params = queryParamsEndAt(queryParams, indexValue, MIN_NAME);
- }
- params.endBeforeSet_ = true;
- return params;
- }
- function queryParamsOrderBy(queryParams, index) {
- var newParams = queryParams.copy();
- newParams.index_ = index;
- return newParams;
- }
- /**
- * Returns a set of REST query string parameters representing this query.
- *
- * @returns query string parameters
- */
- function queryParamsToRestQueryStringParameters(queryParams) {
- var qs = {};
- if (queryParams.isDefault()) {
- return qs;
- }
- var orderBy;
- if (queryParams.index_ === PRIORITY_INDEX) {
- orderBy = "$priority" /* REST_QUERY_CONSTANTS.PRIORITY_INDEX */;
- }
- else if (queryParams.index_ === VALUE_INDEX) {
- orderBy = "$value" /* REST_QUERY_CONSTANTS.VALUE_INDEX */;
- }
- else if (queryParams.index_ === KEY_INDEX) {
- orderBy = "$key" /* REST_QUERY_CONSTANTS.KEY_INDEX */;
- }
- else {
- util.assert(queryParams.index_ instanceof PathIndex, 'Unrecognized index type!');
- orderBy = queryParams.index_.toString();
- }
- qs["orderBy" /* REST_QUERY_CONSTANTS.ORDER_BY */] = util.stringify(orderBy);
- if (queryParams.startSet_) {
- var startParam = queryParams.startAfterSet_
- ? "startAfter" /* REST_QUERY_CONSTANTS.START_AFTER */
- : "startAt" /* REST_QUERY_CONSTANTS.START_AT */;
- qs[startParam] = util.stringify(queryParams.indexStartValue_);
- if (queryParams.startNameSet_) {
- qs[startParam] += ',' + util.stringify(queryParams.indexStartName_);
- }
- }
- if (queryParams.endSet_) {
- var endParam = queryParams.endBeforeSet_
- ? "endBefore" /* REST_QUERY_CONSTANTS.END_BEFORE */
- : "endAt" /* REST_QUERY_CONSTANTS.END_AT */;
- qs[endParam] = util.stringify(queryParams.indexEndValue_);
- if (queryParams.endNameSet_) {
- qs[endParam] += ',' + util.stringify(queryParams.indexEndName_);
- }
- }
- if (queryParams.limitSet_) {
- if (queryParams.isViewFromLeft()) {
- qs["limitToFirst" /* REST_QUERY_CONSTANTS.LIMIT_TO_FIRST */] = queryParams.limit_;
- }
- else {
- qs["limitToLast" /* REST_QUERY_CONSTANTS.LIMIT_TO_LAST */] = queryParams.limit_;
- }
- }
- return qs;
- }
- function queryParamsGetQueryObject(queryParams) {
- var obj = {};
- if (queryParams.startSet_) {
- obj["sp" /* WIRE_PROTOCOL_CONSTANTS.INDEX_START_VALUE */] =
- queryParams.indexStartValue_;
- if (queryParams.startNameSet_) {
- obj["sn" /* WIRE_PROTOCOL_CONSTANTS.INDEX_START_NAME */] =
- queryParams.indexStartName_;
- }
- obj["sin" /* WIRE_PROTOCOL_CONSTANTS.INDEX_START_IS_INCLUSIVE */] =
- !queryParams.startAfterSet_;
- }
- if (queryParams.endSet_) {
- obj["ep" /* WIRE_PROTOCOL_CONSTANTS.INDEX_END_VALUE */] = queryParams.indexEndValue_;
- if (queryParams.endNameSet_) {
- obj["en" /* WIRE_PROTOCOL_CONSTANTS.INDEX_END_NAME */] = queryParams.indexEndName_;
- }
- obj["ein" /* WIRE_PROTOCOL_CONSTANTS.INDEX_END_IS_INCLUSIVE */] =
- !queryParams.endBeforeSet_;
- }
- if (queryParams.limitSet_) {
- obj["l" /* WIRE_PROTOCOL_CONSTANTS.LIMIT */] = queryParams.limit_;
- var viewFrom = queryParams.viewFrom_;
- if (viewFrom === '') {
- if (queryParams.isViewFromLeft()) {
- viewFrom = "l" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_LEFT */;
- }
- else {
- viewFrom = "r" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM_RIGHT */;
- }
- }
- obj["vf" /* WIRE_PROTOCOL_CONSTANTS.VIEW_FROM */] = viewFrom;
- }
- // For now, priority index is the default, so we only specify if it's some other index
- if (queryParams.index_ !== PRIORITY_INDEX) {
- obj["i" /* WIRE_PROTOCOL_CONSTANTS.INDEX */] = queryParams.index_.toString();
- }
- return obj;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An implementation of ServerActions that communicates with the server via REST requests.
- * This is mostly useful for compatibility with crawlers, where we don't want to spin up a full
- * persistent connection (using WebSockets or long-polling)
- */
- var ReadonlyRestClient = /** @class */ (function (_super) {
- tslib.__extends(ReadonlyRestClient, _super);
- /**
- * @param repoInfo_ - Data about the namespace we are connecting to
- * @param onDataUpdate_ - A callback for new data from the server
- */
- function ReadonlyRestClient(repoInfo_, onDataUpdate_, authTokenProvider_, appCheckTokenProvider_) {
- var _this = _super.call(this) || this;
- _this.repoInfo_ = repoInfo_;
- _this.onDataUpdate_ = onDataUpdate_;
- _this.authTokenProvider_ = authTokenProvider_;
- _this.appCheckTokenProvider_ = appCheckTokenProvider_;
- /** @private {function(...[*])} */
- _this.log_ = logWrapper('p:rest:');
- /**
- * We don't actually need to track listens, except to prevent us calling an onComplete for a listen
- * that's been removed. :-/
- */
- _this.listens_ = {};
- return _this;
- }
- ReadonlyRestClient.prototype.reportStats = function (stats) {
- throw new Error('Method not implemented.');
- };
- ReadonlyRestClient.getListenId_ = function (query, tag) {
- if (tag !== undefined) {
- return 'tag$' + tag;
- }
- else {
- util.assert(query._queryParams.isDefault(), "should have a tag if it's not a default query.");
- return query._path.toString();
- }
- };
- /** @inheritDoc */
- ReadonlyRestClient.prototype.listen = function (query, currentHashFn, tag, onComplete) {
- var _this = this;
- var pathString = query._path.toString();
- this.log_('Listen called for ' + pathString + ' ' + query._queryIdentifier);
- // Mark this listener so we can tell if it's removed.
- var listenId = ReadonlyRestClient.getListenId_(query, tag);
- var thisListen = {};
- this.listens_[listenId] = thisListen;
- var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
- this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
- var data = result;
- if (error === 404) {
- data = null;
- error = null;
- }
- if (error === null) {
- _this.onDataUpdate_(pathString, data, /*isMerge=*/ false, tag);
- }
- if (util.safeGet(_this.listens_, listenId) === thisListen) {
- var status_1;
- if (!error) {
- status_1 = 'ok';
- }
- else if (error === 401) {
- status_1 = 'permission_denied';
- }
- else {
- status_1 = 'rest_error:' + error;
- }
- onComplete(status_1, null);
- }
- });
- };
- /** @inheritDoc */
- ReadonlyRestClient.prototype.unlisten = function (query, tag) {
- var listenId = ReadonlyRestClient.getListenId_(query, tag);
- delete this.listens_[listenId];
- };
- ReadonlyRestClient.prototype.get = function (query) {
- var _this = this;
- var queryStringParameters = queryParamsToRestQueryStringParameters(query._queryParams);
- var pathString = query._path.toString();
- var deferred = new util.Deferred();
- this.restRequest_(pathString + '.json', queryStringParameters, function (error, result) {
- var data = result;
- if (error === 404) {
- data = null;
- error = null;
- }
- if (error === null) {
- _this.onDataUpdate_(pathString, data,
- /*isMerge=*/ false,
- /*tag=*/ null);
- deferred.resolve(data);
- }
- else {
- deferred.reject(new Error(data));
- }
- });
- return deferred.promise;
- };
- /** @inheritDoc */
- ReadonlyRestClient.prototype.refreshAuthToken = function (token) {
- // no-op since we just always call getToken.
- };
- /**
- * Performs a REST request to the given path, with the provided query string parameters,
- * and any auth credentials we have.
- */
- ReadonlyRestClient.prototype.restRequest_ = function (pathString, queryStringParameters, callback) {
- var _this = this;
- if (queryStringParameters === void 0) { queryStringParameters = {}; }
- queryStringParameters['format'] = 'export';
- return Promise.all([
- this.authTokenProvider_.getToken(/*forceRefresh=*/ false),
- this.appCheckTokenProvider_.getToken(/*forceRefresh=*/ false)
- ]).then(function (_a) {
- var _b = tslib.__read(_a, 2), authToken = _b[0], appCheckToken = _b[1];
- if (authToken && authToken.accessToken) {
- queryStringParameters['auth'] = authToken.accessToken;
- }
- if (appCheckToken && appCheckToken.token) {
- queryStringParameters['ac'] = appCheckToken.token;
- }
- var url = (_this.repoInfo_.secure ? 'https://' : 'http://') +
- _this.repoInfo_.host +
- pathString +
- '?' +
- 'ns=' +
- _this.repoInfo_.namespace +
- util.querystring(queryStringParameters);
- _this.log_('Sending REST request for ' + url);
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- if (callback && xhr.readyState === 4) {
- _this.log_('REST Response for ' + url + ' received. status:', xhr.status, 'response:', xhr.responseText);
- var res = null;
- if (xhr.status >= 200 && xhr.status < 300) {
- try {
- res = util.jsonEval(xhr.responseText);
- }
- catch (e) {
- warn$1('Failed to parse JSON response for ' +
- url +
- ': ' +
- xhr.responseText);
- }
- callback(null, res);
- }
- else {
- // 401 and 404 are expected.
- if (xhr.status !== 401 && xhr.status !== 404) {
- warn$1('Got unsuccessful REST response for ' +
- url +
- ' Status: ' +
- xhr.status);
- }
- callback(xhr.status);
- }
- callback = null;
- }
- };
- xhr.open('GET', url, /*asynchronous=*/ true);
- xhr.send();
- });
- };
- return ReadonlyRestClient;
- }(ServerActions));
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Mutable object which basically just stores a reference to the "latest" immutable snapshot.
- */
- var SnapshotHolder = /** @class */ (function () {
- function SnapshotHolder() {
- this.rootNode_ = ChildrenNode.EMPTY_NODE;
- }
- SnapshotHolder.prototype.getNode = function (path) {
- return this.rootNode_.getChild(path);
- };
- SnapshotHolder.prototype.updateSnapshot = function (path, newSnapshotNode) {
- this.rootNode_ = this.rootNode_.updateChild(path, newSnapshotNode);
- };
- return SnapshotHolder;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function newSparseSnapshotTree() {
- return {
- value: null,
- children: new Map()
- };
- }
- /**
- * Stores the given node at the specified path. If there is already a node
- * at a shallower path, it merges the new data into that snapshot node.
- *
- * @param path - Path to look up snapshot for.
- * @param data - The new data, or null.
- */
- function sparseSnapshotTreeRemember(sparseSnapshotTree, path, data) {
- if (pathIsEmpty(path)) {
- sparseSnapshotTree.value = data;
- sparseSnapshotTree.children.clear();
- }
- else if (sparseSnapshotTree.value !== null) {
- sparseSnapshotTree.value = sparseSnapshotTree.value.updateChild(path, data);
- }
- else {
- var childKey = pathGetFront(path);
- if (!sparseSnapshotTree.children.has(childKey)) {
- sparseSnapshotTree.children.set(childKey, newSparseSnapshotTree());
- }
- var child = sparseSnapshotTree.children.get(childKey);
- path = pathPopFront(path);
- sparseSnapshotTreeRemember(child, path, data);
- }
- }
- /**
- * Purge the data at path from the cache.
- *
- * @param path - Path to look up snapshot for.
- * @returns True if this node should now be removed.
- */
- function sparseSnapshotTreeForget(sparseSnapshotTree, path) {
- if (pathIsEmpty(path)) {
- sparseSnapshotTree.value = null;
- sparseSnapshotTree.children.clear();
- return true;
- }
- else {
- if (sparseSnapshotTree.value !== null) {
- if (sparseSnapshotTree.value.isLeafNode()) {
- // We're trying to forget a node that doesn't exist
- return false;
- }
- else {
- var value = sparseSnapshotTree.value;
- sparseSnapshotTree.value = null;
- value.forEachChild(PRIORITY_INDEX, function (key, tree) {
- sparseSnapshotTreeRemember(sparseSnapshotTree, new Path(key), tree);
- });
- return sparseSnapshotTreeForget(sparseSnapshotTree, path);
- }
- }
- else if (sparseSnapshotTree.children.size > 0) {
- var childKey = pathGetFront(path);
- path = pathPopFront(path);
- if (sparseSnapshotTree.children.has(childKey)) {
- var safeToRemove = sparseSnapshotTreeForget(sparseSnapshotTree.children.get(childKey), path);
- if (safeToRemove) {
- sparseSnapshotTree.children.delete(childKey);
- }
- }
- return sparseSnapshotTree.children.size === 0;
- }
- else {
- return true;
- }
- }
- }
- /**
- * Recursively iterates through all of the stored tree and calls the
- * callback on each one.
- *
- * @param prefixPath - Path to look up node for.
- * @param func - The function to invoke for each tree.
- */
- function sparseSnapshotTreeForEachTree(sparseSnapshotTree, prefixPath, func) {
- if (sparseSnapshotTree.value !== null) {
- func(prefixPath, sparseSnapshotTree.value);
- }
- else {
- sparseSnapshotTreeForEachChild(sparseSnapshotTree, function (key, tree) {
- var path = new Path(prefixPath.toString() + '/' + key);
- sparseSnapshotTreeForEachTree(tree, path, func);
- });
- }
- }
- /**
- * Iterates through each immediate child and triggers the callback.
- * Only seems to be used in tests.
- *
- * @param func - The function to invoke for each child.
- */
- function sparseSnapshotTreeForEachChild(sparseSnapshotTree, func) {
- sparseSnapshotTree.children.forEach(function (tree, key) {
- func(key, tree);
- });
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Returns the delta from the previous call to get stats.
- *
- * @param collection_ - The collection to "listen" to.
- */
- var StatsListener = /** @class */ (function () {
- function StatsListener(collection_) {
- this.collection_ = collection_;
- this.last_ = null;
- }
- StatsListener.prototype.get = function () {
- var newStats = this.collection_.get();
- var delta = tslib.__assign({}, newStats);
- if (this.last_) {
- each(this.last_, function (stat, value) {
- delta[stat] = delta[stat] - value;
- });
- }
- this.last_ = newStats;
- return delta;
- };
- return StatsListener;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // Assuming some apps may have a short amount of time on page, and a bulk of firebase operations probably
- // happen on page load, we try to report our first set of stats pretty quickly, but we wait at least 10
- // seconds to try to ensure the Firebase connection is established / settled.
- var FIRST_STATS_MIN_TIME = 10 * 1000;
- var FIRST_STATS_MAX_TIME = 30 * 1000;
- // We'll continue to report stats on average every 5 minutes.
- var REPORT_STATS_INTERVAL = 5 * 60 * 1000;
- var StatsReporter = /** @class */ (function () {
- function StatsReporter(collection, server_) {
- this.server_ = server_;
- this.statsToReport_ = {};
- this.statsListener_ = new StatsListener(collection);
- var timeout = FIRST_STATS_MIN_TIME +
- (FIRST_STATS_MAX_TIME - FIRST_STATS_MIN_TIME) * Math.random();
- setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(timeout));
- }
- StatsReporter.prototype.reportStats_ = function () {
- var _this = this;
- var stats = this.statsListener_.get();
- var reportedStats = {};
- var haveStatsToReport = false;
- each(stats, function (stat, value) {
- if (value > 0 && util.contains(_this.statsToReport_, stat)) {
- reportedStats[stat] = value;
- haveStatsToReport = true;
- }
- });
- if (haveStatsToReport) {
- this.server_.reportStats(reportedStats);
- }
- // queue our next run.
- setTimeoutNonBlocking(this.reportStats_.bind(this), Math.floor(Math.random() * 2 * REPORT_STATS_INTERVAL));
- };
- return StatsReporter;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- *
- * @enum
- */
- var OperationType;
- (function (OperationType) {
- OperationType[OperationType["OVERWRITE"] = 0] = "OVERWRITE";
- OperationType[OperationType["MERGE"] = 1] = "MERGE";
- OperationType[OperationType["ACK_USER_WRITE"] = 2] = "ACK_USER_WRITE";
- OperationType[OperationType["LISTEN_COMPLETE"] = 3] = "LISTEN_COMPLETE";
- })(OperationType || (OperationType = {}));
- function newOperationSourceUser() {
- return {
- fromUser: true,
- fromServer: false,
- queryId: null,
- tagged: false
- };
- }
- function newOperationSourceServer() {
- return {
- fromUser: false,
- fromServer: true,
- queryId: null,
- tagged: false
- };
- }
- function newOperationSourceServerTaggedQuery(queryId) {
- return {
- fromUser: false,
- fromServer: true,
- queryId: queryId,
- tagged: true
- };
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var AckUserWrite = /** @class */ (function () {
- /**
- * @param affectedTree - A tree containing true for each affected path. Affected paths can't overlap.
- */
- function AckUserWrite(
- /** @inheritDoc */ path,
- /** @inheritDoc */ affectedTree,
- /** @inheritDoc */ revert) {
- this.path = path;
- this.affectedTree = affectedTree;
- this.revert = revert;
- /** @inheritDoc */
- this.type = OperationType.ACK_USER_WRITE;
- /** @inheritDoc */
- this.source = newOperationSourceUser();
- }
- AckUserWrite.prototype.operationForChild = function (childName) {
- if (!pathIsEmpty(this.path)) {
- util.assert(pathGetFront(this.path) === childName, 'operationForChild called for unrelated child.');
- return new AckUserWrite(pathPopFront(this.path), this.affectedTree, this.revert);
- }
- else if (this.affectedTree.value != null) {
- util.assert(this.affectedTree.children.isEmpty(), 'affectedTree should not have overlapping affected paths.');
- // All child locations are affected as well; just return same operation.
- return this;
- }
- else {
- var childTree = this.affectedTree.subtree(new Path(childName));
- return new AckUserWrite(newEmptyPath(), childTree, this.revert);
- }
- };
- return AckUserWrite;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var ListenComplete = /** @class */ (function () {
- function ListenComplete(source, path) {
- this.source = source;
- this.path = path;
- /** @inheritDoc */
- this.type = OperationType.LISTEN_COMPLETE;
- }
- ListenComplete.prototype.operationForChild = function (childName) {
- if (pathIsEmpty(this.path)) {
- return new ListenComplete(this.source, newEmptyPath());
- }
- else {
- return new ListenComplete(this.source, pathPopFront(this.path));
- }
- };
- return ListenComplete;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var Overwrite = /** @class */ (function () {
- function Overwrite(source, path, snap) {
- this.source = source;
- this.path = path;
- this.snap = snap;
- /** @inheritDoc */
- this.type = OperationType.OVERWRITE;
- }
- Overwrite.prototype.operationForChild = function (childName) {
- if (pathIsEmpty(this.path)) {
- return new Overwrite(this.source, newEmptyPath(), this.snap.getImmediateChild(childName));
- }
- else {
- return new Overwrite(this.source, pathPopFront(this.path), this.snap);
- }
- };
- return Overwrite;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var Merge = /** @class */ (function () {
- function Merge(
- /** @inheritDoc */ source,
- /** @inheritDoc */ path,
- /** @inheritDoc */ children) {
- this.source = source;
- this.path = path;
- this.children = children;
- /** @inheritDoc */
- this.type = OperationType.MERGE;
- }
- Merge.prototype.operationForChild = function (childName) {
- if (pathIsEmpty(this.path)) {
- var childTree = this.children.subtree(new Path(childName));
- if (childTree.isEmpty()) {
- // This child is unaffected
- return null;
- }
- else if (childTree.value) {
- // We have a snapshot for the child in question. This becomes an overwrite of the child.
- return new Overwrite(this.source, newEmptyPath(), childTree.value);
- }
- else {
- // This is a merge at a deeper level
- return new Merge(this.source, newEmptyPath(), childTree);
- }
- }
- else {
- util.assert(pathGetFront(this.path) === childName, "Can't get a merge for a child not on the path of the operation");
- return new Merge(this.source, pathPopFront(this.path), this.children);
- }
- };
- Merge.prototype.toString = function () {
- return ('Operation(' +
- this.path +
- ': ' +
- this.source.toString() +
- ' merge: ' +
- this.children.toString() +
- ')');
- };
- return Merge;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * A cache node only stores complete children. Additionally it holds a flag whether the node can be considered fully
- * initialized in the sense that we know at one point in time this represented a valid state of the world, e.g.
- * initialized with data from the server, or a complete overwrite by the client. The filtered flag also tracks
- * whether a node potentially had children removed due to a filter.
- */
- var CacheNode = /** @class */ (function () {
- function CacheNode(node_, fullyInitialized_, filtered_) {
- this.node_ = node_;
- this.fullyInitialized_ = fullyInitialized_;
- this.filtered_ = filtered_;
- }
- /**
- * Returns whether this node was fully initialized with either server data or a complete overwrite by the client
- */
- CacheNode.prototype.isFullyInitialized = function () {
- return this.fullyInitialized_;
- };
- /**
- * Returns whether this node is potentially missing children due to a filter applied to the node
- */
- CacheNode.prototype.isFiltered = function () {
- return this.filtered_;
- };
- CacheNode.prototype.isCompleteForPath = function (path) {
- if (pathIsEmpty(path)) {
- return this.isFullyInitialized() && !this.filtered_;
- }
- var childKey = pathGetFront(path);
- return this.isCompleteForChild(childKey);
- };
- CacheNode.prototype.isCompleteForChild = function (key) {
- return ((this.isFullyInitialized() && !this.filtered_) || this.node_.hasChild(key));
- };
- CacheNode.prototype.getNode = function () {
- return this.node_;
- };
- return CacheNode;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An EventGenerator is used to convert "raw" changes (Change) as computed by the
- * CacheDiffer into actual events (Event) that can be raised. See generateEventsForChanges()
- * for details.
- *
- */
- var EventGenerator = /** @class */ (function () {
- function EventGenerator(query_) {
- this.query_ = query_;
- this.index_ = this.query_._queryParams.getIndex();
- }
- return EventGenerator;
- }());
- /**
- * Given a set of raw changes (no moved events and prevName not specified yet), and a set of
- * EventRegistrations that should be notified of these changes, generate the actual events to be raised.
- *
- * Notes:
- * - child_moved events will be synthesized at this time for any child_changed events that affect
- * our index.
- * - prevName will be calculated based on the index ordering.
- */
- function eventGeneratorGenerateEventsForChanges(eventGenerator, changes, eventCache, eventRegistrations) {
- var events = [];
- var moves = [];
- changes.forEach(function (change) {
- if (change.type === "child_changed" /* ChangeType.CHILD_CHANGED */ &&
- eventGenerator.index_.indexedValueChanged(change.oldSnap, change.snapshotNode)) {
- moves.push(changeChildMoved(change.childName, change.snapshotNode));
- }
- });
- eventGeneratorGenerateEventsForType(eventGenerator, events, "child_removed" /* ChangeType.CHILD_REMOVED */, changes, eventRegistrations, eventCache);
- eventGeneratorGenerateEventsForType(eventGenerator, events, "child_added" /* ChangeType.CHILD_ADDED */, changes, eventRegistrations, eventCache);
- eventGeneratorGenerateEventsForType(eventGenerator, events, "child_moved" /* ChangeType.CHILD_MOVED */, moves, eventRegistrations, eventCache);
- eventGeneratorGenerateEventsForType(eventGenerator, events, "child_changed" /* ChangeType.CHILD_CHANGED */, changes, eventRegistrations, eventCache);
- eventGeneratorGenerateEventsForType(eventGenerator, events, "value" /* ChangeType.VALUE */, changes, eventRegistrations, eventCache);
- return events;
- }
- /**
- * Given changes of a single change type, generate the corresponding events.
- */
- function eventGeneratorGenerateEventsForType(eventGenerator, events, eventType, changes, registrations, eventCache) {
- var filteredChanges = changes.filter(function (change) { return change.type === eventType; });
- filteredChanges.sort(function (a, b) {
- return eventGeneratorCompareChanges(eventGenerator, a, b);
- });
- filteredChanges.forEach(function (change) {
- var materializedChange = eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache);
- registrations.forEach(function (registration) {
- if (registration.respondsTo(change.type)) {
- events.push(registration.createEvent(materializedChange, eventGenerator.query_));
- }
- });
- });
- }
- function eventGeneratorMaterializeSingleChange(eventGenerator, change, eventCache) {
- if (change.type === 'value' || change.type === 'child_removed') {
- return change;
- }
- else {
- change.prevName = eventCache.getPredecessorChildName(change.childName, change.snapshotNode, eventGenerator.index_);
- return change;
- }
- }
- function eventGeneratorCompareChanges(eventGenerator, a, b) {
- if (a.childName == null || b.childName == null) {
- throw util.assertionError('Should only compare child_ events.');
- }
- var aWrapped = new NamedNode(a.childName, a.snapshotNode);
- var bWrapped = new NamedNode(b.childName, b.snapshotNode);
- return eventGenerator.index_.compare(aWrapped, bWrapped);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function newViewCache(eventCache, serverCache) {
- return { eventCache: eventCache, serverCache: serverCache };
- }
- function viewCacheUpdateEventSnap(viewCache, eventSnap, complete, filtered) {
- return newViewCache(new CacheNode(eventSnap, complete, filtered), viewCache.serverCache);
- }
- function viewCacheUpdateServerSnap(viewCache, serverSnap, complete, filtered) {
- return newViewCache(viewCache.eventCache, new CacheNode(serverSnap, complete, filtered));
- }
- function viewCacheGetCompleteEventSnap(viewCache) {
- return viewCache.eventCache.isFullyInitialized()
- ? viewCache.eventCache.getNode()
- : null;
- }
- function viewCacheGetCompleteServerSnap(viewCache) {
- return viewCache.serverCache.isFullyInitialized()
- ? viewCache.serverCache.getNode()
- : null;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var emptyChildrenSingleton;
- /**
- * Singleton empty children collection.
- *
- */
- var EmptyChildren = function () {
- if (!emptyChildrenSingleton) {
- emptyChildrenSingleton = new SortedMap(stringCompare);
- }
- return emptyChildrenSingleton;
- };
- /**
- * A tree with immutable elements.
- */
- var ImmutableTree = /** @class */ (function () {
- function ImmutableTree(value, children) {
- if (children === void 0) { children = EmptyChildren(); }
- this.value = value;
- this.children = children;
- }
- ImmutableTree.fromObject = function (obj) {
- var tree = new ImmutableTree(null);
- each(obj, function (childPath, childSnap) {
- tree = tree.set(new Path(childPath), childSnap);
- });
- return tree;
- };
- /**
- * True if the value is empty and there are no children
- */
- ImmutableTree.prototype.isEmpty = function () {
- return this.value === null && this.children.isEmpty();
- };
- /**
- * Given a path and predicate, return the first node and the path to that node
- * where the predicate returns true.
- *
- * TODO Do a perf test -- If we're creating a bunch of `{path: value:}`
- * objects on the way back out, it may be better to pass down a pathSoFar obj.
- *
- * @param relativePath - The remainder of the path
- * @param predicate - The predicate to satisfy to return a node
- */
- ImmutableTree.prototype.findRootMostMatchingPathAndValue = function (relativePath, predicate) {
- if (this.value != null && predicate(this.value)) {
- return { path: newEmptyPath(), value: this.value };
- }
- else {
- if (pathIsEmpty(relativePath)) {
- return null;
- }
- else {
- var front = pathGetFront(relativePath);
- var child = this.children.get(front);
- if (child !== null) {
- var childExistingPathAndValue = child.findRootMostMatchingPathAndValue(pathPopFront(relativePath), predicate);
- if (childExistingPathAndValue != null) {
- var fullPath = pathChild(new Path(front), childExistingPathAndValue.path);
- return { path: fullPath, value: childExistingPathAndValue.value };
- }
- else {
- return null;
- }
- }
- else {
- return null;
- }
- }
- }
- };
- /**
- * Find, if it exists, the shortest subpath of the given path that points a defined
- * value in the tree
- */
- ImmutableTree.prototype.findRootMostValueAndPath = function (relativePath) {
- return this.findRootMostMatchingPathAndValue(relativePath, function () { return true; });
- };
- /**
- * @returns The subtree at the given path
- */
- ImmutableTree.prototype.subtree = function (relativePath) {
- if (pathIsEmpty(relativePath)) {
- return this;
- }
- else {
- var front = pathGetFront(relativePath);
- var childTree = this.children.get(front);
- if (childTree !== null) {
- return childTree.subtree(pathPopFront(relativePath));
- }
- else {
- return new ImmutableTree(null);
- }
- }
- };
- /**
- * Sets a value at the specified path.
- *
- * @param relativePath - Path to set value at.
- * @param toSet - Value to set.
- * @returns Resulting tree.
- */
- ImmutableTree.prototype.set = function (relativePath, toSet) {
- if (pathIsEmpty(relativePath)) {
- return new ImmutableTree(toSet, this.children);
- }
- else {
- var front = pathGetFront(relativePath);
- var child = this.children.get(front) || new ImmutableTree(null);
- var newChild = child.set(pathPopFront(relativePath), toSet);
- var newChildren = this.children.insert(front, newChild);
- return new ImmutableTree(this.value, newChildren);
- }
- };
- /**
- * Removes the value at the specified path.
- *
- * @param relativePath - Path to value to remove.
- * @returns Resulting tree.
- */
- ImmutableTree.prototype.remove = function (relativePath) {
- if (pathIsEmpty(relativePath)) {
- if (this.children.isEmpty()) {
- return new ImmutableTree(null);
- }
- else {
- return new ImmutableTree(null, this.children);
- }
- }
- else {
- var front = pathGetFront(relativePath);
- var child = this.children.get(front);
- if (child) {
- var newChild = child.remove(pathPopFront(relativePath));
- var newChildren = void 0;
- if (newChild.isEmpty()) {
- newChildren = this.children.remove(front);
- }
- else {
- newChildren = this.children.insert(front, newChild);
- }
- if (this.value === null && newChildren.isEmpty()) {
- return new ImmutableTree(null);
- }
- else {
- return new ImmutableTree(this.value, newChildren);
- }
- }
- else {
- return this;
- }
- }
- };
- /**
- * Gets a value from the tree.
- *
- * @param relativePath - Path to get value for.
- * @returns Value at path, or null.
- */
- ImmutableTree.prototype.get = function (relativePath) {
- if (pathIsEmpty(relativePath)) {
- return this.value;
- }
- else {
- var front = pathGetFront(relativePath);
- var child = this.children.get(front);
- if (child) {
- return child.get(pathPopFront(relativePath));
- }
- else {
- return null;
- }
- }
- };
- /**
- * Replace the subtree at the specified path with the given new tree.
- *
- * @param relativePath - Path to replace subtree for.
- * @param newTree - New tree.
- * @returns Resulting tree.
- */
- ImmutableTree.prototype.setTree = function (relativePath, newTree) {
- if (pathIsEmpty(relativePath)) {
- return newTree;
- }
- else {
- var front = pathGetFront(relativePath);
- var child = this.children.get(front) || new ImmutableTree(null);
- var newChild = child.setTree(pathPopFront(relativePath), newTree);
- var newChildren = void 0;
- if (newChild.isEmpty()) {
- newChildren = this.children.remove(front);
- }
- else {
- newChildren = this.children.insert(front, newChild);
- }
- return new ImmutableTree(this.value, newChildren);
- }
- };
- /**
- * Performs a depth first fold on this tree. Transforms a tree into a single
- * value, given a function that operates on the path to a node, an optional
- * current value, and a map of child names to folded subtrees
- */
- ImmutableTree.prototype.fold = function (fn) {
- return this.fold_(newEmptyPath(), fn);
- };
- /**
- * Recursive helper for public-facing fold() method
- */
- ImmutableTree.prototype.fold_ = function (pathSoFar, fn) {
- var accum = {};
- this.children.inorderTraversal(function (childKey, childTree) {
- accum[childKey] = childTree.fold_(pathChild(pathSoFar, childKey), fn);
- });
- return fn(pathSoFar, this.value, accum);
- };
- /**
- * Find the first matching value on the given path. Return the result of applying f to it.
- */
- ImmutableTree.prototype.findOnPath = function (path, f) {
- return this.findOnPath_(path, newEmptyPath(), f);
- };
- ImmutableTree.prototype.findOnPath_ = function (pathToFollow, pathSoFar, f) {
- var result = this.value ? f(pathSoFar, this.value) : false;
- if (result) {
- return result;
- }
- else {
- if (pathIsEmpty(pathToFollow)) {
- return null;
- }
- else {
- var front = pathGetFront(pathToFollow);
- var nextChild = this.children.get(front);
- if (nextChild) {
- return nextChild.findOnPath_(pathPopFront(pathToFollow), pathChild(pathSoFar, front), f);
- }
- else {
- return null;
- }
- }
- }
- };
- ImmutableTree.prototype.foreachOnPath = function (path, f) {
- return this.foreachOnPath_(path, newEmptyPath(), f);
- };
- ImmutableTree.prototype.foreachOnPath_ = function (pathToFollow, currentRelativePath, f) {
- if (pathIsEmpty(pathToFollow)) {
- return this;
- }
- else {
- if (this.value) {
- f(currentRelativePath, this.value);
- }
- var front = pathGetFront(pathToFollow);
- var nextChild = this.children.get(front);
- if (nextChild) {
- return nextChild.foreachOnPath_(pathPopFront(pathToFollow), pathChild(currentRelativePath, front), f);
- }
- else {
- return new ImmutableTree(null);
- }
- }
- };
- /**
- * Calls the given function for each node in the tree that has a value.
- *
- * @param f - A function to be called with the path from the root of the tree to
- * a node, and the value at that node. Called in depth-first order.
- */
- ImmutableTree.prototype.foreach = function (f) {
- this.foreach_(newEmptyPath(), f);
- };
- ImmutableTree.prototype.foreach_ = function (currentRelativePath, f) {
- this.children.inorderTraversal(function (childName, childTree) {
- childTree.foreach_(pathChild(currentRelativePath, childName), f);
- });
- if (this.value) {
- f(currentRelativePath, this.value);
- }
- };
- ImmutableTree.prototype.foreachChild = function (f) {
- this.children.inorderTraversal(function (childName, childTree) {
- if (childTree.value) {
- f(childName, childTree.value);
- }
- });
- };
- return ImmutableTree;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * This class holds a collection of writes that can be applied to nodes in unison. It abstracts away the logic with
- * dealing with priority writes and multiple nested writes. At any given path there is only allowed to be one write
- * modifying that path. Any write to an existing path or shadowing an existing path will modify that existing write
- * to reflect the write added.
- */
- var CompoundWrite = /** @class */ (function () {
- function CompoundWrite(writeTree_) {
- this.writeTree_ = writeTree_;
- }
- CompoundWrite.empty = function () {
- return new CompoundWrite(new ImmutableTree(null));
- };
- return CompoundWrite;
- }());
- function compoundWriteAddWrite(compoundWrite, path, node) {
- if (pathIsEmpty(path)) {
- return new CompoundWrite(new ImmutableTree(node));
- }
- else {
- var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
- if (rootmost != null) {
- var rootMostPath = rootmost.path;
- var value = rootmost.value;
- var relativePath = newRelativePath(rootMostPath, path);
- value = value.updateChild(relativePath, node);
- return new CompoundWrite(compoundWrite.writeTree_.set(rootMostPath, value));
- }
- else {
- var subtree = new ImmutableTree(node);
- var newWriteTree = compoundWrite.writeTree_.setTree(path, subtree);
- return new CompoundWrite(newWriteTree);
- }
- }
- }
- function compoundWriteAddWrites(compoundWrite, path, updates) {
- var newWrite = compoundWrite;
- each(updates, function (childKey, node) {
- newWrite = compoundWriteAddWrite(newWrite, pathChild(path, childKey), node);
- });
- return newWrite;
- }
- /**
- * Will remove a write at the given path and deeper paths. This will <em>not</em> modify a write at a higher
- * location, which must be removed by calling this method with that path.
- *
- * @param compoundWrite - The CompoundWrite to remove.
- * @param path - The path at which a write and all deeper writes should be removed
- * @returns The new CompoundWrite with the removed path
- */
- function compoundWriteRemoveWrite(compoundWrite, path) {
- if (pathIsEmpty(path)) {
- return CompoundWrite.empty();
- }
- else {
- var newWriteTree = compoundWrite.writeTree_.setTree(path, new ImmutableTree(null));
- return new CompoundWrite(newWriteTree);
- }
- }
- /**
- * Returns whether this CompoundWrite will fully overwrite a node at a given location and can therefore be
- * considered "complete".
- *
- * @param compoundWrite - The CompoundWrite to check.
- * @param path - The path to check for
- * @returns Whether there is a complete write at that path
- */
- function compoundWriteHasCompleteWrite(compoundWrite, path) {
- return compoundWriteGetCompleteNode(compoundWrite, path) != null;
- }
- /**
- * Returns a node for a path if and only if the node is a "complete" overwrite at that path. This will not aggregate
- * writes from deeper paths, but will return child nodes from a more shallow path.
- *
- * @param compoundWrite - The CompoundWrite to get the node from.
- * @param path - The path to get a complete write
- * @returns The node if complete at that path, or null otherwise.
- */
- function compoundWriteGetCompleteNode(compoundWrite, path) {
- var rootmost = compoundWrite.writeTree_.findRootMostValueAndPath(path);
- if (rootmost != null) {
- return compoundWrite.writeTree_
- .get(rootmost.path)
- .getChild(newRelativePath(rootmost.path, path));
- }
- else {
- return null;
- }
- }
- /**
- * Returns all children that are guaranteed to be a complete overwrite.
- *
- * @param compoundWrite - The CompoundWrite to get children from.
- * @returns A list of all complete children.
- */
- function compoundWriteGetCompleteChildren(compoundWrite) {
- var children = [];
- var node = compoundWrite.writeTree_.value;
- if (node != null) {
- // If it's a leaf node, it has no children; so nothing to do.
- if (!node.isLeafNode()) {
- node.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
- children.push(new NamedNode(childName, childNode));
- });
- }
- }
- else {
- compoundWrite.writeTree_.children.inorderTraversal(function (childName, childTree) {
- if (childTree.value != null) {
- children.push(new NamedNode(childName, childTree.value));
- }
- });
- }
- return children;
- }
- function compoundWriteChildCompoundWrite(compoundWrite, path) {
- if (pathIsEmpty(path)) {
- return compoundWrite;
- }
- else {
- var shadowingNode = compoundWriteGetCompleteNode(compoundWrite, path);
- if (shadowingNode != null) {
- return new CompoundWrite(new ImmutableTree(shadowingNode));
- }
- else {
- return new CompoundWrite(compoundWrite.writeTree_.subtree(path));
- }
- }
- }
- /**
- * Returns true if this CompoundWrite is empty and therefore does not modify any nodes.
- * @returns Whether this CompoundWrite is empty
- */
- function compoundWriteIsEmpty(compoundWrite) {
- return compoundWrite.writeTree_.isEmpty();
- }
- /**
- * Applies this CompoundWrite to a node. The node is returned with all writes from this CompoundWrite applied to the
- * node
- * @param node - The node to apply this CompoundWrite to
- * @returns The node with all writes applied
- */
- function compoundWriteApply(compoundWrite, node) {
- return applySubtreeWrite(newEmptyPath(), compoundWrite.writeTree_, node);
- }
- function applySubtreeWrite(relativePath, writeTree, node) {
- if (writeTree.value != null) {
- // Since there a write is always a leaf, we're done here
- return node.updateChild(relativePath, writeTree.value);
- }
- else {
- var priorityWrite_1 = null;
- writeTree.children.inorderTraversal(function (childKey, childTree) {
- if (childKey === '.priority') {
- // Apply priorities at the end so we don't update priorities for either empty nodes or forget
- // to apply priorities to empty nodes that are later filled
- util.assert(childTree.value !== null, 'Priority writes must always be leaf nodes');
- priorityWrite_1 = childTree.value;
- }
- else {
- node = applySubtreeWrite(pathChild(relativePath, childKey), childTree, node);
- }
- });
- // If there was a priority write, we only apply it if the node is not empty
- if (!node.getChild(relativePath).isEmpty() && priorityWrite_1 !== null) {
- node = node.updateChild(pathChild(relativePath, '.priority'), priorityWrite_1);
- }
- return node;
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Create a new WriteTreeRef for the given path. For use with a new sync point at the given path.
- *
- */
- function writeTreeChildWrites(writeTree, path) {
- return newWriteTreeRef(path, writeTree);
- }
- /**
- * Record a new overwrite from user code.
- *
- * @param visible - This is set to false by some transactions. It should be excluded from event caches
- */
- function writeTreeAddOverwrite(writeTree, path, snap, writeId, visible) {
- util.assert(writeId > writeTree.lastWriteId, 'Stacking an older write on top of newer ones');
- if (visible === undefined) {
- visible = true;
- }
- writeTree.allWrites.push({
- path: path,
- snap: snap,
- writeId: writeId,
- visible: visible
- });
- if (visible) {
- writeTree.visibleWrites = compoundWriteAddWrite(writeTree.visibleWrites, path, snap);
- }
- writeTree.lastWriteId = writeId;
- }
- /**
- * Record a new merge from user code.
- */
- function writeTreeAddMerge(writeTree, path, changedChildren, writeId) {
- util.assert(writeId > writeTree.lastWriteId, 'Stacking an older merge on top of newer ones');
- writeTree.allWrites.push({
- path: path,
- children: changedChildren,
- writeId: writeId,
- visible: true
- });
- writeTree.visibleWrites = compoundWriteAddWrites(writeTree.visibleWrites, path, changedChildren);
- writeTree.lastWriteId = writeId;
- }
- function writeTreeGetWrite(writeTree, writeId) {
- for (var i = 0; i < writeTree.allWrites.length; i++) {
- var record = writeTree.allWrites[i];
- if (record.writeId === writeId) {
- return record;
- }
- }
- return null;
- }
- /**
- * Remove a write (either an overwrite or merge) that has been successfully acknowledge by the server. Recalculates
- * the tree if necessary. We return true if it may have been visible, meaning views need to reevaluate.
- *
- * @returns true if the write may have been visible (meaning we'll need to reevaluate / raise
- * events as a result).
- */
- function writeTreeRemoveWrite(writeTree, writeId) {
- // Note: disabling this check. It could be a transaction that preempted another transaction, and thus was applied
- // out of order.
- //const validClear = revert || this.allWrites_.length === 0 || writeId <= this.allWrites_[0].writeId;
- //assert(validClear, "Either we don't have this write, or it's the first one in the queue");
- var idx = writeTree.allWrites.findIndex(function (s) {
- return s.writeId === writeId;
- });
- util.assert(idx >= 0, 'removeWrite called with nonexistent writeId.');
- var writeToRemove = writeTree.allWrites[idx];
- writeTree.allWrites.splice(idx, 1);
- var removedWriteWasVisible = writeToRemove.visible;
- var removedWriteOverlapsWithOtherWrites = false;
- var i = writeTree.allWrites.length - 1;
- while (removedWriteWasVisible && i >= 0) {
- var currentWrite = writeTree.allWrites[i];
- if (currentWrite.visible) {
- if (i >= idx &&
- writeTreeRecordContainsPath_(currentWrite, writeToRemove.path)) {
- // The removed write was completely shadowed by a subsequent write.
- removedWriteWasVisible = false;
- }
- else if (pathContains(writeToRemove.path, currentWrite.path)) {
- // Either we're covering some writes or they're covering part of us (depending on which came first).
- removedWriteOverlapsWithOtherWrites = true;
- }
- }
- i--;
- }
- if (!removedWriteWasVisible) {
- return false;
- }
- else if (removedWriteOverlapsWithOtherWrites) {
- // There's some shadowing going on. Just rebuild the visible writes from scratch.
- writeTreeResetTree_(writeTree);
- return true;
- }
- else {
- // There's no shadowing. We can safely just remove the write(s) from visibleWrites.
- if (writeToRemove.snap) {
- writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, writeToRemove.path);
- }
- else {
- var children = writeToRemove.children;
- each(children, function (childName) {
- writeTree.visibleWrites = compoundWriteRemoveWrite(writeTree.visibleWrites, pathChild(writeToRemove.path, childName));
- });
- }
- return true;
- }
- }
- function writeTreeRecordContainsPath_(writeRecord, path) {
- if (writeRecord.snap) {
- return pathContains(writeRecord.path, path);
- }
- else {
- for (var childName in writeRecord.children) {
- if (writeRecord.children.hasOwnProperty(childName) &&
- pathContains(pathChild(writeRecord.path, childName), path)) {
- return true;
- }
- }
- return false;
- }
- }
- /**
- * Re-layer the writes and merges into a tree so we can efficiently calculate event snapshots
- */
- function writeTreeResetTree_(writeTree) {
- writeTree.visibleWrites = writeTreeLayerTree_(writeTree.allWrites, writeTreeDefaultFilter_, newEmptyPath());
- if (writeTree.allWrites.length > 0) {
- writeTree.lastWriteId =
- writeTree.allWrites[writeTree.allWrites.length - 1].writeId;
- }
- else {
- writeTree.lastWriteId = -1;
- }
- }
- /**
- * The default filter used when constructing the tree. Keep everything that's visible.
- */
- function writeTreeDefaultFilter_(write) {
- return write.visible;
- }
- /**
- * Static method. Given an array of WriteRecords, a filter for which ones to include, and a path, construct the tree of
- * event data at that path.
- */
- function writeTreeLayerTree_(writes, filter, treeRoot) {
- var compoundWrite = CompoundWrite.empty();
- for (var i = 0; i < writes.length; ++i) {
- var write = writes[i];
- // Theory, a later set will either:
- // a) abort a relevant transaction, so no need to worry about excluding it from calculating that transaction
- // b) not be relevant to a transaction (separate branch), so again will not affect the data for that transaction
- if (filter(write)) {
- var writePath = write.path;
- var relativePath = void 0;
- if (write.snap) {
- if (pathContains(treeRoot, writePath)) {
- relativePath = newRelativePath(treeRoot, writePath);
- compoundWrite = compoundWriteAddWrite(compoundWrite, relativePath, write.snap);
- }
- else if (pathContains(writePath, treeRoot)) {
- relativePath = newRelativePath(writePath, treeRoot);
- compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), write.snap.getChild(relativePath));
- }
- else ;
- }
- else if (write.children) {
- if (pathContains(treeRoot, writePath)) {
- relativePath = newRelativePath(treeRoot, writePath);
- compoundWrite = compoundWriteAddWrites(compoundWrite, relativePath, write.children);
- }
- else if (pathContains(writePath, treeRoot)) {
- relativePath = newRelativePath(writePath, treeRoot);
- if (pathIsEmpty(relativePath)) {
- compoundWrite = compoundWriteAddWrites(compoundWrite, newEmptyPath(), write.children);
- }
- else {
- var child = util.safeGet(write.children, pathGetFront(relativePath));
- if (child) {
- // There exists a child in this node that matches the root path
- var deepNode = child.getChild(pathPopFront(relativePath));
- compoundWrite = compoundWriteAddWrite(compoundWrite, newEmptyPath(), deepNode);
- }
- }
- }
- else ;
- }
- else {
- throw util.assertionError('WriteRecord should have .snap or .children');
- }
- }
- }
- return compoundWrite;
- }
- /**
- * Given optional, underlying server data, and an optional set of constraints (exclude some sets, include hidden
- * writes), attempt to calculate a complete snapshot for the given path
- *
- * @param writeIdsToExclude - An optional set to be excluded
- * @param includeHiddenWrites - Defaults to false, whether or not to layer on writes with visible set to false
- */
- function writeTreeCalcCompleteEventCache(writeTree, treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
- if (!writeIdsToExclude && !includeHiddenWrites) {
- var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
- if (shadowingNode != null) {
- return shadowingNode;
- }
- else {
- var subMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
- if (compoundWriteIsEmpty(subMerge)) {
- return completeServerCache;
- }
- else if (completeServerCache == null &&
- !compoundWriteHasCompleteWrite(subMerge, newEmptyPath())) {
- // We wouldn't have a complete snapshot, since there's no underlying data and no complete shadow
- return null;
- }
- else {
- var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
- return compoundWriteApply(subMerge, layeredCache);
- }
- }
- }
- else {
- var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
- if (!includeHiddenWrites && compoundWriteIsEmpty(merge)) {
- return completeServerCache;
- }
- else {
- // If the server cache is null, and we don't have a complete cache, we need to return null
- if (!includeHiddenWrites &&
- completeServerCache == null &&
- !compoundWriteHasCompleteWrite(merge, newEmptyPath())) {
- return null;
- }
- else {
- var filter = function (write) {
- return ((write.visible || includeHiddenWrites) &&
- (!writeIdsToExclude ||
- !~writeIdsToExclude.indexOf(write.writeId)) &&
- (pathContains(write.path, treePath) ||
- pathContains(treePath, write.path)));
- };
- var mergeAtPath = writeTreeLayerTree_(writeTree.allWrites, filter, treePath);
- var layeredCache = completeServerCache || ChildrenNode.EMPTY_NODE;
- return compoundWriteApply(mergeAtPath, layeredCache);
- }
- }
- }
- }
- /**
- * With optional, underlying server data, attempt to return a children node of children that we have complete data for.
- * Used when creating new views, to pre-fill their complete event children snapshot.
- */
- function writeTreeCalcCompleteEventChildren(writeTree, treePath, completeServerChildren) {
- var completeChildren = ChildrenNode.EMPTY_NODE;
- var topLevelSet = compoundWriteGetCompleteNode(writeTree.visibleWrites, treePath);
- if (topLevelSet) {
- if (!topLevelSet.isLeafNode()) {
- // we're shadowing everything. Return the children.
- topLevelSet.forEachChild(PRIORITY_INDEX, function (childName, childSnap) {
- completeChildren = completeChildren.updateImmediateChild(childName, childSnap);
- });
- }
- return completeChildren;
- }
- else if (completeServerChildren) {
- // Layer any children we have on top of this
- // We know we don't have a top-level set, so just enumerate existing children
- var merge_1 = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
- completeServerChildren.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
- var node = compoundWriteApply(compoundWriteChildCompoundWrite(merge_1, new Path(childName)), childNode);
- completeChildren = completeChildren.updateImmediateChild(childName, node);
- });
- // Add any complete children we have from the set
- compoundWriteGetCompleteChildren(merge_1).forEach(function (namedNode) {
- completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
- });
- return completeChildren;
- }
- else {
- // We don't have anything to layer on top of. Layer on any children we have
- // Note that we can return an empty snap if we have a defined delete
- var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
- compoundWriteGetCompleteChildren(merge).forEach(function (namedNode) {
- completeChildren = completeChildren.updateImmediateChild(namedNode.name, namedNode.node);
- });
- return completeChildren;
- }
- }
- /**
- * Given that the underlying server data has updated, determine what, if anything, needs to be
- * applied to the event cache.
- *
- * Possibilities:
- *
- * 1. No writes are shadowing. Events should be raised, the snap to be applied comes from the server data
- *
- * 2. Some write is completely shadowing. No events to be raised
- *
- * 3. Is partially shadowed. Events
- *
- * Either existingEventSnap or existingServerSnap must exist
- */
- function writeTreeCalcEventCacheAfterServerOverwrite(writeTree, treePath, childPath, existingEventSnap, existingServerSnap) {
- util.assert(existingEventSnap || existingServerSnap, 'Either existingEventSnap or existingServerSnap must exist');
- var path = pathChild(treePath, childPath);
- if (compoundWriteHasCompleteWrite(writeTree.visibleWrites, path)) {
- // At this point we can probably guarantee that we're in case 2, meaning no events
- // May need to check visibility while doing the findRootMostValueAndPath call
- return null;
- }
- else {
- // No complete shadowing. We're either partially shadowing or not shadowing at all.
- var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
- if (compoundWriteIsEmpty(childMerge)) {
- // We're not shadowing at all. Case 1
- return existingServerSnap.getChild(childPath);
- }
- else {
- // This could be more efficient if the serverNode + updates doesn't change the eventSnap
- // However this is tricky to find out, since user updates don't necessary change the server
- // snap, e.g. priority updates on empty nodes, or deep deletes. Another special case is if the server
- // adds nodes, but doesn't change any existing writes. It is therefore not enough to
- // only check if the updates change the serverNode.
- // Maybe check if the merge tree contains these special cases and only do a full overwrite in that case?
- return compoundWriteApply(childMerge, existingServerSnap.getChild(childPath));
- }
- }
- }
- /**
- * Returns a complete child for a given server snap after applying all user writes or null if there is no
- * complete child for this ChildKey.
- */
- function writeTreeCalcCompleteChild(writeTree, treePath, childKey, existingServerSnap) {
- var path = pathChild(treePath, childKey);
- var shadowingNode = compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
- if (shadowingNode != null) {
- return shadowingNode;
- }
- else {
- if (existingServerSnap.isCompleteForChild(childKey)) {
- var childMerge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, path);
- return compoundWriteApply(childMerge, existingServerSnap.getNode().getImmediateChild(childKey));
- }
- else {
- return null;
- }
- }
- }
- /**
- * Returns a node if there is a complete overwrite for this path. More specifically, if there is a write at
- * a higher path, this will return the child of that write relative to the write and this path.
- * Returns null if there is no write at this path.
- */
- function writeTreeShadowingWrite(writeTree, path) {
- return compoundWriteGetCompleteNode(writeTree.visibleWrites, path);
- }
- /**
- * This method is used when processing child remove events on a query. If we can, we pull in children that were outside
- * the window, but may now be in the window.
- */
- function writeTreeCalcIndexedSlice(writeTree, treePath, completeServerData, startPost, count, reverse, index) {
- var toIterate;
- var merge = compoundWriteChildCompoundWrite(writeTree.visibleWrites, treePath);
- var shadowingNode = compoundWriteGetCompleteNode(merge, newEmptyPath());
- if (shadowingNode != null) {
- toIterate = shadowingNode;
- }
- else if (completeServerData != null) {
- toIterate = compoundWriteApply(merge, completeServerData);
- }
- else {
- // no children to iterate on
- return [];
- }
- toIterate = toIterate.withIndex(index);
- if (!toIterate.isEmpty() && !toIterate.isLeafNode()) {
- var nodes = [];
- var cmp = index.getCompare();
- var iter = reverse
- ? toIterate.getReverseIteratorFrom(startPost, index)
- : toIterate.getIteratorFrom(startPost, index);
- var next = iter.getNext();
- while (next && nodes.length < count) {
- if (cmp(next, startPost) !== 0) {
- nodes.push(next);
- }
- next = iter.getNext();
- }
- return nodes;
- }
- else {
- return [];
- }
- }
- function newWriteTree() {
- return {
- visibleWrites: CompoundWrite.empty(),
- allWrites: [],
- lastWriteId: -1
- };
- }
- /**
- * If possible, returns a complete event cache, using the underlying server data if possible. In addition, can be used
- * to get a cache that includes hidden writes, and excludes arbitrary writes. Note that customizing the returned node
- * can lead to a more expensive calculation.
- *
- * @param writeIdsToExclude - Optional writes to exclude.
- * @param includeHiddenWrites - Defaults to false, whether or not to layer on writes with visible set to false
- */
- function writeTreeRefCalcCompleteEventCache(writeTreeRef, completeServerCache, writeIdsToExclude, includeHiddenWrites) {
- return writeTreeCalcCompleteEventCache(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerCache, writeIdsToExclude, includeHiddenWrites);
- }
- /**
- * If possible, returns a children node containing all of the complete children we have data for. The returned data is a
- * mix of the given server data and write data.
- *
- */
- function writeTreeRefCalcCompleteEventChildren(writeTreeRef, completeServerChildren) {
- return writeTreeCalcCompleteEventChildren(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerChildren);
- }
- /**
- * Given that either the underlying server data has updated or the outstanding writes have updated, determine what,
- * if anything, needs to be applied to the event cache.
- *
- * Possibilities:
- *
- * 1. No writes are shadowing. Events should be raised, the snap to be applied comes from the server data
- *
- * 2. Some write is completely shadowing. No events to be raised
- *
- * 3. Is partially shadowed. Events should be raised
- *
- * Either existingEventSnap or existingServerSnap must exist, this is validated via an assert
- *
- *
- */
- function writeTreeRefCalcEventCacheAfterServerOverwrite(writeTreeRef, path, existingEventSnap, existingServerSnap) {
- return writeTreeCalcEventCacheAfterServerOverwrite(writeTreeRef.writeTree, writeTreeRef.treePath, path, existingEventSnap, existingServerSnap);
- }
- /**
- * Returns a node if there is a complete overwrite for this path. More specifically, if there is a write at
- * a higher path, this will return the child of that write relative to the write and this path.
- * Returns null if there is no write at this path.
- *
- */
- function writeTreeRefShadowingWrite(writeTreeRef, path) {
- return writeTreeShadowingWrite(writeTreeRef.writeTree, pathChild(writeTreeRef.treePath, path));
- }
- /**
- * This method is used when processing child remove events on a query. If we can, we pull in children that were outside
- * the window, but may now be in the window
- */
- function writeTreeRefCalcIndexedSlice(writeTreeRef, completeServerData, startPost, count, reverse, index) {
- return writeTreeCalcIndexedSlice(writeTreeRef.writeTree, writeTreeRef.treePath, completeServerData, startPost, count, reverse, index);
- }
- /**
- * Returns a complete child for a given server snap after applying all user writes or null if there is no
- * complete child for this ChildKey.
- */
- function writeTreeRefCalcCompleteChild(writeTreeRef, childKey, existingServerCache) {
- return writeTreeCalcCompleteChild(writeTreeRef.writeTree, writeTreeRef.treePath, childKey, existingServerCache);
- }
- /**
- * Return a WriteTreeRef for a child.
- */
- function writeTreeRefChild(writeTreeRef, childName) {
- return newWriteTreeRef(pathChild(writeTreeRef.treePath, childName), writeTreeRef.writeTree);
- }
- function newWriteTreeRef(path, writeTree) {
- return {
- treePath: path,
- writeTree: writeTree
- };
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var ChildChangeAccumulator = /** @class */ (function () {
- function ChildChangeAccumulator() {
- this.changeMap = new Map();
- }
- ChildChangeAccumulator.prototype.trackChildChange = function (change) {
- var type = change.type;
- var childKey = change.childName;
- util.assert(type === "child_added" /* ChangeType.CHILD_ADDED */ ||
- type === "child_changed" /* ChangeType.CHILD_CHANGED */ ||
- type === "child_removed" /* ChangeType.CHILD_REMOVED */, 'Only child changes supported for tracking');
- util.assert(childKey !== '.priority', 'Only non-priority child changes can be tracked.');
- var oldChange = this.changeMap.get(childKey);
- if (oldChange) {
- var oldType = oldChange.type;
- if (type === "child_added" /* ChangeType.CHILD_ADDED */ &&
- oldType === "child_removed" /* ChangeType.CHILD_REMOVED */) {
- this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.snapshotNode));
- }
- else if (type === "child_removed" /* ChangeType.CHILD_REMOVED */ &&
- oldType === "child_added" /* ChangeType.CHILD_ADDED */) {
- this.changeMap.delete(childKey);
- }
- else if (type === "child_removed" /* ChangeType.CHILD_REMOVED */ &&
- oldType === "child_changed" /* ChangeType.CHILD_CHANGED */) {
- this.changeMap.set(childKey, changeChildRemoved(childKey, oldChange.oldSnap));
- }
- else if (type === "child_changed" /* ChangeType.CHILD_CHANGED */ &&
- oldType === "child_added" /* ChangeType.CHILD_ADDED */) {
- this.changeMap.set(childKey, changeChildAdded(childKey, change.snapshotNode));
- }
- else if (type === "child_changed" /* ChangeType.CHILD_CHANGED */ &&
- oldType === "child_changed" /* ChangeType.CHILD_CHANGED */) {
- this.changeMap.set(childKey, changeChildChanged(childKey, change.snapshotNode, oldChange.oldSnap));
- }
- else {
- throw util.assertionError('Illegal combination of changes: ' +
- change +
- ' occurred after ' +
- oldChange);
- }
- }
- else {
- this.changeMap.set(childKey, change);
- }
- };
- ChildChangeAccumulator.prototype.getChanges = function () {
- return Array.from(this.changeMap.values());
- };
- return ChildChangeAccumulator;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An implementation of CompleteChildSource that never returns any additional children
- */
- // eslint-disable-next-line @typescript-eslint/naming-convention
- var NoCompleteChildSource_ = /** @class */ (function () {
- function NoCompleteChildSource_() {
- }
- NoCompleteChildSource_.prototype.getCompleteChild = function (childKey) {
- return null;
- };
- NoCompleteChildSource_.prototype.getChildAfterChild = function (index, child, reverse) {
- return null;
- };
- return NoCompleteChildSource_;
- }());
- /**
- * Singleton instance.
- */
- var NO_COMPLETE_CHILD_SOURCE = new NoCompleteChildSource_();
- /**
- * An implementation of CompleteChildSource that uses a WriteTree in addition to any other server data or
- * old event caches available to calculate complete children.
- */
- var WriteTreeCompleteChildSource = /** @class */ (function () {
- function WriteTreeCompleteChildSource(writes_, viewCache_, optCompleteServerCache_) {
- if (optCompleteServerCache_ === void 0) { optCompleteServerCache_ = null; }
- this.writes_ = writes_;
- this.viewCache_ = viewCache_;
- this.optCompleteServerCache_ = optCompleteServerCache_;
- }
- WriteTreeCompleteChildSource.prototype.getCompleteChild = function (childKey) {
- var node = this.viewCache_.eventCache;
- if (node.isCompleteForChild(childKey)) {
- return node.getNode().getImmediateChild(childKey);
- }
- else {
- var serverNode = this.optCompleteServerCache_ != null
- ? new CacheNode(this.optCompleteServerCache_, true, false)
- : this.viewCache_.serverCache;
- return writeTreeRefCalcCompleteChild(this.writes_, childKey, serverNode);
- }
- };
- WriteTreeCompleteChildSource.prototype.getChildAfterChild = function (index, child, reverse) {
- var completeServerData = this.optCompleteServerCache_ != null
- ? this.optCompleteServerCache_
- : viewCacheGetCompleteServerSnap(this.viewCache_);
- var nodes = writeTreeRefCalcIndexedSlice(this.writes_, completeServerData, child, 1, reverse, index);
- if (nodes.length === 0) {
- return null;
- }
- else {
- return nodes[0];
- }
- };
- return WriteTreeCompleteChildSource;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function newViewProcessor(filter) {
- return { filter: filter };
- }
- function viewProcessorAssertIndexed(viewProcessor, viewCache) {
- util.assert(viewCache.eventCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Event snap not indexed');
- util.assert(viewCache.serverCache.getNode().isIndexed(viewProcessor.filter.getIndex()), 'Server snap not indexed');
- }
- function viewProcessorApplyOperation(viewProcessor, oldViewCache, operation, writesCache, completeCache) {
- var accumulator = new ChildChangeAccumulator();
- var newViewCache, filterServerNode;
- if (operation.type === OperationType.OVERWRITE) {
- var overwrite = operation;
- if (overwrite.source.fromUser) {
- newViewCache = viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, accumulator);
- }
- else {
- util.assert(overwrite.source.fromServer, 'Unknown source.');
- // We filter the node if it's a tagged update or the node has been previously filtered and the
- // update is not at the root in which case it is ok (and necessary) to mark the node unfiltered
- // again
- filterServerNode =
- overwrite.source.tagged ||
- (oldViewCache.serverCache.isFiltered() && !pathIsEmpty(overwrite.path));
- newViewCache = viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, overwrite.path, overwrite.snap, writesCache, completeCache, filterServerNode, accumulator);
- }
- }
- else if (operation.type === OperationType.MERGE) {
- var merge = operation;
- if (merge.source.fromUser) {
- newViewCache = viewProcessorApplyUserMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, accumulator);
- }
- else {
- util.assert(merge.source.fromServer, 'Unknown source.');
- // We filter the node if it's a tagged update or the node has been previously filtered
- filterServerNode =
- merge.source.tagged || oldViewCache.serverCache.isFiltered();
- newViewCache = viewProcessorApplyServerMerge(viewProcessor, oldViewCache, merge.path, merge.children, writesCache, completeCache, filterServerNode, accumulator);
- }
- }
- else if (operation.type === OperationType.ACK_USER_WRITE) {
- var ackUserWrite = operation;
- if (!ackUserWrite.revert) {
- newViewCache = viewProcessorAckUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, ackUserWrite.affectedTree, writesCache, completeCache, accumulator);
- }
- else {
- newViewCache = viewProcessorRevertUserWrite(viewProcessor, oldViewCache, ackUserWrite.path, writesCache, completeCache, accumulator);
- }
- }
- else if (operation.type === OperationType.LISTEN_COMPLETE) {
- newViewCache = viewProcessorListenComplete(viewProcessor, oldViewCache, operation.path, writesCache, accumulator);
- }
- else {
- throw util.assertionError('Unknown operation type: ' + operation.type);
- }
- var changes = accumulator.getChanges();
- viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, changes);
- return { viewCache: newViewCache, changes: changes };
- }
- function viewProcessorMaybeAddValueEvent(oldViewCache, newViewCache, accumulator) {
- var eventSnap = newViewCache.eventCache;
- if (eventSnap.isFullyInitialized()) {
- var isLeafOrEmpty = eventSnap.getNode().isLeafNode() || eventSnap.getNode().isEmpty();
- var oldCompleteSnap = viewCacheGetCompleteEventSnap(oldViewCache);
- if (accumulator.length > 0 ||
- !oldViewCache.eventCache.isFullyInitialized() ||
- (isLeafOrEmpty && !eventSnap.getNode().equals(oldCompleteSnap)) ||
- !eventSnap.getNode().getPriority().equals(oldCompleteSnap.getPriority())) {
- accumulator.push(changeValue(viewCacheGetCompleteEventSnap(newViewCache)));
- }
- }
- }
- function viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, viewCache, changePath, writesCache, source, accumulator) {
- var oldEventSnap = viewCache.eventCache;
- if (writeTreeRefShadowingWrite(writesCache, changePath) != null) {
- // we have a shadowing write, ignore changes
- return viewCache;
- }
- else {
- var newEventCache = void 0, serverNode = void 0;
- if (pathIsEmpty(changePath)) {
- // TODO: figure out how this plays with "sliding ack windows"
- util.assert(viewCache.serverCache.isFullyInitialized(), 'If change path is empty, we must have complete server data');
- if (viewCache.serverCache.isFiltered()) {
- // We need to special case this, because we need to only apply writes to complete children, or
- // we might end up raising events for incomplete children. If the server data is filtered deep
- // writes cannot be guaranteed to be complete
- var serverCache = viewCacheGetCompleteServerSnap(viewCache);
- var completeChildren = serverCache instanceof ChildrenNode
- ? serverCache
- : ChildrenNode.EMPTY_NODE;
- var completeEventChildren = writeTreeRefCalcCompleteEventChildren(writesCache, completeChildren);
- newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeEventChildren, accumulator);
- }
- else {
- var completeNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
- newEventCache = viewProcessor.filter.updateFullNode(viewCache.eventCache.getNode(), completeNode, accumulator);
- }
- }
- else {
- var childKey = pathGetFront(changePath);
- if (childKey === '.priority') {
- util.assert(pathGetLength(changePath) === 1, "Can't have a priority with additional path components");
- var oldEventNode = oldEventSnap.getNode();
- serverNode = viewCache.serverCache.getNode();
- // we might have overwrites for this priority
- var updatedPriority = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventNode, serverNode);
- if (updatedPriority != null) {
- newEventCache = viewProcessor.filter.updatePriority(oldEventNode, updatedPriority);
- }
- else {
- // priority didn't change, keep old node
- newEventCache = oldEventSnap.getNode();
- }
- }
- else {
- var childChangePath = pathPopFront(changePath);
- // update child
- var newEventChild = void 0;
- if (oldEventSnap.isCompleteForChild(childKey)) {
- serverNode = viewCache.serverCache.getNode();
- var eventChildUpdate = writeTreeRefCalcEventCacheAfterServerOverwrite(writesCache, changePath, oldEventSnap.getNode(), serverNode);
- if (eventChildUpdate != null) {
- newEventChild = oldEventSnap
- .getNode()
- .getImmediateChild(childKey)
- .updateChild(childChangePath, eventChildUpdate);
- }
- else {
- // Nothing changed, just keep the old child
- newEventChild = oldEventSnap.getNode().getImmediateChild(childKey);
- }
- }
- else {
- newEventChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
- }
- if (newEventChild != null) {
- newEventCache = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newEventChild, childChangePath, source, accumulator);
- }
- else {
- // no complete child available or no change
- newEventCache = oldEventSnap.getNode();
- }
- }
- }
- return viewCacheUpdateEventSnap(viewCache, newEventCache, oldEventSnap.isFullyInitialized() || pathIsEmpty(changePath), viewProcessor.filter.filtersNodes());
- }
- }
- function viewProcessorApplyServerOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, filterServerNode, accumulator) {
- var oldServerSnap = oldViewCache.serverCache;
- var newServerCache;
- var serverFilter = filterServerNode
- ? viewProcessor.filter
- : viewProcessor.filter.getIndexedFilter();
- if (pathIsEmpty(changePath)) {
- newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), changedSnap, null);
- }
- else if (serverFilter.filtersNodes() && !oldServerSnap.isFiltered()) {
- // we want to filter the server node, but we didn't filter the server node yet, so simulate a full update
- var newServerNode = oldServerSnap
- .getNode()
- .updateChild(changePath, changedSnap);
- newServerCache = serverFilter.updateFullNode(oldServerSnap.getNode(), newServerNode, null);
- }
- else {
- var childKey = pathGetFront(changePath);
- if (!oldServerSnap.isCompleteForPath(changePath) &&
- pathGetLength(changePath) > 1) {
- // We don't update incomplete nodes with updates intended for other listeners
- return oldViewCache;
- }
- var childChangePath = pathPopFront(changePath);
- var childNode = oldServerSnap.getNode().getImmediateChild(childKey);
- var newChildNode = childNode.updateChild(childChangePath, changedSnap);
- if (childKey === '.priority') {
- newServerCache = serverFilter.updatePriority(oldServerSnap.getNode(), newChildNode);
- }
- else {
- newServerCache = serverFilter.updateChild(oldServerSnap.getNode(), childKey, newChildNode, childChangePath, NO_COMPLETE_CHILD_SOURCE, null);
- }
- }
- var newViewCache = viewCacheUpdateServerSnap(oldViewCache, newServerCache, oldServerSnap.isFullyInitialized() || pathIsEmpty(changePath), serverFilter.filtersNodes());
- var source = new WriteTreeCompleteChildSource(writesCache, newViewCache, completeCache);
- return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, changePath, writesCache, source, accumulator);
- }
- function viewProcessorApplyUserOverwrite(viewProcessor, oldViewCache, changePath, changedSnap, writesCache, completeCache, accumulator) {
- var oldEventSnap = oldViewCache.eventCache;
- var newViewCache, newEventCache;
- var source = new WriteTreeCompleteChildSource(writesCache, oldViewCache, completeCache);
- if (pathIsEmpty(changePath)) {
- newEventCache = viewProcessor.filter.updateFullNode(oldViewCache.eventCache.getNode(), changedSnap, accumulator);
- newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, true, viewProcessor.filter.filtersNodes());
- }
- else {
- var childKey = pathGetFront(changePath);
- if (childKey === '.priority') {
- newEventCache = viewProcessor.filter.updatePriority(oldViewCache.eventCache.getNode(), changedSnap);
- newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventCache, oldEventSnap.isFullyInitialized(), oldEventSnap.isFiltered());
- }
- else {
- var childChangePath = pathPopFront(changePath);
- var oldChild = oldEventSnap.getNode().getImmediateChild(childKey);
- var newChild = void 0;
- if (pathIsEmpty(childChangePath)) {
- // Child overwrite, we can replace the child
- newChild = changedSnap;
- }
- else {
- var childNode = source.getCompleteChild(childKey);
- if (childNode != null) {
- if (pathGetBack(childChangePath) === '.priority' &&
- childNode.getChild(pathParent(childChangePath)).isEmpty()) {
- // This is a priority update on an empty node. If this node exists on the server, the
- // server will send down the priority in the update, so ignore for now
- newChild = childNode;
- }
- else {
- newChild = childNode.updateChild(childChangePath, changedSnap);
- }
- }
- else {
- // There is no complete child node available
- newChild = ChildrenNode.EMPTY_NODE;
- }
- }
- if (!oldChild.equals(newChild)) {
- var newEventSnap = viewProcessor.filter.updateChild(oldEventSnap.getNode(), childKey, newChild, childChangePath, source, accumulator);
- newViewCache = viewCacheUpdateEventSnap(oldViewCache, newEventSnap, oldEventSnap.isFullyInitialized(), viewProcessor.filter.filtersNodes());
- }
- else {
- newViewCache = oldViewCache;
- }
- }
- }
- return newViewCache;
- }
- function viewProcessorCacheHasChild(viewCache, childKey) {
- return viewCache.eventCache.isCompleteForChild(childKey);
- }
- function viewProcessorApplyUserMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, accumulator) {
- // HACK: In the case of a limit query, there may be some changes that bump things out of the
- // window leaving room for new items. It's important we process these changes first, so we
- // iterate the changes twice, first processing any that affect items currently in view.
- // TODO: I consider an item "in view" if cacheHasChild is true, which checks both the server
- // and event snap. I'm not sure if this will result in edge cases when a child is in one but
- // not the other.
- var curViewCache = viewCache;
- changedChildren.foreach(function (relativePath, childNode) {
- var writePath = pathChild(path, relativePath);
- if (viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
- curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
- }
- });
- changedChildren.foreach(function (relativePath, childNode) {
- var writePath = pathChild(path, relativePath);
- if (!viewProcessorCacheHasChild(viewCache, pathGetFront(writePath))) {
- curViewCache = viewProcessorApplyUserOverwrite(viewProcessor, curViewCache, writePath, childNode, writesCache, serverCache, accumulator);
- }
- });
- return curViewCache;
- }
- function viewProcessorApplyMerge(viewProcessor, node, merge) {
- merge.foreach(function (relativePath, childNode) {
- node = node.updateChild(relativePath, childNode);
- });
- return node;
- }
- function viewProcessorApplyServerMerge(viewProcessor, viewCache, path, changedChildren, writesCache, serverCache, filterServerNode, accumulator) {
- // If we don't have a cache yet, this merge was intended for a previously listen in the same location. Ignore it and
- // wait for the complete data update coming soon.
- if (viewCache.serverCache.getNode().isEmpty() &&
- !viewCache.serverCache.isFullyInitialized()) {
- return viewCache;
- }
- // HACK: In the case of a limit query, there may be some changes that bump things out of the
- // window leaving room for new items. It's important we process these changes first, so we
- // iterate the changes twice, first processing any that affect items currently in view.
- // TODO: I consider an item "in view" if cacheHasChild is true, which checks both the server
- // and event snap. I'm not sure if this will result in edge cases when a child is in one but
- // not the other.
- var curViewCache = viewCache;
- var viewMergeTree;
- if (pathIsEmpty(path)) {
- viewMergeTree = changedChildren;
- }
- else {
- viewMergeTree = new ImmutableTree(null).setTree(path, changedChildren);
- }
- var serverNode = viewCache.serverCache.getNode();
- viewMergeTree.children.inorderTraversal(function (childKey, childTree) {
- if (serverNode.hasChild(childKey)) {
- var serverChild = viewCache.serverCache
- .getNode()
- .getImmediateChild(childKey);
- var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childTree);
- curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
- }
- });
- viewMergeTree.children.inorderTraversal(function (childKey, childMergeTree) {
- var isUnknownDeepMerge = !viewCache.serverCache.isCompleteForChild(childKey) &&
- childMergeTree.value === null;
- if (!serverNode.hasChild(childKey) && !isUnknownDeepMerge) {
- var serverChild = viewCache.serverCache
- .getNode()
- .getImmediateChild(childKey);
- var newChild = viewProcessorApplyMerge(viewProcessor, serverChild, childMergeTree);
- curViewCache = viewProcessorApplyServerOverwrite(viewProcessor, curViewCache, new Path(childKey), newChild, writesCache, serverCache, filterServerNode, accumulator);
- }
- });
- return curViewCache;
- }
- function viewProcessorAckUserWrite(viewProcessor, viewCache, ackPath, affectedTree, writesCache, completeCache, accumulator) {
- if (writeTreeRefShadowingWrite(writesCache, ackPath) != null) {
- return viewCache;
- }
- // Only filter server node if it is currently filtered
- var filterServerNode = viewCache.serverCache.isFiltered();
- // Essentially we'll just get our existing server cache for the affected paths and re-apply it as a server update
- // now that it won't be shadowed.
- var serverCache = viewCache.serverCache;
- if (affectedTree.value != null) {
- // This is an overwrite.
- if ((pathIsEmpty(ackPath) && serverCache.isFullyInitialized()) ||
- serverCache.isCompleteForPath(ackPath)) {
- return viewProcessorApplyServerOverwrite(viewProcessor, viewCache, ackPath, serverCache.getNode().getChild(ackPath), writesCache, completeCache, filterServerNode, accumulator);
- }
- else if (pathIsEmpty(ackPath)) {
- // This is a goofy edge case where we are acking data at this location but don't have full data. We
- // should just re-apply whatever we have in our cache as a merge.
- var changedChildren_1 = new ImmutableTree(null);
- serverCache.getNode().forEachChild(KEY_INDEX, function (name, node) {
- changedChildren_1 = changedChildren_1.set(new Path(name), node);
- });
- return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_1, writesCache, completeCache, filterServerNode, accumulator);
- }
- else {
- return viewCache;
- }
- }
- else {
- // This is a merge.
- var changedChildren_2 = new ImmutableTree(null);
- affectedTree.foreach(function (mergePath, value) {
- var serverCachePath = pathChild(ackPath, mergePath);
- if (serverCache.isCompleteForPath(serverCachePath)) {
- changedChildren_2 = changedChildren_2.set(mergePath, serverCache.getNode().getChild(serverCachePath));
- }
- });
- return viewProcessorApplyServerMerge(viewProcessor, viewCache, ackPath, changedChildren_2, writesCache, completeCache, filterServerNode, accumulator);
- }
- }
- function viewProcessorListenComplete(viewProcessor, viewCache, path, writesCache, accumulator) {
- var oldServerNode = viewCache.serverCache;
- var newViewCache = viewCacheUpdateServerSnap(viewCache, oldServerNode.getNode(), oldServerNode.isFullyInitialized() || pathIsEmpty(path), oldServerNode.isFiltered());
- return viewProcessorGenerateEventCacheAfterServerEvent(viewProcessor, newViewCache, path, writesCache, NO_COMPLETE_CHILD_SOURCE, accumulator);
- }
- function viewProcessorRevertUserWrite(viewProcessor, viewCache, path, writesCache, completeServerCache, accumulator) {
- var complete;
- if (writeTreeRefShadowingWrite(writesCache, path) != null) {
- return viewCache;
- }
- else {
- var source = new WriteTreeCompleteChildSource(writesCache, viewCache, completeServerCache);
- var oldEventCache = viewCache.eventCache.getNode();
- var newEventCache = void 0;
- if (pathIsEmpty(path) || pathGetFront(path) === '.priority') {
- var newNode = void 0;
- if (viewCache.serverCache.isFullyInitialized()) {
- newNode = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
- }
- else {
- var serverChildren = viewCache.serverCache.getNode();
- util.assert(serverChildren instanceof ChildrenNode, 'serverChildren would be complete if leaf node');
- newNode = writeTreeRefCalcCompleteEventChildren(writesCache, serverChildren);
- }
- newNode = newNode;
- newEventCache = viewProcessor.filter.updateFullNode(oldEventCache, newNode, accumulator);
- }
- else {
- var childKey = pathGetFront(path);
- var newChild = writeTreeRefCalcCompleteChild(writesCache, childKey, viewCache.serverCache);
- if (newChild == null &&
- viewCache.serverCache.isCompleteForChild(childKey)) {
- newChild = oldEventCache.getImmediateChild(childKey);
- }
- if (newChild != null) {
- newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, newChild, pathPopFront(path), source, accumulator);
- }
- else if (viewCache.eventCache.getNode().hasChild(childKey)) {
- // No complete child available, delete the existing one, if any
- newEventCache = viewProcessor.filter.updateChild(oldEventCache, childKey, ChildrenNode.EMPTY_NODE, pathPopFront(path), source, accumulator);
- }
- else {
- newEventCache = oldEventCache;
- }
- if (newEventCache.isEmpty() &&
- viewCache.serverCache.isFullyInitialized()) {
- // We might have reverted all child writes. Maybe the old event was a leaf node
- complete = writeTreeRefCalcCompleteEventCache(writesCache, viewCacheGetCompleteServerSnap(viewCache));
- if (complete.isLeafNode()) {
- newEventCache = viewProcessor.filter.updateFullNode(newEventCache, complete, accumulator);
- }
- }
- }
- complete =
- viewCache.serverCache.isFullyInitialized() ||
- writeTreeRefShadowingWrite(writesCache, newEmptyPath()) != null;
- return viewCacheUpdateEventSnap(viewCache, newEventCache, complete, viewProcessor.filter.filtersNodes());
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * A view represents a specific location and query that has 1 or more event registrations.
- *
- * It does several things:
- * - Maintains the list of event registrations for this location/query.
- * - Maintains a cache of the data visible for this location/query.
- * - Applies new operations (via applyOperation), updates the cache, and based on the event
- * registrations returns the set of events to be raised.
- */
- var View = /** @class */ (function () {
- function View(query_, initialViewCache) {
- this.query_ = query_;
- this.eventRegistrations_ = [];
- var params = this.query_._queryParams;
- var indexFilter = new IndexedFilter(params.getIndex());
- var filter = queryParamsGetNodeFilter(params);
- this.processor_ = newViewProcessor(filter);
- var initialServerCache = initialViewCache.serverCache;
- var initialEventCache = initialViewCache.eventCache;
- // Don't filter server node with other filter than index, wait for tagged listen
- var serverSnap = indexFilter.updateFullNode(ChildrenNode.EMPTY_NODE, initialServerCache.getNode(), null);
- var eventSnap = filter.updateFullNode(ChildrenNode.EMPTY_NODE, initialEventCache.getNode(), null);
- var newServerCache = new CacheNode(serverSnap, initialServerCache.isFullyInitialized(), indexFilter.filtersNodes());
- var newEventCache = new CacheNode(eventSnap, initialEventCache.isFullyInitialized(), filter.filtersNodes());
- this.viewCache_ = newViewCache(newEventCache, newServerCache);
- this.eventGenerator_ = new EventGenerator(this.query_);
- }
- Object.defineProperty(View.prototype, "query", {
- get: function () {
- return this.query_;
- },
- enumerable: false,
- configurable: true
- });
- return View;
- }());
- function viewGetServerCache(view) {
- return view.viewCache_.serverCache.getNode();
- }
- function viewGetCompleteNode(view) {
- return viewCacheGetCompleteEventSnap(view.viewCache_);
- }
- function viewGetCompleteServerCache(view, path) {
- var cache = viewCacheGetCompleteServerSnap(view.viewCache_);
- if (cache) {
- // If this isn't a "loadsAllData" view, then cache isn't actually a complete cache and
- // we need to see if it contains the child we're interested in.
- if (view.query._queryParams.loadsAllData() ||
- (!pathIsEmpty(path) &&
- !cache.getImmediateChild(pathGetFront(path)).isEmpty())) {
- return cache.getChild(path);
- }
- }
- return null;
- }
- function viewIsEmpty(view) {
- return view.eventRegistrations_.length === 0;
- }
- function viewAddEventRegistration(view, eventRegistration) {
- view.eventRegistrations_.push(eventRegistration);
- }
- /**
- * @param eventRegistration - If null, remove all callbacks.
- * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
- * @returns Cancel events, if cancelError was provided.
- */
- function viewRemoveEventRegistration(view, eventRegistration, cancelError) {
- var cancelEvents = [];
- if (cancelError) {
- util.assert(eventRegistration == null, 'A cancel should cancel all event registrations.');
- var path_1 = view.query._path;
- view.eventRegistrations_.forEach(function (registration) {
- var maybeEvent = registration.createCancelEvent(cancelError, path_1);
- if (maybeEvent) {
- cancelEvents.push(maybeEvent);
- }
- });
- }
- if (eventRegistration) {
- var remaining = [];
- for (var i = 0; i < view.eventRegistrations_.length; ++i) {
- var existing = view.eventRegistrations_[i];
- if (!existing.matches(eventRegistration)) {
- remaining.push(existing);
- }
- else if (eventRegistration.hasAnyCallback()) {
- // We're removing just this one
- remaining = remaining.concat(view.eventRegistrations_.slice(i + 1));
- break;
- }
- }
- view.eventRegistrations_ = remaining;
- }
- else {
- view.eventRegistrations_ = [];
- }
- return cancelEvents;
- }
- /**
- * Applies the given Operation, updates our cache, and returns the appropriate events.
- */
- function viewApplyOperation(view, operation, writesCache, completeServerCache) {
- if (operation.type === OperationType.MERGE &&
- operation.source.queryId !== null) {
- util.assert(viewCacheGetCompleteServerSnap(view.viewCache_), 'We should always have a full cache before handling merges');
- util.assert(viewCacheGetCompleteEventSnap(view.viewCache_), 'Missing event cache, even though we have a server cache');
- }
- var oldViewCache = view.viewCache_;
- var result = viewProcessorApplyOperation(view.processor_, oldViewCache, operation, writesCache, completeServerCache);
- viewProcessorAssertIndexed(view.processor_, result.viewCache);
- util.assert(result.viewCache.serverCache.isFullyInitialized() ||
- !oldViewCache.serverCache.isFullyInitialized(), 'Once a server snap is complete, it should never go back');
- view.viewCache_ = result.viewCache;
- return viewGenerateEventsForChanges_(view, result.changes, result.viewCache.eventCache.getNode(), null);
- }
- function viewGetInitialEvents(view, registration) {
- var eventSnap = view.viewCache_.eventCache;
- var initialChanges = [];
- if (!eventSnap.getNode().isLeafNode()) {
- var eventNode = eventSnap.getNode();
- eventNode.forEachChild(PRIORITY_INDEX, function (key, childNode) {
- initialChanges.push(changeChildAdded(key, childNode));
- });
- }
- if (eventSnap.isFullyInitialized()) {
- initialChanges.push(changeValue(eventSnap.getNode()));
- }
- return viewGenerateEventsForChanges_(view, initialChanges, eventSnap.getNode(), registration);
- }
- function viewGenerateEventsForChanges_(view, changes, eventCache, eventRegistration) {
- var registrations = eventRegistration
- ? [eventRegistration]
- : view.eventRegistrations_;
- return eventGeneratorGenerateEventsForChanges(view.eventGenerator_, changes, eventCache, registrations);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var referenceConstructor$1;
- /**
- * SyncPoint represents a single location in a SyncTree with 1 or more event registrations, meaning we need to
- * maintain 1 or more Views at this location to cache server data and raise appropriate events for server changes
- * and user writes (set, transaction, update).
- *
- * It's responsible for:
- * - Maintaining the set of 1 or more views necessary at this location (a SyncPoint with 0 views should be removed).
- * - Proxying user / server operations to the views as appropriate (i.e. applyServerOverwrite,
- * applyUserOverwrite, etc.)
- */
- var SyncPoint = /** @class */ (function () {
- function SyncPoint() {
- /**
- * The Views being tracked at this location in the tree, stored as a map where the key is a
- * queryId and the value is the View for that query.
- *
- * NOTE: This list will be quite small (usually 1, but perhaps 2 or 3; any more is an odd use case).
- */
- this.views = new Map();
- }
- return SyncPoint;
- }());
- function syncPointSetReferenceConstructor(val) {
- util.assert(!referenceConstructor$1, '__referenceConstructor has already been defined');
- referenceConstructor$1 = val;
- }
- function syncPointGetReferenceConstructor() {
- util.assert(referenceConstructor$1, 'Reference.ts has not been loaded');
- return referenceConstructor$1;
- }
- function syncPointIsEmpty(syncPoint) {
- return syncPoint.views.size === 0;
- }
- function syncPointApplyOperation(syncPoint, operation, writesCache, optCompleteServerCache) {
- var e_1, _a;
- var queryId = operation.source.queryId;
- if (queryId !== null) {
- var view = syncPoint.views.get(queryId);
- util.assert(view != null, 'SyncTree gave us an op for an invalid query.');
- return viewApplyOperation(view, operation, writesCache, optCompleteServerCache);
- }
- else {
- var events = [];
- try {
- for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var view = _c.value;
- events = events.concat(viewApplyOperation(view, operation, writesCache, optCompleteServerCache));
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- return events;
- }
- }
- /**
- * Get a view for the specified query.
- *
- * @param query - The query to return a view for
- * @param writesCache
- * @param serverCache
- * @param serverCacheComplete
- * @returns Events to raise.
- */
- function syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete) {
- var queryId = query._queryIdentifier;
- var view = syncPoint.views.get(queryId);
- if (!view) {
- // TODO: make writesCache take flag for complete server node
- var eventCache = writeTreeRefCalcCompleteEventCache(writesCache, serverCacheComplete ? serverCache : null);
- var eventCacheComplete = false;
- if (eventCache) {
- eventCacheComplete = true;
- }
- else if (serverCache instanceof ChildrenNode) {
- eventCache = writeTreeRefCalcCompleteEventChildren(writesCache, serverCache);
- eventCacheComplete = false;
- }
- else {
- eventCache = ChildrenNode.EMPTY_NODE;
- eventCacheComplete = false;
- }
- var viewCache = newViewCache(new CacheNode(eventCache, eventCacheComplete, false), new CacheNode(serverCache, serverCacheComplete, false));
- return new View(query, viewCache);
- }
- return view;
- }
- /**
- * Add an event callback for the specified query.
- *
- * @param query
- * @param eventRegistration
- * @param writesCache
- * @param serverCache - Complete server cache, if we have it.
- * @param serverCacheComplete
- * @returns Events to raise.
- */
- function syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete) {
- var view = syncPointGetView(syncPoint, query, writesCache, serverCache, serverCacheComplete);
- if (!syncPoint.views.has(query._queryIdentifier)) {
- syncPoint.views.set(query._queryIdentifier, view);
- }
- // This is guaranteed to exist now, we just created anything that was missing
- viewAddEventRegistration(view, eventRegistration);
- return viewGetInitialEvents(view, eventRegistration);
- }
- /**
- * Remove event callback(s). Return cancelEvents if a cancelError is specified.
- *
- * If query is the default query, we'll check all views for the specified eventRegistration.
- * If eventRegistration is null, we'll remove all callbacks for the specified view(s).
- *
- * @param eventRegistration - If null, remove all callbacks.
- * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
- * @returns removed queries and any cancel events
- */
- function syncPointRemoveEventRegistration(syncPoint, query, eventRegistration, cancelError) {
- var e_2, _a;
- var queryId = query._queryIdentifier;
- var removed = [];
- var cancelEvents = [];
- var hadCompleteView = syncPointHasCompleteView(syncPoint);
- if (queryId === 'default') {
- try {
- // When you do ref.off(...), we search all views for the registration to remove.
- for (var _b = tslib.__values(syncPoint.views.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var _d = tslib.__read(_c.value, 2), viewQueryId = _d[0], view = _d[1];
- cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
- if (viewIsEmpty(view)) {
- syncPoint.views.delete(viewQueryId);
- // We'll deal with complete views later.
- if (!view.query._queryParams.loadsAllData()) {
- removed.push(view.query);
- }
- }
- }
- }
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_2) throw e_2.error; }
- }
- }
- else {
- // remove the callback from the specific view.
- var view = syncPoint.views.get(queryId);
- if (view) {
- cancelEvents = cancelEvents.concat(viewRemoveEventRegistration(view, eventRegistration, cancelError));
- if (viewIsEmpty(view)) {
- syncPoint.views.delete(queryId);
- // We'll deal with complete views later.
- if (!view.query._queryParams.loadsAllData()) {
- removed.push(view.query);
- }
- }
- }
- }
- if (hadCompleteView && !syncPointHasCompleteView(syncPoint)) {
- // We removed our last complete view.
- removed.push(new (syncPointGetReferenceConstructor())(query._repo, query._path));
- }
- return { removed: removed, events: cancelEvents };
- }
- function syncPointGetQueryViews(syncPoint) {
- var e_3, _a;
- var result = [];
- try {
- for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var view = _c.value;
- if (!view.query._queryParams.loadsAllData()) {
- result.push(view);
- }
- }
- }
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_3) throw e_3.error; }
- }
- return result;
- }
- /**
- * @param path - The path to the desired complete snapshot
- * @returns A complete cache, if it exists
- */
- function syncPointGetCompleteServerCache(syncPoint, path) {
- var e_4, _a;
- var serverCache = null;
- try {
- for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var view = _c.value;
- serverCache = serverCache || viewGetCompleteServerCache(view, path);
- }
- }
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_4) throw e_4.error; }
- }
- return serverCache;
- }
- function syncPointViewForQuery(syncPoint, query) {
- var params = query._queryParams;
- if (params.loadsAllData()) {
- return syncPointGetCompleteView(syncPoint);
- }
- else {
- var queryId = query._queryIdentifier;
- return syncPoint.views.get(queryId);
- }
- }
- function syncPointViewExistsForQuery(syncPoint, query) {
- return syncPointViewForQuery(syncPoint, query) != null;
- }
- function syncPointHasCompleteView(syncPoint) {
- return syncPointGetCompleteView(syncPoint) != null;
- }
- function syncPointGetCompleteView(syncPoint) {
- var e_5, _a;
- try {
- for (var _b = tslib.__values(syncPoint.views.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var view = _c.value;
- if (view.query._queryParams.loadsAllData()) {
- return view;
- }
- }
- }
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_5) throw e_5.error; }
- }
- return null;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var referenceConstructor;
- function syncTreeSetReferenceConstructor(val) {
- util.assert(!referenceConstructor, '__referenceConstructor has already been defined');
- referenceConstructor = val;
- }
- function syncTreeGetReferenceConstructor() {
- util.assert(referenceConstructor, 'Reference.ts has not been loaded');
- return referenceConstructor;
- }
- /**
- * Static tracker for next query tag.
- */
- var syncTreeNextQueryTag_ = 1;
- /**
- * SyncTree is the central class for managing event callback registration, data caching, views
- * (query processing), and event generation. There are typically two SyncTree instances for
- * each Repo, one for the normal Firebase data, and one for the .info data.
- *
- * It has a number of responsibilities, including:
- * - Tracking all user event callbacks (registered via addEventRegistration() and removeEventRegistration()).
- * - Applying and caching data changes for user set(), transaction(), and update() calls
- * (applyUserOverwrite(), applyUserMerge()).
- * - Applying and caching data changes for server data changes (applyServerOverwrite(),
- * applyServerMerge()).
- * - Generating user-facing events for server and user changes (all of the apply* methods
- * return the set of events that need to be raised as a result).
- * - Maintaining the appropriate set of server listens to ensure we are always subscribed
- * to the correct set of paths and queries to satisfy the current set of user event
- * callbacks (listens are started/stopped using the provided listenProvider).
- *
- * NOTE: Although SyncTree tracks event callbacks and calculates events to raise, the actual
- * events are returned to the caller rather than raised synchronously.
- *
- */
- var SyncTree = /** @class */ (function () {
- /**
- * @param listenProvider_ - Used by SyncTree to start / stop listening
- * to server data.
- */
- function SyncTree(listenProvider_) {
- this.listenProvider_ = listenProvider_;
- /**
- * Tree of SyncPoints. There's a SyncPoint at any location that has 1 or more views.
- */
- this.syncPointTree_ = new ImmutableTree(null);
- /**
- * A tree of all pending user writes (user-initiated set()'s, transaction()'s, update()'s, etc.).
- */
- this.pendingWriteTree_ = newWriteTree();
- this.tagToQueryMap = new Map();
- this.queryToTagMap = new Map();
- }
- return SyncTree;
- }());
- /**
- * Apply the data changes for a user-generated set() or transaction() call.
- *
- * @returns Events to raise.
- */
- function syncTreeApplyUserOverwrite(syncTree, path, newData, writeId, visible) {
- // Record pending write.
- writeTreeAddOverwrite(syncTree.pendingWriteTree_, path, newData, writeId, visible);
- if (!visible) {
- return [];
- }
- else {
- return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceUser(), path, newData));
- }
- }
- /**
- * Apply the data from a user-generated update() call
- *
- * @returns Events to raise.
- */
- function syncTreeApplyUserMerge(syncTree, path, changedChildren, writeId) {
- // Record pending merge.
- writeTreeAddMerge(syncTree.pendingWriteTree_, path, changedChildren, writeId);
- var changeTree = ImmutableTree.fromObject(changedChildren);
- return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceUser(), path, changeTree));
- }
- /**
- * Acknowledge a pending user write that was previously registered with applyUserOverwrite() or applyUserMerge().
- *
- * @param revert - True if the given write failed and needs to be reverted
- * @returns Events to raise.
- */
- function syncTreeAckUserWrite(syncTree, writeId, revert) {
- if (revert === void 0) { revert = false; }
- var write = writeTreeGetWrite(syncTree.pendingWriteTree_, writeId);
- var needToReevaluate = writeTreeRemoveWrite(syncTree.pendingWriteTree_, writeId);
- if (!needToReevaluate) {
- return [];
- }
- else {
- var affectedTree_1 = new ImmutableTree(null);
- if (write.snap != null) {
- // overwrite
- affectedTree_1 = affectedTree_1.set(newEmptyPath(), true);
- }
- else {
- each(write.children, function (pathString) {
- affectedTree_1 = affectedTree_1.set(new Path(pathString), true);
- });
- }
- return syncTreeApplyOperationToSyncPoints_(syncTree, new AckUserWrite(write.path, affectedTree_1, revert));
- }
- }
- /**
- * Apply new server data for the specified path..
- *
- * @returns Events to raise.
- */
- function syncTreeApplyServerOverwrite(syncTree, path, newData) {
- return syncTreeApplyOperationToSyncPoints_(syncTree, new Overwrite(newOperationSourceServer(), path, newData));
- }
- /**
- * Apply new server data to be merged in at the specified path.
- *
- * @returns Events to raise.
- */
- function syncTreeApplyServerMerge(syncTree, path, changedChildren) {
- var changeTree = ImmutableTree.fromObject(changedChildren);
- return syncTreeApplyOperationToSyncPoints_(syncTree, new Merge(newOperationSourceServer(), path, changeTree));
- }
- /**
- * Apply a listen complete for a query
- *
- * @returns Events to raise.
- */
- function syncTreeApplyListenComplete(syncTree, path) {
- return syncTreeApplyOperationToSyncPoints_(syncTree, new ListenComplete(newOperationSourceServer(), path));
- }
- /**
- * Apply a listen complete for a tagged query
- *
- * @returns Events to raise.
- */
- function syncTreeApplyTaggedListenComplete(syncTree, path, tag) {
- var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
- if (queryKey) {
- var r = syncTreeParseQueryKey_(queryKey);
- var queryPath = r.path, queryId = r.queryId;
- var relativePath = newRelativePath(queryPath, path);
- var op = new ListenComplete(newOperationSourceServerTaggedQuery(queryId), relativePath);
- return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
- }
- else {
- // We've already removed the query. No big deal, ignore the update
- return [];
- }
- }
- /**
- * Remove event callback(s).
- *
- * If query is the default query, we'll check all queries for the specified eventRegistration.
- * If eventRegistration is null, we'll remove all callbacks for the specified query/queries.
- *
- * @param eventRegistration - If null, all callbacks are removed.
- * @param cancelError - If a cancelError is provided, appropriate cancel events will be returned.
- * @param skipListenerDedup - When performing a `get()`, we don't add any new listeners, so no
- * deduping needs to take place. This flag allows toggling of that behavior
- * @returns Cancel events, if cancelError was provided.
- */
- function syncTreeRemoveEventRegistration(syncTree, query, eventRegistration, cancelError, skipListenerDedup) {
- if (skipListenerDedup === void 0) { skipListenerDedup = false; }
- // Find the syncPoint first. Then deal with whether or not it has matching listeners
- var path = query._path;
- var maybeSyncPoint = syncTree.syncPointTree_.get(path);
- var cancelEvents = [];
- // A removal on a default query affects all queries at that location. A removal on an indexed query, even one without
- // other query constraints, does *not* affect all queries at that location. So this check must be for 'default', and
- // not loadsAllData().
- if (maybeSyncPoint &&
- (query._queryIdentifier === 'default' ||
- syncPointViewExistsForQuery(maybeSyncPoint, query))) {
- var removedAndEvents = syncPointRemoveEventRegistration(maybeSyncPoint, query, eventRegistration, cancelError);
- if (syncPointIsEmpty(maybeSyncPoint)) {
- syncTree.syncPointTree_ = syncTree.syncPointTree_.remove(path);
- }
- var removed = removedAndEvents.removed;
- cancelEvents = removedAndEvents.events;
- if (!skipListenerDedup) {
- /**
- * We may have just removed one of many listeners and can short-circuit this whole process
- * We may also not have removed a default listener, in which case all of the descendant listeners should already be
- * properly set up.
- */
- // Since indexed queries can shadow if they don't have other query constraints, check for loadsAllData(), instead of
- // queryId === 'default'
- var removingDefault = -1 !==
- removed.findIndex(function (query) {
- return query._queryParams.loadsAllData();
- });
- var covered = syncTree.syncPointTree_.findOnPath(path, function (relativePath, parentSyncPoint) {
- return syncPointHasCompleteView(parentSyncPoint);
- });
- if (removingDefault && !covered) {
- var subtree = syncTree.syncPointTree_.subtree(path);
- // There are potentially child listeners. Determine what if any listens we need to send before executing the
- // removal
- if (!subtree.isEmpty()) {
- // We need to fold over our subtree and collect the listeners to send
- var newViews = syncTreeCollectDistinctViewsForSubTree_(subtree);
- // Ok, we've collected all the listens we need. Set them up.
- for (var i = 0; i < newViews.length; ++i) {
- var view = newViews[i], newQuery = view.query;
- var listener = syncTreeCreateListenerForView_(syncTree, view);
- syncTree.listenProvider_.startListening(syncTreeQueryForListening_(newQuery), syncTreeTagForQuery(syncTree, newQuery), listener.hashFn, listener.onComplete);
- }
- }
- // Otherwise there's nothing below us, so nothing we need to start listening on
- }
- // If we removed anything and we're not covered by a higher up listen, we need to stop listening on this query
- // The above block has us covered in terms of making sure we're set up on listens lower in the tree.
- // Also, note that if we have a cancelError, it's already been removed at the provider level.
- if (!covered && removed.length > 0 && !cancelError) {
- // If we removed a default, then we weren't listening on any of the other queries here. Just cancel the one
- // default. Otherwise, we need to iterate through and cancel each individual query
- if (removingDefault) {
- // We don't tag default listeners
- var defaultTag = null;
- syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(query), defaultTag);
- }
- else {
- removed.forEach(function (queryToRemove) {
- var tagToRemove = syncTree.queryToTagMap.get(syncTreeMakeQueryKey_(queryToRemove));
- syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToRemove), tagToRemove);
- });
- }
- }
- }
- // Now, clear all of the tags we're tracking for the removed listens
- syncTreeRemoveTags_(syncTree, removed);
- }
- return cancelEvents;
- }
- /**
- * Apply new server data for the specified tagged query.
- *
- * @returns Events to raise.
- */
- function syncTreeApplyTaggedQueryOverwrite(syncTree, path, snap, tag) {
- var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
- if (queryKey != null) {
- var r = syncTreeParseQueryKey_(queryKey);
- var queryPath = r.path, queryId = r.queryId;
- var relativePath = newRelativePath(queryPath, path);
- var op = new Overwrite(newOperationSourceServerTaggedQuery(queryId), relativePath, snap);
- return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
- }
- else {
- // Query must have been removed already
- return [];
- }
- }
- /**
- * Apply server data to be merged in for the specified tagged query.
- *
- * @returns Events to raise.
- */
- function syncTreeApplyTaggedQueryMerge(syncTree, path, changedChildren, tag) {
- var queryKey = syncTreeQueryKeyForTag_(syncTree, tag);
- if (queryKey) {
- var r = syncTreeParseQueryKey_(queryKey);
- var queryPath = r.path, queryId = r.queryId;
- var relativePath = newRelativePath(queryPath, path);
- var changeTree = ImmutableTree.fromObject(changedChildren);
- var op = new Merge(newOperationSourceServerTaggedQuery(queryId), relativePath, changeTree);
- return syncTreeApplyTaggedOperation_(syncTree, queryPath, op);
- }
- else {
- // We've already removed the query. No big deal, ignore the update
- return [];
- }
- }
- /**
- * Add an event callback for the specified query.
- *
- * @returns Events to raise.
- */
- function syncTreeAddEventRegistration(syncTree, query, eventRegistration, skipSetupListener) {
- if (skipSetupListener === void 0) { skipSetupListener = false; }
- var path = query._path;
- var serverCache = null;
- var foundAncestorDefaultView = false;
- // Any covering writes will necessarily be at the root, so really all we need to find is the server cache.
- // Consider optimizing this once there's a better understanding of what actual behavior will be.
- syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
- var relativePath = newRelativePath(pathToSyncPoint, path);
- serverCache =
- serverCache || syncPointGetCompleteServerCache(sp, relativePath);
- foundAncestorDefaultView =
- foundAncestorDefaultView || syncPointHasCompleteView(sp);
- });
- var syncPoint = syncTree.syncPointTree_.get(path);
- if (!syncPoint) {
- syncPoint = new SyncPoint();
- syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
- }
- else {
- foundAncestorDefaultView =
- foundAncestorDefaultView || syncPointHasCompleteView(syncPoint);
- serverCache =
- serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
- }
- var serverCacheComplete;
- if (serverCache != null) {
- serverCacheComplete = true;
- }
- else {
- serverCacheComplete = false;
- serverCache = ChildrenNode.EMPTY_NODE;
- var subtree = syncTree.syncPointTree_.subtree(path);
- subtree.foreachChild(function (childName, childSyncPoint) {
- var completeCache = syncPointGetCompleteServerCache(childSyncPoint, newEmptyPath());
- if (completeCache) {
- serverCache = serverCache.updateImmediateChild(childName, completeCache);
- }
- });
- }
- var viewAlreadyExists = syncPointViewExistsForQuery(syncPoint, query);
- if (!viewAlreadyExists && !query._queryParams.loadsAllData()) {
- // We need to track a tag for this query
- var queryKey = syncTreeMakeQueryKey_(query);
- util.assert(!syncTree.queryToTagMap.has(queryKey), 'View does not exist, but we have a tag');
- var tag = syncTreeGetNextQueryTag_();
- syncTree.queryToTagMap.set(queryKey, tag);
- syncTree.tagToQueryMap.set(tag, queryKey);
- }
- var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
- var events = syncPointAddEventRegistration(syncPoint, query, eventRegistration, writesCache, serverCache, serverCacheComplete);
- if (!viewAlreadyExists && !foundAncestorDefaultView && !skipSetupListener) {
- var view = syncPointViewForQuery(syncPoint, query);
- events = events.concat(syncTreeSetupListener_(syncTree, query, view));
- }
- return events;
- }
- /**
- * Returns a complete cache, if we have one, of the data at a particular path. If the location does not have a
- * listener above it, we will get a false "null". This shouldn't be a problem because transactions will always
- * have a listener above, and atomic operations would correctly show a jitter of <increment value> ->
- * <incremented total> as the write is applied locally and then acknowledged at the server.
- *
- * Note: this method will *include* hidden writes from transaction with applyLocally set to false.
- *
- * @param path - The path to the data we want
- * @param writeIdsToExclude - A specific set to be excluded
- */
- function syncTreeCalcCompleteEventCache(syncTree, path, writeIdsToExclude) {
- var includeHiddenSets = true;
- var writeTree = syncTree.pendingWriteTree_;
- var serverCache = syncTree.syncPointTree_.findOnPath(path, function (pathSoFar, syncPoint) {
- var relativePath = newRelativePath(pathSoFar, path);
- var serverCache = syncPointGetCompleteServerCache(syncPoint, relativePath);
- if (serverCache) {
- return serverCache;
- }
- });
- return writeTreeCalcCompleteEventCache(writeTree, path, serverCache, writeIdsToExclude, includeHiddenSets);
- }
- function syncTreeGetServerValue(syncTree, query) {
- var path = query._path;
- var serverCache = null;
- // Any covering writes will necessarily be at the root, so really all we need to find is the server cache.
- // Consider optimizing this once there's a better understanding of what actual behavior will be.
- syncTree.syncPointTree_.foreachOnPath(path, function (pathToSyncPoint, sp) {
- var relativePath = newRelativePath(pathToSyncPoint, path);
- serverCache =
- serverCache || syncPointGetCompleteServerCache(sp, relativePath);
- });
- var syncPoint = syncTree.syncPointTree_.get(path);
- if (!syncPoint) {
- syncPoint = new SyncPoint();
- syncTree.syncPointTree_ = syncTree.syncPointTree_.set(path, syncPoint);
- }
- else {
- serverCache =
- serverCache || syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
- }
- var serverCacheComplete = serverCache != null;
- var serverCacheNode = serverCacheComplete
- ? new CacheNode(serverCache, true, false)
- : null;
- var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, query._path);
- var view = syncPointGetView(syncPoint, query, writesCache, serverCacheComplete ? serverCacheNode.getNode() : ChildrenNode.EMPTY_NODE, serverCacheComplete);
- return viewGetCompleteNode(view);
- }
- /**
- * A helper method that visits all descendant and ancestor SyncPoints, applying the operation.
- *
- * NOTES:
- * - Descendant SyncPoints will be visited first (since we raise events depth-first).
- *
- * - We call applyOperation() on each SyncPoint passing three things:
- * 1. A version of the Operation that has been made relative to the SyncPoint location.
- * 2. A WriteTreeRef of any writes we have cached at the SyncPoint location.
- * 3. A snapshot Node with cached server data, if we have it.
- *
- * - We concatenate all of the events returned by each SyncPoint and return the result.
- */
- function syncTreeApplyOperationToSyncPoints_(syncTree, operation) {
- return syncTreeApplyOperationHelper_(operation, syncTree.syncPointTree_,
- /*serverCache=*/ null, writeTreeChildWrites(syncTree.pendingWriteTree_, newEmptyPath()));
- }
- /**
- * Recursive helper for applyOperationToSyncPoints_
- */
- function syncTreeApplyOperationHelper_(operation, syncPointTree, serverCache, writesCache) {
- if (pathIsEmpty(operation.path)) {
- return syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache);
- }
- else {
- var syncPoint = syncPointTree.get(newEmptyPath());
- // If we don't have cached server data, see if we can get it from this SyncPoint.
- if (serverCache == null && syncPoint != null) {
- serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
- }
- var events = [];
- var childName = pathGetFront(operation.path);
- var childOperation = operation.operationForChild(childName);
- var childTree = syncPointTree.children.get(childName);
- if (childTree && childOperation) {
- var childServerCache = serverCache
- ? serverCache.getImmediateChild(childName)
- : null;
- var childWritesCache = writeTreeRefChild(writesCache, childName);
- events = events.concat(syncTreeApplyOperationHelper_(childOperation, childTree, childServerCache, childWritesCache));
- }
- if (syncPoint) {
- events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
- }
- return events;
- }
- }
- /**
- * Recursive helper for applyOperationToSyncPoints_
- */
- function syncTreeApplyOperationDescendantsHelper_(operation, syncPointTree, serverCache, writesCache) {
- var syncPoint = syncPointTree.get(newEmptyPath());
- // If we don't have cached server data, see if we can get it from this SyncPoint.
- if (serverCache == null && syncPoint != null) {
- serverCache = syncPointGetCompleteServerCache(syncPoint, newEmptyPath());
- }
- var events = [];
- syncPointTree.children.inorderTraversal(function (childName, childTree) {
- var childServerCache = serverCache
- ? serverCache.getImmediateChild(childName)
- : null;
- var childWritesCache = writeTreeRefChild(writesCache, childName);
- var childOperation = operation.operationForChild(childName);
- if (childOperation) {
- events = events.concat(syncTreeApplyOperationDescendantsHelper_(childOperation, childTree, childServerCache, childWritesCache));
- }
- });
- if (syncPoint) {
- events = events.concat(syncPointApplyOperation(syncPoint, operation, writesCache, serverCache));
- }
- return events;
- }
- function syncTreeCreateListenerForView_(syncTree, view) {
- var query = view.query;
- var tag = syncTreeTagForQuery(syncTree, query);
- return {
- hashFn: function () {
- var cache = viewGetServerCache(view) || ChildrenNode.EMPTY_NODE;
- return cache.hash();
- },
- onComplete: function (status) {
- if (status === 'ok') {
- if (tag) {
- return syncTreeApplyTaggedListenComplete(syncTree, query._path, tag);
- }
- else {
- return syncTreeApplyListenComplete(syncTree, query._path);
- }
- }
- else {
- // If a listen failed, kill all of the listeners here, not just the one that triggered the error.
- // Note that this may need to be scoped to just this listener if we change permissions on filtered children
- var error = errorForServerCode(status, query);
- return syncTreeRemoveEventRegistration(syncTree, query,
- /*eventRegistration*/ null, error);
- }
- }
- };
- }
- /**
- * Return the tag associated with the given query.
- */
- function syncTreeTagForQuery(syncTree, query) {
- var queryKey = syncTreeMakeQueryKey_(query);
- return syncTree.queryToTagMap.get(queryKey);
- }
- /**
- * Given a query, computes a "queryKey" suitable for use in our queryToTagMap_.
- */
- function syncTreeMakeQueryKey_(query) {
- return query._path.toString() + '$' + query._queryIdentifier;
- }
- /**
- * Return the query associated with the given tag, if we have one
- */
- function syncTreeQueryKeyForTag_(syncTree, tag) {
- return syncTree.tagToQueryMap.get(tag);
- }
- /**
- * Given a queryKey (created by makeQueryKey), parse it back into a path and queryId.
- */
- function syncTreeParseQueryKey_(queryKey) {
- var splitIndex = queryKey.indexOf('$');
- util.assert(splitIndex !== -1 && splitIndex < queryKey.length - 1, 'Bad queryKey.');
- return {
- queryId: queryKey.substr(splitIndex + 1),
- path: new Path(queryKey.substr(0, splitIndex))
- };
- }
- /**
- * A helper method to apply tagged operations
- */
- function syncTreeApplyTaggedOperation_(syncTree, queryPath, operation) {
- var syncPoint = syncTree.syncPointTree_.get(queryPath);
- util.assert(syncPoint, "Missing sync point for query tag that we're tracking");
- var writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, queryPath);
- return syncPointApplyOperation(syncPoint, operation, writesCache, null);
- }
- /**
- * This collapses multiple unfiltered views into a single view, since we only need a single
- * listener for them.
- */
- function syncTreeCollectDistinctViewsForSubTree_(subtree) {
- return subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
- if (maybeChildSyncPoint && syncPointHasCompleteView(maybeChildSyncPoint)) {
- var completeView = syncPointGetCompleteView(maybeChildSyncPoint);
- return [completeView];
- }
- else {
- // No complete view here, flatten any deeper listens into an array
- var views_1 = [];
- if (maybeChildSyncPoint) {
- views_1 = syncPointGetQueryViews(maybeChildSyncPoint);
- }
- each(childMap, function (_key, childViews) {
- views_1 = views_1.concat(childViews);
- });
- return views_1;
- }
- });
- }
- /**
- * Normalizes a query to a query we send the server for listening
- *
- * @returns The normalized query
- */
- function syncTreeQueryForListening_(query) {
- if (query._queryParams.loadsAllData() && !query._queryParams.isDefault()) {
- // We treat queries that load all data as default queries
- // Cast is necessary because ref() technically returns Firebase which is actually fb.api.Firebase which inherits
- // from Query
- return new (syncTreeGetReferenceConstructor())(query._repo, query._path);
- }
- else {
- return query;
- }
- }
- function syncTreeRemoveTags_(syncTree, queries) {
- for (var j = 0; j < queries.length; ++j) {
- var removedQuery = queries[j];
- if (!removedQuery._queryParams.loadsAllData()) {
- // We should have a tag for this
- var removedQueryKey = syncTreeMakeQueryKey_(removedQuery);
- var removedQueryTag = syncTree.queryToTagMap.get(removedQueryKey);
- syncTree.queryToTagMap.delete(removedQueryKey);
- syncTree.tagToQueryMap.delete(removedQueryTag);
- }
- }
- }
- /**
- * Static accessor for query tags.
- */
- function syncTreeGetNextQueryTag_() {
- return syncTreeNextQueryTag_++;
- }
- /**
- * For a given new listen, manage the de-duplication of outstanding subscriptions.
- *
- * @returns This method can return events to support synchronous data sources
- */
- function syncTreeSetupListener_(syncTree, query, view) {
- var path = query._path;
- var tag = syncTreeTagForQuery(syncTree, query);
- var listener = syncTreeCreateListenerForView_(syncTree, view);
- var events = syncTree.listenProvider_.startListening(syncTreeQueryForListening_(query), tag, listener.hashFn, listener.onComplete);
- var subtree = syncTree.syncPointTree_.subtree(path);
- // The root of this subtree has our query. We're here because we definitely need to send a listen for that, but we
- // may need to shadow other listens as well.
- if (tag) {
- util.assert(!syncPointHasCompleteView(subtree.value), "If we're adding a query, it shouldn't be shadowed");
- }
- else {
- // Shadow everything at or below this location, this is a default listener.
- var queriesToStop = subtree.fold(function (relativePath, maybeChildSyncPoint, childMap) {
- if (!pathIsEmpty(relativePath) &&
- maybeChildSyncPoint &&
- syncPointHasCompleteView(maybeChildSyncPoint)) {
- return [syncPointGetCompleteView(maybeChildSyncPoint).query];
- }
- else {
- // No default listener here, flatten any deeper queries into an array
- var queries_1 = [];
- if (maybeChildSyncPoint) {
- queries_1 = queries_1.concat(syncPointGetQueryViews(maybeChildSyncPoint).map(function (view) { return view.query; }));
- }
- each(childMap, function (_key, childQueries) {
- queries_1 = queries_1.concat(childQueries);
- });
- return queries_1;
- }
- });
- for (var i = 0; i < queriesToStop.length; ++i) {
- var queryToStop = queriesToStop[i];
- syncTree.listenProvider_.stopListening(syncTreeQueryForListening_(queryToStop), syncTreeTagForQuery(syncTree, queryToStop));
- }
- }
- return events;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var ExistingValueProvider = /** @class */ (function () {
- function ExistingValueProvider(node_) {
- this.node_ = node_;
- }
- ExistingValueProvider.prototype.getImmediateChild = function (childName) {
- var child = this.node_.getImmediateChild(childName);
- return new ExistingValueProvider(child);
- };
- ExistingValueProvider.prototype.node = function () {
- return this.node_;
- };
- return ExistingValueProvider;
- }());
- var DeferredValueProvider = /** @class */ (function () {
- function DeferredValueProvider(syncTree, path) {
- this.syncTree_ = syncTree;
- this.path_ = path;
- }
- DeferredValueProvider.prototype.getImmediateChild = function (childName) {
- var childPath = pathChild(this.path_, childName);
- return new DeferredValueProvider(this.syncTree_, childPath);
- };
- DeferredValueProvider.prototype.node = function () {
- return syncTreeCalcCompleteEventCache(this.syncTree_, this.path_);
- };
- return DeferredValueProvider;
- }());
- /**
- * Generate placeholders for deferred values.
- */
- var generateWithValues = function (values) {
- values = values || {};
- values['timestamp'] = values['timestamp'] || new Date().getTime();
- return values;
- };
- /**
- * Value to use when firing local events. When writing server values, fire
- * local events with an approximate value, otherwise return value as-is.
- */
- var resolveDeferredLeafValue = function (value, existingVal, serverValues) {
- if (!value || typeof value !== 'object') {
- return value;
- }
- util.assert('.sv' in value, 'Unexpected leaf node or priority contents');
- if (typeof value['.sv'] === 'string') {
- return resolveScalarDeferredValue(value['.sv'], existingVal, serverValues);
- }
- else if (typeof value['.sv'] === 'object') {
- return resolveComplexDeferredValue(value['.sv'], existingVal);
- }
- else {
- util.assert(false, 'Unexpected server value: ' + JSON.stringify(value, null, 2));
- }
- };
- var resolveScalarDeferredValue = function (op, existing, serverValues) {
- switch (op) {
- case 'timestamp':
- return serverValues['timestamp'];
- default:
- util.assert(false, 'Unexpected server value: ' + op);
- }
- };
- var resolveComplexDeferredValue = function (op, existing, unused) {
- if (!op.hasOwnProperty('increment')) {
- util.assert(false, 'Unexpected server value: ' + JSON.stringify(op, null, 2));
- }
- var delta = op['increment'];
- if (typeof delta !== 'number') {
- util.assert(false, 'Unexpected increment value: ' + delta);
- }
- var existingNode = existing.node();
- util.assert(existingNode !== null && typeof existingNode !== 'undefined', 'Expected ChildrenNode.EMPTY_NODE for nulls');
- // Incrementing a non-number sets the value to the incremented amount
- if (!existingNode.isLeafNode()) {
- return delta;
- }
- var leaf = existingNode;
- var existingVal = leaf.getValue();
- if (typeof existingVal !== 'number') {
- return delta;
- }
- // No need to do over/underflow arithmetic here because JS only handles floats under the covers
- return existingVal + delta;
- };
- /**
- * Recursively replace all deferred values and priorities in the tree with the
- * specified generated replacement values.
- * @param path - path to which write is relative
- * @param node - new data written at path
- * @param syncTree - current data
- */
- var resolveDeferredValueTree = function (path, node, syncTree, serverValues) {
- return resolveDeferredValue(node, new DeferredValueProvider(syncTree, path), serverValues);
- };
- /**
- * Recursively replace all deferred values and priorities in the node with the
- * specified generated replacement values. If there are no server values in the node,
- * it'll be returned as-is.
- */
- var resolveDeferredValueSnapshot = function (node, existing, serverValues) {
- return resolveDeferredValue(node, new ExistingValueProvider(existing), serverValues);
- };
- function resolveDeferredValue(node, existingVal, serverValues) {
- var rawPri = node.getPriority().val();
- var priority = resolveDeferredLeafValue(rawPri, existingVal.getImmediateChild('.priority'), serverValues);
- var newNode;
- if (node.isLeafNode()) {
- var leafNode = node;
- var value = resolveDeferredLeafValue(leafNode.getValue(), existingVal, serverValues);
- if (value !== leafNode.getValue() ||
- priority !== leafNode.getPriority().val()) {
- return new LeafNode(value, nodeFromJSON(priority));
- }
- else {
- return node;
- }
- }
- else {
- var childrenNode = node;
- newNode = childrenNode;
- if (priority !== childrenNode.getPriority().val()) {
- newNode = newNode.updatePriority(new LeafNode(priority));
- }
- childrenNode.forEachChild(PRIORITY_INDEX, function (childName, childNode) {
- var newChildNode = resolveDeferredValue(childNode, existingVal.getImmediateChild(childName), serverValues);
- if (newChildNode !== childNode) {
- newNode = newNode.updateImmediateChild(childName, newChildNode);
- }
- });
- return newNode;
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * A light-weight tree, traversable by path. Nodes can have both values and children.
- * Nodes are not enumerated (by forEachChild) unless they have a value or non-empty
- * children.
- */
- var Tree = /** @class */ (function () {
- /**
- * @param name - Optional name of the node.
- * @param parent - Optional parent node.
- * @param node - Optional node to wrap.
- */
- function Tree(name, parent, node) {
- if (name === void 0) { name = ''; }
- if (parent === void 0) { parent = null; }
- if (node === void 0) { node = { children: {}, childCount: 0 }; }
- this.name = name;
- this.parent = parent;
- this.node = node;
- }
- return Tree;
- }());
- /**
- * Returns a sub-Tree for the given path.
- *
- * @param pathObj - Path to look up.
- * @returns Tree for path.
- */
- function treeSubTree(tree, pathObj) {
- // TODO: Require pathObj to be Path?
- var path = pathObj instanceof Path ? pathObj : new Path(pathObj);
- var child = tree, next = pathGetFront(path);
- while (next !== null) {
- var childNode = util.safeGet(child.node.children, next) || {
- children: {},
- childCount: 0
- };
- child = new Tree(next, child, childNode);
- path = pathPopFront(path);
- next = pathGetFront(path);
- }
- return child;
- }
- /**
- * Returns the data associated with this tree node.
- *
- * @returns The data or null if no data exists.
- */
- function treeGetValue(tree) {
- return tree.node.value;
- }
- /**
- * Sets data to this tree node.
- *
- * @param value - Value to set.
- */
- function treeSetValue(tree, value) {
- tree.node.value = value;
- treeUpdateParents(tree);
- }
- /**
- * @returns Whether the tree has any children.
- */
- function treeHasChildren(tree) {
- return tree.node.childCount > 0;
- }
- /**
- * @returns Whethe rthe tree is empty (no value or children).
- */
- function treeIsEmpty(tree) {
- return treeGetValue(tree) === undefined && !treeHasChildren(tree);
- }
- /**
- * Calls action for each child of this tree node.
- *
- * @param action - Action to be called for each child.
- */
- function treeForEachChild(tree, action) {
- each(tree.node.children, function (child, childTree) {
- action(new Tree(child, tree, childTree));
- });
- }
- /**
- * Does a depth-first traversal of this node's descendants, calling action for each one.
- *
- * @param action - Action to be called for each child.
- * @param includeSelf - Whether to call action on this node as well. Defaults to
- * false.
- * @param childrenFirst - Whether to call action on children before calling it on
- * parent.
- */
- function treeForEachDescendant(tree, action, includeSelf, childrenFirst) {
- if (includeSelf && !childrenFirst) {
- action(tree);
- }
- treeForEachChild(tree, function (child) {
- treeForEachDescendant(child, action, true, childrenFirst);
- });
- if (includeSelf && childrenFirst) {
- action(tree);
- }
- }
- /**
- * Calls action on each ancestor node.
- *
- * @param action - Action to be called on each parent; return
- * true to abort.
- * @param includeSelf - Whether to call action on this node as well.
- * @returns true if the action callback returned true.
- */
- function treeForEachAncestor(tree, action, includeSelf) {
- var node = includeSelf ? tree : tree.parent;
- while (node !== null) {
- if (action(node)) {
- return true;
- }
- node = node.parent;
- }
- return false;
- }
- /**
- * @returns The path of this tree node, as a Path.
- */
- function treeGetPath(tree) {
- return new Path(tree.parent === null
- ? tree.name
- : treeGetPath(tree.parent) + '/' + tree.name);
- }
- /**
- * Adds or removes this child from its parent based on whether it's empty or not.
- */
- function treeUpdateParents(tree) {
- if (tree.parent !== null) {
- treeUpdateChild(tree.parent, tree.name, tree);
- }
- }
- /**
- * Adds or removes the passed child to this tree node, depending on whether it's empty.
- *
- * @param childName - The name of the child to update.
- * @param child - The child to update.
- */
- function treeUpdateChild(tree, childName, child) {
- var childEmpty = treeIsEmpty(child);
- var childExists = util.contains(tree.node.children, childName);
- if (childEmpty && childExists) {
- delete tree.node.children[childName];
- tree.node.childCount--;
- treeUpdateParents(tree);
- }
- else if (!childEmpty && !childExists) {
- tree.node.children[childName] = child.node;
- tree.node.childCount++;
- treeUpdateParents(tree);
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * True for invalid Firebase keys
- */
- var INVALID_KEY_REGEX_ = /[\[\].#$\/\u0000-\u001F\u007F]/;
- /**
- * True for invalid Firebase paths.
- * Allows '/' in paths.
- */
- var INVALID_PATH_REGEX_ = /[\[\].#$\u0000-\u001F\u007F]/;
- /**
- * Maximum number of characters to allow in leaf value
- */
- var MAX_LEAF_SIZE_ = 10 * 1024 * 1024;
- var isValidKey = function (key) {
- return (typeof key === 'string' && key.length !== 0 && !INVALID_KEY_REGEX_.test(key));
- };
- var isValidPathString = function (pathString) {
- return (typeof pathString === 'string' &&
- pathString.length !== 0 &&
- !INVALID_PATH_REGEX_.test(pathString));
- };
- var isValidRootPathString = function (pathString) {
- if (pathString) {
- // Allow '/.info/' at the beginning.
- pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
- }
- return isValidPathString(pathString);
- };
- var isValidPriority = function (priority) {
- return (priority === null ||
- typeof priority === 'string' ||
- (typeof priority === 'number' && !isInvalidJSONNumber(priority)) ||
- (priority &&
- typeof priority === 'object' &&
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- util.contains(priority, '.sv')));
- };
- /**
- * Pre-validate a datum passed as an argument to Firebase function.
- */
- var validateFirebaseDataArg = function (fnName, value, path, optional) {
- if (optional && value === undefined) {
- return;
- }
- validateFirebaseData(util.errorPrefix(fnName, 'value'), value, path);
- };
- /**
- * Validate a data object client-side before sending to server.
- */
- var validateFirebaseData = function (errorPrefix, data, path_) {
- var path = path_ instanceof Path ? new ValidationPath(path_, errorPrefix) : path_;
- if (data === undefined) {
- throw new Error(errorPrefix + 'contains undefined ' + validationPathToErrorString(path));
- }
- if (typeof data === 'function') {
- throw new Error(errorPrefix +
- 'contains a function ' +
- validationPathToErrorString(path) +
- ' with contents = ' +
- data.toString());
- }
- if (isInvalidJSONNumber(data)) {
- throw new Error(errorPrefix +
- 'contains ' +
- data.toString() +
- ' ' +
- validationPathToErrorString(path));
- }
- // Check max leaf size, but try to avoid the utf8 conversion if we can.
- if (typeof data === 'string' &&
- data.length > MAX_LEAF_SIZE_ / 3 &&
- util.stringLength(data) > MAX_LEAF_SIZE_) {
- throw new Error(errorPrefix +
- 'contains a string greater than ' +
- MAX_LEAF_SIZE_ +
- ' utf8 bytes ' +
- validationPathToErrorString(path) +
- " ('" +
- data.substring(0, 50) +
- "...')");
- }
- // TODO = Perf = Consider combining the recursive validation of keys into NodeFromJSON
- // to save extra walking of large objects.
- if (data && typeof data === 'object') {
- var hasDotValue_1 = false;
- var hasActualChild_1 = false;
- each(data, function (key, value) {
- if (key === '.value') {
- hasDotValue_1 = true;
- }
- else if (key !== '.priority' && key !== '.sv') {
- hasActualChild_1 = true;
- if (!isValidKey(key)) {
- throw new Error(errorPrefix +
- ' contains an invalid key (' +
- key +
- ') ' +
- validationPathToErrorString(path) +
- '. Keys must be non-empty strings ' +
- 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
- }
- }
- validationPathPush(path, key);
- validateFirebaseData(errorPrefix, value, path);
- validationPathPop(path);
- });
- if (hasDotValue_1 && hasActualChild_1) {
- throw new Error(errorPrefix +
- ' contains ".value" child ' +
- validationPathToErrorString(path) +
- ' in addition to actual children.');
- }
- }
- };
- /**
- * Pre-validate paths passed in the firebase function.
- */
- var validateFirebaseMergePaths = function (errorPrefix, mergePaths) {
- var i, curPath;
- for (i = 0; i < mergePaths.length; i++) {
- curPath = mergePaths[i];
- var keys = pathSlice(curPath);
- for (var j = 0; j < keys.length; j++) {
- if (keys[j] === '.priority' && j === keys.length - 1) ;
- else if (!isValidKey(keys[j])) {
- throw new Error(errorPrefix +
- 'contains an invalid key (' +
- keys[j] +
- ') in path ' +
- curPath.toString() +
- '. Keys must be non-empty strings ' +
- 'and can\'t contain ".", "#", "$", "/", "[", or "]"');
- }
- }
- }
- // Check that update keys are not descendants of each other.
- // We rely on the property that sorting guarantees that ancestors come
- // right before descendants.
- mergePaths.sort(pathCompare);
- var prevPath = null;
- for (i = 0; i < mergePaths.length; i++) {
- curPath = mergePaths[i];
- if (prevPath !== null && pathContains(prevPath, curPath)) {
- throw new Error(errorPrefix +
- 'contains a path ' +
- prevPath.toString() +
- ' that is ancestor of another path ' +
- curPath.toString());
- }
- prevPath = curPath;
- }
- };
- /**
- * pre-validate an object passed as an argument to firebase function (
- * must be an object - e.g. for firebase.update()).
- */
- var validateFirebaseMergeDataArg = function (fnName, data, path, optional) {
- if (optional && data === undefined) {
- return;
- }
- var errorPrefix = util.errorPrefix(fnName, 'values');
- if (!(data && typeof data === 'object') || Array.isArray(data)) {
- throw new Error(errorPrefix + ' must be an object containing the children to replace.');
- }
- var mergePaths = [];
- each(data, function (key, value) {
- var curPath = new Path(key);
- validateFirebaseData(errorPrefix, value, pathChild(path, curPath));
- if (pathGetBack(curPath) === '.priority') {
- if (!isValidPriority(value)) {
- throw new Error(errorPrefix +
- "contains an invalid value for '" +
- curPath.toString() +
- "', which must be a valid " +
- 'Firebase priority (a string, finite number, server value, or null).');
- }
- }
- mergePaths.push(curPath);
- });
- validateFirebaseMergePaths(errorPrefix, mergePaths);
- };
- var validatePriority = function (fnName, priority, optional) {
- if (optional && priority === undefined) {
- return;
- }
- if (isInvalidJSONNumber(priority)) {
- throw new Error(util.errorPrefix(fnName, 'priority') +
- 'is ' +
- priority.toString() +
- ', but must be a valid Firebase priority (a string, finite number, ' +
- 'server value, or null).');
- }
- // Special case to allow importing data with a .sv.
- if (!isValidPriority(priority)) {
- throw new Error(util.errorPrefix(fnName, 'priority') +
- 'must be a valid Firebase priority ' +
- '(a string, finite number, server value, or null).');
- }
- };
- var validateKey = function (fnName, argumentName, key, optional) {
- if (optional && key === undefined) {
- return;
- }
- if (!isValidKey(key)) {
- throw new Error(util.errorPrefix(fnName, argumentName) +
- 'was an invalid key = "' +
- key +
- '". Firebase keys must be non-empty strings and ' +
- 'can\'t contain ".", "#", "$", "/", "[", or "]").');
- }
- };
- /**
- * @internal
- */
- var validatePathString = function (fnName, argumentName, pathString, optional) {
- if (optional && pathString === undefined) {
- return;
- }
- if (!isValidPathString(pathString)) {
- throw new Error(util.errorPrefix(fnName, argumentName) +
- 'was an invalid path = "' +
- pathString +
- '". Paths must be non-empty strings and ' +
- 'can\'t contain ".", "#", "$", "[", or "]"');
- }
- };
- var validateRootPathString = function (fnName, argumentName, pathString, optional) {
- if (pathString) {
- // Allow '/.info/' at the beginning.
- pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
- }
- validatePathString(fnName, argumentName, pathString, optional);
- };
- /**
- * @internal
- */
- var validateWritablePath = function (fnName, path) {
- if (pathGetFront(path) === '.info') {
- throw new Error(fnName + " failed = Can't modify data under /.info/");
- }
- };
- var validateUrl = function (fnName, parsedUrl) {
- // TODO = Validate server better.
- var pathString = parsedUrl.path.toString();
- if (!(typeof parsedUrl.repoInfo.host === 'string') ||
- parsedUrl.repoInfo.host.length === 0 ||
- (!isValidKey(parsedUrl.repoInfo.namespace) &&
- parsedUrl.repoInfo.host.split(':')[0] !== 'localhost') ||
- (pathString.length !== 0 && !isValidRootPathString(pathString))) {
- throw new Error(util.errorPrefix(fnName, 'url') +
- 'must be a valid firebase URL and ' +
- 'the path can\'t contain ".", "#", "$", "[", or "]".');
- }
- };
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * The event queue serves a few purposes:
- * 1. It ensures we maintain event order in the face of event callbacks doing operations that result in more
- * events being queued.
- * 2. raiseQueuedEvents() handles being called reentrantly nicely. That is, if in the course of raising events,
- * raiseQueuedEvents() is called again, the "inner" call will pick up raising events where the "outer" call
- * left off, ensuring that the events are still raised synchronously and in order.
- * 3. You can use raiseEventsAtPath and raiseEventsForChangedPath to ensure only relevant previously-queued
- * events are raised synchronously.
- *
- * NOTE: This can all go away if/when we move to async events.
- *
- */
- var EventQueue = /** @class */ (function () {
- function EventQueue() {
- this.eventLists_ = [];
- /**
- * Tracks recursion depth of raiseQueuedEvents_, for debugging purposes.
- */
- this.recursionDepth_ = 0;
- }
- return EventQueue;
- }());
- /**
- * @param eventDataList - The new events to queue.
- */
- function eventQueueQueueEvents(eventQueue, eventDataList) {
- // We group events by path, storing them in a single EventList, to make it easier to skip over them quickly.
- var currList = null;
- for (var i = 0; i < eventDataList.length; i++) {
- var data = eventDataList[i];
- var path = data.getPath();
- if (currList !== null && !pathEquals(path, currList.path)) {
- eventQueue.eventLists_.push(currList);
- currList = null;
- }
- if (currList === null) {
- currList = { events: [], path: path };
- }
- currList.events.push(data);
- }
- if (currList) {
- eventQueue.eventLists_.push(currList);
- }
- }
- /**
- * Queues the specified events and synchronously raises all events (including previously queued ones)
- * for the specified path.
- *
- * It is assumed that the new events are all for the specified path.
- *
- * @param path - The path to raise events for.
- * @param eventDataList - The new events to raise.
- */
- function eventQueueRaiseEventsAtPath(eventQueue, path, eventDataList) {
- eventQueueQueueEvents(eventQueue, eventDataList);
- eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
- return pathEquals(eventPath, path);
- });
- }
- /**
- * Queues the specified events and synchronously raises all events (including previously queued ones) for
- * locations related to the specified change path (i.e. all ancestors and descendants).
- *
- * It is assumed that the new events are all related (ancestor or descendant) to the specified path.
- *
- * @param changedPath - The path to raise events for.
- * @param eventDataList - The events to raise
- */
- function eventQueueRaiseEventsForChangedPath(eventQueue, changedPath, eventDataList) {
- eventQueueQueueEvents(eventQueue, eventDataList);
- eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, function (eventPath) {
- return pathContains(eventPath, changedPath) ||
- pathContains(changedPath, eventPath);
- });
- }
- function eventQueueRaiseQueuedEventsMatchingPredicate(eventQueue, predicate) {
- eventQueue.recursionDepth_++;
- var sentAll = true;
- for (var i = 0; i < eventQueue.eventLists_.length; i++) {
- var eventList = eventQueue.eventLists_[i];
- if (eventList) {
- var eventPath = eventList.path;
- if (predicate(eventPath)) {
- eventListRaise(eventQueue.eventLists_[i]);
- eventQueue.eventLists_[i] = null;
- }
- else {
- sentAll = false;
- }
- }
- }
- if (sentAll) {
- eventQueue.eventLists_ = [];
- }
- eventQueue.recursionDepth_--;
- }
- /**
- * Iterates through the list and raises each event
- */
- function eventListRaise(eventList) {
- for (var i = 0; i < eventList.events.length; i++) {
- var eventData = eventList.events[i];
- if (eventData !== null) {
- eventList.events[i] = null;
- var eventFn = eventData.getEventRunner();
- if (logger) {
- log('event: ' + eventData.toString());
- }
- exceptionGuard(eventFn);
- }
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var INTERRUPT_REASON = 'repo_interrupt';
- /**
- * If a transaction does not succeed after 25 retries, we abort it. Among other
- * things this ensure that if there's ever a bug causing a mismatch between
- * client / server hashes for some data, we won't retry indefinitely.
- */
- var MAX_TRANSACTION_RETRIES = 25;
- /**
- * A connection to a single data repository.
- */
- var Repo = /** @class */ (function () {
- function Repo(repoInfo_, forceRestClient_, authTokenProvider_, appCheckProvider_) {
- this.repoInfo_ = repoInfo_;
- this.forceRestClient_ = forceRestClient_;
- this.authTokenProvider_ = authTokenProvider_;
- this.appCheckProvider_ = appCheckProvider_;
- this.dataUpdateCount = 0;
- this.statsListener_ = null;
- this.eventQueue_ = new EventQueue();
- this.nextWriteId_ = 1;
- this.interceptServerDataCallback_ = null;
- /** A list of data pieces and paths to be set when this client disconnects. */
- this.onDisconnect_ = newSparseSnapshotTree();
- /** Stores queues of outstanding transactions for Firebase locations. */
- this.transactionQueueTree_ = new Tree();
- // TODO: This should be @private but it's used by test_access.js and internal.js
- this.persistentConnection_ = null;
- // This key is intentionally not updated if RepoInfo is later changed or replaced
- this.key = this.repoInfo_.toURLString();
- }
- /**
- * @returns The URL corresponding to the root of this Firebase.
- */
- Repo.prototype.toString = function () {
- return ((this.repoInfo_.secure ? 'https://' : 'http://') + this.repoInfo_.host);
- };
- return Repo;
- }());
- function repoStart(repo, appId, authOverride) {
- repo.stats_ = statsManagerGetCollection(repo.repoInfo_);
- if (repo.forceRestClient_ || beingCrawled()) {
- repo.server_ = new ReadonlyRestClient(repo.repoInfo_, function (pathString, data, isMerge, tag) {
- repoOnDataUpdate(repo, pathString, data, isMerge, tag);
- }, repo.authTokenProvider_, repo.appCheckProvider_);
- // Minor hack: Fire onConnect immediately, since there's no actual connection.
- setTimeout(function () { return repoOnConnectStatus(repo, /* connectStatus= */ true); }, 0);
- }
- else {
- // Validate authOverride
- if (typeof authOverride !== 'undefined' && authOverride !== null) {
- if (typeof authOverride !== 'object') {
- throw new Error('Only objects are supported for option databaseAuthVariableOverride');
- }
- try {
- util.stringify(authOverride);
- }
- catch (e) {
- throw new Error('Invalid authOverride provided: ' + e);
- }
- }
- repo.persistentConnection_ = new PersistentConnection(repo.repoInfo_, appId, function (pathString, data, isMerge, tag) {
- repoOnDataUpdate(repo, pathString, data, isMerge, tag);
- }, function (connectStatus) {
- repoOnConnectStatus(repo, connectStatus);
- }, function (updates) {
- repoOnServerInfoUpdate(repo, updates);
- }, repo.authTokenProvider_, repo.appCheckProvider_, authOverride);
- repo.server_ = repo.persistentConnection_;
- }
- repo.authTokenProvider_.addTokenChangeListener(function (token) {
- repo.server_.refreshAuthToken(token);
- });
- repo.appCheckProvider_.addTokenChangeListener(function (result) {
- repo.server_.refreshAppCheckToken(result.token);
- });
- // In the case of multiple Repos for the same repoInfo (i.e. there are multiple Firebase.Contexts being used),
- // we only want to create one StatsReporter. As such, we'll report stats over the first Repo created.
- repo.statsReporter_ = statsManagerGetOrCreateReporter(repo.repoInfo_, function () { return new StatsReporter(repo.stats_, repo.server_); });
- // Used for .info.
- repo.infoData_ = new SnapshotHolder();
- repo.infoSyncTree_ = new SyncTree({
- startListening: function (query, tag, currentHashFn, onComplete) {
- var infoEvents = [];
- var node = repo.infoData_.getNode(query._path);
- // This is possibly a hack, but we have different semantics for .info endpoints. We don't raise null events
- // on initial data...
- if (!node.isEmpty()) {
- infoEvents = syncTreeApplyServerOverwrite(repo.infoSyncTree_, query._path, node);
- setTimeout(function () {
- onComplete('ok');
- }, 0);
- }
- return infoEvents;
- },
- stopListening: function () { }
- });
- repoUpdateInfo(repo, 'connected', false);
- repo.serverSyncTree_ = new SyncTree({
- startListening: function (query, tag, currentHashFn, onComplete) {
- repo.server_.listen(query, currentHashFn, tag, function (status, data) {
- var events = onComplete(status, data);
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, query._path, events);
- });
- // No synchronous events for network-backed sync trees
- return [];
- },
- stopListening: function (query, tag) {
- repo.server_.unlisten(query, tag);
- }
- });
- }
- /**
- * @returns The time in milliseconds, taking the server offset into account if we have one.
- */
- function repoServerTime(repo) {
- var offsetNode = repo.infoData_.getNode(new Path('.info/serverTimeOffset'));
- var offset = offsetNode.val() || 0;
- return new Date().getTime() + offset;
- }
- /**
- * Generate ServerValues using some variables from the repo object.
- */
- function repoGenerateServerValues(repo) {
- return generateWithValues({
- timestamp: repoServerTime(repo)
- });
- }
- /**
- * Called by realtime when we get new messages from the server.
- */
- function repoOnDataUpdate(repo, pathString, data, isMerge, tag) {
- // For testing.
- repo.dataUpdateCount++;
- var path = new Path(pathString);
- data = repo.interceptServerDataCallback_
- ? repo.interceptServerDataCallback_(pathString, data)
- : data;
- var events = [];
- if (tag) {
- if (isMerge) {
- var taggedChildren = util.map(data, function (raw) { return nodeFromJSON(raw); });
- events = syncTreeApplyTaggedQueryMerge(repo.serverSyncTree_, path, taggedChildren, tag);
- }
- else {
- var taggedSnap = nodeFromJSON(data);
- events = syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, path, taggedSnap, tag);
- }
- }
- else if (isMerge) {
- var changedChildren = util.map(data, function (raw) { return nodeFromJSON(raw); });
- events = syncTreeApplyServerMerge(repo.serverSyncTree_, path, changedChildren);
- }
- else {
- var snap = nodeFromJSON(data);
- events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap);
- }
- var affectedPath = path;
- if (events.length > 0) {
- // Since we have a listener outstanding for each transaction, receiving any events
- // is a proxy for some change having occurred.
- affectedPath = repoRerunTransactions(repo, path);
- }
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, events);
- }
- function repoOnConnectStatus(repo, connectStatus) {
- repoUpdateInfo(repo, 'connected', connectStatus);
- if (connectStatus === false) {
- repoRunOnDisconnectEvents(repo);
- }
- }
- function repoOnServerInfoUpdate(repo, updates) {
- each(updates, function (key, value) {
- repoUpdateInfo(repo, key, value);
- });
- }
- function repoUpdateInfo(repo, pathString, value) {
- var path = new Path('/.info/' + pathString);
- var newNode = nodeFromJSON(value);
- repo.infoData_.updateSnapshot(path, newNode);
- var events = syncTreeApplyServerOverwrite(repo.infoSyncTree_, path, newNode);
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
- }
- function repoGetNextWriteId(repo) {
- return repo.nextWriteId_++;
- }
- /**
- * The purpose of `getValue` is to return the latest known value
- * satisfying `query`.
- *
- * This method will first check for in-memory cached values
- * belonging to active listeners. If they are found, such values
- * are considered to be the most up-to-date.
- *
- * If the client is not connected, this method will wait until the
- * repo has established a connection and then request the value for `query`.
- * If the client is not able to retrieve the query result for another reason,
- * it reports an error.
- *
- * @param query - The query to surface a value for.
- */
- function repoGetValue(repo, query, eventRegistration) {
- // Only active queries are cached. There is no persisted cache.
- var cached = syncTreeGetServerValue(repo.serverSyncTree_, query);
- if (cached != null) {
- return Promise.resolve(cached);
- }
- return repo.server_.get(query).then(function (payload) {
- var node = nodeFromJSON(payload).withIndex(query._queryParams.getIndex());
- /**
- * Below we simulate the actions of an `onlyOnce` `onValue()` event where:
- * Add an event registration,
- * Update data at the path,
- * Raise any events,
- * Cleanup the SyncTree
- */
- syncTreeAddEventRegistration(repo.serverSyncTree_, query, eventRegistration, true);
- var events;
- if (query._queryParams.loadsAllData()) {
- events = syncTreeApplyServerOverwrite(repo.serverSyncTree_, query._path, node);
- }
- else {
- var tag = syncTreeTagForQuery(repo.serverSyncTree_, query);
- events = syncTreeApplyTaggedQueryOverwrite(repo.serverSyncTree_, query._path, node, tag);
- }
- /*
- * We need to raise events in the scenario where `get()` is called at a parent path, and
- * while the `get()` is pending, `onValue` is called at a child location. While get() is waiting
- * for the data, `onValue` will register a new event. Then, get() will come back, and update the syncTree
- * and its corresponding serverCache, including the child location where `onValue` is called. Then,
- * `onValue` will receive the event from the server, but look at the syncTree and see that the data received
- * from the server is already at the SyncPoint, and so the `onValue` callback will never get fired.
- * Calling `eventQueueRaiseEventsForChangedPath()` is the correct way to propagate the events and
- * ensure the corresponding child events will get fired.
- */
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, query._path, events);
- syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, eventRegistration, null, true);
- return node;
- }, function (err) {
- repoLog(repo, 'get for query ' + util.stringify(query) + ' failed: ' + err);
- return Promise.reject(new Error(err));
- });
- }
- function repoSetWithPriority(repo, path, newVal, newPriority, onComplete) {
- repoLog(repo, 'set', {
- path: path.toString(),
- value: newVal,
- priority: newPriority
- });
- // TODO: Optimize this behavior to either (a) store flag to skip resolving where possible and / or
- // (b) store unresolved paths on JSON parse
- var serverValues = repoGenerateServerValues(repo);
- var newNodeUnresolved = nodeFromJSON(newVal, newPriority);
- var existing = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path);
- var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, existing, serverValues);
- var writeId = repoGetNextWriteId(repo);
- var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, writeId, true);
- eventQueueQueueEvents(repo.eventQueue_, events);
- repo.server_.put(path.toString(), newNodeUnresolved.val(/*export=*/ true), function (status, errorReason) {
- var success = status === 'ok';
- if (!success) {
- warn$1('set at ' + path + ' failed: ' + status);
- }
- var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId, !success);
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, clearEvents);
- repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
- });
- var affectedPath = repoAbortTransactions(repo, path);
- repoRerunTransactions(repo, affectedPath);
- // We queued the events above, so just flush the queue here
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, []);
- }
- function repoUpdate(repo, path, childrenToMerge, onComplete) {
- repoLog(repo, 'update', { path: path.toString(), value: childrenToMerge });
- // Start with our existing data and merge each child into it.
- var empty = true;
- var serverValues = repoGenerateServerValues(repo);
- var changedChildren = {};
- each(childrenToMerge, function (changedKey, changedValue) {
- empty = false;
- changedChildren[changedKey] = resolveDeferredValueTree(pathChild(path, changedKey), nodeFromJSON(changedValue), repo.serverSyncTree_, serverValues);
- });
- if (!empty) {
- var writeId_1 = repoGetNextWriteId(repo);
- var events = syncTreeApplyUserMerge(repo.serverSyncTree_, path, changedChildren, writeId_1);
- eventQueueQueueEvents(repo.eventQueue_, events);
- repo.server_.merge(path.toString(), childrenToMerge, function (status, errorReason) {
- var success = status === 'ok';
- if (!success) {
- warn$1('update at ' + path + ' failed: ' + status);
- }
- var clearEvents = syncTreeAckUserWrite(repo.serverSyncTree_, writeId_1, !success);
- var affectedPath = clearEvents.length > 0 ? repoRerunTransactions(repo, path) : path;
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, affectedPath, clearEvents);
- repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
- });
- each(childrenToMerge, function (changedPath) {
- var affectedPath = repoAbortTransactions(repo, pathChild(path, changedPath));
- repoRerunTransactions(repo, affectedPath);
- });
- // We queued the events above, so just flush the queue here
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, []);
- }
- else {
- log("update() called with empty data. Don't do anything.");
- repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
- }
- }
- /**
- * Applies all of the changes stored up in the onDisconnect_ tree.
- */
- function repoRunOnDisconnectEvents(repo) {
- repoLog(repo, 'onDisconnectEvents');
- var serverValues = repoGenerateServerValues(repo);
- var resolvedOnDisconnectTree = newSparseSnapshotTree();
- sparseSnapshotTreeForEachTree(repo.onDisconnect_, newEmptyPath(), function (path, node) {
- var resolved = resolveDeferredValueTree(path, node, repo.serverSyncTree_, serverValues);
- sparseSnapshotTreeRemember(resolvedOnDisconnectTree, path, resolved);
- });
- var events = [];
- sparseSnapshotTreeForEachTree(resolvedOnDisconnectTree, newEmptyPath(), function (path, snap) {
- events = events.concat(syncTreeApplyServerOverwrite(repo.serverSyncTree_, path, snap));
- var affectedPath = repoAbortTransactions(repo, path);
- repoRerunTransactions(repo, affectedPath);
- });
- repo.onDisconnect_ = newSparseSnapshotTree();
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, newEmptyPath(), events);
- }
- function repoOnDisconnectCancel(repo, path, onComplete) {
- repo.server_.onDisconnectCancel(path.toString(), function (status, errorReason) {
- if (status === 'ok') {
- sparseSnapshotTreeForget(repo.onDisconnect_, path);
- }
- repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
- });
- }
- function repoOnDisconnectSet(repo, path, value, onComplete) {
- var newNode = nodeFromJSON(value);
- repo.server_.onDisconnectPut(path.toString(), newNode.val(/*export=*/ true), function (status, errorReason) {
- if (status === 'ok') {
- sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
- }
- repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
- });
- }
- function repoOnDisconnectSetWithPriority(repo, path, value, priority, onComplete) {
- var newNode = nodeFromJSON(value, priority);
- repo.server_.onDisconnectPut(path.toString(), newNode.val(/*export=*/ true), function (status, errorReason) {
- if (status === 'ok') {
- sparseSnapshotTreeRemember(repo.onDisconnect_, path, newNode);
- }
- repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
- });
- }
- function repoOnDisconnectUpdate(repo, path, childrenToMerge, onComplete) {
- if (util.isEmpty(childrenToMerge)) {
- log("onDisconnect().update() called with empty data. Don't do anything.");
- repoCallOnCompleteCallback(repo, onComplete, 'ok', undefined);
- return;
- }
- repo.server_.onDisconnectMerge(path.toString(), childrenToMerge, function (status, errorReason) {
- if (status === 'ok') {
- each(childrenToMerge, function (childName, childNode) {
- var newChildNode = nodeFromJSON(childNode);
- sparseSnapshotTreeRemember(repo.onDisconnect_, pathChild(path, childName), newChildNode);
- });
- }
- repoCallOnCompleteCallback(repo, onComplete, status, errorReason);
- });
- }
- function repoAddEventCallbackForQuery(repo, query, eventRegistration) {
- var events;
- if (pathGetFront(query._path) === '.info') {
- events = syncTreeAddEventRegistration(repo.infoSyncTree_, query, eventRegistration);
- }
- else {
- events = syncTreeAddEventRegistration(repo.serverSyncTree_, query, eventRegistration);
- }
- eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
- }
- function repoRemoveEventCallbackForQuery(repo, query, eventRegistration) {
- // These are guaranteed not to raise events, since we're not passing in a cancelError. However, we can future-proof
- // a little bit by handling the return values anyways.
- var events;
- if (pathGetFront(query._path) === '.info') {
- events = syncTreeRemoveEventRegistration(repo.infoSyncTree_, query, eventRegistration);
- }
- else {
- events = syncTreeRemoveEventRegistration(repo.serverSyncTree_, query, eventRegistration);
- }
- eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
- }
- function repoInterrupt(repo) {
- if (repo.persistentConnection_) {
- repo.persistentConnection_.interrupt(INTERRUPT_REASON);
- }
- }
- function repoResume(repo) {
- if (repo.persistentConnection_) {
- repo.persistentConnection_.resume(INTERRUPT_REASON);
- }
- }
- function repoLog(repo) {
- var varArgs = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- varArgs[_i - 1] = arguments[_i];
- }
- var prefix = '';
- if (repo.persistentConnection_) {
- prefix = repo.persistentConnection_.id + ':';
- }
- log.apply(void 0, tslib.__spreadArray([prefix], tslib.__read(varArgs), false));
- }
- function repoCallOnCompleteCallback(repo, callback, status, errorReason) {
- if (callback) {
- exceptionGuard(function () {
- if (status === 'ok') {
- callback(null);
- }
- else {
- var code = (status || 'error').toUpperCase();
- var message = code;
- if (errorReason) {
- message += ': ' + errorReason;
- }
- var error = new Error(message);
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- error.code = code;
- callback(error);
- }
- });
- }
- }
- /**
- * Creates a new transaction, adds it to the transactions we're tracking, and
- * sends it to the server if possible.
- *
- * @param path - Path at which to do transaction.
- * @param transactionUpdate - Update callback.
- * @param onComplete - Completion callback.
- * @param unwatcher - Function that will be called when the transaction no longer
- * need data updates for `path`.
- * @param applyLocally - Whether or not to make intermediate results visible
- */
- function repoStartTransaction(repo, path, transactionUpdate, onComplete, unwatcher, applyLocally) {
- repoLog(repo, 'transaction on ' + path);
- // Initialize transaction.
- var transaction = {
- path: path,
- update: transactionUpdate,
- onComplete: onComplete,
- // One of TransactionStatus enums.
- status: null,
- // Used when combining transactions at different locations to figure out
- // which one goes first.
- order: LUIDGenerator(),
- // Whether to raise local events for this transaction.
- applyLocally: applyLocally,
- // Count of how many times we've retried the transaction.
- retryCount: 0,
- // Function to call to clean up our .on() listener.
- unwatcher: unwatcher,
- // Stores why a transaction was aborted.
- abortReason: null,
- currentWriteId: null,
- currentInputSnapshot: null,
- currentOutputSnapshotRaw: null,
- currentOutputSnapshotResolved: null
- };
- // Run transaction initially.
- var currentState = repoGetLatestState(repo, path, undefined);
- transaction.currentInputSnapshot = currentState;
- var newVal = transaction.update(currentState.val());
- if (newVal === undefined) {
- // Abort transaction.
- transaction.unwatcher();
- transaction.currentOutputSnapshotRaw = null;
- transaction.currentOutputSnapshotResolved = null;
- if (transaction.onComplete) {
- transaction.onComplete(null, false, transaction.currentInputSnapshot);
- }
- }
- else {
- validateFirebaseData('transaction failed: Data returned ', newVal, transaction.path);
- // Mark as run and add to our queue.
- transaction.status = 0 /* TransactionStatus.RUN */;
- var queueNode = treeSubTree(repo.transactionQueueTree_, path);
- var nodeQueue = treeGetValue(queueNode) || [];
- nodeQueue.push(transaction);
- treeSetValue(queueNode, nodeQueue);
- // Update visibleData and raise events
- // Note: We intentionally raise events after updating all of our
- // transaction state, since the user could start new transactions from the
- // event callbacks.
- var priorityForNode = void 0;
- if (typeof newVal === 'object' &&
- newVal !== null &&
- util.contains(newVal, '.priority')) {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- priorityForNode = util.safeGet(newVal, '.priority');
- util.assert(isValidPriority(priorityForNode), 'Invalid priority returned by transaction. ' +
- 'Priority must be a valid string, finite number, server value, or null.');
- }
- else {
- var currentNode = syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path) ||
- ChildrenNode.EMPTY_NODE;
- priorityForNode = currentNode.getPriority().val();
- }
- var serverValues = repoGenerateServerValues(repo);
- var newNodeUnresolved = nodeFromJSON(newVal, priorityForNode);
- var newNode = resolveDeferredValueSnapshot(newNodeUnresolved, currentState, serverValues);
- transaction.currentOutputSnapshotRaw = newNodeUnresolved;
- transaction.currentOutputSnapshotResolved = newNode;
- transaction.currentWriteId = repoGetNextWriteId(repo);
- var events = syncTreeApplyUserOverwrite(repo.serverSyncTree_, path, newNode, transaction.currentWriteId, transaction.applyLocally);
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
- repoSendReadyTransactions(repo, repo.transactionQueueTree_);
- }
- }
- /**
- * @param excludeSets - A specific set to exclude
- */
- function repoGetLatestState(repo, path, excludeSets) {
- return (syncTreeCalcCompleteEventCache(repo.serverSyncTree_, path, excludeSets) ||
- ChildrenNode.EMPTY_NODE);
- }
- /**
- * Sends any already-run transactions that aren't waiting for outstanding
- * transactions to complete.
- *
- * Externally it's called with no arguments, but it calls itself recursively
- * with a particular transactionQueueTree node to recurse through the tree.
- *
- * @param node - transactionQueueTree node to start at.
- */
- function repoSendReadyTransactions(repo, node) {
- if (node === void 0) { node = repo.transactionQueueTree_; }
- // Before recursing, make sure any completed transactions are removed.
- if (!node) {
- repoPruneCompletedTransactionsBelowNode(repo, node);
- }
- if (treeGetValue(node)) {
- var queue = repoBuildTransactionQueue(repo, node);
- util.assert(queue.length > 0, 'Sending zero length transaction queue');
- var allRun = queue.every(function (transaction) { return transaction.status === 0 /* TransactionStatus.RUN */; });
- // If they're all run (and not sent), we can send them. Else, we must wait.
- if (allRun) {
- repoSendTransactionQueue(repo, treeGetPath(node), queue);
- }
- }
- else if (treeHasChildren(node)) {
- treeForEachChild(node, function (childNode) {
- repoSendReadyTransactions(repo, childNode);
- });
- }
- }
- /**
- * Given a list of run transactions, send them to the server and then handle
- * the result (success or failure).
- *
- * @param path - The location of the queue.
- * @param queue - Queue of transactions under the specified location.
- */
- function repoSendTransactionQueue(repo, path, queue) {
- // Mark transactions as sent and increment retry count!
- var setsToIgnore = queue.map(function (txn) {
- return txn.currentWriteId;
- });
- var latestState = repoGetLatestState(repo, path, setsToIgnore);
- var snapToSend = latestState;
- var latestHash = latestState.hash();
- for (var i = 0; i < queue.length; i++) {
- var txn = queue[i];
- util.assert(txn.status === 0 /* TransactionStatus.RUN */, 'tryToSendTransactionQueue_: items in queue should all be run.');
- txn.status = 1 /* TransactionStatus.SENT */;
- txn.retryCount++;
- var relativePath = newRelativePath(path, txn.path);
- // If we've gotten to this point, the output snapshot must be defined.
- snapToSend = snapToSend.updateChild(relativePath /** @type {!Node} */, txn.currentOutputSnapshotRaw);
- }
- var dataToSend = snapToSend.val(true);
- var pathToSend = path;
- // Send the put.
- repo.server_.put(pathToSend.toString(), dataToSend, function (status) {
- repoLog(repo, 'transaction put response', {
- path: pathToSend.toString(),
- status: status
- });
- var events = [];
- if (status === 'ok') {
- // Queue up the callbacks and fire them after cleaning up all of our
- // transaction state, since the callback could trigger more
- // transactions or sets.
- var callbacks = [];
- var _loop_1 = function (i) {
- queue[i].status = 2 /* TransactionStatus.COMPLETED */;
- events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId));
- if (queue[i].onComplete) {
- // We never unset the output snapshot, and given that this
- // transaction is complete, it should be set
- callbacks.push(function () {
- return queue[i].onComplete(null, true, queue[i].currentOutputSnapshotResolved);
- });
- }
- queue[i].unwatcher();
- };
- for (var i = 0; i < queue.length; i++) {
- _loop_1(i);
- }
- // Now remove the completed transactions.
- repoPruneCompletedTransactionsBelowNode(repo, treeSubTree(repo.transactionQueueTree_, path));
- // There may be pending transactions that we can now send.
- repoSendReadyTransactions(repo, repo.transactionQueueTree_);
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
- // Finally, trigger onComplete callbacks.
- for (var i = 0; i < callbacks.length; i++) {
- exceptionGuard(callbacks[i]);
- }
- }
- else {
- // transactions are no longer sent. Update their status appropriately.
- if (status === 'datastale') {
- for (var i = 0; i < queue.length; i++) {
- if (queue[i].status === 3 /* TransactionStatus.SENT_NEEDS_ABORT */) {
- queue[i].status = 4 /* TransactionStatus.NEEDS_ABORT */;
- }
- else {
- queue[i].status = 0 /* TransactionStatus.RUN */;
- }
- }
- }
- else {
- warn$1('transaction at ' + pathToSend.toString() + ' failed: ' + status);
- for (var i = 0; i < queue.length; i++) {
- queue[i].status = 4 /* TransactionStatus.NEEDS_ABORT */;
- queue[i].abortReason = status;
- }
- }
- repoRerunTransactions(repo, path);
- }
- }, latestHash);
- }
- /**
- * Finds all transactions dependent on the data at changedPath and reruns them.
- *
- * Should be called any time cached data changes.
- *
- * Return the highest path that was affected by rerunning transactions. This
- * is the path at which events need to be raised for.
- *
- * @param changedPath - The path in mergedData that changed.
- * @returns The rootmost path that was affected by rerunning transactions.
- */
- function repoRerunTransactions(repo, changedPath) {
- var rootMostTransactionNode = repoGetAncestorTransactionNode(repo, changedPath);
- var path = treeGetPath(rootMostTransactionNode);
- var queue = repoBuildTransactionQueue(repo, rootMostTransactionNode);
- repoRerunTransactionQueue(repo, queue, path);
- return path;
- }
- /**
- * Does all the work of rerunning transactions (as well as cleans up aborted
- * transactions and whatnot).
- *
- * @param queue - The queue of transactions to run.
- * @param path - The path the queue is for.
- */
- function repoRerunTransactionQueue(repo, queue, path) {
- if (queue.length === 0) {
- return; // Nothing to do!
- }
- // Queue up the callbacks and fire them after cleaning up all of our
- // transaction state, since the callback could trigger more transactions or
- // sets.
- var callbacks = [];
- var events = [];
- // Ignore all of the sets we're going to re-run.
- var txnsToRerun = queue.filter(function (q) {
- return q.status === 0 /* TransactionStatus.RUN */;
- });
- var setsToIgnore = txnsToRerun.map(function (q) {
- return q.currentWriteId;
- });
- var _loop_2 = function (i) {
- var transaction = queue[i];
- var relativePath = newRelativePath(path, transaction.path);
- var abortTransaction = false, abortReason;
- util.assert(relativePath !== null, 'rerunTransactionsUnderNode_: relativePath should not be null.');
- if (transaction.status === 4 /* TransactionStatus.NEEDS_ABORT */) {
- abortTransaction = true;
- abortReason = transaction.abortReason;
- events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
- }
- else if (transaction.status === 0 /* TransactionStatus.RUN */) {
- if (transaction.retryCount >= MAX_TRANSACTION_RETRIES) {
- abortTransaction = true;
- abortReason = 'maxretry';
- events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
- }
- else {
- // This code reruns a transaction
- var currentNode = repoGetLatestState(repo, transaction.path, setsToIgnore);
- transaction.currentInputSnapshot = currentNode;
- var newData = queue[i].update(currentNode.val());
- if (newData !== undefined) {
- validateFirebaseData('transaction failed: Data returned ', newData, transaction.path);
- var newDataNode = nodeFromJSON(newData);
- var hasExplicitPriority = typeof newData === 'object' &&
- newData != null &&
- util.contains(newData, '.priority');
- if (!hasExplicitPriority) {
- // Keep the old priority if there wasn't a priority explicitly specified.
- newDataNode = newDataNode.updatePriority(currentNode.getPriority());
- }
- var oldWriteId = transaction.currentWriteId;
- var serverValues = repoGenerateServerValues(repo);
- var newNodeResolved = resolveDeferredValueSnapshot(newDataNode, currentNode, serverValues);
- transaction.currentOutputSnapshotRaw = newDataNode;
- transaction.currentOutputSnapshotResolved = newNodeResolved;
- transaction.currentWriteId = repoGetNextWriteId(repo);
- // Mutates setsToIgnore in place
- setsToIgnore.splice(setsToIgnore.indexOf(oldWriteId), 1);
- events = events.concat(syncTreeApplyUserOverwrite(repo.serverSyncTree_, transaction.path, newNodeResolved, transaction.currentWriteId, transaction.applyLocally));
- events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, oldWriteId, true));
- }
- else {
- abortTransaction = true;
- abortReason = 'nodata';
- events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, transaction.currentWriteId, true));
- }
- }
- }
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, path, events);
- events = [];
- if (abortTransaction) {
- // Abort.
- queue[i].status = 2 /* TransactionStatus.COMPLETED */;
- // Removing a listener can trigger pruning which can muck with
- // mergedData/visibleData (as it prunes data). So defer the unwatcher
- // until we're done.
- (function (unwatcher) {
- setTimeout(unwatcher, Math.floor(0));
- })(queue[i].unwatcher);
- if (queue[i].onComplete) {
- if (abortReason === 'nodata') {
- callbacks.push(function () {
- return queue[i].onComplete(null, false, queue[i].currentInputSnapshot);
- });
- }
- else {
- callbacks.push(function () {
- return queue[i].onComplete(new Error(abortReason), false, null);
- });
- }
- }
- }
- };
- for (var i = 0; i < queue.length; i++) {
- _loop_2(i);
- }
- // Clean up completed transactions.
- repoPruneCompletedTransactionsBelowNode(repo, repo.transactionQueueTree_);
- // Now fire callbacks, now that we're in a good, known state.
- for (var i = 0; i < callbacks.length; i++) {
- exceptionGuard(callbacks[i]);
- }
- // Try to send the transaction result to the server.
- repoSendReadyTransactions(repo, repo.transactionQueueTree_);
- }
- /**
- * Returns the rootmost ancestor node of the specified path that has a pending
- * transaction on it, or just returns the node for the given path if there are
- * no pending transactions on any ancestor.
- *
- * @param path - The location to start at.
- * @returns The rootmost node with a transaction.
- */
- function repoGetAncestorTransactionNode(repo, path) {
- var front;
- // Start at the root and walk deeper into the tree towards path until we
- // find a node with pending transactions.
- var transactionNode = repo.transactionQueueTree_;
- front = pathGetFront(path);
- while (front !== null && treeGetValue(transactionNode) === undefined) {
- transactionNode = treeSubTree(transactionNode, front);
- path = pathPopFront(path);
- front = pathGetFront(path);
- }
- return transactionNode;
- }
- /**
- * Builds the queue of all transactions at or below the specified
- * transactionNode.
- *
- * @param transactionNode
- * @returns The generated queue.
- */
- function repoBuildTransactionQueue(repo, transactionNode) {
- // Walk any child transaction queues and aggregate them into a single queue.
- var transactionQueue = [];
- repoAggregateTransactionQueuesForNode(repo, transactionNode, transactionQueue);
- // Sort them by the order the transactions were created.
- transactionQueue.sort(function (a, b) { return a.order - b.order; });
- return transactionQueue;
- }
- function repoAggregateTransactionQueuesForNode(repo, node, queue) {
- var nodeQueue = treeGetValue(node);
- if (nodeQueue) {
- for (var i = 0; i < nodeQueue.length; i++) {
- queue.push(nodeQueue[i]);
- }
- }
- treeForEachChild(node, function (child) {
- repoAggregateTransactionQueuesForNode(repo, child, queue);
- });
- }
- /**
- * Remove COMPLETED transactions at or below this node in the transactionQueueTree_.
- */
- function repoPruneCompletedTransactionsBelowNode(repo, node) {
- var queue = treeGetValue(node);
- if (queue) {
- var to = 0;
- for (var from = 0; from < queue.length; from++) {
- if (queue[from].status !== 2 /* TransactionStatus.COMPLETED */) {
- queue[to] = queue[from];
- to++;
- }
- }
- queue.length = to;
- treeSetValue(node, queue.length > 0 ? queue : undefined);
- }
- treeForEachChild(node, function (childNode) {
- repoPruneCompletedTransactionsBelowNode(repo, childNode);
- });
- }
- /**
- * Aborts all transactions on ancestors or descendants of the specified path.
- * Called when doing a set() or update() since we consider them incompatible
- * with transactions.
- *
- * @param path - Path for which we want to abort related transactions.
- */
- function repoAbortTransactions(repo, path) {
- var affectedPath = treeGetPath(repoGetAncestorTransactionNode(repo, path));
- var transactionNode = treeSubTree(repo.transactionQueueTree_, path);
- treeForEachAncestor(transactionNode, function (node) {
- repoAbortTransactionsOnNode(repo, node);
- });
- repoAbortTransactionsOnNode(repo, transactionNode);
- treeForEachDescendant(transactionNode, function (node) {
- repoAbortTransactionsOnNode(repo, node);
- });
- return affectedPath;
- }
- /**
- * Abort transactions stored in this transaction queue node.
- *
- * @param node - Node to abort transactions for.
- */
- function repoAbortTransactionsOnNode(repo, node) {
- var queue = treeGetValue(node);
- if (queue) {
- // Queue up the callbacks and fire them after cleaning up all of our
- // transaction state, since the callback could trigger more transactions
- // or sets.
- var callbacks = [];
- // Go through queue. Any already-sent transactions must be marked for
- // abort, while the unsent ones can be immediately aborted and removed.
- var events = [];
- var lastSent = -1;
- for (var i = 0; i < queue.length; i++) {
- if (queue[i].status === 3 /* TransactionStatus.SENT_NEEDS_ABORT */) ;
- else if (queue[i].status === 1 /* TransactionStatus.SENT */) {
- util.assert(lastSent === i - 1, 'All SENT items should be at beginning of queue.');
- lastSent = i;
- // Mark transaction for abort when it comes back.
- queue[i].status = 3 /* TransactionStatus.SENT_NEEDS_ABORT */;
- queue[i].abortReason = 'set';
- }
- else {
- util.assert(queue[i].status === 0 /* TransactionStatus.RUN */, 'Unexpected transaction status in abort');
- // We can abort it immediately.
- queue[i].unwatcher();
- events = events.concat(syncTreeAckUserWrite(repo.serverSyncTree_, queue[i].currentWriteId, true));
- if (queue[i].onComplete) {
- callbacks.push(queue[i].onComplete.bind(null, new Error('set'), false, null));
- }
- }
- }
- if (lastSent === -1) {
- // We're not waiting for any sent transactions. We can clear the queue.
- treeSetValue(node, undefined);
- }
- else {
- // Remove the transactions we aborted.
- queue.length = lastSent + 1;
- }
- // Now fire the callbacks.
- eventQueueRaiseEventsForChangedPath(repo.eventQueue_, treeGetPath(node), events);
- for (var i = 0; i < callbacks.length; i++) {
- exceptionGuard(callbacks[i]);
- }
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function decodePath(pathString) {
- var pathStringDecoded = '';
- var pieces = pathString.split('/');
- for (var i = 0; i < pieces.length; i++) {
- if (pieces[i].length > 0) {
- var piece = pieces[i];
- try {
- piece = decodeURIComponent(piece.replace(/\+/g, ' '));
- }
- catch (e) { }
- pathStringDecoded += '/' + piece;
- }
- }
- return pathStringDecoded;
- }
- /**
- * @returns key value hash
- */
- function decodeQuery(queryString) {
- var e_1, _a;
- var results = {};
- if (queryString.charAt(0) === '?') {
- queryString = queryString.substring(1);
- }
- try {
- for (var _b = tslib.__values(queryString.split('&')), _c = _b.next(); !_c.done; _c = _b.next()) {
- var segment = _c.value;
- if (segment.length === 0) {
- continue;
- }
- var kv = segment.split('=');
- if (kv.length === 2) {
- results[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
- }
- else {
- warn$1("Invalid query segment '".concat(segment, "' in query '").concat(queryString, "'"));
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- return results;
- }
- var parseRepoInfo = function (dataURL, nodeAdmin) {
- var parsedUrl = parseDatabaseURL(dataURL), namespace = parsedUrl.namespace;
- if (parsedUrl.domain === 'firebase.com') {
- fatal(parsedUrl.host +
- ' is no longer supported. ' +
- 'Please use <YOUR FIREBASE>.firebaseio.com instead');
- }
- // Catch common error of uninitialized namespace value.
- if ((!namespace || namespace === 'undefined') &&
- parsedUrl.domain !== 'localhost') {
- fatal('Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com');
- }
- if (!parsedUrl.secure) {
- warnIfPageIsSecure();
- }
- var webSocketOnly = parsedUrl.scheme === 'ws' || parsedUrl.scheme === 'wss';
- return {
- repoInfo: new RepoInfo(parsedUrl.host, parsedUrl.secure, namespace, webSocketOnly, nodeAdmin,
- /*persistenceKey=*/ '',
- /*includeNamespaceInQueryParams=*/ namespace !== parsedUrl.subdomain),
- path: new Path(parsedUrl.pathString)
- };
- };
- var parseDatabaseURL = function (dataURL) {
- // Default to empty strings in the event of a malformed string.
- var host = '', domain = '', subdomain = '', pathString = '', namespace = '';
- // Always default to SSL, unless otherwise specified.
- var secure = true, scheme = 'https', port = 443;
- // Don't do any validation here. The caller is responsible for validating the result of parsing.
- if (typeof dataURL === 'string') {
- // Parse scheme.
- var colonInd = dataURL.indexOf('//');
- if (colonInd >= 0) {
- scheme = dataURL.substring(0, colonInd - 1);
- dataURL = dataURL.substring(colonInd + 2);
- }
- // Parse host, path, and query string.
- var slashInd = dataURL.indexOf('/');
- if (slashInd === -1) {
- slashInd = dataURL.length;
- }
- var questionMarkInd = dataURL.indexOf('?');
- if (questionMarkInd === -1) {
- questionMarkInd = dataURL.length;
- }
- host = dataURL.substring(0, Math.min(slashInd, questionMarkInd));
- if (slashInd < questionMarkInd) {
- // For pathString, questionMarkInd will always come after slashInd
- pathString = decodePath(dataURL.substring(slashInd, questionMarkInd));
- }
- var queryParams = decodeQuery(dataURL.substring(Math.min(dataURL.length, questionMarkInd)));
- // If we have a port, use scheme for determining if it's secure.
- colonInd = host.indexOf(':');
- if (colonInd >= 0) {
- secure = scheme === 'https' || scheme === 'wss';
- port = parseInt(host.substring(colonInd + 1), 10);
- }
- else {
- colonInd = host.length;
- }
- var hostWithoutPort = host.slice(0, colonInd);
- if (hostWithoutPort.toLowerCase() === 'localhost') {
- domain = 'localhost';
- }
- else if (hostWithoutPort.split('.').length <= 2) {
- domain = hostWithoutPort;
- }
- else {
- // Interpret the subdomain of a 3 or more component URL as the namespace name.
- var dotInd = host.indexOf('.');
- subdomain = host.substring(0, dotInd).toLowerCase();
- domain = host.substring(dotInd + 1);
- // Normalize namespaces to lowercase to share storage / connection.
- namespace = subdomain;
- }
- // Always treat the value of the `ns` as the namespace name if it is present.
- if ('ns' in queryParams) {
- namespace = queryParams['ns'];
- }
- }
- return {
- host: host,
- port: port,
- domain: domain,
- subdomain: subdomain,
- secure: secure,
- scheme: scheme,
- pathString: pathString,
- namespace: namespace
- };
- };
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // Modeled after base64 web-safe chars, but ordered by ASCII.
- var PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
- /**
- * Fancy ID generator that creates 20-character string identifiers with the
- * following properties:
- *
- * 1. They're based on timestamp so that they sort *after* any existing ids.
- * 2. They contain 72-bits of random data after the timestamp so that IDs won't
- * collide with other clients' IDs.
- * 3. They sort *lexicographically* (so the timestamp is converted to characters
- * that will sort properly).
- * 4. They're monotonically increasing. Even if you generate more than one in
- * the same timestamp, the latter ones will sort after the former ones. We do
- * this by using the previous random bits but "incrementing" them by 1 (only
- * in the case of a timestamp collision).
- */
- var nextPushId = (function () {
- // Timestamp of last push, used to prevent local collisions if you push twice
- // in one ms.
- var lastPushTime = 0;
- // We generate 72-bits of randomness which get turned into 12 characters and
- // appended to the timestamp to prevent collisions with other clients. We
- // store the last characters we generated because in the event of a collision,
- // we'll use those same characters except "incremented" by one.
- var lastRandChars = [];
- return function (now) {
- var duplicateTime = now === lastPushTime;
- lastPushTime = now;
- var i;
- var timeStampChars = new Array(8);
- for (i = 7; i >= 0; i--) {
- timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
- // NOTE: Can't use << here because javascript will convert to int and lose
- // the upper bits.
- now = Math.floor(now / 64);
- }
- util.assert(now === 0, 'Cannot push at time == 0');
- var id = timeStampChars.join('');
- if (!duplicateTime) {
- for (i = 0; i < 12; i++) {
- lastRandChars[i] = Math.floor(Math.random() * 64);
- }
- }
- else {
- // If the timestamp hasn't changed since last push, use the same random
- // number, except incremented by 1.
- for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
- lastRandChars[i] = 0;
- }
- lastRandChars[i]++;
- }
- for (i = 0; i < 12; i++) {
- id += PUSH_CHARS.charAt(lastRandChars[i]);
- }
- util.assert(id.length === 20, 'nextPushId: Length should be 20.');
- return id;
- };
- })();
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Encapsulates the data needed to raise an event
- */
- var DataEvent = /** @class */ (function () {
- /**
- * @param eventType - One of: value, child_added, child_changed, child_moved, child_removed
- * @param eventRegistration - The function to call to with the event data. User provided
- * @param snapshot - The data backing the event
- * @param prevName - Optional, the name of the previous child for child_* events.
- */
- function DataEvent(eventType, eventRegistration, snapshot, prevName) {
- this.eventType = eventType;
- this.eventRegistration = eventRegistration;
- this.snapshot = snapshot;
- this.prevName = prevName;
- }
- DataEvent.prototype.getPath = function () {
- var ref = this.snapshot.ref;
- if (this.eventType === 'value') {
- return ref._path;
- }
- else {
- return ref.parent._path;
- }
- };
- DataEvent.prototype.getEventType = function () {
- return this.eventType;
- };
- DataEvent.prototype.getEventRunner = function () {
- return this.eventRegistration.getEventRunner(this);
- };
- DataEvent.prototype.toString = function () {
- return (this.getPath().toString() +
- ':' +
- this.eventType +
- ':' +
- util.stringify(this.snapshot.exportVal()));
- };
- return DataEvent;
- }());
- var CancelEvent = /** @class */ (function () {
- function CancelEvent(eventRegistration, error, path) {
- this.eventRegistration = eventRegistration;
- this.error = error;
- this.path = path;
- }
- CancelEvent.prototype.getPath = function () {
- return this.path;
- };
- CancelEvent.prototype.getEventType = function () {
- return 'cancel';
- };
- CancelEvent.prototype.getEventRunner = function () {
- return this.eventRegistration.getEventRunner(this);
- };
- CancelEvent.prototype.toString = function () {
- return this.path.toString() + ':cancel';
- };
- return CancelEvent;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * A wrapper class that converts events from the database@exp SDK to the legacy
- * Database SDK. Events are not converted directly as event registration relies
- * on reference comparison of the original user callback (see `matches()`) and
- * relies on equality of the legacy SDK's `context` object.
- */
- var CallbackContext = /** @class */ (function () {
- function CallbackContext(snapshotCallback, cancelCallback) {
- this.snapshotCallback = snapshotCallback;
- this.cancelCallback = cancelCallback;
- }
- CallbackContext.prototype.onValue = function (expDataSnapshot, previousChildName) {
- this.snapshotCallback.call(null, expDataSnapshot, previousChildName);
- };
- CallbackContext.prototype.onCancel = function (error) {
- util.assert(this.hasCancelCallback, 'Raising a cancel event on a listener with no cancel callback');
- return this.cancelCallback.call(null, error);
- };
- Object.defineProperty(CallbackContext.prototype, "hasCancelCallback", {
- get: function () {
- return !!this.cancelCallback;
- },
- enumerable: false,
- configurable: true
- });
- CallbackContext.prototype.matches = function (other) {
- return (this.snapshotCallback === other.snapshotCallback ||
- (this.snapshotCallback.userCallback !== undefined &&
- this.snapshotCallback.userCallback ===
- other.snapshotCallback.userCallback &&
- this.snapshotCallback.context === other.snapshotCallback.context));
- };
- return CallbackContext;
- }());
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * The `onDisconnect` class allows you to write or clear data when your client
- * disconnects from the Database server. These updates occur whether your
- * client disconnects cleanly or not, so you can rely on them to clean up data
- * even if a connection is dropped or a client crashes.
- *
- * The `onDisconnect` class is most commonly used to manage presence in
- * applications where it is useful to detect how many clients are connected and
- * when other clients disconnect. See
- * {@link https://firebase.google.com/docs/database/web/offline-capabilities | Enabling Offline Capabilities in JavaScript}
- * for more information.
- *
- * To avoid problems when a connection is dropped before the requests can be
- * transferred to the Database server, these functions should be called before
- * writing any data.
- *
- * Note that `onDisconnect` operations are only triggered once. If you want an
- * operation to occur each time a disconnect occurs, you'll need to re-establish
- * the `onDisconnect` operations each time you reconnect.
- */
- var OnDisconnect$1 = /** @class */ (function () {
- /** @hideconstructor */
- function OnDisconnect(_repo, _path) {
- this._repo = _repo;
- this._path = _path;
- }
- /**
- * Cancels all previously queued `onDisconnect()` set or update events for this
- * location and all children.
- *
- * If a write has been queued for this location via a `set()` or `update()` at a
- * parent location, the write at this location will be canceled, though writes
- * to sibling locations will still occur.
- *
- * @returns Resolves when synchronization to the server is complete.
- */
- OnDisconnect.prototype.cancel = function () {
- var deferred = new util.Deferred();
- repoOnDisconnectCancel(this._repo, this._path, deferred.wrapCallback(function () { }));
- return deferred.promise;
- };
- /**
- * Ensures the data at this location is deleted when the client is disconnected
- * (due to closing the browser, navigating to a new page, or network issues).
- *
- * @returns Resolves when synchronization to the server is complete.
- */
- OnDisconnect.prototype.remove = function () {
- validateWritablePath('OnDisconnect.remove', this._path);
- var deferred = new util.Deferred();
- repoOnDisconnectSet(this._repo, this._path, null, deferred.wrapCallback(function () { }));
- return deferred.promise;
- };
- /**
- * Ensures the data at this location is set to the specified value when the
- * client is disconnected (due to closing the browser, navigating to a new page,
- * or network issues).
- *
- * `set()` is especially useful for implementing "presence" systems, where a
- * value should be changed or cleared when a user disconnects so that they
- * appear "offline" to other users. See
- * {@link https://firebase.google.com/docs/database/web/offline-capabilities | Enabling Offline Capabilities in JavaScript}
- * for more information.
- *
- * Note that `onDisconnect` operations are only triggered once. If you want an
- * operation to occur each time a disconnect occurs, you'll need to re-establish
- * the `onDisconnect` operations each time.
- *
- * @param value - The value to be written to this location on disconnect (can
- * be an object, array, string, number, boolean, or null).
- * @returns Resolves when synchronization to the Database is complete.
- */
- OnDisconnect.prototype.set = function (value) {
- validateWritablePath('OnDisconnect.set', this._path);
- validateFirebaseDataArg('OnDisconnect.set', value, this._path, false);
- var deferred = new util.Deferred();
- repoOnDisconnectSet(this._repo, this._path, value, deferred.wrapCallback(function () { }));
- return deferred.promise;
- };
- /**
- * Ensures the data at this location is set to the specified value and priority
- * when the client is disconnected (due to closing the browser, navigating to a
- * new page, or network issues).
- *
- * @param value - The value to be written to this location on disconnect (can
- * be an object, array, string, number, boolean, or null).
- * @param priority - The priority to be written (string, number, or null).
- * @returns Resolves when synchronization to the Database is complete.
- */
- OnDisconnect.prototype.setWithPriority = function (value, priority) {
- validateWritablePath('OnDisconnect.setWithPriority', this._path);
- validateFirebaseDataArg('OnDisconnect.setWithPriority', value, this._path, false);
- validatePriority('OnDisconnect.setWithPriority', priority, false);
- var deferred = new util.Deferred();
- repoOnDisconnectSetWithPriority(this._repo, this._path, value, priority, deferred.wrapCallback(function () { }));
- return deferred.promise;
- };
- /**
- * Writes multiple values at this location when the client is disconnected (due
- * to closing the browser, navigating to a new page, or network issues).
- *
- * The `values` argument contains multiple property-value pairs that will be
- * written to the Database together. Each child property can either be a simple
- * property (for example, "name") or a relative path (for example, "name/first")
- * from the current location to the data to update.
- *
- * As opposed to the `set()` method, `update()` can be use to selectively update
- * only the referenced properties at the current location (instead of replacing
- * all the child properties at the current location).
- *
- * @param values - Object containing multiple values.
- * @returns Resolves when synchronization to the Database is complete.
- */
- OnDisconnect.prototype.update = function (values) {
- validateWritablePath('OnDisconnect.update', this._path);
- validateFirebaseMergeDataArg('OnDisconnect.update', values, this._path, false);
- var deferred = new util.Deferred();
- repoOnDisconnectUpdate(this._repo, this._path, values, deferred.wrapCallback(function () { }));
- return deferred.promise;
- };
- return OnDisconnect;
- }());
-
- /**
- * @license
- * Copyright 2020 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @internal
- */
- var QueryImpl = /** @class */ (function () {
- /**
- * @hideconstructor
- */
- function QueryImpl(_repo, _path, _queryParams, _orderByCalled) {
- this._repo = _repo;
- this._path = _path;
- this._queryParams = _queryParams;
- this._orderByCalled = _orderByCalled;
- }
- Object.defineProperty(QueryImpl.prototype, "key", {
- get: function () {
- if (pathIsEmpty(this._path)) {
- return null;
- }
- else {
- return pathGetBack(this._path);
- }
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(QueryImpl.prototype, "ref", {
- get: function () {
- return new ReferenceImpl(this._repo, this._path);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(QueryImpl.prototype, "_queryIdentifier", {
- get: function () {
- var obj = queryParamsGetQueryObject(this._queryParams);
- var id = ObjectToUniqueKey(obj);
- return id === '{}' ? 'default' : id;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(QueryImpl.prototype, "_queryObject", {
- /**
- * An object representation of the query parameters used by this Query.
- */
- get: function () {
- return queryParamsGetQueryObject(this._queryParams);
- },
- enumerable: false,
- configurable: true
- });
- QueryImpl.prototype.isEqual = function (other) {
- other = util.getModularInstance(other);
- if (!(other instanceof QueryImpl)) {
- return false;
- }
- var sameRepo = this._repo === other._repo;
- var samePath = pathEquals(this._path, other._path);
- var sameQueryIdentifier = this._queryIdentifier === other._queryIdentifier;
- return sameRepo && samePath && sameQueryIdentifier;
- };
- QueryImpl.prototype.toJSON = function () {
- return this.toString();
- };
- QueryImpl.prototype.toString = function () {
- return this._repo.toString() + pathToUrlEncodedString(this._path);
- };
- return QueryImpl;
- }());
- /**
- * Validates that no other order by call has been made
- */
- function validateNoPreviousOrderByCall(query, fnName) {
- if (query._orderByCalled === true) {
- throw new Error(fnName + ": You can't combine multiple orderBy calls.");
- }
- }
- /**
- * Validates start/end values for queries.
- */
- function validateQueryEndpoints(params) {
- var startNode = null;
- var endNode = null;
- if (params.hasStart()) {
- startNode = params.getIndexStartValue();
- }
- if (params.hasEnd()) {
- endNode = params.getIndexEndValue();
- }
- if (params.getIndex() === KEY_INDEX) {
- var tooManyArgsError = 'Query: When ordering by key, you may only pass one argument to ' +
- 'startAt(), endAt(), or equalTo().';
- var wrongArgTypeError = 'Query: When ordering by key, the argument passed to startAt(), startAfter(), ' +
- 'endAt(), endBefore(), or equalTo() must be a string.';
- if (params.hasStart()) {
- var startName = params.getIndexStartName();
- if (startName !== MIN_NAME) {
- throw new Error(tooManyArgsError);
- }
- else if (typeof startNode !== 'string') {
- throw new Error(wrongArgTypeError);
- }
- }
- if (params.hasEnd()) {
- var endName = params.getIndexEndName();
- if (endName !== MAX_NAME) {
- throw new Error(tooManyArgsError);
- }
- else if (typeof endNode !== 'string') {
- throw new Error(wrongArgTypeError);
- }
- }
- }
- else if (params.getIndex() === PRIORITY_INDEX) {
- if ((startNode != null && !isValidPriority(startNode)) ||
- (endNode != null && !isValidPriority(endNode))) {
- throw new Error('Query: When ordering by priority, the first argument passed to startAt(), ' +
- 'startAfter() endAt(), endBefore(), or equalTo() must be a valid priority value ' +
- '(null, a number, or a string).');
- }
- }
- else {
- util.assert(params.getIndex() instanceof PathIndex ||
- params.getIndex() === VALUE_INDEX, 'unknown index type.');
- if ((startNode != null && typeof startNode === 'object') ||
- (endNode != null && typeof endNode === 'object')) {
- throw new Error('Query: First argument passed to startAt(), startAfter(), endAt(), endBefore(), or ' +
- 'equalTo() cannot be an object.');
- }
- }
- }
- /**
- * Validates that limit* has been called with the correct combination of parameters
- */
- function validateLimit(params) {
- if (params.hasStart() &&
- params.hasEnd() &&
- params.hasLimit() &&
- !params.hasAnchoredLimit()) {
- throw new Error("Query: Can't combine startAt(), startAfter(), endAt(), endBefore(), and limit(). Use " +
- 'limitToFirst() or limitToLast() instead.');
- }
- }
- /**
- * @internal
- */
- var ReferenceImpl = /** @class */ (function (_super) {
- tslib.__extends(ReferenceImpl, _super);
- /** @hideconstructor */
- function ReferenceImpl(repo, path) {
- return _super.call(this, repo, path, new QueryParams(), false) || this;
- }
- Object.defineProperty(ReferenceImpl.prototype, "parent", {
- get: function () {
- var parentPath = pathParent(this._path);
- return parentPath === null
- ? null
- : new ReferenceImpl(this._repo, parentPath);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(ReferenceImpl.prototype, "root", {
- get: function () {
- var ref = this;
- while (ref.parent !== null) {
- ref = ref.parent;
- }
- return ref;
- },
- enumerable: false,
- configurable: true
- });
- return ReferenceImpl;
- }(QueryImpl));
- /**
- * A `DataSnapshot` contains data from a Database location.
- *
- * Any time you read data from the Database, you receive the data as a
- * `DataSnapshot`. A `DataSnapshot` is passed to the event callbacks you attach
- * with `on()` or `once()`. You can extract the contents of the snapshot as a
- * JavaScript object by calling the `val()` method. Alternatively, you can
- * traverse into the snapshot by calling `child()` to return child snapshots
- * (which you could then call `val()` on).
- *
- * A `DataSnapshot` is an efficiently generated, immutable copy of the data at
- * a Database location. It cannot be modified and will never change (to modify
- * data, you always call the `set()` method on a `Reference` directly).
- */
- var DataSnapshot$1 = /** @class */ (function () {
- /**
- * @param _node - A SnapshotNode to wrap.
- * @param ref - The location this snapshot came from.
- * @param _index - The iteration order for this snapshot
- * @hideconstructor
- */
- function DataSnapshot(_node,
- /**
- * The location of this DataSnapshot.
- */
- ref, _index) {
- this._node = _node;
- this.ref = ref;
- this._index = _index;
- }
- Object.defineProperty(DataSnapshot.prototype, "priority", {
- /**
- * Gets the priority value of the data in this `DataSnapshot`.
- *
- * Applications need not use priority but can order collections by
- * ordinary properties (see
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data |Sorting and filtering data}
- * ).
- */
- get: function () {
- // typecast here because we never return deferred values or internal priorities (MAX_PRIORITY)
- return this._node.getPriority().val();
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(DataSnapshot.prototype, "key", {
- /**
- * The key (last part of the path) of the location of this `DataSnapshot`.
- *
- * The last token in a Database location is considered its key. For example,
- * "ada" is the key for the /users/ada/ node. Accessing the key on any
- * `DataSnapshot` will return the key for the location that generated it.
- * However, accessing the key on the root URL of a Database will return
- * `null`.
- */
- get: function () {
- return this.ref.key;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(DataSnapshot.prototype, "size", {
- /** Returns the number of child properties of this `DataSnapshot`. */
- get: function () {
- return this._node.numChildren();
- },
- enumerable: false,
- configurable: true
- });
- /**
- * Gets another `DataSnapshot` for the location at the specified relative path.
- *
- * Passing a relative path to the `child()` method of a DataSnapshot returns
- * another `DataSnapshot` for the location at the specified relative path. The
- * relative path can either be a simple child name (for example, "ada") or a
- * deeper, slash-separated path (for example, "ada/name/first"). If the child
- * location has no data, an empty `DataSnapshot` (that is, a `DataSnapshot`
- * whose value is `null`) is returned.
- *
- * @param path - A relative path to the location of child data.
- */
- DataSnapshot.prototype.child = function (path) {
- var childPath = new Path(path);
- var childRef = child(this.ref, path);
- return new DataSnapshot(this._node.getChild(childPath), childRef, PRIORITY_INDEX);
- };
- /**
- * Returns true if this `DataSnapshot` contains any data. It is slightly more
- * efficient than using `snapshot.val() !== null`.
- */
- DataSnapshot.prototype.exists = function () {
- return !this._node.isEmpty();
- };
- /**
- * Exports the entire contents of the DataSnapshot as a JavaScript object.
- *
- * The `exportVal()` method is similar to `val()`, except priority information
- * is included (if available), making it suitable for backing up your data.
- *
- * @returns The DataSnapshot's contents as a JavaScript value (Object,
- * Array, string, number, boolean, or `null`).
- */
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- DataSnapshot.prototype.exportVal = function () {
- return this._node.val(true);
- };
- /**
- * Enumerates the top-level children in the `DataSnapshot`.
- *
- * Because of the way JavaScript objects work, the ordering of data in the
- * JavaScript object returned by `val()` is not guaranteed to match the
- * ordering on the server nor the ordering of `onChildAdded()` events. That is
- * where `forEach()` comes in handy. It guarantees the children of a
- * `DataSnapshot` will be iterated in their query order.
- *
- * If no explicit `orderBy*()` method is used, results are returned
- * ordered by key (unless priorities are used, in which case, results are
- * returned by priority).
- *
- * @param action - A function that will be called for each child DataSnapshot.
- * The callback can return true to cancel further enumeration.
- * @returns true if enumeration was canceled due to your callback returning
- * true.
- */
- DataSnapshot.prototype.forEach = function (action) {
- var _this = this;
- if (this._node.isLeafNode()) {
- return false;
- }
- var childrenNode = this._node;
- // Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
- return !!childrenNode.forEachChild(this._index, function (key, node) {
- return action(new DataSnapshot(node, child(_this.ref, key), PRIORITY_INDEX));
- });
- };
- /**
- * Returns true if the specified child path has (non-null) data.
- *
- * @param path - A relative path to the location of a potential child.
- * @returns `true` if data exists at the specified child path; else
- * `false`.
- */
- DataSnapshot.prototype.hasChild = function (path) {
- var childPath = new Path(path);
- return !this._node.getChild(childPath).isEmpty();
- };
- /**
- * Returns whether or not the `DataSnapshot` has any non-`null` child
- * properties.
- *
- * You can use `hasChildren()` to determine if a `DataSnapshot` has any
- * children. If it does, you can enumerate them using `forEach()`. If it
- * doesn't, then either this snapshot contains a primitive value (which can be
- * retrieved with `val()`) or it is empty (in which case, `val()` will return
- * `null`).
- *
- * @returns true if this snapshot has any children; else false.
- */
- DataSnapshot.prototype.hasChildren = function () {
- if (this._node.isLeafNode()) {
- return false;
- }
- else {
- return !this._node.isEmpty();
- }
- };
- /**
- * Returns a JSON-serializable representation of this object.
- */
- DataSnapshot.prototype.toJSON = function () {
- return this.exportVal();
- };
- /**
- * Extracts a JavaScript value from a `DataSnapshot`.
- *
- * Depending on the data in a `DataSnapshot`, the `val()` method may return a
- * scalar type (string, number, or boolean), an array, or an object. It may
- * also return null, indicating that the `DataSnapshot` is empty (contains no
- * data).
- *
- * @returns The DataSnapshot's contents as a JavaScript value (Object,
- * Array, string, number, boolean, or `null`).
- */
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- DataSnapshot.prototype.val = function () {
- return this._node.val();
- };
- return DataSnapshot;
- }());
- /**
- *
- * Returns a `Reference` representing the location in the Database
- * corresponding to the provided path. If no path is provided, the `Reference`
- * will point to the root of the Database.
- *
- * @param db - The database instance to obtain a reference for.
- * @param path - Optional path representing the location the returned
- * `Reference` will point. If not provided, the returned `Reference` will
- * point to the root of the Database.
- * @returns If a path is provided, a `Reference`
- * pointing to the provided path. Otherwise, a `Reference` pointing to the
- * root of the Database.
- */
- function ref(db, path) {
- db = util.getModularInstance(db);
- db._checkNotDeleted('ref');
- return path !== undefined ? child(db._root, path) : db._root;
- }
- /**
- * Returns a `Reference` representing the location in the Database
- * corresponding to the provided Firebase URL.
- *
- * An exception is thrown if the URL is not a valid Firebase Database URL or it
- * has a different domain than the current `Database` instance.
- *
- * Note that all query parameters (`orderBy`, `limitToLast`, etc.) are ignored
- * and are not applied to the returned `Reference`.
- *
- * @param db - The database instance to obtain a reference for.
- * @param url - The Firebase URL at which the returned `Reference` will
- * point.
- * @returns A `Reference` pointing to the provided
- * Firebase URL.
- */
- function refFromURL(db, url) {
- db = util.getModularInstance(db);
- db._checkNotDeleted('refFromURL');
- var parsedURL = parseRepoInfo(url, db._repo.repoInfo_.nodeAdmin);
- validateUrl('refFromURL', parsedURL);
- var repoInfo = parsedURL.repoInfo;
- if (!db._repo.repoInfo_.isCustomHost() &&
- repoInfo.host !== db._repo.repoInfo_.host) {
- fatal('refFromURL' +
- ': Host name does not match the current database: ' +
- '(found ' +
- repoInfo.host +
- ' but expected ' +
- db._repo.repoInfo_.host +
- ')');
- }
- return ref(db, parsedURL.path.toString());
- }
- /**
- * Gets a `Reference` for the location at the specified relative path.
- *
- * The relative path can either be a simple child name (for example, "ada") or
- * a deeper slash-separated path (for example, "ada/name/first").
- *
- * @param parent - The parent location.
- * @param path - A relative path from this location to the desired child
- * location.
- * @returns The specified child location.
- */
- function child(parent, path) {
- parent = util.getModularInstance(parent);
- if (pathGetFront(parent._path) === null) {
- validateRootPathString('child', 'path', path, false);
- }
- else {
- validatePathString('child', 'path', path, false);
- }
- return new ReferenceImpl(parent._repo, pathChild(parent._path, path));
- }
- /**
- * Returns an `OnDisconnect` object - see
- * {@link https://firebase.google.com/docs/database/web/offline-capabilities | Enabling Offline Capabilities in JavaScript}
- * for more information on how to use it.
- *
- * @param ref - The reference to add OnDisconnect triggers for.
- */
- function onDisconnect(ref) {
- ref = util.getModularInstance(ref);
- return new OnDisconnect$1(ref._repo, ref._path);
- }
- /**
- * Generates a new child location using a unique key and returns its
- * `Reference`.
- *
- * This is the most common pattern for adding data to a collection of items.
- *
- * If you provide a value to `push()`, the value is written to the
- * generated location. If you don't pass a value, nothing is written to the
- * database and the child remains empty (but you can use the `Reference`
- * elsewhere).
- *
- * The unique keys generated by `push()` are ordered by the current time, so the
- * resulting list of items is chronologically sorted. The keys are also
- * designed to be unguessable (they contain 72 random bits of entropy).
- *
- * See {@link https://firebase.google.com/docs/database/web/lists-of-data#append_to_a_list_of_data | Append to a list of data}.
- * See {@link https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html | The 2^120 Ways to Ensure Unique Identifiers}.
- *
- * @param parent - The parent location.
- * @param value - Optional value to be written at the generated location.
- * @returns Combined `Promise` and `Reference`; resolves when write is complete,
- * but can be used immediately as the `Reference` to the child location.
- */
- function push(parent, value) {
- parent = util.getModularInstance(parent);
- validateWritablePath('push', parent._path);
- validateFirebaseDataArg('push', value, parent._path, true);
- var now = repoServerTime(parent._repo);
- var name = nextPushId(now);
- // push() returns a ThennableReference whose promise is fulfilled with a
- // regular Reference. We use child() to create handles to two different
- // references. The first is turned into a ThennableReference below by adding
- // then() and catch() methods and is used as the return value of push(). The
- // second remains a regular Reference and is used as the fulfilled value of
- // the first ThennableReference.
- var thennablePushRef = child(parent, name);
- var pushRef = child(parent, name);
- var promise;
- if (value != null) {
- promise = set(pushRef, value).then(function () { return pushRef; });
- }
- else {
- promise = Promise.resolve(pushRef);
- }
- thennablePushRef.then = promise.then.bind(promise);
- thennablePushRef.catch = promise.then.bind(promise, undefined);
- return thennablePushRef;
- }
- /**
- * Removes the data at this Database location.
- *
- * Any data at child locations will also be deleted.
- *
- * The effect of the remove will be visible immediately and the corresponding
- * event 'value' will be triggered. Synchronization of the remove to the
- * Firebase servers will also be started, and the returned Promise will resolve
- * when complete. If provided, the onComplete callback will be called
- * asynchronously after synchronization has finished.
- *
- * @param ref - The location to remove.
- * @returns Resolves when remove on server is complete.
- */
- function remove(ref) {
- validateWritablePath('remove', ref._path);
- return set(ref, null);
- }
- /**
- * Writes data to this Database location.
- *
- * This will overwrite any data at this location and all child locations.
- *
- * The effect of the write will be visible immediately, and the corresponding
- * events ("value", "child_added", etc.) will be triggered. Synchronization of
- * the data to the Firebase servers will also be started, and the returned
- * Promise will resolve when complete. If provided, the `onComplete` callback
- * will be called asynchronously after synchronization has finished.
- *
- * Passing `null` for the new value is equivalent to calling `remove()`; namely,
- * all data at this location and all child locations will be deleted.
- *
- * `set()` will remove any priority stored at this location, so if priority is
- * meant to be preserved, you need to use `setWithPriority()` instead.
- *
- * Note that modifying data with `set()` will cancel any pending transactions
- * at that location, so extreme care should be taken if mixing `set()` and
- * `transaction()` to modify the same data.
- *
- * A single `set()` will generate a single "value" event at the location where
- * the `set()` was performed.
- *
- * @param ref - The location to write to.
- * @param value - The value to be written (string, number, boolean, object,
- * array, or null).
- * @returns Resolves when write to server is complete.
- */
- function set(ref, value) {
- ref = util.getModularInstance(ref);
- validateWritablePath('set', ref._path);
- validateFirebaseDataArg('set', value, ref._path, false);
- var deferred = new util.Deferred();
- repoSetWithPriority(ref._repo, ref._path, value,
- /*priority=*/ null, deferred.wrapCallback(function () { }));
- return deferred.promise;
- }
- /**
- * Sets a priority for the data at this Database location.
- *
- * Applications need not use priority but can order collections by
- * ordinary properties (see
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data | Sorting and filtering data}
- * ).
- *
- * @param ref - The location to write to.
- * @param priority - The priority to be written (string, number, or null).
- * @returns Resolves when write to server is complete.
- */
- function setPriority(ref, priority) {
- ref = util.getModularInstance(ref);
- validateWritablePath('setPriority', ref._path);
- validatePriority('setPriority', priority, false);
- var deferred = new util.Deferred();
- repoSetWithPriority(ref._repo, pathChild(ref._path, '.priority'), priority, null, deferred.wrapCallback(function () { }));
- return deferred.promise;
- }
- /**
- * Writes data the Database location. Like `set()` but also specifies the
- * priority for that data.
- *
- * Applications need not use priority but can order collections by
- * ordinary properties (see
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data | Sorting and filtering data}
- * ).
- *
- * @param ref - The location to write to.
- * @param value - The value to be written (string, number, boolean, object,
- * array, or null).
- * @param priority - The priority to be written (string, number, or null).
- * @returns Resolves when write to server is complete.
- */
- function setWithPriority(ref, value, priority) {
- validateWritablePath('setWithPriority', ref._path);
- validateFirebaseDataArg('setWithPriority', value, ref._path, false);
- validatePriority('setWithPriority', priority, false);
- if (ref.key === '.length' || ref.key === '.keys') {
- throw 'setWithPriority failed: ' + ref.key + ' is a read-only object.';
- }
- var deferred = new util.Deferred();
- repoSetWithPriority(ref._repo, ref._path, value, priority, deferred.wrapCallback(function () { }));
- return deferred.promise;
- }
- /**
- * Writes multiple values to the Database at once.
- *
- * The `values` argument contains multiple property-value pairs that will be
- * written to the Database together. Each child property can either be a simple
- * property (for example, "name") or a relative path (for example,
- * "name/first") from the current location to the data to update.
- *
- * As opposed to the `set()` method, `update()` can be use to selectively update
- * only the referenced properties at the current location (instead of replacing
- * all the child properties at the current location).
- *
- * The effect of the write will be visible immediately, and the corresponding
- * events ('value', 'child_added', etc.) will be triggered. Synchronization of
- * the data to the Firebase servers will also be started, and the returned
- * Promise will resolve when complete. If provided, the `onComplete` callback
- * will be called asynchronously after synchronization has finished.
- *
- * A single `update()` will generate a single "value" event at the location
- * where the `update()` was performed, regardless of how many children were
- * modified.
- *
- * Note that modifying data with `update()` will cancel any pending
- * transactions at that location, so extreme care should be taken if mixing
- * `update()` and `transaction()` to modify the same data.
- *
- * Passing `null` to `update()` will remove the data at this location.
- *
- * See
- * {@link https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html | Introducing multi-location updates and more}.
- *
- * @param ref - The location to write to.
- * @param values - Object containing multiple values.
- * @returns Resolves when update on server is complete.
- */
- function update(ref, values) {
- validateFirebaseMergeDataArg('update', values, ref._path, false);
- var deferred = new util.Deferred();
- repoUpdate(ref._repo, ref._path, values, deferred.wrapCallback(function () { }));
- return deferred.promise;
- }
- /**
- * Gets the most up-to-date result for this query.
- *
- * @param query - The query to run.
- * @returns A `Promise` which resolves to the resulting DataSnapshot if a value is
- * available, or rejects if the client is unable to return a value (e.g., if the
- * server is unreachable and there is nothing cached).
- */
- function get(query) {
- query = util.getModularInstance(query);
- var callbackContext = new CallbackContext(function () { });
- var container = new ValueEventRegistration(callbackContext);
- return repoGetValue(query._repo, query, container).then(function (node) {
- return new DataSnapshot$1(node, new ReferenceImpl(query._repo, query._path), query._queryParams.getIndex());
- });
- }
- /**
- * Represents registration for 'value' events.
- */
- var ValueEventRegistration = /** @class */ (function () {
- function ValueEventRegistration(callbackContext) {
- this.callbackContext = callbackContext;
- }
- ValueEventRegistration.prototype.respondsTo = function (eventType) {
- return eventType === 'value';
- };
- ValueEventRegistration.prototype.createEvent = function (change, query) {
- var index = query._queryParams.getIndex();
- return new DataEvent('value', this, new DataSnapshot$1(change.snapshotNode, new ReferenceImpl(query._repo, query._path), index));
- };
- ValueEventRegistration.prototype.getEventRunner = function (eventData) {
- var _this = this;
- if (eventData.getEventType() === 'cancel') {
- return function () {
- return _this.callbackContext.onCancel(eventData.error);
- };
- }
- else {
- return function () {
- return _this.callbackContext.onValue(eventData.snapshot, null);
- };
- }
- };
- ValueEventRegistration.prototype.createCancelEvent = function (error, path) {
- if (this.callbackContext.hasCancelCallback) {
- return new CancelEvent(this, error, path);
- }
- else {
- return null;
- }
- };
- ValueEventRegistration.prototype.matches = function (other) {
- if (!(other instanceof ValueEventRegistration)) {
- return false;
- }
- else if (!other.callbackContext || !this.callbackContext) {
- // If no callback specified, we consider it to match any callback.
- return true;
- }
- else {
- return other.callbackContext.matches(this.callbackContext);
- }
- };
- ValueEventRegistration.prototype.hasAnyCallback = function () {
- return this.callbackContext !== null;
- };
- return ValueEventRegistration;
- }());
- /**
- * Represents the registration of a child_x event.
- */
- var ChildEventRegistration = /** @class */ (function () {
- function ChildEventRegistration(eventType, callbackContext) {
- this.eventType = eventType;
- this.callbackContext = callbackContext;
- }
- ChildEventRegistration.prototype.respondsTo = function (eventType) {
- var eventToCheck = eventType === 'children_added' ? 'child_added' : eventType;
- eventToCheck =
- eventToCheck === 'children_removed' ? 'child_removed' : eventToCheck;
- return this.eventType === eventToCheck;
- };
- ChildEventRegistration.prototype.createCancelEvent = function (error, path) {
- if (this.callbackContext.hasCancelCallback) {
- return new CancelEvent(this, error, path);
- }
- else {
- return null;
- }
- };
- ChildEventRegistration.prototype.createEvent = function (change, query) {
- util.assert(change.childName != null, 'Child events should have a childName.');
- var childRef = child(new ReferenceImpl(query._repo, query._path), change.childName);
- var index = query._queryParams.getIndex();
- return new DataEvent(change.type, this, new DataSnapshot$1(change.snapshotNode, childRef, index), change.prevName);
- };
- ChildEventRegistration.prototype.getEventRunner = function (eventData) {
- var _this = this;
- if (eventData.getEventType() === 'cancel') {
- return function () {
- return _this.callbackContext.onCancel(eventData.error);
- };
- }
- else {
- return function () {
- return _this.callbackContext.onValue(eventData.snapshot, eventData.prevName);
- };
- }
- };
- ChildEventRegistration.prototype.matches = function (other) {
- if (other instanceof ChildEventRegistration) {
- return (this.eventType === other.eventType &&
- (!this.callbackContext ||
- !other.callbackContext ||
- this.callbackContext.matches(other.callbackContext)));
- }
- return false;
- };
- ChildEventRegistration.prototype.hasAnyCallback = function () {
- return !!this.callbackContext;
- };
- return ChildEventRegistration;
- }());
- function addEventListener(query, eventType, callback, cancelCallbackOrListenOptions, options) {
- var cancelCallback;
- if (typeof cancelCallbackOrListenOptions === 'object') {
- cancelCallback = undefined;
- options = cancelCallbackOrListenOptions;
- }
- if (typeof cancelCallbackOrListenOptions === 'function') {
- cancelCallback = cancelCallbackOrListenOptions;
- }
- if (options && options.onlyOnce) {
- var userCallback_1 = callback;
- var onceCallback = function (dataSnapshot, previousChildName) {
- repoRemoveEventCallbackForQuery(query._repo, query, container);
- userCallback_1(dataSnapshot, previousChildName);
- };
- onceCallback.userCallback = callback.userCallback;
- onceCallback.context = callback.context;
- callback = onceCallback;
- }
- var callbackContext = new CallbackContext(callback, cancelCallback || undefined);
- var container = eventType === 'value'
- ? new ValueEventRegistration(callbackContext)
- : new ChildEventRegistration(eventType, callbackContext);
- repoAddEventCallbackForQuery(query._repo, query, container);
- return function () { return repoRemoveEventCallbackForQuery(query._repo, query, container); };
- }
- function onValue(query, callback, cancelCallbackOrListenOptions, options) {
- return addEventListener(query, 'value', callback, cancelCallbackOrListenOptions, options);
- }
- function onChildAdded(query, callback, cancelCallbackOrListenOptions, options) {
- return addEventListener(query, 'child_added', callback, cancelCallbackOrListenOptions, options);
- }
- function onChildChanged(query, callback, cancelCallbackOrListenOptions, options) {
- return addEventListener(query, 'child_changed', callback, cancelCallbackOrListenOptions, options);
- }
- function onChildMoved(query, callback, cancelCallbackOrListenOptions, options) {
- return addEventListener(query, 'child_moved', callback, cancelCallbackOrListenOptions, options);
- }
- function onChildRemoved(query, callback, cancelCallbackOrListenOptions, options) {
- return addEventListener(query, 'child_removed', callback, cancelCallbackOrListenOptions, options);
- }
- /**
- * Detaches a callback previously attached with the corresponding `on*()` (`onValue`, `onChildAdded`) listener.
- * Note: This is not the recommended way to remove a listener. Instead, please use the returned callback function from
- * the respective `on*` callbacks.
- *
- * Detach a callback previously attached with `on*()`. Calling `off()` on a parent listener
- * will not automatically remove listeners registered on child nodes, `off()`
- * must also be called on any child listeners to remove the callback.
- *
- * If a callback is not specified, all callbacks for the specified eventType
- * will be removed. Similarly, if no eventType is specified, all callbacks
- * for the `Reference` will be removed.
- *
- * Individual listeners can also be removed by invoking their unsubscribe
- * callbacks.
- *
- * @param query - The query that the listener was registered with.
- * @param eventType - One of the following strings: "value", "child_added",
- * "child_changed", "child_removed", or "child_moved." If omitted, all callbacks
- * for the `Reference` will be removed.
- * @param callback - The callback function that was passed to `on()` or
- * `undefined` to remove all callbacks.
- */
- function off(query, eventType, callback) {
- var container = null;
- var expCallback = callback ? new CallbackContext(callback) : null;
- if (eventType === 'value') {
- container = new ValueEventRegistration(expCallback);
- }
- else if (eventType) {
- container = new ChildEventRegistration(eventType, expCallback);
- }
- repoRemoveEventCallbackForQuery(query._repo, query, container);
- }
- /**
- * A `QueryConstraint` is used to narrow the set of documents returned by a
- * Database query. `QueryConstraint`s are created by invoking {@link endAt},
- * {@link endBefore}, {@link startAt}, {@link startAfter}, {@link
- * limitToFirst}, {@link limitToLast}, {@link orderByChild},
- * {@link orderByChild}, {@link orderByKey} , {@link orderByPriority} ,
- * {@link orderByValue} or {@link equalTo} and
- * can then be passed to {@link query} to create a new query instance that
- * also contains this `QueryConstraint`.
- */
- var QueryConstraint = /** @class */ (function () {
- function QueryConstraint() {
- }
- return QueryConstraint;
- }());
- var QueryEndAtConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryEndAtConstraint, _super);
- function QueryEndAtConstraint(_value, _key) {
- var _this = _super.call(this) || this;
- _this._value = _value;
- _this._key = _key;
- return _this;
- }
- QueryEndAtConstraint.prototype._apply = function (query) {
- validateFirebaseDataArg('endAt', this._value, query._path, true);
- var newParams = queryParamsEndAt(query._queryParams, this._value, this._key);
- validateLimit(newParams);
- validateQueryEndpoints(newParams);
- if (query._queryParams.hasEnd()) {
- throw new Error('endAt: Starting point was already set (by another call to endAt, ' +
- 'endBefore or equalTo).');
- }
- return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
- };
- return QueryEndAtConstraint;
- }(QueryConstraint));
- /**
- * Creates a `QueryConstraint` with the specified ending point.
- *
- * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
- * allows you to choose arbitrary starting and ending points for your queries.
- *
- * The ending point is inclusive, so children with exactly the specified value
- * will be included in the query. The optional key argument can be used to
- * further limit the range of the query. If it is specified, then children that
- * have exactly the specified value must also have a key name less than or equal
- * to the specified key.
- *
- * You can read more about `endAt()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
- *
- * @param value - The value to end at. The argument type depends on which
- * `orderBy*()` function was used in this query. Specify a value that matches
- * the `orderBy*()` type. When used in combination with `orderByKey()`, the
- * value must be a string.
- * @param key - The child key to end at, among the children with the previously
- * specified priority. This argument is only allowed if ordering by child,
- * value, or priority.
- */
- function endAt(value, key) {
- validateKey('endAt', 'key', key, true);
- return new QueryEndAtConstraint(value, key);
- }
- var QueryEndBeforeConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryEndBeforeConstraint, _super);
- function QueryEndBeforeConstraint(_value, _key) {
- var _this = _super.call(this) || this;
- _this._value = _value;
- _this._key = _key;
- return _this;
- }
- QueryEndBeforeConstraint.prototype._apply = function (query) {
- validateFirebaseDataArg('endBefore', this._value, query._path, false);
- var newParams = queryParamsEndBefore(query._queryParams, this._value, this._key);
- validateLimit(newParams);
- validateQueryEndpoints(newParams);
- if (query._queryParams.hasEnd()) {
- throw new Error('endBefore: Starting point was already set (by another call to endAt, ' +
- 'endBefore or equalTo).');
- }
- return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
- };
- return QueryEndBeforeConstraint;
- }(QueryConstraint));
- /**
- * Creates a `QueryConstraint` with the specified ending point (exclusive).
- *
- * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
- * allows you to choose arbitrary starting and ending points for your queries.
- *
- * The ending point is exclusive. If only a value is provided, children
- * with a value less than the specified value will be included in the query.
- * If a key is specified, then children must have a value less than or equal
- * to the specified value and a key name less than the specified key.
- *
- * @param value - The value to end before. The argument type depends on which
- * `orderBy*()` function was used in this query. Specify a value that matches
- * the `orderBy*()` type. When used in combination with `orderByKey()`, the
- * value must be a string.
- * @param key - The child key to end before, among the children with the
- * previously specified priority. This argument is only allowed if ordering by
- * child, value, or priority.
- */
- function endBefore(value, key) {
- validateKey('endBefore', 'key', key, true);
- return new QueryEndBeforeConstraint(value, key);
- }
- var QueryStartAtConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryStartAtConstraint, _super);
- function QueryStartAtConstraint(_value, _key) {
- var _this = _super.call(this) || this;
- _this._value = _value;
- _this._key = _key;
- return _this;
- }
- QueryStartAtConstraint.prototype._apply = function (query) {
- validateFirebaseDataArg('startAt', this._value, query._path, true);
- var newParams = queryParamsStartAt(query._queryParams, this._value, this._key);
- validateLimit(newParams);
- validateQueryEndpoints(newParams);
- if (query._queryParams.hasStart()) {
- throw new Error('startAt: Starting point was already set (by another call to startAt, ' +
- 'startBefore or equalTo).');
- }
- return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
- };
- return QueryStartAtConstraint;
- }(QueryConstraint));
- /**
- * Creates a `QueryConstraint` with the specified starting point.
- *
- * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
- * allows you to choose arbitrary starting and ending points for your queries.
- *
- * The starting point is inclusive, so children with exactly the specified value
- * will be included in the query. The optional key argument can be used to
- * further limit the range of the query. If it is specified, then children that
- * have exactly the specified value must also have a key name greater than or
- * equal to the specified key.
- *
- * You can read more about `startAt()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
- *
- * @param value - The value to start at. The argument type depends on which
- * `orderBy*()` function was used in this query. Specify a value that matches
- * the `orderBy*()` type. When used in combination with `orderByKey()`, the
- * value must be a string.
- * @param key - The child key to start at. This argument is only allowed if
- * ordering by child, value, or priority.
- */
- function startAt(value, key) {
- if (value === void 0) { value = null; }
- validateKey('startAt', 'key', key, true);
- return new QueryStartAtConstraint(value, key);
- }
- var QueryStartAfterConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryStartAfterConstraint, _super);
- function QueryStartAfterConstraint(_value, _key) {
- var _this = _super.call(this) || this;
- _this._value = _value;
- _this._key = _key;
- return _this;
- }
- QueryStartAfterConstraint.prototype._apply = function (query) {
- validateFirebaseDataArg('startAfter', this._value, query._path, false);
- var newParams = queryParamsStartAfter(query._queryParams, this._value, this._key);
- validateLimit(newParams);
- validateQueryEndpoints(newParams);
- if (query._queryParams.hasStart()) {
- throw new Error('startAfter: Starting point was already set (by another call to startAt, ' +
- 'startAfter, or equalTo).');
- }
- return new QueryImpl(query._repo, query._path, newParams, query._orderByCalled);
- };
- return QueryStartAfterConstraint;
- }(QueryConstraint));
- /**
- * Creates a `QueryConstraint` with the specified starting point (exclusive).
- *
- * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
- * allows you to choose arbitrary starting and ending points for your queries.
- *
- * The starting point is exclusive. If only a value is provided, children
- * with a value greater than the specified value will be included in the query.
- * If a key is specified, then children must have a value greater than or equal
- * to the specified value and a a key name greater than the specified key.
- *
- * @param value - The value to start after. The argument type depends on which
- * `orderBy*()` function was used in this query. Specify a value that matches
- * the `orderBy*()` type. When used in combination with `orderByKey()`, the
- * value must be a string.
- * @param key - The child key to start after. This argument is only allowed if
- * ordering by child, value, or priority.
- */
- function startAfter(value, key) {
- validateKey('startAfter', 'key', key, true);
- return new QueryStartAfterConstraint(value, key);
- }
- var QueryLimitToFirstConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryLimitToFirstConstraint, _super);
- function QueryLimitToFirstConstraint(_limit) {
- var _this = _super.call(this) || this;
- _this._limit = _limit;
- return _this;
- }
- QueryLimitToFirstConstraint.prototype._apply = function (query) {
- if (query._queryParams.hasLimit()) {
- throw new Error('limitToFirst: Limit was already set (by another call to limitToFirst ' +
- 'or limitToLast).');
- }
- return new QueryImpl(query._repo, query._path, queryParamsLimitToFirst(query._queryParams, this._limit), query._orderByCalled);
- };
- return QueryLimitToFirstConstraint;
- }(QueryConstraint));
- /**
- * Creates a new `QueryConstraint` that if limited to the first specific number
- * of children.
- *
- * The `limitToFirst()` method is used to set a maximum number of children to be
- * synced for a given callback. If we set a limit of 100, we will initially only
- * receive up to 100 `child_added` events. If we have fewer than 100 messages
- * stored in our Database, a `child_added` event will fire for each message.
- * However, if we have over 100 messages, we will only receive a `child_added`
- * event for the first 100 ordered messages. As items change, we will receive
- * `child_removed` events for each item that drops out of the active list so
- * that the total number stays at 100.
- *
- * You can read more about `limitToFirst()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
- *
- * @param limit - The maximum number of nodes to include in this query.
- */
- function limitToFirst(limit) {
- if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
- throw new Error('limitToFirst: First argument must be a positive integer.');
- }
- return new QueryLimitToFirstConstraint(limit);
- }
- var QueryLimitToLastConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryLimitToLastConstraint, _super);
- function QueryLimitToLastConstraint(_limit) {
- var _this = _super.call(this) || this;
- _this._limit = _limit;
- return _this;
- }
- QueryLimitToLastConstraint.prototype._apply = function (query) {
- if (query._queryParams.hasLimit()) {
- throw new Error('limitToLast: Limit was already set (by another call to limitToFirst ' +
- 'or limitToLast).');
- }
- return new QueryImpl(query._repo, query._path, queryParamsLimitToLast(query._queryParams, this._limit), query._orderByCalled);
- };
- return QueryLimitToLastConstraint;
- }(QueryConstraint));
- /**
- * Creates a new `QueryConstraint` that is limited to return only the last
- * specified number of children.
- *
- * The `limitToLast()` method is used to set a maximum number of children to be
- * synced for a given callback. If we set a limit of 100, we will initially only
- * receive up to 100 `child_added` events. If we have fewer than 100 messages
- * stored in our Database, a `child_added` event will fire for each message.
- * However, if we have over 100 messages, we will only receive a `child_added`
- * event for the last 100 ordered messages. As items change, we will receive
- * `child_removed` events for each item that drops out of the active list so
- * that the total number stays at 100.
- *
- * You can read more about `limitToLast()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
- *
- * @param limit - The maximum number of nodes to include in this query.
- */
- function limitToLast(limit) {
- if (typeof limit !== 'number' || Math.floor(limit) !== limit || limit <= 0) {
- throw new Error('limitToLast: First argument must be a positive integer.');
- }
- return new QueryLimitToLastConstraint(limit);
- }
- var QueryOrderByChildConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryOrderByChildConstraint, _super);
- function QueryOrderByChildConstraint(_path) {
- var _this = _super.call(this) || this;
- _this._path = _path;
- return _this;
- }
- QueryOrderByChildConstraint.prototype._apply = function (query) {
- validateNoPreviousOrderByCall(query, 'orderByChild');
- var parsedPath = new Path(this._path);
- if (pathIsEmpty(parsedPath)) {
- throw new Error('orderByChild: cannot pass in empty path. Use orderByValue() instead.');
- }
- var index = new PathIndex(parsedPath);
- var newParams = queryParamsOrderBy(query._queryParams, index);
- validateQueryEndpoints(newParams);
- return new QueryImpl(query._repo, query._path, newParams,
- /*orderByCalled=*/ true);
- };
- return QueryOrderByChildConstraint;
- }(QueryConstraint));
- /**
- * Creates a new `QueryConstraint` that orders by the specified child key.
- *
- * Queries can only order by one key at a time. Calling `orderByChild()`
- * multiple times on the same query is an error.
- *
- * Firebase queries allow you to order your data by any child key on the fly.
- * However, if you know in advance what your indexes will be, you can define
- * them via the .indexOn rule in your Security Rules for better performance. See
- * the{@link https://firebase.google.com/docs/database/security/indexing-data}
- * rule for more information.
- *
- * You can read more about `orderByChild()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}.
- *
- * @param path - The path to order by.
- */
- function orderByChild(path) {
- if (path === '$key') {
- throw new Error('orderByChild: "$key" is invalid. Use orderByKey() instead.');
- }
- else if (path === '$priority') {
- throw new Error('orderByChild: "$priority" is invalid. Use orderByPriority() instead.');
- }
- else if (path === '$value') {
- throw new Error('orderByChild: "$value" is invalid. Use orderByValue() instead.');
- }
- validatePathString('orderByChild', 'path', path, false);
- return new QueryOrderByChildConstraint(path);
- }
- var QueryOrderByKeyConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryOrderByKeyConstraint, _super);
- function QueryOrderByKeyConstraint() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- QueryOrderByKeyConstraint.prototype._apply = function (query) {
- validateNoPreviousOrderByCall(query, 'orderByKey');
- var newParams = queryParamsOrderBy(query._queryParams, KEY_INDEX);
- validateQueryEndpoints(newParams);
- return new QueryImpl(query._repo, query._path, newParams,
- /*orderByCalled=*/ true);
- };
- return QueryOrderByKeyConstraint;
- }(QueryConstraint));
- /**
- * Creates a new `QueryConstraint` that orders by the key.
- *
- * Sorts the results of a query by their (ascending) key values.
- *
- * You can read more about `orderByKey()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}.
- */
- function orderByKey() {
- return new QueryOrderByKeyConstraint();
- }
- var QueryOrderByPriorityConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryOrderByPriorityConstraint, _super);
- function QueryOrderByPriorityConstraint() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- QueryOrderByPriorityConstraint.prototype._apply = function (query) {
- validateNoPreviousOrderByCall(query, 'orderByPriority');
- var newParams = queryParamsOrderBy(query._queryParams, PRIORITY_INDEX);
- validateQueryEndpoints(newParams);
- return new QueryImpl(query._repo, query._path, newParams,
- /*orderByCalled=*/ true);
- };
- return QueryOrderByPriorityConstraint;
- }(QueryConstraint));
- /**
- * Creates a new `QueryConstraint` that orders by priority.
- *
- * Applications need not use priority but can order collections by
- * ordinary properties (see
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}
- * for alternatives to priority.
- */
- function orderByPriority() {
- return new QueryOrderByPriorityConstraint();
- }
- var QueryOrderByValueConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryOrderByValueConstraint, _super);
- function QueryOrderByValueConstraint() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- QueryOrderByValueConstraint.prototype._apply = function (query) {
- validateNoPreviousOrderByCall(query, 'orderByValue');
- var newParams = queryParamsOrderBy(query._queryParams, VALUE_INDEX);
- validateQueryEndpoints(newParams);
- return new QueryImpl(query._repo, query._path, newParams,
- /*orderByCalled=*/ true);
- };
- return QueryOrderByValueConstraint;
- }(QueryConstraint));
- /**
- * Creates a new `QueryConstraint` that orders by value.
- *
- * If the children of a query are all scalar values (string, number, or
- * boolean), you can order the results by their (ascending) values.
- *
- * You can read more about `orderByValue()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#sort_data | Sort data}.
- */
- function orderByValue() {
- return new QueryOrderByValueConstraint();
- }
- var QueryEqualToValueConstraint = /** @class */ (function (_super) {
- tslib.__extends(QueryEqualToValueConstraint, _super);
- function QueryEqualToValueConstraint(_value, _key) {
- var _this = _super.call(this) || this;
- _this._value = _value;
- _this._key = _key;
- return _this;
- }
- QueryEqualToValueConstraint.prototype._apply = function (query) {
- validateFirebaseDataArg('equalTo', this._value, query._path, false);
- if (query._queryParams.hasStart()) {
- throw new Error('equalTo: Starting point was already set (by another call to startAt/startAfter or ' +
- 'equalTo).');
- }
- if (query._queryParams.hasEnd()) {
- throw new Error('equalTo: Ending point was already set (by another call to endAt/endBefore or ' +
- 'equalTo).');
- }
- return new QueryEndAtConstraint(this._value, this._key)._apply(new QueryStartAtConstraint(this._value, this._key)._apply(query));
- };
- return QueryEqualToValueConstraint;
- }(QueryConstraint));
- /**
- * Creates a `QueryConstraint` that includes children that match the specified
- * value.
- *
- * Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
- * allows you to choose arbitrary starting and ending points for your queries.
- *
- * The optional key argument can be used to further limit the range of the
- * query. If it is specified, then children that have exactly the specified
- * value must also have exactly the specified key as their key name. This can be
- * used to filter result sets with many matches for the same value.
- *
- * You can read more about `equalTo()` in
- * {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
- *
- * @param value - The value to match for. The argument type depends on which
- * `orderBy*()` function was used in this query. Specify a value that matches
- * the `orderBy*()` type. When used in combination with `orderByKey()`, the
- * value must be a string.
- * @param key - The child key to start at, among the children with the
- * previously specified priority. This argument is only allowed if ordering by
- * child, value, or priority.
- */
- function equalTo(value, key) {
- validateKey('equalTo', 'key', key, true);
- return new QueryEqualToValueConstraint(value, key);
- }
- /**
- * Creates a new immutable instance of `Query` that is extended to also include
- * additional query constraints.
- *
- * @param query - The Query instance to use as a base for the new constraints.
- * @param queryConstraints - The list of `QueryConstraint`s to apply.
- * @throws if any of the provided query constraints cannot be combined with the
- * existing or new constraints.
- */
- function query(query) {
- var e_1, _a;
- var queryConstraints = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- queryConstraints[_i - 1] = arguments[_i];
- }
- var queryImpl = util.getModularInstance(query);
- try {
- for (var queryConstraints_1 = tslib.__values(queryConstraints), queryConstraints_1_1 = queryConstraints_1.next(); !queryConstraints_1_1.done; queryConstraints_1_1 = queryConstraints_1.next()) {
- var constraint = queryConstraints_1_1.value;
- queryImpl = constraint._apply(queryImpl);
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (queryConstraints_1_1 && !queryConstraints_1_1.done && (_a = queryConstraints_1.return)) _a.call(queryConstraints_1);
- }
- finally { if (e_1) throw e_1.error; }
- }
- return queryImpl;
- }
- /**
- * Define reference constructor in various modules
- *
- * We are doing this here to avoid several circular
- * dependency issues
- */
- syncPointSetReferenceConstructor(ReferenceImpl);
- syncTreeSetReferenceConstructor(ReferenceImpl);
-
- /**
- * This variable is also defined in the firebase Node.js Admin SDK. Before
- * modifying this definition, consult the definition in:
- *
- * https://github.com/firebase/firebase-admin-node
- *
- * and make sure the two are consistent.
- */
- var FIREBASE_DATABASE_EMULATOR_HOST_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST';
- /**
- * Creates and caches `Repo` instances.
- */
- var repos = {};
- /**
- * If true, any new `Repo` will be created to use `ReadonlyRestClient` (for testing purposes).
- */
- var useRestClient = false;
- /**
- * Update an existing `Repo` in place to point to a new host/port.
- */
- function repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider) {
- repo.repoInfo_ = new RepoInfo("".concat(host, ":").concat(port),
- /* secure= */ false, repo.repoInfo_.namespace, repo.repoInfo_.webSocketOnly, repo.repoInfo_.nodeAdmin, repo.repoInfo_.persistenceKey, repo.repoInfo_.includeNamespaceInQueryParams);
- if (tokenProvider) {
- repo.authTokenProvider_ = tokenProvider;
- }
- }
- /**
- * This function should only ever be called to CREATE a new database instance.
- * @internal
- */
- function repoManagerDatabaseFromApp(app, authProvider, appCheckProvider, url, nodeAdmin) {
- var dbUrl = url || app.options.databaseURL;
- if (dbUrl === undefined) {
- if (!app.options.projectId) {
- fatal("Can't determine Firebase Database URL. Be sure to include " +
- ' a Project ID when calling firebase.initializeApp().');
- }
- log('Using default host for project ', app.options.projectId);
- dbUrl = "".concat(app.options.projectId, "-default-rtdb.firebaseio.com");
- }
- var parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
- var repoInfo = parsedUrl.repoInfo;
- var isEmulator;
- var dbEmulatorHost = undefined;
- if (typeof process !== 'undefined' && process.env) {
- dbEmulatorHost = process.env[FIREBASE_DATABASE_EMULATOR_HOST_VAR];
- }
- if (dbEmulatorHost) {
- isEmulator = true;
- dbUrl = "http://".concat(dbEmulatorHost, "?ns=").concat(repoInfo.namespace);
- parsedUrl = parseRepoInfo(dbUrl, nodeAdmin);
- repoInfo = parsedUrl.repoInfo;
- }
- else {
- isEmulator = !parsedUrl.repoInfo.secure;
- }
- var authTokenProvider = nodeAdmin && isEmulator
- ? new EmulatorTokenProvider(EmulatorTokenProvider.OWNER)
- : new FirebaseAuthTokenProvider(app.name, app.options, authProvider);
- validateUrl('Invalid Firebase Database URL', parsedUrl);
- if (!pathIsEmpty(parsedUrl.path)) {
- fatal('Database URL must point to the root of a Firebase Database ' +
- '(not including a child path).');
- }
- var repo = repoManagerCreateRepo(repoInfo, app, authTokenProvider, new AppCheckTokenProvider(app.name, appCheckProvider));
- return new Database$1(repo, app);
- }
- /**
- * Remove the repo and make sure it is disconnected.
- *
- */
- function repoManagerDeleteRepo(repo, appName) {
- var appRepos = repos[appName];
- // This should never happen...
- if (!appRepos || appRepos[repo.key] !== repo) {
- fatal("Database ".concat(appName, "(").concat(repo.repoInfo_, ") has already been deleted."));
- }
- repoInterrupt(repo);
- delete appRepos[repo.key];
- }
- /**
- * Ensures a repo doesn't already exist and then creates one using the
- * provided app.
- *
- * @param repoInfo - The metadata about the Repo
- * @returns The Repo object for the specified server / repoName.
- */
- function repoManagerCreateRepo(repoInfo, app, authTokenProvider, appCheckProvider) {
- var appRepos = repos[app.name];
- if (!appRepos) {
- appRepos = {};
- repos[app.name] = appRepos;
- }
- var repo = appRepos[repoInfo.toURLString()];
- if (repo) {
- fatal('Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.');
- }
- repo = new Repo(repoInfo, useRestClient, authTokenProvider, appCheckProvider);
- appRepos[repoInfo.toURLString()] = repo;
- return repo;
- }
- /**
- * Forces us to use ReadonlyRestClient instead of PersistentConnection for new Repos.
- */
- function repoManagerForceRestClient(forceRestClient) {
- useRestClient = forceRestClient;
- }
- /**
- * Class representing a Firebase Realtime Database.
- */
- var Database$1 = /** @class */ (function () {
- /** @hideconstructor */
- function Database(_repoInternal,
- /** The {@link @firebase/app#FirebaseApp} associated with this Realtime Database instance. */
- app) {
- this._repoInternal = _repoInternal;
- this.app = app;
- /** Represents a `Database` instance. */
- this['type'] = 'database';
- /** Track if the instance has been used (root or repo accessed) */
- this._instanceStarted = false;
- }
- Object.defineProperty(Database.prototype, "_repo", {
- get: function () {
- if (!this._instanceStarted) {
- repoStart(this._repoInternal, this.app.options.appId, this.app.options['databaseAuthVariableOverride']);
- this._instanceStarted = true;
- }
- return this._repoInternal;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Database.prototype, "_root", {
- get: function () {
- if (!this._rootInternal) {
- this._rootInternal = new ReferenceImpl(this._repo, newEmptyPath());
- }
- return this._rootInternal;
- },
- enumerable: false,
- configurable: true
- });
- Database.prototype._delete = function () {
- if (this._rootInternal !== null) {
- repoManagerDeleteRepo(this._repo, this.app.name);
- this._repoInternal = null;
- this._rootInternal = null;
- }
- return Promise.resolve();
- };
- Database.prototype._checkNotDeleted = function (apiName) {
- if (this._rootInternal === null) {
- fatal('Cannot call ' + apiName + ' on a deleted database.');
- }
- };
- return Database;
- }());
- function checkTransportInit() {
- if (TransportManager.IS_TRANSPORT_INITIALIZED) {
- warn$1('Transport has already been initialized. Please call this function before calling ref or setting up a listener');
- }
- }
- /**
- * Force the use of websockets instead of longPolling.
- */
- function forceWebSockets() {
- checkTransportInit();
- BrowserPollConnection.forceDisallow();
- }
- /**
- * Force the use of longPolling instead of websockets. This will be ignored if websocket protocol is used in databaseURL.
- */
- function forceLongPolling() {
- checkTransportInit();
- WebSocketConnection.forceDisallow();
- BrowserPollConnection.forceAllow();
- }
- /**
- * Modify the provided instance to communicate with the Realtime Database
- * emulator.
- *
- * <p>Note: This method must be called before performing any other operation.
- *
- * @param db - The instance to modify.
- * @param host - The emulator host (ex: localhost)
- * @param port - The emulator port (ex: 8080)
- * @param options.mockUserToken - the mock auth token to use for unit testing Security Rules
- */
- function connectDatabaseEmulator(db, host, port, options) {
- if (options === void 0) { options = {}; }
- db = util.getModularInstance(db);
- db._checkNotDeleted('useEmulator');
- if (db._instanceStarted) {
- fatal('Cannot call useEmulator() after instance has already been initialized.');
- }
- var repo = db._repoInternal;
- var tokenProvider = undefined;
- if (repo.repoInfo_.nodeAdmin) {
- if (options.mockUserToken) {
- fatal('mockUserToken is not supported by the Admin SDK. For client access with mock users, please use the "firebase" package instead of "firebase-admin".');
- }
- tokenProvider = new EmulatorTokenProvider(EmulatorTokenProvider.OWNER);
- }
- else if (options.mockUserToken) {
- var token = typeof options.mockUserToken === 'string'
- ? options.mockUserToken
- : util.createMockUserToken(options.mockUserToken, db.app.options.projectId);
- tokenProvider = new EmulatorTokenProvider(token);
- }
- // Modify the repo to apply emulator settings
- repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
- }
- /**
- * Disconnects from the server (all Database operations will be completed
- * offline).
- *
- * The client automatically maintains a persistent connection to the Database
- * server, which will remain active indefinitely and reconnect when
- * disconnected. However, the `goOffline()` and `goOnline()` methods may be used
- * to control the client connection in cases where a persistent connection is
- * undesirable.
- *
- * While offline, the client will no longer receive data updates from the
- * Database. However, all Database operations performed locally will continue to
- * immediately fire events, allowing your application to continue behaving
- * normally. Additionally, each operation performed locally will automatically
- * be queued and retried upon reconnection to the Database server.
- *
- * To reconnect to the Database and begin receiving remote events, see
- * `goOnline()`.
- *
- * @param db - The instance to disconnect.
- */
- function goOffline(db) {
- db = util.getModularInstance(db);
- db._checkNotDeleted('goOffline');
- repoInterrupt(db._repo);
- }
- /**
- * Reconnects to the server and synchronizes the offline Database state
- * with the server state.
- *
- * This method should be used after disabling the active connection with
- * `goOffline()`. Once reconnected, the client will transmit the proper data
- * and fire the appropriate events so that your client "catches up"
- * automatically.
- *
- * @param db - The instance to reconnect.
- */
- function goOnline(db) {
- db = util.getModularInstance(db);
- db._checkNotDeleted('goOnline');
- repoResume(db._repo);
- }
- function enableLogging(logger, persistent) {
- enableLogging$1(logger, persistent);
- }
-
- /**
- * @license
- * Copyright 2020 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var SERVER_TIMESTAMP = {
- '.sv': 'timestamp'
- };
- /**
- * Returns a placeholder value for auto-populating the current timestamp (time
- * since the Unix epoch, in milliseconds) as determined by the Firebase
- * servers.
- */
- function serverTimestamp() {
- return SERVER_TIMESTAMP;
- }
- /**
- * Returns a placeholder value that can be used to atomically increment the
- * current database value by the provided delta.
- *
- * @param delta - the amount to modify the current value atomically.
- * @returns A placeholder value for modifying data atomically server-side.
- */
- function increment(delta) {
- return {
- '.sv': {
- 'increment': delta
- }
- };
- }
-
- /**
- * @license
- * Copyright 2020 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * A type for the resolve value of {@link runTransaction}.
- */
- var TransactionResult$1 = /** @class */ (function () {
- /** @hideconstructor */
- function TransactionResult(
- /** Whether the transaction was successfully committed. */
- committed,
- /** The resulting data snapshot. */
- snapshot) {
- this.committed = committed;
- this.snapshot = snapshot;
- }
- /** Returns a JSON-serializable representation of this object. */
- TransactionResult.prototype.toJSON = function () {
- return { committed: this.committed, snapshot: this.snapshot.toJSON() };
- };
- return TransactionResult;
- }());
- /**
- * Atomically modifies the data at this location.
- *
- * Atomically modify the data at this location. Unlike a normal `set()`, which
- * just overwrites the data regardless of its previous value, `runTransaction()` is
- * used to modify the existing value to a new value, ensuring there are no
- * conflicts with other clients writing to the same location at the same time.
- *
- * To accomplish this, you pass `runTransaction()` an update function which is
- * used to transform the current value into a new value. If another client
- * writes to the location before your new value is successfully written, your
- * update function will be called again with the new current value, and the
- * write will be retried. This will happen repeatedly until your write succeeds
- * without conflict or you abort the transaction by not returning a value from
- * your update function.
- *
- * Note: Modifying data with `set()` will cancel any pending transactions at
- * that location, so extreme care should be taken if mixing `set()` and
- * `runTransaction()` to update the same data.
- *
- * Note: When using transactions with Security and Firebase Rules in place, be
- * aware that a client needs `.read` access in addition to `.write` access in
- * order to perform a transaction. This is because the client-side nature of
- * transactions requires the client to read the data in order to transactionally
- * update it.
- *
- * @param ref - The location to atomically modify.
- * @param transactionUpdate - A developer-supplied function which will be passed
- * the current data stored at this location (as a JavaScript object). The
- * function should return the new value it would like written (as a JavaScript
- * object). If `undefined` is returned (i.e. you return with no arguments) the
- * transaction will be aborted and the data at this location will not be
- * modified.
- * @param options - An options object to configure transactions.
- * @returns A `Promise` that can optionally be used instead of the `onComplete`
- * callback to handle success and failure.
- */
- function runTransaction(ref,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- transactionUpdate, options) {
- var _a;
- ref = util.getModularInstance(ref);
- validateWritablePath('Reference.transaction', ref._path);
- if (ref.key === '.length' || ref.key === '.keys') {
- throw ('Reference.transaction failed: ' + ref.key + ' is a read-only object.');
- }
- var applyLocally = (_a = options === null || options === void 0 ? void 0 : options.applyLocally) !== null && _a !== void 0 ? _a : true;
- var deferred = new util.Deferred();
- var promiseComplete = function (error, committed, node) {
- var dataSnapshot = null;
- if (error) {
- deferred.reject(error);
- }
- else {
- dataSnapshot = new DataSnapshot$1(node, new ReferenceImpl(ref._repo, ref._path), PRIORITY_INDEX);
- deferred.resolve(new TransactionResult$1(committed, dataSnapshot));
- }
- };
- // Add a watch to make sure we get server updates.
- var unwatcher = onValue(ref, function () { });
- repoStartTransaction(ref._repo, ref._path, transactionUpdate, promiseComplete, unwatcher, applyLocally);
- return deferred.promise;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- PersistentConnection.prototype.simpleListen = function (pathString, onComplete) {
- this.sendRequest('q', { p: pathString }, onComplete);
- };
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- PersistentConnection.prototype.echo = function (data, onEcho) {
- this.sendRequest('echo', { d: data }, onEcho);
- };
- /**
- * @internal
- */
- var hijackHash = function (newHash) {
- var oldPut = PersistentConnection.prototype.put;
- PersistentConnection.prototype.put = function (pathString, data, onComplete, hash) {
- if (hash !== undefined) {
- hash = newHash();
- }
- oldPut.call(this, pathString, data, onComplete, hash);
- };
- return function () {
- PersistentConnection.prototype.put = oldPut;
- };
- };
- /**
- * Forces the RepoManager to create Repos that use ReadonlyRestClient instead of PersistentConnection.
- * @internal
- */
- var forceRestClient = function (forceRestClient) {
- repoManagerForceRestClient(forceRestClient);
- };
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- setWebSocketImpl(Websocket__default["default"].Client);
-
- index_standalone.DataSnapshot = DataSnapshot$1;
- index_standalone.Database = Database$1;
- var OnDisconnect_1 = index_standalone.OnDisconnect = OnDisconnect$1;
- index_standalone.QueryConstraint = QueryConstraint;
- index_standalone.TransactionResult = TransactionResult$1;
- var _QueryImpl = index_standalone._QueryImpl = QueryImpl;
- var _QueryParams = index_standalone._QueryParams = QueryParams;
- var _ReferenceImpl = index_standalone._ReferenceImpl = ReferenceImpl;
- index_standalone._TEST_ACCESS_forceRestClient = forceRestClient;
- index_standalone._TEST_ACCESS_hijackHash = hijackHash;
- var _repoManagerDatabaseFromApp = index_standalone._repoManagerDatabaseFromApp = repoManagerDatabaseFromApp;
- var _setSDKVersion = index_standalone._setSDKVersion = setSDKVersion;
- var _validatePathString = index_standalone._validatePathString = validatePathString;
- var _validateWritablePath = index_standalone._validateWritablePath = validateWritablePath;
- var child_1 = index_standalone.child = child;
- var connectDatabaseEmulator_1 = index_standalone.connectDatabaseEmulator = connectDatabaseEmulator;
- var enableLogging_1 = index_standalone.enableLogging = enableLogging;
- var endAt_1 = index_standalone.endAt = endAt;
- var endBefore_1 = index_standalone.endBefore = endBefore;
- var equalTo_1 = index_standalone.equalTo = equalTo;
- var forceLongPolling_1 = index_standalone.forceLongPolling = forceLongPolling;
- var forceWebSockets_1 = index_standalone.forceWebSockets = forceWebSockets;
- var get_1 = index_standalone.get = get;
- var goOffline_1 = index_standalone.goOffline = goOffline;
- var goOnline_1 = index_standalone.goOnline = goOnline;
- var increment_1 = index_standalone.increment = increment;
- var limitToFirst_1 = index_standalone.limitToFirst = limitToFirst;
- var limitToLast_1 = index_standalone.limitToLast = limitToLast;
- var off_1 = index_standalone.off = off;
- var onChildAdded_1 = index_standalone.onChildAdded = onChildAdded;
- var onChildChanged_1 = index_standalone.onChildChanged = onChildChanged;
- var onChildMoved_1 = index_standalone.onChildMoved = onChildMoved;
- var onChildRemoved_1 = index_standalone.onChildRemoved = onChildRemoved;
- index_standalone.onDisconnect = onDisconnect;
- var onValue_1 = index_standalone.onValue = onValue;
- var orderByChild_1 = index_standalone.orderByChild = orderByChild;
- var orderByKey_1 = index_standalone.orderByKey = orderByKey;
- var orderByPriority_1 = index_standalone.orderByPriority = orderByPriority;
- var orderByValue_1 = index_standalone.orderByValue = orderByValue;
- var push_1 = index_standalone.push = push;
- var query_1 = index_standalone.query = query;
- var ref_1 = index_standalone.ref = ref;
- var refFromURL_1 = index_standalone.refFromURL = refFromURL;
- var remove_1 = index_standalone.remove = remove;
- var runTransaction_1 = index_standalone.runTransaction = runTransaction;
- var serverTimestamp_1 = index_standalone.serverTimestamp = serverTimestamp;
- var set_1 = index_standalone.set = set;
- var setPriority_1 = index_standalone.setPriority = setPriority;
- var setWithPriority_1 = index_standalone.setWithPriority = setWithPriority;
- var startAfter_1 = index_standalone.startAfter = startAfter;
- var startAt_1 = index_standalone.startAt = startAt;
- var update_1 = index_standalone.update = update;
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var logClient = new require$$3.Logger('@firebase/database-compat');
- var warn = function (msg) {
- var message = 'FIREBASE WARNING: ' + msg;
- logClient.warn(message);
- };
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var validateBoolean = function (fnName, argumentName, bool, optional) {
- if (optional && bool === undefined) {
- return;
- }
- if (typeof bool !== 'boolean') {
- throw new Error(require$$1$3.errorPrefix(fnName, argumentName) + 'must be a boolean.');
- }
- };
- var validateEventType = function (fnName, eventType, optional) {
- if (optional && eventType === undefined) {
- return;
- }
- switch (eventType) {
- case 'value':
- case 'child_added':
- case 'child_removed':
- case 'child_changed':
- case 'child_moved':
- break;
- default:
- throw new Error(require$$1$3.errorPrefix(fnName, 'eventType') +
- 'must be a valid event type = "value", "child_added", "child_removed", ' +
- '"child_changed", or "child_moved".');
- }
- };
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var OnDisconnect = /** @class */ (function () {
- function OnDisconnect(_delegate) {
- this._delegate = _delegate;
- }
- OnDisconnect.prototype.cancel = function (onComplete) {
- require$$1$3.validateArgCount('OnDisconnect.cancel', 0, 1, arguments.length);
- require$$1$3.validateCallback('OnDisconnect.cancel', 'onComplete', onComplete, true);
- var result = this._delegate.cancel();
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- OnDisconnect.prototype.remove = function (onComplete) {
- require$$1$3.validateArgCount('OnDisconnect.remove', 0, 1, arguments.length);
- require$$1$3.validateCallback('OnDisconnect.remove', 'onComplete', onComplete, true);
- var result = this._delegate.remove();
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- OnDisconnect.prototype.set = function (value, onComplete) {
- require$$1$3.validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
- require$$1$3.validateCallback('OnDisconnect.set', 'onComplete', onComplete, true);
- var result = this._delegate.set(value);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- OnDisconnect.prototype.setWithPriority = function (value, priority, onComplete) {
- require$$1$3.validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
- require$$1$3.validateCallback('OnDisconnect.setWithPriority', 'onComplete', onComplete, true);
- var result = this._delegate.setWithPriority(value, priority);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- OnDisconnect.prototype.update = function (objectToMerge, onComplete) {
- require$$1$3.validateArgCount('OnDisconnect.update', 1, 2, arguments.length);
- if (Array.isArray(objectToMerge)) {
- var newObjectToMerge = {};
- for (var i = 0; i < objectToMerge.length; ++i) {
- newObjectToMerge['' + i] = objectToMerge[i];
- }
- objectToMerge = newObjectToMerge;
- warn('Passing an Array to firebase.database.onDisconnect().update() is deprecated. Use set() if you want to overwrite the ' +
- 'existing data, or an Object with integer keys if you really do want to only update some of the children.');
- }
- require$$1$3.validateCallback('OnDisconnect.update', 'onComplete', onComplete, true);
- var result = this._delegate.update(objectToMerge);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- return OnDisconnect;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var TransactionResult = /** @class */ (function () {
- /**
- * A type for the resolve value of Firebase.transaction.
- */
- function TransactionResult(committed, snapshot) {
- this.committed = committed;
- this.snapshot = snapshot;
- }
- // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
- // for end-users
- TransactionResult.prototype.toJSON = function () {
- require$$1$3.validateArgCount('TransactionResult.toJSON', 0, 1, arguments.length);
- return { committed: this.committed, snapshot: this.snapshot.toJSON() };
- };
- return TransactionResult;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Class representing a firebase data snapshot. It wraps a SnapshotNode and
- * surfaces the public methods (val, forEach, etc.) we want to expose.
- */
- var DataSnapshot = /** @class */ (function () {
- function DataSnapshot(_database, _delegate) {
- this._database = _database;
- this._delegate = _delegate;
- }
- /**
- * Retrieves the snapshot contents as JSON. Returns null if the snapshot is
- * empty.
- *
- * @returns JSON representation of the DataSnapshot contents, or null if empty.
- */
- DataSnapshot.prototype.val = function () {
- require$$1$3.validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
- return this._delegate.val();
- };
- /**
- * Returns the snapshot contents as JSON, including priorities of node. Suitable for exporting
- * the entire node contents.
- * @returns JSON representation of the DataSnapshot contents, or null if empty.
- */
- DataSnapshot.prototype.exportVal = function () {
- require$$1$3.validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
- return this._delegate.exportVal();
- };
- // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
- // for end-users
- DataSnapshot.prototype.toJSON = function () {
- // Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
- require$$1$3.validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
- return this._delegate.toJSON();
- };
- /**
- * Returns whether the snapshot contains a non-null value.
- *
- * @returns Whether the snapshot contains a non-null value, or is empty.
- */
- DataSnapshot.prototype.exists = function () {
- require$$1$3.validateArgCount('DataSnapshot.exists', 0, 0, arguments.length);
- return this._delegate.exists();
- };
- /**
- * Returns a DataSnapshot of the specified child node's contents.
- *
- * @param path - Path to a child.
- * @returns DataSnapshot for child node.
- */
- DataSnapshot.prototype.child = function (path) {
- require$$1$3.validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
- // Ensure the childPath is a string (can be a number)
- path = String(path);
- _validatePathString('DataSnapshot.child', 'path', path, false);
- return new DataSnapshot(this._database, this._delegate.child(path));
- };
- /**
- * Returns whether the snapshot contains a child at the specified path.
- *
- * @param path - Path to a child.
- * @returns Whether the child exists.
- */
- DataSnapshot.prototype.hasChild = function (path) {
- require$$1$3.validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
- _validatePathString('DataSnapshot.hasChild', 'path', path, false);
- return this._delegate.hasChild(path);
- };
- /**
- * Returns the priority of the object, or null if no priority was set.
- *
- * @returns The priority.
- */
- DataSnapshot.prototype.getPriority = function () {
- require$$1$3.validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
- return this._delegate.priority;
- };
- /**
- * Iterates through child nodes and calls the specified action for each one.
- *
- * @param action - Callback function to be called
- * for each child.
- * @returns True if forEach was canceled by action returning true for
- * one of the child nodes.
- */
- DataSnapshot.prototype.forEach = function (action) {
- var _this = this;
- require$$1$3.validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
- require$$1$3.validateCallback('DataSnapshot.forEach', 'action', action, false);
- return this._delegate.forEach(function (expDataSnapshot) {
- return action(new DataSnapshot(_this._database, expDataSnapshot));
- });
- };
- /**
- * Returns whether this DataSnapshot has children.
- * @returns True if the DataSnapshot contains 1 or more child nodes.
- */
- DataSnapshot.prototype.hasChildren = function () {
- require$$1$3.validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
- return this._delegate.hasChildren();
- };
- Object.defineProperty(DataSnapshot.prototype, "key", {
- get: function () {
- return this._delegate.key;
- },
- enumerable: false,
- configurable: true
- });
- /**
- * Returns the number of children for this DataSnapshot.
- * @returns The number of children that this DataSnapshot contains.
- */
- DataSnapshot.prototype.numChildren = function () {
- require$$1$3.validateArgCount('DataSnapshot.numChildren', 0, 0, arguments.length);
- return this._delegate.size;
- };
- /**
- * @returns The Firebase reference for the location this snapshot's data came
- * from.
- */
- DataSnapshot.prototype.getRef = function () {
- require$$1$3.validateArgCount('DataSnapshot.ref', 0, 0, arguments.length);
- return new Reference(this._database, this._delegate.ref);
- };
- Object.defineProperty(DataSnapshot.prototype, "ref", {
- get: function () {
- return this.getRef();
- },
- enumerable: false,
- configurable: true
- });
- return DataSnapshot;
- }());
- /**
- * A Query represents a filter to be applied to a firebase location. This object purely represents the
- * query expression (and exposes our public API to build the query). The actual query logic is in ViewBase.js.
- *
- * Since every Firebase reference is a query, Firebase inherits from this object.
- */
- var Query = /** @class */ (function () {
- function Query(database, _delegate) {
- this.database = database;
- this._delegate = _delegate;
- }
- Query.prototype.on = function (eventType, callback, cancelCallbackOrContext, context) {
- var _this = this;
- var _a;
- require$$1$3.validateArgCount('Query.on', 2, 4, arguments.length);
- require$$1$3.validateCallback('Query.on', 'callback', callback, false);
- var ret = Query.getCancelAndContextArgs_('Query.on', cancelCallbackOrContext, context);
- var valueCallback = function (expSnapshot, previousChildName) {
- callback.call(ret.context, new DataSnapshot(_this.database, expSnapshot), previousChildName);
- };
- valueCallback.userCallback = callback;
- valueCallback.context = ret.context;
- var cancelCallback = (_a = ret.cancel) === null || _a === void 0 ? void 0 : _a.bind(ret.context);
- switch (eventType) {
- case 'value':
- onValue_1(this._delegate, valueCallback, cancelCallback);
- return callback;
- case 'child_added':
- onChildAdded_1(this._delegate, valueCallback, cancelCallback);
- return callback;
- case 'child_removed':
- onChildRemoved_1(this._delegate, valueCallback, cancelCallback);
- return callback;
- case 'child_changed':
- onChildChanged_1(this._delegate, valueCallback, cancelCallback);
- return callback;
- case 'child_moved':
- onChildMoved_1(this._delegate, valueCallback, cancelCallback);
- return callback;
- default:
- throw new Error(require$$1$3.errorPrefix('Query.on', 'eventType') +
- 'must be a valid event type = "value", "child_added", "child_removed", ' +
- '"child_changed", or "child_moved".');
- }
- };
- Query.prototype.off = function (eventType, callback, context) {
- require$$1$3.validateArgCount('Query.off', 0, 3, arguments.length);
- validateEventType('Query.off', eventType, true);
- require$$1$3.validateCallback('Query.off', 'callback', callback, true);
- require$$1$3.validateContextObject('Query.off', 'context', context, true);
- if (callback) {
- var valueCallback = function () { };
- valueCallback.userCallback = callback;
- valueCallback.context = context;
- off_1(this._delegate, eventType, valueCallback);
- }
- else {
- off_1(this._delegate, eventType);
- }
- };
- /**
- * Get the server-value for this query, or return a cached value if not connected.
- */
- Query.prototype.get = function () {
- var _this = this;
- return get_1(this._delegate).then(function (expSnapshot) {
- return new DataSnapshot(_this.database, expSnapshot);
- });
- };
- /**
- * Attaches a listener, waits for the first event, and then removes the listener
- */
- Query.prototype.once = function (eventType, callback, failureCallbackOrContext, context) {
- var _this = this;
- require$$1$3.validateArgCount('Query.once', 1, 4, arguments.length);
- require$$1$3.validateCallback('Query.once', 'callback', callback, true);
- var ret = Query.getCancelAndContextArgs_('Query.once', failureCallbackOrContext, context);
- var deferred = new require$$1$3.Deferred();
- var valueCallback = function (expSnapshot, previousChildName) {
- var result = new DataSnapshot(_this.database, expSnapshot);
- if (callback) {
- callback.call(ret.context, result, previousChildName);
- }
- deferred.resolve(result);
- };
- valueCallback.userCallback = callback;
- valueCallback.context = ret.context;
- var cancelCallback = function (error) {
- if (ret.cancel) {
- ret.cancel.call(ret.context, error);
- }
- deferred.reject(error);
- };
- switch (eventType) {
- case 'value':
- onValue_1(this._delegate, valueCallback, cancelCallback, {
- onlyOnce: true
- });
- break;
- case 'child_added':
- onChildAdded_1(this._delegate, valueCallback, cancelCallback, {
- onlyOnce: true
- });
- break;
- case 'child_removed':
- onChildRemoved_1(this._delegate, valueCallback, cancelCallback, {
- onlyOnce: true
- });
- break;
- case 'child_changed':
- onChildChanged_1(this._delegate, valueCallback, cancelCallback, {
- onlyOnce: true
- });
- break;
- case 'child_moved':
- onChildMoved_1(this._delegate, valueCallback, cancelCallback, {
- onlyOnce: true
- });
- break;
- default:
- throw new Error(require$$1$3.errorPrefix('Query.once', 'eventType') +
- 'must be a valid event type = "value", "child_added", "child_removed", ' +
- '"child_changed", or "child_moved".');
- }
- return deferred.promise;
- };
- /**
- * Set a limit and anchor it to the start of the window.
- */
- Query.prototype.limitToFirst = function (limit) {
- require$$1$3.validateArgCount('Query.limitToFirst', 1, 1, arguments.length);
- return new Query(this.database, query_1(this._delegate, limitToFirst_1(limit)));
- };
- /**
- * Set a limit and anchor it to the end of the window.
- */
- Query.prototype.limitToLast = function (limit) {
- require$$1$3.validateArgCount('Query.limitToLast', 1, 1, arguments.length);
- return new Query(this.database, query_1(this._delegate, limitToLast_1(limit)));
- };
- /**
- * Given a child path, return a new query ordered by the specified grandchild path.
- */
- Query.prototype.orderByChild = function (path) {
- require$$1$3.validateArgCount('Query.orderByChild', 1, 1, arguments.length);
- return new Query(this.database, query_1(this._delegate, orderByChild_1(path)));
- };
- /**
- * Return a new query ordered by the KeyIndex
- */
- Query.prototype.orderByKey = function () {
- require$$1$3.validateArgCount('Query.orderByKey', 0, 0, arguments.length);
- return new Query(this.database, query_1(this._delegate, orderByKey_1()));
- };
- /**
- * Return a new query ordered by the PriorityIndex
- */
- Query.prototype.orderByPriority = function () {
- require$$1$3.validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
- return new Query(this.database, query_1(this._delegate, orderByPriority_1()));
- };
- /**
- * Return a new query ordered by the ValueIndex
- */
- Query.prototype.orderByValue = function () {
- require$$1$3.validateArgCount('Query.orderByValue', 0, 0, arguments.length);
- return new Query(this.database, query_1(this._delegate, orderByValue_1()));
- };
- Query.prototype.startAt = function (value, name) {
- if (value === void 0) { value = null; }
- require$$1$3.validateArgCount('Query.startAt', 0, 2, arguments.length);
- return new Query(this.database, query_1(this._delegate, startAt_1(value, name)));
- };
- Query.prototype.startAfter = function (value, name) {
- if (value === void 0) { value = null; }
- require$$1$3.validateArgCount('Query.startAfter', 0, 2, arguments.length);
- return new Query(this.database, query_1(this._delegate, startAfter_1(value, name)));
- };
- Query.prototype.endAt = function (value, name) {
- if (value === void 0) { value = null; }
- require$$1$3.validateArgCount('Query.endAt', 0, 2, arguments.length);
- return new Query(this.database, query_1(this._delegate, endAt_1(value, name)));
- };
- Query.prototype.endBefore = function (value, name) {
- if (value === void 0) { value = null; }
- require$$1$3.validateArgCount('Query.endBefore', 0, 2, arguments.length);
- return new Query(this.database, query_1(this._delegate, endBefore_1(value, name)));
- };
- /**
- * Load the selection of children with exactly the specified value, and, optionally,
- * the specified name.
- */
- Query.prototype.equalTo = function (value, name) {
- require$$1$3.validateArgCount('Query.equalTo', 1, 2, arguments.length);
- return new Query(this.database, query_1(this._delegate, equalTo_1(value, name)));
- };
- /**
- * @returns URL for this location.
- */
- Query.prototype.toString = function () {
- require$$1$3.validateArgCount('Query.toString', 0, 0, arguments.length);
- return this._delegate.toString();
- };
- // Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
- // for end-users.
- Query.prototype.toJSON = function () {
- // An optional spacer argument is unnecessary for a string.
- require$$1$3.validateArgCount('Query.toJSON', 0, 1, arguments.length);
- return this._delegate.toJSON();
- };
- /**
- * Return true if this query and the provided query are equivalent; otherwise, return false.
- */
- Query.prototype.isEqual = function (other) {
- require$$1$3.validateArgCount('Query.isEqual', 1, 1, arguments.length);
- if (!(other instanceof Query)) {
- var error = 'Query.isEqual failed: First argument must be an instance of firebase.database.Query.';
- throw new Error(error);
- }
- return this._delegate.isEqual(other._delegate);
- };
- /**
- * Helper used by .on and .once to extract the context and or cancel arguments.
- * @param fnName - The function name (on or once)
- *
- */
- Query.getCancelAndContextArgs_ = function (fnName, cancelOrContext, context) {
- var ret = { cancel: undefined, context: undefined };
- if (cancelOrContext && context) {
- ret.cancel = cancelOrContext;
- require$$1$3.validateCallback(fnName, 'cancel', ret.cancel, true);
- ret.context = context;
- require$$1$3.validateContextObject(fnName, 'context', ret.context, true);
- }
- else if (cancelOrContext) {
- // we have either a cancel callback or a context.
- if (typeof cancelOrContext === 'object' && cancelOrContext !== null) {
- // it's a context!
- ret.context = cancelOrContext;
- }
- else if (typeof cancelOrContext === 'function') {
- ret.cancel = cancelOrContext;
- }
- else {
- throw new Error(require$$1$3.errorPrefix(fnName, 'cancelOrContext') +
- ' must either be a cancel callback or a context object.');
- }
- }
- return ret;
- };
- Object.defineProperty(Query.prototype, "ref", {
- get: function () {
- return new Reference(this.database, new _ReferenceImpl(this._delegate._repo, this._delegate._path));
- },
- enumerable: false,
- configurable: true
- });
- return Query;
- }());
- var Reference = /** @class */ (function (_super) {
- require$$2$3.__extends(Reference, _super);
- /**
- * Call options:
- * new Reference(Repo, Path) or
- * new Reference(url: string, string|RepoManager)
- *
- * Externally - this is the firebase.database.Reference type.
- */
- function Reference(database, _delegate) {
- var _this = _super.call(this, database, new _QueryImpl(_delegate._repo, _delegate._path, new _QueryParams(), false)) || this;
- _this.database = database;
- _this._delegate = _delegate;
- return _this;
- }
- /** @returns {?string} */
- Reference.prototype.getKey = function () {
- require$$1$3.validateArgCount('Reference.key', 0, 0, arguments.length);
- return this._delegate.key;
- };
- Reference.prototype.child = function (pathString) {
- require$$1$3.validateArgCount('Reference.child', 1, 1, arguments.length);
- if (typeof pathString === 'number') {
- pathString = String(pathString);
- }
- return new Reference(this.database, child_1(this._delegate, pathString));
- };
- /** @returns {?Reference} */
- Reference.prototype.getParent = function () {
- require$$1$3.validateArgCount('Reference.parent', 0, 0, arguments.length);
- var parent = this._delegate.parent;
- return parent ? new Reference(this.database, parent) : null;
- };
- /** @returns {!Reference} */
- Reference.prototype.getRoot = function () {
- require$$1$3.validateArgCount('Reference.root', 0, 0, arguments.length);
- return new Reference(this.database, this._delegate.root);
- };
- Reference.prototype.set = function (newVal, onComplete) {
- require$$1$3.validateArgCount('Reference.set', 1, 2, arguments.length);
- require$$1$3.validateCallback('Reference.set', 'onComplete', onComplete, true);
- var result = set_1(this._delegate, newVal);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- Reference.prototype.update = function (values, onComplete) {
- require$$1$3.validateArgCount('Reference.update', 1, 2, arguments.length);
- if (Array.isArray(values)) {
- var newObjectToMerge = {};
- for (var i = 0; i < values.length; ++i) {
- newObjectToMerge['' + i] = values[i];
- }
- values = newObjectToMerge;
- warn('Passing an Array to Firebase.update() is deprecated. ' +
- 'Use set() if you want to overwrite the existing data, or ' +
- 'an Object with integer keys if you really do want to ' +
- 'only update some of the children.');
- }
- _validateWritablePath('Reference.update', this._delegate._path);
- require$$1$3.validateCallback('Reference.update', 'onComplete', onComplete, true);
- var result = update_1(this._delegate, values);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- Reference.prototype.setWithPriority = function (newVal, newPriority, onComplete) {
- require$$1$3.validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
- require$$1$3.validateCallback('Reference.setWithPriority', 'onComplete', onComplete, true);
- var result = setWithPriority_1(this._delegate, newVal, newPriority);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- Reference.prototype.remove = function (onComplete) {
- require$$1$3.validateArgCount('Reference.remove', 0, 1, arguments.length);
- require$$1$3.validateCallback('Reference.remove', 'onComplete', onComplete, true);
- var result = remove_1(this._delegate);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- Reference.prototype.transaction = function (transactionUpdate, onComplete, applyLocally) {
- var _this = this;
- require$$1$3.validateArgCount('Reference.transaction', 1, 3, arguments.length);
- require$$1$3.validateCallback('Reference.transaction', 'transactionUpdate', transactionUpdate, false);
- require$$1$3.validateCallback('Reference.transaction', 'onComplete', onComplete, true);
- validateBoolean('Reference.transaction', 'applyLocally', applyLocally, true);
- var result = runTransaction_1(this._delegate, transactionUpdate, {
- applyLocally: applyLocally
- }).then(function (transactionResult) {
- return new TransactionResult(transactionResult.committed, new DataSnapshot(_this.database, transactionResult.snapshot));
- });
- if (onComplete) {
- result.then(function (transactionResult) {
- return onComplete(null, transactionResult.committed, transactionResult.snapshot);
- }, function (error) { return onComplete(error, false, null); });
- }
- return result;
- };
- Reference.prototype.setPriority = function (priority, onComplete) {
- require$$1$3.validateArgCount('Reference.setPriority', 1, 2, arguments.length);
- require$$1$3.validateCallback('Reference.setPriority', 'onComplete', onComplete, true);
- var result = setPriority_1(this._delegate, priority);
- if (onComplete) {
- result.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- return result;
- };
- Reference.prototype.push = function (value, onComplete) {
- var _this = this;
- require$$1$3.validateArgCount('Reference.push', 0, 2, arguments.length);
- require$$1$3.validateCallback('Reference.push', 'onComplete', onComplete, true);
- var expPromise = push_1(this._delegate, value);
- var promise = expPromise.then(function (expRef) { return new Reference(_this.database, expRef); });
- if (onComplete) {
- promise.then(function () { return onComplete(null); }, function (error) { return onComplete(error); });
- }
- var result = new Reference(this.database, expPromise);
- result.then = promise.then.bind(promise);
- result.catch = promise.catch.bind(promise, undefined);
- return result;
- };
- Reference.prototype.onDisconnect = function () {
- _validateWritablePath('Reference.onDisconnect', this._delegate._path);
- return new OnDisconnect(new OnDisconnect_1(this._delegate._repo, this._delegate._path));
- };
- Object.defineProperty(Reference.prototype, "key", {
- get: function () {
- return this.getKey();
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "parent", {
- get: function () {
- return this.getParent();
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "root", {
- get: function () {
- return this.getRoot();
- },
- enumerable: false,
- configurable: true
- });
- return Reference;
- }(Query));
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Class representing a firebase database.
- */
- var Database = /** @class */ (function () {
- /**
- * The constructor should not be called by users of our public API.
- */
- function Database(_delegate, app) {
- var _this = this;
- this._delegate = _delegate;
- this.app = app;
- this.INTERNAL = {
- delete: function () { return _this._delegate._delete(); },
- forceWebSockets: forceWebSockets_1,
- forceLongPolling: forceLongPolling_1
- };
- }
- /**
- * Modify this instance to communicate with the Realtime Database emulator.
- *
- * <p>Note: This method must be called before performing any other operation.
- *
- * @param host - the emulator host (ex: localhost)
- * @param port - the emulator port (ex: 8080)
- * @param options.mockUserToken - the mock auth token to use for unit testing Security Rules
- */
- Database.prototype.useEmulator = function (host, port, options) {
- if (options === void 0) { options = {}; }
- connectDatabaseEmulator_1(this._delegate, host, port, options);
- };
- Database.prototype.ref = function (path) {
- require$$1$3.validateArgCount('database.ref', 0, 1, arguments.length);
- if (path instanceof Reference) {
- var childRef = refFromURL_1(this._delegate, path.toString());
- return new Reference(this, childRef);
- }
- else {
- var childRef = ref_1(this._delegate, path);
- return new Reference(this, childRef);
- }
- };
- /**
- * Returns a reference to the root or the path specified in url.
- * We throw a exception if the url is not in the same domain as the
- * current repo.
- * @returns Firebase reference.
- */
- Database.prototype.refFromURL = function (url) {
- var apiName = 'database.refFromURL';
- require$$1$3.validateArgCount(apiName, 1, 1, arguments.length);
- var childRef = refFromURL_1(this._delegate, url);
- return new Reference(this, childRef);
- };
- // Make individual repo go offline.
- Database.prototype.goOffline = function () {
- require$$1$3.validateArgCount('database.goOffline', 0, 0, arguments.length);
- return goOffline_1(this._delegate);
- };
- Database.prototype.goOnline = function () {
- require$$1$3.validateArgCount('database.goOnline', 0, 0, arguments.length);
- return goOnline_1(this._delegate);
- };
- Database.ServerValue = {
- TIMESTAMP: serverTimestamp_1(),
- increment: function (delta) { return increment_1(delta); }
- };
- return Database;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Used by console to create a database based on the app,
- * passed database URL and a custom auth implementation.
- *
- * @param app - A valid FirebaseApp-like object
- * @param url - A valid Firebase databaseURL
- * @param version - custom version e.g. firebase-admin version
- * @param customAuthImpl - custom auth implementation
- */
- function initStandalone$1(_a) {
- 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;
- _setSDKVersion(version);
- /**
- * ComponentContainer('database-standalone') is just a placeholder that doesn't perform
- * any actual function.
- */
- var authProvider = new component.Provider('auth-internal', new component.ComponentContainer('database-standalone'));
- authProvider.setComponent(new component.Component('auth-internal', function () { return customAuthImpl; }, "PRIVATE" /* ComponentType.PRIVATE */));
- return {
- instance: new Database(_repoManagerDatabaseFromApp(app, authProvider,
- /* appCheckProvider= */ undefined, url, nodeAdmin), app),
- namespace: namespace
- };
- }
-
- var INTERNAL = /*#__PURE__*/Object.freeze({
- __proto__: null,
- initStandalone: initStandalone$1
- });
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var ServerValue = Database.ServerValue;
- /**
- * A one off register function which returns a database based on the app and
- * passed database URL. (Used by the Admin SDK)
- *
- * @param app - A valid FirebaseApp-like object
- * @param url - A valid Firebase databaseURL
- * @param version - custom version e.g. firebase-admin version
- * @param nodeAdmin - true if the SDK is being initialized from Firebase Admin.
- */
- function initStandalone(app, url, version, nodeAdmin) {
- if (nodeAdmin === void 0) { nodeAdmin = true; }
- require$$1$3.CONSTANTS.NODE_ADMIN = nodeAdmin;
- return initStandalone$1({
- app: app,
- url: url,
- version: version,
- // firebase-admin-node's app.INTERNAL implements FirebaseAuthInternal interface
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- customAuthImpl: app.INTERNAL,
- namespace: {
- Reference: Reference,
- Query: Query,
- Database: Database,
- DataSnapshot: DataSnapshot,
- enableLogging: enableLogging_1,
- INTERNAL: INTERNAL,
- ServerValue: ServerValue
- },
- nodeAdmin: nodeAdmin
- });
- }
-
- exports.DataSnapshot = DataSnapshot;
- exports.Database = Database;
- exports.OnDisconnect = OnDisconnect_1;
- exports.Query = Query;
- exports.Reference = Reference;
- exports.ServerValue = ServerValue;
- exports.enableLogging = enableLogging_1;
- exports.initStandalone = initStandalone;
- //# sourceMappingURL=index.standalone.js.map
|