index.js 306 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555
  1. var {
  2. _nullishCoalesce,
  3. _optionalChain
  4. } = require('@sentry/utils');
  5. Object.defineProperty(exports, '__esModule', { value: true });
  6. const core = require('@sentry/core');
  7. const utils = require('@sentry/utils');
  8. const tracing = require('@sentry-internal/tracing');
  9. // exporting a separate copy of `WINDOW` rather than exporting the one from `@sentry/browser`
  10. // prevents the browser package from being bundled in the CDN bundle, and avoids a
  11. // circular dependency between the browser and replay packages should `@sentry/browser` import
  12. // from `@sentry/replay` in the future
  13. const WINDOW = utils.GLOBAL_OBJ ;
  14. const REPLAY_SESSION_KEY = 'sentryReplaySession';
  15. const REPLAY_EVENT_NAME = 'replay_event';
  16. const UNABLE_TO_SEND_REPLAY = 'Unable to send Replay';
  17. // The idle limit for a session after which recording is paused.
  18. const SESSION_IDLE_PAUSE_DURATION = 300000; // 5 minutes in ms
  19. // The idle limit for a session after which the session expires.
  20. const SESSION_IDLE_EXPIRE_DURATION = 900000; // 15 minutes in ms
  21. /** Default flush delays */
  22. const DEFAULT_FLUSH_MIN_DELAY = 5000;
  23. // XXX: Temp fix for our debounce logic where `maxWait` would never occur if it
  24. // was the same as `wait`
  25. const DEFAULT_FLUSH_MAX_DELAY = 5500;
  26. /* How long to wait for error checkouts */
  27. const BUFFER_CHECKOUT_TIME = 60000;
  28. const RETRY_BASE_INTERVAL = 5000;
  29. const RETRY_MAX_COUNT = 3;
  30. /* The max (uncompressed) size in bytes of a network body. Any body larger than this will be truncated. */
  31. const NETWORK_BODY_MAX_SIZE = 150000;
  32. /* The max size of a single console arg that is captured. Any arg larger than this will be truncated. */
  33. const CONSOLE_ARG_MAX_SIZE = 5000;
  34. /* Min. time to wait before we consider something a slow click. */
  35. const SLOW_CLICK_THRESHOLD = 3000;
  36. /* For scroll actions after a click, we only look for a very short time period to detect programmatic scrolling. */
  37. const SLOW_CLICK_SCROLL_TIMEOUT = 300;
  38. /** When encountering a total segment size exceeding this size, stop the replay (as we cannot properly ingest it). */
  39. const REPLAY_MAX_EVENT_BUFFER_SIZE = 20000000; // ~20MB
  40. /** Replays must be min. 5s long before we send them. */
  41. const MIN_REPLAY_DURATION = 4999;
  42. /* The max. allowed value that the minReplayDuration can be set to. */
  43. const MIN_REPLAY_DURATION_LIMIT = 15000;
  44. /** The max. length of a replay. */
  45. const MAX_REPLAY_DURATION = 3600000; // 60 minutes in ms;
  46. function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var NodeType$1;
  47. (function (NodeType) {
  48. NodeType[NodeType["Document"] = 0] = "Document";
  49. NodeType[NodeType["DocumentType"] = 1] = "DocumentType";
  50. NodeType[NodeType["Element"] = 2] = "Element";
  51. NodeType[NodeType["Text"] = 3] = "Text";
  52. NodeType[NodeType["CDATA"] = 4] = "CDATA";
  53. NodeType[NodeType["Comment"] = 5] = "Comment";
  54. })(NodeType$1 || (NodeType$1 = {}));
  55. function isElement$1(n) {
  56. return n.nodeType === n.ELEMENT_NODE;
  57. }
  58. function isShadowRoot(n) {
  59. const host = _optionalChain$5([n, 'optionalAccess', _ => _.host]);
  60. return Boolean(_optionalChain$5([host, 'optionalAccess', _2 => _2.shadowRoot]) === n);
  61. }
  62. function isNativeShadowDom(shadowRoot) {
  63. return Object.prototype.toString.call(shadowRoot) === '[object ShadowRoot]';
  64. }
  65. function fixBrowserCompatibilityIssuesInCSS(cssText) {
  66. if (cssText.includes(' background-clip: text;') &&
  67. !cssText.includes(' -webkit-background-clip: text;')) {
  68. cssText = cssText.replace(' background-clip: text;', ' -webkit-background-clip: text; background-clip: text;');
  69. }
  70. return cssText;
  71. }
  72. function escapeImportStatement(rule) {
  73. const { cssText } = rule;
  74. if (cssText.split('"').length < 3)
  75. return cssText;
  76. const statement = ['@import', `url(${JSON.stringify(rule.href)})`];
  77. if (rule.layerName === '') {
  78. statement.push(`layer`);
  79. }
  80. else if (rule.layerName) {
  81. statement.push(`layer(${rule.layerName})`);
  82. }
  83. if (rule.supportsText) {
  84. statement.push(`supports(${rule.supportsText})`);
  85. }
  86. if (rule.media.length) {
  87. statement.push(rule.media.mediaText);
  88. }
  89. return statement.join(' ') + ';';
  90. }
  91. function stringifyStylesheet(s) {
  92. try {
  93. const rules = s.rules || s.cssRules;
  94. return rules
  95. ? fixBrowserCompatibilityIssuesInCSS(Array.from(rules, stringifyRule).join(''))
  96. : null;
  97. }
  98. catch (error) {
  99. return null;
  100. }
  101. }
  102. function stringifyRule(rule) {
  103. let importStringified;
  104. if (isCSSImportRule(rule)) {
  105. try {
  106. importStringified =
  107. stringifyStylesheet(rule.styleSheet) ||
  108. escapeImportStatement(rule);
  109. }
  110. catch (error) {
  111. }
  112. }
  113. else if (isCSSStyleRule(rule) && rule.selectorText.includes(':')) {
  114. return fixSafariColons(rule.cssText);
  115. }
  116. return importStringified || rule.cssText;
  117. }
  118. function fixSafariColons(cssStringified) {
  119. const regex = /(\[(?:[\w-]+)[^\\])(:(?:[\w-]+)\])/gm;
  120. return cssStringified.replace(regex, '$1\\$2');
  121. }
  122. function isCSSImportRule(rule) {
  123. return 'styleSheet' in rule;
  124. }
  125. function isCSSStyleRule(rule) {
  126. return 'selectorText' in rule;
  127. }
  128. class Mirror {
  129. constructor() {
  130. this.idNodeMap = new Map();
  131. this.nodeMetaMap = new WeakMap();
  132. }
  133. getId(n) {
  134. if (!n)
  135. return -1;
  136. const id = _optionalChain$5([this, 'access', _3 => _3.getMeta, 'call', _4 => _4(n), 'optionalAccess', _5 => _5.id]);
  137. return _nullishCoalesce$1(id, () => ( -1));
  138. }
  139. getNode(id) {
  140. return this.idNodeMap.get(id) || null;
  141. }
  142. getIds() {
  143. return Array.from(this.idNodeMap.keys());
  144. }
  145. getMeta(n) {
  146. return this.nodeMetaMap.get(n) || null;
  147. }
  148. removeNodeFromMap(n) {
  149. const id = this.getId(n);
  150. this.idNodeMap.delete(id);
  151. if (n.childNodes) {
  152. n.childNodes.forEach((childNode) => this.removeNodeFromMap(childNode));
  153. }
  154. }
  155. has(id) {
  156. return this.idNodeMap.has(id);
  157. }
  158. hasNode(node) {
  159. return this.nodeMetaMap.has(node);
  160. }
  161. add(n, meta) {
  162. const id = meta.id;
  163. this.idNodeMap.set(id, n);
  164. this.nodeMetaMap.set(n, meta);
  165. }
  166. replace(id, n) {
  167. const oldNode = this.getNode(id);
  168. if (oldNode) {
  169. const meta = this.nodeMetaMap.get(oldNode);
  170. if (meta)
  171. this.nodeMetaMap.set(n, meta);
  172. }
  173. this.idNodeMap.set(id, n);
  174. }
  175. reset() {
  176. this.idNodeMap = new Map();
  177. this.nodeMetaMap = new WeakMap();
  178. }
  179. }
  180. function createMirror() {
  181. return new Mirror();
  182. }
  183. function shouldMaskInput({ maskInputOptions, tagName, type, }) {
  184. if (tagName === 'OPTION') {
  185. tagName = 'SELECT';
  186. }
  187. return Boolean(maskInputOptions[tagName.toLowerCase()] ||
  188. (type && maskInputOptions[type]) ||
  189. type === 'password' ||
  190. (tagName === 'INPUT' && !type && maskInputOptions['text']));
  191. }
  192. function maskInputValue({ isMasked, element, value, maskInputFn, }) {
  193. let text = value || '';
  194. if (!isMasked) {
  195. return text;
  196. }
  197. if (maskInputFn) {
  198. text = maskInputFn(text, element);
  199. }
  200. return '*'.repeat(text.length);
  201. }
  202. function toLowerCase(str) {
  203. return str.toLowerCase();
  204. }
  205. function toUpperCase(str) {
  206. return str.toUpperCase();
  207. }
  208. const ORIGINAL_ATTRIBUTE_NAME = '__rrweb_original__';
  209. function is2DCanvasBlank(canvas) {
  210. const ctx = canvas.getContext('2d');
  211. if (!ctx)
  212. return true;
  213. const chunkSize = 50;
  214. for (let x = 0; x < canvas.width; x += chunkSize) {
  215. for (let y = 0; y < canvas.height; y += chunkSize) {
  216. const getImageData = ctx.getImageData;
  217. const originalGetImageData = ORIGINAL_ATTRIBUTE_NAME in getImageData
  218. ? getImageData[ORIGINAL_ATTRIBUTE_NAME]
  219. : getImageData;
  220. const pixelBuffer = new Uint32Array(originalGetImageData.call(ctx, x, y, Math.min(chunkSize, canvas.width - x), Math.min(chunkSize, canvas.height - y)).data.buffer);
  221. if (pixelBuffer.some((pixel) => pixel !== 0))
  222. return false;
  223. }
  224. }
  225. return true;
  226. }
  227. function getInputType(element) {
  228. const type = element.type;
  229. return element.hasAttribute('data-rr-is-password')
  230. ? 'password'
  231. : type
  232. ?
  233. toLowerCase(type)
  234. : null;
  235. }
  236. function getInputValue(el, tagName, type) {
  237. if (tagName === 'INPUT' && (type === 'radio' || type === 'checkbox')) {
  238. return el.getAttribute('value') || '';
  239. }
  240. return el.value;
  241. }
  242. let _id = 1;
  243. const tagNameRegex = new RegExp('[^a-z0-9-_:]');
  244. const IGNORED_NODE = -2;
  245. function genId() {
  246. return _id++;
  247. }
  248. function getValidTagName(element) {
  249. if (element instanceof HTMLFormElement) {
  250. return 'form';
  251. }
  252. const processedTagName = toLowerCase(element.tagName);
  253. if (tagNameRegex.test(processedTagName)) {
  254. return 'div';
  255. }
  256. return processedTagName;
  257. }
  258. function extractOrigin(url) {
  259. let origin = '';
  260. if (url.indexOf('//') > -1) {
  261. origin = url.split('/').slice(0, 3).join('/');
  262. }
  263. else {
  264. origin = url.split('/')[0];
  265. }
  266. origin = origin.split('?')[0];
  267. return origin;
  268. }
  269. let canvasService;
  270. let canvasCtx;
  271. const URL_IN_CSS_REF = /url\((?:(')([^']*)'|(")(.*?)"|([^)]*))\)/gm;
  272. const URL_PROTOCOL_MATCH = /^(?:[a-z+]+:)?\/\//i;
  273. const URL_WWW_MATCH = /^www\..*/i;
  274. const DATA_URI = /^(data:)([^,]*),(.*)/i;
  275. function absoluteToStylesheet(cssText, href) {
  276. return (cssText || '').replace(URL_IN_CSS_REF, (origin, quote1, path1, quote2, path2, path3) => {
  277. const filePath = path1 || path2 || path3;
  278. const maybeQuote = quote1 || quote2 || '';
  279. if (!filePath) {
  280. return origin;
  281. }
  282. if (URL_PROTOCOL_MATCH.test(filePath) || URL_WWW_MATCH.test(filePath)) {
  283. return `url(${maybeQuote}${filePath}${maybeQuote})`;
  284. }
  285. if (DATA_URI.test(filePath)) {
  286. return `url(${maybeQuote}${filePath}${maybeQuote})`;
  287. }
  288. if (filePath[0] === '/') {
  289. return `url(${maybeQuote}${extractOrigin(href) + filePath}${maybeQuote})`;
  290. }
  291. const stack = href.split('/');
  292. const parts = filePath.split('/');
  293. stack.pop();
  294. for (const part of parts) {
  295. if (part === '.') {
  296. continue;
  297. }
  298. else if (part === '..') {
  299. stack.pop();
  300. }
  301. else {
  302. stack.push(part);
  303. }
  304. }
  305. return `url(${maybeQuote}${stack.join('/')}${maybeQuote})`;
  306. });
  307. }
  308. const SRCSET_NOT_SPACES = /^[^ \t\n\r\u000c]+/;
  309. const SRCSET_COMMAS_OR_SPACES = /^[, \t\n\r\u000c]+/;
  310. function getAbsoluteSrcsetString(doc, attributeValue) {
  311. if (attributeValue.trim() === '') {
  312. return attributeValue;
  313. }
  314. let pos = 0;
  315. function collectCharacters(regEx) {
  316. let chars;
  317. const match = regEx.exec(attributeValue.substring(pos));
  318. if (match) {
  319. chars = match[0];
  320. pos += chars.length;
  321. return chars;
  322. }
  323. return '';
  324. }
  325. const output = [];
  326. while (true) {
  327. collectCharacters(SRCSET_COMMAS_OR_SPACES);
  328. if (pos >= attributeValue.length) {
  329. break;
  330. }
  331. let url = collectCharacters(SRCSET_NOT_SPACES);
  332. if (url.slice(-1) === ',') {
  333. url = absoluteToDoc(doc, url.substring(0, url.length - 1));
  334. output.push(url);
  335. }
  336. else {
  337. let descriptorsStr = '';
  338. url = absoluteToDoc(doc, url);
  339. let inParens = false;
  340. while (true) {
  341. const c = attributeValue.charAt(pos);
  342. if (c === '') {
  343. output.push((url + descriptorsStr).trim());
  344. break;
  345. }
  346. else if (!inParens) {
  347. if (c === ',') {
  348. pos += 1;
  349. output.push((url + descriptorsStr).trim());
  350. break;
  351. }
  352. else if (c === '(') {
  353. inParens = true;
  354. }
  355. }
  356. else {
  357. if (c === ')') {
  358. inParens = false;
  359. }
  360. }
  361. descriptorsStr += c;
  362. pos += 1;
  363. }
  364. }
  365. }
  366. return output.join(', ');
  367. }
  368. function absoluteToDoc(doc, attributeValue) {
  369. if (!attributeValue || attributeValue.trim() === '') {
  370. return attributeValue;
  371. }
  372. const a = doc.createElement('a');
  373. a.href = attributeValue;
  374. return a.href;
  375. }
  376. function isSVGElement(el) {
  377. return Boolean(el.tagName === 'svg' || el.ownerSVGElement);
  378. }
  379. function getHref() {
  380. const a = document.createElement('a');
  381. a.href = '';
  382. return a.href;
  383. }
  384. function transformAttribute(doc, tagName, name, value, element, maskAttributeFn) {
  385. if (!value) {
  386. return value;
  387. }
  388. if (name === 'src' ||
  389. (name === 'href' && !(tagName === 'use' && value[0] === '#'))) {
  390. return absoluteToDoc(doc, value);
  391. }
  392. else if (name === 'xlink:href' && value[0] !== '#') {
  393. return absoluteToDoc(doc, value);
  394. }
  395. else if (name === 'background' &&
  396. (tagName === 'table' || tagName === 'td' || tagName === 'th')) {
  397. return absoluteToDoc(doc, value);
  398. }
  399. else if (name === 'srcset') {
  400. return getAbsoluteSrcsetString(doc, value);
  401. }
  402. else if (name === 'style') {
  403. return absoluteToStylesheet(value, getHref());
  404. }
  405. else if (tagName === 'object' && name === 'data') {
  406. return absoluteToDoc(doc, value);
  407. }
  408. if (typeof maskAttributeFn === 'function') {
  409. return maskAttributeFn(name, value, element);
  410. }
  411. return value;
  412. }
  413. function ignoreAttribute(tagName, name, _value) {
  414. return (tagName === 'video' || tagName === 'audio') && name === 'autoplay';
  415. }
  416. function _isBlockedElement(element, blockClass, blockSelector, unblockSelector) {
  417. try {
  418. if (unblockSelector && element.matches(unblockSelector)) {
  419. return false;
  420. }
  421. if (typeof blockClass === 'string') {
  422. if (element.classList.contains(blockClass)) {
  423. return true;
  424. }
  425. }
  426. else {
  427. for (let eIndex = element.classList.length; eIndex--;) {
  428. const className = element.classList[eIndex];
  429. if (blockClass.test(className)) {
  430. return true;
  431. }
  432. }
  433. }
  434. if (blockSelector) {
  435. return element.matches(blockSelector);
  436. }
  437. }
  438. catch (e) {
  439. }
  440. return false;
  441. }
  442. function elementClassMatchesRegex(el, regex) {
  443. for (let eIndex = el.classList.length; eIndex--;) {
  444. const className = el.classList[eIndex];
  445. if (regex.test(className)) {
  446. return true;
  447. }
  448. }
  449. return false;
  450. }
  451. function distanceToMatch(node, matchPredicate, limit = Infinity, distance = 0) {
  452. if (!node)
  453. return -1;
  454. if (node.nodeType !== node.ELEMENT_NODE)
  455. return -1;
  456. if (distance > limit)
  457. return -1;
  458. if (matchPredicate(node))
  459. return distance;
  460. return distanceToMatch(node.parentNode, matchPredicate, limit, distance + 1);
  461. }
  462. function createMatchPredicate(className, selector) {
  463. return (node) => {
  464. const el = node;
  465. if (el === null)
  466. return false;
  467. try {
  468. if (className) {
  469. if (typeof className === 'string') {
  470. if (el.matches(`.${className}`))
  471. return true;
  472. }
  473. else if (elementClassMatchesRegex(el, className)) {
  474. return true;
  475. }
  476. }
  477. if (selector && el.matches(selector))
  478. return true;
  479. return false;
  480. }
  481. catch (e2) {
  482. return false;
  483. }
  484. };
  485. }
  486. function needMaskingText(node, maskTextClass, maskTextSelector, unmaskTextClass, unmaskTextSelector, maskAllText) {
  487. try {
  488. const el = node.nodeType === node.ELEMENT_NODE
  489. ? node
  490. : node.parentElement;
  491. if (el === null)
  492. return false;
  493. if (el.tagName === 'INPUT') {
  494. const autocomplete = el.getAttribute('autocomplete');
  495. const disallowedAutocompleteValues = [
  496. 'current-password',
  497. 'new-password',
  498. 'cc-number',
  499. 'cc-exp',
  500. 'cc-exp-month',
  501. 'cc-exp-year',
  502. 'cc-csc',
  503. ];
  504. if (disallowedAutocompleteValues.includes(autocomplete)) {
  505. return true;
  506. }
  507. }
  508. let maskDistance = -1;
  509. let unmaskDistance = -1;
  510. if (maskAllText) {
  511. unmaskDistance = distanceToMatch(el, createMatchPredicate(unmaskTextClass, unmaskTextSelector));
  512. if (unmaskDistance < 0) {
  513. return true;
  514. }
  515. maskDistance = distanceToMatch(el, createMatchPredicate(maskTextClass, maskTextSelector), unmaskDistance >= 0 ? unmaskDistance : Infinity);
  516. }
  517. else {
  518. maskDistance = distanceToMatch(el, createMatchPredicate(maskTextClass, maskTextSelector));
  519. if (maskDistance < 0) {
  520. return false;
  521. }
  522. unmaskDistance = distanceToMatch(el, createMatchPredicate(unmaskTextClass, unmaskTextSelector), maskDistance >= 0 ? maskDistance : Infinity);
  523. }
  524. return maskDistance >= 0
  525. ? unmaskDistance >= 0
  526. ? maskDistance <= unmaskDistance
  527. : true
  528. : unmaskDistance >= 0
  529. ? false
  530. : !!maskAllText;
  531. }
  532. catch (e) {
  533. }
  534. return !!maskAllText;
  535. }
  536. function onceIframeLoaded(iframeEl, listener, iframeLoadTimeout) {
  537. const win = iframeEl.contentWindow;
  538. if (!win) {
  539. return;
  540. }
  541. let fired = false;
  542. let readyState;
  543. try {
  544. readyState = win.document.readyState;
  545. }
  546. catch (error) {
  547. return;
  548. }
  549. if (readyState !== 'complete') {
  550. const timer = setTimeout(() => {
  551. if (!fired) {
  552. listener();
  553. fired = true;
  554. }
  555. }, iframeLoadTimeout);
  556. iframeEl.addEventListener('load', () => {
  557. clearTimeout(timer);
  558. fired = true;
  559. listener();
  560. });
  561. return;
  562. }
  563. const blankUrl = 'about:blank';
  564. if (win.location.href !== blankUrl ||
  565. iframeEl.src === blankUrl ||
  566. iframeEl.src === '') {
  567. setTimeout(listener, 0);
  568. return iframeEl.addEventListener('load', listener);
  569. }
  570. iframeEl.addEventListener('load', listener);
  571. }
  572. function onceStylesheetLoaded(link, listener, styleSheetLoadTimeout) {
  573. let fired = false;
  574. let styleSheetLoaded;
  575. try {
  576. styleSheetLoaded = link.sheet;
  577. }
  578. catch (error) {
  579. return;
  580. }
  581. if (styleSheetLoaded)
  582. return;
  583. const timer = setTimeout(() => {
  584. if (!fired) {
  585. listener();
  586. fired = true;
  587. }
  588. }, styleSheetLoadTimeout);
  589. link.addEventListener('load', () => {
  590. clearTimeout(timer);
  591. fired = true;
  592. listener();
  593. });
  594. }
  595. function serializeNode(n, options) {
  596. const { doc, mirror, blockClass, blockSelector, unblockSelector, maskAllText, maskAttributeFn, maskTextClass, unmaskTextClass, maskTextSelector, unmaskTextSelector, inlineStylesheet, maskInputOptions = {}, maskTextFn, maskInputFn, dataURLOptions = {}, inlineImages, recordCanvas, keepIframeSrcFn, newlyAddedElement = false, } = options;
  597. const rootId = getRootId(doc, mirror);
  598. switch (n.nodeType) {
  599. case n.DOCUMENT_NODE:
  600. if (n.compatMode !== 'CSS1Compat') {
  601. return {
  602. type: NodeType$1.Document,
  603. childNodes: [],
  604. compatMode: n.compatMode,
  605. };
  606. }
  607. else {
  608. return {
  609. type: NodeType$1.Document,
  610. childNodes: [],
  611. };
  612. }
  613. case n.DOCUMENT_TYPE_NODE:
  614. return {
  615. type: NodeType$1.DocumentType,
  616. name: n.name,
  617. publicId: n.publicId,
  618. systemId: n.systemId,
  619. rootId,
  620. };
  621. case n.ELEMENT_NODE:
  622. return serializeElementNode(n, {
  623. doc,
  624. blockClass,
  625. blockSelector,
  626. unblockSelector,
  627. inlineStylesheet,
  628. maskAttributeFn,
  629. maskInputOptions,
  630. maskInputFn,
  631. dataURLOptions,
  632. inlineImages,
  633. recordCanvas,
  634. keepIframeSrcFn,
  635. newlyAddedElement,
  636. rootId,
  637. maskAllText,
  638. maskTextClass,
  639. unmaskTextClass,
  640. maskTextSelector,
  641. unmaskTextSelector,
  642. });
  643. case n.TEXT_NODE:
  644. return serializeTextNode(n, {
  645. maskAllText,
  646. maskTextClass,
  647. unmaskTextClass,
  648. maskTextSelector,
  649. unmaskTextSelector,
  650. maskTextFn,
  651. maskInputOptions,
  652. maskInputFn,
  653. rootId,
  654. });
  655. case n.CDATA_SECTION_NODE:
  656. return {
  657. type: NodeType$1.CDATA,
  658. textContent: '',
  659. rootId,
  660. };
  661. case n.COMMENT_NODE:
  662. return {
  663. type: NodeType$1.Comment,
  664. textContent: n.textContent || '',
  665. rootId,
  666. };
  667. default:
  668. return false;
  669. }
  670. }
  671. function getRootId(doc, mirror) {
  672. if (!mirror.hasNode(doc))
  673. return undefined;
  674. const docId = mirror.getId(doc);
  675. return docId === 1 ? undefined : docId;
  676. }
  677. function serializeTextNode(n, options) {
  678. const { maskAllText, maskTextClass, unmaskTextClass, maskTextSelector, unmaskTextSelector, maskTextFn, maskInputOptions, maskInputFn, rootId, } = options;
  679. const parentTagName = n.parentNode && n.parentNode.tagName;
  680. let textContent = n.textContent;
  681. const isStyle = parentTagName === 'STYLE' ? true : undefined;
  682. const isScript = parentTagName === 'SCRIPT' ? true : undefined;
  683. const isTextarea = parentTagName === 'TEXTAREA' ? true : undefined;
  684. if (isStyle && textContent) {
  685. try {
  686. if (n.nextSibling || n.previousSibling) {
  687. }
  688. else if (_optionalChain$5([n, 'access', _6 => _6.parentNode, 'access', _7 => _7.sheet, 'optionalAccess', _8 => _8.cssRules])) {
  689. textContent = stringifyStylesheet(n.parentNode.sheet);
  690. }
  691. }
  692. catch (err) {
  693. console.warn(`Cannot get CSS styles from text's parentNode. Error: ${err}`, n);
  694. }
  695. textContent = absoluteToStylesheet(textContent, getHref());
  696. }
  697. if (isScript) {
  698. textContent = 'SCRIPT_PLACEHOLDER';
  699. }
  700. const forceMask = needMaskingText(n, maskTextClass, maskTextSelector, unmaskTextClass, unmaskTextSelector, maskAllText);
  701. if (!isStyle && !isScript && !isTextarea && textContent && forceMask) {
  702. textContent = maskTextFn
  703. ? maskTextFn(textContent)
  704. : textContent.replace(/[\S]/g, '*');
  705. }
  706. if (isTextarea && textContent && (maskInputOptions.textarea || forceMask)) {
  707. textContent = maskInputFn
  708. ? maskInputFn(textContent, n.parentNode)
  709. : textContent.replace(/[\S]/g, '*');
  710. }
  711. if (parentTagName === 'OPTION' && textContent) {
  712. const isInputMasked = shouldMaskInput({
  713. type: null,
  714. tagName: parentTagName,
  715. maskInputOptions,
  716. });
  717. textContent = maskInputValue({
  718. isMasked: needMaskingText(n, maskTextClass, maskTextSelector, unmaskTextClass, unmaskTextSelector, isInputMasked),
  719. element: n,
  720. value: textContent,
  721. maskInputFn,
  722. });
  723. }
  724. return {
  725. type: NodeType$1.Text,
  726. textContent: textContent || '',
  727. isStyle,
  728. rootId,
  729. };
  730. }
  731. function serializeElementNode(n, options) {
  732. const { doc, blockClass, blockSelector, unblockSelector, inlineStylesheet, maskInputOptions = {}, maskAttributeFn, maskInputFn, dataURLOptions = {}, inlineImages, recordCanvas, keepIframeSrcFn, newlyAddedElement = false, rootId, maskAllText, maskTextClass, unmaskTextClass, maskTextSelector, unmaskTextSelector, } = options;
  733. const needBlock = _isBlockedElement(n, blockClass, blockSelector, unblockSelector);
  734. const tagName = getValidTagName(n);
  735. let attributes = {};
  736. const len = n.attributes.length;
  737. for (let i = 0; i < len; i++) {
  738. const attr = n.attributes[i];
  739. if (attr.name && !ignoreAttribute(tagName, attr.name, attr.value)) {
  740. attributes[attr.name] = transformAttribute(doc, tagName, toLowerCase(attr.name), attr.value, n, maskAttributeFn);
  741. }
  742. }
  743. if (tagName === 'link' && inlineStylesheet) {
  744. const stylesheet = Array.from(doc.styleSheets).find((s) => {
  745. return s.href === n.href;
  746. });
  747. let cssText = null;
  748. if (stylesheet) {
  749. cssText = stringifyStylesheet(stylesheet);
  750. }
  751. if (cssText) {
  752. delete attributes.rel;
  753. delete attributes.href;
  754. attributes._cssText = absoluteToStylesheet(cssText, stylesheet.href);
  755. }
  756. }
  757. if (tagName === 'style' &&
  758. n.sheet &&
  759. !(n.innerText || n.textContent || '').trim().length) {
  760. const cssText = stringifyStylesheet(n.sheet);
  761. if (cssText) {
  762. attributes._cssText = absoluteToStylesheet(cssText, getHref());
  763. }
  764. }
  765. if (tagName === 'input' ||
  766. tagName === 'textarea' ||
  767. tagName === 'select' ||
  768. tagName === 'option') {
  769. const el = n;
  770. const type = getInputType(el);
  771. const value = getInputValue(el, toUpperCase(tagName), type);
  772. const checked = el.checked;
  773. if (type !== 'submit' && type !== 'button' && value) {
  774. const forceMask = needMaskingText(el, maskTextClass, maskTextSelector, unmaskTextClass, unmaskTextSelector, shouldMaskInput({
  775. type,
  776. tagName: toUpperCase(tagName),
  777. maskInputOptions,
  778. }));
  779. attributes.value = maskInputValue({
  780. isMasked: forceMask,
  781. element: el,
  782. value,
  783. maskInputFn,
  784. });
  785. }
  786. if (checked) {
  787. attributes.checked = checked;
  788. }
  789. }
  790. if (tagName === 'option') {
  791. if (n.selected && !maskInputOptions['select']) {
  792. attributes.selected = true;
  793. }
  794. else {
  795. delete attributes.selected;
  796. }
  797. }
  798. if (tagName === 'canvas' && recordCanvas) {
  799. if (n.__context === '2d') {
  800. if (!is2DCanvasBlank(n)) {
  801. attributes.rr_dataURL = n.toDataURL(dataURLOptions.type, dataURLOptions.quality);
  802. }
  803. }
  804. else if (!('__context' in n)) {
  805. const canvasDataURL = n.toDataURL(dataURLOptions.type, dataURLOptions.quality);
  806. const blankCanvas = document.createElement('canvas');
  807. blankCanvas.width = n.width;
  808. blankCanvas.height = n.height;
  809. const blankCanvasDataURL = blankCanvas.toDataURL(dataURLOptions.type, dataURLOptions.quality);
  810. if (canvasDataURL !== blankCanvasDataURL) {
  811. attributes.rr_dataURL = canvasDataURL;
  812. }
  813. }
  814. }
  815. if (tagName === 'img' && inlineImages) {
  816. if (!canvasService) {
  817. canvasService = doc.createElement('canvas');
  818. canvasCtx = canvasService.getContext('2d');
  819. }
  820. const image = n;
  821. const oldValue = image.crossOrigin;
  822. image.crossOrigin = 'anonymous';
  823. const recordInlineImage = () => {
  824. image.removeEventListener('load', recordInlineImage);
  825. try {
  826. canvasService.width = image.naturalWidth;
  827. canvasService.height = image.naturalHeight;
  828. canvasCtx.drawImage(image, 0, 0);
  829. attributes.rr_dataURL = canvasService.toDataURL(dataURLOptions.type, dataURLOptions.quality);
  830. }
  831. catch (err) {
  832. console.warn(`Cannot inline img src=${image.currentSrc}! Error: ${err}`);
  833. }
  834. oldValue
  835. ? (attributes.crossOrigin = oldValue)
  836. : image.removeAttribute('crossorigin');
  837. };
  838. if (image.complete && image.naturalWidth !== 0)
  839. recordInlineImage();
  840. else
  841. image.addEventListener('load', recordInlineImage);
  842. }
  843. if (tagName === 'audio' || tagName === 'video') {
  844. attributes.rr_mediaState = n.paused
  845. ? 'paused'
  846. : 'played';
  847. attributes.rr_mediaCurrentTime = n.currentTime;
  848. }
  849. if (!newlyAddedElement) {
  850. if (n.scrollLeft) {
  851. attributes.rr_scrollLeft = n.scrollLeft;
  852. }
  853. if (n.scrollTop) {
  854. attributes.rr_scrollTop = n.scrollTop;
  855. }
  856. }
  857. if (needBlock) {
  858. const { width, height } = n.getBoundingClientRect();
  859. attributes = {
  860. class: attributes.class,
  861. rr_width: `${width}px`,
  862. rr_height: `${height}px`,
  863. };
  864. }
  865. if (tagName === 'iframe' && !keepIframeSrcFn(attributes.src)) {
  866. if (!n.contentDocument) {
  867. attributes.rr_src = attributes.src;
  868. }
  869. delete attributes.src;
  870. }
  871. let isCustomElement;
  872. try {
  873. if (customElements.get(tagName))
  874. isCustomElement = true;
  875. }
  876. catch (e) {
  877. }
  878. return {
  879. type: NodeType$1.Element,
  880. tagName,
  881. attributes,
  882. childNodes: [],
  883. isSVG: isSVGElement(n) || undefined,
  884. needBlock,
  885. rootId,
  886. isCustom: isCustomElement,
  887. };
  888. }
  889. function lowerIfExists(maybeAttr) {
  890. if (maybeAttr === undefined || maybeAttr === null) {
  891. return '';
  892. }
  893. else {
  894. return maybeAttr.toLowerCase();
  895. }
  896. }
  897. function slimDOMExcluded(sn, slimDOMOptions) {
  898. if (slimDOMOptions.comment && sn.type === NodeType$1.Comment) {
  899. return true;
  900. }
  901. else if (sn.type === NodeType$1.Element) {
  902. if (slimDOMOptions.script &&
  903. (sn.tagName === 'script' ||
  904. (sn.tagName === 'link' &&
  905. (sn.attributes.rel === 'preload' ||
  906. sn.attributes.rel === 'modulepreload') &&
  907. sn.attributes.as === 'script') ||
  908. (sn.tagName === 'link' &&
  909. sn.attributes.rel === 'prefetch' &&
  910. typeof sn.attributes.href === 'string' &&
  911. sn.attributes.href.endsWith('.js')))) {
  912. return true;
  913. }
  914. else if (slimDOMOptions.headFavicon &&
  915. ((sn.tagName === 'link' && sn.attributes.rel === 'shortcut icon') ||
  916. (sn.tagName === 'meta' &&
  917. (lowerIfExists(sn.attributes.name).match(/^msapplication-tile(image|color)$/) ||
  918. lowerIfExists(sn.attributes.name) === 'application-name' ||
  919. lowerIfExists(sn.attributes.rel) === 'icon' ||
  920. lowerIfExists(sn.attributes.rel) === 'apple-touch-icon' ||
  921. lowerIfExists(sn.attributes.rel) === 'shortcut icon')))) {
  922. return true;
  923. }
  924. else if (sn.tagName === 'meta') {
  925. if (slimDOMOptions.headMetaDescKeywords &&
  926. lowerIfExists(sn.attributes.name).match(/^description|keywords$/)) {
  927. return true;
  928. }
  929. else if (slimDOMOptions.headMetaSocial &&
  930. (lowerIfExists(sn.attributes.property).match(/^(og|twitter|fb):/) ||
  931. lowerIfExists(sn.attributes.name).match(/^(og|twitter):/) ||
  932. lowerIfExists(sn.attributes.name) === 'pinterest')) {
  933. return true;
  934. }
  935. else if (slimDOMOptions.headMetaRobots &&
  936. (lowerIfExists(sn.attributes.name) === 'robots' ||
  937. lowerIfExists(sn.attributes.name) === 'googlebot' ||
  938. lowerIfExists(sn.attributes.name) === 'bingbot')) {
  939. return true;
  940. }
  941. else if (slimDOMOptions.headMetaHttpEquiv &&
  942. sn.attributes['http-equiv'] !== undefined) {
  943. return true;
  944. }
  945. else if (slimDOMOptions.headMetaAuthorship &&
  946. (lowerIfExists(sn.attributes.name) === 'author' ||
  947. lowerIfExists(sn.attributes.name) === 'generator' ||
  948. lowerIfExists(sn.attributes.name) === 'framework' ||
  949. lowerIfExists(sn.attributes.name) === 'publisher' ||
  950. lowerIfExists(sn.attributes.name) === 'progid' ||
  951. lowerIfExists(sn.attributes.property).match(/^article:/) ||
  952. lowerIfExists(sn.attributes.property).match(/^product:/))) {
  953. return true;
  954. }
  955. else if (slimDOMOptions.headMetaVerification &&
  956. (lowerIfExists(sn.attributes.name) === 'google-site-verification' ||
  957. lowerIfExists(sn.attributes.name) === 'yandex-verification' ||
  958. lowerIfExists(sn.attributes.name) === 'csrf-token' ||
  959. lowerIfExists(sn.attributes.name) === 'p:domain_verify' ||
  960. lowerIfExists(sn.attributes.name) === 'verify-v1' ||
  961. lowerIfExists(sn.attributes.name) === 'verification' ||
  962. lowerIfExists(sn.attributes.name) === 'shopify-checkout-api-token')) {
  963. return true;
  964. }
  965. }
  966. }
  967. return false;
  968. }
  969. function serializeNodeWithId(n, options) {
  970. const { doc, mirror, blockClass, blockSelector, unblockSelector, maskAllText, maskTextClass, unmaskTextClass, maskTextSelector, unmaskTextSelector, skipChild = false, inlineStylesheet = true, maskInputOptions = {}, maskAttributeFn, maskTextFn, maskInputFn, slimDOMOptions, dataURLOptions = {}, inlineImages = false, recordCanvas = false, onSerialize, onIframeLoad, iframeLoadTimeout = 5000, onStylesheetLoad, stylesheetLoadTimeout = 5000, keepIframeSrcFn = () => false, newlyAddedElement = false, } = options;
  971. let { preserveWhiteSpace = true } = options;
  972. const _serializedNode = serializeNode(n, {
  973. doc,
  974. mirror,
  975. blockClass,
  976. blockSelector,
  977. maskAllText,
  978. unblockSelector,
  979. maskTextClass,
  980. unmaskTextClass,
  981. maskTextSelector,
  982. unmaskTextSelector,
  983. inlineStylesheet,
  984. maskInputOptions,
  985. maskAttributeFn,
  986. maskTextFn,
  987. maskInputFn,
  988. dataURLOptions,
  989. inlineImages,
  990. recordCanvas,
  991. keepIframeSrcFn,
  992. newlyAddedElement,
  993. });
  994. if (!_serializedNode) {
  995. console.warn(n, 'not serialized');
  996. return null;
  997. }
  998. let id;
  999. if (mirror.hasNode(n)) {
  1000. id = mirror.getId(n);
  1001. }
  1002. else if (slimDOMExcluded(_serializedNode, slimDOMOptions) ||
  1003. (!preserveWhiteSpace &&
  1004. _serializedNode.type === NodeType$1.Text &&
  1005. !_serializedNode.isStyle &&
  1006. !_serializedNode.textContent.replace(/^\s+|\s+$/gm, '').length)) {
  1007. id = IGNORED_NODE;
  1008. }
  1009. else {
  1010. id = genId();
  1011. }
  1012. const serializedNode = Object.assign(_serializedNode, { id });
  1013. mirror.add(n, serializedNode);
  1014. if (id === IGNORED_NODE) {
  1015. return null;
  1016. }
  1017. if (onSerialize) {
  1018. onSerialize(n);
  1019. }
  1020. let recordChild = !skipChild;
  1021. if (serializedNode.type === NodeType$1.Element) {
  1022. recordChild = recordChild && !serializedNode.needBlock;
  1023. delete serializedNode.needBlock;
  1024. const shadowRoot = n.shadowRoot;
  1025. if (shadowRoot && isNativeShadowDom(shadowRoot))
  1026. serializedNode.isShadowHost = true;
  1027. }
  1028. if ((serializedNode.type === NodeType$1.Document ||
  1029. serializedNode.type === NodeType$1.Element) &&
  1030. recordChild) {
  1031. if (slimDOMOptions.headWhitespace &&
  1032. serializedNode.type === NodeType$1.Element &&
  1033. serializedNode.tagName === 'head') {
  1034. preserveWhiteSpace = false;
  1035. }
  1036. const bypassOptions = {
  1037. doc,
  1038. mirror,
  1039. blockClass,
  1040. blockSelector,
  1041. maskAllText,
  1042. unblockSelector,
  1043. maskTextClass,
  1044. unmaskTextClass,
  1045. maskTextSelector,
  1046. unmaskTextSelector,
  1047. skipChild,
  1048. inlineStylesheet,
  1049. maskInputOptions,
  1050. maskAttributeFn,
  1051. maskTextFn,
  1052. maskInputFn,
  1053. slimDOMOptions,
  1054. dataURLOptions,
  1055. inlineImages,
  1056. recordCanvas,
  1057. preserveWhiteSpace,
  1058. onSerialize,
  1059. onIframeLoad,
  1060. iframeLoadTimeout,
  1061. onStylesheetLoad,
  1062. stylesheetLoadTimeout,
  1063. keepIframeSrcFn,
  1064. };
  1065. for (const childN of Array.from(n.childNodes)) {
  1066. const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
  1067. if (serializedChildNode) {
  1068. serializedNode.childNodes.push(serializedChildNode);
  1069. }
  1070. }
  1071. if (isElement$1(n) && n.shadowRoot) {
  1072. for (const childN of Array.from(n.shadowRoot.childNodes)) {
  1073. const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
  1074. if (serializedChildNode) {
  1075. isNativeShadowDom(n.shadowRoot) &&
  1076. (serializedChildNode.isShadow = true);
  1077. serializedNode.childNodes.push(serializedChildNode);
  1078. }
  1079. }
  1080. }
  1081. }
  1082. if (n.parentNode &&
  1083. isShadowRoot(n.parentNode) &&
  1084. isNativeShadowDom(n.parentNode)) {
  1085. serializedNode.isShadow = true;
  1086. }
  1087. if (serializedNode.type === NodeType$1.Element &&
  1088. serializedNode.tagName === 'iframe') {
  1089. onceIframeLoaded(n, () => {
  1090. const iframeDoc = n.contentDocument;
  1091. if (iframeDoc && onIframeLoad) {
  1092. const serializedIframeNode = serializeNodeWithId(iframeDoc, {
  1093. doc: iframeDoc,
  1094. mirror,
  1095. blockClass,
  1096. blockSelector,
  1097. unblockSelector,
  1098. maskAllText,
  1099. maskTextClass,
  1100. unmaskTextClass,
  1101. maskTextSelector,
  1102. unmaskTextSelector,
  1103. skipChild: false,
  1104. inlineStylesheet,
  1105. maskInputOptions,
  1106. maskAttributeFn,
  1107. maskTextFn,
  1108. maskInputFn,
  1109. slimDOMOptions,
  1110. dataURLOptions,
  1111. inlineImages,
  1112. recordCanvas,
  1113. preserveWhiteSpace,
  1114. onSerialize,
  1115. onIframeLoad,
  1116. iframeLoadTimeout,
  1117. onStylesheetLoad,
  1118. stylesheetLoadTimeout,
  1119. keepIframeSrcFn,
  1120. });
  1121. if (serializedIframeNode) {
  1122. onIframeLoad(n, serializedIframeNode);
  1123. }
  1124. }
  1125. }, iframeLoadTimeout);
  1126. }
  1127. if (serializedNode.type === NodeType$1.Element &&
  1128. serializedNode.tagName === 'link' &&
  1129. serializedNode.attributes.rel === 'stylesheet') {
  1130. onceStylesheetLoaded(n, () => {
  1131. if (onStylesheetLoad) {
  1132. const serializedLinkNode = serializeNodeWithId(n, {
  1133. doc,
  1134. mirror,
  1135. blockClass,
  1136. blockSelector,
  1137. unblockSelector,
  1138. maskAllText,
  1139. maskTextClass,
  1140. unmaskTextClass,
  1141. maskTextSelector,
  1142. unmaskTextSelector,
  1143. skipChild: false,
  1144. inlineStylesheet,
  1145. maskInputOptions,
  1146. maskAttributeFn,
  1147. maskTextFn,
  1148. maskInputFn,
  1149. slimDOMOptions,
  1150. dataURLOptions,
  1151. inlineImages,
  1152. recordCanvas,
  1153. preserveWhiteSpace,
  1154. onSerialize,
  1155. onIframeLoad,
  1156. iframeLoadTimeout,
  1157. onStylesheetLoad,
  1158. stylesheetLoadTimeout,
  1159. keepIframeSrcFn,
  1160. });
  1161. if (serializedLinkNode) {
  1162. onStylesheetLoad(n, serializedLinkNode);
  1163. }
  1164. }
  1165. }, stylesheetLoadTimeout);
  1166. }
  1167. return serializedNode;
  1168. }
  1169. function snapshot(n, options) {
  1170. const { mirror = new Mirror(), blockClass = 'rr-block', blockSelector = null, unblockSelector = null, maskAllText = false, maskTextClass = 'rr-mask', unmaskTextClass = null, maskTextSelector = null, unmaskTextSelector = null, inlineStylesheet = true, inlineImages = false, recordCanvas = false, maskAllInputs = false, maskAttributeFn, maskTextFn, maskInputFn, slimDOM = false, dataURLOptions, preserveWhiteSpace, onSerialize, onIframeLoad, iframeLoadTimeout, onStylesheetLoad, stylesheetLoadTimeout, keepIframeSrcFn = () => false, } = options || {};
  1171. const maskInputOptions = maskAllInputs === true
  1172. ? {
  1173. color: true,
  1174. date: true,
  1175. 'datetime-local': true,
  1176. email: true,
  1177. month: true,
  1178. number: true,
  1179. range: true,
  1180. search: true,
  1181. tel: true,
  1182. text: true,
  1183. time: true,
  1184. url: true,
  1185. week: true,
  1186. textarea: true,
  1187. select: true,
  1188. }
  1189. : maskAllInputs === false
  1190. ? {}
  1191. : maskAllInputs;
  1192. const slimDOMOptions = slimDOM === true || slimDOM === 'all'
  1193. ?
  1194. {
  1195. script: true,
  1196. comment: true,
  1197. headFavicon: true,
  1198. headWhitespace: true,
  1199. headMetaDescKeywords: slimDOM === 'all',
  1200. headMetaSocial: true,
  1201. headMetaRobots: true,
  1202. headMetaHttpEquiv: true,
  1203. headMetaAuthorship: true,
  1204. headMetaVerification: true,
  1205. }
  1206. : slimDOM === false
  1207. ? {}
  1208. : slimDOM;
  1209. return serializeNodeWithId(n, {
  1210. doc: n,
  1211. mirror,
  1212. blockClass,
  1213. blockSelector,
  1214. unblockSelector,
  1215. maskAllText,
  1216. maskTextClass,
  1217. unmaskTextClass,
  1218. maskTextSelector,
  1219. unmaskTextSelector,
  1220. skipChild: false,
  1221. inlineStylesheet,
  1222. maskInputOptions,
  1223. maskAttributeFn,
  1224. maskTextFn,
  1225. maskInputFn,
  1226. slimDOMOptions,
  1227. dataURLOptions,
  1228. inlineImages,
  1229. recordCanvas,
  1230. preserveWhiteSpace,
  1231. onSerialize,
  1232. onIframeLoad,
  1233. iframeLoadTimeout,
  1234. onStylesheetLoad,
  1235. stylesheetLoadTimeout,
  1236. keepIframeSrcFn,
  1237. newlyAddedElement: false,
  1238. });
  1239. }
  1240. function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
  1241. function on(type, fn, target = document) {
  1242. const options = { capture: true, passive: true };
  1243. target.addEventListener(type, fn, options);
  1244. return () => target.removeEventListener(type, fn, options);
  1245. }
  1246. const DEPARTED_MIRROR_ACCESS_WARNING = 'Please stop import mirror directly. Instead of that,' +
  1247. '\r\n' +
  1248. 'now you can use replayer.getMirror() to access the mirror instance of a replayer,' +
  1249. '\r\n' +
  1250. 'or you can use record.mirror to access the mirror instance during recording.';
  1251. let _mirror = {
  1252. map: {},
  1253. getId() {
  1254. console.error(DEPARTED_MIRROR_ACCESS_WARNING);
  1255. return -1;
  1256. },
  1257. getNode() {
  1258. console.error(DEPARTED_MIRROR_ACCESS_WARNING);
  1259. return null;
  1260. },
  1261. removeNodeFromMap() {
  1262. console.error(DEPARTED_MIRROR_ACCESS_WARNING);
  1263. },
  1264. has() {
  1265. console.error(DEPARTED_MIRROR_ACCESS_WARNING);
  1266. return false;
  1267. },
  1268. reset() {
  1269. console.error(DEPARTED_MIRROR_ACCESS_WARNING);
  1270. },
  1271. };
  1272. if (typeof window !== 'undefined' && window.Proxy && window.Reflect) {
  1273. _mirror = new Proxy(_mirror, {
  1274. get(target, prop, receiver) {
  1275. if (prop === 'map') {
  1276. console.error(DEPARTED_MIRROR_ACCESS_WARNING);
  1277. }
  1278. return Reflect.get(target, prop, receiver);
  1279. },
  1280. });
  1281. }
  1282. function throttle$1(func, wait, options = {}) {
  1283. let timeout = null;
  1284. let previous = 0;
  1285. return function (...args) {
  1286. const now = Date.now();
  1287. if (!previous && options.leading === false) {
  1288. previous = now;
  1289. }
  1290. const remaining = wait - (now - previous);
  1291. const context = this;
  1292. if (remaining <= 0 || remaining > wait) {
  1293. if (timeout) {
  1294. clearTimeout(timeout);
  1295. timeout = null;
  1296. }
  1297. previous = now;
  1298. func.apply(context, args);
  1299. }
  1300. else if (!timeout && options.trailing !== false) {
  1301. timeout = setTimeout(() => {
  1302. previous = options.leading === false ? 0 : Date.now();
  1303. timeout = null;
  1304. func.apply(context, args);
  1305. }, remaining);
  1306. }
  1307. };
  1308. }
  1309. function hookSetter(target, key, d, isRevoked, win = window) {
  1310. const original = win.Object.getOwnPropertyDescriptor(target, key);
  1311. win.Object.defineProperty(target, key, isRevoked
  1312. ? d
  1313. : {
  1314. set(value) {
  1315. setTimeout(() => {
  1316. d.set.call(this, value);
  1317. }, 0);
  1318. if (original && original.set) {
  1319. original.set.call(this, value);
  1320. }
  1321. },
  1322. });
  1323. return () => hookSetter(target, key, original || {}, true);
  1324. }
  1325. function patch(source, name, replacement) {
  1326. try {
  1327. if (!(name in source)) {
  1328. return () => {
  1329. };
  1330. }
  1331. const original = source[name];
  1332. const wrapped = replacement(original);
  1333. if (typeof wrapped === 'function') {
  1334. wrapped.prototype = wrapped.prototype || {};
  1335. Object.defineProperties(wrapped, {
  1336. __rrweb_original__: {
  1337. enumerable: false,
  1338. value: original,
  1339. },
  1340. });
  1341. }
  1342. source[name] = wrapped;
  1343. return () => {
  1344. source[name] = original;
  1345. };
  1346. }
  1347. catch (e2) {
  1348. return () => {
  1349. };
  1350. }
  1351. }
  1352. let nowTimestamp = Date.now;
  1353. if (!(/[1-9][0-9]{12}/.test(Date.now().toString()))) {
  1354. nowTimestamp = () => new Date().getTime();
  1355. }
  1356. function getWindowScroll(win) {
  1357. const doc = win.document;
  1358. return {
  1359. left: doc.scrollingElement
  1360. ? doc.scrollingElement.scrollLeft
  1361. : win.pageXOffset !== undefined
  1362. ? win.pageXOffset
  1363. : _optionalChain$4([doc, 'optionalAccess', _ => _.documentElement, 'access', _2 => _2.scrollLeft]) ||
  1364. _optionalChain$4([doc, 'optionalAccess', _3 => _3.body, 'optionalAccess', _4 => _4.parentElement, 'optionalAccess', _5 => _5.scrollLeft]) ||
  1365. _optionalChain$4([doc, 'optionalAccess', _6 => _6.body, 'optionalAccess', _7 => _7.scrollLeft]) ||
  1366. 0,
  1367. top: doc.scrollingElement
  1368. ? doc.scrollingElement.scrollTop
  1369. : win.pageYOffset !== undefined
  1370. ? win.pageYOffset
  1371. : _optionalChain$4([doc, 'optionalAccess', _8 => _8.documentElement, 'access', _9 => _9.scrollTop]) ||
  1372. _optionalChain$4([doc, 'optionalAccess', _10 => _10.body, 'optionalAccess', _11 => _11.parentElement, 'optionalAccess', _12 => _12.scrollTop]) ||
  1373. _optionalChain$4([doc, 'optionalAccess', _13 => _13.body, 'optionalAccess', _14 => _14.scrollTop]) ||
  1374. 0,
  1375. };
  1376. }
  1377. function getWindowHeight() {
  1378. return (window.innerHeight ||
  1379. (document.documentElement && document.documentElement.clientHeight) ||
  1380. (document.body && document.body.clientHeight));
  1381. }
  1382. function getWindowWidth() {
  1383. return (window.innerWidth ||
  1384. (document.documentElement && document.documentElement.clientWidth) ||
  1385. (document.body && document.body.clientWidth));
  1386. }
  1387. function isBlocked(node, blockClass, blockSelector, unblockSelector, checkAncestors) {
  1388. if (!node) {
  1389. return false;
  1390. }
  1391. const el = node.nodeType === node.ELEMENT_NODE
  1392. ? node
  1393. : node.parentElement;
  1394. if (!el)
  1395. return false;
  1396. const blockedPredicate = createMatchPredicate(blockClass, blockSelector);
  1397. if (!checkAncestors) {
  1398. const isUnblocked = unblockSelector && el.matches(unblockSelector);
  1399. return blockedPredicate(el) && !isUnblocked;
  1400. }
  1401. const blockDistance = distanceToMatch(el, blockedPredicate);
  1402. let unblockDistance = -1;
  1403. if (blockDistance < 0) {
  1404. return false;
  1405. }
  1406. if (unblockSelector) {
  1407. unblockDistance = distanceToMatch(el, createMatchPredicate(null, unblockSelector));
  1408. }
  1409. if (blockDistance > -1 && unblockDistance < 0) {
  1410. return true;
  1411. }
  1412. return blockDistance < unblockDistance;
  1413. }
  1414. function isSerialized(n, mirror) {
  1415. return mirror.getId(n) !== -1;
  1416. }
  1417. function isIgnored(n, mirror) {
  1418. return mirror.getId(n) === IGNORED_NODE;
  1419. }
  1420. function isAncestorRemoved(target, mirror) {
  1421. if (isShadowRoot(target)) {
  1422. return false;
  1423. }
  1424. const id = mirror.getId(target);
  1425. if (!mirror.has(id)) {
  1426. return true;
  1427. }
  1428. if (target.parentNode &&
  1429. target.parentNode.nodeType === target.DOCUMENT_NODE) {
  1430. return false;
  1431. }
  1432. if (!target.parentNode) {
  1433. return true;
  1434. }
  1435. return isAncestorRemoved(target.parentNode, mirror);
  1436. }
  1437. function legacy_isTouchEvent(event) {
  1438. return Boolean(event.changedTouches);
  1439. }
  1440. function polyfill(win = window) {
  1441. if ('NodeList' in win && !win.NodeList.prototype.forEach) {
  1442. win.NodeList.prototype.forEach = Array.prototype
  1443. .forEach;
  1444. }
  1445. if ('DOMTokenList' in win && !win.DOMTokenList.prototype.forEach) {
  1446. win.DOMTokenList.prototype.forEach = Array.prototype
  1447. .forEach;
  1448. }
  1449. if (!Node.prototype.contains) {
  1450. Node.prototype.contains = (...args) => {
  1451. let node = args[0];
  1452. if (!(0 in args)) {
  1453. throw new TypeError('1 argument is required');
  1454. }
  1455. do {
  1456. if (this === node) {
  1457. return true;
  1458. }
  1459. } while ((node = node && node.parentNode));
  1460. return false;
  1461. };
  1462. }
  1463. }
  1464. function isSerializedIframe(n, mirror) {
  1465. return Boolean(n.nodeName === 'IFRAME' && mirror.getMeta(n));
  1466. }
  1467. function isSerializedStylesheet(n, mirror) {
  1468. return Boolean(n.nodeName === 'LINK' &&
  1469. n.nodeType === n.ELEMENT_NODE &&
  1470. n.getAttribute &&
  1471. n.getAttribute('rel') === 'stylesheet' &&
  1472. mirror.getMeta(n));
  1473. }
  1474. function hasShadowRoot(n) {
  1475. return Boolean(_optionalChain$4([n, 'optionalAccess', _18 => _18.shadowRoot]));
  1476. }
  1477. class StyleSheetMirror {
  1478. constructor() {
  1479. this.id = 1;
  1480. this.styleIDMap = new WeakMap();
  1481. this.idStyleMap = new Map();
  1482. }
  1483. getId(stylesheet) {
  1484. return _nullishCoalesce(this.styleIDMap.get(stylesheet), () => ( -1));
  1485. }
  1486. has(stylesheet) {
  1487. return this.styleIDMap.has(stylesheet);
  1488. }
  1489. add(stylesheet, id) {
  1490. if (this.has(stylesheet))
  1491. return this.getId(stylesheet);
  1492. let newId;
  1493. if (id === undefined) {
  1494. newId = this.id++;
  1495. }
  1496. else
  1497. newId = id;
  1498. this.styleIDMap.set(stylesheet, newId);
  1499. this.idStyleMap.set(newId, stylesheet);
  1500. return newId;
  1501. }
  1502. getStyle(id) {
  1503. return this.idStyleMap.get(id) || null;
  1504. }
  1505. reset() {
  1506. this.styleIDMap = new WeakMap();
  1507. this.idStyleMap = new Map();
  1508. this.id = 1;
  1509. }
  1510. generateId() {
  1511. return this.id++;
  1512. }
  1513. }
  1514. function getShadowHost(n) {
  1515. let shadowHost = null;
  1516. if (_optionalChain$4([n, 'access', _19 => _19.getRootNode, 'optionalCall', _20 => _20(), 'optionalAccess', _21 => _21.nodeType]) === Node.DOCUMENT_FRAGMENT_NODE &&
  1517. n.getRootNode().host)
  1518. shadowHost = n.getRootNode().host;
  1519. return shadowHost;
  1520. }
  1521. function getRootShadowHost(n) {
  1522. let rootShadowHost = n;
  1523. let shadowHost;
  1524. while ((shadowHost = getShadowHost(rootShadowHost)))
  1525. rootShadowHost = shadowHost;
  1526. return rootShadowHost;
  1527. }
  1528. function shadowHostInDom(n) {
  1529. const doc = n.ownerDocument;
  1530. if (!doc)
  1531. return false;
  1532. const shadowHost = getRootShadowHost(n);
  1533. return doc.contains(shadowHost);
  1534. }
  1535. function inDom(n) {
  1536. const doc = n.ownerDocument;
  1537. if (!doc)
  1538. return false;
  1539. return doc.contains(n) || shadowHostInDom(n);
  1540. }
  1541. let cachedRequestAnimationFrameImplementation;
  1542. function getRequestAnimationFrameImplementation() {
  1543. if (cachedRequestAnimationFrameImplementation) {
  1544. return cachedRequestAnimationFrameImplementation;
  1545. }
  1546. const document = window.document;
  1547. let requestAnimationFrameImplementation = window.requestAnimationFrame;
  1548. if (document && typeof document.createElement === 'function') {
  1549. try {
  1550. const sandbox = document.createElement('iframe');
  1551. sandbox.hidden = true;
  1552. document.head.appendChild(sandbox);
  1553. const contentWindow = sandbox.contentWindow;
  1554. if (contentWindow && contentWindow.requestAnimationFrame) {
  1555. requestAnimationFrameImplementation =
  1556. contentWindow.requestAnimationFrame;
  1557. }
  1558. document.head.removeChild(sandbox);
  1559. }
  1560. catch (e) {
  1561. }
  1562. }
  1563. return (cachedRequestAnimationFrameImplementation =
  1564. requestAnimationFrameImplementation.bind(window));
  1565. }
  1566. function onRequestAnimationFrame(...rest) {
  1567. return getRequestAnimationFrameImplementation()(...rest);
  1568. }
  1569. var EventType = /* @__PURE__ */ ((EventType2) => {
  1570. EventType2[EventType2["DomContentLoaded"] = 0] = "DomContentLoaded";
  1571. EventType2[EventType2["Load"] = 1] = "Load";
  1572. EventType2[EventType2["FullSnapshot"] = 2] = "FullSnapshot";
  1573. EventType2[EventType2["IncrementalSnapshot"] = 3] = "IncrementalSnapshot";
  1574. EventType2[EventType2["Meta"] = 4] = "Meta";
  1575. EventType2[EventType2["Custom"] = 5] = "Custom";
  1576. EventType2[EventType2["Plugin"] = 6] = "Plugin";
  1577. return EventType2;
  1578. })(EventType || {});
  1579. var IncrementalSource = /* @__PURE__ */ ((IncrementalSource2) => {
  1580. IncrementalSource2[IncrementalSource2["Mutation"] = 0] = "Mutation";
  1581. IncrementalSource2[IncrementalSource2["MouseMove"] = 1] = "MouseMove";
  1582. IncrementalSource2[IncrementalSource2["MouseInteraction"] = 2] = "MouseInteraction";
  1583. IncrementalSource2[IncrementalSource2["Scroll"] = 3] = "Scroll";
  1584. IncrementalSource2[IncrementalSource2["ViewportResize"] = 4] = "ViewportResize";
  1585. IncrementalSource2[IncrementalSource2["Input"] = 5] = "Input";
  1586. IncrementalSource2[IncrementalSource2["TouchMove"] = 6] = "TouchMove";
  1587. IncrementalSource2[IncrementalSource2["MediaInteraction"] = 7] = "MediaInteraction";
  1588. IncrementalSource2[IncrementalSource2["StyleSheetRule"] = 8] = "StyleSheetRule";
  1589. IncrementalSource2[IncrementalSource2["CanvasMutation"] = 9] = "CanvasMutation";
  1590. IncrementalSource2[IncrementalSource2["Font"] = 10] = "Font";
  1591. IncrementalSource2[IncrementalSource2["Log"] = 11] = "Log";
  1592. IncrementalSource2[IncrementalSource2["Drag"] = 12] = "Drag";
  1593. IncrementalSource2[IncrementalSource2["StyleDeclaration"] = 13] = "StyleDeclaration";
  1594. IncrementalSource2[IncrementalSource2["Selection"] = 14] = "Selection";
  1595. IncrementalSource2[IncrementalSource2["AdoptedStyleSheet"] = 15] = "AdoptedStyleSheet";
  1596. IncrementalSource2[IncrementalSource2["CustomElement"] = 16] = "CustomElement";
  1597. return IncrementalSource2;
  1598. })(IncrementalSource || {});
  1599. var MouseInteractions = /* @__PURE__ */ ((MouseInteractions2) => {
  1600. MouseInteractions2[MouseInteractions2["MouseUp"] = 0] = "MouseUp";
  1601. MouseInteractions2[MouseInteractions2["MouseDown"] = 1] = "MouseDown";
  1602. MouseInteractions2[MouseInteractions2["Click"] = 2] = "Click";
  1603. MouseInteractions2[MouseInteractions2["ContextMenu"] = 3] = "ContextMenu";
  1604. MouseInteractions2[MouseInteractions2["DblClick"] = 4] = "DblClick";
  1605. MouseInteractions2[MouseInteractions2["Focus"] = 5] = "Focus";
  1606. MouseInteractions2[MouseInteractions2["Blur"] = 6] = "Blur";
  1607. MouseInteractions2[MouseInteractions2["TouchStart"] = 7] = "TouchStart";
  1608. MouseInteractions2[MouseInteractions2["TouchMove_Departed"] = 8] = "TouchMove_Departed";
  1609. MouseInteractions2[MouseInteractions2["TouchEnd"] = 9] = "TouchEnd";
  1610. MouseInteractions2[MouseInteractions2["TouchCancel"] = 10] = "TouchCancel";
  1611. return MouseInteractions2;
  1612. })(MouseInteractions || {});
  1613. var PointerTypes = /* @__PURE__ */ ((PointerTypes2) => {
  1614. PointerTypes2[PointerTypes2["Mouse"] = 0] = "Mouse";
  1615. PointerTypes2[PointerTypes2["Pen"] = 1] = "Pen";
  1616. PointerTypes2[PointerTypes2["Touch"] = 2] = "Touch";
  1617. return PointerTypes2;
  1618. })(PointerTypes || {});
  1619. function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
  1620. function isNodeInLinkedList(n) {
  1621. return '__ln' in n;
  1622. }
  1623. class DoubleLinkedList {
  1624. constructor() {
  1625. this.length = 0;
  1626. this.head = null;
  1627. this.tail = null;
  1628. }
  1629. get(position) {
  1630. if (position >= this.length) {
  1631. throw new Error('Position outside of list range');
  1632. }
  1633. let current = this.head;
  1634. for (let index = 0; index < position; index++) {
  1635. current = _optionalChain$3([current, 'optionalAccess', _ => _.next]) || null;
  1636. }
  1637. return current;
  1638. }
  1639. addNode(n) {
  1640. const node = {
  1641. value: n,
  1642. previous: null,
  1643. next: null,
  1644. };
  1645. n.__ln = node;
  1646. if (n.previousSibling && isNodeInLinkedList(n.previousSibling)) {
  1647. const current = n.previousSibling.__ln.next;
  1648. node.next = current;
  1649. node.previous = n.previousSibling.__ln;
  1650. n.previousSibling.__ln.next = node;
  1651. if (current) {
  1652. current.previous = node;
  1653. }
  1654. }
  1655. else if (n.nextSibling &&
  1656. isNodeInLinkedList(n.nextSibling) &&
  1657. n.nextSibling.__ln.previous) {
  1658. const current = n.nextSibling.__ln.previous;
  1659. node.previous = current;
  1660. node.next = n.nextSibling.__ln;
  1661. n.nextSibling.__ln.previous = node;
  1662. if (current) {
  1663. current.next = node;
  1664. }
  1665. }
  1666. else {
  1667. if (this.head) {
  1668. this.head.previous = node;
  1669. }
  1670. node.next = this.head;
  1671. this.head = node;
  1672. }
  1673. if (node.next === null) {
  1674. this.tail = node;
  1675. }
  1676. this.length++;
  1677. }
  1678. removeNode(n) {
  1679. const current = n.__ln;
  1680. if (!this.head) {
  1681. return;
  1682. }
  1683. if (!current.previous) {
  1684. this.head = current.next;
  1685. if (this.head) {
  1686. this.head.previous = null;
  1687. }
  1688. else {
  1689. this.tail = null;
  1690. }
  1691. }
  1692. else {
  1693. current.previous.next = current.next;
  1694. if (current.next) {
  1695. current.next.previous = current.previous;
  1696. }
  1697. else {
  1698. this.tail = current.previous;
  1699. }
  1700. }
  1701. if (n.__ln) {
  1702. delete n.__ln;
  1703. }
  1704. this.length--;
  1705. }
  1706. }
  1707. const moveKey = (id, parentId) => `${id}@${parentId}`;
  1708. class MutationBuffer {
  1709. constructor() {
  1710. this.frozen = false;
  1711. this.locked = false;
  1712. this.texts = [];
  1713. this.attributes = [];
  1714. this.removes = [];
  1715. this.mapRemoves = [];
  1716. this.movedMap = {};
  1717. this.addedSet = new Set();
  1718. this.movedSet = new Set();
  1719. this.droppedSet = new Set();
  1720. this.processMutations = (mutations) => {
  1721. mutations.forEach(this.processMutation);
  1722. this.emit();
  1723. };
  1724. this.emit = () => {
  1725. if (this.frozen || this.locked) {
  1726. return;
  1727. }
  1728. const adds = [];
  1729. const addedIds = new Set();
  1730. const addList = new DoubleLinkedList();
  1731. const getNextId = (n) => {
  1732. let ns = n;
  1733. let nextId = IGNORED_NODE;
  1734. while (nextId === IGNORED_NODE) {
  1735. ns = ns && ns.nextSibling;
  1736. nextId = ns && this.mirror.getId(ns);
  1737. }
  1738. return nextId;
  1739. };
  1740. const pushAdd = (n) => {
  1741. if (!n.parentNode || !inDom(n)) {
  1742. return;
  1743. }
  1744. const parentId = isShadowRoot(n.parentNode)
  1745. ? this.mirror.getId(getShadowHost(n))
  1746. : this.mirror.getId(n.parentNode);
  1747. const nextId = getNextId(n);
  1748. if (parentId === -1 || nextId === -1) {
  1749. return addList.addNode(n);
  1750. }
  1751. const sn = serializeNodeWithId(n, {
  1752. doc: this.doc,
  1753. mirror: this.mirror,
  1754. blockClass: this.blockClass,
  1755. blockSelector: this.blockSelector,
  1756. maskAllText: this.maskAllText,
  1757. unblockSelector: this.unblockSelector,
  1758. maskTextClass: this.maskTextClass,
  1759. unmaskTextClass: this.unmaskTextClass,
  1760. maskTextSelector: this.maskTextSelector,
  1761. unmaskTextSelector: this.unmaskTextSelector,
  1762. skipChild: true,
  1763. newlyAddedElement: true,
  1764. inlineStylesheet: this.inlineStylesheet,
  1765. maskInputOptions: this.maskInputOptions,
  1766. maskAttributeFn: this.maskAttributeFn,
  1767. maskTextFn: this.maskTextFn,
  1768. maskInputFn: this.maskInputFn,
  1769. slimDOMOptions: this.slimDOMOptions,
  1770. dataURLOptions: this.dataURLOptions,
  1771. recordCanvas: this.recordCanvas,
  1772. inlineImages: this.inlineImages,
  1773. onSerialize: (currentN) => {
  1774. if (isSerializedIframe(currentN, this.mirror)) {
  1775. this.iframeManager.addIframe(currentN);
  1776. }
  1777. if (isSerializedStylesheet(currentN, this.mirror)) {
  1778. this.stylesheetManager.trackLinkElement(currentN);
  1779. }
  1780. if (hasShadowRoot(n)) {
  1781. this.shadowDomManager.addShadowRoot(n.shadowRoot, this.doc);
  1782. }
  1783. },
  1784. onIframeLoad: (iframe, childSn) => {
  1785. this.iframeManager.attachIframe(iframe, childSn);
  1786. this.shadowDomManager.observeAttachShadow(iframe);
  1787. },
  1788. onStylesheetLoad: (link, childSn) => {
  1789. this.stylesheetManager.attachLinkElement(link, childSn);
  1790. },
  1791. });
  1792. if (sn) {
  1793. adds.push({
  1794. parentId,
  1795. nextId,
  1796. node: sn,
  1797. });
  1798. addedIds.add(sn.id);
  1799. }
  1800. };
  1801. while (this.mapRemoves.length) {
  1802. this.mirror.removeNodeFromMap(this.mapRemoves.shift());
  1803. }
  1804. for (const n of this.movedSet) {
  1805. if (isParentRemoved(this.removes, n, this.mirror) &&
  1806. !this.movedSet.has(n.parentNode)) {
  1807. continue;
  1808. }
  1809. pushAdd(n);
  1810. }
  1811. for (const n of this.addedSet) {
  1812. if (!isAncestorInSet(this.droppedSet, n) &&
  1813. !isParentRemoved(this.removes, n, this.mirror)) {
  1814. pushAdd(n);
  1815. }
  1816. else if (isAncestorInSet(this.movedSet, n)) {
  1817. pushAdd(n);
  1818. }
  1819. else {
  1820. this.droppedSet.add(n);
  1821. }
  1822. }
  1823. let candidate = null;
  1824. while (addList.length) {
  1825. let node = null;
  1826. if (candidate) {
  1827. const parentId = this.mirror.getId(candidate.value.parentNode);
  1828. const nextId = getNextId(candidate.value);
  1829. if (parentId !== -1 && nextId !== -1) {
  1830. node = candidate;
  1831. }
  1832. }
  1833. if (!node) {
  1834. let tailNode = addList.tail;
  1835. while (tailNode) {
  1836. const _node = tailNode;
  1837. tailNode = tailNode.previous;
  1838. if (_node) {
  1839. const parentId = this.mirror.getId(_node.value.parentNode);
  1840. const nextId = getNextId(_node.value);
  1841. if (nextId === -1)
  1842. continue;
  1843. else if (parentId !== -1) {
  1844. node = _node;
  1845. break;
  1846. }
  1847. else {
  1848. const unhandledNode = _node.value;
  1849. if (unhandledNode.parentNode &&
  1850. unhandledNode.parentNode.nodeType ===
  1851. Node.DOCUMENT_FRAGMENT_NODE) {
  1852. const shadowHost = unhandledNode.parentNode
  1853. .host;
  1854. const parentId = this.mirror.getId(shadowHost);
  1855. if (parentId !== -1) {
  1856. node = _node;
  1857. break;
  1858. }
  1859. }
  1860. }
  1861. }
  1862. }
  1863. }
  1864. if (!node) {
  1865. while (addList.head) {
  1866. addList.removeNode(addList.head.value);
  1867. }
  1868. break;
  1869. }
  1870. candidate = node.previous;
  1871. addList.removeNode(node.value);
  1872. pushAdd(node.value);
  1873. }
  1874. const payload = {
  1875. texts: this.texts
  1876. .map((text) => ({
  1877. id: this.mirror.getId(text.node),
  1878. value: text.value,
  1879. }))
  1880. .filter((text) => !addedIds.has(text.id))
  1881. .filter((text) => this.mirror.has(text.id)),
  1882. attributes: this.attributes
  1883. .map((attribute) => {
  1884. const { attributes } = attribute;
  1885. if (typeof attributes.style === 'string') {
  1886. const diffAsStr = JSON.stringify(attribute.styleDiff);
  1887. const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
  1888. if (diffAsStr.length < attributes.style.length) {
  1889. if ((diffAsStr + unchangedAsStr).split('var(').length ===
  1890. attributes.style.split('var(').length) {
  1891. attributes.style = attribute.styleDiff;
  1892. }
  1893. }
  1894. }
  1895. return {
  1896. id: this.mirror.getId(attribute.node),
  1897. attributes: attributes,
  1898. };
  1899. })
  1900. .filter((attribute) => !addedIds.has(attribute.id))
  1901. .filter((attribute) => this.mirror.has(attribute.id)),
  1902. removes: this.removes,
  1903. adds,
  1904. };
  1905. if (!payload.texts.length &&
  1906. !payload.attributes.length &&
  1907. !payload.removes.length &&
  1908. !payload.adds.length) {
  1909. return;
  1910. }
  1911. this.texts = [];
  1912. this.attributes = [];
  1913. this.removes = [];
  1914. this.addedSet = new Set();
  1915. this.movedSet = new Set();
  1916. this.droppedSet = new Set();
  1917. this.movedMap = {};
  1918. this.mutationCb(payload);
  1919. };
  1920. this.processMutation = (m) => {
  1921. if (isIgnored(m.target, this.mirror)) {
  1922. return;
  1923. }
  1924. let unattachedDoc;
  1925. try {
  1926. unattachedDoc = document.implementation.createHTMLDocument();
  1927. }
  1928. catch (e) {
  1929. unattachedDoc = this.doc;
  1930. }
  1931. switch (m.type) {
  1932. case 'characterData': {
  1933. const value = m.target.textContent;
  1934. if (!isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector, false) &&
  1935. value !== m.oldValue) {
  1936. this.texts.push({
  1937. value: needMaskingText(m.target, this.maskTextClass, this.maskTextSelector, this.unmaskTextClass, this.unmaskTextSelector, this.maskAllText) && value
  1938. ? this.maskTextFn
  1939. ? this.maskTextFn(value)
  1940. : value.replace(/[\S]/g, '*')
  1941. : value,
  1942. node: m.target,
  1943. });
  1944. }
  1945. break;
  1946. }
  1947. case 'attributes': {
  1948. const target = m.target;
  1949. let attributeName = m.attributeName;
  1950. let value = m.target.getAttribute(attributeName);
  1951. if (attributeName === 'value') {
  1952. const type = getInputType(target);
  1953. const tagName = target.tagName;
  1954. value = getInputValue(target, tagName, type);
  1955. const isInputMasked = shouldMaskInput({
  1956. maskInputOptions: this.maskInputOptions,
  1957. tagName,
  1958. type,
  1959. });
  1960. const forceMask = needMaskingText(m.target, this.maskTextClass, this.maskTextSelector, this.unmaskTextClass, this.unmaskTextSelector, isInputMasked);
  1961. value = maskInputValue({
  1962. isMasked: forceMask,
  1963. element: target,
  1964. value,
  1965. maskInputFn: this.maskInputFn,
  1966. });
  1967. }
  1968. if (isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector, false) ||
  1969. value === m.oldValue) {
  1970. return;
  1971. }
  1972. let item = this.attributes.find((a) => a.node === m.target);
  1973. if (target.tagName === 'IFRAME' &&
  1974. attributeName === 'src' &&
  1975. !this.keepIframeSrcFn(value)) {
  1976. if (!target.contentDocument) {
  1977. attributeName = 'rr_src';
  1978. }
  1979. else {
  1980. return;
  1981. }
  1982. }
  1983. if (!item) {
  1984. item = {
  1985. node: m.target,
  1986. attributes: {},
  1987. styleDiff: {},
  1988. _unchangedStyles: {},
  1989. };
  1990. this.attributes.push(item);
  1991. }
  1992. if (attributeName === 'type' &&
  1993. target.tagName === 'INPUT' &&
  1994. (m.oldValue || '').toLowerCase() === 'password') {
  1995. target.setAttribute('data-rr-is-password', 'true');
  1996. }
  1997. if (!ignoreAttribute(target.tagName, attributeName)) {
  1998. item.attributes[attributeName] = transformAttribute(this.doc, toLowerCase(target.tagName), toLowerCase(attributeName), value, target, this.maskAttributeFn);
  1999. if (attributeName === 'style') {
  2000. const old = unattachedDoc.createElement('span');
  2001. if (m.oldValue) {
  2002. old.setAttribute('style', m.oldValue);
  2003. }
  2004. for (const pname of Array.from(target.style)) {
  2005. const newValue = target.style.getPropertyValue(pname);
  2006. const newPriority = target.style.getPropertyPriority(pname);
  2007. if (newValue !== old.style.getPropertyValue(pname) ||
  2008. newPriority !== old.style.getPropertyPriority(pname)) {
  2009. if (newPriority === '') {
  2010. item.styleDiff[pname] = newValue;
  2011. }
  2012. else {
  2013. item.styleDiff[pname] = [newValue, newPriority];
  2014. }
  2015. }
  2016. else {
  2017. item._unchangedStyles[pname] = [newValue, newPriority];
  2018. }
  2019. }
  2020. for (const pname of Array.from(old.style)) {
  2021. if (target.style.getPropertyValue(pname) === '') {
  2022. item.styleDiff[pname] = false;
  2023. }
  2024. }
  2025. }
  2026. }
  2027. break;
  2028. }
  2029. case 'childList': {
  2030. if (isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector, true)) {
  2031. return;
  2032. }
  2033. m.addedNodes.forEach((n) => this.genAdds(n, m.target));
  2034. m.removedNodes.forEach((n) => {
  2035. const nodeId = this.mirror.getId(n);
  2036. const parentId = isShadowRoot(m.target)
  2037. ? this.mirror.getId(m.target.host)
  2038. : this.mirror.getId(m.target);
  2039. if (isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector, false) ||
  2040. isIgnored(n, this.mirror) ||
  2041. !isSerialized(n, this.mirror)) {
  2042. return;
  2043. }
  2044. if (this.addedSet.has(n)) {
  2045. deepDelete(this.addedSet, n);
  2046. this.droppedSet.add(n);
  2047. }
  2048. else if (this.addedSet.has(m.target) && nodeId === -1) ;
  2049. else if (isAncestorRemoved(m.target, this.mirror)) ;
  2050. else if (this.movedSet.has(n) &&
  2051. this.movedMap[moveKey(nodeId, parentId)]) {
  2052. deepDelete(this.movedSet, n);
  2053. }
  2054. else {
  2055. this.removes.push({
  2056. parentId,
  2057. id: nodeId,
  2058. isShadow: isShadowRoot(m.target) && isNativeShadowDom(m.target)
  2059. ? true
  2060. : undefined,
  2061. });
  2062. }
  2063. this.mapRemoves.push(n);
  2064. });
  2065. break;
  2066. }
  2067. }
  2068. };
  2069. this.genAdds = (n, target) => {
  2070. if (this.processedNodeManager.inOtherBuffer(n, this))
  2071. return;
  2072. if (this.addedSet.has(n) || this.movedSet.has(n))
  2073. return;
  2074. if (this.mirror.hasNode(n)) {
  2075. if (isIgnored(n, this.mirror)) {
  2076. return;
  2077. }
  2078. this.movedSet.add(n);
  2079. let targetId = null;
  2080. if (target && this.mirror.hasNode(target)) {
  2081. targetId = this.mirror.getId(target);
  2082. }
  2083. if (targetId && targetId !== -1) {
  2084. this.movedMap[moveKey(this.mirror.getId(n), targetId)] = true;
  2085. }
  2086. }
  2087. else {
  2088. this.addedSet.add(n);
  2089. this.droppedSet.delete(n);
  2090. }
  2091. if (!isBlocked(n, this.blockClass, this.blockSelector, this.unblockSelector, false)) {
  2092. n.childNodes.forEach((childN) => this.genAdds(childN));
  2093. if (hasShadowRoot(n)) {
  2094. n.shadowRoot.childNodes.forEach((childN) => {
  2095. this.processedNodeManager.add(childN, this);
  2096. this.genAdds(childN, n);
  2097. });
  2098. }
  2099. }
  2100. };
  2101. }
  2102. init(options) {
  2103. [
  2104. 'mutationCb',
  2105. 'blockClass',
  2106. 'blockSelector',
  2107. 'unblockSelector',
  2108. 'maskAllText',
  2109. 'maskTextClass',
  2110. 'unmaskTextClass',
  2111. 'maskTextSelector',
  2112. 'unmaskTextSelector',
  2113. 'inlineStylesheet',
  2114. 'maskInputOptions',
  2115. 'maskAttributeFn',
  2116. 'maskTextFn',
  2117. 'maskInputFn',
  2118. 'keepIframeSrcFn',
  2119. 'recordCanvas',
  2120. 'inlineImages',
  2121. 'slimDOMOptions',
  2122. 'dataURLOptions',
  2123. 'doc',
  2124. 'mirror',
  2125. 'iframeManager',
  2126. 'stylesheetManager',
  2127. 'shadowDomManager',
  2128. 'canvasManager',
  2129. 'processedNodeManager',
  2130. ].forEach((key) => {
  2131. this[key] = options[key];
  2132. });
  2133. }
  2134. freeze() {
  2135. this.frozen = true;
  2136. this.canvasManager.freeze();
  2137. }
  2138. unfreeze() {
  2139. this.frozen = false;
  2140. this.canvasManager.unfreeze();
  2141. this.emit();
  2142. }
  2143. isFrozen() {
  2144. return this.frozen;
  2145. }
  2146. lock() {
  2147. this.locked = true;
  2148. this.canvasManager.lock();
  2149. }
  2150. unlock() {
  2151. this.locked = false;
  2152. this.canvasManager.unlock();
  2153. this.emit();
  2154. }
  2155. reset() {
  2156. this.shadowDomManager.reset();
  2157. this.canvasManager.reset();
  2158. }
  2159. }
  2160. function deepDelete(addsSet, n) {
  2161. addsSet.delete(n);
  2162. n.childNodes.forEach((childN) => deepDelete(addsSet, childN));
  2163. }
  2164. function isParentRemoved(removes, n, mirror) {
  2165. if (removes.length === 0)
  2166. return false;
  2167. return _isParentRemoved(removes, n, mirror);
  2168. }
  2169. function _isParentRemoved(removes, n, mirror) {
  2170. const { parentNode } = n;
  2171. if (!parentNode) {
  2172. return false;
  2173. }
  2174. const parentId = mirror.getId(parentNode);
  2175. if (removes.some((r) => r.id === parentId)) {
  2176. return true;
  2177. }
  2178. return _isParentRemoved(removes, parentNode, mirror);
  2179. }
  2180. function isAncestorInSet(set, n) {
  2181. if (set.size === 0)
  2182. return false;
  2183. return _isAncestorInSet(set, n);
  2184. }
  2185. function _isAncestorInSet(set, n) {
  2186. const { parentNode } = n;
  2187. if (!parentNode) {
  2188. return false;
  2189. }
  2190. if (set.has(parentNode)) {
  2191. return true;
  2192. }
  2193. return _isAncestorInSet(set, parentNode);
  2194. }
  2195. let errorHandler;
  2196. function registerErrorHandler(handler) {
  2197. errorHandler = handler;
  2198. }
  2199. function unregisterErrorHandler() {
  2200. errorHandler = undefined;
  2201. }
  2202. const callbackWrapper = (cb) => {
  2203. if (!errorHandler) {
  2204. return cb;
  2205. }
  2206. const rrwebWrapped = ((...rest) => {
  2207. try {
  2208. return cb(...rest);
  2209. }
  2210. catch (error) {
  2211. if (errorHandler && errorHandler(error) === true) {
  2212. return () => {
  2213. };
  2214. }
  2215. throw error;
  2216. }
  2217. });
  2218. return rrwebWrapped;
  2219. };
  2220. function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
  2221. const mutationBuffers = [];
  2222. function getEventTarget(event) {
  2223. try {
  2224. if ('composedPath' in event) {
  2225. const path = event.composedPath();
  2226. if (path.length) {
  2227. return path[0];
  2228. }
  2229. }
  2230. else if ('path' in event && event.path.length) {
  2231. return event.path[0];
  2232. }
  2233. }
  2234. catch (e2) {
  2235. }
  2236. return event && event.target;
  2237. }
  2238. function initMutationObserver(options, rootEl) {
  2239. const mutationBuffer = new MutationBuffer();
  2240. mutationBuffers.push(mutationBuffer);
  2241. mutationBuffer.init(options);
  2242. let mutationObserverCtor = window.MutationObserver ||
  2243. window.__rrMutationObserver;
  2244. const angularZoneSymbol = _optionalChain$2([window, 'optionalAccess', _ => _.Zone, 'optionalAccess', _2 => _2.__symbol__, 'optionalCall', _3 => _3('MutationObserver')]);
  2245. if (angularZoneSymbol &&
  2246. window[angularZoneSymbol]) {
  2247. mutationObserverCtor = window[angularZoneSymbol];
  2248. }
  2249. const observer = new mutationObserverCtor(callbackWrapper((mutations) => {
  2250. if (options.onMutation && options.onMutation(mutations) === false) {
  2251. return;
  2252. }
  2253. mutationBuffer.processMutations.bind(mutationBuffer)(mutations);
  2254. }));
  2255. observer.observe(rootEl, {
  2256. attributes: true,
  2257. attributeOldValue: true,
  2258. characterData: true,
  2259. characterDataOldValue: true,
  2260. childList: true,
  2261. subtree: true,
  2262. });
  2263. return observer;
  2264. }
  2265. function initMoveObserver({ mousemoveCb, sampling, doc, mirror, }) {
  2266. if (sampling.mousemove === false) {
  2267. return () => {
  2268. };
  2269. }
  2270. const threshold = typeof sampling.mousemove === 'number' ? sampling.mousemove : 50;
  2271. const callbackThreshold = typeof sampling.mousemoveCallback === 'number'
  2272. ? sampling.mousemoveCallback
  2273. : 500;
  2274. let positions = [];
  2275. let timeBaseline;
  2276. const wrappedCb = throttle$1(callbackWrapper((source) => {
  2277. const totalOffset = Date.now() - timeBaseline;
  2278. mousemoveCb(positions.map((p) => {
  2279. p.timeOffset -= totalOffset;
  2280. return p;
  2281. }), source);
  2282. positions = [];
  2283. timeBaseline = null;
  2284. }), callbackThreshold);
  2285. const updatePosition = callbackWrapper(throttle$1(callbackWrapper((evt) => {
  2286. const target = getEventTarget(evt);
  2287. const { clientX, clientY } = legacy_isTouchEvent(evt)
  2288. ? evt.changedTouches[0]
  2289. : evt;
  2290. if (!timeBaseline) {
  2291. timeBaseline = nowTimestamp();
  2292. }
  2293. positions.push({
  2294. x: clientX,
  2295. y: clientY,
  2296. id: mirror.getId(target),
  2297. timeOffset: nowTimestamp() - timeBaseline,
  2298. });
  2299. wrappedCb(typeof DragEvent !== 'undefined' && evt instanceof DragEvent
  2300. ? IncrementalSource.Drag
  2301. : evt instanceof MouseEvent
  2302. ? IncrementalSource.MouseMove
  2303. : IncrementalSource.TouchMove);
  2304. }), threshold, {
  2305. trailing: false,
  2306. }));
  2307. const handlers = [
  2308. on('mousemove', updatePosition, doc),
  2309. on('touchmove', updatePosition, doc),
  2310. on('drag', updatePosition, doc),
  2311. ];
  2312. return callbackWrapper(() => {
  2313. handlers.forEach((h) => h());
  2314. });
  2315. }
  2316. function initMouseInteractionObserver({ mouseInteractionCb, doc, mirror, blockClass, blockSelector, unblockSelector, sampling, }) {
  2317. if (sampling.mouseInteraction === false) {
  2318. return () => {
  2319. };
  2320. }
  2321. const disableMap = sampling.mouseInteraction === true ||
  2322. sampling.mouseInteraction === undefined
  2323. ? {}
  2324. : sampling.mouseInteraction;
  2325. const handlers = [];
  2326. let currentPointerType = null;
  2327. const getHandler = (eventKey) => {
  2328. return (event) => {
  2329. const target = getEventTarget(event);
  2330. if (isBlocked(target, blockClass, blockSelector, unblockSelector, true)) {
  2331. return;
  2332. }
  2333. let pointerType = null;
  2334. let thisEventKey = eventKey;
  2335. if ('pointerType' in event) {
  2336. switch (event.pointerType) {
  2337. case 'mouse':
  2338. pointerType = PointerTypes.Mouse;
  2339. break;
  2340. case 'touch':
  2341. pointerType = PointerTypes.Touch;
  2342. break;
  2343. case 'pen':
  2344. pointerType = PointerTypes.Pen;
  2345. break;
  2346. }
  2347. if (pointerType === PointerTypes.Touch) {
  2348. if (MouseInteractions[eventKey] === MouseInteractions.MouseDown) {
  2349. thisEventKey = 'TouchStart';
  2350. }
  2351. else if (MouseInteractions[eventKey] === MouseInteractions.MouseUp) {
  2352. thisEventKey = 'TouchEnd';
  2353. }
  2354. }
  2355. else if (pointerType === PointerTypes.Pen) ;
  2356. }
  2357. else if (legacy_isTouchEvent(event)) {
  2358. pointerType = PointerTypes.Touch;
  2359. }
  2360. if (pointerType !== null) {
  2361. currentPointerType = pointerType;
  2362. if ((thisEventKey.startsWith('Touch') &&
  2363. pointerType === PointerTypes.Touch) ||
  2364. (thisEventKey.startsWith('Mouse') &&
  2365. pointerType === PointerTypes.Mouse)) {
  2366. pointerType = null;
  2367. }
  2368. }
  2369. else if (MouseInteractions[eventKey] === MouseInteractions.Click) {
  2370. pointerType = currentPointerType;
  2371. currentPointerType = null;
  2372. }
  2373. const e = legacy_isTouchEvent(event) ? event.changedTouches[0] : event;
  2374. if (!e) {
  2375. return;
  2376. }
  2377. const id = mirror.getId(target);
  2378. const { clientX, clientY } = e;
  2379. callbackWrapper(mouseInteractionCb)({
  2380. type: MouseInteractions[thisEventKey],
  2381. id,
  2382. x: clientX,
  2383. y: clientY,
  2384. ...(pointerType !== null && { pointerType }),
  2385. });
  2386. };
  2387. };
  2388. Object.keys(MouseInteractions)
  2389. .filter((key) => Number.isNaN(Number(key)) &&
  2390. !key.endsWith('_Departed') &&
  2391. disableMap[key] !== false)
  2392. .forEach((eventKey) => {
  2393. let eventName = toLowerCase(eventKey);
  2394. const handler = getHandler(eventKey);
  2395. if (window.PointerEvent) {
  2396. switch (MouseInteractions[eventKey]) {
  2397. case MouseInteractions.MouseDown:
  2398. case MouseInteractions.MouseUp:
  2399. eventName = eventName.replace('mouse', 'pointer');
  2400. break;
  2401. case MouseInteractions.TouchStart:
  2402. case MouseInteractions.TouchEnd:
  2403. return;
  2404. }
  2405. }
  2406. handlers.push(on(eventName, handler, doc));
  2407. });
  2408. return callbackWrapper(() => {
  2409. handlers.forEach((h) => h());
  2410. });
  2411. }
  2412. function initScrollObserver({ scrollCb, doc, mirror, blockClass, blockSelector, unblockSelector, sampling, }) {
  2413. const updatePosition = callbackWrapper(throttle$1(callbackWrapper((evt) => {
  2414. const target = getEventTarget(evt);
  2415. if (!target ||
  2416. isBlocked(target, blockClass, blockSelector, unblockSelector, true)) {
  2417. return;
  2418. }
  2419. const id = mirror.getId(target);
  2420. if (target === doc && doc.defaultView) {
  2421. const scrollLeftTop = getWindowScroll(doc.defaultView);
  2422. scrollCb({
  2423. id,
  2424. x: scrollLeftTop.left,
  2425. y: scrollLeftTop.top,
  2426. });
  2427. }
  2428. else {
  2429. scrollCb({
  2430. id,
  2431. x: target.scrollLeft,
  2432. y: target.scrollTop,
  2433. });
  2434. }
  2435. }), sampling.scroll || 100));
  2436. return on('scroll', updatePosition, doc);
  2437. }
  2438. function initViewportResizeObserver({ viewportResizeCb }, { win }) {
  2439. let lastH = -1;
  2440. let lastW = -1;
  2441. const updateDimension = callbackWrapper(throttle$1(callbackWrapper(() => {
  2442. const height = getWindowHeight();
  2443. const width = getWindowWidth();
  2444. if (lastH !== height || lastW !== width) {
  2445. viewportResizeCb({
  2446. width: Number(width),
  2447. height: Number(height),
  2448. });
  2449. lastH = height;
  2450. lastW = width;
  2451. }
  2452. }), 200));
  2453. return on('resize', updateDimension, win);
  2454. }
  2455. const INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
  2456. const lastInputValueMap = new WeakMap();
  2457. function initInputObserver({ inputCb, doc, mirror, blockClass, blockSelector, unblockSelector, ignoreClass, ignoreSelector, maskInputOptions, maskInputFn, sampling, userTriggeredOnInput, maskTextClass, unmaskTextClass, maskTextSelector, unmaskTextSelector, }) {
  2458. function eventHandler(event) {
  2459. let target = getEventTarget(event);
  2460. const userTriggered = event.isTrusted;
  2461. const tagName = target && toUpperCase(target.tagName);
  2462. if (tagName === 'OPTION')
  2463. target = target.parentElement;
  2464. if (!target ||
  2465. !tagName ||
  2466. INPUT_TAGS.indexOf(tagName) < 0 ||
  2467. isBlocked(target, blockClass, blockSelector, unblockSelector, true)) {
  2468. return;
  2469. }
  2470. const el = target;
  2471. if (el.classList.contains(ignoreClass) ||
  2472. (ignoreSelector && el.matches(ignoreSelector))) {
  2473. return;
  2474. }
  2475. const type = getInputType(target);
  2476. let text = getInputValue(el, tagName, type);
  2477. let isChecked = false;
  2478. const isInputMasked = shouldMaskInput({
  2479. maskInputOptions,
  2480. tagName,
  2481. type,
  2482. });
  2483. const forceMask = needMaskingText(target, maskTextClass, maskTextSelector, unmaskTextClass, unmaskTextSelector, isInputMasked);
  2484. if (type === 'radio' || type === 'checkbox') {
  2485. isChecked = target.checked;
  2486. }
  2487. text = maskInputValue({
  2488. isMasked: forceMask,
  2489. element: target,
  2490. value: text,
  2491. maskInputFn,
  2492. });
  2493. cbWithDedup(target, userTriggeredOnInput
  2494. ? { text, isChecked, userTriggered }
  2495. : { text, isChecked });
  2496. const name = target.name;
  2497. if (type === 'radio' && name && isChecked) {
  2498. doc
  2499. .querySelectorAll(`input[type="radio"][name="${name}"]`)
  2500. .forEach((el) => {
  2501. if (el !== target) {
  2502. const text = maskInputValue({
  2503. isMasked: forceMask,
  2504. element: el,
  2505. value: getInputValue(el, tagName, type),
  2506. maskInputFn,
  2507. });
  2508. cbWithDedup(el, userTriggeredOnInput
  2509. ? { text, isChecked: !isChecked, userTriggered: false }
  2510. : { text, isChecked: !isChecked });
  2511. }
  2512. });
  2513. }
  2514. }
  2515. function cbWithDedup(target, v) {
  2516. const lastInputValue = lastInputValueMap.get(target);
  2517. if (!lastInputValue ||
  2518. lastInputValue.text !== v.text ||
  2519. lastInputValue.isChecked !== v.isChecked) {
  2520. lastInputValueMap.set(target, v);
  2521. const id = mirror.getId(target);
  2522. callbackWrapper(inputCb)({
  2523. ...v,
  2524. id,
  2525. });
  2526. }
  2527. }
  2528. const events = sampling.input === 'last' ? ['change'] : ['input', 'change'];
  2529. const handlers = events.map((eventName) => on(eventName, callbackWrapper(eventHandler), doc));
  2530. const currentWindow = doc.defaultView;
  2531. if (!currentWindow) {
  2532. return () => {
  2533. handlers.forEach((h) => h());
  2534. };
  2535. }
  2536. const propertyDescriptor = currentWindow.Object.getOwnPropertyDescriptor(currentWindow.HTMLInputElement.prototype, 'value');
  2537. const hookProperties = [
  2538. [currentWindow.HTMLInputElement.prototype, 'value'],
  2539. [currentWindow.HTMLInputElement.prototype, 'checked'],
  2540. [currentWindow.HTMLSelectElement.prototype, 'value'],
  2541. [currentWindow.HTMLTextAreaElement.prototype, 'value'],
  2542. [currentWindow.HTMLSelectElement.prototype, 'selectedIndex'],
  2543. [currentWindow.HTMLOptionElement.prototype, 'selected'],
  2544. ];
  2545. if (propertyDescriptor && propertyDescriptor.set) {
  2546. handlers.push(...hookProperties.map((p) => hookSetter(p[0], p[1], {
  2547. set() {
  2548. callbackWrapper(eventHandler)({
  2549. target: this,
  2550. isTrusted: false,
  2551. });
  2552. },
  2553. }, false, currentWindow)));
  2554. }
  2555. return callbackWrapper(() => {
  2556. handlers.forEach((h) => h());
  2557. });
  2558. }
  2559. function getNestedCSSRulePositions(rule) {
  2560. const positions = [];
  2561. function recurse(childRule, pos) {
  2562. if ((hasNestedCSSRule('CSSGroupingRule') &&
  2563. childRule.parentRule instanceof CSSGroupingRule) ||
  2564. (hasNestedCSSRule('CSSMediaRule') &&
  2565. childRule.parentRule instanceof CSSMediaRule) ||
  2566. (hasNestedCSSRule('CSSSupportsRule') &&
  2567. childRule.parentRule instanceof CSSSupportsRule) ||
  2568. (hasNestedCSSRule('CSSConditionRule') &&
  2569. childRule.parentRule instanceof CSSConditionRule)) {
  2570. const rules = Array.from(childRule.parentRule.cssRules);
  2571. const index = rules.indexOf(childRule);
  2572. pos.unshift(index);
  2573. }
  2574. else if (childRule.parentStyleSheet) {
  2575. const rules = Array.from(childRule.parentStyleSheet.cssRules);
  2576. const index = rules.indexOf(childRule);
  2577. pos.unshift(index);
  2578. }
  2579. return pos;
  2580. }
  2581. return recurse(rule, positions);
  2582. }
  2583. function getIdAndStyleId(sheet, mirror, styleMirror) {
  2584. let id, styleId;
  2585. if (!sheet)
  2586. return {};
  2587. if (sheet.ownerNode)
  2588. id = mirror.getId(sheet.ownerNode);
  2589. else
  2590. styleId = styleMirror.getId(sheet);
  2591. return {
  2592. styleId,
  2593. id,
  2594. };
  2595. }
  2596. function initStyleSheetObserver({ styleSheetRuleCb, mirror, stylesheetManager }, { win }) {
  2597. if (!win.CSSStyleSheet || !win.CSSStyleSheet.prototype) {
  2598. return () => {
  2599. };
  2600. }
  2601. const insertRule = win.CSSStyleSheet.prototype.insertRule;
  2602. win.CSSStyleSheet.prototype.insertRule = new Proxy(insertRule, {
  2603. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2604. const [rule, index] = argumentsList;
  2605. const { id, styleId } = getIdAndStyleId(thisArg, mirror, stylesheetManager.styleMirror);
  2606. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2607. styleSheetRuleCb({
  2608. id,
  2609. styleId,
  2610. adds: [{ rule, index }],
  2611. });
  2612. }
  2613. return target.apply(thisArg, argumentsList);
  2614. }),
  2615. });
  2616. const deleteRule = win.CSSStyleSheet.prototype.deleteRule;
  2617. win.CSSStyleSheet.prototype.deleteRule = new Proxy(deleteRule, {
  2618. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2619. const [index] = argumentsList;
  2620. const { id, styleId } = getIdAndStyleId(thisArg, mirror, stylesheetManager.styleMirror);
  2621. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2622. styleSheetRuleCb({
  2623. id,
  2624. styleId,
  2625. removes: [{ index }],
  2626. });
  2627. }
  2628. return target.apply(thisArg, argumentsList);
  2629. }),
  2630. });
  2631. let replace;
  2632. if (win.CSSStyleSheet.prototype.replace) {
  2633. replace = win.CSSStyleSheet.prototype.replace;
  2634. win.CSSStyleSheet.prototype.replace = new Proxy(replace, {
  2635. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2636. const [text] = argumentsList;
  2637. const { id, styleId } = getIdAndStyleId(thisArg, mirror, stylesheetManager.styleMirror);
  2638. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2639. styleSheetRuleCb({
  2640. id,
  2641. styleId,
  2642. replace: text,
  2643. });
  2644. }
  2645. return target.apply(thisArg, argumentsList);
  2646. }),
  2647. });
  2648. }
  2649. let replaceSync;
  2650. if (win.CSSStyleSheet.prototype.replaceSync) {
  2651. replaceSync = win.CSSStyleSheet.prototype.replaceSync;
  2652. win.CSSStyleSheet.prototype.replaceSync = new Proxy(replaceSync, {
  2653. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2654. const [text] = argumentsList;
  2655. const { id, styleId } = getIdAndStyleId(thisArg, mirror, stylesheetManager.styleMirror);
  2656. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2657. styleSheetRuleCb({
  2658. id,
  2659. styleId,
  2660. replaceSync: text,
  2661. });
  2662. }
  2663. return target.apply(thisArg, argumentsList);
  2664. }),
  2665. });
  2666. }
  2667. const supportedNestedCSSRuleTypes = {};
  2668. if (canMonkeyPatchNestedCSSRule('CSSGroupingRule')) {
  2669. supportedNestedCSSRuleTypes.CSSGroupingRule = win.CSSGroupingRule;
  2670. }
  2671. else {
  2672. if (canMonkeyPatchNestedCSSRule('CSSMediaRule')) {
  2673. supportedNestedCSSRuleTypes.CSSMediaRule = win.CSSMediaRule;
  2674. }
  2675. if (canMonkeyPatchNestedCSSRule('CSSConditionRule')) {
  2676. supportedNestedCSSRuleTypes.CSSConditionRule = win.CSSConditionRule;
  2677. }
  2678. if (canMonkeyPatchNestedCSSRule('CSSSupportsRule')) {
  2679. supportedNestedCSSRuleTypes.CSSSupportsRule = win.CSSSupportsRule;
  2680. }
  2681. }
  2682. const unmodifiedFunctions = {};
  2683. Object.entries(supportedNestedCSSRuleTypes).forEach(([typeKey, type]) => {
  2684. unmodifiedFunctions[typeKey] = {
  2685. insertRule: type.prototype.insertRule,
  2686. deleteRule: type.prototype.deleteRule,
  2687. };
  2688. type.prototype.insertRule = new Proxy(unmodifiedFunctions[typeKey].insertRule, {
  2689. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2690. const [rule, index] = argumentsList;
  2691. const { id, styleId } = getIdAndStyleId(thisArg.parentStyleSheet, mirror, stylesheetManager.styleMirror);
  2692. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2693. styleSheetRuleCb({
  2694. id,
  2695. styleId,
  2696. adds: [
  2697. {
  2698. rule,
  2699. index: [
  2700. ...getNestedCSSRulePositions(thisArg),
  2701. index || 0,
  2702. ],
  2703. },
  2704. ],
  2705. });
  2706. }
  2707. return target.apply(thisArg, argumentsList);
  2708. }),
  2709. });
  2710. type.prototype.deleteRule = new Proxy(unmodifiedFunctions[typeKey].deleteRule, {
  2711. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2712. const [index] = argumentsList;
  2713. const { id, styleId } = getIdAndStyleId(thisArg.parentStyleSheet, mirror, stylesheetManager.styleMirror);
  2714. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2715. styleSheetRuleCb({
  2716. id,
  2717. styleId,
  2718. removes: [
  2719. { index: [...getNestedCSSRulePositions(thisArg), index] },
  2720. ],
  2721. });
  2722. }
  2723. return target.apply(thisArg, argumentsList);
  2724. }),
  2725. });
  2726. });
  2727. return callbackWrapper(() => {
  2728. win.CSSStyleSheet.prototype.insertRule = insertRule;
  2729. win.CSSStyleSheet.prototype.deleteRule = deleteRule;
  2730. replace && (win.CSSStyleSheet.prototype.replace = replace);
  2731. replaceSync && (win.CSSStyleSheet.prototype.replaceSync = replaceSync);
  2732. Object.entries(supportedNestedCSSRuleTypes).forEach(([typeKey, type]) => {
  2733. type.prototype.insertRule = unmodifiedFunctions[typeKey].insertRule;
  2734. type.prototype.deleteRule = unmodifiedFunctions[typeKey].deleteRule;
  2735. });
  2736. });
  2737. }
  2738. function initAdoptedStyleSheetObserver({ mirror, stylesheetManager, }, host) {
  2739. let hostId = null;
  2740. if (host.nodeName === '#document')
  2741. hostId = mirror.getId(host);
  2742. else
  2743. hostId = mirror.getId(host.host);
  2744. const patchTarget = host.nodeName === '#document'
  2745. ? _optionalChain$2([host, 'access', _4 => _4.defaultView, 'optionalAccess', _5 => _5.Document])
  2746. : _optionalChain$2([host, 'access', _6 => _6.ownerDocument, 'optionalAccess', _7 => _7.defaultView, 'optionalAccess', _8 => _8.ShadowRoot]);
  2747. const originalPropertyDescriptor = _optionalChain$2([patchTarget, 'optionalAccess', _9 => _9.prototype])
  2748. ? Object.getOwnPropertyDescriptor(_optionalChain$2([patchTarget, 'optionalAccess', _10 => _10.prototype]), 'adoptedStyleSheets')
  2749. : undefined;
  2750. if (hostId === null ||
  2751. hostId === -1 ||
  2752. !patchTarget ||
  2753. !originalPropertyDescriptor)
  2754. return () => {
  2755. };
  2756. Object.defineProperty(host, 'adoptedStyleSheets', {
  2757. configurable: originalPropertyDescriptor.configurable,
  2758. enumerable: originalPropertyDescriptor.enumerable,
  2759. get() {
  2760. return _optionalChain$2([originalPropertyDescriptor, 'access', _11 => _11.get, 'optionalAccess', _12 => _12.call, 'call', _13 => _13(this)]);
  2761. },
  2762. set(sheets) {
  2763. const result = _optionalChain$2([originalPropertyDescriptor, 'access', _14 => _14.set, 'optionalAccess', _15 => _15.call, 'call', _16 => _16(this, sheets)]);
  2764. if (hostId !== null && hostId !== -1) {
  2765. try {
  2766. stylesheetManager.adoptStyleSheets(sheets, hostId);
  2767. }
  2768. catch (e) {
  2769. }
  2770. }
  2771. return result;
  2772. },
  2773. });
  2774. return callbackWrapper(() => {
  2775. Object.defineProperty(host, 'adoptedStyleSheets', {
  2776. configurable: originalPropertyDescriptor.configurable,
  2777. enumerable: originalPropertyDescriptor.enumerable,
  2778. get: originalPropertyDescriptor.get,
  2779. set: originalPropertyDescriptor.set,
  2780. });
  2781. });
  2782. }
  2783. function initStyleDeclarationObserver({ styleDeclarationCb, mirror, ignoreCSSAttributes, stylesheetManager, }, { win }) {
  2784. const setProperty = win.CSSStyleDeclaration.prototype.setProperty;
  2785. win.CSSStyleDeclaration.prototype.setProperty = new Proxy(setProperty, {
  2786. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2787. const [property, value, priority] = argumentsList;
  2788. if (ignoreCSSAttributes.has(property)) {
  2789. return setProperty.apply(thisArg, [property, value, priority]);
  2790. }
  2791. const { id, styleId } = getIdAndStyleId(_optionalChain$2([thisArg, 'access', _17 => _17.parentRule, 'optionalAccess', _18 => _18.parentStyleSheet]), mirror, stylesheetManager.styleMirror);
  2792. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2793. styleDeclarationCb({
  2794. id,
  2795. styleId,
  2796. set: {
  2797. property,
  2798. value,
  2799. priority,
  2800. },
  2801. index: getNestedCSSRulePositions(thisArg.parentRule),
  2802. });
  2803. }
  2804. return target.apply(thisArg, argumentsList);
  2805. }),
  2806. });
  2807. const removeProperty = win.CSSStyleDeclaration.prototype.removeProperty;
  2808. win.CSSStyleDeclaration.prototype.removeProperty = new Proxy(removeProperty, {
  2809. apply: callbackWrapper((target, thisArg, argumentsList) => {
  2810. const [property] = argumentsList;
  2811. if (ignoreCSSAttributes.has(property)) {
  2812. return removeProperty.apply(thisArg, [property]);
  2813. }
  2814. const { id, styleId } = getIdAndStyleId(_optionalChain$2([thisArg, 'access', _19 => _19.parentRule, 'optionalAccess', _20 => _20.parentStyleSheet]), mirror, stylesheetManager.styleMirror);
  2815. if ((id && id !== -1) || (styleId && styleId !== -1)) {
  2816. styleDeclarationCb({
  2817. id,
  2818. styleId,
  2819. remove: {
  2820. property,
  2821. },
  2822. index: getNestedCSSRulePositions(thisArg.parentRule),
  2823. });
  2824. }
  2825. return target.apply(thisArg, argumentsList);
  2826. }),
  2827. });
  2828. return callbackWrapper(() => {
  2829. win.CSSStyleDeclaration.prototype.setProperty = setProperty;
  2830. win.CSSStyleDeclaration.prototype.removeProperty = removeProperty;
  2831. });
  2832. }
  2833. function initMediaInteractionObserver({ mediaInteractionCb, blockClass, blockSelector, unblockSelector, mirror, sampling, doc, }) {
  2834. const handler = callbackWrapper((type) => throttle$1(callbackWrapper((event) => {
  2835. const target = getEventTarget(event);
  2836. if (!target ||
  2837. isBlocked(target, blockClass, blockSelector, unblockSelector, true)) {
  2838. return;
  2839. }
  2840. const { currentTime, volume, muted, playbackRate } = target;
  2841. mediaInteractionCb({
  2842. type,
  2843. id: mirror.getId(target),
  2844. currentTime,
  2845. volume,
  2846. muted,
  2847. playbackRate,
  2848. });
  2849. }), sampling.media || 500));
  2850. const handlers = [
  2851. on('play', handler(0), doc),
  2852. on('pause', handler(1), doc),
  2853. on('seeked', handler(2), doc),
  2854. on('volumechange', handler(3), doc),
  2855. on('ratechange', handler(4), doc),
  2856. ];
  2857. return callbackWrapper(() => {
  2858. handlers.forEach((h) => h());
  2859. });
  2860. }
  2861. function initFontObserver({ fontCb, doc }) {
  2862. const win = doc.defaultView;
  2863. if (!win) {
  2864. return () => {
  2865. };
  2866. }
  2867. const handlers = [];
  2868. const fontMap = new WeakMap();
  2869. const originalFontFace = win.FontFace;
  2870. win.FontFace = function FontFace(family, source, descriptors) {
  2871. const fontFace = new originalFontFace(family, source, descriptors);
  2872. fontMap.set(fontFace, {
  2873. family,
  2874. buffer: typeof source !== 'string',
  2875. descriptors,
  2876. fontSource: typeof source === 'string'
  2877. ? source
  2878. : JSON.stringify(Array.from(new Uint8Array(source))),
  2879. });
  2880. return fontFace;
  2881. };
  2882. const restoreHandler = patch(doc.fonts, 'add', function (original) {
  2883. return function (fontFace) {
  2884. setTimeout(callbackWrapper(() => {
  2885. const p = fontMap.get(fontFace);
  2886. if (p) {
  2887. fontCb(p);
  2888. fontMap.delete(fontFace);
  2889. }
  2890. }), 0);
  2891. return original.apply(this, [fontFace]);
  2892. };
  2893. });
  2894. handlers.push(() => {
  2895. win.FontFace = originalFontFace;
  2896. });
  2897. handlers.push(restoreHandler);
  2898. return callbackWrapper(() => {
  2899. handlers.forEach((h) => h());
  2900. });
  2901. }
  2902. function initSelectionObserver(param) {
  2903. const { doc, mirror, blockClass, blockSelector, unblockSelector, selectionCb, } = param;
  2904. let collapsed = true;
  2905. const updateSelection = callbackWrapper(() => {
  2906. const selection = doc.getSelection();
  2907. if (!selection || (collapsed && _optionalChain$2([selection, 'optionalAccess', _21 => _21.isCollapsed])))
  2908. return;
  2909. collapsed = selection.isCollapsed || false;
  2910. const ranges = [];
  2911. const count = selection.rangeCount || 0;
  2912. for (let i = 0; i < count; i++) {
  2913. const range = selection.getRangeAt(i);
  2914. const { startContainer, startOffset, endContainer, endOffset } = range;
  2915. const blocked = isBlocked(startContainer, blockClass, blockSelector, unblockSelector, true) ||
  2916. isBlocked(endContainer, blockClass, blockSelector, unblockSelector, true);
  2917. if (blocked)
  2918. continue;
  2919. ranges.push({
  2920. start: mirror.getId(startContainer),
  2921. startOffset,
  2922. end: mirror.getId(endContainer),
  2923. endOffset,
  2924. });
  2925. }
  2926. selectionCb({ ranges });
  2927. });
  2928. updateSelection();
  2929. return on('selectionchange', updateSelection);
  2930. }
  2931. function initCustomElementObserver({ doc, customElementCb, }) {
  2932. const win = doc.defaultView;
  2933. if (!win || !win.customElements)
  2934. return () => { };
  2935. const restoreHandler = patch(win.customElements, 'define', function (original) {
  2936. return function (name, constructor, options) {
  2937. try {
  2938. customElementCb({
  2939. define: {
  2940. name,
  2941. },
  2942. });
  2943. }
  2944. catch (e) {
  2945. }
  2946. return original.apply(this, [name, constructor, options]);
  2947. };
  2948. });
  2949. return restoreHandler;
  2950. }
  2951. function initObservers(o, _hooks = {}) {
  2952. const currentWindow = o.doc.defaultView;
  2953. if (!currentWindow) {
  2954. return () => {
  2955. };
  2956. }
  2957. const mutationObserver = initMutationObserver(o, o.doc);
  2958. const mousemoveHandler = initMoveObserver(o);
  2959. const mouseInteractionHandler = initMouseInteractionObserver(o);
  2960. const scrollHandler = initScrollObserver(o);
  2961. const viewportResizeHandler = initViewportResizeObserver(o, {
  2962. win: currentWindow,
  2963. });
  2964. const inputHandler = initInputObserver(o);
  2965. const mediaInteractionHandler = initMediaInteractionObserver(o);
  2966. const styleSheetObserver = initStyleSheetObserver(o, { win: currentWindow });
  2967. const adoptedStyleSheetObserver = initAdoptedStyleSheetObserver(o, o.doc);
  2968. const styleDeclarationObserver = initStyleDeclarationObserver(o, {
  2969. win: currentWindow,
  2970. });
  2971. const fontObserver = o.collectFonts
  2972. ? initFontObserver(o)
  2973. : () => {
  2974. };
  2975. const selectionObserver = initSelectionObserver(o);
  2976. const customElementObserver = initCustomElementObserver(o);
  2977. const pluginHandlers = [];
  2978. for (const plugin of o.plugins) {
  2979. pluginHandlers.push(plugin.observer(plugin.callback, currentWindow, plugin.options));
  2980. }
  2981. return callbackWrapper(() => {
  2982. mutationBuffers.forEach((b) => b.reset());
  2983. mutationObserver.disconnect();
  2984. mousemoveHandler();
  2985. mouseInteractionHandler();
  2986. scrollHandler();
  2987. viewportResizeHandler();
  2988. inputHandler();
  2989. mediaInteractionHandler();
  2990. styleSheetObserver();
  2991. adoptedStyleSheetObserver();
  2992. styleDeclarationObserver();
  2993. fontObserver();
  2994. selectionObserver();
  2995. customElementObserver();
  2996. pluginHandlers.forEach((h) => h());
  2997. });
  2998. }
  2999. function hasNestedCSSRule(prop) {
  3000. return typeof window[prop] !== 'undefined';
  3001. }
  3002. function canMonkeyPatchNestedCSSRule(prop) {
  3003. return Boolean(typeof window[prop] !== 'undefined' &&
  3004. window[prop].prototype &&
  3005. 'insertRule' in window[prop].prototype &&
  3006. 'deleteRule' in window[prop].prototype);
  3007. }
  3008. class CrossOriginIframeMirror {
  3009. constructor(generateIdFn) {
  3010. this.generateIdFn = generateIdFn;
  3011. this.iframeIdToRemoteIdMap = new WeakMap();
  3012. this.iframeRemoteIdToIdMap = new WeakMap();
  3013. }
  3014. getId(iframe, remoteId, idToRemoteMap, remoteToIdMap) {
  3015. const idToRemoteIdMap = idToRemoteMap || this.getIdToRemoteIdMap(iframe);
  3016. const remoteIdToIdMap = remoteToIdMap || this.getRemoteIdToIdMap(iframe);
  3017. let id = idToRemoteIdMap.get(remoteId);
  3018. if (!id) {
  3019. id = this.generateIdFn();
  3020. idToRemoteIdMap.set(remoteId, id);
  3021. remoteIdToIdMap.set(id, remoteId);
  3022. }
  3023. return id;
  3024. }
  3025. getIds(iframe, remoteId) {
  3026. const idToRemoteIdMap = this.getIdToRemoteIdMap(iframe);
  3027. const remoteIdToIdMap = this.getRemoteIdToIdMap(iframe);
  3028. return remoteId.map((id) => this.getId(iframe, id, idToRemoteIdMap, remoteIdToIdMap));
  3029. }
  3030. getRemoteId(iframe, id, map) {
  3031. const remoteIdToIdMap = map || this.getRemoteIdToIdMap(iframe);
  3032. if (typeof id !== 'number')
  3033. return id;
  3034. const remoteId = remoteIdToIdMap.get(id);
  3035. if (!remoteId)
  3036. return -1;
  3037. return remoteId;
  3038. }
  3039. getRemoteIds(iframe, ids) {
  3040. const remoteIdToIdMap = this.getRemoteIdToIdMap(iframe);
  3041. return ids.map((id) => this.getRemoteId(iframe, id, remoteIdToIdMap));
  3042. }
  3043. reset(iframe) {
  3044. if (!iframe) {
  3045. this.iframeIdToRemoteIdMap = new WeakMap();
  3046. this.iframeRemoteIdToIdMap = new WeakMap();
  3047. return;
  3048. }
  3049. this.iframeIdToRemoteIdMap.delete(iframe);
  3050. this.iframeRemoteIdToIdMap.delete(iframe);
  3051. }
  3052. getIdToRemoteIdMap(iframe) {
  3053. let idToRemoteIdMap = this.iframeIdToRemoteIdMap.get(iframe);
  3054. if (!idToRemoteIdMap) {
  3055. idToRemoteIdMap = new Map();
  3056. this.iframeIdToRemoteIdMap.set(iframe, idToRemoteIdMap);
  3057. }
  3058. return idToRemoteIdMap;
  3059. }
  3060. getRemoteIdToIdMap(iframe) {
  3061. let remoteIdToIdMap = this.iframeRemoteIdToIdMap.get(iframe);
  3062. if (!remoteIdToIdMap) {
  3063. remoteIdToIdMap = new Map();
  3064. this.iframeRemoteIdToIdMap.set(iframe, remoteIdToIdMap);
  3065. }
  3066. return remoteIdToIdMap;
  3067. }
  3068. }
  3069. function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
  3070. class IframeManagerNoop {
  3071. constructor() {
  3072. this.crossOriginIframeMirror = new CrossOriginIframeMirror(genId);
  3073. this.crossOriginIframeRootIdMap = new WeakMap();
  3074. }
  3075. addIframe() {
  3076. }
  3077. addLoadListener() {
  3078. }
  3079. attachIframe() {
  3080. }
  3081. }
  3082. class IframeManager {
  3083. constructor(options) {
  3084. this.iframes = new WeakMap();
  3085. this.crossOriginIframeMap = new WeakMap();
  3086. this.crossOriginIframeMirror = new CrossOriginIframeMirror(genId);
  3087. this.crossOriginIframeRootIdMap = new WeakMap();
  3088. this.mutationCb = options.mutationCb;
  3089. this.wrappedEmit = options.wrappedEmit;
  3090. this.stylesheetManager = options.stylesheetManager;
  3091. this.recordCrossOriginIframes = options.recordCrossOriginIframes;
  3092. this.crossOriginIframeStyleMirror = new CrossOriginIframeMirror(this.stylesheetManager.styleMirror.generateId.bind(this.stylesheetManager.styleMirror));
  3093. this.mirror = options.mirror;
  3094. if (this.recordCrossOriginIframes) {
  3095. window.addEventListener('message', this.handleMessage.bind(this));
  3096. }
  3097. }
  3098. addIframe(iframeEl) {
  3099. this.iframes.set(iframeEl, true);
  3100. if (iframeEl.contentWindow)
  3101. this.crossOriginIframeMap.set(iframeEl.contentWindow, iframeEl);
  3102. }
  3103. addLoadListener(cb) {
  3104. this.loadListener = cb;
  3105. }
  3106. attachIframe(iframeEl, childSn) {
  3107. this.mutationCb({
  3108. adds: [
  3109. {
  3110. parentId: this.mirror.getId(iframeEl),
  3111. nextId: null,
  3112. node: childSn,
  3113. },
  3114. ],
  3115. removes: [],
  3116. texts: [],
  3117. attributes: [],
  3118. isAttachIframe: true,
  3119. });
  3120. _optionalChain$1([this, 'access', _ => _.loadListener, 'optionalCall', _2 => _2(iframeEl)]);
  3121. if (iframeEl.contentDocument &&
  3122. iframeEl.contentDocument.adoptedStyleSheets &&
  3123. iframeEl.contentDocument.adoptedStyleSheets.length > 0)
  3124. this.stylesheetManager.adoptStyleSheets(iframeEl.contentDocument.adoptedStyleSheets, this.mirror.getId(iframeEl.contentDocument));
  3125. }
  3126. handleMessage(message) {
  3127. const crossOriginMessageEvent = message;
  3128. if (crossOriginMessageEvent.data.type !== 'rrweb' ||
  3129. crossOriginMessageEvent.origin !== crossOriginMessageEvent.data.origin)
  3130. return;
  3131. const iframeSourceWindow = message.source;
  3132. if (!iframeSourceWindow)
  3133. return;
  3134. const iframeEl = this.crossOriginIframeMap.get(message.source);
  3135. if (!iframeEl)
  3136. return;
  3137. const transformedEvent = this.transformCrossOriginEvent(iframeEl, crossOriginMessageEvent.data.event);
  3138. if (transformedEvent)
  3139. this.wrappedEmit(transformedEvent, crossOriginMessageEvent.data.isCheckout);
  3140. }
  3141. transformCrossOriginEvent(iframeEl, e) {
  3142. switch (e.type) {
  3143. case EventType.FullSnapshot: {
  3144. this.crossOriginIframeMirror.reset(iframeEl);
  3145. this.crossOriginIframeStyleMirror.reset(iframeEl);
  3146. this.replaceIdOnNode(e.data.node, iframeEl);
  3147. const rootId = e.data.node.id;
  3148. this.crossOriginIframeRootIdMap.set(iframeEl, rootId);
  3149. this.patchRootIdOnNode(e.data.node, rootId);
  3150. return {
  3151. timestamp: e.timestamp,
  3152. type: EventType.IncrementalSnapshot,
  3153. data: {
  3154. source: IncrementalSource.Mutation,
  3155. adds: [
  3156. {
  3157. parentId: this.mirror.getId(iframeEl),
  3158. nextId: null,
  3159. node: e.data.node,
  3160. },
  3161. ],
  3162. removes: [],
  3163. texts: [],
  3164. attributes: [],
  3165. isAttachIframe: true,
  3166. },
  3167. };
  3168. }
  3169. case EventType.Meta:
  3170. case EventType.Load:
  3171. case EventType.DomContentLoaded: {
  3172. return false;
  3173. }
  3174. case EventType.Plugin: {
  3175. return e;
  3176. }
  3177. case EventType.Custom: {
  3178. this.replaceIds(e.data.payload, iframeEl, ['id', 'parentId', 'previousId', 'nextId']);
  3179. return e;
  3180. }
  3181. case EventType.IncrementalSnapshot: {
  3182. switch (e.data.source) {
  3183. case IncrementalSource.Mutation: {
  3184. e.data.adds.forEach((n) => {
  3185. this.replaceIds(n, iframeEl, [
  3186. 'parentId',
  3187. 'nextId',
  3188. 'previousId',
  3189. ]);
  3190. this.replaceIdOnNode(n.node, iframeEl);
  3191. const rootId = this.crossOriginIframeRootIdMap.get(iframeEl);
  3192. rootId && this.patchRootIdOnNode(n.node, rootId);
  3193. });
  3194. e.data.removes.forEach((n) => {
  3195. this.replaceIds(n, iframeEl, ['parentId', 'id']);
  3196. });
  3197. e.data.attributes.forEach((n) => {
  3198. this.replaceIds(n, iframeEl, ['id']);
  3199. });
  3200. e.data.texts.forEach((n) => {
  3201. this.replaceIds(n, iframeEl, ['id']);
  3202. });
  3203. return e;
  3204. }
  3205. case IncrementalSource.Drag:
  3206. case IncrementalSource.TouchMove:
  3207. case IncrementalSource.MouseMove: {
  3208. e.data.positions.forEach((p) => {
  3209. this.replaceIds(p, iframeEl, ['id']);
  3210. });
  3211. return e;
  3212. }
  3213. case IncrementalSource.ViewportResize: {
  3214. return false;
  3215. }
  3216. case IncrementalSource.MediaInteraction:
  3217. case IncrementalSource.MouseInteraction:
  3218. case IncrementalSource.Scroll:
  3219. case IncrementalSource.CanvasMutation:
  3220. case IncrementalSource.Input: {
  3221. this.replaceIds(e.data, iframeEl, ['id']);
  3222. return e;
  3223. }
  3224. case IncrementalSource.StyleSheetRule:
  3225. case IncrementalSource.StyleDeclaration: {
  3226. this.replaceIds(e.data, iframeEl, ['id']);
  3227. this.replaceStyleIds(e.data, iframeEl, ['styleId']);
  3228. return e;
  3229. }
  3230. case IncrementalSource.Font: {
  3231. return e;
  3232. }
  3233. case IncrementalSource.Selection: {
  3234. e.data.ranges.forEach((range) => {
  3235. this.replaceIds(range, iframeEl, ['start', 'end']);
  3236. });
  3237. return e;
  3238. }
  3239. case IncrementalSource.AdoptedStyleSheet: {
  3240. this.replaceIds(e.data, iframeEl, ['id']);
  3241. this.replaceStyleIds(e.data, iframeEl, ['styleIds']);
  3242. _optionalChain$1([e, 'access', _3 => _3.data, 'access', _4 => _4.styles, 'optionalAccess', _5 => _5.forEach, 'call', _6 => _6((style) => {
  3243. this.replaceStyleIds(style, iframeEl, ['styleId']);
  3244. })]);
  3245. return e;
  3246. }
  3247. }
  3248. }
  3249. }
  3250. return false;
  3251. }
  3252. replace(iframeMirror, obj, iframeEl, keys) {
  3253. for (const key of keys) {
  3254. if (!Array.isArray(obj[key]) && typeof obj[key] !== 'number')
  3255. continue;
  3256. if (Array.isArray(obj[key])) {
  3257. obj[key] = iframeMirror.getIds(iframeEl, obj[key]);
  3258. }
  3259. else {
  3260. obj[key] = iframeMirror.getId(iframeEl, obj[key]);
  3261. }
  3262. }
  3263. return obj;
  3264. }
  3265. replaceIds(obj, iframeEl, keys) {
  3266. return this.replace(this.crossOriginIframeMirror, obj, iframeEl, keys);
  3267. }
  3268. replaceStyleIds(obj, iframeEl, keys) {
  3269. return this.replace(this.crossOriginIframeStyleMirror, obj, iframeEl, keys);
  3270. }
  3271. replaceIdOnNode(node, iframeEl) {
  3272. this.replaceIds(node, iframeEl, ['id', 'rootId']);
  3273. if ('childNodes' in node) {
  3274. node.childNodes.forEach((child) => {
  3275. this.replaceIdOnNode(child, iframeEl);
  3276. });
  3277. }
  3278. }
  3279. patchRootIdOnNode(node, rootId) {
  3280. if (node.type !== NodeType$1.Document && !node.rootId)
  3281. node.rootId = rootId;
  3282. if ('childNodes' in node) {
  3283. node.childNodes.forEach((child) => {
  3284. this.patchRootIdOnNode(child, rootId);
  3285. });
  3286. }
  3287. }
  3288. }
  3289. class ShadowDomManagerNoop {
  3290. init() {
  3291. }
  3292. addShadowRoot() {
  3293. }
  3294. observeAttachShadow() {
  3295. }
  3296. reset() {
  3297. }
  3298. }
  3299. class ShadowDomManager {
  3300. constructor(options) {
  3301. this.shadowDoms = new WeakSet();
  3302. this.restoreHandlers = [];
  3303. this.mutationCb = options.mutationCb;
  3304. this.scrollCb = options.scrollCb;
  3305. this.bypassOptions = options.bypassOptions;
  3306. this.mirror = options.mirror;
  3307. this.init();
  3308. }
  3309. init() {
  3310. this.reset();
  3311. this.patchAttachShadow(Element, document);
  3312. }
  3313. addShadowRoot(shadowRoot, doc) {
  3314. if (!isNativeShadowDom(shadowRoot))
  3315. return;
  3316. if (this.shadowDoms.has(shadowRoot))
  3317. return;
  3318. this.shadowDoms.add(shadowRoot);
  3319. const observer = initMutationObserver({
  3320. ...this.bypassOptions,
  3321. doc,
  3322. mutationCb: this.mutationCb,
  3323. mirror: this.mirror,
  3324. shadowDomManager: this,
  3325. }, shadowRoot);
  3326. this.restoreHandlers.push(() => observer.disconnect());
  3327. this.restoreHandlers.push(initScrollObserver({
  3328. ...this.bypassOptions,
  3329. scrollCb: this.scrollCb,
  3330. doc: shadowRoot,
  3331. mirror: this.mirror,
  3332. }));
  3333. setTimeout(() => {
  3334. if (shadowRoot.adoptedStyleSheets &&
  3335. shadowRoot.adoptedStyleSheets.length > 0)
  3336. this.bypassOptions.stylesheetManager.adoptStyleSheets(shadowRoot.adoptedStyleSheets, this.mirror.getId(shadowRoot.host));
  3337. this.restoreHandlers.push(initAdoptedStyleSheetObserver({
  3338. mirror: this.mirror,
  3339. stylesheetManager: this.bypassOptions.stylesheetManager,
  3340. }, shadowRoot));
  3341. }, 0);
  3342. }
  3343. observeAttachShadow(iframeElement) {
  3344. if (!iframeElement.contentWindow || !iframeElement.contentDocument)
  3345. return;
  3346. this.patchAttachShadow(iframeElement.contentWindow.Element, iframeElement.contentDocument);
  3347. }
  3348. patchAttachShadow(element, doc) {
  3349. const manager = this;
  3350. this.restoreHandlers.push(patch(element.prototype, 'attachShadow', function (original) {
  3351. return function (option) {
  3352. const shadowRoot = original.call(this, option);
  3353. if (this.shadowRoot && inDom(this))
  3354. manager.addShadowRoot(this.shadowRoot, doc);
  3355. return shadowRoot;
  3356. };
  3357. }));
  3358. }
  3359. reset() {
  3360. this.restoreHandlers.forEach((handler) => {
  3361. try {
  3362. handler();
  3363. }
  3364. catch (e) {
  3365. }
  3366. });
  3367. this.restoreHandlers = [];
  3368. this.shadowDoms = new WeakSet();
  3369. }
  3370. }
  3371. class CanvasManagerNoop {
  3372. reset() {
  3373. }
  3374. freeze() {
  3375. }
  3376. unfreeze() {
  3377. }
  3378. lock() {
  3379. }
  3380. unlock() {
  3381. }
  3382. snapshot() {
  3383. }
  3384. }
  3385. class StylesheetManager {
  3386. constructor(options) {
  3387. this.trackedLinkElements = new WeakSet();
  3388. this.styleMirror = new StyleSheetMirror();
  3389. this.mutationCb = options.mutationCb;
  3390. this.adoptedStyleSheetCb = options.adoptedStyleSheetCb;
  3391. }
  3392. attachLinkElement(linkEl, childSn) {
  3393. if ('_cssText' in childSn.attributes)
  3394. this.mutationCb({
  3395. adds: [],
  3396. removes: [],
  3397. texts: [],
  3398. attributes: [
  3399. {
  3400. id: childSn.id,
  3401. attributes: childSn
  3402. .attributes,
  3403. },
  3404. ],
  3405. });
  3406. this.trackLinkElement(linkEl);
  3407. }
  3408. trackLinkElement(linkEl) {
  3409. if (this.trackedLinkElements.has(linkEl))
  3410. return;
  3411. this.trackedLinkElements.add(linkEl);
  3412. this.trackStylesheetInLinkElement(linkEl);
  3413. }
  3414. adoptStyleSheets(sheets, hostId) {
  3415. if (sheets.length === 0)
  3416. return;
  3417. const adoptedStyleSheetData = {
  3418. id: hostId,
  3419. styleIds: [],
  3420. };
  3421. const styles = [];
  3422. for (const sheet of sheets) {
  3423. let styleId;
  3424. if (!this.styleMirror.has(sheet)) {
  3425. styleId = this.styleMirror.add(sheet);
  3426. styles.push({
  3427. styleId,
  3428. rules: Array.from(sheet.rules || CSSRule, (r, index) => ({
  3429. rule: stringifyRule(r),
  3430. index,
  3431. })),
  3432. });
  3433. }
  3434. else
  3435. styleId = this.styleMirror.getId(sheet);
  3436. adoptedStyleSheetData.styleIds.push(styleId);
  3437. }
  3438. if (styles.length > 0)
  3439. adoptedStyleSheetData.styles = styles;
  3440. this.adoptedStyleSheetCb(adoptedStyleSheetData);
  3441. }
  3442. reset() {
  3443. this.styleMirror.reset();
  3444. this.trackedLinkElements = new WeakSet();
  3445. }
  3446. trackStylesheetInLinkElement(linkEl) {
  3447. }
  3448. }
  3449. class ProcessedNodeManager {
  3450. constructor() {
  3451. this.nodeMap = new WeakMap();
  3452. this.loop = true;
  3453. this.periodicallyClear();
  3454. }
  3455. periodicallyClear() {
  3456. onRequestAnimationFrame(() => {
  3457. this.clear();
  3458. if (this.loop)
  3459. this.periodicallyClear();
  3460. });
  3461. }
  3462. inOtherBuffer(node, thisBuffer) {
  3463. const buffers = this.nodeMap.get(node);
  3464. return (buffers && Array.from(buffers).some((buffer) => buffer !== thisBuffer));
  3465. }
  3466. add(node, buffer) {
  3467. this.nodeMap.set(node, (this.nodeMap.get(node) || new Set()).add(buffer));
  3468. }
  3469. clear() {
  3470. this.nodeMap = new WeakMap();
  3471. }
  3472. destroy() {
  3473. this.loop = false;
  3474. }
  3475. }
  3476. function wrapEvent(e) {
  3477. const eWithTime = e;
  3478. eWithTime.timestamp = nowTimestamp();
  3479. return eWithTime;
  3480. }
  3481. let _takeFullSnapshot;
  3482. const mirror = createMirror();
  3483. function record(options = {}) {
  3484. const { emit, checkoutEveryNms, checkoutEveryNth, blockClass = 'rr-block', blockSelector = null, unblockSelector = null, ignoreClass = 'rr-ignore', ignoreSelector = null, maskAllText = false, maskTextClass = 'rr-mask', unmaskTextClass = null, maskTextSelector = null, unmaskTextSelector = null, inlineStylesheet = true, maskAllInputs, maskInputOptions: _maskInputOptions, slimDOMOptions: _slimDOMOptions, maskAttributeFn, maskInputFn, maskTextFn, packFn, sampling = {}, dataURLOptions = {}, mousemoveWait, recordCanvas = false, recordCrossOriginIframes = false, recordAfter = options.recordAfter === 'DOMContentLoaded'
  3485. ? options.recordAfter
  3486. : 'load', userTriggeredOnInput = false, collectFonts = false, inlineImages = false, plugins, keepIframeSrcFn = () => false, ignoreCSSAttributes = new Set([]), errorHandler, onMutation, getCanvasManager, } = options;
  3487. registerErrorHandler(errorHandler);
  3488. const inEmittingFrame = recordCrossOriginIframes
  3489. ? window.parent === window
  3490. : true;
  3491. let passEmitsToParent = false;
  3492. if (!inEmittingFrame) {
  3493. try {
  3494. if (window.parent.document) {
  3495. passEmitsToParent = false;
  3496. }
  3497. }
  3498. catch (e) {
  3499. passEmitsToParent = true;
  3500. }
  3501. }
  3502. if (inEmittingFrame && !emit) {
  3503. throw new Error('emit function is required');
  3504. }
  3505. if (mousemoveWait !== undefined && sampling.mousemove === undefined) {
  3506. sampling.mousemove = mousemoveWait;
  3507. }
  3508. mirror.reset();
  3509. const maskInputOptions = maskAllInputs === true
  3510. ? {
  3511. color: true,
  3512. date: true,
  3513. 'datetime-local': true,
  3514. email: true,
  3515. month: true,
  3516. number: true,
  3517. range: true,
  3518. search: true,
  3519. tel: true,
  3520. text: true,
  3521. time: true,
  3522. url: true,
  3523. week: true,
  3524. textarea: true,
  3525. select: true,
  3526. radio: true,
  3527. checkbox: true,
  3528. }
  3529. : _maskInputOptions !== undefined
  3530. ? _maskInputOptions
  3531. : {};
  3532. const slimDOMOptions = _slimDOMOptions === true || _slimDOMOptions === 'all'
  3533. ? {
  3534. script: true,
  3535. comment: true,
  3536. headFavicon: true,
  3537. headWhitespace: true,
  3538. headMetaSocial: true,
  3539. headMetaRobots: true,
  3540. headMetaHttpEquiv: true,
  3541. headMetaVerification: true,
  3542. headMetaAuthorship: _slimDOMOptions === 'all',
  3543. headMetaDescKeywords: _slimDOMOptions === 'all',
  3544. }
  3545. : _slimDOMOptions
  3546. ? _slimDOMOptions
  3547. : {};
  3548. polyfill();
  3549. let lastFullSnapshotEvent;
  3550. let incrementalSnapshotCount = 0;
  3551. const eventProcessor = (e) => {
  3552. for (const plugin of plugins || []) {
  3553. if (plugin.eventProcessor) {
  3554. e = plugin.eventProcessor(e);
  3555. }
  3556. }
  3557. if (packFn &&
  3558. !passEmitsToParent) {
  3559. e = packFn(e);
  3560. }
  3561. return e;
  3562. };
  3563. const wrappedEmit = (e, isCheckout) => {
  3564. if (_optionalChain([mutationBuffers, 'access', _ => _[0], 'optionalAccess', _2 => _2.isFrozen, 'call', _3 => _3()]) &&
  3565. e.type !== EventType.FullSnapshot &&
  3566. !(e.type === EventType.IncrementalSnapshot &&
  3567. e.data.source === IncrementalSource.Mutation)) {
  3568. mutationBuffers.forEach((buf) => buf.unfreeze());
  3569. }
  3570. if (inEmittingFrame) {
  3571. _optionalChain([emit, 'optionalCall', _4 => _4(eventProcessor(e), isCheckout)]);
  3572. }
  3573. else if (passEmitsToParent) {
  3574. const message = {
  3575. type: 'rrweb',
  3576. event: eventProcessor(e),
  3577. origin: window.location.origin,
  3578. isCheckout,
  3579. };
  3580. window.parent.postMessage(message, '*');
  3581. }
  3582. if (e.type === EventType.FullSnapshot) {
  3583. lastFullSnapshotEvent = e;
  3584. incrementalSnapshotCount = 0;
  3585. }
  3586. else if (e.type === EventType.IncrementalSnapshot) {
  3587. if (e.data.source === IncrementalSource.Mutation &&
  3588. e.data.isAttachIframe) {
  3589. return;
  3590. }
  3591. incrementalSnapshotCount++;
  3592. const exceedCount = checkoutEveryNth && incrementalSnapshotCount >= checkoutEveryNth;
  3593. const exceedTime = checkoutEveryNms &&
  3594. e.timestamp - lastFullSnapshotEvent.timestamp > checkoutEveryNms;
  3595. if (exceedCount || exceedTime) {
  3596. takeFullSnapshot(true);
  3597. }
  3598. }
  3599. };
  3600. const wrappedMutationEmit = (m) => {
  3601. wrappedEmit(wrapEvent({
  3602. type: EventType.IncrementalSnapshot,
  3603. data: {
  3604. source: IncrementalSource.Mutation,
  3605. ...m,
  3606. },
  3607. }));
  3608. };
  3609. const wrappedScrollEmit = (p) => wrappedEmit(wrapEvent({
  3610. type: EventType.IncrementalSnapshot,
  3611. data: {
  3612. source: IncrementalSource.Scroll,
  3613. ...p,
  3614. },
  3615. }));
  3616. const wrappedCanvasMutationEmit = (p) => wrappedEmit(wrapEvent({
  3617. type: EventType.IncrementalSnapshot,
  3618. data: {
  3619. source: IncrementalSource.CanvasMutation,
  3620. ...p,
  3621. },
  3622. }));
  3623. const wrappedAdoptedStyleSheetEmit = (a) => wrappedEmit(wrapEvent({
  3624. type: EventType.IncrementalSnapshot,
  3625. data: {
  3626. source: IncrementalSource.AdoptedStyleSheet,
  3627. ...a,
  3628. },
  3629. }));
  3630. const stylesheetManager = new StylesheetManager({
  3631. mutationCb: wrappedMutationEmit,
  3632. adoptedStyleSheetCb: wrappedAdoptedStyleSheetEmit,
  3633. });
  3634. const iframeManager = typeof __RRWEB_EXCLUDE_IFRAME__ === 'boolean' && __RRWEB_EXCLUDE_IFRAME__
  3635. ? new IframeManagerNoop()
  3636. : new IframeManager({
  3637. mirror,
  3638. mutationCb: wrappedMutationEmit,
  3639. stylesheetManager: stylesheetManager,
  3640. recordCrossOriginIframes,
  3641. wrappedEmit,
  3642. });
  3643. for (const plugin of plugins || []) {
  3644. if (plugin.getMirror)
  3645. plugin.getMirror({
  3646. nodeMirror: mirror,
  3647. crossOriginIframeMirror: iframeManager.crossOriginIframeMirror,
  3648. crossOriginIframeStyleMirror: iframeManager.crossOriginIframeStyleMirror,
  3649. });
  3650. }
  3651. const processedNodeManager = new ProcessedNodeManager();
  3652. const canvasManager = _getCanvasManager(getCanvasManager, {
  3653. mirror,
  3654. win: window,
  3655. mutationCb: (p) => wrappedEmit(wrapEvent({
  3656. type: EventType.IncrementalSnapshot,
  3657. data: {
  3658. source: IncrementalSource.CanvasMutation,
  3659. ...p,
  3660. },
  3661. })),
  3662. recordCanvas,
  3663. blockClass,
  3664. blockSelector,
  3665. unblockSelector,
  3666. sampling: sampling['canvas'],
  3667. dataURLOptions,
  3668. errorHandler,
  3669. });
  3670. const shadowDomManager = typeof __RRWEB_EXCLUDE_SHADOW_DOM__ === 'boolean' &&
  3671. __RRWEB_EXCLUDE_SHADOW_DOM__
  3672. ? new ShadowDomManagerNoop()
  3673. : new ShadowDomManager({
  3674. mutationCb: wrappedMutationEmit,
  3675. scrollCb: wrappedScrollEmit,
  3676. bypassOptions: {
  3677. onMutation,
  3678. blockClass,
  3679. blockSelector,
  3680. unblockSelector,
  3681. maskAllText,
  3682. maskTextClass,
  3683. unmaskTextClass,
  3684. maskTextSelector,
  3685. unmaskTextSelector,
  3686. inlineStylesheet,
  3687. maskInputOptions,
  3688. dataURLOptions,
  3689. maskAttributeFn,
  3690. maskTextFn,
  3691. maskInputFn,
  3692. recordCanvas,
  3693. inlineImages,
  3694. sampling,
  3695. slimDOMOptions,
  3696. iframeManager,
  3697. stylesheetManager,
  3698. canvasManager,
  3699. keepIframeSrcFn,
  3700. processedNodeManager,
  3701. },
  3702. mirror,
  3703. });
  3704. const takeFullSnapshot = (isCheckout = false) => {
  3705. wrappedEmit(wrapEvent({
  3706. type: EventType.Meta,
  3707. data: {
  3708. href: window.location.href,
  3709. width: getWindowWidth(),
  3710. height: getWindowHeight(),
  3711. },
  3712. }), isCheckout);
  3713. stylesheetManager.reset();
  3714. shadowDomManager.init();
  3715. mutationBuffers.forEach((buf) => buf.lock());
  3716. const node = snapshot(document, {
  3717. mirror,
  3718. blockClass,
  3719. blockSelector,
  3720. unblockSelector,
  3721. maskAllText,
  3722. maskTextClass,
  3723. unmaskTextClass,
  3724. maskTextSelector,
  3725. unmaskTextSelector,
  3726. inlineStylesheet,
  3727. maskAllInputs: maskInputOptions,
  3728. maskAttributeFn,
  3729. maskInputFn,
  3730. maskTextFn,
  3731. slimDOM: slimDOMOptions,
  3732. dataURLOptions,
  3733. recordCanvas,
  3734. inlineImages,
  3735. onSerialize: (n) => {
  3736. if (isSerializedIframe(n, mirror)) {
  3737. iframeManager.addIframe(n);
  3738. }
  3739. if (isSerializedStylesheet(n, mirror)) {
  3740. stylesheetManager.trackLinkElement(n);
  3741. }
  3742. if (hasShadowRoot(n)) {
  3743. shadowDomManager.addShadowRoot(n.shadowRoot, document);
  3744. }
  3745. },
  3746. onIframeLoad: (iframe, childSn) => {
  3747. iframeManager.attachIframe(iframe, childSn);
  3748. shadowDomManager.observeAttachShadow(iframe);
  3749. },
  3750. onStylesheetLoad: (linkEl, childSn) => {
  3751. stylesheetManager.attachLinkElement(linkEl, childSn);
  3752. },
  3753. keepIframeSrcFn,
  3754. });
  3755. if (!node) {
  3756. return console.warn('Failed to snapshot the document');
  3757. }
  3758. wrappedEmit(wrapEvent({
  3759. type: EventType.FullSnapshot,
  3760. data: {
  3761. node,
  3762. initialOffset: getWindowScroll(window),
  3763. },
  3764. }));
  3765. mutationBuffers.forEach((buf) => buf.unlock());
  3766. if (document.adoptedStyleSheets && document.adoptedStyleSheets.length > 0)
  3767. stylesheetManager.adoptStyleSheets(document.adoptedStyleSheets, mirror.getId(document));
  3768. };
  3769. _takeFullSnapshot = takeFullSnapshot;
  3770. try {
  3771. const handlers = [];
  3772. const observe = (doc) => {
  3773. return callbackWrapper(initObservers)({
  3774. onMutation,
  3775. mutationCb: wrappedMutationEmit,
  3776. mousemoveCb: (positions, source) => wrappedEmit(wrapEvent({
  3777. type: EventType.IncrementalSnapshot,
  3778. data: {
  3779. source,
  3780. positions,
  3781. },
  3782. })),
  3783. mouseInteractionCb: (d) => wrappedEmit(wrapEvent({
  3784. type: EventType.IncrementalSnapshot,
  3785. data: {
  3786. source: IncrementalSource.MouseInteraction,
  3787. ...d,
  3788. },
  3789. })),
  3790. scrollCb: wrappedScrollEmit,
  3791. viewportResizeCb: (d) => wrappedEmit(wrapEvent({
  3792. type: EventType.IncrementalSnapshot,
  3793. data: {
  3794. source: IncrementalSource.ViewportResize,
  3795. ...d,
  3796. },
  3797. })),
  3798. inputCb: (v) => wrappedEmit(wrapEvent({
  3799. type: EventType.IncrementalSnapshot,
  3800. data: {
  3801. source: IncrementalSource.Input,
  3802. ...v,
  3803. },
  3804. })),
  3805. mediaInteractionCb: (p) => wrappedEmit(wrapEvent({
  3806. type: EventType.IncrementalSnapshot,
  3807. data: {
  3808. source: IncrementalSource.MediaInteraction,
  3809. ...p,
  3810. },
  3811. })),
  3812. styleSheetRuleCb: (r) => wrappedEmit(wrapEvent({
  3813. type: EventType.IncrementalSnapshot,
  3814. data: {
  3815. source: IncrementalSource.StyleSheetRule,
  3816. ...r,
  3817. },
  3818. })),
  3819. styleDeclarationCb: (r) => wrappedEmit(wrapEvent({
  3820. type: EventType.IncrementalSnapshot,
  3821. data: {
  3822. source: IncrementalSource.StyleDeclaration,
  3823. ...r,
  3824. },
  3825. })),
  3826. canvasMutationCb: wrappedCanvasMutationEmit,
  3827. fontCb: (p) => wrappedEmit(wrapEvent({
  3828. type: EventType.IncrementalSnapshot,
  3829. data: {
  3830. source: IncrementalSource.Font,
  3831. ...p,
  3832. },
  3833. })),
  3834. selectionCb: (p) => {
  3835. wrappedEmit(wrapEvent({
  3836. type: EventType.IncrementalSnapshot,
  3837. data: {
  3838. source: IncrementalSource.Selection,
  3839. ...p,
  3840. },
  3841. }));
  3842. },
  3843. customElementCb: (c) => {
  3844. wrappedEmit(wrapEvent({
  3845. type: EventType.IncrementalSnapshot,
  3846. data: {
  3847. source: IncrementalSource.CustomElement,
  3848. ...c,
  3849. },
  3850. }));
  3851. },
  3852. blockClass,
  3853. ignoreClass,
  3854. ignoreSelector,
  3855. maskAllText,
  3856. maskTextClass,
  3857. unmaskTextClass,
  3858. maskTextSelector,
  3859. unmaskTextSelector,
  3860. maskInputOptions,
  3861. inlineStylesheet,
  3862. sampling,
  3863. recordCanvas,
  3864. inlineImages,
  3865. userTriggeredOnInput,
  3866. collectFonts,
  3867. doc,
  3868. maskAttributeFn,
  3869. maskInputFn,
  3870. maskTextFn,
  3871. keepIframeSrcFn,
  3872. blockSelector,
  3873. unblockSelector,
  3874. slimDOMOptions,
  3875. dataURLOptions,
  3876. mirror,
  3877. iframeManager,
  3878. stylesheetManager,
  3879. shadowDomManager,
  3880. processedNodeManager,
  3881. canvasManager,
  3882. ignoreCSSAttributes,
  3883. plugins: _optionalChain([plugins
  3884. , 'optionalAccess', _5 => _5.filter, 'call', _6 => _6((p) => p.observer)
  3885. , 'optionalAccess', _7 => _7.map, 'call', _8 => _8((p) => ({
  3886. observer: p.observer,
  3887. options: p.options,
  3888. callback: (payload) => wrappedEmit(wrapEvent({
  3889. type: EventType.Plugin,
  3890. data: {
  3891. plugin: p.name,
  3892. payload,
  3893. },
  3894. })),
  3895. }))]) || [],
  3896. }, {});
  3897. };
  3898. iframeManager.addLoadListener((iframeEl) => {
  3899. try {
  3900. handlers.push(observe(iframeEl.contentDocument));
  3901. }
  3902. catch (error) {
  3903. console.warn(error);
  3904. }
  3905. });
  3906. const init = () => {
  3907. takeFullSnapshot();
  3908. handlers.push(observe(document));
  3909. };
  3910. if (document.readyState === 'interactive' ||
  3911. document.readyState === 'complete') {
  3912. init();
  3913. }
  3914. else {
  3915. handlers.push(on('DOMContentLoaded', () => {
  3916. wrappedEmit(wrapEvent({
  3917. type: EventType.DomContentLoaded,
  3918. data: {},
  3919. }));
  3920. if (recordAfter === 'DOMContentLoaded')
  3921. init();
  3922. }));
  3923. handlers.push(on('load', () => {
  3924. wrappedEmit(wrapEvent({
  3925. type: EventType.Load,
  3926. data: {},
  3927. }));
  3928. if (recordAfter === 'load')
  3929. init();
  3930. }, window));
  3931. }
  3932. return () => {
  3933. handlers.forEach((h) => h());
  3934. processedNodeManager.destroy();
  3935. _takeFullSnapshot = undefined;
  3936. unregisterErrorHandler();
  3937. };
  3938. }
  3939. catch (error) {
  3940. console.warn(error);
  3941. }
  3942. }
  3943. function takeFullSnapshot(isCheckout) {
  3944. if (!_takeFullSnapshot) {
  3945. throw new Error('please take full snapshot after start recording');
  3946. }
  3947. _takeFullSnapshot(isCheckout);
  3948. }
  3949. record.mirror = mirror;
  3950. record.takeFullSnapshot = takeFullSnapshot;
  3951. function _getCanvasManager(getCanvasManagerFn, options) {
  3952. try {
  3953. return getCanvasManagerFn
  3954. ? getCanvasManagerFn(options)
  3955. : new CanvasManagerNoop();
  3956. }
  3957. catch (e2) {
  3958. console.warn('Unable to initialize CanvasManager');
  3959. return new CanvasManagerNoop();
  3960. }
  3961. }
  3962. const ReplayEventTypeIncrementalSnapshot = 3;
  3963. const ReplayEventTypeCustom = 5;
  3964. /**
  3965. * Converts a timestamp to ms, if it was in s, or keeps it as ms.
  3966. */
  3967. function timestampToMs(timestamp) {
  3968. const isMs = timestamp > 9999999999;
  3969. return isMs ? timestamp : timestamp * 1000;
  3970. }
  3971. /**
  3972. * Converts a timestamp to s, if it was in ms, or keeps it as s.
  3973. */
  3974. function timestampToS(timestamp) {
  3975. const isMs = timestamp > 9999999999;
  3976. return isMs ? timestamp / 1000 : timestamp;
  3977. }
  3978. /**
  3979. * Add a breadcrumb event to replay.
  3980. */
  3981. function addBreadcrumbEvent(replay, breadcrumb) {
  3982. if (breadcrumb.category === 'sentry.transaction') {
  3983. return;
  3984. }
  3985. if (['ui.click', 'ui.input'].includes(breadcrumb.category )) {
  3986. replay.triggerUserActivity();
  3987. } else {
  3988. replay.checkAndHandleExpiredSession();
  3989. }
  3990. replay.addUpdate(() => {
  3991. // This should never reject
  3992. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  3993. replay.throttledAddEvent({
  3994. type: EventType.Custom,
  3995. // TODO: We were converting from ms to seconds for breadcrumbs, spans,
  3996. // but maybe we should just keep them as milliseconds
  3997. timestamp: (breadcrumb.timestamp || 0) * 1000,
  3998. data: {
  3999. tag: 'breadcrumb',
  4000. // normalize to max. 10 depth and 1_000 properties per object
  4001. payload: utils.normalize(breadcrumb, 10, 1000),
  4002. },
  4003. });
  4004. // Do not flush after console log messages
  4005. return breadcrumb.category === 'console';
  4006. });
  4007. }
  4008. const INTERACTIVE_SELECTOR = 'button,a';
  4009. /** Get the closest interactive parent element, or else return the given element. */
  4010. function getClosestInteractive(element) {
  4011. const closestInteractive = element.closest(INTERACTIVE_SELECTOR);
  4012. return closestInteractive || element;
  4013. }
  4014. /**
  4015. * For clicks, we check if the target is inside of a button or link
  4016. * If so, we use this as the target instead
  4017. * This is useful because if you click on the image in <button><img></button>,
  4018. * The target will be the image, not the button, which we don't want here
  4019. */
  4020. function getClickTargetNode(event) {
  4021. const target = getTargetNode(event);
  4022. if (!target || !(target instanceof Element)) {
  4023. return target;
  4024. }
  4025. return getClosestInteractive(target);
  4026. }
  4027. /** Get the event target node. */
  4028. function getTargetNode(event) {
  4029. if (isEventWithTarget(event)) {
  4030. return event.target ;
  4031. }
  4032. return event;
  4033. }
  4034. function isEventWithTarget(event) {
  4035. return typeof event === 'object' && !!event && 'target' in event;
  4036. }
  4037. let handlers;
  4038. /**
  4039. * Register a handler to be called when `window.open()` is called.
  4040. * Returns a cleanup function.
  4041. */
  4042. function onWindowOpen(cb) {
  4043. // Ensure to only register this once
  4044. if (!handlers) {
  4045. handlers = [];
  4046. monkeyPatchWindowOpen();
  4047. }
  4048. handlers.push(cb);
  4049. return () => {
  4050. const pos = handlers ? handlers.indexOf(cb) : -1;
  4051. if (pos > -1) {
  4052. (handlers ).splice(pos, 1);
  4053. }
  4054. };
  4055. }
  4056. function monkeyPatchWindowOpen() {
  4057. utils.fill(WINDOW, 'open', function (originalWindowOpen) {
  4058. return function (...args) {
  4059. if (handlers) {
  4060. try {
  4061. handlers.forEach(handler => handler());
  4062. } catch (e) {
  4063. // ignore errors in here
  4064. }
  4065. }
  4066. return originalWindowOpen.apply(WINDOW, args);
  4067. };
  4068. });
  4069. }
  4070. /** Handle a click. */
  4071. function handleClick(clickDetector, clickBreadcrumb, node) {
  4072. clickDetector.handleClick(clickBreadcrumb, node);
  4073. }
  4074. /** A click detector class that can be used to detect slow or rage clicks on elements. */
  4075. class ClickDetector {
  4076. // protected for testing
  4077. constructor(
  4078. replay,
  4079. slowClickConfig,
  4080. // Just for easier testing
  4081. _addBreadcrumbEvent = addBreadcrumbEvent,
  4082. ) {
  4083. this._lastMutation = 0;
  4084. this._lastScroll = 0;
  4085. this._clicks = [];
  4086. // We want everything in s, but options are in ms
  4087. this._timeout = slowClickConfig.timeout / 1000;
  4088. this._threshold = slowClickConfig.threshold / 1000;
  4089. this._scollTimeout = slowClickConfig.scrollTimeout / 1000;
  4090. this._replay = replay;
  4091. this._ignoreSelector = slowClickConfig.ignoreSelector;
  4092. this._addBreadcrumbEvent = _addBreadcrumbEvent;
  4093. }
  4094. /** Register click detection handlers on mutation or scroll. */
  4095. addListeners() {
  4096. const cleanupWindowOpen = onWindowOpen(() => {
  4097. // Treat window.open as mutation
  4098. this._lastMutation = nowInSeconds();
  4099. });
  4100. this._teardown = () => {
  4101. cleanupWindowOpen();
  4102. this._clicks = [];
  4103. this._lastMutation = 0;
  4104. this._lastScroll = 0;
  4105. };
  4106. }
  4107. /** Clean up listeners. */
  4108. removeListeners() {
  4109. if (this._teardown) {
  4110. this._teardown();
  4111. }
  4112. if (this._checkClickTimeout) {
  4113. clearTimeout(this._checkClickTimeout);
  4114. }
  4115. }
  4116. /** @inheritDoc */
  4117. handleClick(breadcrumb, node) {
  4118. if (ignoreElement(node, this._ignoreSelector) || !isClickBreadcrumb(breadcrumb)) {
  4119. return;
  4120. }
  4121. const newClick = {
  4122. timestamp: timestampToS(breadcrumb.timestamp),
  4123. clickBreadcrumb: breadcrumb,
  4124. // Set this to 0 so we know it originates from the click breadcrumb
  4125. clickCount: 0,
  4126. node,
  4127. };
  4128. // If there was a click in the last 1s on the same element, ignore it - only keep a single reference per second
  4129. if (
  4130. this._clicks.some(click => click.node === newClick.node && Math.abs(click.timestamp - newClick.timestamp) < 1)
  4131. ) {
  4132. return;
  4133. }
  4134. this._clicks.push(newClick);
  4135. // If this is the first new click, set a timeout to check for multi clicks
  4136. if (this._clicks.length === 1) {
  4137. this._scheduleCheckClicks();
  4138. }
  4139. }
  4140. /** @inheritDoc */
  4141. registerMutation(timestamp = Date.now()) {
  4142. this._lastMutation = timestampToS(timestamp);
  4143. }
  4144. /** @inheritDoc */
  4145. registerScroll(timestamp = Date.now()) {
  4146. this._lastScroll = timestampToS(timestamp);
  4147. }
  4148. /** @inheritDoc */
  4149. registerClick(element) {
  4150. const node = getClosestInteractive(element);
  4151. this._handleMultiClick(node );
  4152. }
  4153. /** Count multiple clicks on elements. */
  4154. _handleMultiClick(node) {
  4155. this._getClicks(node).forEach(click => {
  4156. click.clickCount++;
  4157. });
  4158. }
  4159. /** Get all pending clicks for a given node. */
  4160. _getClicks(node) {
  4161. return this._clicks.filter(click => click.node === node);
  4162. }
  4163. /** Check the clicks that happened. */
  4164. _checkClicks() {
  4165. const timedOutClicks = [];
  4166. const now = nowInSeconds();
  4167. this._clicks.forEach(click => {
  4168. if (!click.mutationAfter && this._lastMutation) {
  4169. click.mutationAfter = click.timestamp <= this._lastMutation ? this._lastMutation - click.timestamp : undefined;
  4170. }
  4171. if (!click.scrollAfter && this._lastScroll) {
  4172. click.scrollAfter = click.timestamp <= this._lastScroll ? this._lastScroll - click.timestamp : undefined;
  4173. }
  4174. // All of these are in seconds!
  4175. if (click.timestamp + this._timeout <= now) {
  4176. timedOutClicks.push(click);
  4177. }
  4178. });
  4179. // Remove "old" clicks
  4180. for (const click of timedOutClicks) {
  4181. const pos = this._clicks.indexOf(click);
  4182. if (pos > -1) {
  4183. this._generateBreadcrumbs(click);
  4184. this._clicks.splice(pos, 1);
  4185. }
  4186. }
  4187. // Trigger new check, unless no clicks left
  4188. if (this._clicks.length) {
  4189. this._scheduleCheckClicks();
  4190. }
  4191. }
  4192. /** Generate matching breadcrumb(s) for the click. */
  4193. _generateBreadcrumbs(click) {
  4194. const replay = this._replay;
  4195. const hadScroll = click.scrollAfter && click.scrollAfter <= this._scollTimeout;
  4196. const hadMutation = click.mutationAfter && click.mutationAfter <= this._threshold;
  4197. const isSlowClick = !hadScroll && !hadMutation;
  4198. const { clickCount, clickBreadcrumb } = click;
  4199. // Slow click
  4200. if (isSlowClick) {
  4201. // If `mutationAfter` is set, it means a mutation happened after the threshold, but before the timeout
  4202. // If not, it means we just timed out without scroll & mutation
  4203. const timeAfterClickMs = Math.min(click.mutationAfter || this._timeout, this._timeout) * 1000;
  4204. const endReason = timeAfterClickMs < this._timeout * 1000 ? 'mutation' : 'timeout';
  4205. const breadcrumb = {
  4206. type: 'default',
  4207. message: clickBreadcrumb.message,
  4208. timestamp: clickBreadcrumb.timestamp,
  4209. category: 'ui.slowClickDetected',
  4210. data: {
  4211. ...clickBreadcrumb.data,
  4212. url: WINDOW.location.href,
  4213. route: replay.getCurrentRoute(),
  4214. timeAfterClickMs,
  4215. endReason,
  4216. // If clickCount === 0, it means multiClick was not correctly captured here
  4217. // - we still want to send 1 in this case
  4218. clickCount: clickCount || 1,
  4219. },
  4220. };
  4221. this._addBreadcrumbEvent(replay, breadcrumb);
  4222. return;
  4223. }
  4224. // Multi click
  4225. if (clickCount > 1) {
  4226. const breadcrumb = {
  4227. type: 'default',
  4228. message: clickBreadcrumb.message,
  4229. timestamp: clickBreadcrumb.timestamp,
  4230. category: 'ui.multiClick',
  4231. data: {
  4232. ...clickBreadcrumb.data,
  4233. url: WINDOW.location.href,
  4234. route: replay.getCurrentRoute(),
  4235. clickCount,
  4236. metric: true,
  4237. },
  4238. };
  4239. this._addBreadcrumbEvent(replay, breadcrumb);
  4240. }
  4241. }
  4242. /** Schedule to check current clicks. */
  4243. _scheduleCheckClicks() {
  4244. if (this._checkClickTimeout) {
  4245. clearTimeout(this._checkClickTimeout);
  4246. }
  4247. this._checkClickTimeout = setTimeout(() => this._checkClicks(), 1000);
  4248. }
  4249. }
  4250. const SLOW_CLICK_TAGS = ['A', 'BUTTON', 'INPUT'];
  4251. /** exported for tests only */
  4252. function ignoreElement(node, ignoreSelector) {
  4253. if (!SLOW_CLICK_TAGS.includes(node.tagName)) {
  4254. return true;
  4255. }
  4256. // If <input> tag, we only want to consider input[type='submit'] & input[type='button']
  4257. if (node.tagName === 'INPUT' && !['submit', 'button'].includes(node.getAttribute('type') || '')) {
  4258. return true;
  4259. }
  4260. // If <a> tag, detect special variants that may not lead to an action
  4261. // If target !== _self, we may open the link somewhere else, which would lead to no action
  4262. // Also, when downloading a file, we may not leave the page, but still not trigger an action
  4263. if (
  4264. node.tagName === 'A' &&
  4265. (node.hasAttribute('download') || (node.hasAttribute('target') && node.getAttribute('target') !== '_self'))
  4266. ) {
  4267. return true;
  4268. }
  4269. if (ignoreSelector && node.matches(ignoreSelector)) {
  4270. return true;
  4271. }
  4272. return false;
  4273. }
  4274. function isClickBreadcrumb(breadcrumb) {
  4275. return !!(breadcrumb.data && typeof breadcrumb.data.nodeId === 'number' && breadcrumb.timestamp);
  4276. }
  4277. // This is good enough for us, and is easier to test/mock than `timestampInSeconds`
  4278. function nowInSeconds() {
  4279. return Date.now() / 1000;
  4280. }
  4281. /** Update the click detector based on a recording event of rrweb. */
  4282. function updateClickDetectorForRecordingEvent(clickDetector, event) {
  4283. try {
  4284. // note: We only consider incremental snapshots here
  4285. // This means that any full snapshot is ignored for mutation detection - the reason is that we simply cannot know if a mutation happened here.
  4286. // E.g. think that we are buffering, an error happens and we take a full snapshot because we switched to session mode -
  4287. // in this scenario, we would not know if a dead click happened because of the error, which is a key dead click scenario.
  4288. // Instead, by ignoring full snapshots, we have the risk that we generate a false positive
  4289. // (if a mutation _did_ happen but was "swallowed" by the full snapshot)
  4290. // But this should be more unlikely as we'd generally capture the incremental snapshot right away
  4291. if (!isIncrementalEvent(event)) {
  4292. return;
  4293. }
  4294. const { source } = event.data;
  4295. if (source === IncrementalSource.Mutation) {
  4296. clickDetector.registerMutation(event.timestamp);
  4297. }
  4298. if (source === IncrementalSource.Scroll) {
  4299. clickDetector.registerScroll(event.timestamp);
  4300. }
  4301. if (isIncrementalMouseInteraction(event)) {
  4302. const { type, id } = event.data;
  4303. const node = record.mirror.getNode(id);
  4304. if (node instanceof HTMLElement && type === MouseInteractions.Click) {
  4305. clickDetector.registerClick(node);
  4306. }
  4307. }
  4308. } catch (e) {
  4309. // ignore errors here, e.g. if accessing something that does not exist
  4310. }
  4311. }
  4312. function isIncrementalEvent(event) {
  4313. return event.type === ReplayEventTypeIncrementalSnapshot;
  4314. }
  4315. function isIncrementalMouseInteraction(
  4316. event,
  4317. ) {
  4318. return event.data.source === IncrementalSource.MouseInteraction;
  4319. }
  4320. /**
  4321. * Create a breadcrumb for a replay.
  4322. */
  4323. function createBreadcrumb(
  4324. breadcrumb,
  4325. ) {
  4326. return {
  4327. timestamp: Date.now() / 1000,
  4328. type: 'default',
  4329. ...breadcrumb,
  4330. };
  4331. }
  4332. var NodeType;
  4333. (function (NodeType) {
  4334. NodeType[NodeType["Document"] = 0] = "Document";
  4335. NodeType[NodeType["DocumentType"] = 1] = "DocumentType";
  4336. NodeType[NodeType["Element"] = 2] = "Element";
  4337. NodeType[NodeType["Text"] = 3] = "Text";
  4338. NodeType[NodeType["CDATA"] = 4] = "CDATA";
  4339. NodeType[NodeType["Comment"] = 5] = "Comment";
  4340. })(NodeType || (NodeType = {}));
  4341. // Note that these are the serialized attributes and not attributes directly on
  4342. // the DOM Node. Attributes we are interested in:
  4343. const ATTRIBUTES_TO_RECORD = new Set([
  4344. 'id',
  4345. 'class',
  4346. 'aria-label',
  4347. 'role',
  4348. 'name',
  4349. 'alt',
  4350. 'title',
  4351. 'data-test-id',
  4352. 'data-testid',
  4353. 'disabled',
  4354. 'aria-disabled',
  4355. 'data-sentry-component',
  4356. ]);
  4357. /**
  4358. * Inclusion list of attributes that we want to record from the DOM element
  4359. */
  4360. function getAttributesToRecord(attributes) {
  4361. const obj = {};
  4362. for (const key in attributes) {
  4363. if (ATTRIBUTES_TO_RECORD.has(key)) {
  4364. let normalizedKey = key;
  4365. if (key === 'data-testid' || key === 'data-test-id') {
  4366. normalizedKey = 'testId';
  4367. }
  4368. obj[normalizedKey] = attributes[key];
  4369. }
  4370. }
  4371. return obj;
  4372. }
  4373. const handleDomListener = (
  4374. replay,
  4375. ) => {
  4376. return (handlerData) => {
  4377. if (!replay.isEnabled()) {
  4378. return;
  4379. }
  4380. const result = handleDom(handlerData);
  4381. if (!result) {
  4382. return;
  4383. }
  4384. const isClick = handlerData.name === 'click';
  4385. const event = isClick ? (handlerData.event ) : undefined;
  4386. // Ignore clicks if ctrl/alt/meta/shift keys are held down as they alter behavior of clicks (e.g. open in new tab)
  4387. if (
  4388. isClick &&
  4389. replay.clickDetector &&
  4390. event &&
  4391. event.target &&
  4392. !event.altKey &&
  4393. !event.metaKey &&
  4394. !event.ctrlKey &&
  4395. !event.shiftKey
  4396. ) {
  4397. handleClick(
  4398. replay.clickDetector,
  4399. result ,
  4400. getClickTargetNode(handlerData.event ) ,
  4401. );
  4402. }
  4403. addBreadcrumbEvent(replay, result);
  4404. };
  4405. };
  4406. /** Get the base DOM breadcrumb. */
  4407. function getBaseDomBreadcrumb(target, message) {
  4408. const nodeId = record.mirror.getId(target);
  4409. const node = nodeId && record.mirror.getNode(nodeId);
  4410. const meta = node && record.mirror.getMeta(node);
  4411. const element = meta && isElement(meta) ? meta : null;
  4412. return {
  4413. message,
  4414. data: element
  4415. ? {
  4416. nodeId,
  4417. node: {
  4418. id: nodeId,
  4419. tagName: element.tagName,
  4420. textContent: Array.from(element.childNodes)
  4421. .map((node) => node.type === NodeType.Text && node.textContent)
  4422. .filter(Boolean) // filter out empty values
  4423. .map(text => (text ).trim())
  4424. .join(''),
  4425. attributes: getAttributesToRecord(element.attributes),
  4426. },
  4427. }
  4428. : {},
  4429. };
  4430. }
  4431. /**
  4432. * An event handler to react to DOM events.
  4433. * Exported for tests.
  4434. */
  4435. function handleDom(handlerData) {
  4436. const { target, message } = getDomTarget(handlerData);
  4437. return createBreadcrumb({
  4438. category: `ui.${handlerData.name}`,
  4439. ...getBaseDomBreadcrumb(target, message),
  4440. });
  4441. }
  4442. function getDomTarget(handlerData) {
  4443. const isClick = handlerData.name === 'click';
  4444. let message;
  4445. let target = null;
  4446. // Accessing event.target can throw (see getsentry/raven-js#838, #768)
  4447. try {
  4448. target = isClick ? getClickTargetNode(handlerData.event ) : getTargetNode(handlerData.event );
  4449. message = utils.htmlTreeAsString(target, { maxStringLength: 200 }) || '<unknown>';
  4450. } catch (e) {
  4451. message = '<unknown>';
  4452. }
  4453. return { target, message };
  4454. }
  4455. function isElement(node) {
  4456. return node.type === NodeType.Element;
  4457. }
  4458. /** Handle keyboard events & create breadcrumbs. */
  4459. function handleKeyboardEvent(replay, event) {
  4460. if (!replay.isEnabled()) {
  4461. return;
  4462. }
  4463. // Update user activity, but do not restart recording as it can create
  4464. // noisy/low-value replays (e.g. user comes back from idle, hits alt-tab, new
  4465. // session with a single "keydown" breadcrumb is created)
  4466. replay.updateUserActivity();
  4467. const breadcrumb = getKeyboardBreadcrumb(event);
  4468. if (!breadcrumb) {
  4469. return;
  4470. }
  4471. addBreadcrumbEvent(replay, breadcrumb);
  4472. }
  4473. /** exported only for tests */
  4474. function getKeyboardBreadcrumb(event) {
  4475. const { metaKey, shiftKey, ctrlKey, altKey, key, target } = event;
  4476. // never capture for input fields
  4477. if (!target || isInputElement(target ) || !key) {
  4478. return null;
  4479. }
  4480. // Note: We do not consider shift here, as that means "uppercase"
  4481. const hasModifierKey = metaKey || ctrlKey || altKey;
  4482. const isCharacterKey = key.length === 1; // other keys like Escape, Tab, etc have a longer length
  4483. // Do not capture breadcrumb if only a word key is pressed
  4484. // This could leak e.g. user input
  4485. if (!hasModifierKey && isCharacterKey) {
  4486. return null;
  4487. }
  4488. const message = utils.htmlTreeAsString(target, { maxStringLength: 200 }) || '<unknown>';
  4489. const baseBreadcrumb = getBaseDomBreadcrumb(target , message);
  4490. return createBreadcrumb({
  4491. category: 'ui.keyDown',
  4492. message,
  4493. data: {
  4494. ...baseBreadcrumb.data,
  4495. metaKey,
  4496. shiftKey,
  4497. ctrlKey,
  4498. altKey,
  4499. key,
  4500. },
  4501. });
  4502. }
  4503. function isInputElement(target) {
  4504. return target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable;
  4505. }
  4506. // Map entryType -> function to normalize data for event
  4507. const ENTRY_TYPES
  4508. = {
  4509. // @ts-expect-error TODO: entry type does not fit the create* functions entry type
  4510. resource: createResourceEntry,
  4511. paint: createPaintEntry,
  4512. // @ts-expect-error TODO: entry type does not fit the create* functions entry type
  4513. navigation: createNavigationEntry,
  4514. };
  4515. /**
  4516. * Create replay performance entries from the browser performance entries.
  4517. */
  4518. function createPerformanceEntries(
  4519. entries,
  4520. ) {
  4521. return entries.map(createPerformanceEntry).filter(Boolean) ;
  4522. }
  4523. function createPerformanceEntry(entry) {
  4524. if (!ENTRY_TYPES[entry.entryType]) {
  4525. return null;
  4526. }
  4527. return ENTRY_TYPES[entry.entryType](entry);
  4528. }
  4529. function getAbsoluteTime(time) {
  4530. // browserPerformanceTimeOrigin can be undefined if `performance` or
  4531. // `performance.now` doesn't exist, but this is already checked by this integration
  4532. return ((utils.browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000;
  4533. }
  4534. function createPaintEntry(entry) {
  4535. const { duration, entryType, name, startTime } = entry;
  4536. const start = getAbsoluteTime(startTime);
  4537. return {
  4538. type: entryType,
  4539. name,
  4540. start,
  4541. end: start + duration,
  4542. data: undefined,
  4543. };
  4544. }
  4545. function createNavigationEntry(entry) {
  4546. const {
  4547. entryType,
  4548. name,
  4549. decodedBodySize,
  4550. duration,
  4551. domComplete,
  4552. encodedBodySize,
  4553. domContentLoadedEventStart,
  4554. domContentLoadedEventEnd,
  4555. domInteractive,
  4556. loadEventStart,
  4557. loadEventEnd,
  4558. redirectCount,
  4559. startTime,
  4560. transferSize,
  4561. type,
  4562. } = entry;
  4563. // Ignore entries with no duration, they do not seem to be useful and cause dupes
  4564. if (duration === 0) {
  4565. return null;
  4566. }
  4567. return {
  4568. type: `${entryType}.${type}`,
  4569. start: getAbsoluteTime(startTime),
  4570. end: getAbsoluteTime(domComplete),
  4571. name,
  4572. data: {
  4573. size: transferSize,
  4574. decodedBodySize,
  4575. encodedBodySize,
  4576. duration,
  4577. domInteractive,
  4578. domContentLoadedEventStart,
  4579. domContentLoadedEventEnd,
  4580. loadEventStart,
  4581. loadEventEnd,
  4582. domComplete,
  4583. redirectCount,
  4584. },
  4585. };
  4586. }
  4587. function createResourceEntry(
  4588. entry,
  4589. ) {
  4590. const {
  4591. entryType,
  4592. initiatorType,
  4593. name,
  4594. responseEnd,
  4595. startTime,
  4596. decodedBodySize,
  4597. encodedBodySize,
  4598. responseStatus,
  4599. transferSize,
  4600. } = entry;
  4601. // Core SDK handles these
  4602. if (['fetch', 'xmlhttprequest'].includes(initiatorType)) {
  4603. return null;
  4604. }
  4605. return {
  4606. type: `${entryType}.${initiatorType}`,
  4607. start: getAbsoluteTime(startTime),
  4608. end: getAbsoluteTime(responseEnd),
  4609. name,
  4610. data: {
  4611. size: transferSize,
  4612. statusCode: responseStatus,
  4613. decodedBodySize,
  4614. encodedBodySize,
  4615. },
  4616. };
  4617. }
  4618. /**
  4619. * Add a LCP event to the replay based on an LCP metric.
  4620. */
  4621. function getLargestContentfulPaint(metric
  4622. ) {
  4623. const entries = metric.entries;
  4624. const lastEntry = entries[entries.length - 1] ;
  4625. const element = lastEntry ? lastEntry.element : undefined;
  4626. const value = metric.value;
  4627. const end = getAbsoluteTime(value);
  4628. const data = {
  4629. type: 'largest-contentful-paint',
  4630. name: 'largest-contentful-paint',
  4631. start: end,
  4632. end,
  4633. data: {
  4634. value,
  4635. size: value,
  4636. nodeId: element ? record.mirror.getId(element) : undefined,
  4637. },
  4638. };
  4639. return data;
  4640. }
  4641. /**
  4642. * Sets up a PerformanceObserver to listen to all performance entry types.
  4643. * Returns a callback to stop observing.
  4644. */
  4645. function setupPerformanceObserver(replay) {
  4646. function addPerformanceEntry(entry) {
  4647. // It is possible for entries to come up multiple times
  4648. if (!replay.performanceEntries.includes(entry)) {
  4649. replay.performanceEntries.push(entry);
  4650. }
  4651. }
  4652. function onEntries({ entries }) {
  4653. entries.forEach(addPerformanceEntry);
  4654. }
  4655. const clearCallbacks = [];
  4656. (['navigation', 'paint', 'resource'] ).forEach(type => {
  4657. clearCallbacks.push(tracing.addPerformanceInstrumentationHandler(type, onEntries));
  4658. });
  4659. clearCallbacks.push(
  4660. tracing.addLcpInstrumentationHandler(({ metric }) => {
  4661. replay.replayPerformanceEntries.push(getLargestContentfulPaint(metric));
  4662. }),
  4663. );
  4664. // A callback to cleanup all handlers
  4665. return () => {
  4666. clearCallbacks.forEach(clearCallback => clearCallback());
  4667. };
  4668. }
  4669. /**
  4670. * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
  4671. *
  4672. * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking.
  4673. */
  4674. const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
  4675. const r = `var t=Uint8Array,n=Uint16Array,r=Int32Array,e=new t([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),i=new t([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),a=new t([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),s=function(t,e){for(var i=new n(31),a=0;a<31;++a)i[a]=e+=1<<t[a-1];var s=new r(i[30]);for(a=1;a<30;++a)for(var o=i[a];o<i[a+1];++o)s[o]=o-i[a]<<5|a;return{b:i,r:s}},o=s(e,2),f=o.b,h=o.r;f[28]=258,h[258]=28;for(var l=s(i,0).r,u=new n(32768),c=0;c<32768;++c){var v=(43690&c)>>1|(21845&c)<<1;v=(61680&(v=(52428&v)>>2|(13107&v)<<2))>>4|(3855&v)<<4,u[c]=((65280&v)>>8|(255&v)<<8)>>1}var d=function(t,r,e){for(var i=t.length,a=0,s=new n(r);a<i;++a)t[a]&&++s[t[a]-1];var o,f=new n(r);for(a=1;a<r;++a)f[a]=f[a-1]+s[a-1]<<1;if(e){o=new n(1<<r);var h=15-r;for(a=0;a<i;++a)if(t[a])for(var l=a<<4|t[a],c=r-t[a],v=f[t[a]-1]++<<c,d=v|(1<<c)-1;v<=d;++v)o[u[v]>>h]=l}else for(o=new n(i),a=0;a<i;++a)t[a]&&(o[a]=u[f[t[a]-1]++]>>15-t[a]);return o},g=new t(288);for(c=0;c<144;++c)g[c]=8;for(c=144;c<256;++c)g[c]=9;for(c=256;c<280;++c)g[c]=7;for(c=280;c<288;++c)g[c]=8;var w=new t(32);for(c=0;c<32;++c)w[c]=5;var p=d(g,9,0),y=d(w,5,0),m=function(t){return(t+7)/8|0},b=function(n,r,e){return(null==r||r<0)&&(r=0),(null==e||e>n.length)&&(e=n.length),new t(n.subarray(r,e))},M=["unexpected EOF","invalid block type","invalid length/literal","invalid distance","stream finished","no stream handler",,"no callback","invalid UTF-8 data","extra field too long","date not in range 1980-2099","filename too long","stream finishing","invalid zip data"],E=function(t,n,r){var e=new Error(n||M[t]);if(e.code=t,Error.captureStackTrace&&Error.captureStackTrace(e,E),!r)throw e;return e},z=function(t,n,r){r<<=7&n;var e=n/8|0;t[e]|=r,t[e+1]|=r>>8},A=function(t,n,r){r<<=7&n;var e=n/8|0;t[e]|=r,t[e+1]|=r>>8,t[e+2]|=r>>16},_=function(r,e){for(var i=[],a=0;a<r.length;++a)r[a]&&i.push({s:a,f:r[a]});var s=i.length,o=i.slice();if(!s)return{t:F,l:0};if(1==s){var f=new t(i[0].s+1);return f[i[0].s]=1,{t:f,l:1}}i.sort((function(t,n){return t.f-n.f})),i.push({s:-1,f:25001});var h=i[0],l=i[1],u=0,c=1,v=2;for(i[0]={s:-1,f:h.f+l.f,l:h,r:l};c!=s-1;)h=i[i[u].f<i[v].f?u++:v++],l=i[u!=c&&i[u].f<i[v].f?u++:v++],i[c++]={s:-1,f:h.f+l.f,l:h,r:l};var d=o[0].s;for(a=1;a<s;++a)o[a].s>d&&(d=o[a].s);var g=new n(d+1),w=x(i[c-1],g,0);if(w>e){a=0;var p=0,y=w-e,m=1<<y;for(o.sort((function(t,n){return g[n.s]-g[t.s]||t.f-n.f}));a<s;++a){var b=o[a].s;if(!(g[b]>e))break;p+=m-(1<<w-g[b]),g[b]=e}for(p>>=y;p>0;){var M=o[a].s;g[M]<e?p-=1<<e-g[M]++-1:++a}for(;a>=0&&p;--a){var E=o[a].s;g[E]==e&&(--g[E],++p)}w=e}return{t:new t(g),l:w}},x=function(t,n,r){return-1==t.s?Math.max(x(t.l,n,r+1),x(t.r,n,r+1)):n[t.s]=r},D=function(t){for(var r=t.length;r&&!t[--r];);for(var e=new n(++r),i=0,a=t[0],s=1,o=function(t){e[i++]=t},f=1;f<=r;++f)if(t[f]==a&&f!=r)++s;else{if(!a&&s>2){for(;s>138;s-=138)o(32754);s>2&&(o(s>10?s-11<<5|28690:s-3<<5|12305),s=0)}else if(s>3){for(o(a),--s;s>6;s-=6)o(8304);s>2&&(o(s-3<<5|8208),s=0)}for(;s--;)o(a);s=1,a=t[f]}return{c:e.subarray(0,i),n:r}},T=function(t,n){for(var r=0,e=0;e<n.length;++e)r+=t[e]*n[e];return r},k=function(t,n,r){var e=r.length,i=m(n+2);t[i]=255&e,t[i+1]=e>>8,t[i+2]=255^t[i],t[i+3]=255^t[i+1];for(var a=0;a<e;++a)t[i+a+4]=r[a];return 8*(i+4+e)},C=function(t,r,s,o,f,h,l,u,c,v,m){z(r,m++,s),++f[256];for(var b=_(f,15),M=b.t,E=b.l,x=_(h,15),C=x.t,U=x.l,F=D(M),I=F.c,S=F.n,L=D(C),O=L.c,j=L.n,q=new n(19),B=0;B<I.length;++B)++q[31&I[B]];for(B=0;B<O.length;++B)++q[31&O[B]];for(var G=_(q,7),H=G.t,J=G.l,K=19;K>4&&!H[a[K-1]];--K);var N,P,Q,R,V=v+5<<3,W=T(f,g)+T(h,w)+l,X=T(f,M)+T(h,C)+l+14+3*K+T(q,H)+2*q[16]+3*q[17]+7*q[18];if(c>=0&&V<=W&&V<=X)return k(r,m,t.subarray(c,c+v));if(z(r,m,1+(X<W)),m+=2,X<W){N=d(M,E,0),P=M,Q=d(C,U,0),R=C;var Y=d(H,J,0);z(r,m,S-257),z(r,m+5,j-1),z(r,m+10,K-4),m+=14;for(B=0;B<K;++B)z(r,m+3*B,H[a[B]]);m+=3*K;for(var Z=[I,O],$=0;$<2;++$){var tt=Z[$];for(B=0;B<tt.length;++B){var nt=31&tt[B];z(r,m,Y[nt]),m+=H[nt],nt>15&&(z(r,m,tt[B]>>5&127),m+=tt[B]>>12)}}}else N=p,P=g,Q=y,R=w;for(B=0;B<u;++B){var rt=o[B];if(rt>255){A(r,m,N[(nt=rt>>18&31)+257]),m+=P[nt+257],nt>7&&(z(r,m,rt>>23&31),m+=e[nt]);var et=31&rt;A(r,m,Q[et]),m+=R[et],et>3&&(A(r,m,rt>>5&8191),m+=i[et])}else A(r,m,N[rt]),m+=P[rt]}return A(r,m,N[256]),m+P[256]},U=new r([65540,131080,131088,131104,262176,1048704,1048832,2114560,2117632]),F=new t(0),I=function(){for(var t=new Int32Array(256),n=0;n<256;++n){for(var r=n,e=9;--e;)r=(1&r&&-306674912)^r>>>1;t[n]=r}return t}(),S=function(){var t=1,n=0;return{p:function(r){for(var e=t,i=n,a=0|r.length,s=0;s!=a;){for(var o=Math.min(s+2655,a);s<o;++s)i+=e+=r[s];e=(65535&e)+15*(e>>16),i=(65535&i)+15*(i>>16)}t=e,n=i},d:function(){return(255&(t%=65521))<<24|(65280&t)<<8|(255&(n%=65521))<<8|n>>8}}},L=function(a,s,o,f,u){if(!u&&(u={l:1},s.dictionary)){var c=s.dictionary.subarray(-32768),v=new t(c.length+a.length);v.set(c),v.set(a,c.length),a=v,u.w=c.length}return function(a,s,o,f,u,c){var v=c.z||a.length,d=new t(f+v+5*(1+Math.ceil(v/7e3))+u),g=d.subarray(f,d.length-u),w=c.l,p=7&(c.r||0);if(s){p&&(g[0]=c.r>>3);for(var y=U[s-1],M=y>>13,E=8191&y,z=(1<<o)-1,A=c.p||new n(32768),_=c.h||new n(z+1),x=Math.ceil(o/3),D=2*x,T=function(t){return(a[t]^a[t+1]<<x^a[t+2]<<D)&z},F=new r(25e3),I=new n(288),S=new n(32),L=0,O=0,j=c.i||0,q=0,B=c.w||0,G=0;j+2<v;++j){var H=T(j),J=32767&j,K=_[H];if(A[J]=K,_[H]=J,B<=j){var N=v-j;if((L>7e3||q>24576)&&(N>423||!w)){p=C(a,g,0,F,I,S,O,q,G,j-G,p),q=L=O=0,G=j;for(var P=0;P<286;++P)I[P]=0;for(P=0;P<30;++P)S[P]=0}var Q=2,R=0,V=E,W=J-K&32767;if(N>2&&H==T(j-W))for(var X=Math.min(M,N)-1,Y=Math.min(32767,j),Z=Math.min(258,N);W<=Y&&--V&&J!=K;){if(a[j+Q]==a[j+Q-W]){for(var $=0;$<Z&&a[j+$]==a[j+$-W];++$);if($>Q){if(Q=$,R=W,$>X)break;var tt=Math.min(W,$-2),nt=0;for(P=0;P<tt;++P){var rt=j-W+P&32767,et=rt-A[rt]&32767;et>nt&&(nt=et,K=rt)}}}W+=(J=K)-(K=A[J])&32767}if(R){F[q++]=268435456|h[Q]<<18|l[R];var it=31&h[Q],at=31&l[R];O+=e[it]+i[at],++I[257+it],++S[at],B=j+Q,++L}else F[q++]=a[j],++I[a[j]]}}for(j=Math.max(j,B);j<v;++j)F[q++]=a[j],++I[a[j]];p=C(a,g,w,F,I,S,O,q,G,j-G,p),w||(c.r=7&p|g[p/8|0]<<3,p-=7,c.h=_,c.p=A,c.i=j,c.w=B)}else{for(j=c.w||0;j<v+w;j+=65535){var st=j+65535;st>=v&&(g[p/8|0]=w,st=v),p=k(g,p+1,a.subarray(j,st))}c.i=v}return b(d,0,f+m(p)+u)}(a,null==s.level?6:s.level,null==s.mem?Math.ceil(1.5*Math.max(8,Math.min(13,Math.log(a.length)))):12+s.mem,o,f,u)},O=function(t,n,r){for(;r;++n)t[n]=r,r>>>=8},j=function(){function n(n,r){if("function"==typeof n&&(r=n,n={}),this.ondata=r,this.o=n||{},this.s={l:0,i:32768,w:32768,z:32768},this.b=new t(98304),this.o.dictionary){var e=this.o.dictionary.subarray(-32768);this.b.set(e,32768-e.length),this.s.i=32768-e.length}}return n.prototype.p=function(t,n){this.ondata(L(t,this.o,0,0,this.s),n)},n.prototype.push=function(n,r){this.ondata||E(5),this.s.l&&E(4);var e=n.length+this.s.z;if(e>this.b.length){if(e>2*this.b.length-32768){var i=new t(-32768&e);i.set(this.b.subarray(0,this.s.z)),this.b=i}var a=this.b.length-this.s.z;a&&(this.b.set(n.subarray(0,a),this.s.z),this.s.z=this.b.length,this.p(this.b,!1)),this.b.set(this.b.subarray(-32768)),this.b.set(n.subarray(a),32768),this.s.z=n.length-a+32768,this.s.i=32766,this.s.w=32768}else this.b.set(n,this.s.z),this.s.z+=n.length;this.s.l=1&r,(this.s.z>this.s.w+8191||r)&&(this.p(this.b,r||!1),this.s.w=this.s.i,this.s.i-=2)},n}();function q(t,n){n||(n={});var r=function(){var t=-1;return{p:function(n){for(var r=t,e=0;e<n.length;++e)r=I[255&r^n[e]]^r>>>8;t=r},d:function(){return~t}}}(),e=t.length;r.p(t);var i,a=L(t,n,10+((i=n).filename?i.filename.length+1:0),8),s=a.length;return function(t,n){var r=n.filename;if(t[0]=31,t[1]=139,t[2]=8,t[8]=n.level<2?4:9==n.level?2:0,t[9]=3,0!=n.mtime&&O(t,4,Math.floor(new Date(n.mtime||Date.now())/1e3)),r){t[3]=8;for(var e=0;e<=r.length;++e)t[e+10]=r.charCodeAt(e)}}(a,n),O(a,s-8,r.d()),O(a,s-4,e),a}var B=function(){function t(t,n){this.c=S(),this.v=1,j.call(this,t,n)}return t.prototype.push=function(t,n){this.c.p(t),j.prototype.push.call(this,t,n)},t.prototype.p=function(t,n){var r=L(t,this.o,this.v&&(this.o.dictionary?6:2),n&&4,this.s);this.v&&(function(t,n){var r=n.level,e=0==r?0:r<6?1:9==r?3:2;if(t[0]=120,t[1]=e<<6|(n.dictionary&&32),t[1]|=31-(t[0]<<8|t[1])%31,n.dictionary){var i=S();i.p(n.dictionary),O(t,2,i.d())}}(r,this.o),this.v=0),n&&O(r,r.length-4,this.c.d()),this.ondata(r,n)},t}(),G="undefined"!=typeof TextEncoder&&new TextEncoder,H="undefined"!=typeof TextDecoder&&new TextDecoder;try{H.decode(F,{stream:!0})}catch(t){}var J=function(){function t(t){this.ondata=t}return t.prototype.push=function(t,n){this.ondata||E(5),this.d&&E(4),this.ondata(K(t),this.d=n||!1)},t}();function K(n,r){if(r){for(var e=new t(n.length),i=0;i<n.length;++i)e[i]=n.charCodeAt(i);return e}if(G)return G.encode(n);var a=n.length,s=new t(n.length+(n.length>>1)),o=0,f=function(t){s[o++]=t};for(i=0;i<a;++i){if(o+5>s.length){var h=new t(o+8+(a-i<<1));h.set(s),s=h}var l=n.charCodeAt(i);l<128||r?f(l):l<2048?(f(192|l>>6),f(128|63&l)):l>55295&&l<57344?(f(240|(l=65536+(1047552&l)|1023&n.charCodeAt(++i))>>18),f(128|l>>12&63),f(128|l>>6&63),f(128|63&l)):(f(224|l>>12),f(128|l>>6&63),f(128|63&l))}return b(s,0,o)}const N=new class{constructor(){this._init()}clear(){this._init()}addEvent(t){if(!t)throw new Error("Adding invalid event");const n=this._hasEvents?",":"";this.stream.push(n+t),this._hasEvents=!0}finish(){this.stream.push("]",!0);const t=function(t){let n=0;for(let r=0,e=t.length;r<e;r++)n+=t[r].length;const r=new Uint8Array(n);for(let n=0,e=0,i=t.length;n<i;n++){const i=t[n];r.set(i,e),e+=i.length}return r}(this._deflatedData);return this._init(),t}_init(){this._hasEvents=!1,this._deflatedData=[],this.deflate=new B,this.deflate.ondata=(t,n)=>{this._deflatedData.push(t)},this.stream=new J(((t,n)=>{this.deflate.push(t,n)})),this.stream.push("[")}},P={clear:()=>{N.clear()},addEvent:t=>N.addEvent(t),finish:()=>N.finish(),compress:t=>function(t){return q(K(t))}(t)};addEventListener("message",(function(t){const n=t.data.method,r=t.data.id,e=t.data.arg;if(n in P&&"function"==typeof P[n])try{const t=P[n](e);postMessage({id:r,method:n,success:!0,response:t})}catch(t){postMessage({id:r,method:n,success:!1,response:t.message}),console.error(t)}})),postMessage({id:void 0,method:"init",success:!0,response:void 0});`;
  4676. function e(){const e=new Blob([r]);return URL.createObjectURL(e)}
  4677. /**
  4678. * Log a message in debug mode, and add a breadcrumb when _experiment.traceInternals is enabled.
  4679. */
  4680. function logInfo(message, shouldAddBreadcrumb) {
  4681. if (!DEBUG_BUILD) {
  4682. return;
  4683. }
  4684. utils.logger.info(message);
  4685. if (shouldAddBreadcrumb) {
  4686. addLogBreadcrumb(message);
  4687. }
  4688. }
  4689. /**
  4690. * Log a message, and add a breadcrumb in the next tick.
  4691. * This is necessary when the breadcrumb may be added before the replay is initialized.
  4692. */
  4693. function logInfoNextTick(message, shouldAddBreadcrumb) {
  4694. if (!DEBUG_BUILD) {
  4695. return;
  4696. }
  4697. utils.logger.info(message);
  4698. if (shouldAddBreadcrumb) {
  4699. // Wait a tick here to avoid race conditions for some initial logs
  4700. // which may be added before replay is initialized
  4701. setTimeout(() => {
  4702. addLogBreadcrumb(message);
  4703. }, 0);
  4704. }
  4705. }
  4706. function addLogBreadcrumb(message) {
  4707. core.addBreadcrumb(
  4708. {
  4709. category: 'console',
  4710. data: {
  4711. logger: 'replay',
  4712. },
  4713. level: 'info',
  4714. message,
  4715. },
  4716. { level: 'info' },
  4717. );
  4718. }
  4719. /** This error indicates that the event buffer size exceeded the limit.. */
  4720. class EventBufferSizeExceededError extends Error {
  4721. constructor() {
  4722. super(`Event buffer exceeded maximum size of ${REPLAY_MAX_EVENT_BUFFER_SIZE}.`);
  4723. }
  4724. }
  4725. /**
  4726. * A basic event buffer that does not do any compression.
  4727. * Used as fallback if the compression worker cannot be loaded or is disabled.
  4728. */
  4729. class EventBufferArray {
  4730. /** All the events that are buffered to be sent. */
  4731. /** @inheritdoc */
  4732. constructor() {
  4733. this.events = [];
  4734. this._totalSize = 0;
  4735. this.hasCheckout = false;
  4736. }
  4737. /** @inheritdoc */
  4738. get hasEvents() {
  4739. return this.events.length > 0;
  4740. }
  4741. /** @inheritdoc */
  4742. get type() {
  4743. return 'sync';
  4744. }
  4745. /** @inheritdoc */
  4746. destroy() {
  4747. this.events = [];
  4748. }
  4749. /** @inheritdoc */
  4750. async addEvent(event) {
  4751. const eventSize = JSON.stringify(event).length;
  4752. this._totalSize += eventSize;
  4753. if (this._totalSize > REPLAY_MAX_EVENT_BUFFER_SIZE) {
  4754. throw new EventBufferSizeExceededError();
  4755. }
  4756. this.events.push(event);
  4757. }
  4758. /** @inheritdoc */
  4759. finish() {
  4760. return new Promise(resolve => {
  4761. // Make a copy of the events array reference and immediately clear the
  4762. // events member so that we do not lose new events while uploading
  4763. // attachment.
  4764. const eventsRet = this.events;
  4765. this.clear();
  4766. resolve(JSON.stringify(eventsRet));
  4767. });
  4768. }
  4769. /** @inheritdoc */
  4770. clear() {
  4771. this.events = [];
  4772. this._totalSize = 0;
  4773. this.hasCheckout = false;
  4774. }
  4775. /** @inheritdoc */
  4776. getEarliestTimestamp() {
  4777. const timestamp = this.events.map(event => event.timestamp).sort()[0];
  4778. if (!timestamp) {
  4779. return null;
  4780. }
  4781. return timestampToMs(timestamp);
  4782. }
  4783. }
  4784. /**
  4785. * Event buffer that uses a web worker to compress events.
  4786. * Exported only for testing.
  4787. */
  4788. class WorkerHandler {
  4789. constructor(worker) {
  4790. this._worker = worker;
  4791. this._id = 0;
  4792. }
  4793. /**
  4794. * Ensure the worker is ready (or not).
  4795. * This will either resolve when the worker is ready, or reject if an error occured.
  4796. */
  4797. ensureReady() {
  4798. // Ensure we only check once
  4799. if (this._ensureReadyPromise) {
  4800. return this._ensureReadyPromise;
  4801. }
  4802. this._ensureReadyPromise = new Promise((resolve, reject) => {
  4803. this._worker.addEventListener(
  4804. 'message',
  4805. ({ data }) => {
  4806. if ((data ).success) {
  4807. resolve();
  4808. } else {
  4809. reject();
  4810. }
  4811. },
  4812. { once: true },
  4813. );
  4814. this._worker.addEventListener(
  4815. 'error',
  4816. error => {
  4817. reject(error);
  4818. },
  4819. { once: true },
  4820. );
  4821. });
  4822. return this._ensureReadyPromise;
  4823. }
  4824. /**
  4825. * Destroy the worker.
  4826. */
  4827. destroy() {
  4828. logInfo('[Replay] Destroying compression worker');
  4829. this._worker.terminate();
  4830. }
  4831. /**
  4832. * Post message to worker and wait for response before resolving promise.
  4833. */
  4834. postMessage(method, arg) {
  4835. const id = this._getAndIncrementId();
  4836. return new Promise((resolve, reject) => {
  4837. const listener = ({ data }) => {
  4838. const response = data ;
  4839. if (response.method !== method) {
  4840. return;
  4841. }
  4842. // There can be multiple listeners for a single method, the id ensures
  4843. // that the response matches the caller.
  4844. if (response.id !== id) {
  4845. return;
  4846. }
  4847. // At this point, we'll always want to remove listener regardless of result status
  4848. this._worker.removeEventListener('message', listener);
  4849. if (!response.success) {
  4850. // TODO: Do some error handling, not sure what
  4851. DEBUG_BUILD && utils.logger.error('[Replay]', response.response);
  4852. reject(new Error('Error in compression worker'));
  4853. return;
  4854. }
  4855. resolve(response.response );
  4856. };
  4857. // Note: we can't use `once` option because it's possible it needs to
  4858. // listen to multiple messages
  4859. this._worker.addEventListener('message', listener);
  4860. this._worker.postMessage({ id, method, arg });
  4861. });
  4862. }
  4863. /** Get the current ID and increment it for the next call. */
  4864. _getAndIncrementId() {
  4865. return this._id++;
  4866. }
  4867. }
  4868. /**
  4869. * Event buffer that uses a web worker to compress events.
  4870. * Exported only for testing.
  4871. */
  4872. class EventBufferCompressionWorker {
  4873. /** @inheritdoc */
  4874. constructor(worker) {
  4875. this._worker = new WorkerHandler(worker);
  4876. this._earliestTimestamp = null;
  4877. this._totalSize = 0;
  4878. this.hasCheckout = false;
  4879. }
  4880. /** @inheritdoc */
  4881. get hasEvents() {
  4882. return !!this._earliestTimestamp;
  4883. }
  4884. /** @inheritdoc */
  4885. get type() {
  4886. return 'worker';
  4887. }
  4888. /**
  4889. * Ensure the worker is ready (or not).
  4890. * This will either resolve when the worker is ready, or reject if an error occured.
  4891. */
  4892. ensureReady() {
  4893. return this._worker.ensureReady();
  4894. }
  4895. /**
  4896. * Destroy the event buffer.
  4897. */
  4898. destroy() {
  4899. this._worker.destroy();
  4900. }
  4901. /**
  4902. * Add an event to the event buffer.
  4903. *
  4904. * Returns true if event was successfuly received and processed by worker.
  4905. */
  4906. addEvent(event) {
  4907. const timestamp = timestampToMs(event.timestamp);
  4908. if (!this._earliestTimestamp || timestamp < this._earliestTimestamp) {
  4909. this._earliestTimestamp = timestamp;
  4910. }
  4911. const data = JSON.stringify(event);
  4912. this._totalSize += data.length;
  4913. if (this._totalSize > REPLAY_MAX_EVENT_BUFFER_SIZE) {
  4914. return Promise.reject(new EventBufferSizeExceededError());
  4915. }
  4916. return this._sendEventToWorker(data);
  4917. }
  4918. /**
  4919. * Finish the event buffer and return the compressed data.
  4920. */
  4921. finish() {
  4922. return this._finishRequest();
  4923. }
  4924. /** @inheritdoc */
  4925. clear() {
  4926. this._earliestTimestamp = null;
  4927. this._totalSize = 0;
  4928. this.hasCheckout = false;
  4929. // We do not wait on this, as we assume the order of messages is consistent for the worker
  4930. this._worker.postMessage('clear').then(null, e => {
  4931. DEBUG_BUILD && utils.logger.warn('[Replay] Sending "clear" message to worker failed', e);
  4932. });
  4933. }
  4934. /** @inheritdoc */
  4935. getEarliestTimestamp() {
  4936. return this._earliestTimestamp;
  4937. }
  4938. /**
  4939. * Send the event to the worker.
  4940. */
  4941. _sendEventToWorker(data) {
  4942. return this._worker.postMessage('addEvent', data);
  4943. }
  4944. /**
  4945. * Finish the request and return the compressed data from the worker.
  4946. */
  4947. async _finishRequest() {
  4948. const response = await this._worker.postMessage('finish');
  4949. this._earliestTimestamp = null;
  4950. this._totalSize = 0;
  4951. return response;
  4952. }
  4953. }
  4954. /**
  4955. * This proxy will try to use the compression worker, and fall back to use the simple buffer if an error occurs there.
  4956. * This can happen e.g. if the worker cannot be loaded.
  4957. * Exported only for testing.
  4958. */
  4959. class EventBufferProxy {
  4960. constructor(worker) {
  4961. this._fallback = new EventBufferArray();
  4962. this._compression = new EventBufferCompressionWorker(worker);
  4963. this._used = this._fallback;
  4964. this._ensureWorkerIsLoadedPromise = this._ensureWorkerIsLoaded();
  4965. }
  4966. /** @inheritdoc */
  4967. get type() {
  4968. return this._used.type;
  4969. }
  4970. /** @inheritDoc */
  4971. get hasEvents() {
  4972. return this._used.hasEvents;
  4973. }
  4974. /** @inheritdoc */
  4975. get hasCheckout() {
  4976. return this._used.hasCheckout;
  4977. }
  4978. /** @inheritdoc */
  4979. set hasCheckout(value) {
  4980. this._used.hasCheckout = value;
  4981. }
  4982. /** @inheritDoc */
  4983. destroy() {
  4984. this._fallback.destroy();
  4985. this._compression.destroy();
  4986. }
  4987. /** @inheritdoc */
  4988. clear() {
  4989. return this._used.clear();
  4990. }
  4991. /** @inheritdoc */
  4992. getEarliestTimestamp() {
  4993. return this._used.getEarliestTimestamp();
  4994. }
  4995. /**
  4996. * Add an event to the event buffer.
  4997. *
  4998. * Returns true if event was successfully added.
  4999. */
  5000. addEvent(event) {
  5001. return this._used.addEvent(event);
  5002. }
  5003. /** @inheritDoc */
  5004. async finish() {
  5005. // Ensure the worker is loaded, so the sent event is compressed
  5006. await this.ensureWorkerIsLoaded();
  5007. return this._used.finish();
  5008. }
  5009. /** Ensure the worker has loaded. */
  5010. ensureWorkerIsLoaded() {
  5011. return this._ensureWorkerIsLoadedPromise;
  5012. }
  5013. /** Actually check if the worker has been loaded. */
  5014. async _ensureWorkerIsLoaded() {
  5015. try {
  5016. await this._compression.ensureReady();
  5017. } catch (error) {
  5018. // If the worker fails to load, we fall back to the simple buffer.
  5019. // Nothing more to do from our side here
  5020. logInfo('[Replay] Failed to load the compression worker, falling back to simple buffer');
  5021. return;
  5022. }
  5023. // Now we need to switch over the array buffer to the compression worker
  5024. await this._switchToCompressionWorker();
  5025. }
  5026. /** Switch the used buffer to the compression worker. */
  5027. async _switchToCompressionWorker() {
  5028. const { events, hasCheckout } = this._fallback;
  5029. const addEventPromises = [];
  5030. for (const event of events) {
  5031. addEventPromises.push(this._compression.addEvent(event));
  5032. }
  5033. this._compression.hasCheckout = hasCheckout;
  5034. // We switch over to the new buffer immediately - any further events will be added
  5035. // after the previously buffered ones
  5036. this._used = this._compression;
  5037. // Wait for original events to be re-added before resolving
  5038. try {
  5039. await Promise.all(addEventPromises);
  5040. } catch (error) {
  5041. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to add events when switching buffers.', error);
  5042. }
  5043. }
  5044. }
  5045. /**
  5046. * Create an event buffer for replays.
  5047. */
  5048. function createEventBuffer({
  5049. useCompression,
  5050. workerUrl: customWorkerUrl,
  5051. }) {
  5052. if (
  5053. useCompression &&
  5054. // eslint-disable-next-line no-restricted-globals
  5055. window.Worker
  5056. ) {
  5057. const worker = _loadWorker(customWorkerUrl);
  5058. if (worker) {
  5059. return worker;
  5060. }
  5061. }
  5062. logInfo('[Replay] Using simple buffer');
  5063. return new EventBufferArray();
  5064. }
  5065. function _loadWorker(customWorkerUrl) {
  5066. try {
  5067. const workerUrl = customWorkerUrl || _getWorkerUrl();
  5068. if (!workerUrl) {
  5069. return;
  5070. }
  5071. logInfo(`[Replay] Using compression worker${customWorkerUrl ? ` from ${customWorkerUrl}` : ''}`);
  5072. const worker = new Worker(workerUrl);
  5073. return new EventBufferProxy(worker);
  5074. } catch (error) {
  5075. logInfo('[Replay] Failed to create compression worker');
  5076. // Fall back to use simple event buffer array
  5077. }
  5078. }
  5079. function _getWorkerUrl() {
  5080. if (typeof __SENTRY_EXCLUDE_REPLAY_WORKER__ === 'undefined' || !__SENTRY_EXCLUDE_REPLAY_WORKER__) {
  5081. return e();
  5082. }
  5083. return '';
  5084. }
  5085. /** If sessionStorage is available. */
  5086. function hasSessionStorage() {
  5087. try {
  5088. // This can throw, e.g. when being accessed in a sandboxed iframe
  5089. return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage;
  5090. } catch (e) {
  5091. return false;
  5092. }
  5093. }
  5094. /**
  5095. * Removes the session from Session Storage and unsets session in replay instance
  5096. */
  5097. function clearSession(replay) {
  5098. deleteSession();
  5099. replay.session = undefined;
  5100. }
  5101. /**
  5102. * Deletes a session from storage
  5103. */
  5104. function deleteSession() {
  5105. if (!hasSessionStorage()) {
  5106. return;
  5107. }
  5108. try {
  5109. WINDOW.sessionStorage.removeItem(REPLAY_SESSION_KEY);
  5110. } catch (e) {
  5111. // Ignore potential SecurityError exceptions
  5112. }
  5113. }
  5114. /**
  5115. * Given a sample rate, returns true if replay should be sampled.
  5116. *
  5117. * 1.0 = 100% sampling
  5118. * 0.0 = 0% sampling
  5119. */
  5120. function isSampled(sampleRate) {
  5121. if (sampleRate === undefined) {
  5122. return false;
  5123. }
  5124. // Math.random() returns a number in range of 0 to 1 (inclusive of 0, but not 1)
  5125. return Math.random() < sampleRate;
  5126. }
  5127. /**
  5128. * Get a session with defaults & applied sampling.
  5129. */
  5130. function makeSession(session) {
  5131. const now = Date.now();
  5132. const id = session.id || utils.uuid4();
  5133. // Note that this means we cannot set a started/lastActivity of `0`, but this should not be relevant outside of tests.
  5134. const started = session.started || now;
  5135. const lastActivity = session.lastActivity || now;
  5136. const segmentId = session.segmentId || 0;
  5137. const sampled = session.sampled;
  5138. const previousSessionId = session.previousSessionId;
  5139. return {
  5140. id,
  5141. started,
  5142. lastActivity,
  5143. segmentId,
  5144. sampled,
  5145. previousSessionId,
  5146. };
  5147. }
  5148. /**
  5149. * Save a session to session storage.
  5150. */
  5151. function saveSession(session) {
  5152. if (!hasSessionStorage()) {
  5153. return;
  5154. }
  5155. try {
  5156. WINDOW.sessionStorage.setItem(REPLAY_SESSION_KEY, JSON.stringify(session));
  5157. } catch (e) {
  5158. // Ignore potential SecurityError exceptions
  5159. }
  5160. }
  5161. /**
  5162. * Get the sampled status for a session based on sample rates & current sampled status.
  5163. */
  5164. function getSessionSampleType(sessionSampleRate, allowBuffering) {
  5165. return isSampled(sessionSampleRate) ? 'session' : allowBuffering ? 'buffer' : false;
  5166. }
  5167. /**
  5168. * Create a new session, which in its current implementation is a Sentry event
  5169. * that all replays will be saved to as attachments. Currently, we only expect
  5170. * one of these Sentry events per "replay session".
  5171. */
  5172. function createSession(
  5173. { sessionSampleRate, allowBuffering, stickySession = false },
  5174. { previousSessionId } = {},
  5175. ) {
  5176. const sampled = getSessionSampleType(sessionSampleRate, allowBuffering);
  5177. const session = makeSession({
  5178. sampled,
  5179. previousSessionId,
  5180. });
  5181. if (stickySession) {
  5182. saveSession(session);
  5183. }
  5184. return session;
  5185. }
  5186. /**
  5187. * Fetches a session from storage
  5188. */
  5189. function fetchSession(traceInternals) {
  5190. if (!hasSessionStorage()) {
  5191. return null;
  5192. }
  5193. try {
  5194. // This can throw if cookies are disabled
  5195. const sessionStringFromStorage = WINDOW.sessionStorage.getItem(REPLAY_SESSION_KEY);
  5196. if (!sessionStringFromStorage) {
  5197. return null;
  5198. }
  5199. const sessionObj = JSON.parse(sessionStringFromStorage) ;
  5200. logInfoNextTick('[Replay] Loading existing session', traceInternals);
  5201. return makeSession(sessionObj);
  5202. } catch (e) {
  5203. return null;
  5204. }
  5205. }
  5206. /**
  5207. * Given an initial timestamp and an expiry duration, checks to see if current
  5208. * time should be considered as expired.
  5209. */
  5210. function isExpired(
  5211. initialTime,
  5212. expiry,
  5213. targetTime = +new Date(),
  5214. ) {
  5215. // Always expired if < 0
  5216. if (initialTime === null || expiry === undefined || expiry < 0) {
  5217. return true;
  5218. }
  5219. // Never expires if == 0
  5220. if (expiry === 0) {
  5221. return false;
  5222. }
  5223. return initialTime + expiry <= targetTime;
  5224. }
  5225. /**
  5226. * Checks to see if session is expired
  5227. */
  5228. function isSessionExpired(
  5229. session,
  5230. {
  5231. maxReplayDuration,
  5232. sessionIdleExpire,
  5233. targetTime = Date.now(),
  5234. },
  5235. ) {
  5236. return (
  5237. // First, check that maximum session length has not been exceeded
  5238. isExpired(session.started, maxReplayDuration, targetTime) ||
  5239. // check that the idle timeout has not been exceeded (i.e. user has
  5240. // performed an action within the last `sessionIdleExpire` ms)
  5241. isExpired(session.lastActivity, sessionIdleExpire, targetTime)
  5242. );
  5243. }
  5244. /** If the session should be refreshed or not. */
  5245. function shouldRefreshSession(
  5246. session,
  5247. { sessionIdleExpire, maxReplayDuration },
  5248. ) {
  5249. // If not expired, all good, just keep the session
  5250. if (!isSessionExpired(session, { sessionIdleExpire, maxReplayDuration })) {
  5251. return false;
  5252. }
  5253. // If we are buffering & haven't ever flushed yet, always continue
  5254. if (session.sampled === 'buffer' && session.segmentId === 0) {
  5255. return false;
  5256. }
  5257. return true;
  5258. }
  5259. /**
  5260. * Get or create a session, when initializing the replay.
  5261. * Returns a session that may be unsampled.
  5262. */
  5263. function loadOrCreateSession(
  5264. {
  5265. traceInternals,
  5266. sessionIdleExpire,
  5267. maxReplayDuration,
  5268. previousSessionId,
  5269. }
  5270. ,
  5271. sessionOptions,
  5272. ) {
  5273. const existingSession = sessionOptions.stickySession && fetchSession(traceInternals);
  5274. // No session exists yet, just create a new one
  5275. if (!existingSession) {
  5276. logInfoNextTick('[Replay] Creating new session', traceInternals);
  5277. return createSession(sessionOptions, { previousSessionId });
  5278. }
  5279. if (!shouldRefreshSession(existingSession, { sessionIdleExpire, maxReplayDuration })) {
  5280. return existingSession;
  5281. }
  5282. logInfoNextTick('[Replay] Session in sessionStorage is expired, creating new one...');
  5283. return createSession(sessionOptions, { previousSessionId: existingSession.id });
  5284. }
  5285. function isCustomEvent(event) {
  5286. return event.type === EventType.Custom;
  5287. }
  5288. /**
  5289. * Add an event to the event buffer.
  5290. * In contrast to `addEvent`, this does not return a promise & does not wait for the adding of the event to succeed/fail.
  5291. * Instead this returns `true` if we tried to add the event, else false.
  5292. * It returns `false` e.g. if we are paused, disabled, or out of the max replay duration.
  5293. *
  5294. * `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
  5295. */
  5296. function addEventSync(replay, event, isCheckout) {
  5297. if (!shouldAddEvent(replay, event)) {
  5298. return false;
  5299. }
  5300. // This should never reject
  5301. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  5302. _addEvent(replay, event, isCheckout);
  5303. return true;
  5304. }
  5305. /**
  5306. * Add an event to the event buffer.
  5307. * Resolves to `null` if no event was added, else to `void`.
  5308. *
  5309. * `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
  5310. */
  5311. function addEvent(
  5312. replay,
  5313. event,
  5314. isCheckout,
  5315. ) {
  5316. if (!shouldAddEvent(replay, event)) {
  5317. return Promise.resolve(null);
  5318. }
  5319. return _addEvent(replay, event, isCheckout);
  5320. }
  5321. async function _addEvent(
  5322. replay,
  5323. event,
  5324. isCheckout,
  5325. ) {
  5326. if (!replay.eventBuffer) {
  5327. return null;
  5328. }
  5329. try {
  5330. if (isCheckout && replay.recordingMode === 'buffer') {
  5331. replay.eventBuffer.clear();
  5332. }
  5333. if (isCheckout) {
  5334. replay.eventBuffer.hasCheckout = true;
  5335. }
  5336. const replayOptions = replay.getOptions();
  5337. const eventAfterPossibleCallback = maybeApplyCallback(event, replayOptions.beforeAddRecordingEvent);
  5338. if (!eventAfterPossibleCallback) {
  5339. return;
  5340. }
  5341. return await replay.eventBuffer.addEvent(eventAfterPossibleCallback);
  5342. } catch (error) {
  5343. const reason = error && error instanceof EventBufferSizeExceededError ? 'addEventSizeExceeded' : 'addEvent';
  5344. DEBUG_BUILD && utils.logger.error(error);
  5345. await replay.stop({ reason });
  5346. const client = core.getClient();
  5347. if (client) {
  5348. client.recordDroppedEvent('internal_sdk_error', 'replay');
  5349. }
  5350. }
  5351. }
  5352. /** Exported only for tests. */
  5353. function shouldAddEvent(replay, event) {
  5354. if (!replay.eventBuffer || replay.isPaused() || !replay.isEnabled()) {
  5355. return false;
  5356. }
  5357. const timestampInMs = timestampToMs(event.timestamp);
  5358. // Throw out events that happen more than 5 minutes ago. This can happen if
  5359. // page has been left open and idle for a long period of time and user
  5360. // comes back to trigger a new session. The performance entries rely on
  5361. // `performance.timeOrigin`, which is when the page first opened.
  5362. if (timestampInMs + replay.timeouts.sessionIdlePause < Date.now()) {
  5363. return false;
  5364. }
  5365. // Throw out events that are +60min from the initial timestamp
  5366. if (timestampInMs > replay.getContext().initialTimestamp + replay.getOptions().maxReplayDuration) {
  5367. logInfo(
  5368. `[Replay] Skipping event with timestamp ${timestampInMs} because it is after maxReplayDuration`,
  5369. replay.getOptions()._experiments.traceInternals,
  5370. );
  5371. return false;
  5372. }
  5373. return true;
  5374. }
  5375. function maybeApplyCallback(
  5376. event,
  5377. callback,
  5378. ) {
  5379. try {
  5380. if (typeof callback === 'function' && isCustomEvent(event)) {
  5381. return callback(event);
  5382. }
  5383. } catch (error) {
  5384. DEBUG_BUILD &&
  5385. utils.logger.error('[Replay] An error occured in the `beforeAddRecordingEvent` callback, skipping the event...', error);
  5386. return null;
  5387. }
  5388. return event;
  5389. }
  5390. /** If the event is an error event */
  5391. function isErrorEvent(event) {
  5392. return !event.type;
  5393. }
  5394. /** If the event is a transaction event */
  5395. function isTransactionEvent(event) {
  5396. return event.type === 'transaction';
  5397. }
  5398. /** If the event is an replay event */
  5399. function isReplayEvent(event) {
  5400. return event.type === 'replay_event';
  5401. }
  5402. /** If the event is a feedback event */
  5403. function isFeedbackEvent(event) {
  5404. return event.type === 'feedback';
  5405. }
  5406. /**
  5407. * Returns a listener to be added to `client.on('afterSendErrorEvent, listener)`.
  5408. */
  5409. function handleAfterSendEvent(replay) {
  5410. // Custom transports may still be returning `Promise<void>`, which means we cannot expect the status code to be available there
  5411. // TODO (v8): remove this check as it will no longer be necessary
  5412. const enforceStatusCode = isBaseTransportSend();
  5413. return (event, sendResponse) => {
  5414. if (!replay.isEnabled() || (!isErrorEvent(event) && !isTransactionEvent(event))) {
  5415. return;
  5416. }
  5417. const statusCode = sendResponse && sendResponse.statusCode;
  5418. // We only want to do stuff on successful error sending, otherwise you get error replays without errors attached
  5419. // If not using the base transport, we allow `undefined` response (as a custom transport may not implement this correctly yet)
  5420. // If we do use the base transport, we skip if we encountered an non-OK status code
  5421. if (enforceStatusCode && (!statusCode || statusCode < 200 || statusCode >= 300)) {
  5422. return;
  5423. }
  5424. if (isTransactionEvent(event)) {
  5425. handleTransactionEvent(replay, event);
  5426. return;
  5427. }
  5428. handleErrorEvent(replay, event);
  5429. };
  5430. }
  5431. function handleTransactionEvent(replay, event) {
  5432. const replayContext = replay.getContext();
  5433. // Collect traceIds in _context regardless of `recordingMode`
  5434. // In error mode, _context gets cleared on every checkout
  5435. // We limit to max. 100 transactions linked
  5436. if (event.contexts && event.contexts.trace && event.contexts.trace.trace_id && replayContext.traceIds.size < 100) {
  5437. replayContext.traceIds.add(event.contexts.trace.trace_id );
  5438. }
  5439. }
  5440. function handleErrorEvent(replay, event) {
  5441. const replayContext = replay.getContext();
  5442. // Add error to list of errorIds of replay. This is ok to do even if not
  5443. // sampled because context will get reset at next checkout.
  5444. // XXX: There is also a race condition where it's possible to capture an
  5445. // error to Sentry before Replay SDK has loaded, but response returns after
  5446. // it was loaded, and this gets called.
  5447. // We limit to max. 100 errors linked
  5448. if (event.event_id && replayContext.errorIds.size < 100) {
  5449. replayContext.errorIds.add(event.event_id);
  5450. }
  5451. // If error event is tagged with replay id it means it was sampled (when in buffer mode)
  5452. // Need to be very careful that this does not cause an infinite loop
  5453. if (replay.recordingMode !== 'buffer' || !event.tags || !event.tags.replayId) {
  5454. return;
  5455. }
  5456. const { beforeErrorSampling } = replay.getOptions();
  5457. if (typeof beforeErrorSampling === 'function' && !beforeErrorSampling(event)) {
  5458. return;
  5459. }
  5460. setTimeout(() => {
  5461. // Capture current event buffer as new replay
  5462. // This should never reject
  5463. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  5464. replay.sendBufferedReplayOrFlush();
  5465. });
  5466. }
  5467. function isBaseTransportSend() {
  5468. const client = core.getClient();
  5469. if (!client) {
  5470. return false;
  5471. }
  5472. const transport = client.getTransport();
  5473. if (!transport) {
  5474. return false;
  5475. }
  5476. return (
  5477. (transport.send ).__sentry__baseTransport__ || false
  5478. );
  5479. }
  5480. /**
  5481. * Returns a listener to be added to `client.on('afterSendErrorEvent, listener)`.
  5482. */
  5483. function handleBeforeSendEvent(replay) {
  5484. return (event) => {
  5485. if (!replay.isEnabled() || !isErrorEvent(event)) {
  5486. return;
  5487. }
  5488. handleHydrationError(replay, event);
  5489. };
  5490. }
  5491. function handleHydrationError(replay, event) {
  5492. const exceptionValue = event.exception && event.exception.values && event.exception.values[0].value;
  5493. if (typeof exceptionValue !== 'string') {
  5494. return;
  5495. }
  5496. if (
  5497. // Only matches errors in production builds of react-dom
  5498. // Example https://reactjs.org/docs/error-decoder.html?invariant=423
  5499. exceptionValue.match(/reactjs\.org\/docs\/error-decoder\.html\?invariant=(418|419|422|423|425)/) ||
  5500. // Development builds of react-dom
  5501. // Error 1: Hydration failed because the initial UI does not match what was rendered on the server.
  5502. // Error 2: Text content does not match server-rendered HTML. Warning: Text content did not match.
  5503. exceptionValue.match(/(does not match server-rendered HTML|Hydration failed because)/i)
  5504. ) {
  5505. const breadcrumb = createBreadcrumb({
  5506. category: 'replay.hydrate-error',
  5507. });
  5508. addBreadcrumbEvent(replay, breadcrumb);
  5509. }
  5510. }
  5511. /**
  5512. * Returns true if we think the given event is an error originating inside of rrweb.
  5513. */
  5514. function isRrwebError(event, hint) {
  5515. if (event.type || !event.exception || !event.exception.values || !event.exception.values.length) {
  5516. return false;
  5517. }
  5518. // @ts-expect-error this may be set by rrweb when it finds errors
  5519. if (hint.originalException && hint.originalException.__rrweb__) {
  5520. return true;
  5521. }
  5522. return false;
  5523. }
  5524. /**
  5525. * Add a feedback breadcrumb event to replay.
  5526. */
  5527. function addFeedbackBreadcrumb(replay, event) {
  5528. replay.triggerUserActivity();
  5529. replay.addUpdate(() => {
  5530. if (!event.timestamp) {
  5531. // Ignore events that don't have timestamps (this shouldn't happen, more of a typing issue)
  5532. // Return true here so that we don't flush
  5533. return true;
  5534. }
  5535. // This should never reject
  5536. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  5537. replay.throttledAddEvent({
  5538. type: EventType.Custom,
  5539. timestamp: event.timestamp * 1000,
  5540. data: {
  5541. tag: 'breadcrumb',
  5542. payload: {
  5543. timestamp: event.timestamp,
  5544. type: 'default',
  5545. category: 'sentry.feedback',
  5546. data: {
  5547. feedbackId: event.event_id,
  5548. },
  5549. },
  5550. },
  5551. } );
  5552. return false;
  5553. });
  5554. }
  5555. /**
  5556. * Determine if event should be sampled (only applies in buffer mode).
  5557. * When an event is captured by `hanldleGlobalEvent`, when in buffer mode
  5558. * we determine if we want to sample the error or not.
  5559. */
  5560. function shouldSampleForBufferEvent(replay, event) {
  5561. if (replay.recordingMode !== 'buffer') {
  5562. return false;
  5563. }
  5564. // ignore this error because otherwise we could loop indefinitely with
  5565. // trying to capture replay and failing
  5566. if (event.message === UNABLE_TO_SEND_REPLAY) {
  5567. return false;
  5568. }
  5569. // Require the event to be an error event & to have an exception
  5570. if (!event.exception || event.type) {
  5571. return false;
  5572. }
  5573. return isSampled(replay.getOptions().errorSampleRate);
  5574. }
  5575. /**
  5576. * Returns a listener to be added to `addEventProcessor(listener)`.
  5577. */
  5578. function handleGlobalEventListener(
  5579. replay,
  5580. includeAfterSendEventHandling = false,
  5581. ) {
  5582. const afterSendHandler = includeAfterSendEventHandling ? handleAfterSendEvent(replay) : undefined;
  5583. return Object.assign(
  5584. (event, hint) => {
  5585. // Do nothing if replay has been disabled
  5586. if (!replay.isEnabled()) {
  5587. return event;
  5588. }
  5589. if (isReplayEvent(event)) {
  5590. // Replays have separate set of breadcrumbs, do not include breadcrumbs
  5591. // from core SDK
  5592. delete event.breadcrumbs;
  5593. return event;
  5594. }
  5595. // We only want to handle errors, transactions, and feedbacks, nothing else
  5596. if (!isErrorEvent(event) && !isTransactionEvent(event) && !isFeedbackEvent(event)) {
  5597. return event;
  5598. }
  5599. // Ensure we do not add replay_id if the session is expired
  5600. const isSessionActive = replay.checkAndHandleExpiredSession();
  5601. if (!isSessionActive) {
  5602. return event;
  5603. }
  5604. if (isFeedbackEvent(event)) {
  5605. // This should never reject
  5606. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  5607. replay.flush();
  5608. event.contexts.feedback.replay_id = replay.getSessionId();
  5609. // Add a replay breadcrumb for this piece of feedback
  5610. addFeedbackBreadcrumb(replay, event);
  5611. return event;
  5612. }
  5613. // Unless `captureExceptions` is enabled, we want to ignore errors coming from rrweb
  5614. // As there can be a bunch of stuff going wrong in internals there, that we don't want to bubble up to users
  5615. if (isRrwebError(event, hint) && !replay.getOptions()._experiments.captureExceptions) {
  5616. DEBUG_BUILD && utils.logger.log('[Replay] Ignoring error from rrweb internals', event);
  5617. return null;
  5618. }
  5619. // When in buffer mode, we decide to sample here.
  5620. // Later, in `handleAfterSendEvent`, if the replayId is set, we know that we sampled
  5621. // And convert the buffer session to a full session
  5622. const isErrorEventSampled = shouldSampleForBufferEvent(replay, event);
  5623. // Tag errors if it has been sampled in buffer mode, or if it is session mode
  5624. // Only tag transactions if in session mode
  5625. const shouldTagReplayId = isErrorEventSampled || replay.recordingMode === 'session';
  5626. if (shouldTagReplayId) {
  5627. event.tags = { ...event.tags, replayId: replay.getSessionId() };
  5628. }
  5629. // In cases where a custom client is used that does not support the new hooks (yet),
  5630. // we manually call this hook method here
  5631. if (afterSendHandler) {
  5632. // Pretend the error had a 200 response so we always capture it
  5633. afterSendHandler(event, { statusCode: 200 });
  5634. }
  5635. return event;
  5636. },
  5637. { id: 'Replay' },
  5638. );
  5639. }
  5640. /**
  5641. * Create a "span" for each performance entry.
  5642. */
  5643. function createPerformanceSpans(
  5644. replay,
  5645. entries,
  5646. ) {
  5647. return entries.map(({ type, start, end, name, data }) => {
  5648. const response = replay.throttledAddEvent({
  5649. type: EventType.Custom,
  5650. timestamp: start,
  5651. data: {
  5652. tag: 'performanceSpan',
  5653. payload: {
  5654. op: type,
  5655. description: name,
  5656. startTimestamp: start,
  5657. endTimestamp: end,
  5658. data,
  5659. },
  5660. },
  5661. });
  5662. // If response is a string, it means its either THROTTLED or SKIPPED
  5663. return typeof response === 'string' ? Promise.resolve(null) : response;
  5664. });
  5665. }
  5666. function handleHistory(handlerData) {
  5667. const { from, to } = handlerData;
  5668. const now = Date.now() / 1000;
  5669. return {
  5670. type: 'navigation.push',
  5671. start: now,
  5672. end: now,
  5673. name: to,
  5674. data: {
  5675. previous: from,
  5676. },
  5677. };
  5678. }
  5679. /**
  5680. * Returns a listener to be added to `addHistoryInstrumentationHandler(listener)`.
  5681. */
  5682. function handleHistorySpanListener(replay) {
  5683. return (handlerData) => {
  5684. if (!replay.isEnabled()) {
  5685. return;
  5686. }
  5687. const result = handleHistory(handlerData);
  5688. if (result === null) {
  5689. return;
  5690. }
  5691. // Need to collect visited URLs
  5692. replay.getContext().urls.push(result.name);
  5693. replay.triggerUserActivity();
  5694. replay.addUpdate(() => {
  5695. createPerformanceSpans(replay, [result]);
  5696. // Returning false to flush
  5697. return false;
  5698. });
  5699. };
  5700. }
  5701. /**
  5702. * Check whether a given request URL should be filtered out. This is so we
  5703. * don't log Sentry ingest requests.
  5704. */
  5705. function shouldFilterRequest(replay, url) {
  5706. // If we enabled the `traceInternals` experiment, we want to trace everything
  5707. if (DEBUG_BUILD && replay.getOptions()._experiments.traceInternals) {
  5708. return false;
  5709. }
  5710. return core.isSentryRequestUrl(url, core.getClient());
  5711. }
  5712. /** Add a performance entry breadcrumb */
  5713. function addNetworkBreadcrumb(
  5714. replay,
  5715. result,
  5716. ) {
  5717. if (!replay.isEnabled()) {
  5718. return;
  5719. }
  5720. if (result === null) {
  5721. return;
  5722. }
  5723. if (shouldFilterRequest(replay, result.name)) {
  5724. return;
  5725. }
  5726. replay.addUpdate(() => {
  5727. createPerformanceSpans(replay, [result]);
  5728. // Returning true will cause `addUpdate` to not flush
  5729. // We do not want network requests to cause a flush. This will prevent
  5730. // recurring/polling requests from keeping the replay session alive.
  5731. return true;
  5732. });
  5733. }
  5734. /** only exported for tests */
  5735. function handleFetch(handlerData) {
  5736. const { startTimestamp, endTimestamp, fetchData, response } = handlerData;
  5737. if (!endTimestamp) {
  5738. return null;
  5739. }
  5740. // This is only used as a fallback, so we know the body sizes are never set here
  5741. const { method, url } = fetchData;
  5742. return {
  5743. type: 'resource.fetch',
  5744. start: startTimestamp / 1000,
  5745. end: endTimestamp / 1000,
  5746. name: url,
  5747. data: {
  5748. method,
  5749. statusCode: response ? (response ).status : undefined,
  5750. },
  5751. };
  5752. }
  5753. /**
  5754. * Returns a listener to be added to `addFetchInstrumentationHandler(listener)`.
  5755. */
  5756. function handleFetchSpanListener(replay) {
  5757. return (handlerData) => {
  5758. if (!replay.isEnabled()) {
  5759. return;
  5760. }
  5761. const result = handleFetch(handlerData);
  5762. addNetworkBreadcrumb(replay, result);
  5763. };
  5764. }
  5765. /** only exported for tests */
  5766. function handleXhr(handlerData) {
  5767. const { startTimestamp, endTimestamp, xhr } = handlerData;
  5768. const sentryXhrData = xhr[utils.SENTRY_XHR_DATA_KEY];
  5769. if (!startTimestamp || !endTimestamp || !sentryXhrData) {
  5770. return null;
  5771. }
  5772. // This is only used as a fallback, so we know the body sizes are never set here
  5773. const { method, url, status_code: statusCode } = sentryXhrData;
  5774. if (url === undefined) {
  5775. return null;
  5776. }
  5777. return {
  5778. type: 'resource.xhr',
  5779. name: url,
  5780. start: startTimestamp / 1000,
  5781. end: endTimestamp / 1000,
  5782. data: {
  5783. method,
  5784. statusCode,
  5785. },
  5786. };
  5787. }
  5788. /**
  5789. * Returns a listener to be added to `addXhrInstrumentationHandler(listener)`.
  5790. */
  5791. function handleXhrSpanListener(replay) {
  5792. return (handlerData) => {
  5793. if (!replay.isEnabled()) {
  5794. return;
  5795. }
  5796. const result = handleXhr(handlerData);
  5797. addNetworkBreadcrumb(replay, result);
  5798. };
  5799. }
  5800. /** Get the size of a body. */
  5801. function getBodySize(
  5802. body,
  5803. textEncoder,
  5804. ) {
  5805. if (!body) {
  5806. return undefined;
  5807. }
  5808. try {
  5809. if (typeof body === 'string') {
  5810. return textEncoder.encode(body).length;
  5811. }
  5812. if (body instanceof URLSearchParams) {
  5813. return textEncoder.encode(body.toString()).length;
  5814. }
  5815. if (body instanceof FormData) {
  5816. const formDataStr = _serializeFormData(body);
  5817. return textEncoder.encode(formDataStr).length;
  5818. }
  5819. if (body instanceof Blob) {
  5820. return body.size;
  5821. }
  5822. if (body instanceof ArrayBuffer) {
  5823. return body.byteLength;
  5824. }
  5825. // Currently unhandled types: ArrayBufferView, ReadableStream
  5826. } catch (e) {
  5827. // just return undefined
  5828. }
  5829. return undefined;
  5830. }
  5831. /** Convert a Content-Length header to number/undefined. */
  5832. function parseContentLengthHeader(header) {
  5833. if (!header) {
  5834. return undefined;
  5835. }
  5836. const size = parseInt(header, 10);
  5837. return isNaN(size) ? undefined : size;
  5838. }
  5839. /** Get the string representation of a body. */
  5840. function getBodyString(body) {
  5841. try {
  5842. if (typeof body === 'string') {
  5843. return [body];
  5844. }
  5845. if (body instanceof URLSearchParams) {
  5846. return [body.toString()];
  5847. }
  5848. if (body instanceof FormData) {
  5849. return [_serializeFormData(body)];
  5850. }
  5851. if (!body) {
  5852. return [undefined];
  5853. }
  5854. } catch (e2) {
  5855. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to serialize body', body);
  5856. return [undefined, 'BODY_PARSE_ERROR'];
  5857. }
  5858. DEBUG_BUILD && utils.logger.info('[Replay] Skipping network body because of body type', body);
  5859. return [undefined, 'UNPARSEABLE_BODY_TYPE'];
  5860. }
  5861. /** Merge a warning into an existing network request/response. */
  5862. function mergeWarning(
  5863. info,
  5864. warning,
  5865. ) {
  5866. if (!info) {
  5867. return {
  5868. headers: {},
  5869. size: undefined,
  5870. _meta: {
  5871. warnings: [warning],
  5872. },
  5873. };
  5874. }
  5875. const newMeta = { ...info._meta };
  5876. const existingWarnings = newMeta.warnings || [];
  5877. newMeta.warnings = [...existingWarnings, warning];
  5878. info._meta = newMeta;
  5879. return info;
  5880. }
  5881. /** Convert ReplayNetworkRequestData to a PerformanceEntry. */
  5882. function makeNetworkReplayBreadcrumb(
  5883. type,
  5884. data,
  5885. ) {
  5886. if (!data) {
  5887. return null;
  5888. }
  5889. const { startTimestamp, endTimestamp, url, method, statusCode, request, response } = data;
  5890. const result = {
  5891. type,
  5892. start: startTimestamp / 1000,
  5893. end: endTimestamp / 1000,
  5894. name: url,
  5895. data: utils.dropUndefinedKeys({
  5896. method,
  5897. statusCode,
  5898. request,
  5899. response,
  5900. }),
  5901. };
  5902. return result;
  5903. }
  5904. /** Build the request or response part of a replay network breadcrumb that was skipped. */
  5905. function buildSkippedNetworkRequestOrResponse(bodySize) {
  5906. return {
  5907. headers: {},
  5908. size: bodySize,
  5909. _meta: {
  5910. warnings: ['URL_SKIPPED'],
  5911. },
  5912. };
  5913. }
  5914. /** Build the request or response part of a replay network breadcrumb. */
  5915. function buildNetworkRequestOrResponse(
  5916. headers,
  5917. bodySize,
  5918. body,
  5919. ) {
  5920. if (!bodySize && Object.keys(headers).length === 0) {
  5921. return undefined;
  5922. }
  5923. if (!bodySize) {
  5924. return {
  5925. headers,
  5926. };
  5927. }
  5928. if (!body) {
  5929. return {
  5930. headers,
  5931. size: bodySize,
  5932. };
  5933. }
  5934. const info = {
  5935. headers,
  5936. size: bodySize,
  5937. };
  5938. const { body: normalizedBody, warnings } = normalizeNetworkBody(body);
  5939. info.body = normalizedBody;
  5940. if (warnings && warnings.length > 0) {
  5941. info._meta = {
  5942. warnings,
  5943. };
  5944. }
  5945. return info;
  5946. }
  5947. /** Filter a set of headers */
  5948. function getAllowedHeaders(headers, allowedHeaders) {
  5949. return Object.keys(headers).reduce((filteredHeaders, key) => {
  5950. const normalizedKey = key.toLowerCase();
  5951. // Avoid putting empty strings into the headers
  5952. if (allowedHeaders.includes(normalizedKey) && headers[key]) {
  5953. filteredHeaders[normalizedKey] = headers[key];
  5954. }
  5955. return filteredHeaders;
  5956. }, {});
  5957. }
  5958. function _serializeFormData(formData) {
  5959. // This is a bit simplified, but gives us a decent estimate
  5960. // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
  5961. // @ts-expect-error passing FormData to URLSearchParams actually works
  5962. return new URLSearchParams(formData).toString();
  5963. }
  5964. function normalizeNetworkBody(body)
  5965. {
  5966. if (!body || typeof body !== 'string') {
  5967. return {
  5968. body,
  5969. };
  5970. }
  5971. const exceedsSizeLimit = body.length > NETWORK_BODY_MAX_SIZE;
  5972. const isProbablyJson = _strIsProbablyJson(body);
  5973. if (exceedsSizeLimit) {
  5974. const truncatedBody = body.slice(0, NETWORK_BODY_MAX_SIZE);
  5975. if (isProbablyJson) {
  5976. return {
  5977. body: truncatedBody,
  5978. warnings: ['MAYBE_JSON_TRUNCATED'],
  5979. };
  5980. }
  5981. return {
  5982. body: `${truncatedBody}…`,
  5983. warnings: ['TEXT_TRUNCATED'],
  5984. };
  5985. }
  5986. if (isProbablyJson) {
  5987. try {
  5988. const jsonBody = JSON.parse(body);
  5989. return {
  5990. body: jsonBody,
  5991. };
  5992. } catch (e3) {
  5993. // fall back to just send the body as string
  5994. }
  5995. }
  5996. return {
  5997. body,
  5998. };
  5999. }
  6000. function _strIsProbablyJson(str) {
  6001. const first = str[0];
  6002. const last = str[str.length - 1];
  6003. // Simple check: If this does not start & end with {} or [], it's not JSON
  6004. return (first === '[' && last === ']') || (first === '{' && last === '}');
  6005. }
  6006. /** Match an URL against a list of strings/Regex. */
  6007. function urlMatches(url, urls) {
  6008. const fullUrl = getFullUrl(url);
  6009. return utils.stringMatchesSomePattern(fullUrl, urls);
  6010. }
  6011. /** exported for tests */
  6012. function getFullUrl(url, baseURI = WINDOW.document.baseURI) {
  6013. // Short circuit for common cases:
  6014. if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith(WINDOW.location.origin)) {
  6015. return url;
  6016. }
  6017. const fixedUrl = new URL(url, baseURI);
  6018. // If these do not match, we are not dealing with a relative URL, so just return it
  6019. if (fixedUrl.origin !== new URL(baseURI).origin) {
  6020. return url;
  6021. }
  6022. const fullUrl = fixedUrl.href;
  6023. // Remove trailing slashes, if they don't match the original URL
  6024. if (!url.endsWith('/') && fullUrl.endsWith('/')) {
  6025. return fullUrl.slice(0, -1);
  6026. }
  6027. return fullUrl;
  6028. }
  6029. /**
  6030. * Capture a fetch breadcrumb to a replay.
  6031. * This adds additional data (where approriate).
  6032. */
  6033. async function captureFetchBreadcrumbToReplay(
  6034. breadcrumb,
  6035. hint,
  6036. options
  6037. ,
  6038. ) {
  6039. try {
  6040. const data = await _prepareFetchData(breadcrumb, hint, options);
  6041. // Create a replay performance entry from this breadcrumb
  6042. const result = makeNetworkReplayBreadcrumb('resource.fetch', data);
  6043. addNetworkBreadcrumb(options.replay, result);
  6044. } catch (error) {
  6045. DEBUG_BUILD && utils.logger.error('[Replay] Failed to capture fetch breadcrumb', error);
  6046. }
  6047. }
  6048. /**
  6049. * Enrich a breadcrumb with additional data.
  6050. * This has to be sync & mutate the given breadcrumb,
  6051. * as the breadcrumb is afterwards consumed by other handlers.
  6052. */
  6053. function enrichFetchBreadcrumb(
  6054. breadcrumb,
  6055. hint,
  6056. options,
  6057. ) {
  6058. const { input, response } = hint;
  6059. const body = input ? _getFetchRequestArgBody(input) : undefined;
  6060. const reqSize = getBodySize(body, options.textEncoder);
  6061. const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
  6062. if (reqSize !== undefined) {
  6063. breadcrumb.data.request_body_size = reqSize;
  6064. }
  6065. if (resSize !== undefined) {
  6066. breadcrumb.data.response_body_size = resSize;
  6067. }
  6068. }
  6069. async function _prepareFetchData(
  6070. breadcrumb,
  6071. hint,
  6072. options
  6073. ,
  6074. ) {
  6075. const now = Date.now();
  6076. const { startTimestamp = now, endTimestamp = now } = hint;
  6077. const {
  6078. url,
  6079. method,
  6080. status_code: statusCode = 0,
  6081. request_body_size: requestBodySize,
  6082. response_body_size: responseBodySize,
  6083. } = breadcrumb.data;
  6084. const captureDetails =
  6085. urlMatches(url, options.networkDetailAllowUrls) && !urlMatches(url, options.networkDetailDenyUrls);
  6086. const request = captureDetails
  6087. ? _getRequestInfo(options, hint.input, requestBodySize)
  6088. : buildSkippedNetworkRequestOrResponse(requestBodySize);
  6089. const response = await _getResponseInfo(captureDetails, options, hint.response, responseBodySize);
  6090. return {
  6091. startTimestamp,
  6092. endTimestamp,
  6093. url,
  6094. method,
  6095. statusCode,
  6096. request,
  6097. response,
  6098. };
  6099. }
  6100. function _getRequestInfo(
  6101. { networkCaptureBodies, networkRequestHeaders },
  6102. input,
  6103. requestBodySize,
  6104. ) {
  6105. const headers = input ? getRequestHeaders(input, networkRequestHeaders) : {};
  6106. if (!networkCaptureBodies) {
  6107. return buildNetworkRequestOrResponse(headers, requestBodySize, undefined);
  6108. }
  6109. // We only want to transmit string or string-like bodies
  6110. const requestBody = _getFetchRequestArgBody(input);
  6111. const [bodyStr, warning] = getBodyString(requestBody);
  6112. const data = buildNetworkRequestOrResponse(headers, requestBodySize, bodyStr);
  6113. if (warning) {
  6114. return mergeWarning(data, warning);
  6115. }
  6116. return data;
  6117. }
  6118. /** Exported only for tests. */
  6119. async function _getResponseInfo(
  6120. captureDetails,
  6121. {
  6122. networkCaptureBodies,
  6123. textEncoder,
  6124. networkResponseHeaders,
  6125. }
  6126. ,
  6127. response,
  6128. responseBodySize,
  6129. ) {
  6130. if (!captureDetails && responseBodySize !== undefined) {
  6131. return buildSkippedNetworkRequestOrResponse(responseBodySize);
  6132. }
  6133. const headers = response ? getAllHeaders(response.headers, networkResponseHeaders) : {};
  6134. if (!response || (!networkCaptureBodies && responseBodySize !== undefined)) {
  6135. return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
  6136. }
  6137. const [bodyText, warning] = await _parseFetchResponseBody(response);
  6138. const result = getResponseData(bodyText, {
  6139. networkCaptureBodies,
  6140. textEncoder,
  6141. responseBodySize,
  6142. captureDetails,
  6143. headers,
  6144. });
  6145. if (warning) {
  6146. return mergeWarning(result, warning);
  6147. }
  6148. return result;
  6149. }
  6150. function getResponseData(
  6151. bodyText,
  6152. {
  6153. networkCaptureBodies,
  6154. textEncoder,
  6155. responseBodySize,
  6156. captureDetails,
  6157. headers,
  6158. }
  6159. ,
  6160. ) {
  6161. try {
  6162. const size =
  6163. bodyText && bodyText.length && responseBodySize === undefined
  6164. ? getBodySize(bodyText, textEncoder)
  6165. : responseBodySize;
  6166. if (!captureDetails) {
  6167. return buildSkippedNetworkRequestOrResponse(size);
  6168. }
  6169. if (networkCaptureBodies) {
  6170. return buildNetworkRequestOrResponse(headers, size, bodyText);
  6171. }
  6172. return buildNetworkRequestOrResponse(headers, size, undefined);
  6173. } catch (error) {
  6174. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to serialize response body', error);
  6175. // fallback
  6176. return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
  6177. }
  6178. }
  6179. async function _parseFetchResponseBody(response) {
  6180. const res = _tryCloneResponse(response);
  6181. if (!res) {
  6182. return [undefined, 'BODY_PARSE_ERROR'];
  6183. }
  6184. try {
  6185. const text = await _tryGetResponseText(res);
  6186. return [text];
  6187. } catch (error) {
  6188. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to get text body from response', error);
  6189. return [undefined, 'BODY_PARSE_ERROR'];
  6190. }
  6191. }
  6192. function _getFetchRequestArgBody(fetchArgs = []) {
  6193. // We only support getting the body from the fetch options
  6194. if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
  6195. return undefined;
  6196. }
  6197. return (fetchArgs[1] ).body;
  6198. }
  6199. function getAllHeaders(headers, allowedHeaders) {
  6200. const allHeaders = {};
  6201. allowedHeaders.forEach(header => {
  6202. if (headers.get(header)) {
  6203. allHeaders[header] = headers.get(header) ;
  6204. }
  6205. });
  6206. return allHeaders;
  6207. }
  6208. function getRequestHeaders(fetchArgs, allowedHeaders) {
  6209. if (fetchArgs.length === 1 && typeof fetchArgs[0] !== 'string') {
  6210. return getHeadersFromOptions(fetchArgs[0] , allowedHeaders);
  6211. }
  6212. if (fetchArgs.length === 2) {
  6213. return getHeadersFromOptions(fetchArgs[1] , allowedHeaders);
  6214. }
  6215. return {};
  6216. }
  6217. function getHeadersFromOptions(
  6218. input,
  6219. allowedHeaders,
  6220. ) {
  6221. if (!input) {
  6222. return {};
  6223. }
  6224. const headers = input.headers;
  6225. if (!headers) {
  6226. return {};
  6227. }
  6228. if (headers instanceof Headers) {
  6229. return getAllHeaders(headers, allowedHeaders);
  6230. }
  6231. // We do not support this, as it is not really documented (anymore?)
  6232. if (Array.isArray(headers)) {
  6233. return {};
  6234. }
  6235. return getAllowedHeaders(headers, allowedHeaders);
  6236. }
  6237. function _tryCloneResponse(response) {
  6238. try {
  6239. // We have to clone this, as the body can only be read once
  6240. return response.clone();
  6241. } catch (error) {
  6242. // this can throw if the response was already consumed before
  6243. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to clone response body', error);
  6244. }
  6245. }
  6246. /**
  6247. * Get the response body of a fetch request, or timeout after 500ms.
  6248. * Fetch can return a streaming body, that may not resolve (or not for a long time).
  6249. * If that happens, we rather abort after a short time than keep waiting for this.
  6250. */
  6251. function _tryGetResponseText(response) {
  6252. return new Promise((resolve, reject) => {
  6253. const timeout = setTimeout(() => reject(new Error('Timeout while trying to read response body')), 500);
  6254. _getResponseText(response)
  6255. .then(
  6256. txt => resolve(txt),
  6257. reason => reject(reason),
  6258. )
  6259. .finally(() => clearTimeout(timeout));
  6260. });
  6261. }
  6262. async function _getResponseText(response) {
  6263. // Force this to be a promise, just to be safe
  6264. // eslint-disable-next-line no-return-await
  6265. return await response.text();
  6266. }
  6267. /**
  6268. * Capture an XHR breadcrumb to a replay.
  6269. * This adds additional data (where approriate).
  6270. */
  6271. async function captureXhrBreadcrumbToReplay(
  6272. breadcrumb,
  6273. hint,
  6274. options,
  6275. ) {
  6276. try {
  6277. const data = _prepareXhrData(breadcrumb, hint, options);
  6278. // Create a replay performance entry from this breadcrumb
  6279. const result = makeNetworkReplayBreadcrumb('resource.xhr', data);
  6280. addNetworkBreadcrumb(options.replay, result);
  6281. } catch (error) {
  6282. DEBUG_BUILD && utils.logger.error('[Replay] Failed to capture xhr breadcrumb', error);
  6283. }
  6284. }
  6285. /**
  6286. * Enrich a breadcrumb with additional data.
  6287. * This has to be sync & mutate the given breadcrumb,
  6288. * as the breadcrumb is afterwards consumed by other handlers.
  6289. */
  6290. function enrichXhrBreadcrumb(
  6291. breadcrumb,
  6292. hint,
  6293. options,
  6294. ) {
  6295. const { xhr, input } = hint;
  6296. if (!xhr) {
  6297. return;
  6298. }
  6299. const reqSize = getBodySize(input, options.textEncoder);
  6300. const resSize = xhr.getResponseHeader('content-length')
  6301. ? parseContentLengthHeader(xhr.getResponseHeader('content-length'))
  6302. : _getBodySize(xhr.response, xhr.responseType, options.textEncoder);
  6303. if (reqSize !== undefined) {
  6304. breadcrumb.data.request_body_size = reqSize;
  6305. }
  6306. if (resSize !== undefined) {
  6307. breadcrumb.data.response_body_size = resSize;
  6308. }
  6309. }
  6310. function _prepareXhrData(
  6311. breadcrumb,
  6312. hint,
  6313. options,
  6314. ) {
  6315. const now = Date.now();
  6316. const { startTimestamp = now, endTimestamp = now, input, xhr } = hint;
  6317. const {
  6318. url,
  6319. method,
  6320. status_code: statusCode = 0,
  6321. request_body_size: requestBodySize,
  6322. response_body_size: responseBodySize,
  6323. } = breadcrumb.data;
  6324. if (!url) {
  6325. return null;
  6326. }
  6327. if (!xhr || !urlMatches(url, options.networkDetailAllowUrls) || urlMatches(url, options.networkDetailDenyUrls)) {
  6328. const request = buildSkippedNetworkRequestOrResponse(requestBodySize);
  6329. const response = buildSkippedNetworkRequestOrResponse(responseBodySize);
  6330. return {
  6331. startTimestamp,
  6332. endTimestamp,
  6333. url,
  6334. method,
  6335. statusCode,
  6336. request,
  6337. response,
  6338. };
  6339. }
  6340. const xhrInfo = xhr[utils.SENTRY_XHR_DATA_KEY];
  6341. const networkRequestHeaders = xhrInfo
  6342. ? getAllowedHeaders(xhrInfo.request_headers, options.networkRequestHeaders)
  6343. : {};
  6344. const networkResponseHeaders = getAllowedHeaders(getResponseHeaders(xhr), options.networkResponseHeaders);
  6345. const [requestBody, requestWarning] = options.networkCaptureBodies ? getBodyString(input) : [undefined];
  6346. const [responseBody, responseWarning] = options.networkCaptureBodies ? _getXhrResponseBody(xhr) : [undefined];
  6347. const request = buildNetworkRequestOrResponse(networkRequestHeaders, requestBodySize, requestBody);
  6348. const response = buildNetworkRequestOrResponse(networkResponseHeaders, responseBodySize, responseBody);
  6349. return {
  6350. startTimestamp,
  6351. endTimestamp,
  6352. url,
  6353. method,
  6354. statusCode,
  6355. request: requestWarning ? mergeWarning(request, requestWarning) : request,
  6356. response: responseWarning ? mergeWarning(response, responseWarning) : response,
  6357. };
  6358. }
  6359. function getResponseHeaders(xhr) {
  6360. const headers = xhr.getAllResponseHeaders();
  6361. if (!headers) {
  6362. return {};
  6363. }
  6364. return headers.split('\r\n').reduce((acc, line) => {
  6365. const [key, value] = line.split(': ');
  6366. acc[key.toLowerCase()] = value;
  6367. return acc;
  6368. }, {});
  6369. }
  6370. function _getXhrResponseBody(xhr) {
  6371. // We collect errors that happen, but only log them if we can't get any response body
  6372. const errors = [];
  6373. try {
  6374. return [xhr.responseText];
  6375. } catch (e) {
  6376. errors.push(e);
  6377. }
  6378. // Try to manually parse the response body, if responseText fails
  6379. try {
  6380. return _parseXhrResponse(xhr.response, xhr.responseType);
  6381. } catch (e) {
  6382. errors.push(e);
  6383. }
  6384. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to get xhr response body', ...errors);
  6385. return [undefined];
  6386. }
  6387. /**
  6388. * Get the string representation of the XHR response.
  6389. * Based on MDN, these are the possible types of the response:
  6390. * string
  6391. * ArrayBuffer
  6392. * Blob
  6393. * Document
  6394. * POJO
  6395. *
  6396. * Exported only for tests.
  6397. */
  6398. function _parseXhrResponse(
  6399. body,
  6400. responseType,
  6401. ) {
  6402. try {
  6403. if (typeof body === 'string') {
  6404. return [body];
  6405. }
  6406. if (body instanceof Document) {
  6407. return [body.body.outerHTML];
  6408. }
  6409. if (responseType === 'json' && body && typeof body === 'object') {
  6410. return [JSON.stringify(body)];
  6411. }
  6412. if (!body) {
  6413. return [undefined];
  6414. }
  6415. } catch (e2) {
  6416. DEBUG_BUILD && utils.logger.warn('[Replay] Failed to serialize body', body);
  6417. return [undefined, 'BODY_PARSE_ERROR'];
  6418. }
  6419. DEBUG_BUILD && utils.logger.info('[Replay] Skipping network body because of body type', body);
  6420. return [undefined, 'UNPARSEABLE_BODY_TYPE'];
  6421. }
  6422. function _getBodySize(
  6423. body,
  6424. responseType,
  6425. textEncoder,
  6426. ) {
  6427. try {
  6428. const bodyStr = responseType === 'json' && body && typeof body === 'object' ? JSON.stringify(body) : body;
  6429. return getBodySize(bodyStr, textEncoder);
  6430. } catch (e3) {
  6431. return undefined;
  6432. }
  6433. }
  6434. /**
  6435. * This method does two things:
  6436. * - It enriches the regular XHR/fetch breadcrumbs with request/response size data
  6437. * - It captures the XHR/fetch breadcrumbs to the replay
  6438. * (enriching it with further data that is _not_ added to the regular breadcrumbs)
  6439. */
  6440. function handleNetworkBreadcrumbs(replay) {
  6441. const client = core.getClient();
  6442. try {
  6443. const textEncoder = new TextEncoder();
  6444. const {
  6445. networkDetailAllowUrls,
  6446. networkDetailDenyUrls,
  6447. networkCaptureBodies,
  6448. networkRequestHeaders,
  6449. networkResponseHeaders,
  6450. } = replay.getOptions();
  6451. const options = {
  6452. replay,
  6453. textEncoder,
  6454. networkDetailAllowUrls,
  6455. networkDetailDenyUrls,
  6456. networkCaptureBodies,
  6457. networkRequestHeaders,
  6458. networkResponseHeaders,
  6459. };
  6460. if (client && client.on) {
  6461. client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
  6462. } else {
  6463. // Fallback behavior
  6464. utils.addFetchInstrumentationHandler(handleFetchSpanListener(replay));
  6465. utils.addXhrInstrumentationHandler(handleXhrSpanListener(replay));
  6466. }
  6467. } catch (e2) {
  6468. // Do nothing
  6469. }
  6470. }
  6471. /** just exported for tests */
  6472. function beforeAddNetworkBreadcrumb(
  6473. options,
  6474. breadcrumb,
  6475. hint,
  6476. ) {
  6477. if (!breadcrumb.data) {
  6478. return;
  6479. }
  6480. try {
  6481. if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
  6482. // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
  6483. // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
  6484. // So any async mutations to it will not be reflected in the final breadcrumb
  6485. enrichXhrBreadcrumb(breadcrumb, hint, options);
  6486. // This call should not reject
  6487. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  6488. captureXhrBreadcrumbToReplay(breadcrumb, hint, options);
  6489. }
  6490. if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
  6491. // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
  6492. // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
  6493. // So any async mutations to it will not be reflected in the final breadcrumb
  6494. enrichFetchBreadcrumb(breadcrumb, hint, options);
  6495. // This call should not reject
  6496. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  6497. captureFetchBreadcrumbToReplay(breadcrumb, hint, options);
  6498. }
  6499. } catch (e) {
  6500. DEBUG_BUILD && utils.logger.warn('Error when enriching network breadcrumb');
  6501. }
  6502. }
  6503. function _isXhrBreadcrumb(breadcrumb) {
  6504. return breadcrumb.category === 'xhr';
  6505. }
  6506. function _isFetchBreadcrumb(breadcrumb) {
  6507. return breadcrumb.category === 'fetch';
  6508. }
  6509. function _isXhrHint(hint) {
  6510. return hint && hint.xhr;
  6511. }
  6512. function _isFetchHint(hint) {
  6513. return hint && hint.response;
  6514. }
  6515. let _LAST_BREADCRUMB = null;
  6516. function isBreadcrumbWithCategory(breadcrumb) {
  6517. return !!breadcrumb.category;
  6518. }
  6519. const handleScopeListener =
  6520. (replay) =>
  6521. (scope) => {
  6522. if (!replay.isEnabled()) {
  6523. return;
  6524. }
  6525. const result = handleScope(scope);
  6526. if (!result) {
  6527. return;
  6528. }
  6529. addBreadcrumbEvent(replay, result);
  6530. };
  6531. /**
  6532. * An event handler to handle scope changes.
  6533. */
  6534. function handleScope(scope) {
  6535. // TODO (v8): Remove this guard. This was put in place because we introduced
  6536. // Scope.getLastBreadcrumb mid-v7 which caused incompatibilities with older SDKs.
  6537. // For now, we'll just return null if the method doesn't exist but we should eventually
  6538. // get rid of this guard.
  6539. const newBreadcrumb = scope.getLastBreadcrumb && scope.getLastBreadcrumb();
  6540. // Listener can be called when breadcrumbs have not changed, so we store the
  6541. // reference to the last crumb and only return a crumb if it has changed
  6542. if (_LAST_BREADCRUMB === newBreadcrumb || !newBreadcrumb) {
  6543. return null;
  6544. }
  6545. _LAST_BREADCRUMB = newBreadcrumb;
  6546. if (
  6547. !isBreadcrumbWithCategory(newBreadcrumb) ||
  6548. ['fetch', 'xhr', 'sentry.event', 'sentry.transaction'].includes(newBreadcrumb.category) ||
  6549. newBreadcrumb.category.startsWith('ui.')
  6550. ) {
  6551. return null;
  6552. }
  6553. if (newBreadcrumb.category === 'console') {
  6554. return normalizeConsoleBreadcrumb(newBreadcrumb);
  6555. }
  6556. return createBreadcrumb(newBreadcrumb);
  6557. }
  6558. /** exported for tests only */
  6559. function normalizeConsoleBreadcrumb(
  6560. breadcrumb,
  6561. ) {
  6562. const args = breadcrumb.data && breadcrumb.data.arguments;
  6563. if (!Array.isArray(args) || args.length === 0) {
  6564. return createBreadcrumb(breadcrumb);
  6565. }
  6566. let isTruncated = false;
  6567. // Avoid giant args captures
  6568. const normalizedArgs = args.map(arg => {
  6569. if (!arg) {
  6570. return arg;
  6571. }
  6572. if (typeof arg === 'string') {
  6573. if (arg.length > CONSOLE_ARG_MAX_SIZE) {
  6574. isTruncated = true;
  6575. return `${arg.slice(0, CONSOLE_ARG_MAX_SIZE)}…`;
  6576. }
  6577. return arg;
  6578. }
  6579. if (typeof arg === 'object') {
  6580. try {
  6581. const normalizedArg = utils.normalize(arg, 7);
  6582. const stringified = JSON.stringify(normalizedArg);
  6583. if (stringified.length > CONSOLE_ARG_MAX_SIZE) {
  6584. isTruncated = true;
  6585. // We use the pretty printed JSON string here as a base
  6586. return `${JSON.stringify(normalizedArg, null, 2).slice(0, CONSOLE_ARG_MAX_SIZE)}…`;
  6587. }
  6588. return normalizedArg;
  6589. } catch (e) {
  6590. // fall back to default
  6591. }
  6592. }
  6593. return arg;
  6594. });
  6595. return createBreadcrumb({
  6596. ...breadcrumb,
  6597. data: {
  6598. ...breadcrumb.data,
  6599. arguments: normalizedArgs,
  6600. ...(isTruncated ? { _meta: { warnings: ['CONSOLE_ARG_TRUNCATED'] } } : {}),
  6601. },
  6602. });
  6603. }
  6604. /**
  6605. * Add global listeners that cannot be removed.
  6606. */
  6607. function addGlobalListeners(replay) {
  6608. // Listeners from core SDK //
  6609. const scope = core.getCurrentScope();
  6610. const client = core.getClient();
  6611. scope.addScopeListener(handleScopeListener(replay));
  6612. utils.addClickKeypressInstrumentationHandler(handleDomListener(replay));
  6613. utils.addHistoryInstrumentationHandler(handleHistorySpanListener(replay));
  6614. handleNetworkBreadcrumbs(replay);
  6615. // Tag all (non replay) events that get sent to Sentry with the current
  6616. // replay ID so that we can reference them later in the UI
  6617. const eventProcessor = handleGlobalEventListener(replay, !hasHooks(client));
  6618. if (client && client.addEventProcessor) {
  6619. client.addEventProcessor(eventProcessor);
  6620. } else {
  6621. core.addEventProcessor(eventProcessor);
  6622. }
  6623. // If a custom client has no hooks yet, we continue to use the "old" implementation
  6624. if (hasHooks(client)) {
  6625. client.on('beforeSendEvent', handleBeforeSendEvent(replay));
  6626. client.on('afterSendEvent', handleAfterSendEvent(replay));
  6627. client.on('createDsc', (dsc) => {
  6628. const replayId = replay.getSessionId();
  6629. // We do not want to set the DSC when in buffer mode, as that means the replay has not been sent (yet)
  6630. if (replayId && replay.isEnabled() && replay.recordingMode === 'session') {
  6631. // Ensure to check that the session is still active - it could have expired in the meanwhile
  6632. const isSessionActive = replay.checkAndHandleExpiredSession();
  6633. if (isSessionActive) {
  6634. dsc.replay_id = replayId;
  6635. }
  6636. }
  6637. });
  6638. client.on('startTransaction', transaction => {
  6639. replay.lastTransaction = transaction;
  6640. });
  6641. // We may be missing the initial startTransaction due to timing issues,
  6642. // so we capture it on finish again.
  6643. client.on('finishTransaction', transaction => {
  6644. replay.lastTransaction = transaction;
  6645. });
  6646. // We want to flush replay
  6647. client.on('beforeSendFeedback', (feedbackEvent, options) => {
  6648. const replayId = replay.getSessionId();
  6649. if (options && options.includeReplay && replay.isEnabled() && replayId) {
  6650. // This should never reject
  6651. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  6652. replay.flush();
  6653. if (feedbackEvent.contexts && feedbackEvent.contexts.feedback) {
  6654. feedbackEvent.contexts.feedback.replay_id = replayId;
  6655. }
  6656. }
  6657. });
  6658. }
  6659. }
  6660. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6661. function hasHooks(client) {
  6662. return !!(client && client.on);
  6663. }
  6664. /**
  6665. * Create a "span" for the total amount of memory being used by JS objects
  6666. * (including v8 internal objects).
  6667. */
  6668. async function addMemoryEntry(replay) {
  6669. // window.performance.memory is a non-standard API and doesn't work on all browsers, so we try-catch this
  6670. try {
  6671. return Promise.all(
  6672. createPerformanceSpans(replay, [
  6673. // @ts-expect-error memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
  6674. createMemoryEntry(WINDOW.performance.memory),
  6675. ]),
  6676. );
  6677. } catch (error) {
  6678. // Do nothing
  6679. return [];
  6680. }
  6681. }
  6682. function createMemoryEntry(memoryEntry) {
  6683. const { jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize } = memoryEntry;
  6684. // we don't want to use `getAbsoluteTime` because it adds the event time to the
  6685. // time origin, so we get the current timestamp instead
  6686. const time = Date.now() / 1000;
  6687. return {
  6688. type: 'memory',
  6689. name: 'memory',
  6690. start: time,
  6691. end: time,
  6692. data: {
  6693. memory: {
  6694. jsHeapSizeLimit,
  6695. totalJSHeapSize,
  6696. usedJSHeapSize,
  6697. },
  6698. },
  6699. };
  6700. }
  6701. /**
  6702. * Heavily simplified debounce function based on lodash.debounce.
  6703. *
  6704. * This function takes a callback function (@param fun) and delays its invocation
  6705. * by @param wait milliseconds. Optionally, a maxWait can be specified in @param options,
  6706. * which ensures that the callback is invoked at least once after the specified max. wait time.
  6707. *
  6708. * @param func the function whose invocation is to be debounced
  6709. * @param wait the minimum time until the function is invoked after it was called once
  6710. * @param options the options object, which can contain the `maxWait` property
  6711. *
  6712. * @returns the debounced version of the function, which needs to be called at least once to start the
  6713. * debouncing process. Subsequent calls will reset the debouncing timer and, in case @paramfunc
  6714. * was already invoked in the meantime, return @param func's return value.
  6715. * The debounced function has two additional properties:
  6716. * - `flush`: Invokes the debounced function immediately and returns its return value
  6717. * - `cancel`: Cancels the debouncing process and resets the debouncing timer
  6718. */
  6719. function debounce(func, wait, options) {
  6720. let callbackReturnValue;
  6721. let timerId;
  6722. let maxTimerId;
  6723. const maxWait = options && options.maxWait ? Math.max(options.maxWait, wait) : 0;
  6724. function invokeFunc() {
  6725. cancelTimers();
  6726. callbackReturnValue = func();
  6727. return callbackReturnValue;
  6728. }
  6729. function cancelTimers() {
  6730. timerId !== undefined && clearTimeout(timerId);
  6731. maxTimerId !== undefined && clearTimeout(maxTimerId);
  6732. timerId = maxTimerId = undefined;
  6733. }
  6734. function flush() {
  6735. if (timerId !== undefined || maxTimerId !== undefined) {
  6736. return invokeFunc();
  6737. }
  6738. return callbackReturnValue;
  6739. }
  6740. function debounced() {
  6741. if (timerId) {
  6742. clearTimeout(timerId);
  6743. }
  6744. timerId = setTimeout(invokeFunc, wait);
  6745. if (maxWait && maxTimerId === undefined) {
  6746. maxTimerId = setTimeout(invokeFunc, maxWait);
  6747. }
  6748. return callbackReturnValue;
  6749. }
  6750. debounced.cancel = cancelTimers;
  6751. debounced.flush = flush;
  6752. return debounced;
  6753. }
  6754. /**
  6755. * Handler for recording events.
  6756. *
  6757. * Adds to event buffer, and has varying flushing behaviors if the event was a checkout.
  6758. */
  6759. function getHandleRecordingEmit(replay) {
  6760. let hadFirstEvent = false;
  6761. return (event, _isCheckout) => {
  6762. // If this is false, it means session is expired, create and a new session and wait for checkout
  6763. if (!replay.checkAndHandleExpiredSession()) {
  6764. DEBUG_BUILD && utils.logger.warn('[Replay] Received replay event after session expired.');
  6765. return;
  6766. }
  6767. // `_isCheckout` is only set when the checkout is due to `checkoutEveryNms`
  6768. // We also want to treat the first event as a checkout, so we handle this specifically here
  6769. const isCheckout = _isCheckout || !hadFirstEvent;
  6770. hadFirstEvent = true;
  6771. if (replay.clickDetector) {
  6772. updateClickDetectorForRecordingEvent(replay.clickDetector, event);
  6773. }
  6774. // The handler returns `true` if we do not want to trigger debounced flush, `false` if we want to debounce flush.
  6775. replay.addUpdate(() => {
  6776. // The session is always started immediately on pageload/init, but for
  6777. // error-only replays, it should reflect the most recent checkout
  6778. // when an error occurs. Clear any state that happens before this current
  6779. // checkout. This needs to happen before `addEvent()` which updates state
  6780. // dependent on this reset.
  6781. if (replay.recordingMode === 'buffer' && isCheckout) {
  6782. replay.setInitialState();
  6783. }
  6784. // If the event is not added (e.g. due to being paused, disabled, or out of the max replay duration),
  6785. // Skip all further steps
  6786. if (!addEventSync(replay, event, isCheckout)) {
  6787. // Return true to skip scheduling a debounced flush
  6788. return true;
  6789. }
  6790. // Different behavior for full snapshots (type=2), ignore other event types
  6791. // See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16
  6792. if (!isCheckout) {
  6793. return false;
  6794. }
  6795. // Additionally, create a meta event that will capture certain SDK settings.
  6796. // In order to handle buffer mode, this needs to either be done when we
  6797. // receive checkout events or at flush time.
  6798. //
  6799. // `isCheckout` is always true, but want to be explicit that it should
  6800. // only be added for checkouts
  6801. addSettingsEvent(replay, isCheckout);
  6802. // If there is a previousSessionId after a full snapshot occurs, then
  6803. // the replay session was started due to session expiration. The new session
  6804. // is started before triggering a new checkout and contains the id
  6805. // of the previous session. Do not immediately flush in this case
  6806. // to avoid capturing only the checkout and instead the replay will
  6807. // be captured if they perform any follow-up actions.
  6808. if (replay.session && replay.session.previousSessionId) {
  6809. return true;
  6810. }
  6811. // When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer
  6812. // this should usually be the timestamp of the checkout event, but to be safe...
  6813. if (replay.recordingMode === 'buffer' && replay.session && replay.eventBuffer) {
  6814. const earliestEvent = replay.eventBuffer.getEarliestTimestamp();
  6815. if (earliestEvent) {
  6816. logInfo(
  6817. `[Replay] Updating session start time to earliest event in buffer to ${new Date(earliestEvent)}`,
  6818. replay.getOptions()._experiments.traceInternals,
  6819. );
  6820. replay.session.started = earliestEvent;
  6821. if (replay.getOptions().stickySession) {
  6822. saveSession(replay.session);
  6823. }
  6824. }
  6825. }
  6826. if (replay.recordingMode === 'session') {
  6827. // If the full snapshot is due to an initial load, we will not have
  6828. // a previous session ID. In this case, we want to buffer events
  6829. // for a set amount of time before flushing. This can help avoid
  6830. // capturing replays of users that immediately close the window.
  6831. // This should never reject
  6832. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  6833. void replay.flush();
  6834. }
  6835. return true;
  6836. });
  6837. };
  6838. }
  6839. /**
  6840. * Exported for tests
  6841. */
  6842. function createOptionsEvent(replay) {
  6843. const options = replay.getOptions();
  6844. return {
  6845. type: EventType.Custom,
  6846. timestamp: Date.now(),
  6847. data: {
  6848. tag: 'options',
  6849. payload: {
  6850. shouldRecordCanvas: replay.isRecordingCanvas(),
  6851. sessionSampleRate: options.sessionSampleRate,
  6852. errorSampleRate: options.errorSampleRate,
  6853. useCompressionOption: options.useCompression,
  6854. blockAllMedia: options.blockAllMedia,
  6855. maskAllText: options.maskAllText,
  6856. maskAllInputs: options.maskAllInputs,
  6857. useCompression: replay.eventBuffer ? replay.eventBuffer.type === 'worker' : false,
  6858. networkDetailHasUrls: options.networkDetailAllowUrls.length > 0,
  6859. networkCaptureBodies: options.networkCaptureBodies,
  6860. networkRequestHasHeaders: options.networkRequestHeaders.length > 0,
  6861. networkResponseHasHeaders: options.networkResponseHeaders.length > 0,
  6862. },
  6863. },
  6864. };
  6865. }
  6866. /**
  6867. * Add a "meta" event that contains a simplified view on current configuration
  6868. * options. This should only be included on the first segment of a recording.
  6869. */
  6870. function addSettingsEvent(replay, isCheckout) {
  6871. // Only need to add this event when sending the first segment
  6872. if (!isCheckout || !replay.session || replay.session.segmentId !== 0) {
  6873. return;
  6874. }
  6875. addEventSync(replay, createOptionsEvent(replay), false);
  6876. }
  6877. /**
  6878. * Create a replay envelope ready to be sent.
  6879. * This includes both the replay event, as well as the recording data.
  6880. */
  6881. function createReplayEnvelope(
  6882. replayEvent,
  6883. recordingData,
  6884. dsn,
  6885. tunnel,
  6886. ) {
  6887. return utils.createEnvelope(
  6888. utils.createEventEnvelopeHeaders(replayEvent, utils.getSdkMetadataForEnvelopeHeader(replayEvent), tunnel, dsn),
  6889. [
  6890. [{ type: 'replay_event' }, replayEvent],
  6891. [
  6892. {
  6893. type: 'replay_recording',
  6894. // If string then we need to encode to UTF8, otherwise will have
  6895. // wrong size. TextEncoder has similar browser support to
  6896. // MutationObserver, although it does not accept IE11.
  6897. length:
  6898. typeof recordingData === 'string' ? new TextEncoder().encode(recordingData).length : recordingData.length,
  6899. },
  6900. recordingData,
  6901. ],
  6902. ],
  6903. );
  6904. }
  6905. /**
  6906. * Prepare the recording data ready to be sent.
  6907. */
  6908. function prepareRecordingData({
  6909. recordingData,
  6910. headers,
  6911. }
  6912. ) {
  6913. let payloadWithSequence;
  6914. // XXX: newline is needed to separate sequence id from events
  6915. const replayHeaders = `${JSON.stringify(headers)}
  6916. `;
  6917. if (typeof recordingData === 'string') {
  6918. payloadWithSequence = `${replayHeaders}${recordingData}`;
  6919. } else {
  6920. const enc = new TextEncoder();
  6921. // XXX: newline is needed to separate sequence id from events
  6922. const sequence = enc.encode(replayHeaders);
  6923. // Merge the two Uint8Arrays
  6924. payloadWithSequence = new Uint8Array(sequence.length + recordingData.length);
  6925. payloadWithSequence.set(sequence);
  6926. payloadWithSequence.set(recordingData, sequence.length);
  6927. }
  6928. return payloadWithSequence;
  6929. }
  6930. /**
  6931. * Prepare a replay event & enrich it with the SDK metadata.
  6932. */
  6933. async function prepareReplayEvent({
  6934. client,
  6935. scope,
  6936. replayId: event_id,
  6937. event,
  6938. }
  6939. ) {
  6940. const integrations =
  6941. typeof client._integrations === 'object' && client._integrations !== null && !Array.isArray(client._integrations)
  6942. ? Object.keys(client._integrations)
  6943. : undefined;
  6944. const eventHint = { event_id, integrations };
  6945. if (client.emit) {
  6946. client.emit('preprocessEvent', event, eventHint);
  6947. }
  6948. const preparedEvent = (await core.prepareEvent(
  6949. client.getOptions(),
  6950. event,
  6951. eventHint,
  6952. scope,
  6953. client,
  6954. core.getIsolationScope(),
  6955. )) ;
  6956. // If e.g. a global event processor returned null
  6957. if (!preparedEvent) {
  6958. return null;
  6959. }
  6960. // This normally happens in browser client "_prepareEvent"
  6961. // but since we do not use this private method from the client, but rather the plain import
  6962. // we need to do this manually.
  6963. preparedEvent.platform = preparedEvent.platform || 'javascript';
  6964. // extract the SDK name because `client._prepareEvent` doesn't add it to the event
  6965. const metadata = client.getSdkMetadata && client.getSdkMetadata();
  6966. const { name, version } = (metadata && metadata.sdk) || {};
  6967. preparedEvent.sdk = {
  6968. ...preparedEvent.sdk,
  6969. name: name || 'sentry.javascript.unknown',
  6970. version: version || '0.0.0',
  6971. };
  6972. return preparedEvent;
  6973. }
  6974. /**
  6975. * Send replay attachment using `fetch()`
  6976. */
  6977. async function sendReplayRequest({
  6978. recordingData,
  6979. replayId,
  6980. segmentId: segment_id,
  6981. eventContext,
  6982. timestamp,
  6983. session,
  6984. }) {
  6985. const preparedRecordingData = prepareRecordingData({
  6986. recordingData,
  6987. headers: {
  6988. segment_id,
  6989. },
  6990. });
  6991. const { urls, errorIds, traceIds, initialTimestamp } = eventContext;
  6992. const client = core.getClient();
  6993. const scope = core.getCurrentScope();
  6994. const transport = client && client.getTransport();
  6995. const dsn = client && client.getDsn();
  6996. if (!client || !transport || !dsn || !session.sampled) {
  6997. return;
  6998. }
  6999. const baseEvent = {
  7000. type: REPLAY_EVENT_NAME,
  7001. replay_start_timestamp: initialTimestamp / 1000,
  7002. timestamp: timestamp / 1000,
  7003. error_ids: errorIds,
  7004. trace_ids: traceIds,
  7005. urls,
  7006. replay_id: replayId,
  7007. segment_id,
  7008. replay_type: session.sampled,
  7009. };
  7010. const replayEvent = await prepareReplayEvent({ scope, client, replayId, event: baseEvent });
  7011. if (!replayEvent) {
  7012. // Taken from baseclient's `_processEvent` method, where this is handled for errors/transactions
  7013. client.recordDroppedEvent('event_processor', 'replay', baseEvent);
  7014. logInfo('An event processor returned `null`, will not send event.');
  7015. return;
  7016. }
  7017. /*
  7018. For reference, the fully built event looks something like this:
  7019. {
  7020. "type": "replay_event",
  7021. "timestamp": 1670837008.634,
  7022. "error_ids": [
  7023. "errorId"
  7024. ],
  7025. "trace_ids": [
  7026. "traceId"
  7027. ],
  7028. "urls": [
  7029. "https://example.com"
  7030. ],
  7031. "replay_id": "eventId",
  7032. "segment_id": 3,
  7033. "replay_type": "error",
  7034. "platform": "javascript",
  7035. "event_id": "eventId",
  7036. "environment": "production",
  7037. "sdk": {
  7038. "integrations": [
  7039. "BrowserTracing",
  7040. "Replay"
  7041. ],
  7042. "name": "sentry.javascript.browser",
  7043. "version": "7.25.0"
  7044. },
  7045. "sdkProcessingMetadata": {},
  7046. "contexts": {
  7047. },
  7048. }
  7049. */
  7050. // Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to
  7051. // sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may
  7052. // have temporarily added, etc. Even if we don't happen to be using it at some point in the future, let's not get rid
  7053. // of this `delete`, lest we miss putting it back in the next time the property is in use.)
  7054. delete replayEvent.sdkProcessingMetadata;
  7055. const envelope = createReplayEnvelope(replayEvent, preparedRecordingData, dsn, client.getOptions().tunnel);
  7056. let response;
  7057. try {
  7058. response = await transport.send(envelope);
  7059. } catch (err) {
  7060. const error = new Error(UNABLE_TO_SEND_REPLAY);
  7061. try {
  7062. // In case browsers don't allow this property to be writable
  7063. // @ts-expect-error This needs lib es2022 and newer
  7064. error.cause = err;
  7065. } catch (e) {
  7066. // nothing to do
  7067. }
  7068. throw error;
  7069. }
  7070. // TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore
  7071. if (!response) {
  7072. return response;
  7073. }
  7074. // If the status code is invalid, we want to immediately stop & not retry
  7075. if (typeof response.statusCode === 'number' && (response.statusCode < 200 || response.statusCode >= 300)) {
  7076. throw new TransportStatusCodeError(response.statusCode);
  7077. }
  7078. const rateLimits = utils.updateRateLimits({}, response);
  7079. if (utils.isRateLimited(rateLimits, 'replay')) {
  7080. throw new RateLimitError(rateLimits);
  7081. }
  7082. return response;
  7083. }
  7084. /**
  7085. * This error indicates that the transport returned an invalid status code.
  7086. */
  7087. class TransportStatusCodeError extends Error {
  7088. constructor(statusCode) {
  7089. super(`Transport returned status code ${statusCode}`);
  7090. }
  7091. }
  7092. /**
  7093. * This error indicates that we hit a rate limit API error.
  7094. */
  7095. class RateLimitError extends Error {
  7096. constructor(rateLimits) {
  7097. super('Rate limit hit');
  7098. this.rateLimits = rateLimits;
  7099. }
  7100. }
  7101. /**
  7102. * Finalize and send the current replay event to Sentry
  7103. */
  7104. async function sendReplay(
  7105. replayData,
  7106. retryConfig = {
  7107. count: 0,
  7108. interval: RETRY_BASE_INTERVAL,
  7109. },
  7110. ) {
  7111. const { recordingData, options } = replayData;
  7112. // short circuit if there's no events to upload (this shouldn't happen as _runFlush makes this check)
  7113. if (!recordingData.length) {
  7114. return;
  7115. }
  7116. try {
  7117. await sendReplayRequest(replayData);
  7118. return true;
  7119. } catch (err) {
  7120. if (err instanceof TransportStatusCodeError || err instanceof RateLimitError) {
  7121. throw err;
  7122. }
  7123. // Capture error for every failed replay
  7124. core.setContext('Replays', {
  7125. _retryCount: retryConfig.count,
  7126. });
  7127. if (DEBUG_BUILD && options._experiments && options._experiments.captureExceptions) {
  7128. core.captureException(err);
  7129. }
  7130. // If an error happened here, it's likely that uploading the attachment
  7131. // failed, we'll can retry with the same events payload
  7132. if (retryConfig.count >= RETRY_MAX_COUNT) {
  7133. const error = new Error(`${UNABLE_TO_SEND_REPLAY} - max retries exceeded`);
  7134. try {
  7135. // In case browsers don't allow this property to be writable
  7136. // @ts-expect-error This needs lib es2022 and newer
  7137. error.cause = err;
  7138. } catch (e) {
  7139. // nothing to do
  7140. }
  7141. throw error;
  7142. }
  7143. // will retry in intervals of 5, 10, 30
  7144. retryConfig.interval *= ++retryConfig.count;
  7145. return new Promise((resolve, reject) => {
  7146. setTimeout(async () => {
  7147. try {
  7148. await sendReplay(replayData, retryConfig);
  7149. resolve(true);
  7150. } catch (err) {
  7151. reject(err);
  7152. }
  7153. }, retryConfig.interval);
  7154. });
  7155. }
  7156. }
  7157. const THROTTLED = '__THROTTLED';
  7158. const SKIPPED = '__SKIPPED';
  7159. /**
  7160. * Create a throttled function off a given function.
  7161. * When calling the throttled function, it will call the original function only
  7162. * if it hasn't been called more than `maxCount` times in the last `durationSeconds`.
  7163. *
  7164. * Returns `THROTTLED` if throttled for the first time, after that `SKIPPED`,
  7165. * or else the return value of the original function.
  7166. */
  7167. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  7168. function throttle(
  7169. fn,
  7170. maxCount,
  7171. durationSeconds,
  7172. ) {
  7173. const counter = new Map();
  7174. const _cleanup = (now) => {
  7175. const threshold = now - durationSeconds;
  7176. counter.forEach((_value, key) => {
  7177. if (key < threshold) {
  7178. counter.delete(key);
  7179. }
  7180. });
  7181. };
  7182. const _getTotalCount = () => {
  7183. return [...counter.values()].reduce((a, b) => a + b, 0);
  7184. };
  7185. let isThrottled = false;
  7186. return (...rest) => {
  7187. // Date in second-precision, which we use as basis for the throttling
  7188. const now = Math.floor(Date.now() / 1000);
  7189. // First, make sure to delete any old entries
  7190. _cleanup(now);
  7191. // If already over limit, do nothing
  7192. if (_getTotalCount() >= maxCount) {
  7193. const wasThrottled = isThrottled;
  7194. isThrottled = true;
  7195. return wasThrottled ? SKIPPED : THROTTLED;
  7196. }
  7197. isThrottled = false;
  7198. const count = counter.get(now) || 0;
  7199. counter.set(now, count + 1);
  7200. return fn(...rest);
  7201. };
  7202. }
  7203. /* eslint-disable max-lines */ // TODO: We might want to split this file up
  7204. /**
  7205. * The main replay container class, which holds all the state and methods for recording and sending replays.
  7206. */
  7207. class ReplayContainer {
  7208. /**
  7209. * Recording can happen in one of three modes:
  7210. * - session: Record the whole session, sending it continuously
  7211. * - buffer: Always keep the last 60s of recording, requires:
  7212. * - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs
  7213. * - or calling `flush()` to send the replay
  7214. */
  7215. /**
  7216. * The current or last active transcation.
  7217. * This is only available when performance is enabled.
  7218. */
  7219. /**
  7220. * These are here so we can overwrite them in tests etc.
  7221. * @hidden
  7222. */
  7223. /**
  7224. * Options to pass to `rrweb.record()`
  7225. */
  7226. /**
  7227. * Timestamp of the last user activity. This lives across sessions.
  7228. */
  7229. /**
  7230. * Is the integration currently active?
  7231. */
  7232. /**
  7233. * Paused is a state where:
  7234. * - DOM Recording is not listening at all
  7235. * - Nothing will be added to event buffer (e.g. core SDK events)
  7236. */
  7237. /**
  7238. * Have we attached listeners to the core SDK?
  7239. * Note we have to track this as there is no way to remove instrumentation handlers.
  7240. */
  7241. /**
  7242. * Function to stop recording
  7243. */
  7244. /**
  7245. * Internal use for canvas recording options
  7246. */
  7247. constructor({
  7248. options,
  7249. recordingOptions,
  7250. }
  7251. ) {ReplayContainer.prototype.__init.call(this);ReplayContainer.prototype.__init2.call(this);ReplayContainer.prototype.__init3.call(this);ReplayContainer.prototype.__init4.call(this);ReplayContainer.prototype.__init5.call(this);ReplayContainer.prototype.__init6.call(this);
  7252. this.eventBuffer = null;
  7253. this.performanceEntries = [];
  7254. this.replayPerformanceEntries = [];
  7255. this.recordingMode = 'session';
  7256. this.timeouts = {
  7257. sessionIdlePause: SESSION_IDLE_PAUSE_DURATION,
  7258. sessionIdleExpire: SESSION_IDLE_EXPIRE_DURATION,
  7259. } ;
  7260. this._lastActivity = Date.now();
  7261. this._isEnabled = false;
  7262. this._isPaused = false;
  7263. this._hasInitializedCoreListeners = false;
  7264. this._context = {
  7265. errorIds: new Set(),
  7266. traceIds: new Set(),
  7267. urls: [],
  7268. initialTimestamp: Date.now(),
  7269. initialUrl: '',
  7270. };
  7271. this._recordingOptions = recordingOptions;
  7272. this._options = options;
  7273. this._debouncedFlush = debounce(() => this._flush(), this._options.flushMinDelay, {
  7274. maxWait: this._options.flushMaxDelay,
  7275. });
  7276. this._throttledAddEvent = throttle(
  7277. (event, isCheckout) => addEvent(this, event, isCheckout),
  7278. // Max 300 events...
  7279. 300,
  7280. // ... per 5s
  7281. 5,
  7282. );
  7283. const { slowClickTimeout, slowClickIgnoreSelectors } = this.getOptions();
  7284. const slowClickConfig = slowClickTimeout
  7285. ? {
  7286. threshold: Math.min(SLOW_CLICK_THRESHOLD, slowClickTimeout),
  7287. timeout: slowClickTimeout,
  7288. scrollTimeout: SLOW_CLICK_SCROLL_TIMEOUT,
  7289. ignoreSelector: slowClickIgnoreSelectors ? slowClickIgnoreSelectors.join(',') : '',
  7290. }
  7291. : undefined;
  7292. if (slowClickConfig) {
  7293. this.clickDetector = new ClickDetector(this, slowClickConfig);
  7294. }
  7295. }
  7296. /** Get the event context. */
  7297. getContext() {
  7298. return this._context;
  7299. }
  7300. /** If recording is currently enabled. */
  7301. isEnabled() {
  7302. return this._isEnabled;
  7303. }
  7304. /** If recording is currently paused. */
  7305. isPaused() {
  7306. return this._isPaused;
  7307. }
  7308. /**
  7309. * Determine if canvas recording is enabled
  7310. */
  7311. isRecordingCanvas() {
  7312. return Boolean(this._canvas);
  7313. }
  7314. /** Get the replay integration options. */
  7315. getOptions() {
  7316. return this._options;
  7317. }
  7318. /**
  7319. * Initializes the plugin based on sampling configuration. Should not be
  7320. * called outside of constructor.
  7321. */
  7322. initializeSampling(previousSessionId) {
  7323. const { errorSampleRate, sessionSampleRate } = this._options;
  7324. // If neither sample rate is > 0, then do nothing - user will need to call one of
  7325. // `start()` or `startBuffering` themselves.
  7326. if (errorSampleRate <= 0 && sessionSampleRate <= 0) {
  7327. return;
  7328. }
  7329. // Otherwise if there is _any_ sample rate set, try to load an existing
  7330. // session, or create a new one.
  7331. this._initializeSessionForSampling(previousSessionId);
  7332. if (!this.session) {
  7333. // This should not happen, something wrong has occurred
  7334. this._handleException(new Error('Unable to initialize and create session'));
  7335. return;
  7336. }
  7337. if (this.session.sampled === false) {
  7338. // This should only occur if `errorSampleRate` is 0 and was unsampled for
  7339. // session-based replay. In this case there is nothing to do.
  7340. return;
  7341. }
  7342. // If segmentId > 0, it means we've previously already captured this session
  7343. // In this case, we still want to continue in `session` recording mode
  7344. this.recordingMode = this.session.sampled === 'buffer' && this.session.segmentId === 0 ? 'buffer' : 'session';
  7345. logInfoNextTick(
  7346. `[Replay] Starting replay in ${this.recordingMode} mode`,
  7347. this._options._experiments.traceInternals,
  7348. );
  7349. this._initializeRecording();
  7350. }
  7351. /**
  7352. * Start a replay regardless of sampling rate. Calling this will always
  7353. * create a new session. Will throw an error if replay is already in progress.
  7354. *
  7355. * Creates or loads a session, attaches listeners to varying events (DOM,
  7356. * _performanceObserver, Recording, Sentry SDK, etc)
  7357. */
  7358. start() {
  7359. if (this._isEnabled && this.recordingMode === 'session') {
  7360. throw new Error('Replay recording is already in progress');
  7361. }
  7362. if (this._isEnabled && this.recordingMode === 'buffer') {
  7363. throw new Error('Replay buffering is in progress, call `flush()` to save the replay');
  7364. }
  7365. logInfoNextTick('[Replay] Starting replay in session mode', this._options._experiments.traceInternals);
  7366. const session = loadOrCreateSession(
  7367. {
  7368. maxReplayDuration: this._options.maxReplayDuration,
  7369. sessionIdleExpire: this.timeouts.sessionIdleExpire,
  7370. traceInternals: this._options._experiments.traceInternals,
  7371. },
  7372. {
  7373. stickySession: this._options.stickySession,
  7374. // This is intentional: create a new session-based replay when calling `start()`
  7375. sessionSampleRate: 1,
  7376. allowBuffering: false,
  7377. },
  7378. );
  7379. this.session = session;
  7380. this._initializeRecording();
  7381. }
  7382. /**
  7383. * Start replay buffering. Buffers until `flush()` is called or, if
  7384. * `replaysOnErrorSampleRate` > 0, an error occurs.
  7385. */
  7386. startBuffering() {
  7387. if (this._isEnabled) {
  7388. throw new Error('Replay recording is already in progress');
  7389. }
  7390. logInfoNextTick('[Replay] Starting replay in buffer mode', this._options._experiments.traceInternals);
  7391. const session = loadOrCreateSession(
  7392. {
  7393. sessionIdleExpire: this.timeouts.sessionIdleExpire,
  7394. maxReplayDuration: this._options.maxReplayDuration,
  7395. traceInternals: this._options._experiments.traceInternals,
  7396. },
  7397. {
  7398. stickySession: this._options.stickySession,
  7399. sessionSampleRate: 0,
  7400. allowBuffering: true,
  7401. },
  7402. );
  7403. this.session = session;
  7404. this.recordingMode = 'buffer';
  7405. this._initializeRecording();
  7406. }
  7407. /**
  7408. * Start recording.
  7409. *
  7410. * Note that this will cause a new DOM checkout
  7411. */
  7412. startRecording() {
  7413. try {
  7414. const canvasOptions = this._canvas;
  7415. this._stopRecording = record({
  7416. ...this._recordingOptions,
  7417. // When running in error sampling mode, we need to overwrite `checkoutEveryNms`
  7418. // Without this, it would record forever, until an error happens, which we don't want
  7419. // instead, we'll always keep the last 60 seconds of replay before an error happened
  7420. ...(this.recordingMode === 'buffer' && { checkoutEveryNms: BUFFER_CHECKOUT_TIME }),
  7421. emit: getHandleRecordingEmit(this),
  7422. onMutation: this._onMutationHandler,
  7423. ...(canvasOptions
  7424. ? {
  7425. recordCanvas: canvasOptions.recordCanvas,
  7426. getCanvasManager: canvasOptions.getCanvasManager,
  7427. sampling: canvasOptions.sampling,
  7428. dataURLOptions: canvasOptions.dataURLOptions,
  7429. }
  7430. : {}),
  7431. });
  7432. } catch (err) {
  7433. this._handleException(err);
  7434. }
  7435. }
  7436. /**
  7437. * Stops the recording, if it was running.
  7438. *
  7439. * Returns true if it was previously stopped, or is now stopped,
  7440. * otherwise false.
  7441. */
  7442. stopRecording() {
  7443. try {
  7444. if (this._stopRecording) {
  7445. this._stopRecording();
  7446. this._stopRecording = undefined;
  7447. }
  7448. return true;
  7449. } catch (err) {
  7450. this._handleException(err);
  7451. return false;
  7452. }
  7453. }
  7454. /**
  7455. * Currently, this needs to be manually called (e.g. for tests). Sentry SDK
  7456. * does not support a teardown
  7457. */
  7458. async stop({ forceFlush = false, reason } = {}) {
  7459. if (!this._isEnabled) {
  7460. return;
  7461. }
  7462. // We can't move `_isEnabled` after awaiting a flush, otherwise we can
  7463. // enter into an infinite loop when `stop()` is called while flushing.
  7464. this._isEnabled = false;
  7465. try {
  7466. logInfo(
  7467. `[Replay] Stopping Replay${reason ? ` triggered by ${reason}` : ''}`,
  7468. this._options._experiments.traceInternals,
  7469. );
  7470. this._removeListeners();
  7471. this.stopRecording();
  7472. this._debouncedFlush.cancel();
  7473. // See comment above re: `_isEnabled`, we "force" a flush, ignoring the
  7474. // `_isEnabled` state of the plugin since it was disabled above.
  7475. if (forceFlush) {
  7476. await this._flush({ force: true });
  7477. }
  7478. // After flush, destroy event buffer
  7479. this.eventBuffer && this.eventBuffer.destroy();
  7480. this.eventBuffer = null;
  7481. // Clear session from session storage, note this means if a new session
  7482. // is started after, it will not have `previousSessionId`
  7483. clearSession(this);
  7484. } catch (err) {
  7485. this._handleException(err);
  7486. }
  7487. }
  7488. /**
  7489. * Pause some replay functionality. See comments for `_isPaused`.
  7490. * This differs from stop as this only stops DOM recording, it is
  7491. * not as thorough of a shutdown as `stop()`.
  7492. */
  7493. pause() {
  7494. if (this._isPaused) {
  7495. return;
  7496. }
  7497. this._isPaused = true;
  7498. this.stopRecording();
  7499. logInfo('[Replay] Pausing replay', this._options._experiments.traceInternals);
  7500. }
  7501. /**
  7502. * Resumes recording, see notes for `pause().
  7503. *
  7504. * Note that calling `startRecording()` here will cause a
  7505. * new DOM checkout.`
  7506. */
  7507. resume() {
  7508. if (!this._isPaused || !this._checkSession()) {
  7509. return;
  7510. }
  7511. this._isPaused = false;
  7512. this.startRecording();
  7513. logInfo('[Replay] Resuming replay', this._options._experiments.traceInternals);
  7514. }
  7515. /**
  7516. * If not in "session" recording mode, flush event buffer which will create a new replay.
  7517. * Unless `continueRecording` is false, the replay will continue to record and
  7518. * behave as a "session"-based replay.
  7519. *
  7520. * Otherwise, queue up a flush.
  7521. */
  7522. async sendBufferedReplayOrFlush({ continueRecording = true } = {}) {
  7523. if (this.recordingMode === 'session') {
  7524. return this.flushImmediate();
  7525. }
  7526. const activityTime = Date.now();
  7527. logInfo('[Replay] Converting buffer to session', this._options._experiments.traceInternals);
  7528. // Allow flush to complete before resuming as a session recording, otherwise
  7529. // the checkout from `startRecording` may be included in the payload.
  7530. // Prefer to keep the error replay as a separate (and smaller) segment
  7531. // than the session replay.
  7532. await this.flushImmediate();
  7533. const hasStoppedRecording = this.stopRecording();
  7534. if (!continueRecording || !hasStoppedRecording) {
  7535. return;
  7536. }
  7537. // To avoid race conditions where this is called multiple times, we check here again that we are still buffering
  7538. if ((this.recordingMode ) === 'session') {
  7539. return;
  7540. }
  7541. // Re-start recording in session-mode
  7542. this.recordingMode = 'session';
  7543. // Once this session ends, we do not want to refresh it
  7544. if (this.session) {
  7545. this._updateUserActivity(activityTime);
  7546. this._updateSessionActivity(activityTime);
  7547. this._maybeSaveSession();
  7548. }
  7549. this.startRecording();
  7550. }
  7551. /**
  7552. * We want to batch uploads of replay events. Save events only if
  7553. * `<flushMinDelay>` milliseconds have elapsed since the last event
  7554. * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
  7555. *
  7556. * Accepts a callback to perform side-effects and returns true to stop batch
  7557. * processing and hand back control to caller.
  7558. */
  7559. addUpdate(cb) {
  7560. // We need to always run `cb` (e.g. in the case of `this.recordingMode == 'buffer'`)
  7561. const cbResult = cb();
  7562. // If this option is turned on then we will only want to call `flush`
  7563. // explicitly
  7564. if (this.recordingMode === 'buffer') {
  7565. return;
  7566. }
  7567. // If callback is true, we do not want to continue with flushing -- the
  7568. // caller will need to handle it.
  7569. if (cbResult === true) {
  7570. return;
  7571. }
  7572. // addUpdate is called quite frequently - use _debouncedFlush so that it
  7573. // respects the flush delays and does not flush immediately
  7574. this._debouncedFlush();
  7575. }
  7576. /**
  7577. * Updates the user activity timestamp and resumes recording. This should be
  7578. * called in an event handler for a user action that we consider as the user
  7579. * being "active" (e.g. a mouse click).
  7580. */
  7581. triggerUserActivity() {
  7582. this._updateUserActivity();
  7583. // This case means that recording was once stopped due to inactivity.
  7584. // Ensure that recording is resumed.
  7585. if (!this._stopRecording) {
  7586. // Create a new session, otherwise when the user action is flushed, it
  7587. // will get rejected due to an expired session.
  7588. if (!this._checkSession()) {
  7589. return;
  7590. }
  7591. // Note: This will cause a new DOM checkout
  7592. this.resume();
  7593. return;
  7594. }
  7595. // Otherwise... recording was never suspended, continue as normalish
  7596. this.checkAndHandleExpiredSession();
  7597. this._updateSessionActivity();
  7598. }
  7599. /**
  7600. * Updates the user activity timestamp *without* resuming
  7601. * recording. Some user events (e.g. keydown) can be create
  7602. * low-value replays that only contain the keypress as a
  7603. * breadcrumb. Instead this would require other events to
  7604. * create a new replay after a session has expired.
  7605. */
  7606. updateUserActivity() {
  7607. this._updateUserActivity();
  7608. this._updateSessionActivity();
  7609. }
  7610. /**
  7611. * Only flush if `this.recordingMode === 'session'`
  7612. */
  7613. conditionalFlush() {
  7614. if (this.recordingMode === 'buffer') {
  7615. return Promise.resolve();
  7616. }
  7617. return this.flushImmediate();
  7618. }
  7619. /**
  7620. * Flush using debounce flush
  7621. */
  7622. flush() {
  7623. return this._debouncedFlush() ;
  7624. }
  7625. /**
  7626. * Always flush via `_debouncedFlush` so that we do not have flushes triggered
  7627. * from calling both `flush` and `_debouncedFlush`. Otherwise, there could be
  7628. * cases of mulitple flushes happening closely together.
  7629. */
  7630. flushImmediate() {
  7631. this._debouncedFlush();
  7632. // `.flush` is provided by the debounced function, analogously to lodash.debounce
  7633. return this._debouncedFlush.flush() ;
  7634. }
  7635. /**
  7636. * Cancels queued up flushes.
  7637. */
  7638. cancelFlush() {
  7639. this._debouncedFlush.cancel();
  7640. }
  7641. /** Get the current sesion (=replay) ID */
  7642. getSessionId() {
  7643. return this.session && this.session.id;
  7644. }
  7645. /**
  7646. * Checks if recording should be stopped due to user inactivity. Otherwise
  7647. * check if session is expired and create a new session if so. Triggers a new
  7648. * full snapshot on new session.
  7649. *
  7650. * Returns true if session is not expired, false otherwise.
  7651. * @hidden
  7652. */
  7653. checkAndHandleExpiredSession() {
  7654. // Prevent starting a new session if the last user activity is older than
  7655. // SESSION_IDLE_PAUSE_DURATION. Otherwise non-user activity can trigger a new
  7656. // session+recording. This creates noisy replays that do not have much
  7657. // content in them.
  7658. if (
  7659. this._lastActivity &&
  7660. isExpired(this._lastActivity, this.timeouts.sessionIdlePause) &&
  7661. this.session &&
  7662. this.session.sampled === 'session'
  7663. ) {
  7664. // Pause recording only for session-based replays. Otherwise, resuming
  7665. // will create a new replay and will conflict with users who only choose
  7666. // to record error-based replays only. (e.g. the resumed replay will not
  7667. // contain a reference to an error)
  7668. this.pause();
  7669. return;
  7670. }
  7671. // --- There is recent user activity --- //
  7672. // This will create a new session if expired, based on expiry length
  7673. if (!this._checkSession()) {
  7674. // Check session handles the refreshing itself
  7675. return false;
  7676. }
  7677. return true;
  7678. }
  7679. /**
  7680. * Capture some initial state that can change throughout the lifespan of the
  7681. * replay. This is required because otherwise they would be captured at the
  7682. * first flush.
  7683. */
  7684. setInitialState() {
  7685. const urlPath = `${WINDOW.location.pathname}${WINDOW.location.hash}${WINDOW.location.search}`;
  7686. const url = `${WINDOW.location.origin}${urlPath}`;
  7687. this.performanceEntries = [];
  7688. this.replayPerformanceEntries = [];
  7689. // Reset _context as well
  7690. this._clearContext();
  7691. this._context.initialUrl = url;
  7692. this._context.initialTimestamp = Date.now();
  7693. this._context.urls.push(url);
  7694. }
  7695. /**
  7696. * Add a breadcrumb event, that may be throttled.
  7697. * If it was throttled, we add a custom breadcrumb to indicate that.
  7698. */
  7699. throttledAddEvent(
  7700. event,
  7701. isCheckout,
  7702. ) {
  7703. const res = this._throttledAddEvent(event, isCheckout);
  7704. // If this is THROTTLED, it means we have throttled the event for the first time
  7705. // In this case, we want to add a breadcrumb indicating that something was skipped
  7706. if (res === THROTTLED) {
  7707. const breadcrumb = createBreadcrumb({
  7708. category: 'replay.throttled',
  7709. });
  7710. this.addUpdate(() => {
  7711. // Return `false` if the event _was_ added, as that means we schedule a flush
  7712. return !addEventSync(this, {
  7713. type: ReplayEventTypeCustom,
  7714. timestamp: breadcrumb.timestamp || 0,
  7715. data: {
  7716. tag: 'breadcrumb',
  7717. payload: breadcrumb,
  7718. metric: true,
  7719. },
  7720. });
  7721. });
  7722. }
  7723. return res;
  7724. }
  7725. /**
  7726. * This will get the parametrized route name of the current page.
  7727. * This is only available if performance is enabled, and if an instrumented router is used.
  7728. */
  7729. getCurrentRoute() {
  7730. // eslint-disable-next-line deprecation/deprecation
  7731. const lastTransaction = this.lastTransaction || core.getCurrentScope().getTransaction();
  7732. const attributes = (lastTransaction && core.spanToJSON(lastTransaction).data) || {};
  7733. const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
  7734. if (!lastTransaction || !source || !['route', 'custom'].includes(source)) {
  7735. return undefined;
  7736. }
  7737. return core.spanToJSON(lastTransaction).description;
  7738. }
  7739. /**
  7740. * Initialize and start all listeners to varying events (DOM,
  7741. * Performance Observer, Recording, Sentry SDK, etc)
  7742. */
  7743. _initializeRecording() {
  7744. this.setInitialState();
  7745. // this method is generally called on page load or manually - in both cases
  7746. // we should treat it as an activity
  7747. this._updateSessionActivity();
  7748. this.eventBuffer = createEventBuffer({
  7749. useCompression: this._options.useCompression,
  7750. workerUrl: this._options.workerUrl,
  7751. });
  7752. this._removeListeners();
  7753. this._addListeners();
  7754. // Need to set as enabled before we start recording, as `record()` can trigger a flush with a new checkout
  7755. this._isEnabled = true;
  7756. this._isPaused = false;
  7757. this.startRecording();
  7758. }
  7759. /** A wrapper to conditionally capture exceptions. */
  7760. _handleException(error) {
  7761. DEBUG_BUILD && utils.logger.error('[Replay]', error);
  7762. if (DEBUG_BUILD && this._options._experiments && this._options._experiments.captureExceptions) {
  7763. core.captureException(error);
  7764. }
  7765. }
  7766. /**
  7767. * Loads (or refreshes) the current session.
  7768. */
  7769. _initializeSessionForSampling(previousSessionId) {
  7770. // Whenever there is _any_ error sample rate, we always allow buffering
  7771. // Because we decide on sampling when an error occurs, we need to buffer at all times if sampling for errors
  7772. const allowBuffering = this._options.errorSampleRate > 0;
  7773. const session = loadOrCreateSession(
  7774. {
  7775. sessionIdleExpire: this.timeouts.sessionIdleExpire,
  7776. maxReplayDuration: this._options.maxReplayDuration,
  7777. traceInternals: this._options._experiments.traceInternals,
  7778. previousSessionId,
  7779. },
  7780. {
  7781. stickySession: this._options.stickySession,
  7782. sessionSampleRate: this._options.sessionSampleRate,
  7783. allowBuffering,
  7784. },
  7785. );
  7786. this.session = session;
  7787. }
  7788. /**
  7789. * Checks and potentially refreshes the current session.
  7790. * Returns false if session is not recorded.
  7791. */
  7792. _checkSession() {
  7793. // If there is no session yet, we do not want to refresh anything
  7794. // This should generally not happen, but to be safe....
  7795. if (!this.session) {
  7796. return false;
  7797. }
  7798. const currentSession = this.session;
  7799. if (
  7800. shouldRefreshSession(currentSession, {
  7801. sessionIdleExpire: this.timeouts.sessionIdleExpire,
  7802. maxReplayDuration: this._options.maxReplayDuration,
  7803. })
  7804. ) {
  7805. // This should never reject
  7806. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  7807. this._refreshSession(currentSession);
  7808. return false;
  7809. }
  7810. return true;
  7811. }
  7812. /**
  7813. * Refresh a session with a new one.
  7814. * This stops the current session (without forcing a flush, as that would never work since we are expired),
  7815. * and then does a new sampling based on the refreshed session.
  7816. */
  7817. async _refreshSession(session) {
  7818. if (!this._isEnabled) {
  7819. return;
  7820. }
  7821. await this.stop({ reason: 'refresh session' });
  7822. this.initializeSampling(session.id);
  7823. }
  7824. /**
  7825. * Adds listeners to record events for the replay
  7826. */
  7827. _addListeners() {
  7828. try {
  7829. WINDOW.document.addEventListener('visibilitychange', this._handleVisibilityChange);
  7830. WINDOW.addEventListener('blur', this._handleWindowBlur);
  7831. WINDOW.addEventListener('focus', this._handleWindowFocus);
  7832. WINDOW.addEventListener('keydown', this._handleKeyboardEvent);
  7833. if (this.clickDetector) {
  7834. this.clickDetector.addListeners();
  7835. }
  7836. // There is no way to remove these listeners, so ensure they are only added once
  7837. if (!this._hasInitializedCoreListeners) {
  7838. addGlobalListeners(this);
  7839. this._hasInitializedCoreListeners = true;
  7840. }
  7841. } catch (err) {
  7842. this._handleException(err);
  7843. }
  7844. this._performanceCleanupCallback = setupPerformanceObserver(this);
  7845. }
  7846. /**
  7847. * Cleans up listeners that were created in `_addListeners`
  7848. */
  7849. _removeListeners() {
  7850. try {
  7851. WINDOW.document.removeEventListener('visibilitychange', this._handleVisibilityChange);
  7852. WINDOW.removeEventListener('blur', this._handleWindowBlur);
  7853. WINDOW.removeEventListener('focus', this._handleWindowFocus);
  7854. WINDOW.removeEventListener('keydown', this._handleKeyboardEvent);
  7855. if (this.clickDetector) {
  7856. this.clickDetector.removeListeners();
  7857. }
  7858. if (this._performanceCleanupCallback) {
  7859. this._performanceCleanupCallback();
  7860. }
  7861. } catch (err) {
  7862. this._handleException(err);
  7863. }
  7864. }
  7865. /**
  7866. * Handle when visibility of the page content changes. Opening a new tab will
  7867. * cause the state to change to hidden because of content of current page will
  7868. * be hidden. Likewise, moving a different window to cover the contents of the
  7869. * page will also trigger a change to a hidden state.
  7870. */
  7871. __init() {this._handleVisibilityChange = () => {
  7872. if (WINDOW.document.visibilityState === 'visible') {
  7873. this._doChangeToForegroundTasks();
  7874. } else {
  7875. this._doChangeToBackgroundTasks();
  7876. }
  7877. };}
  7878. /**
  7879. * Handle when page is blurred
  7880. */
  7881. __init2() {this._handleWindowBlur = () => {
  7882. const breadcrumb = createBreadcrumb({
  7883. category: 'ui.blur',
  7884. });
  7885. // Do not count blur as a user action -- it's part of the process of them
  7886. // leaving the page
  7887. this._doChangeToBackgroundTasks(breadcrumb);
  7888. };}
  7889. /**
  7890. * Handle when page is focused
  7891. */
  7892. __init3() {this._handleWindowFocus = () => {
  7893. const breadcrumb = createBreadcrumb({
  7894. category: 'ui.focus',
  7895. });
  7896. // Do not count focus as a user action -- instead wait until they focus and
  7897. // interactive with page
  7898. this._doChangeToForegroundTasks(breadcrumb);
  7899. };}
  7900. /** Ensure page remains active when a key is pressed. */
  7901. __init4() {this._handleKeyboardEvent = (event) => {
  7902. handleKeyboardEvent(this, event);
  7903. };}
  7904. /**
  7905. * Tasks to run when we consider a page to be hidden (via blurring and/or visibility)
  7906. */
  7907. _doChangeToBackgroundTasks(breadcrumb) {
  7908. if (!this.session) {
  7909. return;
  7910. }
  7911. const expired = isSessionExpired(this.session, {
  7912. maxReplayDuration: this._options.maxReplayDuration,
  7913. sessionIdleExpire: this.timeouts.sessionIdleExpire,
  7914. });
  7915. if (expired) {
  7916. return;
  7917. }
  7918. if (breadcrumb) {
  7919. this._createCustomBreadcrumb(breadcrumb);
  7920. }
  7921. // Send replay when the page/tab becomes hidden. There is no reason to send
  7922. // replay if it becomes visible, since no actions we care about were done
  7923. // while it was hidden
  7924. // This should never reject
  7925. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  7926. void this.conditionalFlush();
  7927. }
  7928. /**
  7929. * Tasks to run when we consider a page to be visible (via focus and/or visibility)
  7930. */
  7931. _doChangeToForegroundTasks(breadcrumb) {
  7932. if (!this.session) {
  7933. return;
  7934. }
  7935. const isSessionActive = this.checkAndHandleExpiredSession();
  7936. if (!isSessionActive) {
  7937. // If the user has come back to the page within SESSION_IDLE_PAUSE_DURATION
  7938. // ms, we will re-use the existing session, otherwise create a new
  7939. // session
  7940. logInfo('[Replay] Document has become active, but session has expired');
  7941. return;
  7942. }
  7943. if (breadcrumb) {
  7944. this._createCustomBreadcrumb(breadcrumb);
  7945. }
  7946. }
  7947. /**
  7948. * Update user activity (across session lifespans)
  7949. */
  7950. _updateUserActivity(_lastActivity = Date.now()) {
  7951. this._lastActivity = _lastActivity;
  7952. }
  7953. /**
  7954. * Updates the session's last activity timestamp
  7955. */
  7956. _updateSessionActivity(_lastActivity = Date.now()) {
  7957. if (this.session) {
  7958. this.session.lastActivity = _lastActivity;
  7959. this._maybeSaveSession();
  7960. }
  7961. }
  7962. /**
  7963. * Helper to create (and buffer) a replay breadcrumb from a core SDK breadcrumb
  7964. */
  7965. _createCustomBreadcrumb(breadcrumb) {
  7966. this.addUpdate(() => {
  7967. // This should never reject
  7968. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  7969. this.throttledAddEvent({
  7970. type: EventType.Custom,
  7971. timestamp: breadcrumb.timestamp || 0,
  7972. data: {
  7973. tag: 'breadcrumb',
  7974. payload: breadcrumb,
  7975. },
  7976. });
  7977. });
  7978. }
  7979. /**
  7980. * Observed performance events are added to `this.performanceEntries`. These
  7981. * are included in the replay event before it is finished and sent to Sentry.
  7982. */
  7983. _addPerformanceEntries() {
  7984. const performanceEntries = createPerformanceEntries(this.performanceEntries).concat(this.replayPerformanceEntries);
  7985. this.performanceEntries = [];
  7986. this.replayPerformanceEntries = [];
  7987. return Promise.all(createPerformanceSpans(this, performanceEntries));
  7988. }
  7989. /**
  7990. * Clear _context
  7991. */
  7992. _clearContext() {
  7993. // XXX: `initialTimestamp` and `initialUrl` do not get cleared
  7994. this._context.errorIds.clear();
  7995. this._context.traceIds.clear();
  7996. this._context.urls = [];
  7997. }
  7998. /** Update the initial timestamp based on the buffer content. */
  7999. _updateInitialTimestampFromEventBuffer() {
  8000. const { session, eventBuffer } = this;
  8001. if (!session || !eventBuffer) {
  8002. return;
  8003. }
  8004. // we only ever update this on the initial segment
  8005. if (session.segmentId) {
  8006. return;
  8007. }
  8008. const earliestEvent = eventBuffer.getEarliestTimestamp();
  8009. if (earliestEvent && earliestEvent < this._context.initialTimestamp) {
  8010. this._context.initialTimestamp = earliestEvent;
  8011. }
  8012. }
  8013. /**
  8014. * Return and clear _context
  8015. */
  8016. _popEventContext() {
  8017. const _context = {
  8018. initialTimestamp: this._context.initialTimestamp,
  8019. initialUrl: this._context.initialUrl,
  8020. errorIds: Array.from(this._context.errorIds),
  8021. traceIds: Array.from(this._context.traceIds),
  8022. urls: this._context.urls,
  8023. };
  8024. this._clearContext();
  8025. return _context;
  8026. }
  8027. /**
  8028. * Flushes replay event buffer to Sentry.
  8029. *
  8030. * Performance events are only added right before flushing - this is
  8031. * due to the buffered performance observer events.
  8032. *
  8033. * Should never be called directly, only by `flush`
  8034. */
  8035. async _runFlush() {
  8036. const replayId = this.getSessionId();
  8037. if (!this.session || !this.eventBuffer || !replayId) {
  8038. DEBUG_BUILD && utils.logger.error('[Replay] No session or eventBuffer found to flush.');
  8039. return;
  8040. }
  8041. await this._addPerformanceEntries();
  8042. // Check eventBuffer again, as it could have been stopped in the meanwhile
  8043. if (!this.eventBuffer || !this.eventBuffer.hasEvents) {
  8044. return;
  8045. }
  8046. // Only attach memory event if eventBuffer is not empty
  8047. await addMemoryEntry(this);
  8048. // Check eventBuffer again, as it could have been stopped in the meanwhile
  8049. if (!this.eventBuffer) {
  8050. return;
  8051. }
  8052. // if this changed in the meanwhile, e.g. because the session was refreshed or similar, we abort here
  8053. if (replayId !== this.getSessionId()) {
  8054. return;
  8055. }
  8056. try {
  8057. // This uses the data from the eventBuffer, so we need to call this before `finish()
  8058. this._updateInitialTimestampFromEventBuffer();
  8059. const timestamp = Date.now();
  8060. // Check total duration again, to avoid sending outdated stuff
  8061. // We leave 30s wiggle room to accomodate late flushing etc.
  8062. // This _could_ happen when the browser is suspended during flushing, in which case we just want to stop
  8063. if (timestamp - this._context.initialTimestamp > this._options.maxReplayDuration + 30000) {
  8064. throw new Error('Session is too long, not sending replay');
  8065. }
  8066. const eventContext = this._popEventContext();
  8067. // Always increment segmentId regardless of outcome of sending replay
  8068. const segmentId = this.session.segmentId++;
  8069. this._maybeSaveSession();
  8070. // Note this empties the event buffer regardless of outcome of sending replay
  8071. const recordingData = await this.eventBuffer.finish();
  8072. await sendReplay({
  8073. replayId,
  8074. recordingData,
  8075. segmentId,
  8076. eventContext,
  8077. session: this.session,
  8078. options: this.getOptions(),
  8079. timestamp,
  8080. });
  8081. } catch (err) {
  8082. this._handleException(err);
  8083. // This means we retried 3 times and all of them failed,
  8084. // or we ran into a problem we don't want to retry, like rate limiting.
  8085. // In this case, we want to completely stop the replay - otherwise, we may get inconsistent segments
  8086. // This should never reject
  8087. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  8088. this.stop({ reason: 'sendReplay' });
  8089. const client = core.getClient();
  8090. if (client) {
  8091. client.recordDroppedEvent('send_error', 'replay');
  8092. }
  8093. }
  8094. }
  8095. /**
  8096. * Flush recording data to Sentry. Creates a lock so that only a single flush
  8097. * can be active at a time. Do not call this directly.
  8098. */
  8099. __init5() {this._flush = async ({
  8100. force = false,
  8101. }
  8102. = {}) => {
  8103. if (!this._isEnabled && !force) {
  8104. // This can happen if e.g. the replay was stopped because of exceeding the retry limit
  8105. return;
  8106. }
  8107. if (!this.checkAndHandleExpiredSession()) {
  8108. DEBUG_BUILD && utils.logger.error('[Replay] Attempting to finish replay event after session expired.');
  8109. return;
  8110. }
  8111. if (!this.session) {
  8112. // should never happen, as we would have bailed out before
  8113. return;
  8114. }
  8115. const start = this.session.started;
  8116. const now = Date.now();
  8117. const duration = now - start;
  8118. // A flush is about to happen, cancel any queued flushes
  8119. this._debouncedFlush.cancel();
  8120. // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
  8121. // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
  8122. const tooShort = duration < this._options.minReplayDuration;
  8123. const tooLong = duration > this._options.maxReplayDuration + 5000;
  8124. if (tooShort || tooLong) {
  8125. logInfo(
  8126. `[Replay] Session duration (${Math.floor(duration / 1000)}s) is too ${
  8127. tooShort ? 'short' : 'long'
  8128. }, not sending replay.`,
  8129. this._options._experiments.traceInternals,
  8130. );
  8131. if (tooShort) {
  8132. this._debouncedFlush();
  8133. }
  8134. return;
  8135. }
  8136. const eventBuffer = this.eventBuffer;
  8137. if (eventBuffer && this.session.segmentId === 0 && !eventBuffer.hasCheckout) {
  8138. logInfo('[Replay] Flushing initial segment without checkout.', this._options._experiments.traceInternals);
  8139. // TODO FN: Evaluate if we want to stop here, or remove this again?
  8140. }
  8141. // this._flushLock acts as a lock so that future calls to `_flush()`
  8142. // will be blocked until this promise resolves
  8143. if (!this._flushLock) {
  8144. this._flushLock = this._runFlush();
  8145. await this._flushLock;
  8146. this._flushLock = undefined;
  8147. return;
  8148. }
  8149. // Wait for previous flush to finish, then call the debounced `_flush()`.
  8150. // It's possible there are other flush requests queued and waiting for it
  8151. // to resolve. We want to reduce all outstanding requests (as well as any
  8152. // new flush requests that occur within a second of the locked flush
  8153. // completing) into a single flush.
  8154. try {
  8155. await this._flushLock;
  8156. } catch (err) {
  8157. DEBUG_BUILD && utils.logger.error(err);
  8158. } finally {
  8159. this._debouncedFlush();
  8160. }
  8161. };}
  8162. /** Save the session, if it is sticky */
  8163. _maybeSaveSession() {
  8164. if (this.session && this._options.stickySession) {
  8165. saveSession(this.session);
  8166. }
  8167. }
  8168. /** Handler for rrweb.record.onMutation */
  8169. __init6() {this._onMutationHandler = (mutations) => {
  8170. const count = mutations.length;
  8171. const mutationLimit = this._options.mutationLimit;
  8172. const mutationBreadcrumbLimit = this._options.mutationBreadcrumbLimit;
  8173. const overMutationLimit = mutationLimit && count > mutationLimit;
  8174. // Create a breadcrumb if a lot of mutations happen at the same time
  8175. // We can show this in the UI as an information with potential performance improvements
  8176. if (count > mutationBreadcrumbLimit || overMutationLimit) {
  8177. const breadcrumb = createBreadcrumb({
  8178. category: 'replay.mutations',
  8179. data: {
  8180. count,
  8181. limit: overMutationLimit,
  8182. },
  8183. });
  8184. this._createCustomBreadcrumb(breadcrumb);
  8185. }
  8186. // Stop replay if over the mutation limit
  8187. if (overMutationLimit) {
  8188. // This should never reject
  8189. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  8190. this.stop({ reason: 'mutationLimit', forceFlush: this.recordingMode === 'session' });
  8191. return false;
  8192. }
  8193. // `true` means we use the regular mutation handling by rrweb
  8194. return true;
  8195. };}
  8196. }
  8197. function getOption(
  8198. selectors,
  8199. defaultSelectors,
  8200. deprecatedClassOption,
  8201. deprecatedSelectorOption,
  8202. ) {
  8203. const deprecatedSelectors = typeof deprecatedSelectorOption === 'string' ? deprecatedSelectorOption.split(',') : [];
  8204. const allSelectors = [
  8205. ...selectors,
  8206. // @deprecated
  8207. ...deprecatedSelectors,
  8208. // sentry defaults
  8209. ...defaultSelectors,
  8210. ];
  8211. // @deprecated
  8212. if (typeof deprecatedClassOption !== 'undefined') {
  8213. // NOTE: No support for RegExp
  8214. if (typeof deprecatedClassOption === 'string') {
  8215. allSelectors.push(`.${deprecatedClassOption}`);
  8216. }
  8217. utils.consoleSandbox(() => {
  8218. // eslint-disable-next-line no-console
  8219. console.warn(
  8220. '[Replay] You are using a deprecated configuration item for privacy. Read the documentation on how to use the new privacy configuration.',
  8221. );
  8222. });
  8223. }
  8224. return allSelectors.join(',');
  8225. }
  8226. /**
  8227. * Returns privacy related configuration for use in rrweb
  8228. */
  8229. function getPrivacyOptions({
  8230. mask,
  8231. unmask,
  8232. block,
  8233. unblock,
  8234. ignore,
  8235. // eslint-disable-next-line deprecation/deprecation
  8236. blockClass,
  8237. // eslint-disable-next-line deprecation/deprecation
  8238. blockSelector,
  8239. // eslint-disable-next-line deprecation/deprecation
  8240. maskTextClass,
  8241. // eslint-disable-next-line deprecation/deprecation
  8242. maskTextSelector,
  8243. // eslint-disable-next-line deprecation/deprecation
  8244. ignoreClass,
  8245. }) {
  8246. const defaultBlockedElements = ['base[href="/"]'];
  8247. const maskSelector = getOption(mask, ['.sentry-mask', '[data-sentry-mask]'], maskTextClass, maskTextSelector);
  8248. const unmaskSelector = getOption(unmask, ['.sentry-unmask', '[data-sentry-unmask]']);
  8249. const options = {
  8250. // We are making the decision to make text and input selectors the same
  8251. maskTextSelector: maskSelector,
  8252. unmaskTextSelector: unmaskSelector,
  8253. blockSelector: getOption(
  8254. block,
  8255. ['.sentry-block', '[data-sentry-block]', ...defaultBlockedElements],
  8256. blockClass,
  8257. blockSelector,
  8258. ),
  8259. unblockSelector: getOption(unblock, ['.sentry-unblock', '[data-sentry-unblock]']),
  8260. ignoreSelector: getOption(ignore, ['.sentry-ignore', '[data-sentry-ignore]', 'input[type="file"]'], ignoreClass),
  8261. };
  8262. if (blockClass instanceof RegExp) {
  8263. options.blockClass = blockClass;
  8264. }
  8265. if (maskTextClass instanceof RegExp) {
  8266. options.maskTextClass = maskTextClass;
  8267. }
  8268. return options;
  8269. }
  8270. /**
  8271. * Masks an attribute if necessary, otherwise return attribute value as-is.
  8272. */
  8273. function maskAttribute({
  8274. el,
  8275. key,
  8276. maskAttributes,
  8277. maskAllText,
  8278. privacyOptions,
  8279. value,
  8280. }) {
  8281. // We only mask attributes if `maskAllText` is true
  8282. if (!maskAllText) {
  8283. return value;
  8284. }
  8285. // unmaskTextSelector takes precendence
  8286. if (privacyOptions.unmaskTextSelector && el.matches(privacyOptions.unmaskTextSelector)) {
  8287. return value;
  8288. }
  8289. if (
  8290. maskAttributes.includes(key) ||
  8291. // Need to mask `value` attribute for `<input>` if it's a button-like
  8292. // type
  8293. (key === 'value' && el.tagName === 'INPUT' && ['submit', 'button'].includes(el.getAttribute('type') || ''))
  8294. ) {
  8295. return value.replace(/[\S]/g, '*');
  8296. }
  8297. return value;
  8298. }
  8299. const MEDIA_SELECTORS =
  8300. 'img,image,svg,video,object,picture,embed,map,audio,link[rel="icon"],link[rel="apple-touch-icon"]';
  8301. const DEFAULT_NETWORK_HEADERS = ['content-length', 'content-type', 'accept'];
  8302. let _initialized = false;
  8303. const replayIntegration = ((options) => {
  8304. // eslint-disable-next-line deprecation/deprecation
  8305. return new Replay(options);
  8306. }) ;
  8307. /**
  8308. * The main replay integration class, to be passed to `init({ integrations: [] })`.
  8309. * @deprecated Use `replayIntegration()` instead.
  8310. */
  8311. class Replay {
  8312. /**
  8313. * @inheritDoc
  8314. */
  8315. static __initStatic() {this.id = 'Replay';}
  8316. /**
  8317. * @inheritDoc
  8318. */
  8319. /**
  8320. * Options to pass to `rrweb.record()`
  8321. */
  8322. /**
  8323. * Initial options passed to the replay integration, merged with default values.
  8324. * Note: `sessionSampleRate` and `errorSampleRate` are not required here, as they
  8325. * can only be finally set when setupOnce() is called.
  8326. *
  8327. * @private
  8328. */
  8329. constructor({
  8330. flushMinDelay = DEFAULT_FLUSH_MIN_DELAY,
  8331. flushMaxDelay = DEFAULT_FLUSH_MAX_DELAY,
  8332. minReplayDuration = MIN_REPLAY_DURATION,
  8333. maxReplayDuration = MAX_REPLAY_DURATION,
  8334. stickySession = true,
  8335. useCompression = true,
  8336. workerUrl,
  8337. _experiments = {},
  8338. sessionSampleRate,
  8339. errorSampleRate,
  8340. maskAllText = true,
  8341. maskAllInputs = true,
  8342. blockAllMedia = true,
  8343. mutationBreadcrumbLimit = 750,
  8344. mutationLimit = 10000,
  8345. slowClickTimeout = 7000,
  8346. slowClickIgnoreSelectors = [],
  8347. networkDetailAllowUrls = [],
  8348. networkDetailDenyUrls = [],
  8349. networkCaptureBodies = true,
  8350. networkRequestHeaders = [],
  8351. networkResponseHeaders = [],
  8352. mask = [],
  8353. maskAttributes = ['title', 'placeholder'],
  8354. unmask = [],
  8355. block = [],
  8356. unblock = [],
  8357. ignore = [],
  8358. maskFn,
  8359. beforeAddRecordingEvent,
  8360. beforeErrorSampling,
  8361. // eslint-disable-next-line deprecation/deprecation
  8362. blockClass,
  8363. // eslint-disable-next-line deprecation/deprecation
  8364. blockSelector,
  8365. // eslint-disable-next-line deprecation/deprecation
  8366. maskInputOptions,
  8367. // eslint-disable-next-line deprecation/deprecation
  8368. maskTextClass,
  8369. // eslint-disable-next-line deprecation/deprecation
  8370. maskTextSelector,
  8371. // eslint-disable-next-line deprecation/deprecation
  8372. ignoreClass,
  8373. } = {}) {
  8374. // eslint-disable-next-line deprecation/deprecation
  8375. this.name = Replay.id;
  8376. const privacyOptions = getPrivacyOptions({
  8377. mask,
  8378. unmask,
  8379. block,
  8380. unblock,
  8381. ignore,
  8382. blockClass,
  8383. blockSelector,
  8384. maskTextClass,
  8385. maskTextSelector,
  8386. ignoreClass,
  8387. });
  8388. this._recordingOptions = {
  8389. maskAllInputs,
  8390. maskAllText,
  8391. maskInputOptions: { ...(maskInputOptions || {}), password: true },
  8392. maskTextFn: maskFn,
  8393. maskInputFn: maskFn,
  8394. maskAttributeFn: (key, value, el) =>
  8395. maskAttribute({
  8396. maskAttributes,
  8397. maskAllText,
  8398. privacyOptions,
  8399. key,
  8400. value,
  8401. el,
  8402. }),
  8403. ...privacyOptions,
  8404. // Our defaults
  8405. slimDOMOptions: 'all',
  8406. inlineStylesheet: true,
  8407. // Disable inline images as it will increase segment/replay size
  8408. inlineImages: false,
  8409. // collect fonts, but be aware that `sentry.io` needs to be an allowed
  8410. // origin for playback
  8411. collectFonts: true,
  8412. errorHandler: (err) => {
  8413. try {
  8414. err.__rrweb__ = true;
  8415. } catch (error) {
  8416. // ignore errors here
  8417. // this can happen if the error is frozen or does not allow mutation for other reasons
  8418. }
  8419. },
  8420. };
  8421. this._initialOptions = {
  8422. flushMinDelay,
  8423. flushMaxDelay,
  8424. minReplayDuration: Math.min(minReplayDuration, MIN_REPLAY_DURATION_LIMIT),
  8425. maxReplayDuration: Math.min(maxReplayDuration, MAX_REPLAY_DURATION),
  8426. stickySession,
  8427. sessionSampleRate,
  8428. errorSampleRate,
  8429. useCompression,
  8430. workerUrl,
  8431. blockAllMedia,
  8432. maskAllInputs,
  8433. maskAllText,
  8434. mutationBreadcrumbLimit,
  8435. mutationLimit,
  8436. slowClickTimeout,
  8437. slowClickIgnoreSelectors,
  8438. networkDetailAllowUrls,
  8439. networkDetailDenyUrls,
  8440. networkCaptureBodies,
  8441. networkRequestHeaders: _getMergedNetworkHeaders(networkRequestHeaders),
  8442. networkResponseHeaders: _getMergedNetworkHeaders(networkResponseHeaders),
  8443. beforeAddRecordingEvent,
  8444. beforeErrorSampling,
  8445. _experiments,
  8446. };
  8447. if (typeof sessionSampleRate === 'number') {
  8448. // eslint-disable-next-line
  8449. console.warn(
  8450. `[Replay] You are passing \`sessionSampleRate\` to the Replay integration.
  8451. This option is deprecated and will be removed soon.
  8452. Instead, configure \`replaysSessionSampleRate\` directly in the SDK init options, e.g.:
  8453. Sentry.init({ replaysSessionSampleRate: ${sessionSampleRate} })`,
  8454. );
  8455. this._initialOptions.sessionSampleRate = sessionSampleRate;
  8456. }
  8457. if (typeof errorSampleRate === 'number') {
  8458. // eslint-disable-next-line
  8459. console.warn(
  8460. `[Replay] You are passing \`errorSampleRate\` to the Replay integration.
  8461. This option is deprecated and will be removed soon.
  8462. Instead, configure \`replaysOnErrorSampleRate\` directly in the SDK init options, e.g.:
  8463. Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
  8464. );
  8465. this._initialOptions.errorSampleRate = errorSampleRate;
  8466. }
  8467. if (this._initialOptions.blockAllMedia) {
  8468. // `blockAllMedia` is a more user friendly option to configure blocking
  8469. // embedded media elements
  8470. this._recordingOptions.blockSelector = !this._recordingOptions.blockSelector
  8471. ? MEDIA_SELECTORS
  8472. : `${this._recordingOptions.blockSelector},${MEDIA_SELECTORS}`;
  8473. }
  8474. if (this._isInitialized && utils.isBrowser()) {
  8475. throw new Error('Multiple Sentry Session Replay instances are not supported');
  8476. }
  8477. this._isInitialized = true;
  8478. }
  8479. /** If replay has already been initialized */
  8480. get _isInitialized() {
  8481. return _initialized;
  8482. }
  8483. /** Update _isInitialized */
  8484. set _isInitialized(value) {
  8485. _initialized = value;
  8486. }
  8487. /**
  8488. * Setup and initialize replay container
  8489. */
  8490. setupOnce() {
  8491. if (!utils.isBrowser()) {
  8492. return;
  8493. }
  8494. this._setup();
  8495. // Once upon a time, we tried to create a transaction in `setupOnce` and it would
  8496. // potentially create a transaction before some native SDK integrations have run
  8497. // and applied their own global event processor. An example is:
  8498. // https://github.com/getsentry/sentry-javascript/blob/b47ceafbdac7f8b99093ce6023726ad4687edc48/packages/browser/src/integrations/useragent.ts
  8499. //
  8500. // So we call `this._initialize()` in next event loop as a workaround to wait for other
  8501. // global event processors to finish. This is no longer needed, but keeping it
  8502. // here to avoid any future issues.
  8503. setTimeout(() => this._initialize());
  8504. }
  8505. /**
  8506. * Start a replay regardless of sampling rate. Calling this will always
  8507. * create a new session. Will throw an error if replay is already in progress.
  8508. *
  8509. * Creates or loads a session, attaches listeners to varying events (DOM,
  8510. * PerformanceObserver, Recording, Sentry SDK, etc)
  8511. */
  8512. start() {
  8513. if (!this._replay) {
  8514. return;
  8515. }
  8516. this._replay.start();
  8517. }
  8518. /**
  8519. * Start replay buffering. Buffers until `flush()` is called or, if
  8520. * `replaysOnErrorSampleRate` > 0, until an error occurs.
  8521. */
  8522. startBuffering() {
  8523. if (!this._replay) {
  8524. return;
  8525. }
  8526. this._replay.startBuffering();
  8527. }
  8528. /**
  8529. * Currently, this needs to be manually called (e.g. for tests). Sentry SDK
  8530. * does not support a teardown
  8531. */
  8532. stop() {
  8533. if (!this._replay) {
  8534. return Promise.resolve();
  8535. }
  8536. return this._replay.stop({ forceFlush: this._replay.recordingMode === 'session' });
  8537. }
  8538. /**
  8539. * If not in "session" recording mode, flush event buffer which will create a new replay.
  8540. * Unless `continueRecording` is false, the replay will continue to record and
  8541. * behave as a "session"-based replay.
  8542. *
  8543. * Otherwise, queue up a flush.
  8544. */
  8545. flush(options) {
  8546. if (!this._replay || !this._replay.isEnabled()) {
  8547. return Promise.resolve();
  8548. }
  8549. return this._replay.sendBufferedReplayOrFlush(options);
  8550. }
  8551. /**
  8552. * Get the current session ID.
  8553. */
  8554. getReplayId() {
  8555. if (!this._replay || !this._replay.isEnabled()) {
  8556. return;
  8557. }
  8558. return this._replay.getSessionId();
  8559. }
  8560. /**
  8561. * Initializes replay.
  8562. */
  8563. _initialize() {
  8564. if (!this._replay) {
  8565. return;
  8566. }
  8567. // We have to run this in _initialize, because this runs in setTimeout
  8568. // So when this runs all integrations have been added
  8569. // Before this, we cannot access integrations on the client,
  8570. // so we need to mutate the options here
  8571. this._maybeLoadFromReplayCanvasIntegration();
  8572. this._replay.initializeSampling();
  8573. }
  8574. /** Setup the integration. */
  8575. _setup() {
  8576. // Client is not available in constructor, so we need to wait until setupOnce
  8577. const finalOptions = loadReplayOptionsFromClient(this._initialOptions);
  8578. this._replay = new ReplayContainer({
  8579. options: finalOptions,
  8580. recordingOptions: this._recordingOptions,
  8581. });
  8582. }
  8583. /** Get canvas options from ReplayCanvas integration, if it is also added. */
  8584. _maybeLoadFromReplayCanvasIntegration() {
  8585. // To save bundle size, we skip checking for stuff here
  8586. // and instead just try-catch everything - as generally this should all be defined
  8587. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  8588. try {
  8589. const client = core.getClient();
  8590. const canvasIntegration = client.getIntegrationByName('ReplayCanvas')
  8591. ;
  8592. if (!canvasIntegration) {
  8593. return;
  8594. }
  8595. this._replay['_canvas'] = canvasIntegration.getOptions();
  8596. } catch (e) {
  8597. // ignore errors here
  8598. }
  8599. /* eslint-enable @typescript-eslint/no-non-null-assertion */
  8600. }
  8601. }Replay.__initStatic();
  8602. /** Parse Replay-related options from SDK options */
  8603. function loadReplayOptionsFromClient(initialOptions) {
  8604. const client = core.getClient();
  8605. const opt = client && (client.getOptions() );
  8606. const finalOptions = { sessionSampleRate: 0, errorSampleRate: 0, ...utils.dropUndefinedKeys(initialOptions) };
  8607. if (!opt) {
  8608. utils.consoleSandbox(() => {
  8609. // eslint-disable-next-line no-console
  8610. console.warn('SDK client is not available.');
  8611. });
  8612. return finalOptions;
  8613. }
  8614. if (
  8615. initialOptions.sessionSampleRate == null && // TODO remove once deprecated rates are removed
  8616. initialOptions.errorSampleRate == null && // TODO remove once deprecated rates are removed
  8617. opt.replaysSessionSampleRate == null &&
  8618. opt.replaysOnErrorSampleRate == null
  8619. ) {
  8620. utils.consoleSandbox(() => {
  8621. // eslint-disable-next-line no-console
  8622. console.warn(
  8623. 'Replay is disabled because neither `replaysSessionSampleRate` nor `replaysOnErrorSampleRate` are set.',
  8624. );
  8625. });
  8626. }
  8627. if (typeof opt.replaysSessionSampleRate === 'number') {
  8628. finalOptions.sessionSampleRate = opt.replaysSessionSampleRate;
  8629. }
  8630. if (typeof opt.replaysOnErrorSampleRate === 'number') {
  8631. finalOptions.errorSampleRate = opt.replaysOnErrorSampleRate;
  8632. }
  8633. return finalOptions;
  8634. }
  8635. function _getMergedNetworkHeaders(headers) {
  8636. return [...DEFAULT_NETWORK_HEADERS, ...headers.map(header => header.toLowerCase())];
  8637. }
  8638. /**
  8639. * This is a small utility to get a type-safe instance of the Replay integration.
  8640. */
  8641. // eslint-disable-next-line deprecation/deprecation
  8642. function getReplay() {
  8643. const client = core.getClient();
  8644. return (
  8645. client && client.getIntegrationByName && client.getIntegrationByName('Replay')
  8646. );
  8647. }
  8648. exports.Replay = Replay;
  8649. exports.getReplay = getReplay;
  8650. exports.replayIntegration = replayIntegration;
  8651. //# sourceMappingURL=index.js.map