12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044 |
- import { _getProvider, getApp, _removeServiceInstance, _registerComponent, registerVersion, SDK_VERSION as SDK_VERSION$1 } from '@firebase/app';
- import { Component } from '@firebase/component';
- import { Logger, LogLevel } from '@firebase/logger';
- import { inspect } from 'util';
- import { FirebaseError, getDefaultEmulatorHostnameAndPort, createMockUserToken, getModularInstance, deepEqual } from '@firebase/util';
- import nodeFetch from 'node-fetch';
- import { randomBytes as randomBytes$1 } from 'crypto';
-
- const version$1 = "3.8.1";
-
- /**
- * @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.
- */
- /**
- * Simple wrapper around a nullable UID. Mostly exists to make code more
- * readable.
- */
- class User {
- constructor(uid) {
- this.uid = uid;
- }
- isAuthenticated() {
- return this.uid != null;
- }
- /**
- * Returns a key representing this user, suitable for inclusion in a
- * dictionary.
- */
- toKey() {
- if (this.isAuthenticated()) {
- return 'uid:' + this.uid;
- }
- else {
- return 'anonymous-user';
- }
- }
- isEqual(otherUser) {
- return otherUser.uid === this.uid;
- }
- }
- /** A user with a null UID. */
- User.UNAUTHENTICATED = new User(null);
- // TODO(mikelehen): Look into getting a proper uid-equivalent for
- // non-FirebaseAuth providers.
- User.GOOGLE_CREDENTIALS = new User('google-credentials-uid');
- User.FIRST_PARTY = new User('first-party-uid');
- User.MOCK_USER = new User('mock-user');
-
- const version = "9.16.0";
-
- /**
- * @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.
- */
- let SDK_VERSION = version;
- function setSDKVersion(version) {
- SDK_VERSION = version;
- }
-
- /**
- * @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.
- */
- /** Formats an object as a JSON string, suitable for logging. */
- function formatJSON(value) {
- // util.inspect() results in much more readable output than JSON.stringify()
- return inspect(value, { depth: 100 });
- }
-
- /**
- * @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.
- */
- const logClient = new Logger('@firebase/firestore');
- /**
- * Sets the verbosity of Cloud Firestore logs (debug, error, or silent).
- *
- * @param logLevel - The verbosity you set for activity and error logging. Can
- * be any of the following values:
- *
- * <ul>
- * <li>`debug` for the most verbose logging level, primarily for
- * debugging.</li>
- * <li>`error` to log errors only.</li>
- * <li><code>`silent` to turn off logging.</li>
- * </ul>
- */
- function setLogLevel(logLevel) {
- logClient.setLogLevel(logLevel);
- }
- function logDebug(msg, ...obj) {
- if (logClient.logLevel <= LogLevel.DEBUG) {
- const args = obj.map(argToString);
- logClient.debug(`Firestore (${SDK_VERSION}): ${msg}`, ...args);
- }
- }
- function logError(msg, ...obj) {
- if (logClient.logLevel <= LogLevel.ERROR) {
- const args = obj.map(argToString);
- logClient.error(`Firestore (${SDK_VERSION}): ${msg}`, ...args);
- }
- }
- /**
- * @internal
- */
- function logWarn(msg, ...obj) {
- if (logClient.logLevel <= LogLevel.WARN) {
- const args = obj.map(argToString);
- logClient.warn(`Firestore (${SDK_VERSION}): ${msg}`, ...args);
- }
- }
- /**
- * Converts an additional log parameter to a string representation.
- */
- function argToString(obj) {
- if (typeof obj === 'string') {
- return obj;
- }
- else {
- try {
- return formatJSON(obj);
- }
- catch (e) {
- // Converting to JSON failed, just log the object directly
- 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.
- */
- /**
- * Unconditionally fails, throwing an Error with the given message.
- * Messages are stripped in production builds.
- *
- * Returns `never` and can be used in expressions:
- * @example
- * let futureVar = fail('not implemented yet');
- */
- function fail(failure = 'Unexpected state') {
- // Log the failure in addition to throw an exception, just in case the
- // exception is swallowed.
- const message = `FIRESTORE (${SDK_VERSION}) INTERNAL ASSERTION FAILED: ` + failure;
- logError(message);
- // NOTE: We don't use FirestoreError here because these are internal failures
- // that cannot be handled by the user. (Also it would create a circular
- // dependency between the error and assert modules which doesn't work.)
- throw new Error(message);
- }
- /**
- * Fails if the given assertion condition is false, throwing an Error with the
- * given message if it did.
- *
- * Messages are stripped in production builds.
- */
- function hardAssert(assertion, message) {
- if (!assertion) {
- fail();
- }
- }
- /**
- * Casts `obj` to `T`. In non-production builds, verifies that `obj` is an
- * instance of `T` before casting.
- */
- function debugCast(obj,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- constructor) {
- 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.
- */
- const Code = {
- // Causes are copied from:
- // https://github.com/grpc/grpc/blob/bceec94ea4fc5f0085d81235d8e1c06798dc341a/include/grpc%2B%2B/impl/codegen/status_code_enum.h
- /** Not an error; returned on success. */
- OK: 'ok',
- /** The operation was cancelled (typically by the caller). */
- CANCELLED: 'cancelled',
- /** Unknown error or an error from a different error domain. */
- UNKNOWN: 'unknown',
- /**
- * Client specified an invalid argument. Note that this differs from
- * FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are
- * problematic regardless of the state of the system (e.g., a malformed file
- * name).
- */
- INVALID_ARGUMENT: 'invalid-argument',
- /**
- * Deadline expired before operation could complete. For operations that
- * change the state of the system, this error may be returned even if the
- * operation has completed successfully. For example, a successful response
- * from a server could have been delayed long enough for the deadline to
- * expire.
- */
- DEADLINE_EXCEEDED: 'deadline-exceeded',
- /** Some requested entity (e.g., file or directory) was not found. */
- NOT_FOUND: 'not-found',
- /**
- * Some entity that we attempted to create (e.g., file or directory) already
- * exists.
- */
- ALREADY_EXISTS: 'already-exists',
- /**
- * The caller does not have permission to execute the specified operation.
- * PERMISSION_DENIED must not be used for rejections caused by exhausting
- * some resource (use RESOURCE_EXHAUSTED instead for those errors).
- * PERMISSION_DENIED must not be used if the caller can not be identified
- * (use UNAUTHENTICATED instead for those errors).
- */
- PERMISSION_DENIED: 'permission-denied',
- /**
- * The request does not have valid authentication credentials for the
- * operation.
- */
- UNAUTHENTICATED: 'unauthenticated',
- /**
- * Some resource has been exhausted, perhaps a per-user quota, or perhaps the
- * entire file system is out of space.
- */
- RESOURCE_EXHAUSTED: 'resource-exhausted',
- /**
- * Operation was rejected because the system is not in a state required for
- * the operation's execution. For example, directory to be deleted may be
- * non-empty, an rmdir operation is applied to a non-directory, etc.
- *
- * A litmus test that may help a service implementor in deciding
- * between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE:
- * (a) Use UNAVAILABLE if the client can retry just the failing call.
- * (b) Use ABORTED if the client should retry at a higher-level
- * (e.g., restarting a read-modify-write sequence).
- * (c) Use FAILED_PRECONDITION if the client should not retry until
- * the system state has been explicitly fixed. E.g., if an "rmdir"
- * fails because the directory is non-empty, FAILED_PRECONDITION
- * should be returned since the client should not retry unless
- * they have first fixed up the directory by deleting files from it.
- * (d) Use FAILED_PRECONDITION if the client performs conditional
- * REST Get/Update/Delete on a resource and the resource on the
- * server does not match the condition. E.g., conflicting
- * read-modify-write on the same resource.
- */
- FAILED_PRECONDITION: 'failed-precondition',
- /**
- * The operation was aborted, typically due to a concurrency issue like
- * sequencer check failures, transaction aborts, etc.
- *
- * See litmus test above for deciding between FAILED_PRECONDITION, ABORTED,
- * and UNAVAILABLE.
- */
- ABORTED: 'aborted',
- /**
- * Operation was attempted past the valid range. E.g., seeking or reading
- * past end of file.
- *
- * Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed
- * if the system state changes. For example, a 32-bit file system will
- * generate INVALID_ARGUMENT if asked to read at an offset that is not in the
- * range [0,2^32-1], but it will generate OUT_OF_RANGE if asked to read from
- * an offset past the current file size.
- *
- * There is a fair bit of overlap between FAILED_PRECONDITION and
- * OUT_OF_RANGE. We recommend using OUT_OF_RANGE (the more specific error)
- * when it applies so that callers who are iterating through a space can
- * easily look for an OUT_OF_RANGE error to detect when they are done.
- */
- OUT_OF_RANGE: 'out-of-range',
- /** Operation is not implemented or not supported/enabled in this service. */
- UNIMPLEMENTED: 'unimplemented',
- /**
- * Internal errors. Means some invariants expected by underlying System has
- * been broken. If you see one of these errors, Something is very broken.
- */
- INTERNAL: 'internal',
- /**
- * The service is currently unavailable. This is a most likely a transient
- * condition and may be corrected by retrying with a backoff.
- *
- * See litmus test above for deciding between FAILED_PRECONDITION, ABORTED,
- * and UNAVAILABLE.
- */
- UNAVAILABLE: 'unavailable',
- /** Unrecoverable data loss or corruption. */
- DATA_LOSS: 'data-loss'
- };
- /** An error returned by a Firestore operation. */
- class FirestoreError extends FirebaseError {
- /** @hideconstructor */
- constructor(
- /**
- * The backend error code associated with this error.
- */
- code,
- /**
- * A custom error description.
- */
- message) {
- super(code, message);
- this.code = code;
- this.message = message;
- // HACK: We write a toString property directly because Error is not a real
- // class and so inheritance does not work correctly. We could alternatively
- // do the same "back-door inheritance" trick that FirebaseError does.
- this.toString = () => `${this.name}: [code=${this.code}]: ${this.message}`;
- }
- }
-
- /**
- * @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 Deferred {
- constructor() {
- this.promise = new Promise((resolve, reject) => {
- this.resolve = resolve;
- this.reject = reject;
- });
- }
- }
-
- /**
- * @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 OAuthToken {
- constructor(value, user) {
- this.user = user;
- this.type = 'OAuth';
- this.headers = new Map();
- this.headers.set('Authorization', `Bearer ${value}`);
- }
- }
- /**
- * A CredentialsProvider that always yields an empty token.
- * @internal
- */
- class EmptyAuthCredentialsProvider {
- getToken() {
- return Promise.resolve(null);
- }
- invalidateToken() { }
- start(asyncQueue, changeListener) {
- // Fire with initial user.
- asyncQueue.enqueueRetryable(() => changeListener(User.UNAUTHENTICATED));
- }
- shutdown() { }
- }
- /**
- * A CredentialsProvider that always returns a constant token. Used for
- * emulator token mocking.
- */
- class EmulatorAuthCredentialsProvider {
- constructor(token) {
- this.token = token;
- /**
- * Stores the listener registered with setChangeListener()
- * This isn't actually necessary since the UID never changes, but we use this
- * to verify the listen contract is adhered to in tests.
- */
- this.changeListener = null;
- }
- getToken() {
- return Promise.resolve(this.token);
- }
- invalidateToken() { }
- start(asyncQueue, changeListener) {
- this.changeListener = changeListener;
- // Fire with initial user.
- asyncQueue.enqueueRetryable(() => changeListener(this.token.user));
- }
- shutdown() {
- this.changeListener = null;
- }
- }
- /** Credential provider for the Lite SDK. */
- class LiteAuthCredentialsProvider {
- constructor(authProvider) {
- this.auth = null;
- authProvider.onInit(auth => {
- this.auth = auth;
- });
- }
- getToken() {
- if (!this.auth) {
- return Promise.resolve(null);
- }
- return this.auth.getToken().then(tokenData => {
- if (tokenData) {
- hardAssert(typeof tokenData.accessToken === 'string');
- return new OAuthToken(tokenData.accessToken, new User(this.auth.getUid()));
- }
- else {
- return null;
- }
- });
- }
- invalidateToken() { }
- start(asyncQueue, changeListener) { }
- shutdown() { }
- }
- /*
- * FirstPartyToken provides a fresh token each time its value
- * is requested, because if the token is too old, requests will be rejected.
- * Technically this may no longer be necessary since the SDK should gracefully
- * recover from unauthenticated errors (see b/33147818 for context), but it's
- * safer to keep the implementation as-is.
- */
- class FirstPartyToken {
- constructor(gapi, sessionIndex, iamToken, authTokenFactory) {
- this.gapi = gapi;
- this.sessionIndex = sessionIndex;
- this.iamToken = iamToken;
- this.authTokenFactory = authTokenFactory;
- this.type = 'FirstParty';
- this.user = User.FIRST_PARTY;
- this._headers = new Map();
- }
- /** Gets an authorization token, using a provided factory function, or falling back to First Party GAPI. */
- getAuthToken() {
- if (this.authTokenFactory) {
- return this.authTokenFactory();
- }
- else {
- // Make sure this really is a Gapi client.
- hardAssert(!!(typeof this.gapi === 'object' &&
- this.gapi !== null &&
- this.gapi['auth'] &&
- this.gapi['auth']['getAuthHeaderValueForFirstParty']));
- return this.gapi['auth']['getAuthHeaderValueForFirstParty']([]);
- }
- }
- get headers() {
- this._headers.set('X-Goog-AuthUser', this.sessionIndex);
- // Use array notation to prevent minification
- const authHeaderTokenValue = this.getAuthToken();
- if (authHeaderTokenValue) {
- this._headers.set('Authorization', authHeaderTokenValue);
- }
- if (this.iamToken) {
- this._headers.set('X-Goog-Iam-Authorization-Token', this.iamToken);
- }
- return this._headers;
- }
- }
- /*
- * Provides user credentials required for the Firestore JavaScript SDK
- * to authenticate the user, using technique that is only available
- * to applications hosted by Google.
- */
- class FirstPartyAuthCredentialsProvider {
- constructor(gapi, sessionIndex, iamToken, authTokenFactory) {
- this.gapi = gapi;
- this.sessionIndex = sessionIndex;
- this.iamToken = iamToken;
- this.authTokenFactory = authTokenFactory;
- }
- getToken() {
- return Promise.resolve(new FirstPartyToken(this.gapi, this.sessionIndex, this.iamToken, this.authTokenFactory));
- }
- start(asyncQueue, changeListener) {
- // Fire with initial uid.
- asyncQueue.enqueueRetryable(() => changeListener(User.FIRST_PARTY));
- }
- shutdown() { }
- invalidateToken() { }
- }
- class AppCheckToken {
- constructor(value) {
- this.value = value;
- this.type = 'AppCheck';
- this.headers = new Map();
- if (value && value.length > 0) {
- this.headers.set('x-firebase-appcheck', this.value);
- }
- }
- }
- /** AppCheck token provider for the Lite SDK. */
- class LiteAppCheckTokenProvider {
- constructor(appCheckProvider) {
- this.appCheckProvider = appCheckProvider;
- this.appCheck = null;
- appCheckProvider.onInit(appCheck => {
- this.appCheck = appCheck;
- });
- }
- getToken() {
- if (!this.appCheck) {
- return Promise.resolve(null);
- }
- return this.appCheck.getToken().then(tokenResult => {
- if (tokenResult) {
- hardAssert(typeof tokenResult.token === 'string');
- return new AppCheckToken(tokenResult.token);
- }
- else {
- return null;
- }
- });
- }
- invalidateToken() { }
- start(asyncQueue, changeListener) { }
- shutdown() { }
- }
- /**
- * Builds a CredentialsProvider depending on the type of
- * the credentials passed in.
- */
- function makeAuthCredentialsProvider(credentials) {
- if (!credentials) {
- return new EmptyAuthCredentialsProvider();
- }
- switch (credentials['type']) {
- case 'gapi':
- const client = credentials['client'];
- return new FirstPartyAuthCredentialsProvider(client, credentials['sessionIndex'] || '0', credentials['iamToken'] || null, credentials['authTokenFactory'] || null);
- case 'provider':
- return credentials['client'];
- default:
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'makeAuthCredentialsProvider failed due to invalid credential type');
- }
- }
-
- /**
- * @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 DatabaseInfo {
- /**
- * Constructs a DatabaseInfo using the provided host, databaseId and
- * persistenceKey.
- *
- * @param databaseId - The database to use.
- * @param appId - The Firebase App Id.
- * @param persistenceKey - A unique identifier for this Firestore's local
- * storage (used in conjunction with the databaseId).
- * @param host - The Firestore backend host to connect to.
- * @param ssl - Whether to use SSL when connecting.
- * @param forceLongPolling - Whether to use the forceLongPolling option
- * when using WebChannel as the network transport.
- * @param autoDetectLongPolling - Whether to use the detectBufferingProxy
- * option when using WebChannel as the network transport.
- * @param useFetchStreams Whether to use the Fetch API instead of
- * XMLHTTPRequest
- */
- constructor(databaseId, appId, persistenceKey, host, ssl, forceLongPolling, autoDetectLongPolling, useFetchStreams) {
- this.databaseId = databaseId;
- this.appId = appId;
- this.persistenceKey = persistenceKey;
- this.host = host;
- this.ssl = ssl;
- this.forceLongPolling = forceLongPolling;
- this.autoDetectLongPolling = autoDetectLongPolling;
- this.useFetchStreams = useFetchStreams;
- }
- }
- /** The default database name for a project. */
- const DEFAULT_DATABASE_NAME = '(default)';
- /**
- * Represents the database ID a Firestore client is associated with.
- * @internal
- */
- class DatabaseId {
- constructor(projectId, database) {
- this.projectId = projectId;
- this.database = database ? database : DEFAULT_DATABASE_NAME;
- }
- static empty() {
- return new DatabaseId('', '');
- }
- get isDefaultDatabase() {
- return this.database === DEFAULT_DATABASE_NAME;
- }
- isEqual(other) {
- return (other instanceof DatabaseId &&
- other.projectId === this.projectId &&
- other.database === this.database);
- }
- }
- function databaseIdFromApp(app, database) {
- if (!Object.prototype.hasOwnProperty.apply(app.options, ['projectId'])) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, '"projectId" not provided in firebase.initializeApp.');
- }
- return new DatabaseId(app.options.projectId, 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.
- */
- const DOCUMENT_KEY_NAME = '__name__';
- /**
- * Path represents an ordered sequence of string segments.
- */
- class BasePath {
- constructor(segments, offset, length) {
- if (offset === undefined) {
- offset = 0;
- }
- else if (offset > segments.length) {
- fail();
- }
- if (length === undefined) {
- length = segments.length - offset;
- }
- else if (length > segments.length - offset) {
- fail();
- }
- this.segments = segments;
- this.offset = offset;
- this.len = length;
- }
- get length() {
- return this.len;
- }
- isEqual(other) {
- return BasePath.comparator(this, other) === 0;
- }
- child(nameOrPath) {
- const segments = this.segments.slice(this.offset, this.limit());
- if (nameOrPath instanceof BasePath) {
- nameOrPath.forEach(segment => {
- segments.push(segment);
- });
- }
- else {
- segments.push(nameOrPath);
- }
- return this.construct(segments);
- }
- /** The index of one past the last segment of the path. */
- limit() {
- return this.offset + this.length;
- }
- popFirst(size) {
- size = size === undefined ? 1 : size;
- return this.construct(this.segments, this.offset + size, this.length - size);
- }
- popLast() {
- return this.construct(this.segments, this.offset, this.length - 1);
- }
- firstSegment() {
- return this.segments[this.offset];
- }
- lastSegment() {
- return this.get(this.length - 1);
- }
- get(index) {
- return this.segments[this.offset + index];
- }
- isEmpty() {
- return this.length === 0;
- }
- isPrefixOf(other) {
- if (other.length < this.length) {
- return false;
- }
- for (let i = 0; i < this.length; i++) {
- if (this.get(i) !== other.get(i)) {
- return false;
- }
- }
- return true;
- }
- isImmediateParentOf(potentialChild) {
- if (this.length + 1 !== potentialChild.length) {
- return false;
- }
- for (let i = 0; i < this.length; i++) {
- if (this.get(i) !== potentialChild.get(i)) {
- return false;
- }
- }
- return true;
- }
- forEach(fn) {
- for (let i = this.offset, end = this.limit(); i < end; i++) {
- fn(this.segments[i]);
- }
- }
- toArray() {
- return this.segments.slice(this.offset, this.limit());
- }
- static comparator(p1, p2) {
- const len = Math.min(p1.length, p2.length);
- for (let i = 0; i < len; i++) {
- const left = p1.get(i);
- const right = p2.get(i);
- if (left < right) {
- return -1;
- }
- if (left > right) {
- return 1;
- }
- }
- if (p1.length < p2.length) {
- return -1;
- }
- if (p1.length > p2.length) {
- return 1;
- }
- return 0;
- }
- }
- /**
- * A slash-separated path for navigating resources (documents and collections)
- * within Firestore.
- *
- * @internal
- */
- class ResourcePath extends BasePath {
- construct(segments, offset, length) {
- return new ResourcePath(segments, offset, length);
- }
- canonicalString() {
- // NOTE: The client is ignorant of any path segments containing escape
- // sequences (e.g. __id123__) and just passes them through raw (they exist
- // for legacy reasons and should not be used frequently).
- return this.toArray().join('/');
- }
- toString() {
- return this.canonicalString();
- }
- /**
- * Creates a resource path from the given slash-delimited string. If multiple
- * arguments are provided, all components are combined. Leading and trailing
- * slashes from all components are ignored.
- */
- static fromString(...pathComponents) {
- // NOTE: The client is ignorant of any path segments containing escape
- // sequences (e.g. __id123__) and just passes them through raw (they exist
- // for legacy reasons and should not be used frequently).
- const segments = [];
- for (const path of pathComponents) {
- if (path.indexOf('//') >= 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid segment (${path}). Paths must not contain // in them.`);
- }
- // Strip leading and traling slashed.
- segments.push(...path.split('/').filter(segment => segment.length > 0));
- }
- return new ResourcePath(segments);
- }
- static emptyPath() {
- return new ResourcePath([]);
- }
- }
- const identifierRegExp = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
- /**
- * A dot-separated path for navigating sub-objects within a document.
- * @internal
- */
- class FieldPath$1 extends BasePath {
- construct(segments, offset, length) {
- return new FieldPath$1(segments, offset, length);
- }
- /**
- * Returns true if the string could be used as a segment in a field path
- * without escaping.
- */
- static isValidIdentifier(segment) {
- return identifierRegExp.test(segment);
- }
- canonicalString() {
- return this.toArray()
- .map(str => {
- str = str.replace(/\\/g, '\\\\').replace(/`/g, '\\`');
- if (!FieldPath$1.isValidIdentifier(str)) {
- str = '`' + str + '`';
- }
- return str;
- })
- .join('.');
- }
- toString() {
- return this.canonicalString();
- }
- /**
- * Returns true if this field references the key of a document.
- */
- isKeyField() {
- return this.length === 1 && this.get(0) === DOCUMENT_KEY_NAME;
- }
- /**
- * The field designating the key of a document.
- */
- static keyField() {
- return new FieldPath$1([DOCUMENT_KEY_NAME]);
- }
- /**
- * Parses a field string from the given server-formatted string.
- *
- * - Splitting the empty string is not allowed (for now at least).
- * - Empty segments within the string (e.g. if there are two consecutive
- * separators) are not allowed.
- *
- * TODO(b/37244157): we should make this more strict. Right now, it allows
- * non-identifier path components, even if they aren't escaped.
- */
- static fromServerFormat(path) {
- const segments = [];
- let current = '';
- let i = 0;
- const addCurrentSegment = () => {
- if (current.length === 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid field path (${path}). Paths must not be empty, begin ` +
- `with '.', end with '.', or contain '..'`);
- }
- segments.push(current);
- current = '';
- };
- let inBackticks = false;
- while (i < path.length) {
- const c = path[i];
- if (c === '\\') {
- if (i + 1 === path.length) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Path has trailing escape character: ' + path);
- }
- const next = path[i + 1];
- if (!(next === '\\' || next === '.' || next === '`')) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Path has invalid escape sequence: ' + path);
- }
- current += next;
- i += 2;
- }
- else if (c === '`') {
- inBackticks = !inBackticks;
- i++;
- }
- else if (c === '.' && !inBackticks) {
- addCurrentSegment();
- i++;
- }
- else {
- current += c;
- i++;
- }
- }
- addCurrentSegment();
- if (inBackticks) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Unterminated ` in path: ' + path);
- }
- return new FieldPath$1(segments);
- }
- static emptyPath() {
- return new FieldPath$1([]);
- }
- }
-
- /**
- * @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.
- */
- /**
- * @internal
- */
- class DocumentKey {
- constructor(path) {
- this.path = path;
- }
- static fromPath(path) {
- return new DocumentKey(ResourcePath.fromString(path));
- }
- static fromName(name) {
- return new DocumentKey(ResourcePath.fromString(name).popFirst(5));
- }
- static empty() {
- return new DocumentKey(ResourcePath.emptyPath());
- }
- get collectionGroup() {
- return this.path.popLast().lastSegment();
- }
- /** Returns true if the document is in the specified collectionId. */
- hasCollectionId(collectionId) {
- return (this.path.length >= 2 &&
- this.path.get(this.path.length - 2) === collectionId);
- }
- /** Returns the collection group (i.e. the name of the parent collection) for this key. */
- getCollectionGroup() {
- return this.path.get(this.path.length - 2);
- }
- /** Returns the fully qualified path to the parent collection. */
- getCollectionPath() {
- return this.path.popLast();
- }
- isEqual(other) {
- return (other !== null && ResourcePath.comparator(this.path, other.path) === 0);
- }
- toString() {
- return this.path.toString();
- }
- static comparator(k1, k2) {
- return ResourcePath.comparator(k1.path, k2.path);
- }
- static isDocumentKey(path) {
- return path.length % 2 === 0;
- }
- /**
- * Creates and returns a new document key with the given segments.
- *
- * @param segments - The segments of the path to the document
- * @returns A new instance of DocumentKey
- */
- static fromSegments(segments) {
- return new DocumentKey(new ResourcePath(segments.slice()));
- }
- }
-
- /**
- * @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 validateNonEmptyArgument(functionName, argumentName, argument) {
- if (!argument) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Function ${functionName}() cannot be called with an empty ${argumentName}.`);
- }
- }
- /**
- * Validates that two boolean options are not set at the same time.
- * @internal
- */
- function validateIsNotUsedTogether(optionName1, argument1, optionName2, argument2) {
- if (argument1 === true && argument2 === true) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `${optionName1} and ${optionName2} cannot be used together.`);
- }
- }
- /**
- * Validates that `path` refers to a document (indicated by the fact it contains
- * an even numbers of segments).
- */
- function validateDocumentPath(path) {
- if (!DocumentKey.isDocumentKey(path)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid document reference. Document references must have an even number of segments, but ${path} has ${path.length}.`);
- }
- }
- /**
- * Validates that `path` refers to a collection (indicated by the fact it
- * contains an odd numbers of segments).
- */
- function validateCollectionPath(path) {
- if (DocumentKey.isDocumentKey(path)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid collection reference. Collection references must have an odd number of segments, but ${path} has ${path.length}.`);
- }
- }
- /**
- * Returns true if it's a non-null object without a custom prototype
- * (i.e. excludes Array, Date, etc.).
- */
- function isPlainObject(input) {
- return (typeof input === 'object' &&
- input !== null &&
- (Object.getPrototypeOf(input) === Object.prototype ||
- Object.getPrototypeOf(input) === null));
- }
- /** Returns a string describing the type / value of the provided input. */
- function valueDescription(input) {
- if (input === undefined) {
- return 'undefined';
- }
- else if (input === null) {
- return 'null';
- }
- else if (typeof input === 'string') {
- if (input.length > 20) {
- input = `${input.substring(0, 20)}...`;
- }
- return JSON.stringify(input);
- }
- else if (typeof input === 'number' || typeof input === 'boolean') {
- return '' + input;
- }
- else if (typeof input === 'object') {
- if (input instanceof Array) {
- return 'an array';
- }
- else {
- const customObjectName = tryGetCustomObjectType(input);
- if (customObjectName) {
- return `a custom ${customObjectName} object`;
- }
- else {
- return 'an object';
- }
- }
- }
- else if (typeof input === 'function') {
- return 'a function';
- }
- else {
- return fail();
- }
- }
- /** try to get the constructor name for an object. */
- function tryGetCustomObjectType(input) {
- if (input.constructor) {
- return input.constructor.name;
- }
- return null;
- }
- /**
- * Casts `obj` to `T`, optionally unwrapping Compat types to expose the
- * underlying instance. Throws if `obj` is not an instance of `T`.
- *
- * This cast is used in the Lite and Full SDK to verify instance types for
- * arguments passed to the public API.
- * @internal
- */
- function cast(obj,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- constructor) {
- if ('_delegate' in obj) {
- // Unwrap Compat types
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- obj = obj._delegate;
- }
- if (!(obj instanceof constructor)) {
- if (constructor.name === obj.constructor.name) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Type does not match the expected instance. Did you pass a ' +
- `reference from a different Firestore SDK?`);
- }
- else {
- const description = valueDescription(obj);
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Expected type '${constructor.name}', but it was: ${description}`);
- }
- }
- return obj;
- }
- function validatePositiveNumber(functionName, n) {
- if (n <= 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Function ${functionName}() requires a positive number, but it was: ${n}.`);
- }
- }
-
- /**
- * @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.
- */
- const LOG_TAG$3 = 'RestConnection';
- /**
- * Maps RPC names to the corresponding REST endpoint name.
- *
- * We use array notation to avoid mangling.
- */
- const RPC_NAME_URL_MAPPING = {};
- RPC_NAME_URL_MAPPING['BatchGetDocuments'] = 'batchGet';
- RPC_NAME_URL_MAPPING['Commit'] = 'commit';
- RPC_NAME_URL_MAPPING['RunQuery'] = 'runQuery';
- RPC_NAME_URL_MAPPING['RunAggregationQuery'] = 'runAggregationQuery';
- const RPC_URL_VERSION = 'v1';
- // SDK_VERSION is updated to different value at runtime depending on the entry point,
- // so we need to get its value when we need it in a function.
- function getGoogApiClientValue() {
- return 'gl-js/ fire/' + SDK_VERSION;
- }
- /**
- * Base class for all Rest-based connections to the backend (WebChannel and
- * HTTP).
- */
- class RestConnection {
- constructor(databaseInfo) {
- this.databaseInfo = databaseInfo;
- this.databaseId = databaseInfo.databaseId;
- const proto = databaseInfo.ssl ? 'https' : 'http';
- this.baseUrl = proto + '://' + databaseInfo.host;
- this.databaseRoot =
- 'projects/' +
- this.databaseId.projectId +
- '/databases/' +
- this.databaseId.database +
- '/documents';
- }
- get shouldResourcePathBeIncludedInRequest() {
- // Both `invokeRPC()` and `invokeStreamingRPC()` use their `path` arguments to determine
- // where to run the query, and expect the `request` to NOT specify the "path".
- return false;
- }
- invokeRPC(rpcName, path, req, authToken, appCheckToken) {
- const url = this.makeUrl(rpcName, path);
- logDebug(LOG_TAG$3, 'Sending: ', url, req);
- const headers = {};
- this.modifyHeadersForRequest(headers, authToken, appCheckToken);
- return this.performRPCRequest(rpcName, url, headers, req).then(response => {
- logDebug(LOG_TAG$3, 'Received: ', response);
- return response;
- }, (err) => {
- logWarn(LOG_TAG$3, `${rpcName} failed with error: `, err, 'url: ', url, 'request:', req);
- throw err;
- });
- }
- invokeStreamingRPC(rpcName, path, request, authToken, appCheckToken, expectedResponseCount) {
- // The REST API automatically aggregates all of the streamed results, so we
- // can just use the normal invoke() method.
- return this.invokeRPC(rpcName, path, request, authToken, appCheckToken);
- }
- /**
- * Modifies the headers for a request, adding any authorization token if
- * present and any additional headers for the request.
- */
- modifyHeadersForRequest(headers, authToken, appCheckToken) {
- headers['X-Goog-Api-Client'] = getGoogApiClientValue();
- // Content-Type: text/plain will avoid preflight requests which might
- // mess with CORS and redirects by proxies. If we add custom headers
- // we will need to change this code to potentially use the $httpOverwrite
- // parameter supported by ESF to avoid triggering preflight requests.
- headers['Content-Type'] = 'text/plain';
- if (this.databaseInfo.appId) {
- headers['X-Firebase-GMPID'] = this.databaseInfo.appId;
- }
- if (authToken) {
- authToken.headers.forEach((value, key) => (headers[key] = value));
- }
- if (appCheckToken) {
- appCheckToken.headers.forEach((value, key) => (headers[key] = value));
- }
- }
- makeUrl(rpcName, path) {
- const urlRpcName = RPC_NAME_URL_MAPPING[rpcName];
- return `${this.baseUrl}/${RPC_URL_VERSION}/${path}:${urlRpcName}`;
- }
- }
-
- /**
- * @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.
- */
- /**
- * Error Codes describing the different ways GRPC can fail. These are copied
- * directly from GRPC's sources here:
- *
- * https://github.com/grpc/grpc/blob/bceec94ea4fc5f0085d81235d8e1c06798dc341a/include/grpc%2B%2B/impl/codegen/status_code_enum.h
- *
- * Important! The names of these identifiers matter because the string forms
- * are used for reverse lookups from the webchannel stream. Do NOT change the
- * names of these identifiers or change this into a const enum.
- */
- var RpcCode;
- (function (RpcCode) {
- RpcCode[RpcCode["OK"] = 0] = "OK";
- RpcCode[RpcCode["CANCELLED"] = 1] = "CANCELLED";
- RpcCode[RpcCode["UNKNOWN"] = 2] = "UNKNOWN";
- RpcCode[RpcCode["INVALID_ARGUMENT"] = 3] = "INVALID_ARGUMENT";
- RpcCode[RpcCode["DEADLINE_EXCEEDED"] = 4] = "DEADLINE_EXCEEDED";
- RpcCode[RpcCode["NOT_FOUND"] = 5] = "NOT_FOUND";
- RpcCode[RpcCode["ALREADY_EXISTS"] = 6] = "ALREADY_EXISTS";
- RpcCode[RpcCode["PERMISSION_DENIED"] = 7] = "PERMISSION_DENIED";
- RpcCode[RpcCode["UNAUTHENTICATED"] = 16] = "UNAUTHENTICATED";
- RpcCode[RpcCode["RESOURCE_EXHAUSTED"] = 8] = "RESOURCE_EXHAUSTED";
- RpcCode[RpcCode["FAILED_PRECONDITION"] = 9] = "FAILED_PRECONDITION";
- RpcCode[RpcCode["ABORTED"] = 10] = "ABORTED";
- RpcCode[RpcCode["OUT_OF_RANGE"] = 11] = "OUT_OF_RANGE";
- RpcCode[RpcCode["UNIMPLEMENTED"] = 12] = "UNIMPLEMENTED";
- RpcCode[RpcCode["INTERNAL"] = 13] = "INTERNAL";
- RpcCode[RpcCode["UNAVAILABLE"] = 14] = "UNAVAILABLE";
- RpcCode[RpcCode["DATA_LOSS"] = 15] = "DATA_LOSS";
- })(RpcCode || (RpcCode = {}));
- /**
- * Determines whether an error code represents a permanent error when received
- * in response to a non-write operation.
- *
- * See isPermanentWriteError for classifying write errors.
- */
- function isPermanentError(code) {
- switch (code) {
- case Code.OK:
- return fail();
- case Code.CANCELLED:
- case Code.UNKNOWN:
- case Code.DEADLINE_EXCEEDED:
- case Code.RESOURCE_EXHAUSTED:
- case Code.INTERNAL:
- case Code.UNAVAILABLE:
- // Unauthenticated means something went wrong with our token and we need
- // to retry with new credentials which will happen automatically.
- case Code.UNAUTHENTICATED:
- return false;
- case Code.INVALID_ARGUMENT:
- case Code.NOT_FOUND:
- case Code.ALREADY_EXISTS:
- case Code.PERMISSION_DENIED:
- case Code.FAILED_PRECONDITION:
- // Aborted might be retried in some scenarios, but that is dependant on
- // the context and should handled individually by the calling code.
- // See https://cloud.google.com/apis/design/errors.
- case Code.ABORTED:
- case Code.OUT_OF_RANGE:
- case Code.UNIMPLEMENTED:
- case Code.DATA_LOSS:
- return true;
- default:
- return fail();
- }
- }
- /**
- * Converts an HTTP Status Code to the equivalent error code.
- *
- * @param status - An HTTP Status Code, like 200, 404, 503, etc.
- * @returns The equivalent Code. Unknown status codes are mapped to
- * Code.UNKNOWN.
- */
- function mapCodeFromHttpStatus(status) {
- if (status === undefined) {
- logError('RPC_ERROR', 'HTTP error has no status');
- return Code.UNKNOWN;
- }
- // The canonical error codes for Google APIs [1] specify mapping onto HTTP
- // status codes but the mapping is not bijective. In each case of ambiguity
- // this function chooses a primary error.
- //
- // [1]
- // https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
- switch (status) {
- case 200: // OK
- return Code.OK;
- case 400: // Bad Request
- return Code.FAILED_PRECONDITION;
- // Other possibilities based on the forward mapping
- // return Code.INVALID_ARGUMENT;
- // return Code.OUT_OF_RANGE;
- case 401: // Unauthorized
- return Code.UNAUTHENTICATED;
- case 403: // Forbidden
- return Code.PERMISSION_DENIED;
- case 404: // Not Found
- return Code.NOT_FOUND;
- case 409: // Conflict
- return Code.ABORTED;
- // Other possibilities:
- // return Code.ALREADY_EXISTS;
- case 416: // Range Not Satisfiable
- return Code.OUT_OF_RANGE;
- case 429: // Too Many Requests
- return Code.RESOURCE_EXHAUSTED;
- case 499: // Client Closed Request
- return Code.CANCELLED;
- case 500: // Internal Server Error
- return Code.UNKNOWN;
- // Other possibilities:
- // return Code.INTERNAL;
- // return Code.DATA_LOSS;
- case 501: // Unimplemented
- return Code.UNIMPLEMENTED;
- case 503: // Service Unavailable
- return Code.UNAVAILABLE;
- case 504: // Gateway Timeout
- return Code.DEADLINE_EXCEEDED;
- default:
- if (status >= 200 && status < 300) {
- return Code.OK;
- }
- if (status >= 400 && status < 500) {
- return Code.FAILED_PRECONDITION;
- }
- if (status >= 500 && status < 600) {
- return Code.INTERNAL;
- }
- return Code.UNKNOWN;
- }
- }
-
- /**
- * @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 Rest-based connection that relies on the native HTTP stack
- * (e.g. `fetch` or a polyfill).
- */
- class FetchConnection extends RestConnection {
- /**
- * @param databaseInfo - The connection info.
- * @param fetchImpl - `fetch` or a Polyfill that implements the fetch API.
- */
- constructor(databaseInfo, fetchImpl) {
- super(databaseInfo);
- this.fetchImpl = fetchImpl;
- }
- openStream(rpcName, token) {
- throw new Error('Not supported by FetchConnection');
- }
- async performRPCRequest(rpcName, url, headers, body) {
- var _a;
- const requestJson = JSON.stringify(body);
- let response;
- try {
- response = await this.fetchImpl(url, {
- method: 'POST',
- headers,
- body: requestJson
- });
- }
- catch (e) {
- const err = e;
- throw new FirestoreError(mapCodeFromHttpStatus(err.status), 'Request failed with error: ' + err.statusText);
- }
- if (!response.ok) {
- let errorResponse = await response.json();
- if (Array.isArray(errorResponse)) {
- errorResponse = errorResponse[0];
- }
- const errorMessage = (_a = errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.error) === null || _a === void 0 ? void 0 : _a.message;
- throw new FirestoreError(mapCodeFromHttpStatus(response.status), `Request failed with error: ${errorMessage !== null && errorMessage !== void 0 ? errorMessage : response.statusText}`);
- }
- return response.json();
- }
- }
-
- /**
- * @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.
- */
- /** Initializes the HTTP connection for the REST API. */
- function newConnection(databaseInfo) {
- // node-fetch is meant to be API compatible with `fetch`, but its type doesn't
- // match 100%.
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return new FetchConnection(databaseInfo, nodeFetch);
- }
-
- /**
- * @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.
- */
- /**
- * Generates `nBytes` of random bytes.
- *
- * If `nBytes < 0` , an error will be thrown.
- */
- function randomBytes(nBytes) {
- return randomBytes$1(nBytes);
- }
-
- /**
- * @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 AutoId {
- static newId() {
- // Alphanumeric characters
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- // The largest byte value that is a multiple of `char.length`.
- const maxMultiple = Math.floor(256 / chars.length) * chars.length;
- let autoId = '';
- const targetLength = 20;
- while (autoId.length < targetLength) {
- const bytes = randomBytes(40);
- for (let i = 0; i < bytes.length; ++i) {
- // Only accept values that are [0, maxMultiple), this ensures they can
- // be evenly mapped to indices of `chars` via a modulo operation.
- if (autoId.length < targetLength && bytes[i] < maxMultiple) {
- autoId += chars.charAt(bytes[i] % chars.length);
- }
- }
- }
- return autoId;
- }
- }
- function primitiveComparator(left, right) {
- if (left < right) {
- return -1;
- }
- if (left > right) {
- return 1;
- }
- return 0;
- }
- /** Helper to compare arrays using isEqual(). */
- function arrayEquals(left, right, comparator) {
- if (left.length !== right.length) {
- return false;
- }
- return left.every((value, index) => comparator(value, right[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.
- */
- function objectSize(obj) {
- let count = 0;
- for (const key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
- count++;
- }
- }
- return count;
- }
- function forEach(obj, fn) {
- for (const key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
- fn(key, obj[key]);
- }
- }
- }
- function isEmpty(obj) {
- for (const key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
- return false;
- }
- }
- return 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.
- */
- /**
- * Returns whether a variable is either undefined or null.
- */
- function isNullOrUndefined(value) {
- return value === null || value === undefined;
- }
- /** Returns whether the value represents -0. */
- function isNegativeZero(value) {
- // Detect if the value is -0.0. Based on polyfill from
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
- return value === 0 && 1 / value === 1 / -0;
- }
- /**
- * Returns whether a value is an integer and in the safe integer range
- * @param value - The value to test for being an integer and in the safe range
- */
- function isSafeInteger(value) {
- return (typeof value === 'number' &&
- Number.isInteger(value) &&
- !isNegativeZero(value) &&
- value <= Number.MAX_SAFE_INTEGER &&
- value >= Number.MIN_SAFE_INTEGER);
- }
-
- /**
- * @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.
- */
- /** Converts a Base64 encoded string to a binary string. */
- function decodeBase64(encoded) {
- // Note: We used to validate the base64 string here via a regular expression.
- // This was removed to improve the performance of indexing.
- return Buffer.from(encoded, 'base64').toString('binary');
- }
- /** Converts a binary string to a Base64 encoded string. */
- function encodeBase64(raw) {
- return Buffer.from(raw, 'binary').toString('base64');
- }
-
- /**
- * @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.
- */
- /**
- * Immutable class that represents a "proto" byte string.
- *
- * Proto byte strings can either be Base64-encoded strings or Uint8Arrays when
- * sent on the wire. This class abstracts away this differentiation by holding
- * the proto byte string in a common class that must be converted into a string
- * before being sent as a proto.
- * @internal
- */
- class ByteString {
- constructor(binaryString) {
- this.binaryString = binaryString;
- }
- static fromBase64String(base64) {
- const binaryString = decodeBase64(base64);
- return new ByteString(binaryString);
- }
- static fromUint8Array(array) {
- // TODO(indexing); Remove the copy of the byte string here as this method
- // is frequently called during indexing.
- const binaryString = binaryStringFromUint8Array(array);
- return new ByteString(binaryString);
- }
- [Symbol.iterator]() {
- let i = 0;
- return {
- next: () => {
- if (i < this.binaryString.length) {
- return { value: this.binaryString.charCodeAt(i++), done: false };
- }
- else {
- return { value: undefined, done: true };
- }
- }
- };
- }
- toBase64() {
- return encodeBase64(this.binaryString);
- }
- toUint8Array() {
- return uint8ArrayFromBinaryString(this.binaryString);
- }
- approximateByteSize() {
- return this.binaryString.length * 2;
- }
- compareTo(other) {
- return primitiveComparator(this.binaryString, other.binaryString);
- }
- isEqual(other) {
- return this.binaryString === other.binaryString;
- }
- }
- ByteString.EMPTY_BYTE_STRING = new ByteString('');
- /**
- * Helper function to convert an Uint8array to a binary string.
- */
- function binaryStringFromUint8Array(array) {
- let binaryString = '';
- for (let i = 0; i < array.length; ++i) {
- binaryString += String.fromCharCode(array[i]);
- }
- return binaryString;
- }
- /**
- * Helper function to convert a binary string to an Uint8Array.
- */
- function uint8ArrayFromBinaryString(binaryString) {
- const buffer = new Uint8Array(binaryString.length);
- for (let i = 0; i < binaryString.length; i++) {
- buffer[i] = binaryString.charCodeAt(i);
- }
- return buffer;
- }
-
- /**
- * @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 RegExp matching ISO 8601 UTC timestamps with optional fraction.
- const ISO_TIMESTAMP_REG_EXP = new RegExp(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.(\d+))?Z$/);
- /**
- * Converts the possible Proto values for a timestamp value into a "seconds and
- * nanos" representation.
- */
- function normalizeTimestamp(date) {
- hardAssert(!!date);
- // The json interface (for the browser) will return an iso timestamp string,
- // while the proto js library (for node) will return a
- // google.protobuf.Timestamp instance.
- if (typeof date === 'string') {
- // The date string can have higher precision (nanos) than the Date class
- // (millis), so we do some custom parsing here.
- // Parse the nanos right out of the string.
- let nanos = 0;
- const fraction = ISO_TIMESTAMP_REG_EXP.exec(date);
- hardAssert(!!fraction);
- if (fraction[1]) {
- // Pad the fraction out to 9 digits (nanos).
- let nanoStr = fraction[1];
- nanoStr = (nanoStr + '000000000').substr(0, 9);
- nanos = Number(nanoStr);
- }
- // Parse the date to get the seconds.
- const parsedDate = new Date(date);
- const seconds = Math.floor(parsedDate.getTime() / 1000);
- return { seconds, nanos };
- }
- else {
- // TODO(b/37282237): Use strings for Proto3 timestamps
- // assert(!this.options.useProto3Json,
- // 'The timestamp instance format requires Proto JS.');
- const seconds = normalizeNumber(date.seconds);
- const nanos = normalizeNumber(date.nanos);
- return { seconds, nanos };
- }
- }
- /**
- * Converts the possible Proto types for numbers into a JavaScript number.
- * Returns 0 if the value is not numeric.
- */
- function normalizeNumber(value) {
- // TODO(bjornick): Handle int64 greater than 53 bits.
- if (typeof value === 'number') {
- return value;
- }
- else if (typeof value === 'string') {
- return Number(value);
- }
- else {
- return 0;
- }
- }
- /** Converts the possible Proto types for Blobs into a ByteString. */
- function normalizeByteString(blob) {
- if (typeof blob === 'string') {
- return ByteString.fromBase64String(blob);
- }
- else {
- return ByteString.fromUint8Array(blob);
- }
- }
-
- /**
- * @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 earliest date supported by Firestore timestamps (0001-01-01T00:00:00Z).
- const MIN_SECONDS = -62135596800;
- // Number of nanoseconds in a millisecond.
- const MS_TO_NANOS = 1e6;
- /**
- * A `Timestamp` represents a point in time independent of any time zone or
- * calendar, represented as seconds and fractions of seconds at nanosecond
- * resolution in UTC Epoch time.
- *
- * It is encoded using the Proleptic Gregorian Calendar which extends the
- * Gregorian calendar backwards to year one. It is encoded assuming all minutes
- * are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second
- * table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to
- * 9999-12-31T23:59:59.999999999Z.
- *
- * For examples and further specifications, refer to the
- * {@link https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto | Timestamp definition}.
- */
- class Timestamp {
- /**
- * Creates a new timestamp.
- *
- * @param seconds - The number of seconds of UTC time since Unix epoch
- * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
- * 9999-12-31T23:59:59Z inclusive.
- * @param nanoseconds - The non-negative fractions of a second at nanosecond
- * resolution. Negative second values with fractions must still have
- * non-negative nanoseconds values that count forward in time. Must be
- * from 0 to 999,999,999 inclusive.
- */
- constructor(
- /**
- * The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z.
- */
- seconds,
- /**
- * The fractions of a second at nanosecond resolution.*
- */
- nanoseconds) {
- this.seconds = seconds;
- this.nanoseconds = nanoseconds;
- if (nanoseconds < 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Timestamp nanoseconds out of range: ' + nanoseconds);
- }
- if (nanoseconds >= 1e9) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Timestamp nanoseconds out of range: ' + nanoseconds);
- }
- if (seconds < MIN_SECONDS) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Timestamp seconds out of range: ' + seconds);
- }
- // This will break in the year 10,000.
- if (seconds >= 253402300800) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Timestamp seconds out of range: ' + seconds);
- }
- }
- /**
- * Creates a new timestamp with the current date, with millisecond precision.
- *
- * @returns a new timestamp representing the current date.
- */
- static now() {
- return Timestamp.fromMillis(Date.now());
- }
- /**
- * Creates a new timestamp from the given date.
- *
- * @param date - The date to initialize the `Timestamp` from.
- * @returns A new `Timestamp` representing the same point in time as the given
- * date.
- */
- static fromDate(date) {
- return Timestamp.fromMillis(date.getTime());
- }
- /**
- * Creates a new timestamp from the given number of milliseconds.
- *
- * @param milliseconds - Number of milliseconds since Unix epoch
- * 1970-01-01T00:00:00Z.
- * @returns A new `Timestamp` representing the same point in time as the given
- * number of milliseconds.
- */
- static fromMillis(milliseconds) {
- const seconds = Math.floor(milliseconds / 1000);
- const nanos = Math.floor((milliseconds - seconds * 1000) * MS_TO_NANOS);
- return new Timestamp(seconds, nanos);
- }
- /**
- * Converts a `Timestamp` to a JavaScript `Date` object. This conversion
- * causes a loss of precision since `Date` objects only support millisecond
- * precision.
- *
- * @returns JavaScript `Date` object representing the same point in time as
- * this `Timestamp`, with millisecond precision.
- */
- toDate() {
- return new Date(this.toMillis());
- }
- /**
- * Converts a `Timestamp` to a numeric timestamp (in milliseconds since
- * epoch). This operation causes a loss of precision.
- *
- * @returns The point in time corresponding to this timestamp, represented as
- * the number of milliseconds since Unix epoch 1970-01-01T00:00:00Z.
- */
- toMillis() {
- return this.seconds * 1000 + this.nanoseconds / MS_TO_NANOS;
- }
- _compareTo(other) {
- if (this.seconds === other.seconds) {
- return primitiveComparator(this.nanoseconds, other.nanoseconds);
- }
- return primitiveComparator(this.seconds, other.seconds);
- }
- /**
- * Returns true if this `Timestamp` is equal to the provided one.
- *
- * @param other - The `Timestamp` to compare against.
- * @returns true if this `Timestamp` is equal to the provided one.
- */
- isEqual(other) {
- return (other.seconds === this.seconds && other.nanoseconds === this.nanoseconds);
- }
- /** Returns a textual representation of this `Timestamp`. */
- toString() {
- return ('Timestamp(seconds=' +
- this.seconds +
- ', nanoseconds=' +
- this.nanoseconds +
- ')');
- }
- /** Returns a JSON-serializable representation of this `Timestamp`. */
- toJSON() {
- return { seconds: this.seconds, nanoseconds: this.nanoseconds };
- }
- /**
- * Converts this object to a primitive string, which allows `Timestamp` objects
- * to be compared using the `>`, `<=`, `>=` and `>` operators.
- */
- valueOf() {
- // This method returns a string of the form <seconds>.<nanoseconds> where
- // <seconds> is translated to have a non-negative value and both <seconds>
- // and <nanoseconds> are left-padded with zeroes to be a consistent length.
- // Strings with this format then have a lexiographical ordering that matches
- // the expected ordering. The <seconds> translation is done to avoid having
- // a leading negative sign (i.e. a leading '-' character) in its string
- // representation, which would affect its lexiographical ordering.
- const adjustedSeconds = this.seconds - MIN_SECONDS;
- // Note: Up to 12 decimal digits are required to represent all valid
- // 'seconds' values.
- const formattedSeconds = String(adjustedSeconds).padStart(12, '0');
- const formattedNanoseconds = String(this.nanoseconds).padStart(9, '0');
- return formattedSeconds + '.' + formattedNanoseconds;
- }
- }
-
- /**
- * @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.
- */
- /**
- * Represents a locally-applied ServerTimestamp.
- *
- * Server Timestamps are backed by MapValues that contain an internal field
- * `__type__` with a value of `server_timestamp`. The previous value and local
- * write time are stored in its `__previous_value__` and `__local_write_time__`
- * fields respectively.
- *
- * Notes:
- * - ServerTimestampValue instances are created as the result of applying a
- * transform. They can only exist in the local view of a document. Therefore
- * they do not need to be parsed or serialized.
- * - When evaluated locally (e.g. for snapshot.data()), they by default
- * evaluate to `null`. This behavior can be configured by passing custom
- * FieldValueOptions to value().
- * - With respect to other ServerTimestampValues, they sort by their
- * localWriteTime.
- */
- const SERVER_TIMESTAMP_SENTINEL = 'server_timestamp';
- const TYPE_KEY = '__type__';
- const PREVIOUS_VALUE_KEY = '__previous_value__';
- const LOCAL_WRITE_TIME_KEY = '__local_write_time__';
- function isServerTimestamp(value) {
- var _a, _b;
- const type = (_b = (((_a = value === null || value === void 0 ? void 0 : value.mapValue) === null || _a === void 0 ? void 0 : _a.fields) || {})[TYPE_KEY]) === null || _b === void 0 ? void 0 : _b.stringValue;
- return type === SERVER_TIMESTAMP_SENTINEL;
- }
- /**
- * Returns the value of the field before this ServerTimestamp was set.
- *
- * Preserving the previous values allows the user to display the last resoled
- * value until the backend responds with the timestamp.
- */
- function getPreviousValue(value) {
- const previousValue = value.mapValue.fields[PREVIOUS_VALUE_KEY];
- if (isServerTimestamp(previousValue)) {
- return getPreviousValue(previousValue);
- }
- return previousValue;
- }
- /**
- * Returns the local time at which this timestamp was first set.
- */
- function getLocalWriteTime(value) {
- const localWriteTime = normalizeTimestamp(value.mapValue.fields[LOCAL_WRITE_TIME_KEY].timestampValue);
- return new Timestamp(localWriteTime.seconds, localWriteTime.nanos);
- }
-
- /**
- * @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.
- */
- const MAX_VALUE_TYPE = '__max__';
- const MAX_VALUE = {
- mapValue: {
- fields: {
- '__type__': { stringValue: MAX_VALUE_TYPE }
- }
- }
- };
- /** Extracts the backend's type order for the provided value. */
- function typeOrder(value) {
- if ('nullValue' in value) {
- return 0 /* TypeOrder.NullValue */;
- }
- else if ('booleanValue' in value) {
- return 1 /* TypeOrder.BooleanValue */;
- }
- else if ('integerValue' in value || 'doubleValue' in value) {
- return 2 /* TypeOrder.NumberValue */;
- }
- else if ('timestampValue' in value) {
- return 3 /* TypeOrder.TimestampValue */;
- }
- else if ('stringValue' in value) {
- return 5 /* TypeOrder.StringValue */;
- }
- else if ('bytesValue' in value) {
- return 6 /* TypeOrder.BlobValue */;
- }
- else if ('referenceValue' in value) {
- return 7 /* TypeOrder.RefValue */;
- }
- else if ('geoPointValue' in value) {
- return 8 /* TypeOrder.GeoPointValue */;
- }
- else if ('arrayValue' in value) {
- return 9 /* TypeOrder.ArrayValue */;
- }
- else if ('mapValue' in value) {
- if (isServerTimestamp(value)) {
- return 4 /* TypeOrder.ServerTimestampValue */;
- }
- else if (isMaxValue(value)) {
- return 9007199254740991 /* TypeOrder.MaxValue */;
- }
- return 10 /* TypeOrder.ObjectValue */;
- }
- else {
- return fail();
- }
- }
- /** Tests `left` and `right` for equality based on the backend semantics. */
- function valueEquals(left, right) {
- if (left === right) {
- return true;
- }
- const leftType = typeOrder(left);
- const rightType = typeOrder(right);
- if (leftType !== rightType) {
- return false;
- }
- switch (leftType) {
- case 0 /* TypeOrder.NullValue */:
- return true;
- case 1 /* TypeOrder.BooleanValue */:
- return left.booleanValue === right.booleanValue;
- case 4 /* TypeOrder.ServerTimestampValue */:
- return getLocalWriteTime(left).isEqual(getLocalWriteTime(right));
- case 3 /* TypeOrder.TimestampValue */:
- return timestampEquals(left, right);
- case 5 /* TypeOrder.StringValue */:
- return left.stringValue === right.stringValue;
- case 6 /* TypeOrder.BlobValue */:
- return blobEquals(left, right);
- case 7 /* TypeOrder.RefValue */:
- return left.referenceValue === right.referenceValue;
- case 8 /* TypeOrder.GeoPointValue */:
- return geoPointEquals(left, right);
- case 2 /* TypeOrder.NumberValue */:
- return numberEquals(left, right);
- case 9 /* TypeOrder.ArrayValue */:
- return arrayEquals(left.arrayValue.values || [], right.arrayValue.values || [], valueEquals);
- case 10 /* TypeOrder.ObjectValue */:
- return objectEquals(left, right);
- case 9007199254740991 /* TypeOrder.MaxValue */:
- return true;
- default:
- return fail();
- }
- }
- function timestampEquals(left, right) {
- if (typeof left.timestampValue === 'string' &&
- typeof right.timestampValue === 'string' &&
- left.timestampValue.length === right.timestampValue.length) {
- // Use string equality for ISO 8601 timestamps
- return left.timestampValue === right.timestampValue;
- }
- const leftTimestamp = normalizeTimestamp(left.timestampValue);
- const rightTimestamp = normalizeTimestamp(right.timestampValue);
- return (leftTimestamp.seconds === rightTimestamp.seconds &&
- leftTimestamp.nanos === rightTimestamp.nanos);
- }
- function geoPointEquals(left, right) {
- return (normalizeNumber(left.geoPointValue.latitude) ===
- normalizeNumber(right.geoPointValue.latitude) &&
- normalizeNumber(left.geoPointValue.longitude) ===
- normalizeNumber(right.geoPointValue.longitude));
- }
- function blobEquals(left, right) {
- return normalizeByteString(left.bytesValue).isEqual(normalizeByteString(right.bytesValue));
- }
- function numberEquals(left, right) {
- if ('integerValue' in left && 'integerValue' in right) {
- return (normalizeNumber(left.integerValue) === normalizeNumber(right.integerValue));
- }
- else if ('doubleValue' in left && 'doubleValue' in right) {
- const n1 = normalizeNumber(left.doubleValue);
- const n2 = normalizeNumber(right.doubleValue);
- if (n1 === n2) {
- return isNegativeZero(n1) === isNegativeZero(n2);
- }
- else {
- return isNaN(n1) && isNaN(n2);
- }
- }
- return false;
- }
- function objectEquals(left, right) {
- const leftMap = left.mapValue.fields || {};
- const rightMap = right.mapValue.fields || {};
- if (objectSize(leftMap) !== objectSize(rightMap)) {
- return false;
- }
- for (const key in leftMap) {
- if (leftMap.hasOwnProperty(key)) {
- if (rightMap[key] === undefined ||
- !valueEquals(leftMap[key], rightMap[key])) {
- return false;
- }
- }
- }
- return true;
- }
- /** Returns true if the ArrayValue contains the specified element. */
- function arrayValueContains(haystack, needle) {
- return ((haystack.values || []).find(v => valueEquals(v, needle)) !== undefined);
- }
- function valueCompare(left, right) {
- if (left === right) {
- return 0;
- }
- const leftType = typeOrder(left);
- const rightType = typeOrder(right);
- if (leftType !== rightType) {
- return primitiveComparator(leftType, rightType);
- }
- switch (leftType) {
- case 0 /* TypeOrder.NullValue */:
- case 9007199254740991 /* TypeOrder.MaxValue */:
- return 0;
- case 1 /* TypeOrder.BooleanValue */:
- return primitiveComparator(left.booleanValue, right.booleanValue);
- case 2 /* TypeOrder.NumberValue */:
- return compareNumbers(left, right);
- case 3 /* TypeOrder.TimestampValue */:
- return compareTimestamps(left.timestampValue, right.timestampValue);
- case 4 /* TypeOrder.ServerTimestampValue */:
- return compareTimestamps(getLocalWriteTime(left), getLocalWriteTime(right));
- case 5 /* TypeOrder.StringValue */:
- return primitiveComparator(left.stringValue, right.stringValue);
- case 6 /* TypeOrder.BlobValue */:
- return compareBlobs(left.bytesValue, right.bytesValue);
- case 7 /* TypeOrder.RefValue */:
- return compareReferences(left.referenceValue, right.referenceValue);
- case 8 /* TypeOrder.GeoPointValue */:
- return compareGeoPoints(left.geoPointValue, right.geoPointValue);
- case 9 /* TypeOrder.ArrayValue */:
- return compareArrays(left.arrayValue, right.arrayValue);
- case 10 /* TypeOrder.ObjectValue */:
- return compareMaps(left.mapValue, right.mapValue);
- default:
- throw fail();
- }
- }
- function compareNumbers(left, right) {
- const leftNumber = normalizeNumber(left.integerValue || left.doubleValue);
- const rightNumber = normalizeNumber(right.integerValue || right.doubleValue);
- if (leftNumber < rightNumber) {
- return -1;
- }
- else if (leftNumber > rightNumber) {
- return 1;
- }
- else if (leftNumber === rightNumber) {
- return 0;
- }
- else {
- // one or both are NaN.
- if (isNaN(leftNumber)) {
- return isNaN(rightNumber) ? 0 : -1;
- }
- else {
- return 1;
- }
- }
- }
- function compareTimestamps(left, right) {
- if (typeof left === 'string' &&
- typeof right === 'string' &&
- left.length === right.length) {
- return primitiveComparator(left, right);
- }
- const leftTimestamp = normalizeTimestamp(left);
- const rightTimestamp = normalizeTimestamp(right);
- const comparison = primitiveComparator(leftTimestamp.seconds, rightTimestamp.seconds);
- if (comparison !== 0) {
- return comparison;
- }
- return primitiveComparator(leftTimestamp.nanos, rightTimestamp.nanos);
- }
- function compareReferences(leftPath, rightPath) {
- const leftSegments = leftPath.split('/');
- const rightSegments = rightPath.split('/');
- for (let i = 0; i < leftSegments.length && i < rightSegments.length; i++) {
- const comparison = primitiveComparator(leftSegments[i], rightSegments[i]);
- if (comparison !== 0) {
- return comparison;
- }
- }
- return primitiveComparator(leftSegments.length, rightSegments.length);
- }
- function compareGeoPoints(left, right) {
- const comparison = primitiveComparator(normalizeNumber(left.latitude), normalizeNumber(right.latitude));
- if (comparison !== 0) {
- return comparison;
- }
- return primitiveComparator(normalizeNumber(left.longitude), normalizeNumber(right.longitude));
- }
- function compareBlobs(left, right) {
- const leftBytes = normalizeByteString(left);
- const rightBytes = normalizeByteString(right);
- return leftBytes.compareTo(rightBytes);
- }
- function compareArrays(left, right) {
- const leftArray = left.values || [];
- const rightArray = right.values || [];
- for (let i = 0; i < leftArray.length && i < rightArray.length; ++i) {
- const compare = valueCompare(leftArray[i], rightArray[i]);
- if (compare) {
- return compare;
- }
- }
- return primitiveComparator(leftArray.length, rightArray.length);
- }
- function compareMaps(left, right) {
- if (left === MAX_VALUE.mapValue && right === MAX_VALUE.mapValue) {
- return 0;
- }
- else if (left === MAX_VALUE.mapValue) {
- return 1;
- }
- else if (right === MAX_VALUE.mapValue) {
- return -1;
- }
- const leftMap = left.fields || {};
- const leftKeys = Object.keys(leftMap);
- const rightMap = right.fields || {};
- const rightKeys = Object.keys(rightMap);
- // Even though MapValues are likely sorted correctly based on their insertion
- // order (e.g. when received from the backend), local modifications can bring
- // elements out of order. We need to re-sort the elements to ensure that
- // canonical IDs are independent of insertion order.
- leftKeys.sort();
- rightKeys.sort();
- for (let i = 0; i < leftKeys.length && i < rightKeys.length; ++i) {
- const keyCompare = primitiveComparator(leftKeys[i], rightKeys[i]);
- if (keyCompare !== 0) {
- return keyCompare;
- }
- const compare = valueCompare(leftMap[leftKeys[i]], rightMap[rightKeys[i]]);
- if (compare !== 0) {
- return compare;
- }
- }
- return primitiveComparator(leftKeys.length, rightKeys.length);
- }
- /** Returns a reference value for the provided database and key. */
- function refValue(databaseId, key) {
- return {
- referenceValue: `projects/${databaseId.projectId}/databases/${databaseId.database}/documents/${key.path.canonicalString()}`
- };
- }
- /** Returns true if `value` is an ArrayValue. */
- function isArray(value) {
- return !!value && 'arrayValue' in value;
- }
- /** Returns true if `value` is a NullValue. */
- function isNullValue(value) {
- return !!value && 'nullValue' in value;
- }
- /** Returns true if `value` is NaN. */
- function isNanValue(value) {
- return !!value && 'doubleValue' in value && isNaN(Number(value.doubleValue));
- }
- /** Returns true if `value` is a MapValue. */
- function isMapValue(value) {
- return !!value && 'mapValue' in value;
- }
- /** Creates a deep copy of `source`. */
- function deepClone(source) {
- if (source.geoPointValue) {
- return { geoPointValue: Object.assign({}, source.geoPointValue) };
- }
- else if (source.timestampValue &&
- typeof source.timestampValue === 'object') {
- return { timestampValue: Object.assign({}, source.timestampValue) };
- }
- else if (source.mapValue) {
- const target = { mapValue: { fields: {} } };
- forEach(source.mapValue.fields, (key, val) => (target.mapValue.fields[key] = deepClone(val)));
- return target;
- }
- else if (source.arrayValue) {
- const target = { arrayValue: { values: [] } };
- for (let i = 0; i < (source.arrayValue.values || []).length; ++i) {
- target.arrayValue.values[i] = deepClone(source.arrayValue.values[i]);
- }
- return target;
- }
- else {
- return Object.assign({}, source);
- }
- }
- /** Returns true if the Value represents the canonical {@link #MAX_VALUE} . */
- function isMaxValue(value) {
- return ((((value.mapValue || {}).fields || {})['__type__'] || {}).stringValue ===
- MAX_VALUE_TYPE);
- }
-
- /**
- * @license
- * Copyright 2022 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.
- */
- /**
- * Represents a bound of a query.
- *
- * The bound is specified with the given components representing a position and
- * whether it's just before or just after the position (relative to whatever the
- * query order is).
- *
- * The position represents a logical index position for a query. It's a prefix
- * of values for the (potentially implicit) order by clauses of a query.
- *
- * Bound provides a function to determine whether a document comes before or
- * after a bound. This is influenced by whether the position is just before or
- * just after the provided values.
- */
- class Bound {
- constructor(position, inclusive) {
- this.position = position;
- this.inclusive = inclusive;
- }
- }
- function boundEquals(left, right) {
- if (left === null) {
- return right === null;
- }
- else if (right === null) {
- return false;
- }
- if (left.inclusive !== right.inclusive ||
- left.position.length !== right.position.length) {
- return false;
- }
- for (let i = 0; i < left.position.length; i++) {
- const leftPosition = left.position[i];
- const rightPosition = right.position[i];
- if (!valueEquals(leftPosition, rightPosition)) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * @license
- * Copyright 2022 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 Filter {
- }
- class FieldFilter extends Filter {
- constructor(field, op, value) {
- super();
- this.field = field;
- this.op = op;
- this.value = value;
- }
- /**
- * Creates a filter based on the provided arguments.
- */
- static create(field, op, value) {
- if (field.isKeyField()) {
- if (op === "in" /* Operator.IN */ || op === "not-in" /* Operator.NOT_IN */) {
- return this.createKeyFieldInFilter(field, op, value);
- }
- else {
- return new KeyFieldFilter(field, op, value);
- }
- }
- else if (op === "array-contains" /* Operator.ARRAY_CONTAINS */) {
- return new ArrayContainsFilter(field, value);
- }
- else if (op === "in" /* Operator.IN */) {
- return new InFilter(field, value);
- }
- else if (op === "not-in" /* Operator.NOT_IN */) {
- return new NotInFilter(field, value);
- }
- else if (op === "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */) {
- return new ArrayContainsAnyFilter(field, value);
- }
- else {
- return new FieldFilter(field, op, value);
- }
- }
- static createKeyFieldInFilter(field, op, value) {
- return op === "in" /* Operator.IN */
- ? new KeyFieldInFilter(field, value)
- : new KeyFieldNotInFilter(field, value);
- }
- matches(doc) {
- const other = doc.data.field(this.field);
- // Types do not have to match in NOT_EQUAL filters.
- if (this.op === "!=" /* Operator.NOT_EQUAL */) {
- return (other !== null &&
- this.matchesComparison(valueCompare(other, this.value)));
- }
- // Only compare types with matching backend order (such as double and int).
- return (other !== null &&
- typeOrder(this.value) === typeOrder(other) &&
- this.matchesComparison(valueCompare(other, this.value)));
- }
- matchesComparison(comparison) {
- switch (this.op) {
- case "<" /* Operator.LESS_THAN */:
- return comparison < 0;
- case "<=" /* Operator.LESS_THAN_OR_EQUAL */:
- return comparison <= 0;
- case "==" /* Operator.EQUAL */:
- return comparison === 0;
- case "!=" /* Operator.NOT_EQUAL */:
- return comparison !== 0;
- case ">" /* Operator.GREATER_THAN */:
- return comparison > 0;
- case ">=" /* Operator.GREATER_THAN_OR_EQUAL */:
- return comparison >= 0;
- default:
- return fail();
- }
- }
- isInequality() {
- return ([
- "<" /* Operator.LESS_THAN */,
- "<=" /* Operator.LESS_THAN_OR_EQUAL */,
- ">" /* Operator.GREATER_THAN */,
- ">=" /* Operator.GREATER_THAN_OR_EQUAL */,
- "!=" /* Operator.NOT_EQUAL */,
- "not-in" /* Operator.NOT_IN */
- ].indexOf(this.op) >= 0);
- }
- getFlattenedFilters() {
- return [this];
- }
- getFilters() {
- return [this];
- }
- getFirstInequalityField() {
- if (this.isInequality()) {
- return this.field;
- }
- return null;
- }
- }
- class CompositeFilter extends Filter {
- constructor(filters, op) {
- super();
- this.filters = filters;
- this.op = op;
- this.memoizedFlattenedFilters = null;
- }
- /**
- * Creates a filter based on the provided arguments.
- */
- static create(filters, op) {
- return new CompositeFilter(filters, op);
- }
- matches(doc) {
- if (compositeFilterIsConjunction(this)) {
- // For conjunctions, all filters must match, so return false if any filter doesn't match.
- return this.filters.find(filter => !filter.matches(doc)) === undefined;
- }
- else {
- // For disjunctions, at least one filter should match.
- return this.filters.find(filter => filter.matches(doc)) !== undefined;
- }
- }
- getFlattenedFilters() {
- if (this.memoizedFlattenedFilters !== null) {
- return this.memoizedFlattenedFilters;
- }
- this.memoizedFlattenedFilters = this.filters.reduce((result, subfilter) => {
- return result.concat(subfilter.getFlattenedFilters());
- }, []);
- return this.memoizedFlattenedFilters;
- }
- // Returns a mutable copy of `this.filters`
- getFilters() {
- return Object.assign([], this.filters);
- }
- getFirstInequalityField() {
- const found = this.findFirstMatchingFilter(filter => filter.isInequality());
- if (found !== null) {
- return found.field;
- }
- return null;
- }
- // Performs a depth-first search to find and return the first FieldFilter in the composite filter
- // that satisfies the predicate. Returns `null` if none of the FieldFilters satisfy the
- // predicate.
- findFirstMatchingFilter(predicate) {
- for (const fieldFilter of this.getFlattenedFilters()) {
- if (predicate(fieldFilter)) {
- return fieldFilter;
- }
- }
- return null;
- }
- }
- function compositeFilterIsConjunction(compositeFilter) {
- return compositeFilter.op === "and" /* CompositeOperator.AND */;
- }
- function filterEquals(f1, f2) {
- if (f1 instanceof FieldFilter) {
- return fieldFilterEquals(f1, f2);
- }
- else if (f1 instanceof CompositeFilter) {
- return compositeFilterEquals(f1, f2);
- }
- else {
- fail();
- }
- }
- function fieldFilterEquals(f1, f2) {
- return (f2 instanceof FieldFilter &&
- f1.op === f2.op &&
- f1.field.isEqual(f2.field) &&
- valueEquals(f1.value, f2.value));
- }
- function compositeFilterEquals(f1, f2) {
- if (f2 instanceof CompositeFilter &&
- f1.op === f2.op &&
- f1.filters.length === f2.filters.length) {
- const subFiltersMatch = f1.filters.reduce((result, f1Filter, index) => result && filterEquals(f1Filter, f2.filters[index]), true);
- return subFiltersMatch;
- }
- return false;
- }
- /** Filter that matches on key fields (i.e. '__name__'). */
- class KeyFieldFilter extends FieldFilter {
- constructor(field, op, value) {
- super(field, op, value);
- this.key = DocumentKey.fromName(value.referenceValue);
- }
- matches(doc) {
- const comparison = DocumentKey.comparator(doc.key, this.key);
- return this.matchesComparison(comparison);
- }
- }
- /** Filter that matches on key fields within an array. */
- class KeyFieldInFilter extends FieldFilter {
- constructor(field, value) {
- super(field, "in" /* Operator.IN */, value);
- this.keys = extractDocumentKeysFromArrayValue("in" /* Operator.IN */, value);
- }
- matches(doc) {
- return this.keys.some(key => key.isEqual(doc.key));
- }
- }
- /** Filter that matches on key fields not present within an array. */
- class KeyFieldNotInFilter extends FieldFilter {
- constructor(field, value) {
- super(field, "not-in" /* Operator.NOT_IN */, value);
- this.keys = extractDocumentKeysFromArrayValue("not-in" /* Operator.NOT_IN */, value);
- }
- matches(doc) {
- return !this.keys.some(key => key.isEqual(doc.key));
- }
- }
- function extractDocumentKeysFromArrayValue(op, value) {
- var _a;
- return (((_a = value.arrayValue) === null || _a === void 0 ? void 0 : _a.values) || []).map(v => {
- return DocumentKey.fromName(v.referenceValue);
- });
- }
- /** A Filter that implements the array-contains operator. */
- class ArrayContainsFilter extends FieldFilter {
- constructor(field, value) {
- super(field, "array-contains" /* Operator.ARRAY_CONTAINS */, value);
- }
- matches(doc) {
- const other = doc.data.field(this.field);
- return isArray(other) && arrayValueContains(other.arrayValue, this.value);
- }
- }
- /** A Filter that implements the IN operator. */
- class InFilter extends FieldFilter {
- constructor(field, value) {
- super(field, "in" /* Operator.IN */, value);
- }
- matches(doc) {
- const other = doc.data.field(this.field);
- return other !== null && arrayValueContains(this.value.arrayValue, other);
- }
- }
- /** A Filter that implements the not-in operator. */
- class NotInFilter extends FieldFilter {
- constructor(field, value) {
- super(field, "not-in" /* Operator.NOT_IN */, value);
- }
- matches(doc) {
- if (arrayValueContains(this.value.arrayValue, { nullValue: 'NULL_VALUE' })) {
- return false;
- }
- const other = doc.data.field(this.field);
- return other !== null && !arrayValueContains(this.value.arrayValue, other);
- }
- }
- /** A Filter that implements the array-contains-any operator. */
- class ArrayContainsAnyFilter extends FieldFilter {
- constructor(field, value) {
- super(field, "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */, value);
- }
- matches(doc) {
- const other = doc.data.field(this.field);
- if (!isArray(other) || !other.arrayValue.values) {
- return false;
- }
- return other.arrayValue.values.some(val => arrayValueContains(this.value.arrayValue, val));
- }
- }
-
- /**
- * @license
- * Copyright 2022 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 ordering on a field, in some Direction. Direction defaults to ASCENDING.
- */
- class OrderBy {
- constructor(field, dir = "asc" /* Direction.ASCENDING */) {
- this.field = field;
- this.dir = dir;
- }
- }
- function orderByEquals(left, right) {
- return left.dir === right.dir && left.field.isEqual(right.field);
- }
-
- /**
- * @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 version of a document in Firestore. This corresponds to the version
- * timestamp, such as update_time or read_time.
- */
- class SnapshotVersion {
- constructor(timestamp) {
- this.timestamp = timestamp;
- }
- static fromTimestamp(value) {
- return new SnapshotVersion(value);
- }
- static min() {
- return new SnapshotVersion(new Timestamp(0, 0));
- }
- static max() {
- return new SnapshotVersion(new Timestamp(253402300799, 1e9 - 1));
- }
- compareTo(other) {
- return this.timestamp._compareTo(other.timestamp);
- }
- isEqual(other) {
- return this.timestamp.isEqual(other.timestamp);
- }
- /** Returns a number representation of the version for use in spec tests. */
- toMicroseconds() {
- // Convert to microseconds.
- return this.timestamp.seconds * 1e6 + this.timestamp.nanoseconds / 1000;
- }
- toString() {
- return 'SnapshotVersion(' + this.timestamp.toString() + ')';
- }
- toTimestamp() {
- return this.timestamp;
- }
- }
-
- /**
- * @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 immutable sorted map implementation, based on a Left-leaning Red-Black
- // tree.
- class SortedMap {
- constructor(comparator, root) {
- this.comparator = comparator;
- this.root = root ? root : LLRBNode.EMPTY;
- }
- // Returns a copy of the map, with the specified key/value added or replaced.
- insert(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.
- remove(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.
- get(key) {
- let node = this.root;
- while (!node.isEmpty()) {
- const 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 index of the element in this sorted map, or -1 if it doesn't
- // exist.
- indexOf(key) {
- // Number of nodes that were pruned when descending right
- let prunedNodes = 0;
- let node = this.root;
- while (!node.isEmpty()) {
- const cmp = this.comparator(key, node.key);
- if (cmp === 0) {
- return prunedNodes + node.left.size;
- }
- else if (cmp < 0) {
- node = node.left;
- }
- else {
- // Count all nodes left of the node plus the node itself
- prunedNodes += node.left.size + 1;
- node = node.right;
- }
- }
- // Node not found
- return -1;
- }
- isEmpty() {
- return this.root.isEmpty();
- }
- // Returns the total number of nodes in the map.
- get size() {
- return this.root.size;
- }
- // Returns the minimum key in the map.
- minKey() {
- return this.root.minKey();
- }
- // Returns the maximum key in the map.
- maxKey() {
- return this.root.maxKey();
- }
- // Traverses the map in key order and calls the specified action function
- // 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.
- inorderTraversal(action) {
- return this.root.inorderTraversal(action);
- }
- forEach(fn) {
- this.inorderTraversal((k, v) => {
- fn(k, v);
- return false;
- });
- }
- toString() {
- const descriptions = [];
- this.inorderTraversal((k, v) => {
- descriptions.push(`${k}:${v}`);
- return false;
- });
- return `{${descriptions.join(', ')}}`;
- }
- // Traverses the map in reverse key order and calls the specified action
- // function 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.
- reverseTraversal(action) {
- return this.root.reverseTraversal(action);
- }
- // Returns an iterator over the SortedMap.
- getIterator() {
- return new SortedMapIterator(this.root, null, this.comparator, false);
- }
- getIteratorFrom(key) {
- return new SortedMapIterator(this.root, key, this.comparator, false);
- }
- getReverseIterator() {
- return new SortedMapIterator(this.root, null, this.comparator, true);
- }
- getReverseIteratorFrom(key) {
- return new SortedMapIterator(this.root, key, this.comparator, true);
- }
- } // end SortedMap
- // An iterator over an LLRBNode.
- class SortedMapIterator {
- constructor(node, startKey, comparator, isReverse) {
- this.isReverse = isReverse;
- this.nodeStack = [];
- let cmp = 1;
- while (!node.isEmpty()) {
- cmp = startKey ? comparator(node.key, startKey) : 1;
- // flip the comparison if we're going in reverse
- if (startKey && 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;
- }
- }
- }
- }
- getNext() {
- let node = this.nodeStack.pop();
- const 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;
- }
- hasNext() {
- return this.nodeStack.length > 0;
- }
- peek() {
- if (this.nodeStack.length === 0) {
- return null;
- }
- const node = this.nodeStack[this.nodeStack.length - 1];
- return { key: node.key, value: node.value };
- }
- } // end SortedMapIterator
- // Represents a node in a Left-leaning Red-Black tree.
- class LLRBNode {
- constructor(key, value, color, left, right) {
- this.key = key;
- this.value = value;
- this.color = color != null ? color : LLRBNode.RED;
- this.left = left != null ? left : LLRBNode.EMPTY;
- this.right = right != null ? right : LLRBNode.EMPTY;
- this.size = this.left.size + 1 + this.right.size;
- }
- // Returns a copy of the current node, optionally replacing pieces of it.
- copy(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);
- }
- isEmpty() {
- return false;
- }
- // Traverses the tree in key order and calls the specified action function
- // for each node. If action returns true, traversal is aborted.
- // Returns the first truthy value returned by action, or the last falsey
- // value returned by action.
- inorderTraversal(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. If action returns true, traversal is aborted.
- // Returns the first truthy value returned by action, or the last falsey
- // value returned by action.
- reverseTraversal(action) {
- return (this.right.reverseTraversal(action) ||
- action(this.key, this.value) ||
- this.left.reverseTraversal(action));
- }
- // Returns the minimum node in the tree.
- min() {
- if (this.left.isEmpty()) {
- return this;
- }
- else {
- return this.left.min();
- }
- }
- // Returns the maximum key in the tree.
- minKey() {
- return this.min().key;
- }
- // Returns the maximum key in the tree.
- maxKey() {
- if (this.right.isEmpty()) {
- return this.key;
- }
- else {
- return this.right.maxKey();
- }
- }
- // Returns new tree, with the key/value added.
- insert(key, value, comparator) {
- let n = this;
- const 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();
- }
- removeMin() {
- if (this.left.isEmpty()) {
- return LLRBNode.EMPTY;
- }
- let 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();
- }
- // Returns new tree, with the specified item removed.
- remove(key, comparator) {
- let smallest;
- let 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 LLRBNode.EMPTY;
- }
- 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();
- }
- isRed() {
- return this.color;
- }
- // Returns new tree after performing any needed rotations.
- fixUp() {
- let 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;
- }
- moveRedLeft() {
- let 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;
- }
- moveRedRight() {
- let n = this.colorFlip();
- if (n.left.left.isRed()) {
- n = n.rotateRight();
- n = n.colorFlip();
- }
- return n;
- }
- rotateLeft() {
- const nl = this.copy(null, null, LLRBNode.RED, null, this.right.left);
- return this.right.copy(null, null, this.color, nl, null);
- }
- rotateRight() {
- const nr = this.copy(null, null, LLRBNode.RED, this.left.right, null);
- return this.left.copy(null, null, this.color, null, nr);
- }
- colorFlip() {
- const left = this.left.copy(null, null, !this.left.color, null, null);
- const right = this.right.copy(null, null, !this.right.color, null, null);
- return this.copy(null, null, !this.color, left, right);
- }
- // For testing.
- checkMaxDepth() {
- const blackDepth = this.check();
- if (Math.pow(2.0, blackDepth) <= this.size + 1) {
- return true;
- }
- else {
- return false;
- }
- }
- // In a balanced RB tree, the black-depth (number of black nodes) from root to
- // leaves is equal on both sides. This function verifies that or asserts.
- check() {
- if (this.isRed() && this.left.isRed()) {
- throw fail();
- }
- if (this.right.isRed()) {
- throw fail();
- }
- const blackDepth = this.left.check();
- if (blackDepth !== this.right.check()) {
- throw fail();
- }
- else {
- return blackDepth + (this.isRed() ? 0 : 1);
- }
- }
- } // end LLRBNode
- // Empty node is shared between all LLRB trees.
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- LLRBNode.EMPTY = null;
- LLRBNode.RED = true;
- LLRBNode.BLACK = false;
- // Represents an empty node (a leaf node in the Red-Black Tree).
- class LLRBEmptyNode {
- constructor() {
- this.size = 0;
- }
- get key() {
- throw fail();
- }
- get value() {
- throw fail();
- }
- get color() {
- throw fail();
- }
- get left() {
- throw fail();
- }
- get right() {
- throw fail();
- }
- // Returns a copy of the current node.
- copy(key, value, color, left, right) {
- return this;
- }
- // Returns a copy of the tree, with the specified key/value added.
- insert(key, value, comparator) {
- return new LLRBNode(key, value);
- }
- // Returns a copy of the tree, with the specified key removed.
- remove(key, comparator) {
- return this;
- }
- isEmpty() {
- return true;
- }
- inorderTraversal(action) {
- return false;
- }
- reverseTraversal(action) {
- return false;
- }
- minKey() {
- return null;
- }
- maxKey() {
- return null;
- }
- isRed() {
- return false;
- }
- // For testing.
- checkMaxDepth() {
- return true;
- }
- check() {
- return 0;
- }
- } // end LLRBEmptyNode
- LLRBNode.EMPTY = new LLRBEmptyNode();
-
- /**
- * @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.
- */
- /**
- * SortedSet is an immutable (copy-on-write) collection that holds elements
- * in order specified by the provided comparator.
- *
- * NOTE: if provided comparator returns 0 for two elements, we consider them to
- * be equal!
- */
- class SortedSet {
- constructor(comparator) {
- this.comparator = comparator;
- this.data = new SortedMap(this.comparator);
- }
- has(elem) {
- return this.data.get(elem) !== null;
- }
- first() {
- return this.data.minKey();
- }
- last() {
- return this.data.maxKey();
- }
- get size() {
- return this.data.size;
- }
- indexOf(elem) {
- return this.data.indexOf(elem);
- }
- /** Iterates elements in order defined by "comparator" */
- forEach(cb) {
- this.data.inorderTraversal((k, v) => {
- cb(k);
- return false;
- });
- }
- /** Iterates over `elem`s such that: range[0] <= elem < range[1]. */
- forEachInRange(range, cb) {
- const iter = this.data.getIteratorFrom(range[0]);
- while (iter.hasNext()) {
- const elem = iter.getNext();
- if (this.comparator(elem.key, range[1]) >= 0) {
- return;
- }
- cb(elem.key);
- }
- }
- /**
- * Iterates over `elem`s such that: start <= elem until false is returned.
- */
- forEachWhile(cb, start) {
- let iter;
- if (start !== undefined) {
- iter = this.data.getIteratorFrom(start);
- }
- else {
- iter = this.data.getIterator();
- }
- while (iter.hasNext()) {
- const elem = iter.getNext();
- const result = cb(elem.key);
- if (!result) {
- return;
- }
- }
- }
- /** Finds the least element greater than or equal to `elem`. */
- firstAfterOrEqual(elem) {
- const iter = this.data.getIteratorFrom(elem);
- return iter.hasNext() ? iter.getNext().key : null;
- }
- getIterator() {
- return new SortedSetIterator(this.data.getIterator());
- }
- getIteratorFrom(key) {
- return new SortedSetIterator(this.data.getIteratorFrom(key));
- }
- /** Inserts or updates an element */
- add(elem) {
- return this.copy(this.data.remove(elem).insert(elem, true));
- }
- /** Deletes an element */
- delete(elem) {
- if (!this.has(elem)) {
- return this;
- }
- return this.copy(this.data.remove(elem));
- }
- isEmpty() {
- return this.data.isEmpty();
- }
- unionWith(other) {
- let result = this;
- // Make sure `result` always refers to the larger one of the two sets.
- if (result.size < other.size) {
- result = other;
- other = this;
- }
- other.forEach(elem => {
- result = result.add(elem);
- });
- return result;
- }
- isEqual(other) {
- if (!(other instanceof SortedSet)) {
- return false;
- }
- if (this.size !== other.size) {
- return false;
- }
- const thisIt = this.data.getIterator();
- const otherIt = other.data.getIterator();
- while (thisIt.hasNext()) {
- const thisElem = thisIt.getNext().key;
- const otherElem = otherIt.getNext().key;
- if (this.comparator(thisElem, otherElem) !== 0) {
- return false;
- }
- }
- return true;
- }
- toArray() {
- const res = [];
- this.forEach(targetId => {
- res.push(targetId);
- });
- return res;
- }
- toString() {
- const result = [];
- this.forEach(elem => result.push(elem));
- return 'SortedSet(' + result.toString() + ')';
- }
- copy(data) {
- const result = new SortedSet(this.comparator);
- result.data = data;
- return result;
- }
- }
- class SortedSetIterator {
- constructor(iter) {
- this.iter = iter;
- }
- getNext() {
- return this.iter.getNext().key;
- }
- hasNext() {
- return this.iter.hasNext();
- }
- }
-
- /**
- * @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.
- */
- /**
- * Provides a set of fields that can be used to partially patch a document.
- * FieldMask is used in conjunction with ObjectValue.
- * Examples:
- * foo - Overwrites foo entirely with the provided value. If foo is not
- * present in the companion ObjectValue, the field is deleted.
- * foo.bar - Overwrites only the field bar of the object foo.
- * If foo is not an object, foo is replaced with an object
- * containing foo
- */
- class FieldMask {
- constructor(fields) {
- this.fields = fields;
- // TODO(dimond): validation of FieldMask
- // Sort the field mask to support `FieldMask.isEqual()` and assert below.
- fields.sort(FieldPath$1.comparator);
- }
- static empty() {
- return new FieldMask([]);
- }
- /**
- * Returns a new FieldMask object that is the result of adding all the given
- * fields paths to this field mask.
- */
- unionWith(extraFields) {
- let mergedMaskSet = new SortedSet(FieldPath$1.comparator);
- for (const fieldPath of this.fields) {
- mergedMaskSet = mergedMaskSet.add(fieldPath);
- }
- for (const fieldPath of extraFields) {
- mergedMaskSet = mergedMaskSet.add(fieldPath);
- }
- return new FieldMask(mergedMaskSet.toArray());
- }
- /**
- * Verifies that `fieldPath` is included by at least one field in this field
- * mask.
- *
- * This is an O(n) operation, where `n` is the size of the field mask.
- */
- covers(fieldPath) {
- for (const fieldMaskPath of this.fields) {
- if (fieldMaskPath.isPrefixOf(fieldPath)) {
- return true;
- }
- }
- return false;
- }
- isEqual(other) {
- return arrayEquals(this.fields, other.fields, (l, r) => l.isEqual(r));
- }
- }
-
- /**
- * @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 ObjectValue represents a MapValue in the Firestore Proto and offers the
- * ability to add and remove fields (via the ObjectValueBuilder).
- */
- class ObjectValue {
- constructor(value) {
- this.value = value;
- }
- static empty() {
- return new ObjectValue({ mapValue: {} });
- }
- /**
- * Returns the value at the given path or null.
- *
- * @param path - the path to search
- * @returns The value at the path or null if the path is not set.
- */
- field(path) {
- if (path.isEmpty()) {
- return this.value;
- }
- else {
- let currentLevel = this.value;
- for (let i = 0; i < path.length - 1; ++i) {
- currentLevel = (currentLevel.mapValue.fields || {})[path.get(i)];
- if (!isMapValue(currentLevel)) {
- return null;
- }
- }
- currentLevel = (currentLevel.mapValue.fields || {})[path.lastSegment()];
- return currentLevel || null;
- }
- }
- /**
- * Sets the field to the provided value.
- *
- * @param path - The field path to set.
- * @param value - The value to set.
- */
- set(path, value) {
- const fieldsMap = this.getFieldsMap(path.popLast());
- fieldsMap[path.lastSegment()] = deepClone(value);
- }
- /**
- * Sets the provided fields to the provided values.
- *
- * @param data - A map of fields to values (or null for deletes).
- */
- setAll(data) {
- let parent = FieldPath$1.emptyPath();
- let upserts = {};
- let deletes = [];
- data.forEach((value, path) => {
- if (!parent.isImmediateParentOf(path)) {
- // Insert the accumulated changes at this parent location
- const fieldsMap = this.getFieldsMap(parent);
- this.applyChanges(fieldsMap, upserts, deletes);
- upserts = {};
- deletes = [];
- parent = path.popLast();
- }
- if (value) {
- upserts[path.lastSegment()] = deepClone(value);
- }
- else {
- deletes.push(path.lastSegment());
- }
- });
- const fieldsMap = this.getFieldsMap(parent);
- this.applyChanges(fieldsMap, upserts, deletes);
- }
- /**
- * Removes the field at the specified path. If there is no field at the
- * specified path, nothing is changed.
- *
- * @param path - The field path to remove.
- */
- delete(path) {
- const nestedValue = this.field(path.popLast());
- if (isMapValue(nestedValue) && nestedValue.mapValue.fields) {
- delete nestedValue.mapValue.fields[path.lastSegment()];
- }
- }
- isEqual(other) {
- return valueEquals(this.value, other.value);
- }
- /**
- * Returns the map that contains the leaf element of `path`. If the parent
- * entry does not yet exist, or if it is not a map, a new map will be created.
- */
- getFieldsMap(path) {
- let current = this.value;
- if (!current.mapValue.fields) {
- current.mapValue = { fields: {} };
- }
- for (let i = 0; i < path.length; ++i) {
- let next = current.mapValue.fields[path.get(i)];
- if (!isMapValue(next) || !next.mapValue.fields) {
- next = { mapValue: { fields: {} } };
- current.mapValue.fields[path.get(i)] = next;
- }
- current = next;
- }
- return current.mapValue.fields;
- }
- /**
- * Modifies `fieldsMap` by adding, replacing or deleting the specified
- * entries.
- */
- applyChanges(fieldsMap, inserts, deletes) {
- forEach(inserts, (key, val) => (fieldsMap[key] = val));
- for (const field of deletes) {
- delete fieldsMap[field];
- }
- }
- clone() {
- return new ObjectValue(deepClone(this.value));
- }
- }
-
- /**
- * @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.
- */
- /**
- * Represents a document in Firestore with a key, version, data and whether it
- * has local mutations applied to it.
- *
- * Documents can transition between states via `convertToFoundDocument()`,
- * `convertToNoDocument()` and `convertToUnknownDocument()`. If a document does
- * not transition to one of these states even after all mutations have been
- * applied, `isValidDocument()` returns false and the document should be removed
- * from all views.
- */
- class MutableDocument {
- constructor(key, documentType, version, readTime, createTime, data, documentState) {
- this.key = key;
- this.documentType = documentType;
- this.version = version;
- this.readTime = readTime;
- this.createTime = createTime;
- this.data = data;
- this.documentState = documentState;
- }
- /**
- * Creates a document with no known version or data, but which can serve as
- * base document for mutations.
- */
- static newInvalidDocument(documentKey) {
- return new MutableDocument(documentKey, 0 /* DocumentType.INVALID */,
- /* version */ SnapshotVersion.min(),
- /* readTime */ SnapshotVersion.min(),
- /* createTime */ SnapshotVersion.min(), ObjectValue.empty(), 0 /* DocumentState.SYNCED */);
- }
- /**
- * Creates a new document that is known to exist with the given data at the
- * given version.
- */
- static newFoundDocument(documentKey, version, createTime, value) {
- return new MutableDocument(documentKey, 1 /* DocumentType.FOUND_DOCUMENT */,
- /* version */ version,
- /* readTime */ SnapshotVersion.min(),
- /* createTime */ createTime, value, 0 /* DocumentState.SYNCED */);
- }
- /** Creates a new document that is known to not exist at the given version. */
- static newNoDocument(documentKey, version) {
- return new MutableDocument(documentKey, 2 /* DocumentType.NO_DOCUMENT */,
- /* version */ version,
- /* readTime */ SnapshotVersion.min(),
- /* createTime */ SnapshotVersion.min(), ObjectValue.empty(), 0 /* DocumentState.SYNCED */);
- }
- /**
- * Creates a new document that is known to exist at the given version but
- * whose data is not known (e.g. a document that was updated without a known
- * base document).
- */
- static newUnknownDocument(documentKey, version) {
- return new MutableDocument(documentKey, 3 /* DocumentType.UNKNOWN_DOCUMENT */,
- /* version */ version,
- /* readTime */ SnapshotVersion.min(),
- /* createTime */ SnapshotVersion.min(), ObjectValue.empty(), 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */);
- }
- /**
- * Changes the document type to indicate that it exists and that its version
- * and data are known.
- */
- convertToFoundDocument(version, value) {
- // If a document is switching state from being an invalid or deleted
- // document to a valid (FOUND_DOCUMENT) document, either due to receiving an
- // update from Watch or due to applying a local set mutation on top
- // of a deleted document, our best guess about its createTime would be the
- // version at which the document transitioned to a FOUND_DOCUMENT.
- if (this.createTime.isEqual(SnapshotVersion.min()) &&
- (this.documentType === 2 /* DocumentType.NO_DOCUMENT */ ||
- this.documentType === 0 /* DocumentType.INVALID */)) {
- this.createTime = version;
- }
- this.version = version;
- this.documentType = 1 /* DocumentType.FOUND_DOCUMENT */;
- this.data = value;
- this.documentState = 0 /* DocumentState.SYNCED */;
- return this;
- }
- /**
- * Changes the document type to indicate that it doesn't exist at the given
- * version.
- */
- convertToNoDocument(version) {
- this.version = version;
- this.documentType = 2 /* DocumentType.NO_DOCUMENT */;
- this.data = ObjectValue.empty();
- this.documentState = 0 /* DocumentState.SYNCED */;
- return this;
- }
- /**
- * Changes the document type to indicate that it exists at a given version but
- * that its data is not known (e.g. a document that was updated without a known
- * base document).
- */
- convertToUnknownDocument(version) {
- this.version = version;
- this.documentType = 3 /* DocumentType.UNKNOWN_DOCUMENT */;
- this.data = ObjectValue.empty();
- this.documentState = 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */;
- return this;
- }
- setHasCommittedMutations() {
- this.documentState = 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */;
- return this;
- }
- setHasLocalMutations() {
- this.documentState = 1 /* DocumentState.HAS_LOCAL_MUTATIONS */;
- this.version = SnapshotVersion.min();
- return this;
- }
- setReadTime(readTime) {
- this.readTime = readTime;
- return this;
- }
- get hasLocalMutations() {
- return this.documentState === 1 /* DocumentState.HAS_LOCAL_MUTATIONS */;
- }
- get hasCommittedMutations() {
- return this.documentState === 2 /* DocumentState.HAS_COMMITTED_MUTATIONS */;
- }
- get hasPendingWrites() {
- return this.hasLocalMutations || this.hasCommittedMutations;
- }
- isValidDocument() {
- return this.documentType !== 0 /* DocumentType.INVALID */;
- }
- isFoundDocument() {
- return this.documentType === 1 /* DocumentType.FOUND_DOCUMENT */;
- }
- isNoDocument() {
- return this.documentType === 2 /* DocumentType.NO_DOCUMENT */;
- }
- isUnknownDocument() {
- return this.documentType === 3 /* DocumentType.UNKNOWN_DOCUMENT */;
- }
- isEqual(other) {
- return (other instanceof MutableDocument &&
- this.key.isEqual(other.key) &&
- this.version.isEqual(other.version) &&
- this.documentType === other.documentType &&
- this.documentState === other.documentState &&
- this.data.isEqual(other.data));
- }
- mutableCopy() {
- return new MutableDocument(this.key, this.documentType, this.version, this.readTime, this.createTime, this.data.clone(), this.documentState);
- }
- toString() {
- return (`Document(${this.key}, ${this.version}, ${JSON.stringify(this.data.value)}, ` +
- `{createTime: ${this.createTime}}), ` +
- `{documentType: ${this.documentType}}), ` +
- `{documentState: ${this.documentState}})`);
- }
- }
-
- /**
- * @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.
- */
- // Visible for testing
- class TargetImpl {
- constructor(path, collectionGroup = null, orderBy = [], filters = [], limit = null, startAt = null, endAt = null) {
- this.path = path;
- this.collectionGroup = collectionGroup;
- this.orderBy = orderBy;
- this.filters = filters;
- this.limit = limit;
- this.startAt = startAt;
- this.endAt = endAt;
- this.memoizedCanonicalId = null;
- }
- }
- /**
- * Initializes a Target with a path and optional additional query constraints.
- * Path must currently be empty if this is a collection group query.
- *
- * NOTE: you should always construct `Target` from `Query.toTarget` instead of
- * using this factory method, because `Query` provides an implicit `orderBy`
- * property.
- */
- function newTarget(path, collectionGroup = null, orderBy = [], filters = [], limit = null, startAt = null, endAt = null) {
- return new TargetImpl(path, collectionGroup, orderBy, filters, limit, startAt, endAt);
- }
- function targetEquals(left, right) {
- if (left.limit !== right.limit) {
- return false;
- }
- if (left.orderBy.length !== right.orderBy.length) {
- return false;
- }
- for (let i = 0; i < left.orderBy.length; i++) {
- if (!orderByEquals(left.orderBy[i], right.orderBy[i])) {
- return false;
- }
- }
- if (left.filters.length !== right.filters.length) {
- return false;
- }
- for (let i = 0; i < left.filters.length; i++) {
- if (!filterEquals(left.filters[i], right.filters[i])) {
- return false;
- }
- }
- if (left.collectionGroup !== right.collectionGroup) {
- return false;
- }
- if (!left.path.isEqual(right.path)) {
- return false;
- }
- if (!boundEquals(left.startAt, right.startAt)) {
- return false;
- }
- return boundEquals(left.endAt, right.endAt);
- }
-
- /**
- * @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.
- */
- /**
- * Query encapsulates all the query attributes we support in the SDK. It can
- * be run against the LocalStore, as well as be converted to a `Target` to
- * query the RemoteStore results.
- *
- * Visible for testing.
- */
- class QueryImpl {
- /**
- * Initializes a Query with a path and optional additional query constraints.
- * Path must currently be empty if this is a collection group query.
- */
- constructor(path, collectionGroup = null, explicitOrderBy = [], filters = [], limit = null, limitType = "F" /* LimitType.First */, startAt = null, endAt = null) {
- this.path = path;
- this.collectionGroup = collectionGroup;
- this.explicitOrderBy = explicitOrderBy;
- this.filters = filters;
- this.limit = limit;
- this.limitType = limitType;
- this.startAt = startAt;
- this.endAt = endAt;
- this.memoizedOrderBy = null;
- // The corresponding `Target` of this `Query` instance.
- this.memoizedTarget = null;
- if (this.startAt) ;
- if (this.endAt) ;
- }
- }
- /** Creates a new Query for a query that matches all documents at `path` */
- function newQueryForPath(path) {
- return new QueryImpl(path);
- }
- function getFirstOrderByField(query) {
- return query.explicitOrderBy.length > 0
- ? query.explicitOrderBy[0].field
- : null;
- }
- function getInequalityFilterField(query) {
- for (const filter of query.filters) {
- const result = filter.getFirstInequalityField();
- if (result !== null) {
- return result;
- }
- }
- return null;
- }
- /**
- * Creates a new Query for a collection group query that matches all documents
- * within the provided collection group.
- */
- function newQueryForCollectionGroup(collectionId) {
- return new QueryImpl(ResourcePath.emptyPath(), collectionId);
- }
- /**
- * Returns whether the query matches a collection group rather than a specific
- * collection.
- */
- function isCollectionGroupQuery(query) {
- return query.collectionGroup !== null;
- }
- /**
- * Returns the implicit order by constraint that is used to execute the Query,
- * which can be different from the order by constraints the user provided (e.g.
- * the SDK and backend always orders by `__name__`).
- */
- function queryOrderBy(query) {
- const queryImpl = debugCast(query);
- if (queryImpl.memoizedOrderBy === null) {
- queryImpl.memoizedOrderBy = [];
- const inequalityField = getInequalityFilterField(queryImpl);
- const firstOrderByField = getFirstOrderByField(queryImpl);
- if (inequalityField !== null && firstOrderByField === null) {
- // In order to implicitly add key ordering, we must also add the
- // inequality filter field for it to be a valid query.
- // Note that the default inequality field and key ordering is ascending.
- if (!inequalityField.isKeyField()) {
- queryImpl.memoizedOrderBy.push(new OrderBy(inequalityField));
- }
- queryImpl.memoizedOrderBy.push(new OrderBy(FieldPath$1.keyField(), "asc" /* Direction.ASCENDING */));
- }
- else {
- let foundKeyOrdering = false;
- for (const orderBy of queryImpl.explicitOrderBy) {
- queryImpl.memoizedOrderBy.push(orderBy);
- if (orderBy.field.isKeyField()) {
- foundKeyOrdering = true;
- }
- }
- if (!foundKeyOrdering) {
- // The order of the implicit key ordering always matches the last
- // explicit order by
- const lastDirection = queryImpl.explicitOrderBy.length > 0
- ? queryImpl.explicitOrderBy[queryImpl.explicitOrderBy.length - 1]
- .dir
- : "asc" /* Direction.ASCENDING */;
- queryImpl.memoizedOrderBy.push(new OrderBy(FieldPath$1.keyField(), lastDirection));
- }
- }
- }
- return queryImpl.memoizedOrderBy;
- }
- /**
- * Converts this `Query` instance to it's corresponding `Target` representation.
- */
- function queryToTarget(query) {
- const queryImpl = debugCast(query);
- if (!queryImpl.memoizedTarget) {
- if (queryImpl.limitType === "F" /* LimitType.First */) {
- queryImpl.memoizedTarget = newTarget(queryImpl.path, queryImpl.collectionGroup, queryOrderBy(queryImpl), queryImpl.filters, queryImpl.limit, queryImpl.startAt, queryImpl.endAt);
- }
- else {
- // Flip the orderBy directions since we want the last results
- const orderBys = [];
- for (const orderBy of queryOrderBy(queryImpl)) {
- const dir = orderBy.dir === "desc" /* Direction.DESCENDING */
- ? "asc" /* Direction.ASCENDING */
- : "desc" /* Direction.DESCENDING */;
- orderBys.push(new OrderBy(orderBy.field, dir));
- }
- // We need to swap the cursors to match the now-flipped query ordering.
- const startAt = queryImpl.endAt
- ? new Bound(queryImpl.endAt.position, queryImpl.endAt.inclusive)
- : null;
- const endAt = queryImpl.startAt
- ? new Bound(queryImpl.startAt.position, queryImpl.startAt.inclusive)
- : null;
- // Now return as a LimitType.First query.
- queryImpl.memoizedTarget = newTarget(queryImpl.path, queryImpl.collectionGroup, orderBys, queryImpl.filters, queryImpl.limit, startAt, endAt);
- }
- }
- return queryImpl.memoizedTarget;
- }
- function queryWithAddedFilter(query, filter) {
- filter.getFirstInequalityField();
- getInequalityFilterField(query);
- const newFilters = query.filters.concat([filter]);
- return new QueryImpl(query.path, query.collectionGroup, query.explicitOrderBy.slice(), newFilters, query.limit, query.limitType, query.startAt, query.endAt);
- }
- function queryWithAddedOrderBy(query, orderBy) {
- // TODO(dimond): validate that orderBy does not list the same key twice.
- const newOrderBy = query.explicitOrderBy.concat([orderBy]);
- return new QueryImpl(query.path, query.collectionGroup, newOrderBy, query.filters.slice(), query.limit, query.limitType, query.startAt, query.endAt);
- }
- function queryWithLimit(query, limit, limitType) {
- return new QueryImpl(query.path, query.collectionGroup, query.explicitOrderBy.slice(), query.filters.slice(), limit, limitType, query.startAt, query.endAt);
- }
- function queryWithStartAt(query, bound) {
- return new QueryImpl(query.path, query.collectionGroup, query.explicitOrderBy.slice(), query.filters.slice(), query.limit, query.limitType, bound, query.endAt);
- }
- function queryWithEndAt(query, bound) {
- return new QueryImpl(query.path, query.collectionGroup, query.explicitOrderBy.slice(), query.filters.slice(), query.limit, query.limitType, query.startAt, bound);
- }
- function queryEquals(left, right) {
- return (targetEquals(queryToTarget(left), queryToTarget(right)) &&
- left.limitType === right.limitType);
- }
-
- /**
- * @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.
- */
- /**
- * Returns an DoubleValue for `value` that is encoded based the serializer's
- * `useProto3Json` setting.
- */
- function toDouble(serializer, value) {
- if (serializer.useProto3Json) {
- if (isNaN(value)) {
- return { doubleValue: 'NaN' };
- }
- else if (value === Infinity) {
- return { doubleValue: 'Infinity' };
- }
- else if (value === -Infinity) {
- return { doubleValue: '-Infinity' };
- }
- }
- return { doubleValue: isNegativeZero(value) ? '-0' : value };
- }
- /**
- * Returns an IntegerValue for `value`.
- */
- function toInteger(value) {
- return { integerValue: '' + value };
- }
- /**
- * Returns a value for a number that's appropriate to put into a proto.
- * The return value is an IntegerValue if it can safely represent the value,
- * otherwise a DoubleValue is returned.
- */
- function toNumber(serializer, value) {
- return isSafeInteger(value) ? toInteger(value) : toDouble(serializer, value);
- }
-
- /**
- * @license
- * Copyright 2018 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 to represent a field transform on a mutation. */
- class TransformOperation {
- constructor() {
- // Make sure that the structural type of `TransformOperation` is unique.
- // See https://github.com/microsoft/TypeScript/issues/5451
- this._ = undefined;
- }
- }
- /** Transforms a value into a server-generated timestamp. */
- class ServerTimestampTransform extends TransformOperation {
- }
- /** Transforms an array value via a union operation. */
- class ArrayUnionTransformOperation extends TransformOperation {
- constructor(elements) {
- super();
- this.elements = elements;
- }
- }
- /** Transforms an array value via a remove operation. */
- class ArrayRemoveTransformOperation extends TransformOperation {
- constructor(elements) {
- super();
- this.elements = elements;
- }
- }
- /**
- * Implements the backend semantics for locally computed NUMERIC_ADD (increment)
- * transforms. Converts all field values to integers or doubles, but unlike the
- * backend does not cap integer values at 2^63. Instead, JavaScript number
- * arithmetic is used and precision loss can occur for values greater than 2^53.
- */
- class NumericIncrementTransformOperation extends TransformOperation {
- constructor(serializer, operand) {
- super();
- this.serializer = serializer;
- this.operand = operand;
- }
- }
-
- /**
- * @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 field path and the TransformOperation to perform upon it. */
- class FieldTransform {
- constructor(field, transform) {
- this.field = field;
- this.transform = transform;
- }
- }
- /**
- * Encodes a precondition for a mutation. This follows the model that the
- * backend accepts with the special case of an explicit "empty" precondition
- * (meaning no precondition).
- */
- class Precondition {
- constructor(updateTime, exists) {
- this.updateTime = updateTime;
- this.exists = exists;
- }
- /** Creates a new empty Precondition. */
- static none() {
- return new Precondition();
- }
- /** Creates a new Precondition with an exists flag. */
- static exists(exists) {
- return new Precondition(undefined, exists);
- }
- /** Creates a new Precondition based on a version a document exists at. */
- static updateTime(version) {
- return new Precondition(version);
- }
- /** Returns whether this Precondition is empty. */
- get isNone() {
- return this.updateTime === undefined && this.exists === undefined;
- }
- isEqual(other) {
- return (this.exists === other.exists &&
- (this.updateTime
- ? !!other.updateTime && this.updateTime.isEqual(other.updateTime)
- : !other.updateTime));
- }
- }
- /**
- * A mutation describes a self-contained change to a document. Mutations can
- * create, replace, delete, and update subsets of documents.
- *
- * Mutations not only act on the value of the document but also its version.
- *
- * For local mutations (mutations that haven't been committed yet), we preserve
- * the existing version for Set and Patch mutations. For Delete mutations, we
- * reset the version to 0.
- *
- * Here's the expected transition table.
- *
- * MUTATION APPLIED TO RESULTS IN
- *
- * SetMutation Document(v3) Document(v3)
- * SetMutation NoDocument(v3) Document(v0)
- * SetMutation InvalidDocument(v0) Document(v0)
- * PatchMutation Document(v3) Document(v3)
- * PatchMutation NoDocument(v3) NoDocument(v3)
- * PatchMutation InvalidDocument(v0) UnknownDocument(v3)
- * DeleteMutation Document(v3) NoDocument(v0)
- * DeleteMutation NoDocument(v3) NoDocument(v0)
- * DeleteMutation InvalidDocument(v0) NoDocument(v0)
- *
- * For acknowledged mutations, we use the updateTime of the WriteResponse as
- * the resulting version for Set and Patch mutations. As deletes have no
- * explicit update time, we use the commitTime of the WriteResponse for
- * Delete mutations.
- *
- * If a mutation is acknowledged by the backend but fails the precondition check
- * locally, we transition to an `UnknownDocument` and rely on Watch to send us
- * the updated version.
- *
- * Field transforms are used only with Patch and Set Mutations. We use the
- * `updateTransforms` message to store transforms, rather than the `transforms`s
- * messages.
- *
- * ## Subclassing Notes
- *
- * Every type of mutation needs to implement its own applyToRemoteDocument() and
- * applyToLocalView() to implement the actual behavior of applying the mutation
- * to some source document (see `setMutationApplyToRemoteDocument()` for an
- * example).
- */
- class Mutation {
- }
- /**
- * A mutation that creates or replaces the document at the given key with the
- * object value contents.
- */
- class SetMutation extends Mutation {
- constructor(key, value, precondition, fieldTransforms = []) {
- super();
- this.key = key;
- this.value = value;
- this.precondition = precondition;
- this.fieldTransforms = fieldTransforms;
- this.type = 0 /* MutationType.Set */;
- }
- getFieldMask() {
- return null;
- }
- }
- /**
- * A mutation that modifies fields of the document at the given key with the
- * given values. The values are applied through a field mask:
- *
- * * When a field is in both the mask and the values, the corresponding field
- * is updated.
- * * When a field is in neither the mask nor the values, the corresponding
- * field is unmodified.
- * * When a field is in the mask but not in the values, the corresponding field
- * is deleted.
- * * When a field is not in the mask but is in the values, the values map is
- * ignored.
- */
- class PatchMutation extends Mutation {
- constructor(key, data, fieldMask, precondition, fieldTransforms = []) {
- super();
- this.key = key;
- this.data = data;
- this.fieldMask = fieldMask;
- this.precondition = precondition;
- this.fieldTransforms = fieldTransforms;
- this.type = 1 /* MutationType.Patch */;
- }
- getFieldMask() {
- return this.fieldMask;
- }
- }
- /** A mutation that deletes the document at the given key. */
- class DeleteMutation extends Mutation {
- constructor(key, precondition) {
- super();
- this.key = key;
- this.precondition = precondition;
- this.type = 2 /* MutationType.Delete */;
- this.fieldTransforms = [];
- }
- getFieldMask() {
- return null;
- }
- }
- /**
- * A mutation that verifies the existence of the document at the given key with
- * the provided precondition.
- *
- * The `verify` operation is only used in Transactions, and this class serves
- * primarily to facilitate serialization into protos.
- */
- class VerifyMutation extends Mutation {
- constructor(key, precondition) {
- super();
- this.key = key;
- this.precondition = precondition;
- this.type = 3 /* MutationType.Verify */;
- this.fieldTransforms = [];
- }
- getFieldMask() {
- 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.
- */
- const DIRECTIONS = (() => {
- const dirs = {};
- dirs["asc" /* Direction.ASCENDING */] = 'ASCENDING';
- dirs["desc" /* Direction.DESCENDING */] = 'DESCENDING';
- return dirs;
- })();
- const OPERATORS = (() => {
- const ops = {};
- ops["<" /* Operator.LESS_THAN */] = 'LESS_THAN';
- ops["<=" /* Operator.LESS_THAN_OR_EQUAL */] = 'LESS_THAN_OR_EQUAL';
- ops[">" /* Operator.GREATER_THAN */] = 'GREATER_THAN';
- ops[">=" /* Operator.GREATER_THAN_OR_EQUAL */] = 'GREATER_THAN_OR_EQUAL';
- ops["==" /* Operator.EQUAL */] = 'EQUAL';
- ops["!=" /* Operator.NOT_EQUAL */] = 'NOT_EQUAL';
- ops["array-contains" /* Operator.ARRAY_CONTAINS */] = 'ARRAY_CONTAINS';
- ops["in" /* Operator.IN */] = 'IN';
- ops["not-in" /* Operator.NOT_IN */] = 'NOT_IN';
- ops["array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */] = 'ARRAY_CONTAINS_ANY';
- return ops;
- })();
- const COMPOSITE_OPERATORS = (() => {
- const ops = {};
- ops["and" /* CompositeOperator.AND */] = 'AND';
- ops["or" /* CompositeOperator.OR */] = 'OR';
- return ops;
- })();
- function assertPresent(value, description) {
- }
- /**
- * This class generates JsonObject values for the Datastore API suitable for
- * sending to either GRPC stub methods or via the JSON/HTTP REST API.
- *
- * The serializer supports both Protobuf.js and Proto3 JSON formats. By
- * setting `useProto3Json` to true, the serializer will use the Proto3 JSON
- * format.
- *
- * For a description of the Proto3 JSON format check
- * https://developers.google.com/protocol-buffers/docs/proto3#json
- *
- * TODO(klimt): We can remove the databaseId argument if we keep the full
- * resource name in documents.
- */
- class JsonProtoSerializer {
- constructor(databaseId, useProto3Json) {
- this.databaseId = databaseId;
- this.useProto3Json = useProto3Json;
- }
- }
- /**
- * Returns a value for a number (or null) that's appropriate to put into
- * a google.protobuf.Int32Value proto.
- * DO NOT USE THIS FOR ANYTHING ELSE.
- * This method cheats. It's typed as returning "number" because that's what
- * our generated proto interfaces say Int32Value must be. But GRPC actually
- * expects a { value: <number> } struct.
- */
- function toInt32Proto(serializer, val) {
- if (serializer.useProto3Json || isNullOrUndefined(val)) {
- return val;
- }
- else {
- return { value: val };
- }
- }
- /**
- * Returns a value for a Date that's appropriate to put into a proto.
- */
- function toTimestamp(serializer, timestamp) {
- if (serializer.useProto3Json) {
- // Serialize to ISO-8601 date format, but with full nano resolution.
- // Since JS Date has only millis, let's only use it for the seconds and
- // then manually add the fractions to the end.
- const jsDateStr = new Date(timestamp.seconds * 1000).toISOString();
- // Remove .xxx frac part and Z in the end.
- const strUntilSeconds = jsDateStr.replace(/\.\d*/, '').replace('Z', '');
- // Pad the fraction out to 9 digits (nanos).
- const nanoStr = ('000000000' + timestamp.nanoseconds).slice(-9);
- return `${strUntilSeconds}.${nanoStr}Z`;
- }
- else {
- return {
- seconds: '' + timestamp.seconds,
- nanos: timestamp.nanoseconds
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- };
- }
- }
- function fromTimestamp(date) {
- const timestamp = normalizeTimestamp(date);
- return new Timestamp(timestamp.seconds, timestamp.nanos);
- }
- /**
- * Returns a value for bytes that's appropriate to put in a proto.
- *
- * Visible for testing.
- */
- function toBytes(serializer, bytes) {
- if (serializer.useProto3Json) {
- return bytes.toBase64();
- }
- else {
- return bytes.toUint8Array();
- }
- }
- function toVersion(serializer, version) {
- return toTimestamp(serializer, version.toTimestamp());
- }
- function fromVersion(version) {
- hardAssert(!!version);
- return SnapshotVersion.fromTimestamp(fromTimestamp(version));
- }
- function toResourceName(databaseId, path) {
- return fullyQualifiedPrefixPath(databaseId)
- .child('documents')
- .child(path)
- .canonicalString();
- }
- function fromResourceName(name) {
- const resource = ResourcePath.fromString(name);
- hardAssert(isValidResourceName(resource));
- return resource;
- }
- function toName(serializer, key) {
- return toResourceName(serializer.databaseId, key.path);
- }
- function fromName(serializer, name) {
- const resource = fromResourceName(name);
- if (resource.get(1) !== serializer.databaseId.projectId) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Tried to deserialize key from different project: ' +
- resource.get(1) +
- ' vs ' +
- serializer.databaseId.projectId);
- }
- if (resource.get(3) !== serializer.databaseId.database) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Tried to deserialize key from different database: ' +
- resource.get(3) +
- ' vs ' +
- serializer.databaseId.database);
- }
- return new DocumentKey(extractLocalPathFromResourceName(resource));
- }
- function toQueryPath(serializer, path) {
- return toResourceName(serializer.databaseId, path);
- }
- function getEncodedDatabaseId(serializer) {
- const path = new ResourcePath([
- 'projects',
- serializer.databaseId.projectId,
- 'databases',
- serializer.databaseId.database
- ]);
- return path.canonicalString();
- }
- function fullyQualifiedPrefixPath(databaseId) {
- return new ResourcePath([
- 'projects',
- databaseId.projectId,
- 'databases',
- databaseId.database
- ]);
- }
- function extractLocalPathFromResourceName(resourceName) {
- hardAssert(resourceName.length > 4 && resourceName.get(4) === 'documents');
- return resourceName.popFirst(5);
- }
- /** Creates a Document proto from key and fields (but no create/update time) */
- function toMutationDocument(serializer, key, fields) {
- return {
- name: toName(serializer, key),
- fields: fields.value.mapValue.fields
- };
- }
- function fromDocument(serializer, document, hasCommittedMutations) {
- const key = fromName(serializer, document.name);
- const version = fromVersion(document.updateTime);
- // If we read a document from persistence that is missing createTime, it's due
- // to older SDK versions not storing this information. In such cases, we'll
- // set the createTime to zero. This can be removed in the long term.
- const createTime = document.createTime
- ? fromVersion(document.createTime)
- : SnapshotVersion.min();
- const data = new ObjectValue({ mapValue: { fields: document.fields } });
- const result = MutableDocument.newFoundDocument(key, version, createTime, data);
- if (hasCommittedMutations) {
- result.setHasCommittedMutations();
- }
- return hasCommittedMutations ? result.setHasCommittedMutations() : result;
- }
- function fromFound(serializer, doc) {
- hardAssert(!!doc.found);
- assertPresent(doc.found.name);
- assertPresent(doc.found.updateTime);
- const key = fromName(serializer, doc.found.name);
- const version = fromVersion(doc.found.updateTime);
- const createTime = doc.found.createTime
- ? fromVersion(doc.found.createTime)
- : SnapshotVersion.min();
- const data = new ObjectValue({ mapValue: { fields: doc.found.fields } });
- return MutableDocument.newFoundDocument(key, version, createTime, data);
- }
- function fromMissing(serializer, result) {
- hardAssert(!!result.missing);
- hardAssert(!!result.readTime);
- const key = fromName(serializer, result.missing);
- const version = fromVersion(result.readTime);
- return MutableDocument.newNoDocument(key, version);
- }
- function fromBatchGetDocumentsResponse(serializer, result) {
- if ('found' in result) {
- return fromFound(serializer, result);
- }
- else if ('missing' in result) {
- return fromMissing(serializer, result);
- }
- return fail();
- }
- function toMutation(serializer, mutation) {
- let result;
- if (mutation instanceof SetMutation) {
- result = {
- update: toMutationDocument(serializer, mutation.key, mutation.value)
- };
- }
- else if (mutation instanceof DeleteMutation) {
- result = { delete: toName(serializer, mutation.key) };
- }
- else if (mutation instanceof PatchMutation) {
- result = {
- update: toMutationDocument(serializer, mutation.key, mutation.data),
- updateMask: toDocumentMask(mutation.fieldMask)
- };
- }
- else if (mutation instanceof VerifyMutation) {
- result = {
- verify: toName(serializer, mutation.key)
- };
- }
- else {
- return fail();
- }
- if (mutation.fieldTransforms.length > 0) {
- result.updateTransforms = mutation.fieldTransforms.map(transform => toFieldTransform(serializer, transform));
- }
- if (!mutation.precondition.isNone) {
- result.currentDocument = toPrecondition(serializer, mutation.precondition);
- }
- return result;
- }
- function toPrecondition(serializer, precondition) {
- if (precondition.updateTime !== undefined) {
- return {
- updateTime: toVersion(serializer, precondition.updateTime)
- };
- }
- else if (precondition.exists !== undefined) {
- return { exists: precondition.exists };
- }
- else {
- return fail();
- }
- }
- function toFieldTransform(serializer, fieldTransform) {
- const transform = fieldTransform.transform;
- if (transform instanceof ServerTimestampTransform) {
- return {
- fieldPath: fieldTransform.field.canonicalString(),
- setToServerValue: 'REQUEST_TIME'
- };
- }
- else if (transform instanceof ArrayUnionTransformOperation) {
- return {
- fieldPath: fieldTransform.field.canonicalString(),
- appendMissingElements: {
- values: transform.elements
- }
- };
- }
- else if (transform instanceof ArrayRemoveTransformOperation) {
- return {
- fieldPath: fieldTransform.field.canonicalString(),
- removeAllFromArray: {
- values: transform.elements
- }
- };
- }
- else if (transform instanceof NumericIncrementTransformOperation) {
- return {
- fieldPath: fieldTransform.field.canonicalString(),
- increment: transform.operand
- };
- }
- else {
- throw fail();
- }
- }
- function toQueryTarget(serializer, target) {
- // Dissect the path into parent, collectionId, and optional key filter.
- const result = { structuredQuery: {} };
- const path = target.path;
- if (target.collectionGroup !== null) {
- result.parent = toQueryPath(serializer, path);
- result.structuredQuery.from = [
- {
- collectionId: target.collectionGroup,
- allDescendants: true
- }
- ];
- }
- else {
- result.parent = toQueryPath(serializer, path.popLast());
- result.structuredQuery.from = [{ collectionId: path.lastSegment() }];
- }
- const where = toFilters(target.filters);
- if (where) {
- result.structuredQuery.where = where;
- }
- const orderBy = toOrder(target.orderBy);
- if (orderBy) {
- result.structuredQuery.orderBy = orderBy;
- }
- const limit = toInt32Proto(serializer, target.limit);
- if (limit !== null) {
- result.structuredQuery.limit = limit;
- }
- if (target.startAt) {
- result.structuredQuery.startAt = toStartAtCursor(target.startAt);
- }
- if (target.endAt) {
- result.structuredQuery.endAt = toEndAtCursor(target.endAt);
- }
- return result;
- }
- function toRunAggregationQueryRequest(serializer, target) {
- const queryTarget = toQueryTarget(serializer, target);
- return {
- structuredAggregationQuery: {
- aggregations: [
- {
- count: {},
- alias: 'count_alias'
- }
- ],
- structuredQuery: queryTarget.structuredQuery
- },
- parent: queryTarget.parent
- };
- }
- function toFilters(filters) {
- if (filters.length === 0) {
- return;
- }
- return toFilter(CompositeFilter.create(filters, "and" /* CompositeOperator.AND */));
- }
- function toOrder(orderBys) {
- if (orderBys.length === 0) {
- return;
- }
- return orderBys.map(order => toPropertyOrder(order));
- }
- function toStartAtCursor(cursor) {
- return {
- before: cursor.inclusive,
- values: cursor.position
- };
- }
- function toEndAtCursor(cursor) {
- return {
- before: !cursor.inclusive,
- values: cursor.position
- };
- }
- // visible for testing
- function toDirection(dir) {
- return DIRECTIONS[dir];
- }
- // visible for testing
- function toOperatorName(op) {
- return OPERATORS[op];
- }
- function toCompositeOperatorName(op) {
- return COMPOSITE_OPERATORS[op];
- }
- function toFieldPathReference(path) {
- return { fieldPath: path.canonicalString() };
- }
- // visible for testing
- function toPropertyOrder(orderBy) {
- return {
- field: toFieldPathReference(orderBy.field),
- direction: toDirection(orderBy.dir)
- };
- }
- // visible for testing
- function toFilter(filter) {
- if (filter instanceof FieldFilter) {
- return toUnaryOrFieldFilter(filter);
- }
- else if (filter instanceof CompositeFilter) {
- return toCompositeFilter(filter);
- }
- else {
- return fail();
- }
- }
- function toCompositeFilter(filter) {
- const protos = filter.getFilters().map(filter => toFilter(filter));
- if (protos.length === 1) {
- return protos[0];
- }
- return {
- compositeFilter: {
- op: toCompositeOperatorName(filter.op),
- filters: protos
- }
- };
- }
- function toUnaryOrFieldFilter(filter) {
- if (filter.op === "==" /* Operator.EQUAL */) {
- if (isNanValue(filter.value)) {
- return {
- unaryFilter: {
- field: toFieldPathReference(filter.field),
- op: 'IS_NAN'
- }
- };
- }
- else if (isNullValue(filter.value)) {
- return {
- unaryFilter: {
- field: toFieldPathReference(filter.field),
- op: 'IS_NULL'
- }
- };
- }
- }
- else if (filter.op === "!=" /* Operator.NOT_EQUAL */) {
- if (isNanValue(filter.value)) {
- return {
- unaryFilter: {
- field: toFieldPathReference(filter.field),
- op: 'IS_NOT_NAN'
- }
- };
- }
- else if (isNullValue(filter.value)) {
- return {
- unaryFilter: {
- field: toFieldPathReference(filter.field),
- op: 'IS_NOT_NULL'
- }
- };
- }
- }
- return {
- fieldFilter: {
- field: toFieldPathReference(filter.field),
- op: toOperatorName(filter.op),
- value: filter.value
- }
- };
- }
- function toDocumentMask(fieldMask) {
- const canonicalFields = [];
- fieldMask.fields.forEach(field => canonicalFields.push(field.canonicalString()));
- return {
- fieldPaths: canonicalFields
- };
- }
- function isValidResourceName(path) {
- // Resource names have at least 4 components (project ID, database ID)
- return (path.length >= 4 &&
- path.get(0) === 'projects' &&
- path.get(2) === 'databases');
- }
-
- /**
- * @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.
- */
- function newSerializer(databaseId) {
- return new JsonProtoSerializer(databaseId, /* useProto3Json= */ 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.
- */
- const LOG_TAG$2 = 'ExponentialBackoff';
- /**
- * Initial backoff time in milliseconds after an error.
- * Set to 1s according to https://cloud.google.com/apis/design/errors.
- */
- const DEFAULT_BACKOFF_INITIAL_DELAY_MS = 1000;
- const DEFAULT_BACKOFF_FACTOR = 1.5;
- /** Maximum backoff time in milliseconds */
- const DEFAULT_BACKOFF_MAX_DELAY_MS = 60 * 1000;
- /**
- * A helper for running delayed tasks following an exponential backoff curve
- * between attempts.
- *
- * Each delay is made up of a "base" delay which follows the exponential
- * backoff curve, and a +/- 50% "jitter" that is calculated and added to the
- * base delay. This prevents clients from accidentally synchronizing their
- * delays causing spikes of load to the backend.
- */
- class ExponentialBackoff {
- constructor(
- /**
- * The AsyncQueue to run backoff operations on.
- */
- queue,
- /**
- * The ID to use when scheduling backoff operations on the AsyncQueue.
- */
- timerId,
- /**
- * The initial delay (used as the base delay on the first retry attempt).
- * Note that jitter will still be applied, so the actual delay could be as
- * little as 0.5*initialDelayMs.
- */
- initialDelayMs = DEFAULT_BACKOFF_INITIAL_DELAY_MS,
- /**
- * The multiplier to use to determine the extended base delay after each
- * attempt.
- */
- backoffFactor = DEFAULT_BACKOFF_FACTOR,
- /**
- * The maximum base delay after which no further backoff is performed.
- * Note that jitter will still be applied, so the actual delay could be as
- * much as 1.5*maxDelayMs.
- */
- maxDelayMs = DEFAULT_BACKOFF_MAX_DELAY_MS) {
- this.queue = queue;
- this.timerId = timerId;
- this.initialDelayMs = initialDelayMs;
- this.backoffFactor = backoffFactor;
- this.maxDelayMs = maxDelayMs;
- this.currentBaseMs = 0;
- this.timerPromise = null;
- /** The last backoff attempt, as epoch milliseconds. */
- this.lastAttemptTime = Date.now();
- this.reset();
- }
- /**
- * Resets the backoff delay.
- *
- * The very next backoffAndWait() will have no delay. If it is called again
- * (i.e. due to an error), initialDelayMs (plus jitter) will be used, and
- * subsequent ones will increase according to the backoffFactor.
- */
- reset() {
- this.currentBaseMs = 0;
- }
- /**
- * Resets the backoff delay to the maximum delay (e.g. for use after a
- * RESOURCE_EXHAUSTED error).
- */
- resetToMax() {
- this.currentBaseMs = this.maxDelayMs;
- }
- /**
- * Returns a promise that resolves after currentDelayMs, and increases the
- * delay for any subsequent attempts. If there was a pending backoff operation
- * already, it will be canceled.
- */
- backoffAndRun(op) {
- // Cancel any pending backoff operation.
- this.cancel();
- // First schedule using the current base (which may be 0 and should be
- // honored as such).
- const desiredDelayWithJitterMs = Math.floor(this.currentBaseMs + this.jitterDelayMs());
- // Guard against lastAttemptTime being in the future due to a clock change.
- const delaySoFarMs = Math.max(0, Date.now() - this.lastAttemptTime);
- // Guard against the backoff delay already being past.
- const remainingDelayMs = Math.max(0, desiredDelayWithJitterMs - delaySoFarMs);
- if (remainingDelayMs > 0) {
- logDebug(LOG_TAG$2, `Backing off for ${remainingDelayMs} ms ` +
- `(base delay: ${this.currentBaseMs} ms, ` +
- `delay with jitter: ${desiredDelayWithJitterMs} ms, ` +
- `last attempt: ${delaySoFarMs} ms ago)`);
- }
- this.timerPromise = this.queue.enqueueAfterDelay(this.timerId, remainingDelayMs, () => {
- this.lastAttemptTime = Date.now();
- return op();
- });
- // Apply backoff factor to determine next delay and ensure it is within
- // bounds.
- this.currentBaseMs *= this.backoffFactor;
- if (this.currentBaseMs < this.initialDelayMs) {
- this.currentBaseMs = this.initialDelayMs;
- }
- if (this.currentBaseMs > this.maxDelayMs) {
- this.currentBaseMs = this.maxDelayMs;
- }
- }
- skipBackoff() {
- if (this.timerPromise !== null) {
- this.timerPromise.skipDelay();
- this.timerPromise = null;
- }
- }
- cancel() {
- if (this.timerPromise !== null) {
- this.timerPromise.cancel();
- this.timerPromise = null;
- }
- }
- /** Returns a random value in the range [-currentBaseMs/2, currentBaseMs/2] */
- jitterDelayMs() {
- return (Math.random() - 0.5) * this.currentBaseMs;
- }
- }
-
- /**
- * @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.
- */
- /**
- * Datastore and its related methods are a wrapper around the external Google
- * Cloud Datastore grpc API, which provides an interface that is more convenient
- * for the rest of the client SDK architecture to consume.
- */
- class Datastore {
- }
- /**
- * An implementation of Datastore that exposes additional state for internal
- * consumption.
- */
- class DatastoreImpl extends Datastore {
- constructor(authCredentials, appCheckCredentials, connection, serializer) {
- super();
- this.authCredentials = authCredentials;
- this.appCheckCredentials = appCheckCredentials;
- this.connection = connection;
- this.serializer = serializer;
- this.terminated = false;
- }
- verifyInitialized() {
- if (this.terminated) {
- throw new FirestoreError(Code.FAILED_PRECONDITION, 'The client has already been terminated.');
- }
- }
- /** Invokes the provided RPC with auth and AppCheck tokens. */
- invokeRPC(rpcName, path, request) {
- this.verifyInitialized();
- return Promise.all([
- this.authCredentials.getToken(),
- this.appCheckCredentials.getToken()
- ])
- .then(([authToken, appCheckToken]) => {
- return this.connection.invokeRPC(rpcName, path, request, authToken, appCheckToken);
- })
- .catch((error) => {
- if (error.name === 'FirebaseError') {
- if (error.code === Code.UNAUTHENTICATED) {
- this.authCredentials.invalidateToken();
- this.appCheckCredentials.invalidateToken();
- }
- throw error;
- }
- else {
- throw new FirestoreError(Code.UNKNOWN, error.toString());
- }
- });
- }
- /** Invokes the provided RPC with streamed results with auth and AppCheck tokens. */
- invokeStreamingRPC(rpcName, path, request, expectedResponseCount) {
- this.verifyInitialized();
- return Promise.all([
- this.authCredentials.getToken(),
- this.appCheckCredentials.getToken()
- ])
- .then(([authToken, appCheckToken]) => {
- return this.connection.invokeStreamingRPC(rpcName, path, request, authToken, appCheckToken, expectedResponseCount);
- })
- .catch((error) => {
- if (error.name === 'FirebaseError') {
- if (error.code === Code.UNAUTHENTICATED) {
- this.authCredentials.invalidateToken();
- this.appCheckCredentials.invalidateToken();
- }
- throw error;
- }
- else {
- throw new FirestoreError(Code.UNKNOWN, error.toString());
- }
- });
- }
- terminate() {
- this.terminated = true;
- }
- }
- // TODO(firestorexp): Make sure there is only one Datastore instance per
- // firestore-exp client.
- function newDatastore(authCredentials, appCheckCredentials, connection, serializer) {
- return new DatastoreImpl(authCredentials, appCheckCredentials, connection, serializer);
- }
- async function invokeCommitRpc(datastore, mutations) {
- const datastoreImpl = debugCast(datastore);
- const path = getEncodedDatabaseId(datastoreImpl.serializer) + '/documents';
- const request = {
- writes: mutations.map(m => toMutation(datastoreImpl.serializer, m))
- };
- await datastoreImpl.invokeRPC('Commit', path, request);
- }
- async function invokeBatchGetDocumentsRpc(datastore, keys) {
- const datastoreImpl = debugCast(datastore);
- const path = getEncodedDatabaseId(datastoreImpl.serializer) + '/documents';
- const request = {
- documents: keys.map(k => toName(datastoreImpl.serializer, k))
- };
- const response = await datastoreImpl.invokeStreamingRPC('BatchGetDocuments', path, request, keys.length);
- const docs = new Map();
- response.forEach(proto => {
- const doc = fromBatchGetDocumentsResponse(datastoreImpl.serializer, proto);
- docs.set(doc.key.toString(), doc);
- });
- const result = [];
- keys.forEach(key => {
- const doc = docs.get(key.toString());
- hardAssert(!!doc);
- result.push(doc);
- });
- return result;
- }
- async function invokeRunQueryRpc(datastore, query) {
- const datastoreImpl = debugCast(datastore);
- const request = toQueryTarget(datastoreImpl.serializer, queryToTarget(query));
- const response = await datastoreImpl.invokeStreamingRPC('RunQuery', request.parent, { structuredQuery: request.structuredQuery });
- return (response
- // Omit RunQueryResponses that only contain readTimes.
- .filter(proto => !!proto.document)
- .map(proto => fromDocument(datastoreImpl.serializer, proto.document, undefined)));
- }
- async function invokeRunAggregationQueryRpc(datastore, query) {
- const datastoreImpl = debugCast(datastore);
- const request = toRunAggregationQueryRequest(datastoreImpl.serializer, queryToTarget(query));
- const parent = request.parent;
- if (!datastoreImpl.connection.shouldResourcePathBeIncludedInRequest) {
- delete request.parent;
- }
- const response = await datastoreImpl.invokeStreamingRPC('RunAggregationQuery', parent, request, /*expectedResponseCount=*/ 1);
- return (response
- // Omit RunAggregationQueryResponse that only contain readTimes.
- .filter(proto => !!proto.result)
- .map(proto => proto.result.aggregateFields));
- }
-
- /**
- * @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.
- */
- const LOG_TAG$1 = 'ComponentProvider';
- /**
- * An instance map that ensures only one Datastore exists per Firestore
- * instance.
- */
- const datastoreInstances = new Map();
- /**
- * Returns an initialized and started Datastore for the given Firestore
- * instance. Callers must invoke removeComponents() when the Firestore
- * instance is terminated.
- */
- function getDatastore(firestore) {
- if (firestore._terminated) {
- throw new FirestoreError(Code.FAILED_PRECONDITION, 'The client has already been terminated.');
- }
- if (!datastoreInstances.has(firestore)) {
- logDebug(LOG_TAG$1, 'Initializing Datastore');
- const databaseInfo = makeDatabaseInfo(firestore._databaseId, firestore.app.options.appId || '', firestore._persistenceKey, firestore._freezeSettings());
- const connection = newConnection(databaseInfo);
- const serializer = newSerializer(firestore._databaseId);
- const datastore = newDatastore(firestore._authCredentials, firestore._appCheckCredentials, connection, serializer);
- datastoreInstances.set(firestore, datastore);
- }
- return datastoreInstances.get(firestore);
- }
- /**
- * Removes all components associated with the provided instance. Must be called
- * when the `Firestore` instance is terminated.
- */
- function removeComponents(firestore) {
- const datastore = datastoreInstances.get(firestore);
- if (datastore) {
- logDebug(LOG_TAG$1, 'Removing Datastore');
- datastoreInstances.delete(firestore);
- datastore.terminate();
- }
- }
- function makeDatabaseInfo(databaseId, appId, persistenceKey, settings) {
- return new DatabaseInfo(databaseId, appId, persistenceKey, settings.host, settings.ssl, settings.experimentalForceLongPolling, settings.experimentalAutoDetectLongPolling, settings.useFetchStreams);
- }
-
- /**
- * @license
- * Copyright 2018 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.
- */
- const LRU_COLLECTION_DISABLED = -1;
- const LRU_DEFAULT_CACHE_SIZE_BYTES = 40 * 1024 * 1024;
-
- /**
- * @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.
- */
- /** Verifies whether `e` is an IndexedDbTransactionError. */
- function isIndexedDbTransactionError(e) {
- // Use name equality, as instanceof checks on errors don't work with errors
- // that wrap other errors.
- return e.name === 'IndexedDbTransactionError';
- }
-
- /**
- * @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.
- */
- const LRU_MINIMUM_CACHE_SIZE_BYTES = 1 * 1024 * 1024;
-
- /**
- * @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.
- */
- // settings() defaults:
- const DEFAULT_HOST = 'firestore.googleapis.com';
- const DEFAULT_SSL = true;
- /**
- * A concrete type describing all the values that can be applied via a
- * user-supplied `FirestoreSettings` object. This is a separate type so that
- * defaults can be supplied and the value can be checked for equality.
- */
- class FirestoreSettingsImpl {
- constructor(settings) {
- var _a;
- if (settings.host === undefined) {
- if (settings.ssl !== undefined) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, "Can't provide ssl option if host option is not set");
- }
- this.host = DEFAULT_HOST;
- this.ssl = DEFAULT_SSL;
- }
- else {
- this.host = settings.host;
- this.ssl = (_a = settings.ssl) !== null && _a !== void 0 ? _a : DEFAULT_SSL;
- }
- this.credentials = settings.credentials;
- this.ignoreUndefinedProperties = !!settings.ignoreUndefinedProperties;
- if (settings.cacheSizeBytes === undefined) {
- this.cacheSizeBytes = LRU_DEFAULT_CACHE_SIZE_BYTES;
- }
- else {
- if (settings.cacheSizeBytes !== LRU_COLLECTION_DISABLED &&
- settings.cacheSizeBytes < LRU_MINIMUM_CACHE_SIZE_BYTES) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `cacheSizeBytes must be at least ${LRU_MINIMUM_CACHE_SIZE_BYTES}`);
- }
- else {
- this.cacheSizeBytes = settings.cacheSizeBytes;
- }
- }
- this.experimentalForceLongPolling = !!settings.experimentalForceLongPolling;
- this.experimentalAutoDetectLongPolling =
- !!settings.experimentalAutoDetectLongPolling;
- this.useFetchStreams = !!settings.useFetchStreams;
- validateIsNotUsedTogether('experimentalForceLongPolling', settings.experimentalForceLongPolling, 'experimentalAutoDetectLongPolling', settings.experimentalAutoDetectLongPolling);
- }
- isEqual(other) {
- return (this.host === other.host &&
- this.ssl === other.ssl &&
- this.credentials === other.credentials &&
- this.cacheSizeBytes === other.cacheSizeBytes &&
- this.experimentalForceLongPolling ===
- other.experimentalForceLongPolling &&
- this.experimentalAutoDetectLongPolling ===
- other.experimentalAutoDetectLongPolling &&
- this.ignoreUndefinedProperties === other.ignoreUndefinedProperties &&
- this.useFetchStreams === other.useFetchStreams);
- }
- }
-
- /**
- * @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.
- */
- /**
- * The Cloud Firestore service interface.
- *
- * Do not call this constructor directly. Instead, use {@link (getFirestore:1)}.
- */
- class Firestore {
- /** @hideconstructor */
- constructor(_authCredentials, _appCheckCredentials, _databaseId, _app) {
- this._authCredentials = _authCredentials;
- this._appCheckCredentials = _appCheckCredentials;
- this._databaseId = _databaseId;
- this._app = _app;
- /**
- * Whether it's a Firestore or Firestore Lite instance.
- */
- this.type = 'firestore-lite';
- this._persistenceKey = '(lite)';
- this._settings = new FirestoreSettingsImpl({});
- this._settingsFrozen = false;
- }
- /**
- * The {@link @firebase/app#FirebaseApp} associated with this `Firestore` service
- * instance.
- */
- get app() {
- if (!this._app) {
- throw new FirestoreError(Code.FAILED_PRECONDITION, "Firestore was not initialized using the Firebase SDK. 'app' is " +
- 'not available');
- }
- return this._app;
- }
- get _initialized() {
- return this._settingsFrozen;
- }
- get _terminated() {
- return this._terminateTask !== undefined;
- }
- _setSettings(settings) {
- if (this._settingsFrozen) {
- throw new FirestoreError(Code.FAILED_PRECONDITION, 'Firestore has already been started and its settings can no longer ' +
- 'be changed. You can only modify settings before calling any other ' +
- 'methods on a Firestore object.');
- }
- this._settings = new FirestoreSettingsImpl(settings);
- if (settings.credentials !== undefined) {
- this._authCredentials = makeAuthCredentialsProvider(settings.credentials);
- }
- }
- _getSettings() {
- return this._settings;
- }
- _freezeSettings() {
- this._settingsFrozen = true;
- return this._settings;
- }
- _delete() {
- if (!this._terminateTask) {
- this._terminateTask = this._terminate();
- }
- return this._terminateTask;
- }
- /** Returns a JSON-serializable representation of this `Firestore` instance. */
- toJSON() {
- return {
- app: this._app,
- databaseId: this._databaseId,
- settings: this._settings
- };
- }
- /**
- * Terminates all components used by this client. Subclasses can override
- * this method to clean up their own dependencies, but must also call this
- * method.
- *
- * Only ever called once.
- */
- _terminate() {
- removeComponents(this);
- return Promise.resolve();
- }
- }
- function initializeFirestore(app, settings, databaseId) {
- if (!databaseId) {
- databaseId = DEFAULT_DATABASE_NAME;
- }
- const provider = _getProvider(app, 'firestore/lite');
- if (provider.isInitialized(databaseId)) {
- throw new FirestoreError(Code.FAILED_PRECONDITION, 'Firestore can only be initialized once per app.');
- }
- return provider.initialize({
- options: settings,
- instanceIdentifier: databaseId
- });
- }
- function getFirestore(appOrDatabaseId, optionalDatabaseId) {
- const app = typeof appOrDatabaseId === 'object' ? appOrDatabaseId : getApp();
- const databaseId = typeof appOrDatabaseId === 'string'
- ? appOrDatabaseId
- : optionalDatabaseId || '(default)';
- const db = _getProvider(app, 'firestore/lite').getImmediate({
- identifier: databaseId
- });
- if (!db._initialized) {
- const emulator = getDefaultEmulatorHostnameAndPort('firestore');
- if (emulator) {
- connectFirestoreEmulator(db, ...emulator);
- }
- }
- return db;
- }
- /**
- * Modify this instance to communicate with the Cloud Firestore emulator.
- *
- * Note: This must be called before this instance has been used to do any
- * operations.
- *
- * @param firestore - The `Firestore` instance to configure to connect to the
- * emulator.
- * @param host - the emulator host (ex: localhost).
- * @param port - the emulator port (ex: 9000).
- * @param options.mockUserToken - the mock auth token to use for unit testing
- * Security Rules.
- */
- function connectFirestoreEmulator(firestore, host, port, options = {}) {
- var _a;
- firestore = cast(firestore, Firestore);
- const settings = firestore._getSettings();
- if (settings.host !== DEFAULT_HOST && settings.host !== host) {
- logWarn('Host has been set in both settings() and useEmulator(), emulator host ' +
- 'will be used');
- }
- firestore._setSettings(Object.assign(Object.assign({}, settings), { host: `${host}:${port}`, ssl: false }));
- if (options.mockUserToken) {
- let token;
- let user;
- if (typeof options.mockUserToken === 'string') {
- token = options.mockUserToken;
- user = User.MOCK_USER;
- }
- else {
- // Let createMockUserToken validate first (catches common mistakes like
- // invalid field "uid" and missing field "sub" / "user_id".)
- token = createMockUserToken(options.mockUserToken, (_a = firestore._app) === null || _a === void 0 ? void 0 : _a.options.projectId);
- const uid = options.mockUserToken.sub || options.mockUserToken.user_id;
- if (!uid) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, "mockUserToken must contain 'sub' or 'user_id' field!");
- }
- user = new User(uid);
- }
- firestore._authCredentials = new EmulatorAuthCredentialsProvider(new OAuthToken(token, user));
- }
- }
- /**
- * Terminates the provided `Firestore` instance.
- *
- * After calling `terminate()` only the `clearIndexedDbPersistence()` functions
- * may be used. Any other function will throw a `FirestoreError`. Termination
- * does not cancel any pending writes, and any promises that are awaiting a
- * response from the server will not be resolved.
- *
- * To restart after termination, create a new instance of `Firestore` with
- * {@link (getFirestore:1)}.
- *
- * Note: Under normal circumstances, calling `terminate()` is not required. This
- * function is useful only when you want to force this instance to release all of
- * its resources or in combination with {@link clearIndexedDbPersistence} to
- * ensure that all local state is destroyed between test runs.
- *
- * @param firestore - The `Firestore` instance to terminate.
- * @returns A `Promise` that is resolved when the instance has been successfully
- * terminated.
- */
- function terminate(firestore) {
- firestore = cast(firestore, Firestore);
- _removeServiceInstance(firestore.app, 'firestore/lite');
- return firestore._delete();
- }
-
- /**
- * @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.
- */
- function registerFirestore() {
- setSDKVersion(`${SDK_VERSION$1}_lite`);
- _registerComponent(new Component('firestore/lite', (container, { instanceIdentifier: databaseId, options: settings }) => {
- const app = container.getProvider('app').getImmediate();
- const firestoreInstance = new Firestore(new LiteAuthCredentialsProvider(container.getProvider('auth-internal')), new LiteAppCheckTokenProvider(container.getProvider('app-check-internal')), databaseIdFromApp(app, databaseId), app);
- if (settings) {
- firestoreInstance._setSettings(settings);
- }
- return firestoreInstance;
- }, 'PUBLIC').setMultipleInstances(true));
- // RUNTIME_ENV and BUILD_TARGET are replaced by real values during the compilation
- registerVersion('firestore-lite', version$1, 'node');
- registerVersion('firestore-lite', version$1, 'esm2017');
- }
-
- /**
- * @license
- * Copyright 2022 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.
- */
- /**
- * Represents an aggregation that can be performed by Firestore.
- */
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- class AggregateField {
- constructor() {
- /** A type string to uniquely identify instances of this class. */
- this.type = 'AggregateField';
- }
- }
- /**
- * The results of executing an aggregation query.
- */
- class AggregateQuerySnapshot {
- /** @hideconstructor */
- constructor(query, _data) {
- this._data = _data;
- /** A type string to uniquely identify instances of this class. */
- this.type = 'AggregateQuerySnapshot';
- this.query = query;
- }
- /**
- * Returns the results of the aggregations performed over the underlying
- * query.
- *
- * The keys of the returned object will be the same as those of the
- * `AggregateSpec` object specified to the aggregation method, and the values
- * will be the corresponding aggregation result.
- *
- * @returns The results of the aggregations performed over the underlying
- * query.
- */
- data() {
- return this._data;
- }
- }
-
- /**
- * @license
- * Copyright 2022 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.
- */
- /**
- * CountQueryRunner encapsulates the logic needed to run the count aggregation
- * queries.
- */
- class CountQueryRunner {
- constructor(query, datastore, userDataWriter) {
- this.query = query;
- this.datastore = datastore;
- this.userDataWriter = userDataWriter;
- }
- run() {
- return invokeRunAggregationQueryRpc(this.datastore, this.query._query).then(result => {
- hardAssert(result[0] !== undefined);
- const counts = Object.entries(result[0])
- .filter(([key, value]) => key === 'count_alias')
- .map(([key, value]) => this.userDataWriter.convertValue(value));
- const countValue = counts[0];
- hardAssert(typeof countValue === 'number');
- return Promise.resolve(new AggregateQuerySnapshot(this.query, {
- count: countValue
- }));
- });
- }
- }
-
- /**
- * @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 `DocumentReference` refers to a document location in a Firestore database
- * and can be used to write, read, or listen to the location. The document at
- * the referenced location may or may not exist.
- */
- class DocumentReference {
- /** @hideconstructor */
- constructor(firestore,
- /**
- * If provided, the `FirestoreDataConverter` associated with this instance.
- */
- converter, _key) {
- this.converter = converter;
- this._key = _key;
- /** The type of this Firestore reference. */
- this.type = 'document';
- this.firestore = firestore;
- }
- get _path() {
- return this._key.path;
- }
- /**
- * The document's identifier within its collection.
- */
- get id() {
- return this._key.path.lastSegment();
- }
- /**
- * A string representing the path of the referenced document (relative
- * to the root of the database).
- */
- get path() {
- return this._key.path.canonicalString();
- }
- /**
- * The collection this `DocumentReference` belongs to.
- */
- get parent() {
- return new CollectionReference(this.firestore, this.converter, this._key.path.popLast());
- }
- withConverter(converter) {
- return new DocumentReference(this.firestore, converter, this._key);
- }
- }
- /**
- * A `Query` refers to a query which you can read or listen to. You can also
- * construct refined `Query` objects by adding filters and ordering.
- */
- class Query {
- // This is the lite version of the Query class in the main SDK.
- /** @hideconstructor protected */
- constructor(firestore,
- /**
- * If provided, the `FirestoreDataConverter` associated with this instance.
- */
- converter, _query) {
- this.converter = converter;
- this._query = _query;
- /** The type of this Firestore reference. */
- this.type = 'query';
- this.firestore = firestore;
- }
- withConverter(converter) {
- return new Query(this.firestore, converter, this._query);
- }
- }
- /**
- * A `CollectionReference` object can be used for adding documents, getting
- * document references, and querying for documents (using {@link query}).
- */
- class CollectionReference extends Query {
- /** @hideconstructor */
- constructor(firestore, converter, _path) {
- super(firestore, converter, newQueryForPath(_path));
- this._path = _path;
- /** The type of this Firestore reference. */
- this.type = 'collection';
- }
- /** The collection's identifier. */
- get id() {
- return this._query.path.lastSegment();
- }
- /**
- * A string representing the path of the referenced collection (relative
- * to the root of the database).
- */
- get path() {
- return this._query.path.canonicalString();
- }
- /**
- * A reference to the containing `DocumentReference` if this is a
- * subcollection. If this isn't a subcollection, the reference is null.
- */
- get parent() {
- const parentPath = this._path.popLast();
- if (parentPath.isEmpty()) {
- return null;
- }
- else {
- return new DocumentReference(this.firestore,
- /* converter= */ null, new DocumentKey(parentPath));
- }
- }
- withConverter(converter) {
- return new CollectionReference(this.firestore, converter, this._path);
- }
- }
- function collection(parent, path, ...pathSegments) {
- parent = getModularInstance(parent);
- validateNonEmptyArgument('collection', 'path', path);
- if (parent instanceof Firestore) {
- const absolutePath = ResourcePath.fromString(path, ...pathSegments);
- validateCollectionPath(absolutePath);
- return new CollectionReference(parent, /* converter= */ null, absolutePath);
- }
- else {
- if (!(parent instanceof DocumentReference) &&
- !(parent instanceof CollectionReference)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Expected first argument to collection() to be a CollectionReference, ' +
- 'a DocumentReference or FirebaseFirestore');
- }
- const absolutePath = parent._path.child(ResourcePath.fromString(path, ...pathSegments));
- validateCollectionPath(absolutePath);
- return new CollectionReference(parent.firestore,
- /* converter= */ null, absolutePath);
- }
- }
- // TODO(firestorelite): Consider using ErrorFactory -
- // https://github.com/firebase/firebase-js-sdk/blob/0131e1f/packages/util/src/errors.ts#L106
- /**
- * Creates and returns a new `Query` instance that includes all documents in the
- * database that are contained in a collection or subcollection with the
- * given `collectionId`.
- *
- * @param firestore - A reference to the root `Firestore` instance.
- * @param collectionId - Identifies the collections to query over. Every
- * collection or subcollection with this ID as the last segment of its path
- * will be included. Cannot contain a slash.
- * @returns The created `Query`.
- */
- function collectionGroup(firestore, collectionId) {
- firestore = cast(firestore, Firestore);
- validateNonEmptyArgument('collectionGroup', 'collection id', collectionId);
- if (collectionId.indexOf('/') >= 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid collection ID '${collectionId}' passed to function ` +
- `collectionGroup(). Collection IDs must not contain '/'.`);
- }
- return new Query(firestore,
- /* converter= */ null, newQueryForCollectionGroup(collectionId));
- }
- function doc(parent, path, ...pathSegments) {
- parent = getModularInstance(parent);
- // We allow omission of 'pathString' but explicitly prohibit passing in both
- // 'undefined' and 'null'.
- if (arguments.length === 1) {
- path = AutoId.newId();
- }
- validateNonEmptyArgument('doc', 'path', path);
- if (parent instanceof Firestore) {
- const absolutePath = ResourcePath.fromString(path, ...pathSegments);
- validateDocumentPath(absolutePath);
- return new DocumentReference(parent,
- /* converter= */ null, new DocumentKey(absolutePath));
- }
- else {
- if (!(parent instanceof DocumentReference) &&
- !(parent instanceof CollectionReference)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Expected first argument to collection() to be a CollectionReference, ' +
- 'a DocumentReference or FirebaseFirestore');
- }
- const absolutePath = parent._path.child(ResourcePath.fromString(path, ...pathSegments));
- validateDocumentPath(absolutePath);
- return new DocumentReference(parent.firestore, parent instanceof CollectionReference ? parent.converter : null, new DocumentKey(absolutePath));
- }
- }
- /**
- * Returns true if the provided references are equal.
- *
- * @param left - A reference to compare.
- * @param right - A reference to compare.
- * @returns true if the references point to the same location in the same
- * Firestore database.
- */
- function refEqual(left, right) {
- left = getModularInstance(left);
- right = getModularInstance(right);
- if ((left instanceof DocumentReference ||
- left instanceof CollectionReference) &&
- (right instanceof DocumentReference || right instanceof CollectionReference)) {
- return (left.firestore === right.firestore &&
- left.path === right.path &&
- left.converter === right.converter);
- }
- return false;
- }
- /**
- * Returns true if the provided queries point to the same collection and apply
- * the same constraints.
- *
- * @param left - A `Query` to compare.
- * @param right - A `Query` to compare.
- * @returns true if the references point to the same location in the same
- * Firestore database.
- */
- function queryEqual(left, right) {
- left = getModularInstance(left);
- right = getModularInstance(right);
- if (left instanceof Query && right instanceof Query) {
- return (left.firestore === right.firestore &&
- queryEquals(left._query, right._query) &&
- left.converter === right.converter);
- }
- return false;
- }
-
- /**
- * @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.
- */
- /**
- * An immutable object representing an array of bytes.
- */
- class Bytes {
- /** @hideconstructor */
- constructor(byteString) {
- this._byteString = byteString;
- }
- /**
- * Creates a new `Bytes` object from the given Base64 string, converting it to
- * bytes.
- *
- * @param base64 - The Base64 string used to create the `Bytes` object.
- */
- static fromBase64String(base64) {
- try {
- return new Bytes(ByteString.fromBase64String(base64));
- }
- catch (e) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Failed to construct data from Base64 string: ' + e);
- }
- }
- /**
- * Creates a new `Bytes` object from the given Uint8Array.
- *
- * @param array - The Uint8Array used to create the `Bytes` object.
- */
- static fromUint8Array(array) {
- return new Bytes(ByteString.fromUint8Array(array));
- }
- /**
- * Returns the underlying bytes as a Base64-encoded string.
- *
- * @returns The Base64-encoded string created from the `Bytes` object.
- */
- toBase64() {
- return this._byteString.toBase64();
- }
- /**
- * Returns the underlying bytes in a new `Uint8Array`.
- *
- * @returns The Uint8Array created from the `Bytes` object.
- */
- toUint8Array() {
- return this._byteString.toUint8Array();
- }
- /**
- * Returns a string representation of the `Bytes` object.
- *
- * @returns A string representation of the `Bytes` object.
- */
- toString() {
- return 'Bytes(base64: ' + this.toBase64() + ')';
- }
- /**
- * Returns true if this `Bytes` object is equal to the provided one.
- *
- * @param other - The `Bytes` object to compare against.
- * @returns true if this `Bytes` object is equal to the provided one.
- */
- isEqual(other) {
- return this._byteString.isEqual(other._byteString);
- }
- }
-
- /**
- * @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 `FieldPath` refers to a field in a document. The path may consist of a
- * single field name (referring to a top-level field in the document), or a
- * list of field names (referring to a nested field in the document).
- *
- * Create a `FieldPath` by providing field names. If more than one field
- * name is provided, the path will point to a nested field in a document.
- */
- class FieldPath {
- /**
- * Creates a `FieldPath` from the provided field names. If more than one field
- * name is provided, the path will point to a nested field in a document.
- *
- * @param fieldNames - A list of field names.
- */
- constructor(...fieldNames) {
- for (let i = 0; i < fieldNames.length; ++i) {
- if (fieldNames[i].length === 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid field name at argument $(i + 1). ` +
- 'Field names must not be empty.');
- }
- }
- this._internalPath = new FieldPath$1(fieldNames);
- }
- /**
- * Returns true if this `FieldPath` is equal to the provided one.
- *
- * @param other - The `FieldPath` to compare against.
- * @returns true if this `FieldPath` is equal to the provided one.
- */
- isEqual(other) {
- return this._internalPath.isEqual(other._internalPath);
- }
- }
- /**
- * Returns a special sentinel `FieldPath` to refer to the ID of a document.
- * It can be used in queries to sort or filter by the document ID.
- */
- function documentId() {
- return new FieldPath(DOCUMENT_KEY_NAME);
- }
-
- /**
- * @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.
- */
- /**
- * Sentinel values that can be used when writing document fields with `set()`
- * or `update()`.
- */
- class FieldValue {
- /**
- * @param _methodName - The public API endpoint that returns this class.
- * @hideconstructor
- */
- constructor(_methodName) {
- this._methodName = _methodName;
- }
- }
-
- /**
- * @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 immutable object representing a geographic location in Firestore. The
- * location is represented as latitude/longitude pair.
- *
- * Latitude values are in the range of [-90, 90].
- * Longitude values are in the range of [-180, 180].
- */
- class GeoPoint {
- /**
- * Creates a new immutable `GeoPoint` object with the provided latitude and
- * longitude values.
- * @param latitude - The latitude as number between -90 and 90.
- * @param longitude - The longitude as number between -180 and 180.
- */
- constructor(latitude, longitude) {
- if (!isFinite(latitude) || latitude < -90 || latitude > 90) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Latitude must be a number between -90 and 90, but was: ' + latitude);
- }
- if (!isFinite(longitude) || longitude < -180 || longitude > 180) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Longitude must be a number between -180 and 180, but was: ' + longitude);
- }
- this._lat = latitude;
- this._long = longitude;
- }
- /**
- * The latitude of this `GeoPoint` instance.
- */
- get latitude() {
- return this._lat;
- }
- /**
- * The longitude of this `GeoPoint` instance.
- */
- get longitude() {
- return this._long;
- }
- /**
- * Returns true if this `GeoPoint` is equal to the provided one.
- *
- * @param other - The `GeoPoint` to compare against.
- * @returns true if this `GeoPoint` is equal to the provided one.
- */
- isEqual(other) {
- return this._lat === other._lat && this._long === other._long;
- }
- /** Returns a JSON-serializable representation of this GeoPoint. */
- toJSON() {
- return { latitude: this._lat, longitude: this._long };
- }
- /**
- * Actually private to JS consumers of our API, so this function is prefixed
- * with an underscore.
- */
- _compareTo(other) {
- return (primitiveComparator(this._lat, other._lat) ||
- primitiveComparator(this._long, other._long));
- }
- }
-
- /**
- * @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.
- */
- const RESERVED_FIELD_REGEX = /^__.*__$/;
- /** The result of parsing document data (e.g. for a setData call). */
- class ParsedSetData {
- constructor(data, fieldMask, fieldTransforms) {
- this.data = data;
- this.fieldMask = fieldMask;
- this.fieldTransforms = fieldTransforms;
- }
- toMutation(key, precondition) {
- if (this.fieldMask !== null) {
- return new PatchMutation(key, this.data, this.fieldMask, precondition, this.fieldTransforms);
- }
- else {
- return new SetMutation(key, this.data, precondition, this.fieldTransforms);
- }
- }
- }
- /** The result of parsing "update" data (i.e. for an updateData call). */
- class ParsedUpdateData {
- constructor(data,
- // The fieldMask does not include document transforms.
- fieldMask, fieldTransforms) {
- this.data = data;
- this.fieldMask = fieldMask;
- this.fieldTransforms = fieldTransforms;
- }
- toMutation(key, precondition) {
- return new PatchMutation(key, this.data, this.fieldMask, precondition, this.fieldTransforms);
- }
- }
- function isWrite(dataSource) {
- switch (dataSource) {
- case 0 /* UserDataSource.Set */: // fall through
- case 2 /* UserDataSource.MergeSet */: // fall through
- case 1 /* UserDataSource.Update */:
- return true;
- case 3 /* UserDataSource.Argument */:
- case 4 /* UserDataSource.ArrayArgument */:
- return false;
- default:
- throw fail();
- }
- }
- /** A "context" object passed around while parsing user data. */
- class ParseContextImpl {
- /**
- * Initializes a ParseContext with the given source and path.
- *
- * @param settings - The settings for the parser.
- * @param databaseId - The database ID of the Firestore instance.
- * @param serializer - The serializer to use to generate the Value proto.
- * @param ignoreUndefinedProperties - Whether to ignore undefined properties
- * rather than throw.
- * @param fieldTransforms - A mutable list of field transforms encountered
- * while parsing the data.
- * @param fieldMask - A mutable list of field paths encountered while parsing
- * the data.
- *
- * TODO(b/34871131): We don't support array paths right now, so path can be
- * null to indicate the context represents any location within an array (in
- * which case certain features will not work and errors will be somewhat
- * compromised).
- */
- constructor(settings, databaseId, serializer, ignoreUndefinedProperties, fieldTransforms, fieldMask) {
- this.settings = settings;
- this.databaseId = databaseId;
- this.serializer = serializer;
- this.ignoreUndefinedProperties = ignoreUndefinedProperties;
- // Minor hack: If fieldTransforms is undefined, we assume this is an
- // external call and we need to validate the entire path.
- if (fieldTransforms === undefined) {
- this.validatePath();
- }
- this.fieldTransforms = fieldTransforms || [];
- this.fieldMask = fieldMask || [];
- }
- get path() {
- return this.settings.path;
- }
- get dataSource() {
- return this.settings.dataSource;
- }
- /** Returns a new context with the specified settings overwritten. */
- contextWith(configuration) {
- return new ParseContextImpl(Object.assign(Object.assign({}, this.settings), configuration), this.databaseId, this.serializer, this.ignoreUndefinedProperties, this.fieldTransforms, this.fieldMask);
- }
- childContextForField(field) {
- var _a;
- const childPath = (_a = this.path) === null || _a === void 0 ? void 0 : _a.child(field);
- const context = this.contextWith({ path: childPath, arrayElement: false });
- context.validatePathSegment(field);
- return context;
- }
- childContextForFieldPath(field) {
- var _a;
- const childPath = (_a = this.path) === null || _a === void 0 ? void 0 : _a.child(field);
- const context = this.contextWith({ path: childPath, arrayElement: false });
- context.validatePath();
- return context;
- }
- childContextForArray(index) {
- // TODO(b/34871131): We don't support array paths right now; so make path
- // undefined.
- return this.contextWith({ path: undefined, arrayElement: true });
- }
- createError(reason) {
- return createError(reason, this.settings.methodName, this.settings.hasConverter || false, this.path, this.settings.targetDoc);
- }
- /** Returns 'true' if 'fieldPath' was traversed when creating this context. */
- contains(fieldPath) {
- return (this.fieldMask.find(field => fieldPath.isPrefixOf(field)) !== undefined ||
- this.fieldTransforms.find(transform => fieldPath.isPrefixOf(transform.field)) !== undefined);
- }
- validatePath() {
- // TODO(b/34871131): Remove null check once we have proper paths for fields
- // within arrays.
- if (!this.path) {
- return;
- }
- for (let i = 0; i < this.path.length; i++) {
- this.validatePathSegment(this.path.get(i));
- }
- }
- validatePathSegment(segment) {
- if (segment.length === 0) {
- throw this.createError('Document fields must not be empty');
- }
- if (isWrite(this.dataSource) && RESERVED_FIELD_REGEX.test(segment)) {
- throw this.createError('Document fields cannot begin and end with "__"');
- }
- }
- }
- /**
- * Helper for parsing raw user input (provided via the API) into internal model
- * classes.
- */
- class UserDataReader {
- constructor(databaseId, ignoreUndefinedProperties, serializer) {
- this.databaseId = databaseId;
- this.ignoreUndefinedProperties = ignoreUndefinedProperties;
- this.serializer = serializer || newSerializer(databaseId);
- }
- /** Creates a new top-level parse context. */
- createContext(dataSource, methodName, targetDoc, hasConverter = false) {
- return new ParseContextImpl({
- dataSource,
- methodName,
- targetDoc,
- path: FieldPath$1.emptyPath(),
- arrayElement: false,
- hasConverter
- }, this.databaseId, this.serializer, this.ignoreUndefinedProperties);
- }
- }
- function newUserDataReader(firestore) {
- const settings = firestore._freezeSettings();
- const serializer = newSerializer(firestore._databaseId);
- return new UserDataReader(firestore._databaseId, !!settings.ignoreUndefinedProperties, serializer);
- }
- /** Parse document data from a set() call. */
- function parseSetData(userDataReader, methodName, targetDoc, input, hasConverter, options = {}) {
- const context = userDataReader.createContext(options.merge || options.mergeFields
- ? 2 /* UserDataSource.MergeSet */
- : 0 /* UserDataSource.Set */, methodName, targetDoc, hasConverter);
- validatePlainObject('Data must be an object, but it was:', context, input);
- const updateData = parseObject(input, context);
- let fieldMask;
- let fieldTransforms;
- if (options.merge) {
- fieldMask = new FieldMask(context.fieldMask);
- fieldTransforms = context.fieldTransforms;
- }
- else if (options.mergeFields) {
- const validatedFieldPaths = [];
- for (const stringOrFieldPath of options.mergeFields) {
- const fieldPath = fieldPathFromArgument$1(methodName, stringOrFieldPath, targetDoc);
- if (!context.contains(fieldPath)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Field '${fieldPath}' is specified in your field mask but missing from your input data.`);
- }
- if (!fieldMaskContains(validatedFieldPaths, fieldPath)) {
- validatedFieldPaths.push(fieldPath);
- }
- }
- fieldMask = new FieldMask(validatedFieldPaths);
- fieldTransforms = context.fieldTransforms.filter(transform => fieldMask.covers(transform.field));
- }
- else {
- fieldMask = null;
- fieldTransforms = context.fieldTransforms;
- }
- return new ParsedSetData(new ObjectValue(updateData), fieldMask, fieldTransforms);
- }
- class DeleteFieldValueImpl extends FieldValue {
- _toFieldTransform(context) {
- if (context.dataSource === 2 /* UserDataSource.MergeSet */) {
- // No transform to add for a delete, but we need to add it to our
- // fieldMask so it gets deleted.
- context.fieldMask.push(context.path);
- }
- else if (context.dataSource === 1 /* UserDataSource.Update */) {
- throw context.createError(`${this._methodName}() can only appear at the top level ` +
- 'of your update data');
- }
- else {
- // We shouldn't encounter delete sentinels for queries or non-merge set() calls.
- throw context.createError(`${this._methodName}() cannot be used with set() unless you pass ` +
- '{merge:true}');
- }
- return null;
- }
- isEqual(other) {
- return other instanceof DeleteFieldValueImpl;
- }
- }
- /**
- * Creates a child context for parsing SerializableFieldValues.
- *
- * This is different than calling `ParseContext.contextWith` because it keeps
- * the fieldTransforms and fieldMask separate.
- *
- * The created context has its `dataSource` set to `UserDataSource.Argument`.
- * Although these values are used with writes, any elements in these FieldValues
- * are not considered writes since they cannot contain any FieldValue sentinels,
- * etc.
- *
- * @param fieldValue - The sentinel FieldValue for which to create a child
- * context.
- * @param context - The parent context.
- * @param arrayElement - Whether or not the FieldValue has an array.
- */
- function createSentinelChildContext(fieldValue, context, arrayElement) {
- return new ParseContextImpl({
- dataSource: 3 /* UserDataSource.Argument */,
- targetDoc: context.settings.targetDoc,
- methodName: fieldValue._methodName,
- arrayElement
- }, context.databaseId, context.serializer, context.ignoreUndefinedProperties);
- }
- class ServerTimestampFieldValueImpl extends FieldValue {
- _toFieldTransform(context) {
- return new FieldTransform(context.path, new ServerTimestampTransform());
- }
- isEqual(other) {
- return other instanceof ServerTimestampFieldValueImpl;
- }
- }
- class ArrayUnionFieldValueImpl extends FieldValue {
- constructor(methodName, _elements) {
- super(methodName);
- this._elements = _elements;
- }
- _toFieldTransform(context) {
- const parseContext = createSentinelChildContext(this, context,
- /*array=*/ true);
- const parsedElements = this._elements.map(element => parseData(element, parseContext));
- const arrayUnion = new ArrayUnionTransformOperation(parsedElements);
- return new FieldTransform(context.path, arrayUnion);
- }
- isEqual(other) {
- // TODO(mrschmidt): Implement isEquals
- return this === other;
- }
- }
- class ArrayRemoveFieldValueImpl extends FieldValue {
- constructor(methodName, _elements) {
- super(methodName);
- this._elements = _elements;
- }
- _toFieldTransform(context) {
- const parseContext = createSentinelChildContext(this, context,
- /*array=*/ true);
- const parsedElements = this._elements.map(element => parseData(element, parseContext));
- const arrayUnion = new ArrayRemoveTransformOperation(parsedElements);
- return new FieldTransform(context.path, arrayUnion);
- }
- isEqual(other) {
- // TODO(mrschmidt): Implement isEquals
- return this === other;
- }
- }
- class NumericIncrementFieldValueImpl extends FieldValue {
- constructor(methodName, _operand) {
- super(methodName);
- this._operand = _operand;
- }
- _toFieldTransform(context) {
- const numericIncrement = new NumericIncrementTransformOperation(context.serializer, toNumber(context.serializer, this._operand));
- return new FieldTransform(context.path, numericIncrement);
- }
- isEqual(other) {
- // TODO(mrschmidt): Implement isEquals
- return this === other;
- }
- }
- /** Parse update data from an update() call. */
- function parseUpdateData(userDataReader, methodName, targetDoc, input) {
- const context = userDataReader.createContext(1 /* UserDataSource.Update */, methodName, targetDoc);
- validatePlainObject('Data must be an object, but it was:', context, input);
- const fieldMaskPaths = [];
- const updateData = ObjectValue.empty();
- forEach(input, (key, value) => {
- const path = fieldPathFromDotSeparatedString(methodName, key, targetDoc);
- // For Compat types, we have to "extract" the underlying types before
- // performing validation.
- value = getModularInstance(value);
- const childContext = context.childContextForFieldPath(path);
- if (value instanceof DeleteFieldValueImpl) {
- // Add it to the field mask, but don't add anything to updateData.
- fieldMaskPaths.push(path);
- }
- else {
- const parsedValue = parseData(value, childContext);
- if (parsedValue != null) {
- fieldMaskPaths.push(path);
- updateData.set(path, parsedValue);
- }
- }
- });
- const mask = new FieldMask(fieldMaskPaths);
- return new ParsedUpdateData(updateData, mask, context.fieldTransforms);
- }
- /** Parse update data from a list of field/value arguments. */
- function parseUpdateVarargs(userDataReader, methodName, targetDoc, field, value, moreFieldsAndValues) {
- const context = userDataReader.createContext(1 /* UserDataSource.Update */, methodName, targetDoc);
- const keys = [fieldPathFromArgument$1(methodName, field, targetDoc)];
- const values = [value];
- if (moreFieldsAndValues.length % 2 !== 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Function ${methodName}() needs to be called with an even number ` +
- 'of arguments that alternate between field names and values.');
- }
- for (let i = 0; i < moreFieldsAndValues.length; i += 2) {
- keys.push(fieldPathFromArgument$1(methodName, moreFieldsAndValues[i]));
- values.push(moreFieldsAndValues[i + 1]);
- }
- const fieldMaskPaths = [];
- const updateData = ObjectValue.empty();
- // We iterate in reverse order to pick the last value for a field if the
- // user specified the field multiple times.
- for (let i = keys.length - 1; i >= 0; --i) {
- if (!fieldMaskContains(fieldMaskPaths, keys[i])) {
- const path = keys[i];
- let value = values[i];
- // For Compat types, we have to "extract" the underlying types before
- // performing validation.
- value = getModularInstance(value);
- const childContext = context.childContextForFieldPath(path);
- if (value instanceof DeleteFieldValueImpl) {
- // Add it to the field mask, but don't add anything to updateData.
- fieldMaskPaths.push(path);
- }
- else {
- const parsedValue = parseData(value, childContext);
- if (parsedValue != null) {
- fieldMaskPaths.push(path);
- updateData.set(path, parsedValue);
- }
- }
- }
- }
- const mask = new FieldMask(fieldMaskPaths);
- return new ParsedUpdateData(updateData, mask, context.fieldTransforms);
- }
- /**
- * Parse a "query value" (e.g. value in a where filter or a value in a cursor
- * bound).
- *
- * @param allowArrays - Whether the query value is an array that may directly
- * contain additional arrays (e.g. the operand of an `in` query).
- */
- function parseQueryValue(userDataReader, methodName, input, allowArrays = false) {
- const context = userDataReader.createContext(allowArrays ? 4 /* UserDataSource.ArrayArgument */ : 3 /* UserDataSource.Argument */, methodName);
- const parsed = parseData(input, context);
- return parsed;
- }
- /**
- * Parses user data to Protobuf Values.
- *
- * @param input - Data to be parsed.
- * @param context - A context object representing the current path being parsed,
- * the source of the data being parsed, etc.
- * @returns The parsed value, or null if the value was a FieldValue sentinel
- * that should not be included in the resulting parsed data.
- */
- function parseData(input, context) {
- // Unwrap the API type from the Compat SDK. This will return the API type
- // from firestore-exp.
- input = getModularInstance(input);
- if (looksLikeJsonObject(input)) {
- validatePlainObject('Unsupported field value:', context, input);
- return parseObject(input, context);
- }
- else if (input instanceof FieldValue) {
- // FieldValues usually parse into transforms (except deleteField())
- // in which case we do not want to include this field in our parsed data
- // (as doing so will overwrite the field directly prior to the transform
- // trying to transform it). So we don't add this location to
- // context.fieldMask and we return null as our parsing result.
- parseSentinelFieldValue(input, context);
- return null;
- }
- else if (input === undefined && context.ignoreUndefinedProperties) {
- // If the input is undefined it can never participate in the fieldMask, so
- // don't handle this below. If `ignoreUndefinedProperties` is false,
- // `parseScalarValue` will reject an undefined value.
- return null;
- }
- else {
- // If context.path is null we are inside an array and we don't support
- // field mask paths more granular than the top-level array.
- if (context.path) {
- context.fieldMask.push(context.path);
- }
- if (input instanceof Array) {
- // TODO(b/34871131): Include the path containing the array in the error
- // message.
- // In the case of IN queries, the parsed data is an array (representing
- // the set of values to be included for the IN query) that may directly
- // contain additional arrays (each representing an individual field
- // value), so we disable this validation.
- if (context.settings.arrayElement &&
- context.dataSource !== 4 /* UserDataSource.ArrayArgument */) {
- throw context.createError('Nested arrays are not supported');
- }
- return parseArray(input, context);
- }
- else {
- return parseScalarValue(input, context);
- }
- }
- }
- function parseObject(obj, context) {
- const fields = {};
- if (isEmpty(obj)) {
- // If we encounter an empty object, we explicitly add it to the update
- // mask to ensure that the server creates a map entry.
- if (context.path && context.path.length > 0) {
- context.fieldMask.push(context.path);
- }
- }
- else {
- forEach(obj, (key, val) => {
- const parsedValue = parseData(val, context.childContextForField(key));
- if (parsedValue != null) {
- fields[key] = parsedValue;
- }
- });
- }
- return { mapValue: { fields } };
- }
- function parseArray(array, context) {
- const values = [];
- let entryIndex = 0;
- for (const entry of array) {
- let parsedEntry = parseData(entry, context.childContextForArray(entryIndex));
- if (parsedEntry == null) {
- // Just include nulls in the array for fields being replaced with a
- // sentinel.
- parsedEntry = { nullValue: 'NULL_VALUE' };
- }
- values.push(parsedEntry);
- entryIndex++;
- }
- return { arrayValue: { values } };
- }
- /**
- * "Parses" the provided FieldValueImpl, adding any necessary transforms to
- * context.fieldTransforms.
- */
- function parseSentinelFieldValue(value, context) {
- // Sentinels are only supported with writes, and not within arrays.
- if (!isWrite(context.dataSource)) {
- throw context.createError(`${value._methodName}() can only be used with update() and set()`);
- }
- if (!context.path) {
- throw context.createError(`${value._methodName}() is not currently supported inside arrays`);
- }
- const fieldTransform = value._toFieldTransform(context);
- if (fieldTransform) {
- context.fieldTransforms.push(fieldTransform);
- }
- }
- /**
- * Helper to parse a scalar value (i.e. not an Object, Array, or FieldValue)
- *
- * @returns The parsed value
- */
- function parseScalarValue(value, context) {
- value = getModularInstance(value);
- if (value === null) {
- return { nullValue: 'NULL_VALUE' };
- }
- else if (typeof value === 'number') {
- return toNumber(context.serializer, value);
- }
- else if (typeof value === 'boolean') {
- return { booleanValue: value };
- }
- else if (typeof value === 'string') {
- return { stringValue: value };
- }
- else if (value instanceof Date) {
- const timestamp = Timestamp.fromDate(value);
- return {
- timestampValue: toTimestamp(context.serializer, timestamp)
- };
- }
- else if (value instanceof Timestamp) {
- // Firestore backend truncates precision down to microseconds. To ensure
- // offline mode works the same with regards to truncation, perform the
- // truncation immediately without waiting for the backend to do that.
- const timestamp = new Timestamp(value.seconds, Math.floor(value.nanoseconds / 1000) * 1000);
- return {
- timestampValue: toTimestamp(context.serializer, timestamp)
- };
- }
- else if (value instanceof GeoPoint) {
- return {
- geoPointValue: {
- latitude: value.latitude,
- longitude: value.longitude
- }
- };
- }
- else if (value instanceof Bytes) {
- return { bytesValue: toBytes(context.serializer, value._byteString) };
- }
- else if (value instanceof DocumentReference) {
- const thisDb = context.databaseId;
- const otherDb = value.firestore._databaseId;
- if (!otherDb.isEqual(thisDb)) {
- throw context.createError('Document reference is for database ' +
- `${otherDb.projectId}/${otherDb.database} but should be ` +
- `for database ${thisDb.projectId}/${thisDb.database}`);
- }
- return {
- referenceValue: toResourceName(value.firestore._databaseId || context.databaseId, value._key.path)
- };
- }
- else {
- throw context.createError(`Unsupported field value: ${valueDescription(value)}`);
- }
- }
- /**
- * Checks whether an object looks like a JSON object that should be converted
- * into a struct. Normal class/prototype instances are considered to look like
- * JSON objects since they should be converted to a struct value. Arrays, Dates,
- * GeoPoints, etc. are not considered to look like JSON objects since they map
- * to specific FieldValue types other than ObjectValue.
- */
- function looksLikeJsonObject(input) {
- return (typeof input === 'object' &&
- input !== null &&
- !(input instanceof Array) &&
- !(input instanceof Date) &&
- !(input instanceof Timestamp) &&
- !(input instanceof GeoPoint) &&
- !(input instanceof Bytes) &&
- !(input instanceof DocumentReference) &&
- !(input instanceof FieldValue));
- }
- function validatePlainObject(message, context, input) {
- if (!looksLikeJsonObject(input) || !isPlainObject(input)) {
- const description = valueDescription(input);
- if (description === 'an object') {
- // Massage the error if it was an object.
- throw context.createError(message + ' a custom object');
- }
- else {
- throw context.createError(message + ' ' + description);
- }
- }
- }
- /**
- * Helper that calls fromDotSeparatedString() but wraps any error thrown.
- */
- function fieldPathFromArgument$1(methodName, path, targetDoc) {
- // If required, replace the FieldPath Compat class with with the firestore-exp
- // FieldPath.
- path = getModularInstance(path);
- if (path instanceof FieldPath) {
- return path._internalPath;
- }
- else if (typeof path === 'string') {
- return fieldPathFromDotSeparatedString(methodName, path);
- }
- else {
- const message = 'Field path arguments must be of type string or ';
- throw createError(message, methodName,
- /* hasConverter= */ false,
- /* path= */ undefined, targetDoc);
- }
- }
- /**
- * Matches any characters in a field path string that are reserved.
- */
- const FIELD_PATH_RESERVED = new RegExp('[~\\*/\\[\\]]');
- /**
- * Wraps fromDotSeparatedString with an error message about the method that
- * was thrown.
- * @param methodName - The publicly visible method name
- * @param path - The dot-separated string form of a field path which will be
- * split on dots.
- * @param targetDoc - The document against which the field path will be
- * evaluated.
- */
- function fieldPathFromDotSeparatedString(methodName, path, targetDoc) {
- const found = path.search(FIELD_PATH_RESERVED);
- if (found >= 0) {
- throw createError(`Invalid field path (${path}). Paths must not contain ` +
- `'~', '*', '/', '[', or ']'`, methodName,
- /* hasConverter= */ false,
- /* path= */ undefined, targetDoc);
- }
- try {
- return new FieldPath(...path.split('.'))._internalPath;
- }
- catch (e) {
- throw createError(`Invalid field path (${path}). Paths must not be empty, ` +
- `begin with '.', end with '.', or contain '..'`, methodName,
- /* hasConverter= */ false,
- /* path= */ undefined, targetDoc);
- }
- }
- function createError(reason, methodName, hasConverter, path, targetDoc) {
- const hasPath = path && !path.isEmpty();
- const hasDocument = targetDoc !== undefined;
- let message = `Function ${methodName}() called with invalid data`;
- if (hasConverter) {
- message += ' (via `toFirestore()`)';
- }
- message += '. ';
- let description = '';
- if (hasPath || hasDocument) {
- description += ' (found';
- if (hasPath) {
- description += ` in field ${path}`;
- }
- if (hasDocument) {
- description += ` in document ${targetDoc}`;
- }
- description += ')';
- }
- return new FirestoreError(Code.INVALID_ARGUMENT, message + reason + description);
- }
- /** Checks `haystack` if FieldPath `needle` is present. Runs in O(n). */
- function fieldMaskContains(haystack, needle) {
- return haystack.some(v => v.isEqual(needle));
- }
-
- /**
- * @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 `DocumentSnapshot` contains data read from a document in your Firestore
- * database. The data can be extracted with `.data()` or `.get(<field>)` to
- * get a specific field.
- *
- * For a `DocumentSnapshot` that points to a non-existing document, any data
- * access will return 'undefined'. You can use the `exists()` method to
- * explicitly verify a document's existence.
- */
- class DocumentSnapshot {
- // Note: This class is stripped down version of the DocumentSnapshot in
- // the legacy SDK. The changes are:
- // - No support for SnapshotMetadata.
- // - No support for SnapshotOptions.
- /** @hideconstructor protected */
- constructor(_firestore, _userDataWriter, _key, _document, _converter) {
- this._firestore = _firestore;
- this._userDataWriter = _userDataWriter;
- this._key = _key;
- this._document = _document;
- this._converter = _converter;
- }
- /** Property of the `DocumentSnapshot` that provides the document's ID. */
- get id() {
- return this._key.path.lastSegment();
- }
- /**
- * The `DocumentReference` for the document included in the `DocumentSnapshot`.
- */
- get ref() {
- return new DocumentReference(this._firestore, this._converter, this._key);
- }
- /**
- * Signals whether or not the document at the snapshot's location exists.
- *
- * @returns true if the document exists.
- */
- exists() {
- return this._document !== null;
- }
- /**
- * Retrieves all fields in the document as an `Object`. Returns `undefined` if
- * the document doesn't exist.
- *
- * @returns An `Object` containing all fields in the document or `undefined`
- * if the document doesn't exist.
- */
- data() {
- if (!this._document) {
- return undefined;
- }
- else if (this._converter) {
- // We only want to use the converter and create a new DocumentSnapshot
- // if a converter has been provided.
- const snapshot = new QueryDocumentSnapshot(this._firestore, this._userDataWriter, this._key, this._document,
- /* converter= */ null);
- return this._converter.fromFirestore(snapshot);
- }
- else {
- return this._userDataWriter.convertValue(this._document.data.value);
- }
- }
- /**
- * Retrieves the field specified by `fieldPath`. Returns `undefined` if the
- * document or field doesn't exist.
- *
- * @param fieldPath - The path (for example 'foo' or 'foo.bar') to a specific
- * field.
- * @returns The data at the specified field location or undefined if no such
- * field exists in the document.
- */
- // We are using `any` here to avoid an explicit cast by our users.
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- get(fieldPath) {
- if (this._document) {
- const value = this._document.data.field(fieldPathFromArgument('DocumentSnapshot.get', fieldPath));
- if (value !== null) {
- return this._userDataWriter.convertValue(value);
- }
- }
- return undefined;
- }
- }
- /**
- * A `QueryDocumentSnapshot` contains data read from a document in your
- * Firestore database as part of a query. The document is guaranteed to exist
- * and its data can be extracted with `.data()` or `.get(<field>)` to get a
- * specific field.
- *
- * A `QueryDocumentSnapshot` offers the same API surface as a
- * `DocumentSnapshot`. Since query results contain only existing documents, the
- * `exists` property will always be true and `data()` will never return
- * 'undefined'.
- */
- class QueryDocumentSnapshot extends DocumentSnapshot {
- /**
- * Retrieves all fields in the document as an `Object`.
- *
- * @override
- * @returns An `Object` containing all fields in the document.
- */
- data() {
- return super.data();
- }
- }
- /**
- * A `QuerySnapshot` contains zero or more `DocumentSnapshot` objects
- * representing the results of a query. The documents can be accessed as an
- * array via the `docs` property or enumerated using the `forEach` method. The
- * number of documents can be determined via the `empty` and `size`
- * properties.
- */
- class QuerySnapshot {
- /** @hideconstructor */
- constructor(_query, _docs) {
- this._docs = _docs;
- this.query = _query;
- }
- /** An array of all the documents in the `QuerySnapshot`. */
- get docs() {
- return [...this._docs];
- }
- /** The number of documents in the `QuerySnapshot`. */
- get size() {
- return this.docs.length;
- }
- /** True if there are no documents in the `QuerySnapshot`. */
- get empty() {
- return this.docs.length === 0;
- }
- /**
- * Enumerates all of the documents in the `QuerySnapshot`.
- *
- * @param callback - A callback to be called with a `QueryDocumentSnapshot` for
- * each document in the snapshot.
- * @param thisArg - The `this` binding for the callback.
- */
- forEach(callback, thisArg) {
- this._docs.forEach(callback, thisArg);
- }
- }
- /**
- * Returns true if the provided snapshots are equal.
- *
- * @param left - A snapshot to compare.
- * @param right - A snapshot to compare.
- * @returns true if the snapshots are equal.
- */
- function snapshotEqual(left, right) {
- left = getModularInstance(left);
- right = getModularInstance(right);
- if (left instanceof DocumentSnapshot && right instanceof DocumentSnapshot) {
- return (left._firestore === right._firestore &&
- left._key.isEqual(right._key) &&
- (left._document === null
- ? right._document === null
- : left._document.isEqual(right._document)) &&
- left._converter === right._converter);
- }
- else if (left instanceof QuerySnapshot && right instanceof QuerySnapshot) {
- return (queryEqual(left.query, right.query) &&
- arrayEquals(left.docs, right.docs, snapshotEqual));
- }
- return false;
- }
- /**
- * Helper that calls `fromDotSeparatedString()` but wraps any error thrown.
- */
- function fieldPathFromArgument(methodName, arg) {
- if (typeof arg === 'string') {
- return fieldPathFromDotSeparatedString(methodName, arg);
- }
- else if (arg instanceof FieldPath) {
- return arg._internalPath;
- }
- else {
- return arg._delegate._internalPath;
- }
- }
-
- /**
- * @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.
- */
- function validateHasExplicitOrderByForLimitToLast(query) {
- if (query.limitType === "L" /* LimitType.Last */ &&
- query.explicitOrderBy.length === 0) {
- throw new FirestoreError(Code.UNIMPLEMENTED, 'limitToLast() queries require specifying at least one orderBy() clause');
- }
- }
- /**
- * An `AppliableConstraint` is an abstraction of a constraint that can be applied
- * to a Firestore query.
- */
- class AppliableConstraint {
- }
- /**
- * A `QueryConstraint` is used to narrow the set of documents returned by a
- * Firestore query. `QueryConstraint`s are created by invoking {@link where},
- * {@link orderBy}, {@link startAt}, {@link startAfter}, {@link
- * endBefore}, {@link endAt}, {@link limit}, {@link limitToLast} and
- * can then be passed to {@link query} to create a new query instance that
- * also contains this `QueryConstraint`.
- */
- class QueryConstraint extends AppliableConstraint {
- }
- function query(query, queryConstraint, ...additionalQueryConstraints) {
- let queryConstraints = [];
- if (queryConstraint instanceof AppliableConstraint) {
- queryConstraints.push(queryConstraint);
- }
- queryConstraints = queryConstraints.concat(additionalQueryConstraints);
- validateQueryConstraintArray(queryConstraints);
- for (const constraint of queryConstraints) {
- query = constraint._apply(query);
- }
- return query;
- }
- /**
- * A `QueryFieldFilterConstraint` is used to narrow the set of documents returned by
- * a Firestore query by filtering on one or more document fields.
- * `QueryFieldFilterConstraint`s are created by invoking {@link where} and can then
- * be passed to {@link query} to create a new query instance that also contains
- * this `QueryFieldFilterConstraint`.
- */
- class QueryFieldFilterConstraint extends QueryConstraint {
- /**
- * @internal
- */
- constructor(_field, _op, _value) {
- super();
- this._field = _field;
- this._op = _op;
- this._value = _value;
- /** The type of this query constraint */
- this.type = 'where';
- }
- static _create(_field, _op, _value) {
- return new QueryFieldFilterConstraint(_field, _op, _value);
- }
- _apply(query) {
- const filter = this._parse(query);
- validateNewFieldFilter(query._query, filter);
- return new Query(query.firestore, query.converter, queryWithAddedFilter(query._query, filter));
- }
- _parse(query) {
- const reader = newUserDataReader(query.firestore);
- const filter = newQueryFilter(query._query, 'where', reader, query.firestore._databaseId, this._field, this._op, this._value);
- return filter;
- }
- }
- /**
- * Creates a {@link QueryFieldFilterConstraint} that enforces that documents
- * must contain the specified field and that the value should satisfy the
- * relation constraint provided.
- *
- * @param fieldPath - The path to compare
- * @param opStr - The operation string (e.g "<", "<=", "==", "<",
- * "<=", "!=").
- * @param value - The value for comparison
- * @returns The created {@link QueryFieldFilterConstraint}.
- */
- function where(fieldPath, opStr, value) {
- const op = opStr;
- const field = fieldPathFromArgument('where', fieldPath);
- return QueryFieldFilterConstraint._create(field, op, value);
- }
- /**
- * A `QueryCompositeFilterConstraint` is used to narrow the set of documents
- * returned by a Firestore query by performing the logical OR or AND of multiple
- * {@link QueryFieldFilterConstraint}s or {@link QueryCompositeFilterConstraint}s.
- * `QueryCompositeFilterConstraint`s are created by invoking {@link or} or
- * {@link and} and can then be passed to {@link query} to create a new query
- * instance that also contains the `QueryCompositeFilterConstraint`.
- * @internal TODO remove this internal tag with OR Query support in the server
- */
- class QueryCompositeFilterConstraint extends AppliableConstraint {
- /**
- * @internal
- */
- constructor(
- /** The type of this query constraint */
- type, _queryConstraints) {
- super();
- this.type = type;
- this._queryConstraints = _queryConstraints;
- }
- static _create(type, _queryConstraints) {
- return new QueryCompositeFilterConstraint(type, _queryConstraints);
- }
- _parse(query) {
- const parsedFilters = this._queryConstraints
- .map(queryConstraint => {
- return queryConstraint._parse(query);
- })
- .filter(parsedFilter => parsedFilter.getFilters().length > 0);
- if (parsedFilters.length === 1) {
- return parsedFilters[0];
- }
- return CompositeFilter.create(parsedFilters, this._getOperator());
- }
- _apply(query) {
- const parsedFilter = this._parse(query);
- if (parsedFilter.getFilters().length === 0) {
- // Return the existing query if not adding any more filters (e.g. an empty
- // composite filter).
- return query;
- }
- validateNewFilter(query._query, parsedFilter);
- return new Query(query.firestore, query.converter, queryWithAddedFilter(query._query, parsedFilter));
- }
- _getQueryConstraints() {
- return this._queryConstraints;
- }
- _getOperator() {
- return this.type === 'and' ? "and" /* CompositeOperator.AND */ : "or" /* CompositeOperator.OR */;
- }
- }
- /**
- * Creates a new {@link QueryCompositeFilterConstraint} that is a disjunction of
- * the given filter constraints. A disjunction filter includes a document if it
- * satisfies any of the given filters.
- *
- * @param queryConstraints - Optional. The list of
- * {@link QueryFilterConstraint}s to perform a disjunction for. These must be
- * created with calls to {@link where}, {@link or}, or {@link and}.
- * @returns The newly created {@link QueryCompositeFilterConstraint}.
- * @internal TODO remove this internal tag with OR Query support in the server
- */
- function or(...queryConstraints) {
- // Only support QueryFilterConstraints
- queryConstraints.forEach(queryConstraint => validateQueryFilterConstraint('or', queryConstraint));
- return QueryCompositeFilterConstraint._create("or" /* CompositeOperator.OR */, queryConstraints);
- }
- /**
- * Creates a new {@link QueryCompositeFilterConstraint} that is a conjunction of
- * the given filter constraints. A conjunction filter includes a document if it
- * satisfies all of the given filters.
- *
- * @param queryConstraints - Optional. The list of
- * {@link QueryFilterConstraint}s to perform a conjunction for. These must be
- * created with calls to {@link where}, {@link or}, or {@link and}.
- * @returns The newly created {@link QueryCompositeFilterConstraint}.
- * @internal TODO remove this internal tag with OR Query support in the server
- */
- function and(...queryConstraints) {
- // Only support QueryFilterConstraints
- queryConstraints.forEach(queryConstraint => validateQueryFilterConstraint('and', queryConstraint));
- return QueryCompositeFilterConstraint._create("and" /* CompositeOperator.AND */, queryConstraints);
- }
- /**
- * A `QueryOrderByConstraint` is used to sort the set of documents returned by a
- * Firestore query. `QueryOrderByConstraint`s are created by invoking
- * {@link orderBy} and can then be passed to {@link query} to create a new query
- * instance that also contains this `QueryOrderByConstraint`.
- *
- * Note: Documents that do not contain the orderBy field will not be present in
- * the query result.
- */
- class QueryOrderByConstraint extends QueryConstraint {
- /**
- * @internal
- */
- constructor(_field, _direction) {
- super();
- this._field = _field;
- this._direction = _direction;
- /** The type of this query constraint */
- this.type = 'orderBy';
- }
- static _create(_field, _direction) {
- return new QueryOrderByConstraint(_field, _direction);
- }
- _apply(query) {
- const orderBy = newQueryOrderBy(query._query, this._field, this._direction);
- return new Query(query.firestore, query.converter, queryWithAddedOrderBy(query._query, orderBy));
- }
- }
- /**
- * Creates a {@link QueryOrderByConstraint} that sorts the query result by the
- * specified field, optionally in descending order instead of ascending.
- *
- * Note: Documents that do not contain the specified field will not be present
- * in the query result.
- *
- * @param fieldPath - The field to sort by.
- * @param directionStr - Optional direction to sort by ('asc' or 'desc'). If
- * not specified, order will be ascending.
- * @returns The created {@link QueryOrderByConstraint}.
- */
- function orderBy(fieldPath, directionStr = 'asc') {
- const direction = directionStr;
- const path = fieldPathFromArgument('orderBy', fieldPath);
- return QueryOrderByConstraint._create(path, direction);
- }
- /**
- * A `QueryLimitConstraint` is used to limit the number of documents returned by
- * a Firestore query.
- * `QueryLimitConstraint`s are created by invoking {@link limit} or
- * {@link limitToLast} and can then be passed to {@link query} to create a new
- * query instance that also contains this `QueryLimitConstraint`.
- */
- class QueryLimitConstraint extends QueryConstraint {
- /**
- * @internal
- */
- constructor(
- /** The type of this query constraint */
- type, _limit, _limitType) {
- super();
- this.type = type;
- this._limit = _limit;
- this._limitType = _limitType;
- }
- static _create(type, _limit, _limitType) {
- return new QueryLimitConstraint(type, _limit, _limitType);
- }
- _apply(query) {
- return new Query(query.firestore, query.converter, queryWithLimit(query._query, this._limit, this._limitType));
- }
- }
- /**
- * Creates a {@link QueryLimitConstraint} that only returns the first matching
- * documents.
- *
- * @param limit - The maximum number of items to return.
- * @returns The created {@link QueryLimitConstraint}.
- */
- function limit(limit) {
- validatePositiveNumber('limit', limit);
- return QueryLimitConstraint._create('limit', limit, "F" /* LimitType.First */);
- }
- /**
- * Creates a {@link QueryLimitConstraint} that only returns the last matching
- * documents.
- *
- * You must specify at least one `orderBy` clause for `limitToLast` queries,
- * otherwise an exception will be thrown during execution.
- *
- * @param limit - The maximum number of items to return.
- * @returns The created {@link QueryLimitConstraint}.
- */
- function limitToLast(limit) {
- validatePositiveNumber('limitToLast', limit);
- return QueryLimitConstraint._create('limitToLast', limit, "L" /* LimitType.Last */);
- }
- /**
- * A `QueryStartAtConstraint` is used to exclude documents from the start of a
- * result set returned by a Firestore query.
- * `QueryStartAtConstraint`s are created by invoking {@link (startAt:1)} or
- * {@link (startAfter:1)} and can then be passed to {@link query} to create a
- * new query instance that also contains this `QueryStartAtConstraint`.
- */
- class QueryStartAtConstraint extends QueryConstraint {
- /**
- * @internal
- */
- constructor(
- /** The type of this query constraint */
- type, _docOrFields, _inclusive) {
- super();
- this.type = type;
- this._docOrFields = _docOrFields;
- this._inclusive = _inclusive;
- }
- static _create(type, _docOrFields, _inclusive) {
- return new QueryStartAtConstraint(type, _docOrFields, _inclusive);
- }
- _apply(query) {
- const bound = newQueryBoundFromDocOrFields(query, this.type, this._docOrFields, this._inclusive);
- return new Query(query.firestore, query.converter, queryWithStartAt(query._query, bound));
- }
- }
- function startAt(...docOrFields) {
- return QueryStartAtConstraint._create('startAt', docOrFields,
- /*inclusive=*/ true);
- }
- function startAfter(...docOrFields) {
- return QueryStartAtConstraint._create('startAfter', docOrFields,
- /*inclusive=*/ false);
- }
- /**
- * A `QueryEndAtConstraint` is used to exclude documents from the end of a
- * result set returned by a Firestore query.
- * `QueryEndAtConstraint`s are created by invoking {@link (endAt:1)} or
- * {@link (endBefore:1)} and can then be passed to {@link query} to create a new
- * query instance that also contains this `QueryEndAtConstraint`.
- */
- class QueryEndAtConstraint extends QueryConstraint {
- /**
- * @internal
- */
- constructor(
- /** The type of this query constraint */
- type, _docOrFields, _inclusive) {
- super();
- this.type = type;
- this._docOrFields = _docOrFields;
- this._inclusive = _inclusive;
- }
- static _create(type, _docOrFields, _inclusive) {
- return new QueryEndAtConstraint(type, _docOrFields, _inclusive);
- }
- _apply(query) {
- const bound = newQueryBoundFromDocOrFields(query, this.type, this._docOrFields, this._inclusive);
- return new Query(query.firestore, query.converter, queryWithEndAt(query._query, bound));
- }
- }
- function endBefore(...docOrFields) {
- return QueryEndAtConstraint._create('endBefore', docOrFields,
- /*inclusive=*/ false);
- }
- function endAt(...docOrFields) {
- return QueryEndAtConstraint._create('endAt', docOrFields,
- /*inclusive=*/ true);
- }
- /** Helper function to create a bound from a document or fields */
- function newQueryBoundFromDocOrFields(query, methodName, docOrFields, inclusive) {
- docOrFields[0] = getModularInstance(docOrFields[0]);
- if (docOrFields[0] instanceof DocumentSnapshot) {
- return newQueryBoundFromDocument(query._query, query.firestore._databaseId, methodName, docOrFields[0]._document, inclusive);
- }
- else {
- const reader = newUserDataReader(query.firestore);
- return newQueryBoundFromFields(query._query, query.firestore._databaseId, reader, methodName, docOrFields, inclusive);
- }
- }
- function newQueryFilter(query, methodName, dataReader, databaseId, fieldPath, op, value) {
- let fieldValue;
- if (fieldPath.isKeyField()) {
- if (op === "array-contains" /* Operator.ARRAY_CONTAINS */ || op === "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid Query. You can't perform '${op}' queries on documentId().`);
- }
- else if (op === "in" /* Operator.IN */ || op === "not-in" /* Operator.NOT_IN */) {
- validateDisjunctiveFilterElements(value, op);
- const referenceList = [];
- for (const arrayValue of value) {
- referenceList.push(parseDocumentIdValue(databaseId, query, arrayValue));
- }
- fieldValue = { arrayValue: { values: referenceList } };
- }
- else {
- fieldValue = parseDocumentIdValue(databaseId, query, value);
- }
- }
- else {
- if (op === "in" /* Operator.IN */ ||
- op === "not-in" /* Operator.NOT_IN */ ||
- op === "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */) {
- validateDisjunctiveFilterElements(value, op);
- }
- fieldValue = parseQueryValue(dataReader, methodName, value,
- /* allowArrays= */ op === "in" /* Operator.IN */ || op === "not-in" /* Operator.NOT_IN */);
- }
- const filter = FieldFilter.create(fieldPath, op, fieldValue);
- return filter;
- }
- function newQueryOrderBy(query, fieldPath, direction) {
- if (query.startAt !== null) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid query. You must not call startAt() or startAfter() before ' +
- 'calling orderBy().');
- }
- if (query.endAt !== null) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid query. You must not call endAt() or endBefore() before ' +
- 'calling orderBy().');
- }
- const orderBy = new OrderBy(fieldPath, direction);
- validateNewOrderBy(query, orderBy);
- return orderBy;
- }
- /**
- * Create a `Bound` from a query and a document.
- *
- * Note that the `Bound` will always include the key of the document
- * and so only the provided document will compare equal to the returned
- * position.
- *
- * Will throw if the document does not contain all fields of the order by
- * of the query or if any of the fields in the order by are an uncommitted
- * server timestamp.
- */
- function newQueryBoundFromDocument(query, databaseId, methodName, doc, inclusive) {
- if (!doc) {
- throw new FirestoreError(Code.NOT_FOUND, `Can't use a DocumentSnapshot that doesn't exist for ` +
- `${methodName}().`);
- }
- const components = [];
- // Because people expect to continue/end a query at the exact document
- // provided, we need to use the implicit sort order rather than the explicit
- // sort order, because it's guaranteed to contain the document key. That way
- // the position becomes unambiguous and the query continues/ends exactly at
- // the provided document. Without the key (by using the explicit sort
- // orders), multiple documents could match the position, yielding duplicate
- // results.
- for (const orderBy of queryOrderBy(query)) {
- if (orderBy.field.isKeyField()) {
- components.push(refValue(databaseId, doc.key));
- }
- else {
- const value = doc.data.field(orderBy.field);
- if (isServerTimestamp(value)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid query. You are trying to start or end a query using a ' +
- 'document for which the field "' +
- orderBy.field +
- '" is an uncommitted server timestamp. (Since the value of ' +
- 'this field is unknown, you cannot start/end a query with it.)');
- }
- else if (value !== null) {
- components.push(value);
- }
- else {
- const field = orderBy.field.canonicalString();
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. You are trying to start or end a query using a ` +
- `document for which the field '${field}' (used as the ` +
- `orderBy) does not exist.`);
- }
- }
- }
- return new Bound(components, inclusive);
- }
- /**
- * Converts a list of field values to a `Bound` for the given query.
- */
- function newQueryBoundFromFields(query, databaseId, dataReader, methodName, values, inclusive) {
- // Use explicit order by's because it has to match the query the user made
- const orderBy = query.explicitOrderBy;
- if (values.length > orderBy.length) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Too many arguments provided to ${methodName}(). ` +
- `The number of arguments must be less than or equal to the ` +
- `number of orderBy() clauses`);
- }
- const components = [];
- for (let i = 0; i < values.length; i++) {
- const rawValue = values[i];
- const orderByComponent = orderBy[i];
- if (orderByComponent.field.isKeyField()) {
- if (typeof rawValue !== 'string') {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. Expected a string for document ID in ` +
- `${methodName}(), but got a ${typeof rawValue}`);
- }
- if (!isCollectionGroupQuery(query) && rawValue.indexOf('/') !== -1) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. When querying a collection and ordering by documentId(), ` +
- `the value passed to ${methodName}() must be a plain document ID, but ` +
- `'${rawValue}' contains a slash.`);
- }
- const path = query.path.child(ResourcePath.fromString(rawValue));
- if (!DocumentKey.isDocumentKey(path)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. When querying a collection group and ordering by ` +
- `documentId(), the value passed to ${methodName}() must result in a ` +
- `valid document path, but '${path}' is not because it contains an odd number ` +
- `of segments.`);
- }
- const key = new DocumentKey(path);
- components.push(refValue(databaseId, key));
- }
- else {
- const wrapped = parseQueryValue(dataReader, methodName, rawValue);
- components.push(wrapped);
- }
- }
- return new Bound(components, inclusive);
- }
- /**
- * Parses the given `documentIdValue` into a `ReferenceValue`, throwing
- * appropriate errors if the value is anything other than a `DocumentReference`
- * or `string`, or if the string is malformed.
- */
- function parseDocumentIdValue(databaseId, query, documentIdValue) {
- documentIdValue = getModularInstance(documentIdValue);
- if (typeof documentIdValue === 'string') {
- if (documentIdValue === '') {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid query. When querying with documentId(), you ' +
- 'must provide a valid document ID, but it was an empty string.');
- }
- if (!isCollectionGroupQuery(query) && documentIdValue.indexOf('/') !== -1) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. When querying a collection by ` +
- `documentId(), you must provide a plain document ID, but ` +
- `'${documentIdValue}' contains a '/' character.`);
- }
- const path = query.path.child(ResourcePath.fromString(documentIdValue));
- if (!DocumentKey.isDocumentKey(path)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. When querying a collection group by ` +
- `documentId(), the value provided must result in a valid document path, ` +
- `but '${path}' is not because it has an odd number of segments (${path.length}).`);
- }
- return refValue(databaseId, new DocumentKey(path));
- }
- else if (documentIdValue instanceof DocumentReference) {
- return refValue(databaseId, documentIdValue._key);
- }
- else {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. When querying with documentId(), you must provide a valid ` +
- `string or a DocumentReference, but it was: ` +
- `${valueDescription(documentIdValue)}.`);
- }
- }
- /**
- * Validates that the value passed into a disjunctive filter satisfies all
- * array requirements.
- */
- function validateDisjunctiveFilterElements(value, operator) {
- if (!Array.isArray(value) || value.length === 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid Query. A non-empty array is required for ' +
- `'${operator.toString()}' filters.`);
- }
- if (value.length > 10) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid Query. '${operator.toString()}' filters support a ` +
- 'maximum of 10 elements in the value array.');
- }
- }
- /**
- * Given an operator, returns the set of operators that cannot be used with it.
- *
- * Operators in a query must adhere to the following set of rules:
- * 1. Only one array operator is allowed.
- * 2. Only one disjunctive operator is allowed.
- * 3. `NOT_EQUAL` cannot be used with another `NOT_EQUAL` operator.
- * 4. `NOT_IN` cannot be used with array, disjunctive, or `NOT_EQUAL` operators.
- *
- * Array operators: `ARRAY_CONTAINS`, `ARRAY_CONTAINS_ANY`
- * Disjunctive operators: `IN`, `ARRAY_CONTAINS_ANY`, `NOT_IN`
- */
- function conflictingOps(op) {
- switch (op) {
- case "!=" /* Operator.NOT_EQUAL */:
- return ["!=" /* Operator.NOT_EQUAL */, "not-in" /* Operator.NOT_IN */];
- case "array-contains" /* Operator.ARRAY_CONTAINS */:
- return [
- "array-contains" /* Operator.ARRAY_CONTAINS */,
- "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */,
- "not-in" /* Operator.NOT_IN */
- ];
- case "in" /* Operator.IN */:
- return ["array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */, "in" /* Operator.IN */, "not-in" /* Operator.NOT_IN */];
- case "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */:
- return [
- "array-contains" /* Operator.ARRAY_CONTAINS */,
- "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */,
- "in" /* Operator.IN */,
- "not-in" /* Operator.NOT_IN */
- ];
- case "not-in" /* Operator.NOT_IN */:
- return [
- "array-contains" /* Operator.ARRAY_CONTAINS */,
- "array-contains-any" /* Operator.ARRAY_CONTAINS_ANY */,
- "in" /* Operator.IN */,
- "not-in" /* Operator.NOT_IN */,
- "!=" /* Operator.NOT_EQUAL */
- ];
- default:
- return [];
- }
- }
- function validateNewFieldFilter(query, fieldFilter) {
- if (fieldFilter.isInequality()) {
- const existingInequality = getInequalityFilterField(query);
- const newInequality = fieldFilter.field;
- if (existingInequality !== null &&
- !existingInequality.isEqual(newInequality)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid query. All where filters with an inequality' +
- ' (<, <=, !=, not-in, >, or >=) must be on the same field. But you have' +
- ` inequality filters on '${existingInequality.toString()}'` +
- ` and '${newInequality.toString()}'`);
- }
- const firstOrderByField = getFirstOrderByField(query);
- if (firstOrderByField !== null) {
- validateOrderByAndInequalityMatch(query, newInequality, firstOrderByField);
- }
- }
- const conflictingOp = findOpInsideFilters(query.filters, conflictingOps(fieldFilter.op));
- if (conflictingOp !== null) {
- // Special case when it's a duplicate op to give a slightly clearer error message.
- if (conflictingOp === fieldFilter.op) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Invalid query. You cannot use more than one ' +
- `'${fieldFilter.op.toString()}' filter.`);
- }
- else {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. You cannot use '${fieldFilter.op.toString()}' filters ` +
- `with '${conflictingOp.toString()}' filters.`);
- }
- }
- }
- function validateNewFilter(query, filter) {
- let testQuery = query;
- const subFilters = filter.getFlattenedFilters();
- for (const subFilter of subFilters) {
- validateNewFieldFilter(testQuery, subFilter);
- testQuery = queryWithAddedFilter(testQuery, subFilter);
- }
- }
- // Checks if any of the provided filter operators are included in the given list of filters and
- // returns the first one that is, or null if none are.
- function findOpInsideFilters(filters, operators) {
- for (const filter of filters) {
- for (const fieldFilter of filter.getFlattenedFilters()) {
- if (operators.indexOf(fieldFilter.op) >= 0) {
- return fieldFilter.op;
- }
- }
- }
- return null;
- }
- function validateNewOrderBy(query, orderBy) {
- if (getFirstOrderByField(query) === null) {
- // This is the first order by. It must match any inequality.
- const inequalityField = getInequalityFilterField(query);
- if (inequalityField !== null) {
- validateOrderByAndInequalityMatch(query, inequalityField, orderBy.field);
- }
- }
- }
- function validateOrderByAndInequalityMatch(baseQuery, inequality, orderBy) {
- if (!orderBy.isEqual(inequality)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Invalid query. You have a where filter with an inequality ` +
- `(<, <=, !=, not-in, >, or >=) on field '${inequality.toString()}' ` +
- `and so you must also use '${inequality.toString()}' ` +
- `as your first argument to orderBy(), but your first orderBy() ` +
- `is on field '${orderBy.toString()}' instead.`);
- }
- }
- function validateQueryFilterConstraint(functionName, queryConstraint) {
- if (!(queryConstraint instanceof QueryFieldFilterConstraint) &&
- !(queryConstraint instanceof QueryCompositeFilterConstraint)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, `Function ${functionName}() requires AppliableConstraints created with a call to 'where(...)', 'or(...)', or 'and(...)'.`);
- }
- }
- function validateQueryConstraintArray(queryConstraint) {
- const compositeFilterCount = queryConstraint.filter(filter => filter instanceof QueryCompositeFilterConstraint).length;
- const fieldFilterCount = queryConstraint.filter(filter => filter instanceof QueryFieldFilterConstraint).length;
- if (compositeFilterCount > 1 ||
- (compositeFilterCount > 0 && fieldFilterCount > 0)) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'InvalidQuery. When using composite filters, you cannot use ' +
- 'more than one filter at the top level. Consider nesting the multiple ' +
- 'filters within an `and(...)` statement. For example: ' +
- 'change `query(query, where(...), or(...))` to ' +
- '`query(query, and(where(...), or(...)))`.');
- }
- }
-
- /**
- * @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.
- */
- /**
- * Converts Firestore's internal types to the JavaScript types that we expose
- * to the user.
- *
- * @internal
- */
- class AbstractUserDataWriter {
- convertValue(value, serverTimestampBehavior = 'none') {
- switch (typeOrder(value)) {
- case 0 /* TypeOrder.NullValue */:
- return null;
- case 1 /* TypeOrder.BooleanValue */:
- return value.booleanValue;
- case 2 /* TypeOrder.NumberValue */:
- return normalizeNumber(value.integerValue || value.doubleValue);
- case 3 /* TypeOrder.TimestampValue */:
- return this.convertTimestamp(value.timestampValue);
- case 4 /* TypeOrder.ServerTimestampValue */:
- return this.convertServerTimestamp(value, serverTimestampBehavior);
- case 5 /* TypeOrder.StringValue */:
- return value.stringValue;
- case 6 /* TypeOrder.BlobValue */:
- return this.convertBytes(normalizeByteString(value.bytesValue));
- case 7 /* TypeOrder.RefValue */:
- return this.convertReference(value.referenceValue);
- case 8 /* TypeOrder.GeoPointValue */:
- return this.convertGeoPoint(value.geoPointValue);
- case 9 /* TypeOrder.ArrayValue */:
- return this.convertArray(value.arrayValue, serverTimestampBehavior);
- case 10 /* TypeOrder.ObjectValue */:
- return this.convertObject(value.mapValue, serverTimestampBehavior);
- default:
- throw fail();
- }
- }
- convertObject(mapValue, serverTimestampBehavior) {
- const result = {};
- forEach(mapValue.fields, (key, value) => {
- result[key] = this.convertValue(value, serverTimestampBehavior);
- });
- return result;
- }
- convertGeoPoint(value) {
- return new GeoPoint(normalizeNumber(value.latitude), normalizeNumber(value.longitude));
- }
- convertArray(arrayValue, serverTimestampBehavior) {
- return (arrayValue.values || []).map(value => this.convertValue(value, serverTimestampBehavior));
- }
- convertServerTimestamp(value, serverTimestampBehavior) {
- switch (serverTimestampBehavior) {
- case 'previous':
- const previousValue = getPreviousValue(value);
- if (previousValue == null) {
- return null;
- }
- return this.convertValue(previousValue, serverTimestampBehavior);
- case 'estimate':
- return this.convertTimestamp(getLocalWriteTime(value));
- default:
- return null;
- }
- }
- convertTimestamp(value) {
- const normalizedValue = normalizeTimestamp(value);
- return new Timestamp(normalizedValue.seconds, normalizedValue.nanos);
- }
- convertDocumentKey(name, expectedDatabaseId) {
- const resourcePath = ResourcePath.fromString(name);
- hardAssert(isValidResourceName(resourcePath));
- const databaseId = new DatabaseId(resourcePath.get(1), resourcePath.get(3));
- const key = new DocumentKey(resourcePath.popFirst(5));
- if (!databaseId.isEqual(expectedDatabaseId)) {
- // TODO(b/64130202): Somehow support foreign references.
- logError(`Document ${key} contains a document ` +
- `reference within a different database (` +
- `${databaseId.projectId}/${databaseId.database}) which is not ` +
- `supported. It will be treated as a reference in the current ` +
- `database (${expectedDatabaseId.projectId}/${expectedDatabaseId.database}) ` +
- `instead.`);
- }
- return key;
- }
- }
-
- /**
- * @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.
- */
- /**
- * Converts custom model object of type T into `DocumentData` by applying the
- * converter if it exists.
- *
- * This function is used when converting user objects to `DocumentData`
- * because we want to provide the user with a more specific error message if
- * their `set()` or fails due to invalid data originating from a `toFirestore()`
- * call.
- */
- function applyFirestoreDataConverter(converter, value, options) {
- let convertedValue;
- if (converter) {
- if (options && (options.merge || options.mergeFields)) {
- // Cast to `any` in order to satisfy the union type constraint on
- // toFirestore().
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- convertedValue = converter.toFirestore(value, options);
- }
- else {
- convertedValue = converter.toFirestore(value);
- }
- }
- else {
- convertedValue = value;
- }
- return convertedValue;
- }
- class LiteUserDataWriter extends AbstractUserDataWriter {
- constructor(firestore) {
- super();
- this.firestore = firestore;
- }
- convertBytes(bytes) {
- return new Bytes(bytes);
- }
- convertReference(name) {
- const key = this.convertDocumentKey(name, this.firestore._databaseId);
- return new DocumentReference(this.firestore, /* converter= */ null, key);
- }
- }
- /**
- * Reads the document referred to by the specified document reference.
- *
- * All documents are directly fetched from the server, even if the document was
- * previously read or modified. Recent modifications are only reflected in the
- * retrieved `DocumentSnapshot` if they have already been applied by the
- * backend. If the client is offline, the read fails. If you like to use
- * caching or see local modifications, please use the full Firestore SDK.
- *
- * @param reference - The reference of the document to fetch.
- * @returns A Promise resolved with a `DocumentSnapshot` containing the current
- * document contents.
- */
- function getDoc(reference) {
- reference = cast(reference, DocumentReference);
- const datastore = getDatastore(reference.firestore);
- const userDataWriter = new LiteUserDataWriter(reference.firestore);
- return invokeBatchGetDocumentsRpc(datastore, [reference._key]).then(result => {
- hardAssert(result.length === 1);
- const document = result[0];
- return new DocumentSnapshot(reference.firestore, userDataWriter, reference._key, document.isFoundDocument() ? document : null, reference.converter);
- });
- }
- /**
- * Executes the query and returns the results as a {@link QuerySnapshot}.
- *
- * All queries are executed directly by the server, even if the the query was
- * previously executed. Recent modifications are only reflected in the retrieved
- * results if they have already been applied by the backend. If the client is
- * offline, the operation fails. To see previously cached result and local
- * modifications, use the full Firestore SDK.
- *
- * @param query - The `Query` to execute.
- * @returns A Promise that will be resolved with the results of the query.
- */
- function getDocs(query) {
- query = cast(query, Query);
- validateHasExplicitOrderByForLimitToLast(query._query);
- const datastore = getDatastore(query.firestore);
- const userDataWriter = new LiteUserDataWriter(query.firestore);
- return invokeRunQueryRpc(datastore, query._query).then(result => {
- const docs = result.map(doc => new QueryDocumentSnapshot(query.firestore, userDataWriter, doc.key, doc, query.converter));
- if (query._query.limitType === "L" /* LimitType.Last */) {
- // Limit to last queries reverse the orderBy constraint that was
- // specified by the user. As such, we need to reverse the order of the
- // results to return the documents in the expected order.
- docs.reverse();
- }
- return new QuerySnapshot(query, docs);
- });
- }
- function setDoc(reference, data, options) {
- reference = cast(reference, DocumentReference);
- const convertedValue = applyFirestoreDataConverter(reference.converter, data, options);
- const dataReader = newUserDataReader(reference.firestore);
- const parsed = parseSetData(dataReader, 'setDoc', reference._key, convertedValue, reference.converter !== null, options);
- const datastore = getDatastore(reference.firestore);
- return invokeCommitRpc(datastore, [
- parsed.toMutation(reference._key, Precondition.none())
- ]);
- }
- function updateDoc(reference, fieldOrUpdateData, value, ...moreFieldsAndValues) {
- reference = cast(reference, DocumentReference);
- const dataReader = newUserDataReader(reference.firestore);
- // For Compat types, we have to "extract" the underlying types before
- // performing validation.
- fieldOrUpdateData = getModularInstance(fieldOrUpdateData);
- let parsed;
- if (typeof fieldOrUpdateData === 'string' ||
- fieldOrUpdateData instanceof FieldPath) {
- parsed = parseUpdateVarargs(dataReader, 'updateDoc', reference._key, fieldOrUpdateData, value, moreFieldsAndValues);
- }
- else {
- parsed = parseUpdateData(dataReader, 'updateDoc', reference._key, fieldOrUpdateData);
- }
- const datastore = getDatastore(reference.firestore);
- return invokeCommitRpc(datastore, [
- parsed.toMutation(reference._key, Precondition.exists(true))
- ]);
- }
- /**
- * Deletes the document referred to by the specified `DocumentReference`.
- *
- * The deletion will only be reflected in document reads that occur after the
- * returned promise resolves. If the client is offline, the
- * delete fails. If you would like to see local modifications or buffer writes
- * until the client is online, use the full Firestore SDK.
- *
- * @param reference - A reference to the document to delete.
- * @returns A `Promise` resolved once the document has been successfully
- * deleted from the backend.
- */
- function deleteDoc(reference) {
- reference = cast(reference, DocumentReference);
- const datastore = getDatastore(reference.firestore);
- return invokeCommitRpc(datastore, [
- new DeleteMutation(reference._key, Precondition.none())
- ]);
- }
- /**
- * Add a new document to specified `CollectionReference` with the given data,
- * assigning it a document ID automatically.
- *
- * The result of this write will only be reflected in document reads that occur
- * after the returned promise resolves. If the client is offline, the
- * write fails. If you would like to see local modifications or buffer writes
- * until the client is online, use the full Firestore SDK.
- *
- * @param reference - A reference to the collection to add this document to.
- * @param data - An Object containing the data for the new document.
- * @throws Error - If the provided input is not a valid Firestore document.
- * @returns A `Promise` resolved with a `DocumentReference` pointing to the
- * newly created document after it has been written to the backend.
- */
- function addDoc(reference, data) {
- reference = cast(reference, CollectionReference);
- const docRef = doc(reference);
- const convertedValue = applyFirestoreDataConverter(reference.converter, data);
- const dataReader = newUserDataReader(reference.firestore);
- const parsed = parseSetData(dataReader, 'addDoc', docRef._key, convertedValue, docRef.converter !== null, {});
- const datastore = getDatastore(reference.firestore);
- return invokeCommitRpc(datastore, [
- parsed.toMutation(docRef._key, Precondition.exists(false))
- ]).then(() => docRef);
- }
-
- /**
- * @license
- * Copyright 2022 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.
- */
- /**
- * Calculates the number of documents in the result set of the given query,
- * without actually downloading the documents.
- *
- * Using this function to count the documents is efficient because only the
- * final count, not the documents' data, is downloaded. This function can even
- * count the documents if the result set would be prohibitively large to
- * download entirely (e.g. thousands of documents).
- *
- * @param query - The query whose result set size to calculate.
- * @returns A Promise that will be resolved with the count; the count can be
- * retrieved from `snapshot.data().count`, where `snapshot` is the
- * `AggregateQuerySnapshot` to which the returned Promise resolves.
- */
- function getCount(query) {
- const firestore = cast(query.firestore, Firestore);
- const datastore = getDatastore(firestore);
- const userDataWriter = new LiteUserDataWriter(firestore);
- return new CountQueryRunner(query, datastore, userDataWriter).run();
- }
- /**
- * Compares two `AggregateQuerySnapshot` instances for equality.
- *
- * Two `AggregateQuerySnapshot` instances are considered "equal" if they have
- * underlying queries that compare equal, and the same data.
- *
- * @param left - The first `AggregateQuerySnapshot` to compare.
- * @param right - The second `AggregateQuerySnapshot` to compare.
- *
- * @returns `true` if the objects are "equal", as defined above, or `false`
- * otherwise.
- */
- function aggregateQuerySnapshotEqual(left, right) {
- return (queryEqual(left.query, right.query) && deepEqual(left.data(), right.data()));
- }
-
- /**
- * @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.
- */
- /**
- * Returns a sentinel for use with {@link @firebase/firestore/lite#(updateDoc:1)} or
- * {@link @firebase/firestore/lite#(setDoc:1)} with `{merge: true}` to mark a field for deletion.
- */
- function deleteField() {
- return new DeleteFieldValueImpl('deleteField');
- }
- /**
- * Returns a sentinel used with {@link @firebase/firestore/lite#(setDoc:1)} or {@link @firebase/firestore/lite#(updateDoc:1)} to
- * include a server-generated timestamp in the written data.
- */
- function serverTimestamp() {
- return new ServerTimestampFieldValueImpl('serverTimestamp');
- }
- /**
- * Returns a special value that can be used with {@link @firebase/firestore/lite#(setDoc:1)} or {@link
- * @firebase/firestore/lite#(updateDoc:1)} that tells the server to union the given elements with any array
- * value that already exists on the server. Each specified element that doesn't
- * already exist in the array will be added to the end. If the field being
- * modified is not already an array it will be overwritten with an array
- * containing exactly the specified elements.
- *
- * @param elements - The elements to union into the array.
- * @returns The `FieldValue` sentinel for use in a call to `setDoc()` or
- * `updateDoc()`.
- */
- function arrayUnion(...elements) {
- // NOTE: We don't actually parse the data until it's used in set() or
- // update() since we'd need the Firestore instance to do this.
- return new ArrayUnionFieldValueImpl('arrayUnion', elements);
- }
- /**
- * Returns a special value that can be used with {@link (setDoc:1)} or {@link
- * updateDoc:1} that tells the server to remove the given elements from any
- * array value that already exists on the server. All instances of each element
- * specified will be removed from the array. If the field being modified is not
- * already an array it will be overwritten with an empty array.
- *
- * @param elements - The elements to remove from the array.
- * @returns The `FieldValue` sentinel for use in a call to `setDoc()` or
- * `updateDoc()`
- */
- function arrayRemove(...elements) {
- // NOTE: We don't actually parse the data until it's used in set() or
- // update() since we'd need the Firestore instance to do this.
- return new ArrayRemoveFieldValueImpl('arrayRemove', elements);
- }
- /**
- * Returns a special value that can be used with {@link @firebase/firestore/lite#(setDoc:1)} or {@link
- * @firebase/firestore/lite#(updateDoc:1)} that tells the server to increment the field's current value by
- * the given value.
- *
- * If either the operand or the current field value uses floating point
- * precision, all arithmetic follows IEEE 754 semantics. If both values are
- * integers, values outside of JavaScript's safe number range
- * (`Number.MIN_SAFE_INTEGER` to `Number.MAX_SAFE_INTEGER`) are also subject to
- * precision loss. Furthermore, once processed by the Firestore backend, all
- * integer operations are capped between -2^63 and 2^63-1.
- *
- * If the current field value is not of type `number`, or if the field does not
- * yet exist, the transformation sets the field to the given value.
- *
- * @param n - The value to increment by.
- * @returns The `FieldValue` sentinel for use in a call to `setDoc()` or
- * `updateDoc()`
- */
- function increment(n) {
- return new NumericIncrementFieldValueImpl('increment', n);
- }
-
- /**
- * @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 write batch, used to perform multiple writes as a single atomic unit.
- *
- * A `WriteBatch` object can be acquired by calling {@link writeBatch}. It
- * provides methods for adding writes to the write batch. None of the writes
- * will be committed (or visible locally) until {@link WriteBatch.commit} is
- * called.
- */
- class WriteBatch {
- /** @hideconstructor */
- constructor(_firestore, _commitHandler) {
- this._firestore = _firestore;
- this._commitHandler = _commitHandler;
- this._mutations = [];
- this._committed = false;
- this._dataReader = newUserDataReader(_firestore);
- }
- set(documentRef, data, options) {
- this._verifyNotCommitted();
- const ref = validateReference(documentRef, this._firestore);
- const convertedValue = applyFirestoreDataConverter(ref.converter, data, options);
- const parsed = parseSetData(this._dataReader, 'WriteBatch.set', ref._key, convertedValue, ref.converter !== null, options);
- this._mutations.push(parsed.toMutation(ref._key, Precondition.none()));
- return this;
- }
- update(documentRef, fieldOrUpdateData, value, ...moreFieldsAndValues) {
- this._verifyNotCommitted();
- const ref = validateReference(documentRef, this._firestore);
- // For Compat types, we have to "extract" the underlying types before
- // performing validation.
- fieldOrUpdateData = getModularInstance(fieldOrUpdateData);
- let parsed;
- if (typeof fieldOrUpdateData === 'string' ||
- fieldOrUpdateData instanceof FieldPath) {
- parsed = parseUpdateVarargs(this._dataReader, 'WriteBatch.update', ref._key, fieldOrUpdateData, value, moreFieldsAndValues);
- }
- else {
- parsed = parseUpdateData(this._dataReader, 'WriteBatch.update', ref._key, fieldOrUpdateData);
- }
- this._mutations.push(parsed.toMutation(ref._key, Precondition.exists(true)));
- return this;
- }
- /**
- * Deletes the document referred to by the provided {@link DocumentReference}.
- *
- * @param documentRef - A reference to the document to be deleted.
- * @returns This `WriteBatch` instance. Used for chaining method calls.
- */
- delete(documentRef) {
- this._verifyNotCommitted();
- const ref = validateReference(documentRef, this._firestore);
- this._mutations = this._mutations.concat(new DeleteMutation(ref._key, Precondition.none()));
- return this;
- }
- /**
- * Commits all of the writes in this write batch as a single atomic unit.
- *
- * The result of these writes will only be reflected in document reads that
- * occur after the returned promise resolves. If the client is offline, the
- * write fails. If you would like to see local modifications or buffer writes
- * until the client is online, use the full Firestore SDK.
- *
- * @returns A `Promise` resolved once all of the writes in the batch have been
- * successfully written to the backend as an atomic unit (note that it won't
- * resolve while you're offline).
- */
- commit() {
- this._verifyNotCommitted();
- this._committed = true;
- if (this._mutations.length > 0) {
- return this._commitHandler(this._mutations);
- }
- return Promise.resolve();
- }
- _verifyNotCommitted() {
- if (this._committed) {
- throw new FirestoreError(Code.FAILED_PRECONDITION, 'A write batch can no longer be used after commit() ' +
- 'has been called.');
- }
- }
- }
- function validateReference(documentRef, firestore) {
- documentRef = getModularInstance(documentRef);
- if (documentRef.firestore !== firestore) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Provided document reference is from a different Firestore instance.');
- }
- else {
- return documentRef;
- }
- }
- /**
- * Creates a write batch, used for performing multiple writes as a single
- * atomic operation. The maximum number of writes allowed in a single WriteBatch
- * is 500.
- *
- * The result of these writes will only be reflected in document reads that
- * occur after the returned promise resolves. If the client is offline, the
- * write fails. If you would like to see local modifications or buffer writes
- * until the client is online, use the full Firestore SDK.
- *
- * @returns A `WriteBatch` that can be used to atomically execute multiple
- * writes.
- */
- function writeBatch(firestore) {
- firestore = cast(firestore, Firestore);
- const datastore = getDatastore(firestore);
- return new WriteBatch(firestore, writes => invokeCommitRpc(datastore, writes));
- }
-
- /**
- * @license
- * Copyright 2022 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.
- */
- const DEFAULT_TRANSACTION_OPTIONS = {
- maxAttempts: 5
- };
- function validateTransactionOptions(options) {
- if (options.maxAttempts < 1) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Max attempts must be at least 1');
- }
- }
-
- /**
- * @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.
- */
- /**
- * Internal transaction object responsible for accumulating the mutations to
- * perform and the base versions for any documents read.
- */
- class Transaction$1 {
- constructor(datastore) {
- this.datastore = datastore;
- // The version of each document that was read during this transaction.
- this.readVersions = new Map();
- this.mutations = [];
- this.committed = false;
- /**
- * A deferred usage error that occurred previously in this transaction that
- * will cause the transaction to fail once it actually commits.
- */
- this.lastWriteError = null;
- /**
- * Set of documents that have been written in the transaction.
- *
- * When there's more than one write to the same key in a transaction, any
- * writes after the first are handled differently.
- */
- this.writtenDocs = new Set();
- }
- async lookup(keys) {
- this.ensureCommitNotCalled();
- if (this.mutations.length > 0) {
- throw new FirestoreError(Code.INVALID_ARGUMENT, 'Firestore transactions require all reads to be executed before all writes.');
- }
- const docs = await invokeBatchGetDocumentsRpc(this.datastore, keys);
- docs.forEach(doc => this.recordVersion(doc));
- return docs;
- }
- set(key, data) {
- this.write(data.toMutation(key, this.precondition(key)));
- this.writtenDocs.add(key.toString());
- }
- update(key, data) {
- try {
- this.write(data.toMutation(key, this.preconditionForUpdate(key)));
- }
- catch (e) {
- this.lastWriteError = e;
- }
- this.writtenDocs.add(key.toString());
- }
- delete(key) {
- this.write(new DeleteMutation(key, this.precondition(key)));
- this.writtenDocs.add(key.toString());
- }
- async commit() {
- this.ensureCommitNotCalled();
- if (this.lastWriteError) {
- throw this.lastWriteError;
- }
- const unwritten = this.readVersions;
- // For each mutation, note that the doc was written.
- this.mutations.forEach(mutation => {
- unwritten.delete(mutation.key.toString());
- });
- // For each document that was read but not written to, we want to perform
- // a `verify` operation.
- unwritten.forEach((_, path) => {
- const key = DocumentKey.fromPath(path);
- this.mutations.push(new VerifyMutation(key, this.precondition(key)));
- });
- await invokeCommitRpc(this.datastore, this.mutations);
- this.committed = true;
- }
- recordVersion(doc) {
- let docVersion;
- if (doc.isFoundDocument()) {
- docVersion = doc.version;
- }
- else if (doc.isNoDocument()) {
- // Represent a deleted doc using SnapshotVersion.min().
- docVersion = SnapshotVersion.min();
- }
- else {
- throw fail();
- }
- const existingVersion = this.readVersions.get(doc.key.toString());
- if (existingVersion) {
- if (!docVersion.isEqual(existingVersion)) {
- // This transaction will fail no matter what.
- throw new FirestoreError(Code.ABORTED, 'Document version changed between two reads.');
- }
- }
- else {
- this.readVersions.set(doc.key.toString(), docVersion);
- }
- }
- /**
- * Returns the version of this document when it was read in this transaction,
- * as a precondition, or no precondition if it was not read.
- */
- precondition(key) {
- const version = this.readVersions.get(key.toString());
- if (!this.writtenDocs.has(key.toString()) && version) {
- if (version.isEqual(SnapshotVersion.min())) {
- return Precondition.exists(false);
- }
- else {
- return Precondition.updateTime(version);
- }
- }
- else {
- return Precondition.none();
- }
- }
- /**
- * Returns the precondition for a document if the operation is an update.
- */
- preconditionForUpdate(key) {
- const version = this.readVersions.get(key.toString());
- // The first time a document is written, we want to take into account the
- // read time and existence
- if (!this.writtenDocs.has(key.toString()) && version) {
- if (version.isEqual(SnapshotVersion.min())) {
- // The document doesn't exist, so fail the transaction.
- // This has to be validated locally because you can't send a
- // precondition that a document does not exist without changing the
- // semantics of the backend write to be an insert. This is the reverse
- // of what we want, since we want to assert that the document doesn't
- // exist but then send the update and have it fail. Since we can't
- // express that to the backend, we have to validate locally.
- // Note: this can change once we can send separate verify writes in the
- // transaction.
- throw new FirestoreError(Code.INVALID_ARGUMENT, "Can't update a document that doesn't exist.");
- }
- // Document exists, base precondition on document update time.
- return Precondition.updateTime(version);
- }
- else {
- // Document was not read, so we just use the preconditions for a blind
- // update.
- return Precondition.exists(true);
- }
- }
- write(mutation) {
- this.ensureCommitNotCalled();
- this.mutations.push(mutation);
- }
- ensureCommitNotCalled() {
- }
- }
-
- /**
- * @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.
- */
- /**
- * TransactionRunner encapsulates the logic needed to run and retry transactions
- * with backoff.
- */
- class TransactionRunner {
- constructor(asyncQueue, datastore, options, updateFunction, deferred) {
- this.asyncQueue = asyncQueue;
- this.datastore = datastore;
- this.options = options;
- this.updateFunction = updateFunction;
- this.deferred = deferred;
- this.attemptsRemaining = options.maxAttempts;
- this.backoff = new ExponentialBackoff(this.asyncQueue, "transaction_retry" /* TimerId.TransactionRetry */);
- }
- /** Runs the transaction and sets the result on deferred. */
- run() {
- this.attemptsRemaining -= 1;
- this.runWithBackOff();
- }
- runWithBackOff() {
- this.backoff.backoffAndRun(async () => {
- const transaction = new Transaction$1(this.datastore);
- const userPromise = this.tryRunUpdateFunction(transaction);
- if (userPromise) {
- userPromise
- .then(result => {
- this.asyncQueue.enqueueAndForget(() => {
- return transaction
- .commit()
- .then(() => {
- this.deferred.resolve(result);
- })
- .catch(commitError => {
- this.handleTransactionError(commitError);
- });
- });
- })
- .catch(userPromiseError => {
- this.handleTransactionError(userPromiseError);
- });
- }
- });
- }
- tryRunUpdateFunction(transaction) {
- try {
- const userPromise = this.updateFunction(transaction);
- if (isNullOrUndefined(userPromise) ||
- !userPromise.catch ||
- !userPromise.then) {
- this.deferred.reject(Error('Transaction callback must return a Promise'));
- return null;
- }
- return userPromise;
- }
- catch (error) {
- // Do not retry errors thrown by user provided updateFunction.
- this.deferred.reject(error);
- return null;
- }
- }
- handleTransactionError(error) {
- if (this.attemptsRemaining > 0 && this.isRetryableTransactionError(error)) {
- this.attemptsRemaining -= 1;
- this.asyncQueue.enqueueAndForget(() => {
- this.runWithBackOff();
- return Promise.resolve();
- });
- }
- else {
- this.deferred.reject(error);
- }
- }
- isRetryableTransactionError(error) {
- if (error.name === 'FirebaseError') {
- // In transactions, the backend will fail outdated reads with FAILED_PRECONDITION and
- // non-matching document versions with ABORTED. These errors should be retried.
- const code = error.code;
- return (code === 'aborted' ||
- code === 'failed-precondition' ||
- code === 'already-exists' ||
- !isPermanentError(code));
- }
- return false;
- }
- }
-
- /**
- * @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.
- */
- /**
- * Represents an operation scheduled to be run in the future on an AsyncQueue.
- *
- * It is created via DelayedOperation.createAndSchedule().
- *
- * Supports cancellation (via cancel()) and early execution (via skipDelay()).
- *
- * Note: We implement `PromiseLike` instead of `Promise`, as the `Promise` type
- * in newer versions of TypeScript defines `finally`, which is not available in
- * IE.
- */
- class DelayedOperation {
- constructor(asyncQueue, timerId, targetTimeMs, op, removalCallback) {
- this.asyncQueue = asyncQueue;
- this.timerId = timerId;
- this.targetTimeMs = targetTimeMs;
- this.op = op;
- this.removalCallback = removalCallback;
- this.deferred = new Deferred();
- this.then = this.deferred.promise.then.bind(this.deferred.promise);
- // It's normal for the deferred promise to be canceled (due to cancellation)
- // and so we attach a dummy catch callback to avoid
- // 'UnhandledPromiseRejectionWarning' log spam.
- this.deferred.promise.catch(err => { });
- }
- /**
- * Creates and returns a DelayedOperation that has been scheduled to be
- * executed on the provided asyncQueue after the provided delayMs.
- *
- * @param asyncQueue - The queue to schedule the operation on.
- * @param id - A Timer ID identifying the type of operation this is.
- * @param delayMs - The delay (ms) before the operation should be scheduled.
- * @param op - The operation to run.
- * @param removalCallback - A callback to be called synchronously once the
- * operation is executed or canceled, notifying the AsyncQueue to remove it
- * from its delayedOperations list.
- * PORTING NOTE: This exists to prevent making removeDelayedOperation() and
- * the DelayedOperation class public.
- */
- static createAndSchedule(asyncQueue, timerId, delayMs, op, removalCallback) {
- const targetTime = Date.now() + delayMs;
- const delayedOp = new DelayedOperation(asyncQueue, timerId, targetTime, op, removalCallback);
- delayedOp.start(delayMs);
- return delayedOp;
- }
- /**
- * Starts the timer. This is called immediately after construction by
- * createAndSchedule().
- */
- start(delayMs) {
- this.timerHandle = setTimeout(() => this.handleDelayElapsed(), delayMs);
- }
- /**
- * Queues the operation to run immediately (if it hasn't already been run or
- * canceled).
- */
- skipDelay() {
- return this.handleDelayElapsed();
- }
- /**
- * Cancels the operation if it hasn't already been executed or canceled. The
- * promise will be rejected.
- *
- * As long as the operation has not yet been run, calling cancel() provides a
- * guarantee that the operation will not be run.
- */
- cancel(reason) {
- if (this.timerHandle !== null) {
- this.clearTimeout();
- this.deferred.reject(new FirestoreError(Code.CANCELLED, 'Operation cancelled' + (reason ? ': ' + reason : '')));
- }
- }
- handleDelayElapsed() {
- this.asyncQueue.enqueueAndForget(() => {
- if (this.timerHandle !== null) {
- this.clearTimeout();
- return this.op().then(result => {
- return this.deferred.resolve(result);
- });
- }
- else {
- return Promise.resolve();
- }
- });
- }
- clearTimeout() {
- if (this.timerHandle !== null) {
- this.removalCallback(this);
- clearTimeout(this.timerHandle);
- this.timerHandle = null;
- }
- }
- }
-
- /**
- * @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.
- */
- const LOG_TAG = 'AsyncQueue';
- class AsyncQueueImpl {
- constructor() {
- // The last promise in the queue.
- this.tail = Promise.resolve();
- // A list of retryable operations. Retryable operations are run in order and
- // retried with backoff.
- this.retryableOps = [];
- // Is this AsyncQueue being shut down? Once it is set to true, it will not
- // be changed again.
- this._isShuttingDown = false;
- // Operations scheduled to be queued in the future. Operations are
- // automatically removed after they are run or canceled.
- this.delayedOperations = [];
- // visible for testing
- this.failure = null;
- // Flag set while there's an outstanding AsyncQueue operation, used for
- // assertion sanity-checks.
- this.operationInProgress = false;
- // Enabled during shutdown on Safari to prevent future access to IndexedDB.
- this.skipNonRestrictedTasks = false;
- // List of TimerIds to fast-forward delays for.
- this.timerIdsToSkip = [];
- // Backoff timer used to schedule retries for retryable operations
- this.backoff = new ExponentialBackoff(this, "async_queue_retry" /* TimerId.AsyncQueueRetry */);
- // Visibility handler that triggers an immediate retry of all retryable
- // operations. Meant to speed up recovery when we regain file system access
- // after page comes into foreground.
- this.visibilityHandler = () => {
- this.backoff.skipBackoff();
- };
- }
- get isShuttingDown() {
- return this._isShuttingDown;
- }
- /**
- * Adds a new operation to the queue without waiting for it to complete (i.e.
- * we ignore the Promise result).
- */
- enqueueAndForget(op) {
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- this.enqueue(op);
- }
- enqueueAndForgetEvenWhileRestricted(op) {
- this.verifyNotFailed();
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- this.enqueueInternal(op);
- }
- enterRestrictedMode(purgeExistingTasks) {
- if (!this._isShuttingDown) {
- this._isShuttingDown = true;
- this.skipNonRestrictedTasks = purgeExistingTasks || false;
- }
- }
- enqueue(op) {
- this.verifyNotFailed();
- if (this._isShuttingDown) {
- // Return a Promise which never resolves.
- return new Promise(() => { });
- }
- // Create a deferred Promise that we can return to the callee. This
- // allows us to return a "hanging Promise" only to the callee and still
- // advance the queue even when the operation is not run.
- const task = new Deferred();
- return this.enqueueInternal(() => {
- if (this._isShuttingDown && this.skipNonRestrictedTasks) {
- // We do not resolve 'task'
- return Promise.resolve();
- }
- op().then(task.resolve, task.reject);
- return task.promise;
- }).then(() => task.promise);
- }
- enqueueRetryable(op) {
- this.enqueueAndForget(() => {
- this.retryableOps.push(op);
- return this.retryNextOp();
- });
- }
- /**
- * Runs the next operation from the retryable queue. If the operation fails,
- * reschedules with backoff.
- */
- async retryNextOp() {
- if (this.retryableOps.length === 0) {
- return;
- }
- try {
- await this.retryableOps[0]();
- this.retryableOps.shift();
- this.backoff.reset();
- }
- catch (e) {
- if (isIndexedDbTransactionError(e)) {
- logDebug(LOG_TAG, 'Operation failed with retryable error: ' + e);
- }
- else {
- throw e; // Failure will be handled by AsyncQueue
- }
- }
- if (this.retryableOps.length > 0) {
- // If there are additional operations, we re-schedule `retryNextOp()`.
- // This is necessary to run retryable operations that failed during
- // their initial attempt since we don't know whether they are already
- // enqueued. If, for example, `op1`, `op2`, `op3` are enqueued and `op1`
- // needs to be re-run, we will run `op1`, `op1`, `op2` using the
- // already enqueued calls to `retryNextOp()`. `op3()` will then run in the
- // call scheduled here.
- // Since `backoffAndRun()` cancels an existing backoff and schedules a
- // new backoff on every call, there is only ever a single additional
- // operation in the queue.
- this.backoff.backoffAndRun(() => this.retryNextOp());
- }
- }
- enqueueInternal(op) {
- const newTail = this.tail.then(() => {
- this.operationInProgress = true;
- return op()
- .catch((error) => {
- this.failure = error;
- this.operationInProgress = false;
- const message = getMessageOrStack(error);
- logError('INTERNAL UNHANDLED ERROR: ', message);
- // Re-throw the error so that this.tail becomes a rejected Promise and
- // all further attempts to chain (via .then) will just short-circuit
- // and return the rejected Promise.
- throw error;
- })
- .then(result => {
- this.operationInProgress = false;
- return result;
- });
- });
- this.tail = newTail;
- return newTail;
- }
- enqueueAfterDelay(timerId, delayMs, op) {
- this.verifyNotFailed();
- // Fast-forward delays for timerIds that have been overriden.
- if (this.timerIdsToSkip.indexOf(timerId) > -1) {
- delayMs = 0;
- }
- const delayedOp = DelayedOperation.createAndSchedule(this, timerId, delayMs, op, removedOp => this.removeDelayedOperation(removedOp));
- this.delayedOperations.push(delayedOp);
- return delayedOp;
- }
- verifyNotFailed() {
- if (this.failure) {
- fail();
- }
- }
- verifyOperationInProgress() {
- }
- /**
- * Waits until all currently queued tasks are finished executing. Delayed
- * operations are not run.
- */
- async drain() {
- // Operations in the queue prior to draining may have enqueued additional
- // operations. Keep draining the queue until the tail is no longer advanced,
- // which indicates that no more new operations were enqueued and that all
- // operations were executed.
- let currentTail;
- do {
- currentTail = this.tail;
- await currentTail;
- } while (currentTail !== this.tail);
- }
- /**
- * For Tests: Determine if a delayed operation with a particular TimerId
- * exists.
- */
- containsDelayedOperation(timerId) {
- for (const op of this.delayedOperations) {
- if (op.timerId === timerId) {
- return true;
- }
- }
- return false;
- }
- /**
- * For Tests: Runs some or all delayed operations early.
- *
- * @param lastTimerId - Delayed operations up to and including this TimerId
- * will be drained. Pass TimerId.All to run all delayed operations.
- * @returns a Promise that resolves once all operations have been run.
- */
- runAllDelayedOperationsUntil(lastTimerId) {
- // Note that draining may generate more delayed ops, so we do that first.
- return this.drain().then(() => {
- // Run ops in the same order they'd run if they ran naturally.
- this.delayedOperations.sort((a, b) => a.targetTimeMs - b.targetTimeMs);
- for (const op of this.delayedOperations) {
- op.skipDelay();
- if (lastTimerId !== "all" /* TimerId.All */ && op.timerId === lastTimerId) {
- break;
- }
- }
- return this.drain();
- });
- }
- /**
- * For Tests: Skip all subsequent delays for a timer id.
- */
- skipDelaysForTimerId(timerId) {
- this.timerIdsToSkip.push(timerId);
- }
- /** Called once a DelayedOperation is run or canceled. */
- removeDelayedOperation(op) {
- // NOTE: indexOf / slice are O(n), but delayedOperations is expected to be small.
- const index = this.delayedOperations.indexOf(op);
- this.delayedOperations.splice(index, 1);
- }
- }
- function newAsyncQueue() {
- return new AsyncQueueImpl();
- }
- /**
- * Chrome includes Error.message in Error.stack. Other browsers do not.
- * This returns expected output of message + stack when available.
- * @param error - Error or FirestoreError
- */
- function getMessageOrStack(error) {
- let message = error.message || '';
- if (error.stack) {
- if (error.stack.includes(error.message)) {
- message = error.stack;
- }
- else {
- message = error.message + '\n' + error.stack;
- }
- }
- return message;
- }
-
- /**
- * @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.
- */
- // TODO(mrschmidt) Consider using `BaseTransaction` as the base class in the
- // legacy SDK.
- /**
- * A reference to a transaction.
- *
- * The `Transaction` object passed to a transaction's `updateFunction` provides
- * the methods to read and write data within the transaction context. See
- * {@link runTransaction}.
- */
- class Transaction {
- /** @hideconstructor */
- constructor(_firestore, _transaction) {
- this._firestore = _firestore;
- this._transaction = _transaction;
- this._dataReader = newUserDataReader(_firestore);
- }
- /**
- * Reads the document referenced by the provided {@link DocumentReference}.
- *
- * @param documentRef - A reference to the document to be read.
- * @returns A `DocumentSnapshot` with the read data.
- */
- get(documentRef) {
- const ref = validateReference(documentRef, this._firestore);
- const userDataWriter = new LiteUserDataWriter(this._firestore);
- return this._transaction.lookup([ref._key]).then(docs => {
- if (!docs || docs.length !== 1) {
- return fail();
- }
- const doc = docs[0];
- if (doc.isFoundDocument()) {
- return new DocumentSnapshot(this._firestore, userDataWriter, doc.key, doc, ref.converter);
- }
- else if (doc.isNoDocument()) {
- return new DocumentSnapshot(this._firestore, userDataWriter, ref._key, null, ref.converter);
- }
- else {
- throw fail();
- }
- });
- }
- set(documentRef, value, options) {
- const ref = validateReference(documentRef, this._firestore);
- const convertedValue = applyFirestoreDataConverter(ref.converter, value, options);
- const parsed = parseSetData(this._dataReader, 'Transaction.set', ref._key, convertedValue, ref.converter !== null, options);
- this._transaction.set(ref._key, parsed);
- return this;
- }
- update(documentRef, fieldOrUpdateData, value, ...moreFieldsAndValues) {
- const ref = validateReference(documentRef, this._firestore);
- // For Compat types, we have to "extract" the underlying types before
- // performing validation.
- fieldOrUpdateData = getModularInstance(fieldOrUpdateData);
- let parsed;
- if (typeof fieldOrUpdateData === 'string' ||
- fieldOrUpdateData instanceof FieldPath) {
- parsed = parseUpdateVarargs(this._dataReader, 'Transaction.update', ref._key, fieldOrUpdateData, value, moreFieldsAndValues);
- }
- else {
- parsed = parseUpdateData(this._dataReader, 'Transaction.update', ref._key, fieldOrUpdateData);
- }
- this._transaction.update(ref._key, parsed);
- return this;
- }
- /**
- * Deletes the document referred to by the provided {@link DocumentReference}.
- *
- * @param documentRef - A reference to the document to be deleted.
- * @returns This `Transaction` instance. Used for chaining method calls.
- */
- delete(documentRef) {
- const ref = validateReference(documentRef, this._firestore);
- this._transaction.delete(ref._key);
- return this;
- }
- }
- /**
- * Executes the given `updateFunction` and then attempts to commit the changes
- * applied within the transaction. If any document read within the transaction
- * has changed, Cloud Firestore retries the `updateFunction`. If it fails to
- * commit after 5 attempts, the transaction fails.
- *
- * The maximum number of writes allowed in a single transaction is 500.
- *
- * @param firestore - A reference to the Firestore database to run this
- * transaction against.
- * @param updateFunction - The function to execute within the transaction
- * context.
- * @param options - An options object to configure maximum number of attempts to
- * commit.
- * @returns If the transaction completed successfully or was explicitly aborted
- * (the `updateFunction` returned a failed promise), the promise returned by the
- * `updateFunction `is returned here. Otherwise, if the transaction failed, a
- * rejected promise with the corresponding failure error is returned.
- */
- function runTransaction(firestore, updateFunction, options) {
- firestore = cast(firestore, Firestore);
- const datastore = getDatastore(firestore);
- const optionsWithDefaults = Object.assign(Object.assign({}, DEFAULT_TRANSACTION_OPTIONS), options);
- validateTransactionOptions(optionsWithDefaults);
- const deferred = new Deferred();
- new TransactionRunner(newAsyncQueue(), datastore, optionsWithDefaults, internalTransaction => updateFunction(new Transaction(firestore, internalTransaction)), deferred).run();
- return deferred.promise;
- }
-
- /**
- * Firestore Lite
- *
- * @remarks Firestore Lite is a small online-only SDK that allows read
- * and write access to your Firestore database. All operations connect
- * directly to the backend, and `onSnapshot()` APIs are not supported.
- * @packageDocumentation
- */
- registerFirestore();
-
- export { AggregateField, AggregateQuerySnapshot, Bytes, CollectionReference, DocumentReference, DocumentSnapshot, FieldPath, FieldValue, Firestore, FirestoreError, GeoPoint, Query, QueryCompositeFilterConstraint, QueryConstraint, QueryDocumentSnapshot, QueryEndAtConstraint, QueryFieldFilterConstraint, QueryLimitConstraint, QueryOrderByConstraint, QuerySnapshot, QueryStartAtConstraint, Timestamp, Transaction, WriteBatch, addDoc, aggregateQuerySnapshotEqual, and, arrayRemove, arrayUnion, collection, collectionGroup, connectFirestoreEmulator, deleteDoc, deleteField, doc, documentId, endAt, endBefore, getCount, getDoc, getDocs, getFirestore, increment, initializeFirestore, limit, limitToLast, or, orderBy, query, queryEqual, refEqual, runTransaction, serverTimestamp, setDoc, setLogLevel, snapshotEqual, startAfter, startAt, terminate, updateDoc, where, writeBatch };
- //# sourceMappingURL=index.node.mjs.map
|