Skip to main content
Module

x/sentry/index.mjs

The Official Sentry Deno SDK
Go to Latest
File
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300
/// <reference types="./index.d.ts" />// eslint-disable-next-line @typescript-eslint/unbound-methodconst objectToString = Object.prototype.toString;
/** * Checks whether given value's type is one of a few Error or Error-like * {@link isError}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isError(wat) { switch (objectToString.call(wat)) { case '[object Error]': case '[object Exception]': case '[object DOMException]': return true; default: return isInstanceOf(wat, Error); }}/** * Checks whether given value is an instance of the given built-in class. * * @param wat The value to be checked * @param className * @returns A boolean representing the result. */function isBuiltin(wat, className) { return objectToString.call(wat) === `[object ${className}]`;}
/** * Checks whether given value's type is ErrorEvent * {@link isErrorEvent}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isErrorEvent$1(wat) { return isBuiltin(wat, 'ErrorEvent');}
/** * Checks whether given value's type is a string * {@link isString}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isString(wat) { return isBuiltin(wat, 'String');}
/** * Checks whether given string is parameterized * {@link isParameterizedString}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isParameterizedString(wat) { return ( typeof wat === 'object' && wat !== null && '__sentry_template_string__' in wat && '__sentry_template_values__' in wat );}
/** * Checks whether given value is a primitive (undefined, null, number, boolean, string, bigint, symbol) * {@link isPrimitive}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isPrimitive(wat) { return wat === null || isParameterizedString(wat) || (typeof wat !== 'object' && typeof wat !== 'function');}
/** * Checks whether given value's type is an object literal, or a class instance. * {@link isPlainObject}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isPlainObject(wat) { return isBuiltin(wat, 'Object');}
/** * Checks whether given value's type is an Event instance * {@link isEvent}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isEvent(wat) { return typeof Event !== 'undefined' && isInstanceOf(wat, Event);}
/** * Checks whether given value's type is an Element instance * {@link isElement}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isElement(wat) { return typeof Element !== 'undefined' && isInstanceOf(wat, Element);}
/** * Checks whether given value's type is an regexp * {@link isRegExp}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isRegExp(wat) { return isBuiltin(wat, 'RegExp');}
/** * Checks whether given value has a then function. * @param wat A value to be checked. */function isThenable(wat) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return Boolean(wat && wat.then && typeof wat.then === 'function');}
/** * Checks whether given value's type is a SyntheticEvent * {@link isSyntheticEvent}. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isSyntheticEvent(wat) { return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;}
/** * Checks whether given value's type is an instance of provided constructor. * {@link isInstanceOf}. * * @param wat A value to be checked. * @param base A constructor to be used in a check. * @returns A boolean representing the result. */function isInstanceOf(wat, base) { try { return wat instanceof base; } catch (_e) { return false; }}
/** * Checks whether given value's type is a Vue ViewModel. * * @param wat A value to be checked. * @returns A boolean representing the result. */function isVueViewModel(wat) { // Not using Object.prototype.toString because in Vue 3 it would read the instance's Symbol(Symbol.toStringTag) property. return !!(typeof wat === 'object' && wat !== null && ((wat ).__isVue || (wat )._isVue));}
/** * Truncates given string to the maximum characters count * * @param str An object that contains serializable values * @param max Maximum number of characters in truncated string (0 = unlimited) * @returns string Encoded */function truncate(str, max = 0) { if (typeof str !== 'string' || max === 0) { return str; } return str.length <= max ? str : `${str.slice(0, max)}...`;}
/** * This is basically just `trim_line` from * https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67 * * @param str An object that contains serializable values * @param max Maximum number of characters in truncated string * @returns string Encoded */function snipLine(line, colno) { let newLine = line; const lineLength = newLine.length; if (lineLength <= 150) { return newLine; } if (colno > lineLength) { // eslint-disable-next-line no-param-reassign colno = lineLength; } let start = Math.max(colno - 60, 0); if (start < 5) { start = 0; } let end = Math.min(start + 140, lineLength); if (end > lineLength - 5) { end = lineLength; } if (end === lineLength) { start = Math.max(end - 140, 0); } newLine = newLine.slice(start, end); if (start > 0) { newLine = `'{snip} ${newLine}`; } if (end < lineLength) { newLine += ' {snip}'; } return newLine;}
/** * Join values in array * @param input array of values to be joined together * @param delimiter string to be placed in-between values * @returns Joined values */// eslint-disable-next-line @typescript-eslint/no-explicit-anyfunction safeJoin(input, delimiter) { if (!Array.isArray(input)) { return ''; } const output = []; // eslint-disable-next-line @typescript-eslint/prefer-for-of for (let i = 0; i < input.length; i++) { const value = input[i]; try { // This is a hack to fix a Vue3-specific bug that causes an infinite loop of // console warnings. This happens when a Vue template is rendered with // an undeclared variable, which we try to stringify, ultimately causing // Vue to issue another warning which repeats indefinitely. // see: https://github.com/getsentry/sentry-javascript/pull/8981 if (isVueViewModel(value)) { output.push('[VueViewModel]'); } else { output.push(String(value)); } } catch (e) { output.push('[value cannot be serialized]'); } } return output.join(delimiter);}
/** * Checks if the given value matches a regex or string * * @param value The string to test * @param pattern Either a regex or a string against which `value` will be matched * @param requireExactStringMatch If true, `value` must match `pattern` exactly. If false, `value` will match * `pattern` if it contains `pattern`. Only applies to string-type patterns. */function isMatchingPattern( value, pattern, requireExactStringMatch = false,) { if (!isString(value)) { return false; } if (isRegExp(pattern)) { return pattern.test(value); } if (isString(pattern)) { return requireExactStringMatch ? value === pattern : value.includes(pattern); } return false;}
/** * Test the given string against an array of strings and regexes. By default, string matching is done on a * substring-inclusion basis rather than a strict equality basis * * @param testString The string to test * @param patterns The patterns against which to test the string * @param requireExactStringMatch If true, `testString` must match one of the given string patterns exactly in order to * count. If false, `testString` will match a string pattern if it contains that pattern. * @returns */function stringMatchesSomePattern( testString, patterns = [], requireExactStringMatch = false,) { return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch));}
/** * Creates exceptions inside `event.exception.values` for errors that are nested on properties based on the `key` parameter. */function applyAggregateErrorsToEvent( exceptionFromErrorImplementation, parser, maxValueLimit = 250, key, limit, event, hint,) { if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) { return; } // Generally speaking the last item in `event.exception.values` is the exception originating from the original Error const originalException = event.exception.values.length > 0 ? event.exception.values[event.exception.values.length - 1] : undefined; // We only create exception grouping if there is an exception in the event. if (originalException) { event.exception.values = truncateAggregateExceptions( aggregateExceptionsFromError( exceptionFromErrorImplementation, parser, limit, hint.originalException , key, event.exception.values, originalException, 0, ), maxValueLimit, ); }}
function aggregateExceptionsFromError( exceptionFromErrorImplementation, parser, limit, error, key, prevExceptions, exception, exceptionId,) { if (prevExceptions.length >= limit + 1) { return prevExceptions; } let newExceptions = [...prevExceptions]; // Recursively call this function in order to walk down a chain of errors if (isInstanceOf(error[key], Error)) { applyExceptionGroupFieldsForParentException(exception, exceptionId); const newException = exceptionFromErrorImplementation(parser, error[key]); const newExceptionId = newExceptions.length; applyExceptionGroupFieldsForChildException(newException, key, newExceptionId, exceptionId); newExceptions = aggregateExceptionsFromError( exceptionFromErrorImplementation, parser, limit, error[key], key, [newException, ...newExceptions], newException, newExceptionId, ); } // This will create exception grouping for AggregateErrors // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError if (Array.isArray(error.errors)) { error.errors.forEach((childError, i) => { if (isInstanceOf(childError, Error)) { applyExceptionGroupFieldsForParentException(exception, exceptionId); const newException = exceptionFromErrorImplementation(parser, childError); const newExceptionId = newExceptions.length; applyExceptionGroupFieldsForChildException(newException, `errors[${i}]`, newExceptionId, exceptionId); newExceptions = aggregateExceptionsFromError( exceptionFromErrorImplementation, parser, limit, childError, key, [newException, ...newExceptions], newException, newExceptionId, ); } }); } return newExceptions;}
function applyExceptionGroupFieldsForParentException(exception, exceptionId) { // Don't know if this default makes sense. The protocol requires us to set these values so we pick *some* default. exception.mechanism = exception.mechanism || { type: 'generic', handled: true }; exception.mechanism = { ...exception.mechanism, ...(exception.type === 'AggregateError' && { is_exception_group: true }), exception_id: exceptionId, };}
function applyExceptionGroupFieldsForChildException( exception, source, exceptionId, parentId,) { // Don't know if this default makes sense. The protocol requires us to set these values so we pick *some* default. exception.mechanism = exception.mechanism || { type: 'generic', handled: true }; exception.mechanism = { ...exception.mechanism, type: 'chained', source, exception_id: exceptionId, parent_id: parentId, };}
/** * Truncate the message (exception.value) of all exceptions in the event. * Because this event processor is ran after `applyClientOptions`, * we need to truncate the message of the added exceptions here. */function truncateAggregateExceptions(exceptions, maxValueLength) { return exceptions.map(exception => { if (exception.value) { exception.value = truncate(exception.value, maxValueLength); } return exception; });}
/** Internal global with common properties and Sentry extensions */
/** Get's the global object for the current JavaScript runtime */const GLOBAL_OBJ = globalThis ;
/** * Returns a global singleton contained in the global `__SENTRY__` object. * * If the singleton doesn't already exist in `__SENTRY__`, it will be created using the given factory * function and added to the `__SENTRY__` object. * * @param name name of the global singleton on __SENTRY__ * @param creator creator Factory function to create the singleton if it doesn't already exist on `__SENTRY__` * @param obj (Optional) The global object on which to look for `__SENTRY__`, if not `GLOBAL_OBJ`'s return value * @returns the singleton */function getGlobalSingleton(name, creator, obj) { const gbl = (obj || GLOBAL_OBJ) ; const __SENTRY__ = (gbl.__SENTRY__ = gbl.__SENTRY__ || {}); const singleton = __SENTRY__[name] || (__SENTRY__[name] = creator()); return singleton;}
const WINDOW$1 = GLOBAL_OBJ ;
const DEFAULT_MAX_STRING_LENGTH = 80;
/** * Given a child DOM element, returns a query-selector statement describing that * and its ancestors * e.g. [HTMLElement] => body > div > input#foo.btn[name=baz] * @returns generated DOM path */function htmlTreeAsString( elem, options = {},) { if (!elem) { return '<unknown>'; } // try/catch both: // - accessing event.target (see getsentry/raven-js#838, #768) // - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly // - can throw an exception in some circumstances. try { let currentElem = elem ; const MAX_TRAVERSE_HEIGHT = 5; const out = []; let height = 0; let len = 0; const separator = ' > '; const sepLength = separator.length; let nextStr; const keyAttrs = Array.isArray(options) ? options : options.keyAttrs; const maxStringLength = (!Array.isArray(options) && options.maxStringLength) || DEFAULT_MAX_STRING_LENGTH; while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) { nextStr = _htmlElementAsString(currentElem, keyAttrs); // bail out if // - nextStr is the 'html' element // - the length of the string that would be created exceeds maxStringLength // (ignore this limit if we are on the first iteration) if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= maxStringLength)) { break; } out.push(nextStr); len += nextStr.length; currentElem = currentElem.parentNode; } return out.reverse().join(separator); } catch (_oO) { return '<unknown>'; }}
/** * Returns a simple, query-selector representation of a DOM element * e.g. [HTMLElement] => input#foo.btn[name=baz] * @returns generated DOM path */function _htmlElementAsString(el, keyAttrs) { const elem = el;
const out = []; let className; let classes; let key; let attr; let i; if (!elem || !elem.tagName) { return ''; } // @ts-expect-error WINDOW has HTMLElement if (WINDOW$1.HTMLElement) { // If using the component name annotation plugin, this value may be available on the DOM node if (elem instanceof HTMLElement && elem.dataset) { if (elem.dataset['sentryComponent']) { return elem.dataset['sentryComponent']; } if (elem.dataset['sentryElement']) { return elem.dataset['sentryElement']; } } } out.push(elem.tagName.toLowerCase()); // Pairs of attribute keys defined in `serializeAttribute` and their values on element. const keyAttrPairs = keyAttrs && keyAttrs.length ? keyAttrs.filter(keyAttr => elem.getAttribute(keyAttr)).map(keyAttr => [keyAttr, elem.getAttribute(keyAttr)]) : null; if (keyAttrPairs && keyAttrPairs.length) { keyAttrPairs.forEach(keyAttrPair => { out.push(`[${keyAttrPair[0]}="${keyAttrPair[1]}"]`); }); } else { if (elem.id) { out.push(`#${elem.id}`); } className = elem.className; if (className && isString(className)) { classes = className.split(/\s+/); for (i = 0; i < classes.length; i++) { out.push(`.${classes[i]}`); } } } const allowedAttrs = ['aria-label', 'type', 'name', 'title', 'alt']; for (i = 0; i < allowedAttrs.length; i++) { key = allowedAttrs[i]; attr = elem.getAttribute(key); if (attr) { out.push(`[${key}="${attr}"]`); } } return out.join('');}
/** * 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. * * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking. */const DEBUG_BUILD$1 = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
/** Prefix for logging strings */const PREFIX = 'Sentry Logger ';
const CONSOLE_LEVELS = [ 'debug', 'info', 'warn', 'error', 'log', 'assert', 'trace',] ;
/** This may be mutated by the console instrumentation. */const originalConsoleMethods
= {};/** JSDoc */
/** * Temporarily disable sentry console instrumentations. * * @param callback The function to run against the original `console` messages * @returns The results of the callback */function consoleSandbox(callback) { if (!('console' in GLOBAL_OBJ)) { return callback(); } const console = GLOBAL_OBJ.console ; const wrappedFuncs = {}; const wrappedLevels = Object.keys(originalConsoleMethods) ; // Restore all wrapped console methods wrappedLevels.forEach(level => { const originalConsoleMethod = originalConsoleMethods[level] ; wrappedFuncs[level] = console[level] ; console[level] = originalConsoleMethod; }); try { return callback(); } finally { // Revert restoration to wrapped state wrappedLevels.forEach(level => { console[level] = wrappedFuncs[level] ; }); }}
function makeLogger() { let enabled = false; const logger = { enable: () => { enabled = true; }, disable: () => { enabled = false; }, isEnabled: () => enabled, }; if (DEBUG_BUILD$1) { CONSOLE_LEVELS.forEach(name => { // eslint-disable-next-line @typescript-eslint/no-explicit-any logger[name] = (...args) => { if (enabled) { consoleSandbox(() => { GLOBAL_OBJ.console[name](`${PREFIX}[${name}]:`, ...args); }); } }; }); } else { CONSOLE_LEVELS.forEach(name => { logger[name] = () => undefined; }); } return logger ;}
const logger = makeLogger();
/** Regular expression used to parse a Dsn. */const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)([\w.-]+)(?::(\d+))?\/(.+)/;
function isValidProtocol(protocol) { return protocol === 'http' || protocol === 'https';}
/** * Renders the string representation of this Dsn. * * By default, this will render the public representation without the password * component. To get the deprecated private representation, set `withPassword` * to true. * * @param withPassword When set to true, the password will be included. */function dsnToString(dsn, withPassword = false) { const { host, path, pass, port, projectId, protocol, publicKey } = dsn; return ( `${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` + `@${host}${port ? `:${port}` : ''}/${path ? `${path}/` : path}${projectId}` );}
/** * Parses a Dsn from a given string. * * @param str A Dsn as string * @returns Dsn as DsnComponents or undefined if @param str is not a valid DSN string */function dsnFromString(str) { const match = DSN_REGEX.exec(str); if (!match) { // This should be logged to the console consoleSandbox(() => { // eslint-disable-next-line no-console console.error(`Invalid Sentry Dsn: ${str}`); }); return undefined; } const [protocol, publicKey, pass = '', host, port = '', lastPath] = match.slice(1); let path = ''; let projectId = lastPath; const split = projectId.split('/'); if (split.length > 1) { path = split.slice(0, -1).join('/'); projectId = split.pop() ; } if (projectId) { const projectMatch = projectId.match(/^\d+/); if (projectMatch) { projectId = projectMatch[0]; } } return dsnFromComponents({ host, pass, path, projectId, port, protocol: protocol , publicKey });}
function dsnFromComponents(components) { return { protocol: components.protocol, publicKey: components.publicKey || '', pass: components.pass || '', host: components.host, port: components.port || '', path: components.path || '', projectId: components.projectId, };}
function validateDsn(dsn) { if (!DEBUG_BUILD$1) { return true; } const { port, projectId, protocol } = dsn; const requiredComponents = ['protocol', 'publicKey', 'host', 'projectId']; const hasMissingRequiredComponent = requiredComponents.find(component => { if (!dsn[component]) { logger.error(`Invalid Sentry Dsn: ${component} missing`); return true; } return false; }); if (hasMissingRequiredComponent) { return false; } if (!projectId.match(/^\d+$/)) { logger.error(`Invalid Sentry Dsn: Invalid projectId ${projectId}`); return false; } if (!isValidProtocol(protocol)) { logger.error(`Invalid Sentry Dsn: Invalid protocol ${protocol}`); return false; } if (port && isNaN(parseInt(port, 10))) { logger.error(`Invalid Sentry Dsn: Invalid port ${port}`); return false; } return true;}
/** * Creates a valid Sentry Dsn object, identifying a Sentry instance and project. * @returns a valid DsnComponents object or `undefined` if @param from is an invalid DSN source */function makeDsn(from) { const components = typeof from === 'string' ? dsnFromString(from) : dsnFromComponents(from); if (!components || !validateDsn(components)) { return undefined; } return components;}
/** An error emitted by Sentry SDKs and related utilities. */class SentryError extends Error { /** Display name of this error instance. */ constructor( message, logLevel = 'warn') { super(message);this.message = message; this.name = new.target.prototype.constructor.name; // This sets the prototype to be `Error`, not `SentryError`. It's unclear why we do this, but commenting this line // out causes various (seemingly totally unrelated) playwright tests consistently time out. FYI, this makes // instances of `SentryError` fail `obj instanceof SentryError` checks. Object.setPrototypeOf(this, new.target.prototype); this.logLevel = logLevel; }}
/** * Replace a method in an object with a wrapped version of itself. * * @param source An object that contains a method to be wrapped. * @param name The name of the method to be wrapped. * @param replacementFactory A higher-order function that takes the original version of the given method and returns a * wrapped version. Note: The function returned by `replacementFactory` needs to be a non-arrow function, in order to * preserve the correct value of `this`, and the original method must be called using `origMethod.call(this, <other * args>)` or `origMethod.apply(this, [<other args>])` (rather than being called directly), again to preserve `this`. * @returns void */function fill(source, name, replacementFactory) { if (!(name in source)) { return; } const original = source[name] ; const wrapped = replacementFactory(original) ; // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work // otherwise it'll throw "TypeError: Object.defineProperties called on non-object" if (typeof wrapped === 'function') { markFunctionWrapped(wrapped, original); } source[name] = wrapped;}
/** * Defines a non-enumerable property on the given object. * * @param obj The object on which to set the property * @param name The name of the property to be set * @param value The value to which to set the property */function addNonEnumerableProperty(obj, name, value) { try { Object.defineProperty(obj, name, { // enumerable: false, // the default, so we can save on bundle size by not explicitly setting it value: value, writable: true, configurable: true, }); } catch (o_O) { DEBUG_BUILD$1 && logger.log(`Failed to add non-enumerable property "${name}" to object`, obj); }}
/** * Remembers the original function on the wrapped function and * patches up the prototype. * * @param wrapped the wrapper function * @param original the original function that gets wrapped */function markFunctionWrapped(wrapped, original) { try { const proto = original.prototype || {}; wrapped.prototype = original.prototype = proto; addNonEnumerableProperty(wrapped, '__sentry_original__', original); } catch (o_O) {} // eslint-disable-line no-empty}
/** * This extracts the original function if available. See * `markFunctionWrapped` for more information. * * @param func the function to unwrap * @returns the unwrapped version of the function if available. */function getOriginalFunction(func) { return func.__sentry_original__;}
/** * Encodes given object into url-friendly format * * @param object An object that contains serializable values * @returns string Encoded */function urlEncode(object) { return Object.keys(object) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(object[key])}`) .join('&');}
/** * Transforms any `Error` or `Event` into a plain object with all of their enumerable properties, and some of their * non-enumerable properties attached. * * @param value Initial source that we have to transform in order for it to be usable by the serializer * @returns An Event or Error turned into an object - or the value argurment itself, when value is neither an Event nor * an Error. */function convertToPlainObject( value,)
{ if (isError(value)) { return { message: value.message, name: value.name, stack: value.stack, ...getOwnProperties(value), }; } else if (isEvent(value)) { const newObj = { type: value.type, target: serializeEventTarget(value.target), currentTarget: serializeEventTarget(value.currentTarget), ...getOwnProperties(value), }; if (typeof CustomEvent !== 'undefined' && isInstanceOf(value, CustomEvent)) { newObj.detail = value.detail; } return newObj; } else { return value; }}
/** Creates a string representation of the target of an `Event` object */function serializeEventTarget(target) { try { return isElement(target) ? htmlTreeAsString(target) : Object.prototype.toString.call(target); } catch (_oO) { return '<unknown>'; }}
/** Filters out all but an object's own properties */function getOwnProperties(obj) { if (typeof obj === 'object' && obj !== null) { const extractedProps = {}; for (const property in obj) { if (Object.prototype.hasOwnProperty.call(obj, property)) { extractedProps[property] = (obj )[property]; } } return extractedProps; } else { return {}; }}
/** * Given any captured exception, extract its keys and create a sorted * and truncated list that will be used inside the event message. * eg. `Non-error exception captured with keys: foo, bar, baz` */function extractExceptionKeysForMessage(exception, maxLength = 40) { const keys = Object.keys(convertToPlainObject(exception)); keys.sort(); if (!keys.length) { return '[object has no keys]'; } if (keys[0].length >= maxLength) { return truncate(keys[0], maxLength); } for (let includedKeys = keys.length; includedKeys > 0; includedKeys--) { const serialized = keys.slice(0, includedKeys).join(', '); if (serialized.length > maxLength) { continue; } if (includedKeys === keys.length) { return serialized; } return truncate(serialized, maxLength); } return '';}
/** * Given any object, return a new object having removed all fields whose value was `undefined`. * Works recursively on objects and arrays. * * Attention: This function keeps circular references in the returned object. */function dropUndefinedKeys(inputValue) { // This map keeps track of what already visited nodes map to. // Our Set - based memoBuilder doesn't work here because we want to the output object to have the same circular // references as the input object. const memoizationMap = new Map(); // This function just proxies `_dropUndefinedKeys` to keep the `memoBuilder` out of this function's API return _dropUndefinedKeys(inputValue, memoizationMap);}
function _dropUndefinedKeys(inputValue, memoizationMap) { if (isPojo(inputValue)) { // If this node has already been visited due to a circular reference, return the object it was mapped to in the new object const memoVal = memoizationMap.get(inputValue); if (memoVal !== undefined) { return memoVal ; } const returnValue = {}; // Store the mapping of this value in case we visit it again, in case of circular data memoizationMap.set(inputValue, returnValue); for (const key of Object.keys(inputValue)) { if (typeof inputValue[key] !== 'undefined') { returnValue[key] = _dropUndefinedKeys(inputValue[key], memoizationMap); } } return returnValue ; } if (Array.isArray(inputValue)) { // If this node has already been visited due to a circular reference, return the array it was mapped to in the new object const memoVal = memoizationMap.get(inputValue); if (memoVal !== undefined) { return memoVal ; } const returnValue = []; // Store the mapping of this value in case we visit it again, in case of circular data memoizationMap.set(inputValue, returnValue); inputValue.forEach((item) => { returnValue.push(_dropUndefinedKeys(item, memoizationMap)); }); return returnValue ; } return inputValue;}
function isPojo(input) { if (!isPlainObject(input)) { return false; } try { const name = (Object.getPrototypeOf(input) ).constructor.name; return !name || name === 'Object'; } catch (e) { return true; }}
const STACKTRACE_FRAME_LIMIT = 50;const UNKNOWN_FUNCTION = '?';// Used to sanitize webpack (error: *) wrapped stack errorsconst WEBPACK_ERROR_REGEXP = /\(error: (.*)\)/;const STRIP_FRAME_REGEXP = /captureMessage|captureException/;
/** * Creates a stack parser with the supplied line parsers * * StackFrames are returned in the correct order for Sentry Exception * frames and with Sentry SDK internal frames removed from the top and bottom * */function createStackParser(...parsers) { const sortedParsers = parsers.sort((a, b) => a[0] - b[0]).map(p => p[1]); return (stack, skipFirstLines = 0, framesToPop = 0) => { const frames = []; const lines = stack.split('\n'); for (let i = skipFirstLines; i < lines.length; i++) { const line = lines[i]; // Ignore lines over 1kb as they are unlikely to be stack frames. // Many of the regular expressions use backtracking which results in run time that increases exponentially with // input size. Huge strings can result in hangs/Denial of Service: // https://github.com/getsentry/sentry-javascript/issues/2286 if (line.length > 1024) { continue; } // https://github.com/getsentry/sentry-javascript/issues/5459 // Remove webpack (error: *) wrappers const cleanedLine = WEBPACK_ERROR_REGEXP.test(line) ? line.replace(WEBPACK_ERROR_REGEXP, '$1') : line; // https://github.com/getsentry/sentry-javascript/issues/7813 // Skip Error: lines if (cleanedLine.match(/\S*Error: /)) { continue; } for (const parser of sortedParsers) { const frame = parser(cleanedLine); if (frame) { frames.push(frame); break; } } if (frames.length >= STACKTRACE_FRAME_LIMIT + framesToPop) { break; } } return stripSentryFramesAndReverse(frames.slice(framesToPop)); };}
/** * Gets a stack parser implementation from Options.stackParser * @see Options * * If options contains an array of line parsers, it is converted into a parser */function stackParserFromStackParserOptions(stackParser) { if (Array.isArray(stackParser)) { return createStackParser(...stackParser); } return stackParser;}
/** * Removes Sentry frames from the top and bottom of the stack if present and enforces a limit of max number of frames. * Assumes stack input is ordered from top to bottom and returns the reverse representation so call site of the * function that caused the crash is the last frame in the array. * @hidden */function stripSentryFramesAndReverse(stack) { if (!stack.length) { return []; } const localStack = Array.from(stack); // If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call) if (/sentryWrapped/.test(localStack[localStack.length - 1].function || '')) { localStack.pop(); } // Reversing in the middle of the procedure allows us to just pop the values off the stack localStack.reverse(); // If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call) if (STRIP_FRAME_REGEXP.test(localStack[localStack.length - 1].function || '')) { localStack.pop(); // When using synthetic events, we will have a 2 levels deep stack, as `new Error('Sentry syntheticException')` // is produced within the hub itself, making it: // // Sentry.captureException() // getCurrentHub().captureException() // // instead of just the top `Sentry` call itself. // This forces us to possibly strip an additional frame in the exact same was as above. if (STRIP_FRAME_REGEXP.test(localStack[localStack.length - 1].function || '')) { localStack.pop(); } } return localStack.slice(0, STACKTRACE_FRAME_LIMIT).map(frame => ({ ...frame, filename: frame.filename || localStack[localStack.length - 1].filename, function: frame.function || UNKNOWN_FUNCTION, }));}
const defaultFunctionName = '<anonymous>';
/** * Safely extract function name from itself */function getFunctionName(fn) { try { if (!fn || typeof fn !== 'function') { return defaultFunctionName; } return fn.name || defaultFunctionName; } catch (e) { // Just accessing custom props in some Selenium environments // can cause a "Permission denied" exception (see raven-js#495). return defaultFunctionName; }}
// We keep the handlers globallyconst handlers = {};const instrumented = {};
/** Add a handler function. */function addHandler(type, handler) { handlers[type] = handlers[type] || []; (handlers[type] ).push(handler);}
/** Maybe run an instrumentation function, unless it was already called. */function maybeInstrument(type, instrumentFn) { if (!instrumented[type]) { instrumentFn(); instrumented[type] = true; }}
/** Trigger handlers for a given instrumentation type. */function triggerHandlers(type, data) { const typeHandlers = type && handlers[type]; if (!typeHandlers) { return; } for (const handler of typeHandlers) { try { handler(data); } catch (e) { DEBUG_BUILD$1 && logger.error( `Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(handler)}\nError:`, e, ); } }}
/** * Add an instrumentation handler for when a console.xxx method is called. * * Use at your own risk, this might break without changelog notice, only used internally. * @hidden */function addConsoleInstrumentationHandler(handler) { const type = 'console'; addHandler(type, handler); maybeInstrument(type, instrumentConsole);}
function instrumentConsole() { if (!('console' in GLOBAL_OBJ)) { return; } CONSOLE_LEVELS.forEach(function (level) { if (!(level in GLOBAL_OBJ.console)) { return; } fill(GLOBAL_OBJ.console, level, function (originalConsoleMethod) { originalConsoleMethods[level] = originalConsoleMethod; return function (...args) { const handlerData = { args, level }; triggerHandlers('console', handlerData); const log = originalConsoleMethods[level]; log && log.apply(GLOBAL_OBJ.console, args); }; }); });}
const WINDOW = GLOBAL_OBJ ;
/** * Tells whether current environment supports Fetch API * {@link supportsFetch}. * * @returns Answer to the given question. */function supportsFetch() { if (!('fetch' in WINDOW)) { return false; } try { new Headers(); new Request('http://www.example.com'); new Response(); return true; } catch (e) { return false; }}/** * isNativeFetch checks if the given function is a native implementation of fetch() */// eslint-disable-next-line @typescript-eslint/ban-typesfunction isNativeFetch(func) { return func && /^function fetch\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());}
/** * Tells whether current environment supports Fetch API natively * {@link supportsNativeFetch}. * * @returns true if `window.fetch` is natively implemented, false otherwise */function supportsNativeFetch() { if (typeof EdgeRuntime === 'string') { return true; } if (!supportsFetch()) { return false; } // Fast path to avoid DOM I/O // eslint-disable-next-line @typescript-eslint/unbound-method if (isNativeFetch(WINDOW.fetch)) { return true; } // window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension) // so create a "pure" iframe to see if that has native fetch let result = false; const doc = WINDOW.document; // eslint-disable-next-line deprecation/deprecation if (doc && typeof (doc.createElement ) === 'function') { try { const sandbox = doc.createElement('iframe'); sandbox.hidden = true; doc.head.appendChild(sandbox); if (sandbox.contentWindow && sandbox.contentWindow.fetch) { // eslint-disable-next-line @typescript-eslint/unbound-method result = isNativeFetch(sandbox.contentWindow.fetch); } doc.head.removeChild(sandbox); } catch (err) { DEBUG_BUILD$1 && logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err); } } return result;}
/** * Add an instrumentation handler for when a fetch request happens. * The handler function is called once when the request starts and once when it ends, * which can be identified by checking if it has an `endTimestamp`. * * Use at your own risk, this might break without changelog notice, only used internally. * @hidden */function addFetchInstrumentationHandler(handler) { const type = 'fetch'; addHandler(type, handler); maybeInstrument(type, instrumentFetch);}
function instrumentFetch() { if (!supportsNativeFetch()) { return; } fill(GLOBAL_OBJ, 'fetch', function (originalFetch) { return function (...args) { const { method, url } = parseFetchArgs(args); const handlerData = { args, fetchData: { method, url, }, startTimestamp: Date.now(), }; triggerHandlers('fetch', { ...handlerData, }); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return originalFetch.apply(GLOBAL_OBJ, args).then( (response) => { const finishedHandlerData = { ...handlerData, endTimestamp: Date.now(), response, }; triggerHandlers('fetch', finishedHandlerData); return response; }, (error) => { const erroredHandlerData = { ...handlerData, endTimestamp: Date.now(), error, }; triggerHandlers('fetch', erroredHandlerData); // NOTE: If you are a Sentry user, and you are seeing this stack frame, // it means the sentry.javascript SDK caught an error invoking your application code. // This is expected behavior and NOT indicative of a bug with sentry.javascript. throw error; }, ); }; });}
function hasProp(obj, prop) { return !!obj && typeof obj === 'object' && !!(obj )[prop];}
function getUrlFromResource(resource) { if (typeof resource === 'string') { return resource; } if (!resource) { return ''; } if (hasProp(resource, 'url')) { return resource.url; } if (resource.toString) { return resource.toString(); } return '';}
/** * Parses the fetch arguments to find the used Http method and the url of the request. * Exported for tests only. */function parseFetchArgs(fetchArgs) { if (fetchArgs.length === 0) { return { method: 'GET', url: '' }; } if (fetchArgs.length === 2) { const [url, options] = fetchArgs ; return { url: getUrlFromResource(url), method: hasProp(options, 'method') ? String(options.method).toUpperCase() : 'GET', }; } const arg = fetchArgs[0]; return { url: getUrlFromResource(arg ), method: hasProp(arg, 'method') ? String(arg.method).toUpperCase() : 'GET', };}
let _oldOnErrorHandler = null;
/** * Add an instrumentation handler for when an error is captured by the global error handler. * * Use at your own risk, this might break without changelog notice, only used internally. * @hidden */function addGlobalErrorInstrumentationHandler(handler) { const type = 'error'; addHandler(type, handler); maybeInstrument(type, instrumentError);}
function instrumentError() { _oldOnErrorHandler = GLOBAL_OBJ.onerror; GLOBAL_OBJ.onerror = function ( msg, url, line, column, error, ) { const handlerData = { column, error, line, msg, url, }; triggerHandlers('error', handlerData); if (_oldOnErrorHandler && !_oldOnErrorHandler.__SENTRY_LOADER__) { // eslint-disable-next-line prefer-rest-params return _oldOnErrorHandler.apply(this, arguments); } return false; }; GLOBAL_OBJ.onerror.__SENTRY_INSTRUMENTED__ = true;}
let _oldOnUnhandledRejectionHandler = null;
/** * Add an instrumentation handler for when an unhandled promise rejection is captured. * * Use at your own risk, this might break without changelog notice, only used internally. * @hidden */function addGlobalUnhandledRejectionInstrumentationHandler( handler,) { const type = 'unhandledrejection'; addHandler(type, handler); maybeInstrument(type, instrumentUnhandledRejection);}
function instrumentUnhandledRejection() { _oldOnUnhandledRejectionHandler = GLOBAL_OBJ.onunhandledrejection; GLOBAL_OBJ.onunhandledrejection = function (e) { const handlerData = e; triggerHandlers('unhandledrejection', handlerData); if (_oldOnUnhandledRejectionHandler && !_oldOnUnhandledRejectionHandler.__SENTRY_LOADER__) { // eslint-disable-next-line prefer-rest-params return _oldOnUnhandledRejectionHandler.apply(this, arguments); } return true; }; GLOBAL_OBJ.onunhandledrejection.__SENTRY_INSTRUMENTED__ = true;}
/* eslint-disable @typescript-eslint/no-unsafe-member-access *//* eslint-disable @typescript-eslint/no-explicit-any */
/** * Helper to decycle json objects */function memoBuilder() { const hasWeakSet = typeof WeakSet === 'function'; const inner = hasWeakSet ? new WeakSet() : []; function memoize(obj) { if (hasWeakSet) { if (inner.has(obj)) { return true; } inner.add(obj); return false; } // eslint-disable-next-line @typescript-eslint/prefer-for-of for (let i = 0; i < inner.length; i++) { const value = inner[i]; if (value === obj) { return true; } } inner.push(obj); return false; } function unmemoize(obj) { if (hasWeakSet) { inner.delete(obj); } else { for (let i = 0; i < inner.length; i++) { if (inner[i] === obj) { inner.splice(i, 1); break; } } } } return [memoize, unmemoize];}
/** * UUID4 generator * * @returns string Generated UUID4. */function uuid4() { const gbl = GLOBAL_OBJ ; const crypto = gbl.crypto || gbl.msCrypto; let getRandomByte = () => Math.random() * 16; try { if (crypto && crypto.randomUUID) { return crypto.randomUUID().replace(/-/g, ''); } if (crypto && crypto.getRandomValues) { getRandomByte = () => { // crypto.getRandomValues might return undefined instead of the typed array // in old Chromium versions (e.g. 23.0.1235.0 (151422)) // However, `typedArray` is still filled in-place. // @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#typedarray const typedArray = new Uint8Array(1); crypto.getRandomValues(typedArray); return typedArray[0]; }; } } catch (_) { // some runtimes can crash invoking crypto // https://github.com/getsentry/sentry-javascript/issues/8935 } // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523 // Concatenating the following numbers as strings results in '10000000100040008000100000000000' return (([1e7] ) + 1e3 + 4e3 + 8e3 + 1e11).replace(/[018]/g, c => // eslint-disable-next-line no-bitwise ((c ) ^ ((getRandomByte() & 15) >> ((c ) / 4))).toString(16), );}
function getFirstException(event) { return event.exception && event.exception.values ? event.exception.values[0] : undefined;}
/** * Extracts either message or type+value from an event that can be used for user-facing logs * @returns event's description */function getEventDescription(event) { const { message, event_id: eventId } = event; if (message) { return message; } const firstException = getFirstException(event); if (firstException) { if (firstException.type && firstException.value) { return `${firstException.type}: ${firstException.value}`; } return firstException.type || firstException.value || eventId || '<unknown>'; } return eventId || '<unknown>';}
/** * Adds exception values, type and value to an synthetic Exception. * @param event The event to modify. * @param value Value of the exception. * @param type Type of the exception. * @hidden */function addExceptionTypeValue(event, value, type) { const exception = (event.exception = event.exception || {}); const values = (exception.values = exception.values || []); const firstException = (values[0] = values[0] || {}); if (!firstException.value) { firstException.value = value || ''; } if (!firstException.type) { firstException.type = type || 'Error'; }}
/** * Adds exception mechanism data to a given event. Uses defaults if the second parameter is not passed. * * @param event The event to modify. * @param newMechanism Mechanism data to add to the event. * @hidden */function addExceptionMechanism(event, newMechanism) { const firstException = getFirstException(event); if (!firstException) { return; } const defaultMechanism = { type: 'generic', handled: true }; const currentMechanism = firstException.mechanism; firstException.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism }; if (newMechanism && 'data' in newMechanism) { const mergedData = { ...(currentMechanism && currentMechanism.data), ...newMechanism.data }; firstException.mechanism.data = mergedData; }}
/** * This function adds context (pre/post/line) lines to the provided frame * * @param lines string[] containing all lines * @param frame StackFrame that will be mutated * @param linesOfContext number of context lines we want to add pre/post */function addContextToFrame(lines, frame, linesOfContext = 5) { // When there is no line number in the frame, attaching context is nonsensical and will even break grouping if (frame.lineno === undefined) { return; } const maxLines = lines.length; const sourceLine = Math.max(Math.min(maxLines - 1, frame.lineno - 1), 0); frame.pre_context = lines .slice(Math.max(0, sourceLine - linesOfContext), sourceLine) .map((line) => snipLine(line, 0)); frame.context_line = snipLine(lines[Math.min(maxLines - 1, sourceLine)], frame.colno || 0); frame.post_context = lines .slice(Math.min(sourceLine + 1, maxLines), sourceLine + 1 + linesOfContext) .map((line) => snipLine(line, 0));}
/** * Checks whether or not we've already captured the given exception (note: not an identical exception - the very object * in question), and marks it captured if not. * * This is useful because it's possible for an error to get captured by more than one mechanism. After we intercept and * record an error, we rethrow it (assuming we've intercepted it before it's reached the top-level global handlers), so * that we don't interfere with whatever effects the error might have had were the SDK not there. At that point, because * the error has been rethrown, it's possible for it to bubble up to some other code we've instrumented. If it's not * caught after that, it will bubble all the way up to the global handlers (which of course we also instrument). This * function helps us ensure that even if we encounter the same error more than once, we only record it the first time we * see it. * * Note: It will ignore primitives (always return `false` and not mark them as seen), as properties can't be set on * them. {@link: Object.objectify} can be used on exceptions to convert any that are primitives into their equivalent * object wrapper forms so that this check will always work. However, because we need to flag the exact object which * will get rethrown, and because that rethrowing happens outside of the event processing pipeline, the objectification * must be done before the exception captured. * * @param A thrown exception to check or flag as having been seen * @returns `true` if the exception has already been captured, `false` if not (with the side effect of marking it seen) */function checkOrSetAlreadyCaught(exception) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (exception && (exception ).__sentry_captured__) { return true; } try { // set it this way rather than by assignment so that it's not ennumerable and therefore isn't recorded by the // `ExtraErrorData` integration addNonEnumerableProperty(exception , '__sentry_captured__', true); } catch (err) { // `exception` is a primitive, so we can't mark it seen } return false;}
/** * Checks whether the given input is already an array, and if it isn't, wraps it in one. * * @param maybeArray Input to turn into an array, if necessary * @returns The input, if already an array, or an array with the input as the only element, if not */function arrayify(maybeArray) { return Array.isArray(maybeArray) ? maybeArray : [maybeArray];}
/** * Recursively normalizes the given object. * * - Creates a copy to prevent original input mutation * - Skips non-enumerable properties * - When stringifying, calls `toJSON` if implemented * - Removes circular references * - Translates non-serializable values (`undefined`/`NaN`/functions) to serializable format * - Translates known global objects/classes to a string representations * - Takes care of `Error` object serialization * - Optionally limits depth of final output * - Optionally limits number of properties/elements included in any single object/array * * @param input The object to be normalized. * @param depth The max depth to which to normalize the object. (Anything deeper stringified whole.) * @param maxProperties The max number of elements or properties to be included in any single array or * object in the normallized output. * @returns A normalized version of the object, or `"**non-serializable**"` if any errors are thrown during normalization. */// eslint-disable-next-line @typescript-eslint/no-explicit-anyfunction normalize(input, depth = 100, maxProperties = +Infinity) { try { // since we're at the outermost level, we don't provide a key return visit('', input, depth, maxProperties); } catch (err) { return { ERROR: `**non-serializable** (${err})` }; }}
/** JSDoc */function normalizeToSize( // eslint-disable-next-line @typescript-eslint/no-explicit-any object, // Default Node.js REPL depth depth = 3, // 100kB, as 200kB is max payload size, so half sounds reasonable maxSize = 100 * 1024,) { const normalized = normalize(object, depth); if (jsonSize(normalized) > maxSize) { return normalizeToSize(object, depth - 1, maxSize); } return normalized ;}
/** * Visits a node to perform normalization on it * * @param key The key corresponding to the given node * @param value The node to be visited * @param depth Optional number indicating the maximum recursion depth * @param maxProperties Optional maximum number of properties/elements included in any single object/array * @param memo Optional Memo class handling decycling */function visit( key, value, depth = +Infinity, maxProperties = +Infinity, memo = memoBuilder(),) { const [memoize, unmemoize] = memo; // Get the simple cases out of the way first if ( value == null || // this matches null and undefined -> eqeq not eqeqeq (['number', 'boolean', 'string'].includes(typeof value) && !Number.isNaN(value)) ) { return value ; } const stringified = stringifyValue(key, value); // Anything we could potentially dig into more (objects or arrays) will have come back as `"[object XXXX]"`. // Everything else will have already been serialized, so if we don't see that pattern, we're done. if (!stringified.startsWith('[object ')) { return stringified; } // From here on, we can assert that `value` is either an object or an array. // Do not normalize objects that we know have already been normalized. As a general rule, the // "__sentry_skip_normalization__" property should only be used sparingly and only should only be set on objects that // have already been normalized. if ((value )['__sentry_skip_normalization__']) { return value ; } // We can set `__sentry_override_normalization_depth__` on an object to ensure that from there // We keep a certain amount of depth. // This should be used sparingly, e.g. we use it for the redux integration to ensure we get a certain amount of state. const remainingDepth = typeof (value )['__sentry_override_normalization_depth__'] === 'number' ? ((value )['__sentry_override_normalization_depth__'] ) : depth; // We're also done if we've reached the max depth if (remainingDepth === 0) { // At this point we know `serialized` is a string of the form `"[object XXXX]"`. Clean it up so it's just `"[XXXX]"`. return stringified.replace('object ', ''); } // If we've already visited this branch, bail out, as it's circular reference. If not, note that we're seeing it now. if (memoize(value)) { return '[Circular ~]'; } // If the value has a `toJSON` method, we call it to extract more information const valueWithToJSON = value ; if (valueWithToJSON && typeof valueWithToJSON.toJSON === 'function') { try { const jsonValue = valueWithToJSON.toJSON(); // We need to normalize the return value of `.toJSON()` in case it has circular references return visit('', jsonValue, remainingDepth - 1, maxProperties, memo); } catch (err) { // pass (The built-in `toJSON` failed, but we can still try to do it ourselves) } } // At this point we know we either have an object or an array, we haven't seen it before, and we're going to recurse // because we haven't yet reached the max depth. Create an accumulator to hold the results of visiting each // property/entry, and keep track of the number of items we add to it. const normalized = (Array.isArray(value) ? [] : {}) ; let numAdded = 0; // Before we begin, convert`Error` and`Event` instances into plain objects, since some of each of their relevant // properties are non-enumerable and otherwise would get missed. const visitable = convertToPlainObject(value ); for (const visitKey in visitable) { // Avoid iterating over fields in the prototype if they've somehow been exposed to enumeration. if (!Object.prototype.hasOwnProperty.call(visitable, visitKey)) { continue; } if (numAdded >= maxProperties) { normalized[visitKey] = '[MaxProperties ~]'; break; } // Recursively visit all the child nodes const visitValue = visitable[visitKey]; normalized[visitKey] = visit(visitKey, visitValue, remainingDepth - 1, maxProperties, memo); numAdded++; } // Once we've visited all the branches, remove the parent from memo storage unmemoize(value); // Return accumulated values return normalized;}
/* eslint-disable complexity *//** * Stringify the given value. Handles various known special values and types. * * Not meant to be used on simple primitives which already have a string representation, as it will, for example, turn * the number 1231 into "[Object Number]", nor on `null`, as it will throw. * * @param value The value to stringify * @returns A stringified representation of the given value */function stringifyValue( key, // this type is a tiny bit of a cheat, since this function does handle NaN (which is technically a number), but for // our internal use, it'll do value,) { try { if (key === 'domain' && value && typeof value === 'object' && (value )._events) { return '[Domain]'; } if (key === 'domainEmitter') { return '[DomainEmitter]'; } // It's safe to use `global`, `window`, and `document` here in this manner, as we are asserting using `typeof` first // which won't throw if they are not present. if (typeof global !== 'undefined' && value === global) { return '[Global]'; } // eslint-disable-next-line no-restricted-globals if (typeof window !== 'undefined' && value === window) { return '[Window]'; } // eslint-disable-next-line no-restricted-globals if (typeof document !== 'undefined' && value === document) { return '[Document]'; } if (isVueViewModel(value)) { return '[VueViewModel]'; } // React's SyntheticEvent thingy if (isSyntheticEvent(value)) { return '[SyntheticEvent]'; } if (typeof value === 'number' && value !== value) { return '[NaN]'; } if (typeof value === 'function') { return `[Function: ${getFunctionName(value)}]`; } if (typeof value === 'symbol') { return `[${String(value)}]`; } // stringified BigInts are indistinguishable from regular numbers, so we need to label them to avoid confusion if (typeof value === 'bigint') { return `[BigInt: ${String(value)}]`; } // Now that we've knocked out all the special cases and the primitives, all we have left are objects. Simply casting // them to strings means that instances of classes which haven't defined their `toStringTag` will just come out as // `"[object Object]"`. If we instead look at the constructor's name (which is the same as the name of the class), // we can make sure that only plain objects come out that way. const objName = getConstructorName(value); // Handle HTML Elements if (/^HTML(\w*)Element$/.test(objName)) { return `[HTMLElement: ${objName}]`; } return `[object ${objName}]`; } catch (err) { return `**non-serializable** (${err})`; }}/* eslint-enable complexity */
function getConstructorName(value) { const prototype = Object.getPrototypeOf(value); return prototype ? prototype.constructor.name : 'null prototype';}
/** Calculates bytes size of input string */function utf8Length(value) { // eslint-disable-next-line no-bitwise return ~-encodeURI(value).split(/%..|./).length;}
/** Calculates bytes size of input object */// eslint-disable-next-line @typescript-eslint/no-explicit-anyfunction jsonSize(value) { return utf8Length(JSON.stringify(value));}
// Slightly modified (no IE8 support, ES6) and transcribed to TypeScript// https://github.com/calvinmetcalf/rollup-plugin-node-builtins/blob/63ab8aacd013767445ca299e468d9a60a95328d7/src/es6/path.js//// Copyright Joyent, Inc.and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
/** JSDoc */function normalizeArray(parts, allowAboveRoot) { // if the path tries to go above the root, `up` ends up > 0 let up = 0; for (let i = parts.length - 1; i >= 0; i--) { const last = parts[i]; if (last === '.') { parts.splice(i, 1); } else if (last === '..') { parts.splice(i, 1); up++; } else if (up) { parts.splice(i, 1); up--; } } // if the path is allowed to go above the root, restore leading ..s if (allowAboveRoot) { for (; up--; up) { parts.unshift('..'); } } return parts;}
// Split a filename into [root, dir, basename, ext], unix version// 'root' is just a slash, or nothing.const splitPathRe = /^(\S+:\\|\/?)([\s\S]*?)((?:\.{1,2}|[^/\\]+?|)(\.[^./\\]*|))(?:[/\\]*)$/;/** JSDoc */function splitPath(filename) { // Truncate files names greater than 1024 characters to avoid regex dos // https://github.com/getsentry/sentry-javascript/pull/8737#discussion_r1285719172 const truncated = filename.length > 1024 ? `<truncated>${filename.slice(-1024)}` : filename; const parts = splitPathRe.exec(truncated); return parts ? parts.slice(1) : [];}
// path.resolve([from ...], to)// posix version/** JSDoc */function resolve(...args) { let resolvedPath = ''; let resolvedAbsolute = false; for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { const path = i >= 0 ? args[i] : '/'; // Skip empty entries if (!path) { continue; } resolvedPath = `${path}/${resolvedPath}`; resolvedAbsolute = path.charAt(0) === '/'; } // At this point the path should be resolved to a full absolute path, but // handle relative paths to be safe (might happen when process.cwd() fails) // Normalize the path resolvedPath = normalizeArray( resolvedPath.split('/').filter(p => !!p), !resolvedAbsolute, ).join('/'); return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';}
/** JSDoc */function trim(arr) { let start = 0; for (; start < arr.length; start++) { if (arr[start] !== '') { break; } } let end = arr.length - 1; for (; end >= 0; end--) { if (arr[end] !== '') { break; } } if (start > end) { return []; } return arr.slice(start, end - start + 1);}
// path.relative(from, to)// posix version/** JSDoc */function relative(from, to) { /* eslint-disable no-param-reassign */ from = resolve(from).slice(1); to = resolve(to).slice(1); /* eslint-enable no-param-reassign */ const fromParts = trim(from.split('/')); const toParts = trim(to.split('/')); const length = Math.min(fromParts.length, toParts.length); let samePartsLength = length; for (let i = 0; i < length; i++) { if (fromParts[i] !== toParts[i]) { samePartsLength = i; break; } } let outputParts = []; for (let i = samePartsLength; i < fromParts.length; i++) { outputParts.push('..'); } outputParts = outputParts.concat(toParts.slice(samePartsLength)); return outputParts.join('/');}
/** JSDoc */function dirname(path) { const result = splitPath(path); const root = result[0]; let dir = result[1]; if (!root && !dir) { // No dirname whatsoever return '.'; } if (dir) { // It has a dirname, strip trailing slash dir = dir.slice(0, dir.length - 1); } return root + dir;}
/** JSDoc */function basename(path, ext) { let f = splitPath(path)[2]; if (ext && f.slice(ext.length * -1) === ext) { f = f.slice(0, f.length - ext.length); } return f;}
/* eslint-disable @typescript-eslint/explicit-function-return-type *//* eslint-disable @typescript-eslint/no-explicit-any */
/** SyncPromise internal states */var States; (function (States) { /** Pending */ const PENDING = 0; States[States["PENDING"] = PENDING] = "PENDING"; /** Resolved / OK */ const RESOLVED = 1; States[States["RESOLVED"] = RESOLVED] = "RESOLVED"; /** Rejected / Error */ const REJECTED = 2; States[States["REJECTED"] = REJECTED] = "REJECTED";})(States || (States = {}));
// Overloads so we can call resolvedSyncPromise without arguments and generic argument
/** * Creates a resolved sync promise. * * @param value the value to resolve the promise with * @returns the resolved sync promise */function resolvedSyncPromise(value) { return new SyncPromise(resolve => { resolve(value); });}
/** * Creates a rejected sync promise. * * @param value the value to reject the promise with * @returns the rejected sync promise */function rejectedSyncPromise(reason) { return new SyncPromise((_, reject) => { reject(reason); });}
/** * Thenable class that behaves like a Promise and follows it's interface * but is not async internally */class SyncPromise {
constructor( executor, ) {SyncPromise.prototype.__init.call(this);SyncPromise.prototype.__init2.call(this);SyncPromise.prototype.__init3.call(this);SyncPromise.prototype.__init4.call(this); this._state = States.PENDING; this._handlers = []; try { executor(this._resolve, this._reject); } catch (e) { this._reject(e); } } /** JSDoc */ then( onfulfilled, onrejected, ) { return new SyncPromise((resolve, reject) => { this._handlers.push([ false, result => { if (!onfulfilled) { // TODO: ¯\_(ツ)_/¯ // TODO: FIXME resolve(result ); } else { try { resolve(onfulfilled(result)); } catch (e) { reject(e); } } }, reason => { if (!onrejected) { reject(reason); } else { try { resolve(onrejected(reason)); } catch (e) { reject(e); } } }, ]); this._executeHandlers(); }); } /** JSDoc */ catch( onrejected, ) { return this.then(val => val, onrejected); } /** JSDoc */ finally(onfinally) { return new SyncPromise((resolve, reject) => { let val; let isRejected; return this.then( value => { isRejected = false; val = value; if (onfinally) { onfinally(); } }, reason => { isRejected = true; val = reason; if (onfinally) { onfinally(); } }, ).then(() => { if (isRejected) { reject(val); return; } resolve(val ); }); }); } /** JSDoc */ __init() {this._resolve = (value) => { this._setResult(States.RESOLVED, value); };} /** JSDoc */ __init2() {this._reject = (reason) => { this._setResult(States.REJECTED, reason); };} /** JSDoc */ __init3() {this._setResult = (state, value) => { if (this._state !== States.PENDING) { return; } if (isThenable(value)) { void (value ).then(this._resolve, this._reject); return; } this._state = state; this._value = value; this._executeHandlers(); };} /** JSDoc */ __init4() {this._executeHandlers = () => { if (this._state === States.PENDING) { return; } const cachedHandlers = this._handlers.slice(); this._handlers = []; cachedHandlers.forEach(handler => { if (handler[0]) { return; } if (this._state === States.RESOLVED) { handler[1](this._value ); } if (this._state === States.REJECTED) { handler[2](this._value); } handler[0] = true; }); };}}
/** * Creates an new PromiseBuffer object with the specified limit * @param limit max number of promises that can be stored in the buffer */function makePromiseBuffer(limit) { const buffer = []; function isReady() { return limit === undefined || buffer.length < limit; } /** * Remove a promise from the queue. * * @param task Can be any PromiseLike<T> * @returns Removed promise. */ function remove(task) { return buffer.splice(buffer.indexOf(task), 1)[0]; } /** * Add a promise (representing an in-flight action) to the queue, and set it to remove itself on fulfillment. * * @param taskProducer A function producing any PromiseLike<T>; In previous versions this used to be `task: * PromiseLike<T>`, but under that model, Promises were instantly created on the call-site and their executor * functions therefore ran immediately. Thus, even if the buffer was full, the action still happened. By * requiring the promise to be wrapped in a function, we can defer promise creation until after the buffer * limit check. * @returns The original promise. */ function add(taskProducer) { if (!isReady()) { return rejectedSyncPromise(new SentryError('Not adding Promise because buffer limit was reached.')); } // start the task and add its promise to the queue const task = taskProducer(); if (buffer.indexOf(task) === -1) { buffer.push(task); } void task .then(() => remove(task)) // Use `then(null, rejectionHandler)` rather than `catch(rejectionHandler)` so that we can use `PromiseLike` // rather than `Promise`. `PromiseLike` doesn't have a `.catch` method, making its polyfill smaller. (ES5 didn't // have promises, so TS has to polyfill when down-compiling.) .then(null, () => remove(task).then(null, () => { // We have to add another catch here because `remove()` starts a new promise chain. }), ); return task; } /** * Wait for all promises in the queue to resolve or for timeout to expire, whichever comes first. * * @param timeout The time, in ms, after which to resolve to `false` if the queue is still non-empty. Passing `0` (or * not passing anything) will make the promise wait as long as it takes for the queue to drain before resolving to * `true`. * @returns A promise which will resolve to `true` if the queue is already empty or drains before the timeout, and * `false` otherwise */ function drain(timeout) { return new SyncPromise((resolve, reject) => { let counter = buffer.length; if (!counter) { return resolve(true); } // wait for `timeout` ms and then resolve to `false` (if not cancelled first) const capturedSetTimeout = setTimeout(() => { if (timeout && timeout > 0) { resolve(false); } }, timeout); // if all promises resolve in time, cancel the timer and resolve to `true` buffer.forEach(item => { void resolvedSyncPromise(item).then(() => { if (!--counter) { clearTimeout(capturedSetTimeout); resolve(true); } }, reject); }); }); } return { $: buffer, add, drain, };}
/** * This code was originally copied from the 'cookie` module at v0.5.0 and was simplified for our use case. * https://github.com/jshttp/cookie/blob/a0c84147aab6266bdb3996cf4062e93907c0b0fc/index.js * It had the following license: * * (The MIT License) * * Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com> * Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//** * Parses a cookie string */function parseCookie(str) { const obj = {}; let index = 0; while (index < str.length) { const eqIdx = str.indexOf('=', index); // no more cookie pairs if (eqIdx === -1) { break; } let endIdx = str.indexOf(';', index); if (endIdx === -1) { endIdx = str.length; } else if (endIdx < eqIdx) { // backtrack on prior semicolon index = str.lastIndexOf(';', eqIdx - 1) + 1; continue; } const key = str.slice(index, eqIdx).trim(); // only assign once if (undefined === obj[key]) { let val = str.slice(eqIdx + 1, endIdx).trim(); // quoted values if (val.charCodeAt(0) === 0x22) { val = val.slice(1, -1); } try { obj[key] = val.indexOf('%') !== -1 ? decodeURIComponent(val) : val; } catch (e) { obj[key] = val; } } index = endIdx + 1; } return obj;}
/** * Parses string form of URL into an object * // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B * // intentionally using regex and not <a/> href parsing trick because React Native and other * // environments where DOM might not be available * @returns parsed URL object *//** * Strip the query string and fragment off of a given URL or path (if present) * * @param urlPath Full URL or path, including possible query string and/or fragment * @returns URL or path without query string or fragment */function stripUrlQueryAndFragment(urlPath) { // eslint-disable-next-line no-useless-escape return urlPath.split(/[\?#]/, 1)[0];}
const DEFAULT_INCLUDES = { ip: false, request: true, transaction: true, user: true,};const DEFAULT_REQUEST_INCLUDES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];const DEFAULT_USER_INCLUDES = ['id', 'username', 'email'];
/** * Options deciding what parts of the request to use when enhancing an event *//** * Extracts a complete and parameterized path from the request object and uses it to construct transaction name. * If the parameterized transaction name cannot be extracted, we fall back to the raw URL. * * Additionally, this function determines and returns the transaction name source * * eg. GET /mountpoint/user/:id * * @param req A request object * @param options What to include in the transaction name (method, path, or a custom route name to be * used instead of the request's route) * * @returns A tuple of the fully constructed transaction name [0] and its source [1] (can be either 'route' or 'url') */function extractPathForTransaction( req, options = {},) { const method = req.method && req.method.toUpperCase(); let path = ''; let source = 'url'; // Check to see if there's a parameterized route we can use (as there is in Express) if (options.customRoute || req.route) { path = options.customRoute || `${req.baseUrl || ''}${req.route && req.route.path}`; source = 'route'; } // Otherwise, just take the original URL else if (req.originalUrl || req.url) { path = stripUrlQueryAndFragment(req.originalUrl || req.url || ''); } let name = ''; if (options.method && method) { name += method; } if (options.method && options.path) { name += ' '; } if (options.path && path) { name += path; } return [name, source];}
/** JSDoc */function extractTransaction(req, type) { switch (type) { case 'path': { return extractPathForTransaction(req, { path: true })[0]; } case 'handler': { return (req.route && req.route.stack && req.route.stack[0] && req.route.stack[0].name) || '<anonymous>'; } case 'methodPath': default: { // if exist _reconstructedRoute return that path instead of route.path const customRoute = req._reconstructedRoute ? req._reconstructedRoute : undefined; return extractPathForTransaction(req, { path: true, method: true, customRoute })[0]; } }}
/** JSDoc */function extractUserData( user, keys,) { const extractedUser = {}; const attributes = Array.isArray(keys) ? keys : DEFAULT_USER_INCLUDES; attributes.forEach(key => { if (user && key in user) { extractedUser[key] = user[key]; } }); return extractedUser;}
/** * Normalize data from the request object, accounting for framework differences. * * @param req The request object from which to extract data * @param options.include An optional array of keys to include in the normalized data. Defaults to * DEFAULT_REQUEST_INCLUDES if not provided. * @param options.deps Injected, platform-specific dependencies * @returns An object containing normalized request data */function extractRequestData( req, options,) { const { include = DEFAULT_REQUEST_INCLUDES } = options || {}; // eslint-disable-next-line @typescript-eslint/no-explicit-any const requestData = {}; // headers: // node, express, koa, nextjs: req.headers const headers = (req.headers || {}); // method: // node, express, koa, nextjs: req.method const method = req.method; // host: // express: req.hostname in > 4 and req.host in < 4 // koa: req.host // node, nextjs: req.headers.host // Express 4 mistakenly strips off port number from req.host / req.hostname so we can't rely on them // See: https://github.com/expressjs/express/issues/3047#issuecomment-236653223 // Also: https://github.com/getsentry/sentry-javascript/issues/1917 const host = headers.host || req.hostname || req.host || '<no host>'; // protocol: // node, nextjs: <n/a> // express, koa: req.protocol const protocol = req.protocol === 'https' || (req.socket && req.socket.encrypted) ? 'https' : 'http'; // url (including path and query string): // node, express: req.originalUrl // koa, nextjs: req.url const originalUrl = req.originalUrl || req.url || ''; // absolute url const absoluteUrl = originalUrl.startsWith(protocol) ? originalUrl : `${protocol}://${host}${originalUrl}`; include.forEach(key => { switch (key) { case 'headers': { requestData.headers = headers; // Remove the Cookie header in case cookie data should not be included in the event if (!include.includes('cookies')) { delete (requestData.headers ).cookie; } break; } case 'method': { requestData.method = method; break; } case 'url': { requestData.url = absoluteUrl; break; } case 'cookies': { // cookies: // node, express, koa: req.headers.cookie // vercel, sails.js, express (w/ cookie middleware), nextjs: req.cookies requestData.cookies = // TODO (v8 / #5257): We're only sending the empty object for backwards compatibility, so the last bit can // come off in v8 req.cookies || (headers.cookie && parseCookie(headers.cookie)) || {}; break; } case 'query_string': { // query string: // node: req.url (raw) // express, koa, nextjs: req.query requestData.query_string = extractQueryParams(req); break; } case 'data': { if (method === 'GET' || method === 'HEAD') { break; } // body data: // express, koa, nextjs: req.body // // when using node by itself, you have to read the incoming stream(see // https://nodejs.dev/learn/get-http-request-body-data-using-nodejs); if a user is doing that, we can't know // where they're going to store the final result, so they'll have to capture this data themselves if (req.body !== undefined) { requestData.data = isString(req.body) ? req.body : JSON.stringify(normalize(req.body)); } break; } default: { if ({}.hasOwnProperty.call(req, key)) { requestData[key] = (req )[key]; } } } }); return requestData;}
/** * Add data from the given request to the given event * * @param event The event to which the request data will be added * @param req Request object * @param options.include Flags to control what data is included * @param options.deps Injected platform-specific dependencies * @returns The mutated `Event` object */function addRequestDataToEvent( event, req, options,) { const include = { ...DEFAULT_INCLUDES, ...(options && options.include), }; if (include.request) { const extractedRequestData = Array.isArray(include.request) ? extractRequestData(req, { include: include.request }) : extractRequestData(req); event.request = { ...event.request, ...extractedRequestData, }; } if (include.user) { const extractedUser = req.user && isPlainObject(req.user) ? extractUserData(req.user, include.user) : {}; if (Object.keys(extractedUser).length) { event.user = { ...event.user, ...extractedUser, }; } } // client ip: // node, nextjs: req.socket.remoteAddress // express, koa: req.ip if (include.ip) { const ip = req.ip || (req.socket && req.socket.remoteAddress); if (ip) { event.user = { ...event.user, ip_address: ip, }; } } if (include.transaction && !event.transaction && event.type === 'transaction') { // TODO do we even need this anymore? // TODO make this work for nextjs event.transaction = extractTransaction(req, include.transaction); } return event;}
function extractQueryParams(req) { // url (including path and query string): // node, express: req.originalUrl // koa, nextjs: req.url let originalUrl = req.originalUrl || req.url || ''; if (!originalUrl) { return; } // The `URL` constructor can't handle internal URLs of the form `/some/path/here`, so stick a dummy protocol and // hostname on the beginning. Since the point here is just to grab the query string, it doesn't matter what we use. if (originalUrl.startsWith('/')) { originalUrl = `http://dogs.are.great${originalUrl}`; } try { const queryParams = req.query || new URL(originalUrl).search.slice(1); return queryParams.length ? queryParams : undefined; } catch (e2) { return undefined; }}
// Note: Ideally the `SeverityLevel` type would be derived from `validSeverityLevels`, but that would mean either//// a) moving `validSeverityLevels` to `@sentry/types`,// b) moving the`SeverityLevel` type here, or// c) importing `validSeverityLevels` from here into `@sentry/types`.//// Option A would make `@sentry/types` a runtime dependency of `@sentry/utils` (not good), and options B and C would// create a circular dependency between `@sentry/types` and `@sentry/utils` (also not good). So a TODO accompanying the// type, reminding anyone who changes it to change this list also, will have to do.
const validSeverityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug'];
/** * Converts a string-based level into a `SeverityLevel`, normalizing it along the way. * * @param level String representation of desired `SeverityLevel`. * @returns The `SeverityLevel` corresponding to the given string, or 'log' if the string isn't a valid level. */function severityLevelFromString(level) { return (level === 'warn' ? 'warning' : validSeverityLevels.includes(level) ? level : 'log') ;}
/** * Does this filename look like it's part of the app code? */function filenameIsInApp(filename, isNative = false) { const isInternal = isNative || (filename && // It's not internal if it's an absolute linux path !filename.startsWith('/') && // It's not internal if it's an absolute windows path !filename.match(/^[A-Z]:/) && // It's not internal if the path is starting with a dot !filename.startsWith('.') && // It's not internal if the frame has a protocol. In node, this is usually the case if the file got pre-processed with a bundler like webpack !filename.match(/^[a-zA-Z]([a-zA-Z0-9.\-+])*:\/\//)); // Schema from: https://stackoverflow.com/a/3641782 // in_app is all that's not an internal Node function or a module within node_modules // note that isNative appears to return true even for node core libraries // see https://github.com/getsentry/raven-node/issues/176 return !isInternal && filename !== undefined && !filename.includes('node_modules/');}
/** Node Stack line parser */function node(getModule) { const FILENAME_MATCH = /^\s*[-]{4,}$/; const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/; // eslint-disable-next-line complexity return (line) => { const lineMatch = line.match(FULL_MATCH); if (lineMatch) { let object; let method; let functionName; let typeName; let methodName; if (lineMatch[1]) { functionName = lineMatch[1]; let methodStart = functionName.lastIndexOf('.'); if (functionName[methodStart - 1] === '.') { methodStart--; } if (methodStart > 0) { object = functionName.slice(0, methodStart); method = functionName.slice(methodStart + 1); const objectEnd = object.indexOf('.Module'); if (objectEnd > 0) { functionName = functionName.slice(objectEnd + 1); object = object.slice(0, objectEnd); } } typeName = undefined; } if (method) { typeName = object; methodName = method; } if (method === '<anonymous>') { methodName = undefined; functionName = undefined; } if (functionName === undefined) { methodName = methodName || UNKNOWN_FUNCTION; functionName = typeName ? `${typeName}.${methodName}` : methodName; } let filename = lineMatch[2] && lineMatch[2].startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2]; const isNative = lineMatch[5] === 'native'; // If it's a Windows path, trim the leading slash so that `/C:/foo` becomes `C:/foo` if (filename && filename.match(/\/[A-Z]:/)) { filename = filename.slice(1); } if (!filename && lineMatch[5] && !isNative) { filename = lineMatch[5]; } return { filename, module: getModule ? getModule(filename) : undefined, function: functionName, lineno: parseInt(lineMatch[3], 10) || undefined, colno: parseInt(lineMatch[4], 10) || undefined, in_app: filenameIsInApp(filename, isNative), }; } if (line.match(FILENAME_MATCH)) { return { filename: line, }; } return undefined; };}
/** * Node.js stack line parser * * This is in @sentry/utils so it can be used from the Electron SDK in the browser for when `nodeIntegration == true`. * This allows it to be used without referencing or importing any node specific code which causes bundlers to complain */function nodeStackLineParser(getModule) { return [90, node(getModule)];}
const ONE_SECOND_IN_MS = 1000;
/** * A partial definition of the [Performance Web API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Performance} * for accessing a high-resolution monotonic clock. *//** * Returns a timestamp in seconds since the UNIX epoch using the Date API. * * TODO(v8): Return type should be rounded. */function dateTimestampInSeconds() { return Date.now() / ONE_SECOND_IN_MS;}
/** * Returns a wrapper around the native Performance API browser implementation, or undefined for browsers that do not * support the API. * * Wrapping the native API works around differences in behavior from different browsers. */function createUnixTimestampInSecondsFunc() { const { performance } = GLOBAL_OBJ ; if (!performance || !performance.now) { return dateTimestampInSeconds; } // Some browser and environments don't have a timeOrigin, so we fallback to // using Date.now() to compute the starting time. const approxStartingTimeOrigin = Date.now() - performance.now(); const timeOrigin = performance.timeOrigin == undefined ? approxStartingTimeOrigin : performance.timeOrigin; // performance.now() is a monotonic clock, which means it starts at 0 when the process begins. To get the current // wall clock time (actual UNIX timestamp), we need to add the starting time origin and the current time elapsed. // // TODO: This does not account for the case where the monotonic clock that powers performance.now() drifts from the // wall clock time, which causes the returned timestamp to be inaccurate. We should investigate how to detect and // correct for this. // See: https://github.com/getsentry/sentry-javascript/issues/2590 // See: https://github.com/mdn/content/issues/4713 // See: https://dev.to/noamr/when-a-millisecond-is-not-a-millisecond-3h6 return () => { return (timeOrigin + performance.now()) / ONE_SECOND_IN_MS; };}
/** * Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the * availability of the Performance API. * * BUG: Note that because of how browsers implement the Performance API, the clock might stop when the computer is * asleep. This creates a skew between `dateTimestampInSeconds` and `timestampInSeconds`. The * skew can grow to arbitrary amounts like days, weeks or months. * See https://github.com/getsentry/sentry-javascript/issues/2590. */const timestampInSeconds = createUnixTimestampInSecondsFunc();
/** * The number of milliseconds since the UNIX epoch. This value is only usable in a browser, and only when the * performance API is available. */(() => { // Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or // performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin // data as reliable if they are within a reasonable threshold of the current time. const { performance } = GLOBAL_OBJ ; if (!performance || !performance.now) { return undefined; } const threshold = 3600 * 1000; const performanceNow = performance.now(); const dateNow = Date.now(); // if timeOrigin isn't available set delta to threshold so it isn't used const timeOriginDelta = performance.timeOrigin ? Math.abs(performance.timeOrigin + performanceNow - dateNow) : threshold; const timeOriginIsReliable = timeOriginDelta < threshold; // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. // Also as of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always // a valid fallback. In the absence of an initial time provided by the browser, fallback to the current time from the // Date API. // eslint-disable-next-line deprecation/deprecation const navigationStart = performance.timing && performance.timing.navigationStart; const hasNavigationStart = typeof navigationStart === 'number'; // if navigationStart isn't available set delta to threshold so it isn't used const navigationStartDelta = hasNavigationStart ? Math.abs(navigationStart + performanceNow - dateNow) : threshold; const navigationStartIsReliable = navigationStartDelta < threshold; if (timeOriginIsReliable || navigationStartIsReliable) { // Use the more reliable time origin if (timeOriginDelta <= navigationStartDelta) { return performance.timeOrigin; } else { return navigationStart; } } return dateNow;})();
const SENTRY_BAGGAGE_KEY_PREFIX = 'sentry-';
const SENTRY_BAGGAGE_KEY_PREFIX_REGEX = /^sentry-/;
/** * Takes a baggage header and turns it into Dynamic Sampling Context, by extracting all the "sentry-" prefixed values * from it. * * @param baggageHeader A very bread definition of a baggage header as it might appear in various frameworks. * @returns The Dynamic Sampling Context that was found on `baggageHeader`, if there was any, `undefined` otherwise. */function baggageHeaderToDynamicSamplingContext( // Very liberal definition of what any incoming header might look like baggageHeader,) { const baggageObject = parseBaggageHeader(baggageHeader); if (!baggageObject) { return undefined; } // Read all "sentry-" prefixed values out of the baggage object and put it onto a dynamic sampling context object. const dynamicSamplingContext = Object.entries(baggageObject).reduce((acc, [key, value]) => { if (key.match(SENTRY_BAGGAGE_KEY_PREFIX_REGEX)) { const nonPrefixedKey = key.slice(SENTRY_BAGGAGE_KEY_PREFIX.length); acc[nonPrefixedKey] = value; } return acc; }, {}); // Only return a dynamic sampling context object if there are keys in it. // A keyless object means there were no sentry values on the header, which means that there is no DSC. if (Object.keys(dynamicSamplingContext).length > 0) { return dynamicSamplingContext ; } else { return undefined; }}
/** * Take a baggage header and parse it into an object. */function parseBaggageHeader( baggageHeader,) { if (!baggageHeader || (!isString(baggageHeader) && !Array.isArray(baggageHeader))) { return undefined; } if (Array.isArray(baggageHeader)) { // Combine all baggage headers into one object containing the baggage values so we can later read the Sentry-DSC-values from it return baggageHeader.reduce((acc, curr) => { const currBaggageObject = baggageHeaderToObject(curr); for (const key of Object.keys(currBaggageObject)) { acc[key] = currBaggageObject[key]; } return acc; }, {}); } return baggageHeaderToObject(baggageHeader);}
/** * Will parse a baggage header, which is a simple key-value map, into a flat object. * * @param baggageHeader The baggage header to parse. * @returns a flat object containing all the key-value pairs from `baggageHeader`. */function baggageHeaderToObject(baggageHeader) { return baggageHeader .split(',') .map(baggageEntry => baggageEntry.split('=').map(keyOrValue => decodeURIComponent(keyOrValue.trim()))) .reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {});}
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor -- RegExp is used for readability hereconst TRACEPARENT_REGEXP = new RegExp( '^[ \\t]*' + // whitespace '([0-9a-f]{32})?' + // trace_id '-?([0-9a-f]{16})?' + // span_id '-?([01])?' + // sampled '[ \\t]*$', // whitespace);
/** * Extract transaction context data from a `sentry-trace` header. * * @param traceparent Traceparent string * * @returns Object containing data from the header, or undefined if traceparent string is malformed */function extractTraceparentData(traceparent) { if (!traceparent) { return undefined; } const matches = traceparent.match(TRACEPARENT_REGEXP); if (!matches) { return undefined; } let parentSampled; if (matches[3] === '1') { parentSampled = true; } else if (matches[3] === '0') { parentSampled = false; } return { traceId: matches[1], parentSampled, parentSpanId: matches[2], };}
/** * Create a propagation context from incoming headers or * creates a minimal new one if the headers are undefined. */function propagationContextFromHeaders( sentryTrace, baggage,) { const traceparentData = extractTraceparentData(sentryTrace); const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggage); const { traceId, parentSpanId, parentSampled } = traceparentData || {}; if (!traceparentData) { return { traceId: traceId || uuid4(), spanId: uuid4().substring(16), }; } else { return { traceId: traceId || uuid4(), parentSpanId: parentSpanId || uuid4().substring(16), spanId: uuid4().substring(16), sampled: parentSampled, dsc: dynamicSamplingContext || {}, // If we have traceparent data but no DSC it means we are not head of trace and we must freeze it }; }}
/** * Create sentry-trace header from span context values. */function generateSentryTraceHeader( traceId = uuid4(), spanId = uuid4().substring(16), sampled,) { let sampledString = ''; if (sampled !== undefined) { sampledString = sampled ? '-1' : '-0'; } return `${traceId}-${spanId}${sampledString}`;}
/** * Creates an envelope. * Make sure to always explicitly provide the generic to this function * so that the envelope types resolve correctly. */function createEnvelope(headers, items = []) { return [headers, items] ;}
/** * Add an item to an envelope. * Make sure to always explicitly provide the generic to this function * so that the envelope types resolve correctly. */function addItemToEnvelope(envelope, newItem) { const [headers, items] = envelope; return [headers, [...items, newItem]] ;}
/** * Convenience function to loop through the items and item types of an envelope. * (This function was mostly created because working with envelope types is painful at the moment) * * If the callback returns true, the rest of the items will be skipped. */function forEachEnvelopeItem( envelope, callback,) { const envelopeItems = envelope[1]; for (const envelopeItem of envelopeItems) { const envelopeItemType = envelopeItem[0].type; const result = callback(envelopeItem, envelopeItemType); if (result) { return true; } } return false;}
/** * Encode a string to UTF8 array. */function encodeUTF8(input) { return GLOBAL_OBJ.__SENTRY__ && GLOBAL_OBJ.__SENTRY__.encodePolyfill ? GLOBAL_OBJ.__SENTRY__.encodePolyfill(input) : new TextEncoder().encode(input);}
/** * Serializes an envelope. */function serializeEnvelope(envelope) { const [envHeaders, items] = envelope; // Initially we construct our envelope as a string and only convert to binary chunks if we encounter binary data let parts = JSON.stringify(envHeaders); function append(next) { if (typeof parts === 'string') { parts = typeof next === 'string' ? parts + next : [encodeUTF8(parts), next]; } else { parts.push(typeof next === 'string' ? encodeUTF8(next) : next); } } for (const item of items) { const [itemHeaders, payload] = item; append(`\n${JSON.stringify(itemHeaders)}\n`); if (typeof payload === 'string' || payload instanceof Uint8Array) { append(payload); } else { let stringifiedPayload; try { stringifiedPayload = JSON.stringify(payload); } catch (e) { // In case, despite all our efforts to keep `payload` circular-dependency-free, `JSON.strinify()` still // fails, we try again after normalizing it again with infinite normalization depth. This of course has a // performance impact but in this case a performance hit is better than throwing. stringifiedPayload = JSON.stringify(normalize(payload)); } append(stringifiedPayload); } } return typeof parts === 'string' ? parts : concatBuffers(parts);}
function concatBuffers(buffers) { const totalLength = buffers.reduce((acc, buf) => acc + buf.length, 0); const merged = new Uint8Array(totalLength); let offset = 0; for (const buffer of buffers) { merged.set(buffer, offset); offset += buffer.length; } return merged;}
/** * Creates attachment envelope items */function createAttachmentEnvelopeItem(attachment) { const buffer = typeof attachment.data === 'string' ? encodeUTF8(attachment.data) : attachment.data; return [ dropUndefinedKeys({ type: 'attachment', length: buffer.length, filename: attachment.filename, content_type: attachment.contentType, attachment_type: attachment.attachmentType, }), buffer, ];}
const ITEM_TYPE_TO_DATA_CATEGORY_MAP = { session: 'session', sessions: 'session', attachment: 'attachment', transaction: 'transaction', event: 'error', client_report: 'internal', user_report: 'default', profile: 'profile', replay_event: 'replay', replay_recording: 'replay', check_in: 'monitor', feedback: 'feedback', span: 'span', statsd: 'metric_bucket',};
/** * Maps the type of an envelope item to a data category. */function envelopeItemTypeToDataCategory(type) { return ITEM_TYPE_TO_DATA_CATEGORY_MAP[type];}
/** Extracts the minimal SDK info from the metadata or an events */function getSdkMetadataForEnvelopeHeader(metadataOrEvent) { if (!metadataOrEvent || !metadataOrEvent.sdk) { return; } const { name, version } = metadataOrEvent.sdk; return { name, version };}
/** * Creates event envelope headers, based on event, sdk info and tunnel * Note: This function was extracted from the core package to make it available in Replay */function createEventEnvelopeHeaders( event, sdkInfo, tunnel, dsn,) { const dynamicSamplingContext = event.sdkProcessingMetadata && event.sdkProcessingMetadata.dynamicSamplingContext; return { event_id: event.event_id , sent_at: new Date().toISOString(), ...(sdkInfo && { sdk: sdkInfo }), ...(!!tunnel && dsn && { dsn: dsnToString(dsn) }), ...(dynamicSamplingContext && { trace: dropUndefinedKeys({ ...dynamicSamplingContext }), }), };}
// Intentionally keeping the key broad, as we don't know for sure what rate limit headers get returned from backend
const DEFAULT_RETRY_AFTER = 60 * 1000; // 60 seconds
/** * Extracts Retry-After value from the request header or returns default value * @param header string representation of 'Retry-After' header * @param now current unix timestamp * */function parseRetryAfterHeader(header, now = Date.now()) { const headerDelay = parseInt(`${header}`, 10); if (!isNaN(headerDelay)) { return headerDelay * 1000; } const headerDate = Date.parse(`${header}`); if (!isNaN(headerDate)) { return headerDate - now; } return DEFAULT_RETRY_AFTER;}
/** * Gets the time that the given category is disabled until for rate limiting. * In case no category-specific limit is set but a general rate limit across all categories is active, * that time is returned. * * @return the time in ms that the category is disabled until or 0 if there's no active rate limit. */function disabledUntil(limits, dataCategory) { return limits[dataCategory] || limits.all || 0;}
/** * Checks if a category is rate limited */function isRateLimited(limits, dataCategory, now = Date.now()) { return disabledUntil(limits, dataCategory) > now;}
/** * Update ratelimits from incoming headers. * * @return the updated RateLimits object. */function updateRateLimits( limits, { statusCode, headers }, now = Date.now(),) { const updatedRateLimits = { ...limits, }; // "The name is case-insensitive." // https://developer.mozilla.org/en-US/docs/Web/API/Headers/get const rateLimitHeader = headers && headers['x-sentry-rate-limits']; const retryAfterHeader = headers && headers['retry-after']; if (rateLimitHeader) { /** * rate limit headers are of the form * <header>,<header>,.. * where each <header> is of the form * <retry_after>: <categories>: <scope>: <reason_code>: <namespaces> * where * <retry_after> is a delay in seconds * <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form * <category>;<category>;... * <scope> is what's being limited (org, project, or key) - ignored by SDK * <reason_code> is an arbitrary string like "org_quota" - ignored by SDK * <namespaces> Semicolon-separated list of metric namespace identifiers. Defines which namespace(s) will be affected. * Only present if rate limit applies to the metric_bucket data category. */ for (const limit of rateLimitHeader.trim().split(',')) { const [retryAfter, categories, , , namespaces] = limit.split(':', 5); const headerDelay = parseInt(retryAfter, 10); const delay = (!isNaN(headerDelay) ? headerDelay : 60) * 1000; // 60sec default if (!categories) { updatedRateLimits.all = now + delay; } else { for (const category of categories.split(';')) { if (category === 'metric_bucket') { // namespaces will be present when category === 'metric_bucket' if (!namespaces || namespaces.split(';').includes('custom')) { updatedRateLimits[category] = now + delay; } } else { updatedRateLimits[category] = now + delay; } } } } } else if (retryAfterHeader) { updatedRateLimits.all = now + parseRetryAfterHeader(retryAfterHeader, now); } else if (statusCode === 429) { updatedRateLimits.all = now + 60 * 1000; } return updatedRateLimits;}
/** * Extracts stack frames from the error.stack string */function parseStackFrames(stackParser, error) { return stackParser(error.stack || '', 1);}
/** * Extracts stack frames from the error and builds a Sentry Exception */function exceptionFromError(stackParser, error) { const exception = { type: error.name || error.constructor.name, value: error.message, }; const frames = parseStackFrames(stackParser, error); if (frames.length) { exception.stacktrace = { frames }; } return exception;}
/** If a plain object has a property that is an `Error`, return this error. */function getErrorPropertyFromObject(obj) { for (const prop in obj) { if (Object.prototype.hasOwnProperty.call(obj, prop)) { const value = obj[prop]; if (value instanceof Error) { return value; } } } return undefined;}
function getMessageForObject(exception) { if ('name' in exception && typeof exception.name === 'string') { let message = `'${exception.name}' captured as exception`; if ('message' in exception && typeof exception.message === 'string') { message += ` with message '${exception.message}'`; } return message; } else if ('message' in exception && typeof exception.message === 'string') { return exception.message; } const keys = extractExceptionKeysForMessage(exception); // Some ErrorEvent instances do not have an `error` property, which is why they are not handled before // We still want to try to get a decent message for these cases if (isErrorEvent$1(exception)) { return `Event \`ErrorEvent\` captured as exception with message \`${exception.message}\``; } const className = getObjectClassName(exception); return `${ className && className !== 'Object' ? `'${className}'` : 'Object' } captured as exception with keys: ${keys}`;}
function getObjectClassName(obj) { try { const prototype = Object.getPrototypeOf(obj); return prototype ? prototype.constructor.name : undefined; } catch (e) { // ignore errors here }}
function getException( client, mechanism, exception, hint,) { if (isError(exception)) { return [exception, undefined]; } // Mutate this! mechanism.synthetic = true; if (isPlainObject(exception)) { const normalizeDepth = client && client.getOptions().normalizeDepth; const extras = { ['__serialized__']: normalizeToSize(exception , normalizeDepth) }; const errorFromProp = getErrorPropertyFromObject(exception); if (errorFromProp) { return [errorFromProp, extras]; } const message = getMessageForObject(exception); const ex = (hint && hint.syntheticException) || new Error(message); ex.message = message; return [ex, extras]; } // This handles when someone does: `throw "something awesome";` // We use synthesized Error here so we can extract a (rough) stack trace. const ex = (hint && hint.syntheticException) || new Error(exception ); ex.message = `${exception}`; return [ex, undefined];}
/** * Builds and Event from a Exception * @hidden */function eventFromUnknownInput( client, stackParser, exception, hint,) { const providedMechanism = hint && hint.data && (hint.data ).mechanism; const mechanism = providedMechanism || { handled: true, type: 'generic', }; const [ex, extras] = getException(client, mechanism, exception, hint); const event = { exception: { values: [exceptionFromError(stackParser, ex)], }, }; if (extras) { event.extra = extras; } addExceptionTypeValue(event, undefined, undefined); addExceptionMechanism(event, mechanism); return { ...event, event_id: hint && hint.event_id, };}
/** * Builds and Event from a Message * @hidden */function eventFromMessage( stackParser, message, level = 'info', hint, attachStacktrace,) { const event = { event_id: hint && hint.event_id, level, }; if (attachStacktrace && hint && hint.syntheticException) { const frames = parseStackFrames(stackParser, hint.syntheticException); if (frames.length) { event.exception = { values: [ { value: message, stacktrace: { frames }, }, ], }; } } if (isParameterizedString(message)) { const { __sentry_template_string__, __sentry_template_values__ } = message; event.logentry = { message: __sentry_template_string__, params: __sentry_template_values__, }; return event; } event.message = message; return event;}
/** A simple Least Recently Used map */class LRUMap {
constructor( _maxSize) {this._maxSize = _maxSize; this._cache = new Map(); } /** Get the current size of the cache */ get size() { return this._cache.size; } /** Get an entry or undefined if it was not in the cache. Re-inserts to update the recently used order */ get(key) { const value = this._cache.get(key); if (value === undefined) { return undefined; } // Remove and re-insert to update the order this._cache.delete(key); this._cache.set(key, value); return value; } /** Insert an entry and evict an older entry if we've reached maxSize */ set(key, value) { if (this._cache.size >= this._maxSize) { // keys() returns an iterator in insertion order so keys().next() gives us the oldest key this._cache.delete(this._cache.keys().next().value); } this._cache.set(key, value); } /** Remove an entry and return the entry if it was in the cache */ remove(key) { const value = this._cache.get(key); if (value) { this._cache.delete(key); } return value; } /** Clear all entries */ clear() { this._cache.clear(); } /** Get all the keys */ keys() { return Array.from(this._cache.keys()); } /** Get all the values */ values() { const values = []; this._cache.forEach(value => values.push(value)); return values; }}
/** * 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. * * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking. */const DEBUG_BUILD = (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__);
/** * An object that contains a hub and maintains a scope stack. * @hidden *//** * Returns the global shim registry. * * FIXME: This function is problematic, because despite always returning a valid Carrier, * it has an optional `__SENTRY__` property, which then in turn requires us to always perform an unnecessary check * at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there. **/function getMainCarrier() { // This ensures a Sentry carrier exists getSentryCarrier(GLOBAL_OBJ); return GLOBAL_OBJ;}
/** Will either get the existing sentry carrier, or create a new one. */function getSentryCarrier(carrier) { if (!carrier.__SENTRY__) { carrier.__SENTRY__ = { extensions: {}, }; } return carrier.__SENTRY__;}
/** * Creates a new `Session` object by setting certain default parameters. If optional @param context * is passed, the passed properties are applied to the session object. * * @param context (optional) additional properties to be applied to the returned session object * * @returns a new `Session` object */function makeSession(context) { // Both timestamp and started are in seconds since the UNIX epoch. const startingTime = timestampInSeconds(); const session = { sid: uuid4(), init: true, timestamp: startingTime, started: startingTime, duration: 0, status: 'ok', errors: 0, ignoreDuration: false, toJSON: () => sessionToJSON(session), }; if (context) { updateSession(session, context); } return session;}
/** * Updates a session object with the properties passed in the context. * * Note that this function mutates the passed object and returns void. * (Had to do this instead of returning a new and updated session because closing and sending a session * makes an update to the session after it was passed to the sending logic. * @see BaseClient.captureSession ) * * @param session the `Session` to update * @param context the `SessionContext` holding the properties that should be updated in @param session */// eslint-disable-next-line complexityfunction updateSession(session, context = {}) { if (context.user) { if (!session.ipAddress && context.user.ip_address) { session.ipAddress = context.user.ip_address; } if (!session.did && !context.did) { session.did = context.user.id || context.user.email || context.user.username; } } session.timestamp = context.timestamp || timestampInSeconds(); if (context.abnormal_mechanism) { session.abnormal_mechanism = context.abnormal_mechanism; } if (context.ignoreDuration) { session.ignoreDuration = context.ignoreDuration; } if (context.sid) { // Good enough uuid validation. — Kamil session.sid = context.sid.length === 32 ? context.sid : uuid4(); } if (context.init !== undefined) { session.init = context.init; } if (!session.did && context.did) { session.did = `${context.did}`; } if (typeof context.started === 'number') { session.started = context.started; } if (session.ignoreDuration) { session.duration = undefined; } else if (typeof context.duration === 'number') { session.duration = context.duration; } else { const duration = session.timestamp - session.started; session.duration = duration >= 0 ? duration : 0; } if (context.release) { session.release = context.release; } if (context.environment) { session.environment = context.environment; } if (!session.ipAddress && context.ipAddress) { session.ipAddress = context.ipAddress; } if (!session.userAgent && context.userAgent) { session.userAgent = context.userAgent; } if (typeof context.errors === 'number') { session.errors = context.errors; } if (context.status) { session.status = context.status; }}
/** * Closes a session by setting its status and updating the session object with it. * Internally calls `updateSession` to update the passed session object. * * Note that this function mutates the passed session (@see updateSession for explanation). * * @param session the `Session` object to be closed * @param status the `SessionStatus` with which the session was closed. If you don't pass a status, * this function will keep the previously set status, unless it was `'ok'` in which case * it is changed to `'exited'`. */function closeSession(session, status) { let context = {}; if (status) { context = { status }; } else if (session.status === 'ok') { context = { status: 'exited' }; } updateSession(session, context);}
/** * Serializes a passed session object to a JSON object with a slightly different structure. * This is necessary because the Sentry backend requires a slightly different schema of a session * than the one the JS SDKs use internally. * * @param session the session to be converted * * @returns a JSON object of the passed session */function sessionToJSON(session) { return dropUndefinedKeys({ sid: `${session.sid}`, init: session.init, // Make sure that sec is converted to ms for date constructor started: new Date(session.started * 1000).toISOString(), timestamp: new Date(session.timestamp * 1000).toISOString(), status: session.status, errors: session.errors, did: typeof session.did === 'number' || typeof session.did === 'string' ? `${session.did}` : undefined, duration: session.duration, abnormal_mechanism: session.abnormal_mechanism, attrs: { release: session.release, environment: session.environment, ip_address: session.ipAddress, user_agent: session.userAgent, }, });}
const SCOPE_SPAN_FIELD = '_sentrySpan';
/** * Set the active span for a given scope. * NOTE: This should NOT be used directly, but is only used internally by the trace methods. */function _setSpanForScope(scope, span) { if (span) { addNonEnumerableProperty(scope , SCOPE_SPAN_FIELD, span); } else { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete (scope )[SCOPE_SPAN_FIELD]; }}
/** * Get the active span for a given scope. * NOTE: This should NOT be used directly, but is only used internally by the trace methods. */function _getSpanForScope(scope) { return scope[SCOPE_SPAN_FIELD];}
/** * Default value for maximum number of breadcrumbs added to an event. */const DEFAULT_MAX_BREADCRUMBS = 100;
/** * Holds additional event information. */class Scope { /** Flag if notifying is happening. */ /** Callback for client to receive scope changes. */ /** Callback list that will be called during event processing. */ /** Array of breadcrumbs. */ /** User */ /** Tags */ /** Extra */ /** Contexts */ /** Attachments */ /** Propagation Context for distributed tracing */ /** * A place to stash data which is needed at some point in the SDK's event processing pipeline but which shouldn't get * sent to Sentry */ /** Fingerprint */ /** Severity */ /** * Transaction Name * * IMPORTANT: The transaction name on the scope has nothing to do with root spans/transaction objects. * It's purpose is to assign a transaction to the scope that's added to non-transaction events. */ /** Session */ /** Request Mode Session Status */ /** The client on this scope */ // NOTE: Any field which gets added here should get added not only to the constructor but also to the `clone` method. constructor() { this._notifyingListeners = false; this._scopeListeners = []; this._eventProcessors = []; this._breadcrumbs = []; this._attachments = []; this._user = {}; this._tags = {}; this._extra = {}; this._contexts = {}; this._sdkProcessingMetadata = {}; this._propagationContext = generatePropagationContext(); } /** * @inheritDoc */ clone() { const newScope = new Scope(); newScope._breadcrumbs = [...this._breadcrumbs]; newScope._tags = { ...this._tags }; newScope._extra = { ...this._extra }; newScope._contexts = { ...this._contexts }; newScope._user = this._user; newScope._level = this._level; newScope._session = this._session; newScope._transactionName = this._transactionName; newScope._fingerprint = this._fingerprint; newScope._eventProcessors = [...this._eventProcessors]; newScope._requestSession = this._requestSession; newScope._attachments = [...this._attachments]; newScope._sdkProcessingMetadata = { ...this._sdkProcessingMetadata }; newScope._propagationContext = { ...this._propagationContext }; newScope._client = this._client; _setSpanForScope(newScope, _getSpanForScope(this)); return newScope; } /** * @inheritDoc */ setClient(client) { this._client = client; } /** * @inheritDoc */ getClient() { return this._client ; } /** * @inheritDoc */ addScopeListener(callback) { this._scopeListeners.push(callback); } /** * @inheritDoc */ addEventProcessor(callback) { this._eventProcessors.push(callback); return this; } /** * @inheritDoc */ setUser(user) { // If null is passed we want to unset everything, but still define keys, // so that later down in the pipeline any existing values are cleared. this._user = user || { email: undefined, id: undefined, ip_address: undefined, username: undefined, }; if (this._session) { updateSession(this._session, { user }); } this._notifyScopeListeners(); return this; } /** * @inheritDoc */ getUser() { return this._user; } /** * @inheritDoc */ getRequestSession() { return this._requestSession; } /** * @inheritDoc */ setRequestSession(requestSession) { this._requestSession = requestSession; return this; } /** * @inheritDoc */ setTags(tags) { this._tags = { ...this._tags, ...tags, }; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setTag(key, value) { this._tags = { ...this._tags, [key]: value }; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setExtras(extras) { this._extra = { ...this._extra, ...extras, }; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setExtra(key, extra) { this._extra = { ...this._extra, [key]: extra }; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setFingerprint(fingerprint) { this._fingerprint = fingerprint; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setLevel(level) { this._level = level; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setTransactionName(name) { this._transactionName = name; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setContext(key, context) { if (context === null) { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete this._contexts[key]; } else { this._contexts[key] = context; } this._notifyScopeListeners(); return this; } /** * @inheritDoc */ setSession(session) { if (!session) { delete this._session; } else { this._session = session; } this._notifyScopeListeners(); return this; } /** * @inheritDoc */ getSession() { return this._session; } /** * @inheritDoc */ update(captureContext) { if (!captureContext) { return this; } const scopeToMerge = typeof captureContext === 'function' ? captureContext(this) : captureContext; const [scopeInstance, requestSession] = scopeToMerge instanceof Scope ? [scopeToMerge.getScopeData(), scopeToMerge.getRequestSession()] : isPlainObject(scopeToMerge) ? [captureContext , (captureContext ).requestSession] : []; const { tags, extra, user, contexts, level, fingerprint = [], propagationContext } = scopeInstance || {}; this._tags = { ...this._tags, ...tags }; this._extra = { ...this._extra, ...extra }; this._contexts = { ...this._contexts, ...contexts }; if (user && Object.keys(user).length) { this._user = user; } if (level) { this._level = level; } if (fingerprint.length) { this._fingerprint = fingerprint; } if (propagationContext) { this._propagationContext = propagationContext; } if (requestSession) { this._requestSession = requestSession; } return this; } /** * @inheritDoc */ clear() { // client is not cleared here on purpose! this._breadcrumbs = []; this._tags = {}; this._extra = {}; this._user = {}; this._contexts = {}; this._level = undefined; this._transactionName = undefined; this._fingerprint = undefined; this._requestSession = undefined; this._session = undefined; _setSpanForScope(this, undefined); this._attachments = []; this._propagationContext = generatePropagationContext(); this._notifyScopeListeners(); return this; } /** * @inheritDoc */ addBreadcrumb(breadcrumb, maxBreadcrumbs) { const maxCrumbs = typeof maxBreadcrumbs === 'number' ? maxBreadcrumbs : DEFAULT_MAX_BREADCRUMBS; // No data has been changed, so don't notify scope listeners if (maxCrumbs <= 0) { return this; } const mergedBreadcrumb = { timestamp: dateTimestampInSeconds(), ...breadcrumb, }; const breadcrumbs = this._breadcrumbs; breadcrumbs.push(mergedBreadcrumb); this._breadcrumbs = breadcrumbs.length > maxCrumbs ? breadcrumbs.slice(-maxCrumbs) : breadcrumbs; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ getLastBreadcrumb() { return this._breadcrumbs[this._breadcrumbs.length - 1]; } /** * @inheritDoc */ clearBreadcrumbs() { this._breadcrumbs = []; this._notifyScopeListeners(); return this; } /** * @inheritDoc */ addAttachment(attachment) { this._attachments.push(attachment); return this; } /** * @inheritDoc */ clearAttachments() { this._attachments = []; return this; } /** @inheritDoc */ getScopeData() { return { breadcrumbs: this._breadcrumbs, attachments: this._attachments, contexts: this._contexts, tags: this._tags, extra: this._extra, user: this._user, level: this._level, fingerprint: this._fingerprint || [], eventProcessors: this._eventProcessors, propagationContext: this._propagationContext, sdkProcessingMetadata: this._sdkProcessingMetadata, transactionName: this._transactionName, span: _getSpanForScope(this), }; } /** * @inheritDoc */ setSDKProcessingMetadata(newData) { this._sdkProcessingMetadata = { ...this._sdkProcessingMetadata, ...newData }; return this; } /** * @inheritDoc */ setPropagationContext(context) { this._propagationContext = context; return this; } /** * @inheritDoc */ getPropagationContext() { return this._propagationContext; } /** * @inheritDoc */ captureException(exception, hint) { const eventId = hint && hint.event_id ? hint.event_id : uuid4(); if (!this._client) { logger.warn('No client configured on scope - will not capture exception!'); return eventId; } const syntheticException = new Error('Sentry syntheticException'); this._client.captureException( exception, { originalException: exception, syntheticException, ...hint, event_id: eventId, }, this, ); return eventId; } /** * @inheritDoc */ captureMessage(message, level, hint) { const eventId = hint && hint.event_id ? hint.event_id : uuid4(); if (!this._client) { logger.warn('No client configured on scope - will not capture message!'); return eventId; } const syntheticException = new Error(message); this._client.captureMessage( message, level, { originalException: message, syntheticException, ...hint, event_id: eventId, }, this, ); return eventId; } /** * @inheritDoc */ captureEvent(event, hint) { const eventId = hint && hint.event_id ? hint.event_id : uuid4(); if (!this._client) { logger.warn('No client configured on scope - will not capture event!'); return eventId; } this._client.captureEvent(event, { ...hint, event_id: eventId }, this); return eventId; } /** * This will be called on every set call. */ _notifyScopeListeners() { // We need this check for this._notifyingListeners to be able to work on scope during updates // If this check is not here we'll produce endless recursion when something is done with the scope // during the callback. if (!this._notifyingListeners) { this._notifyingListeners = true; this._scopeListeners.forEach(callback => { callback(this); }); this._notifyingListeners = false; } }}
function generatePropagationContext() { return { traceId: uuid4(), spanId: uuid4().substring(16), };}
/** Get the default current scope. */function getDefaultCurrentScope() { return getGlobalSingleton('defaultCurrentScope', () => new Scope());}
/** Get the default isolation scope. */function getDefaultIsolationScope() { return getGlobalSingleton('defaultIsolationScope', () => new Scope());}
/** * Get the currently active scope. */function getCurrentScope() { const carrier = getMainCarrier(); const acs = getAsyncContextStrategy(carrier); return acs.getCurrentScope();}
/** * Get the currently active isolation scope. * The isolation scope is active for the current exection context. */function getIsolationScope() { const carrier = getMainCarrier(); const acs = getAsyncContextStrategy(carrier); return acs.getIsolationScope();}
/** * Get the global scope. * This scope is applied to _all_ events. */function getGlobalScope() { return getGlobalSingleton('globalScope', () => new Scope());}
/** * Creates a new scope with and executes the given operation within. * The scope is automatically removed once the operation * finishes or throws. *//** * Either creates a new active scope, or sets the given scope as active scope in the given callback. */function withScope$1( ...rest) { const carrier = getMainCarrier(); const acs = getAsyncContextStrategy(carrier); // If a scope is defined, we want to make this the active scope instead of the default one if (rest.length === 2) { const [scope, callback] = rest; if (!scope) { return acs.withScope(callback); } return acs.withSetScope(scope, callback); } return acs.withScope(rest[0]);}
/** * Attempts to fork the current isolation scope and the current scope based on the current async context strategy. If no * async context strategy is set, the isolation scope and the current scope will not be forked (this is currently the * case, for example, in the browser). * * Usage of this function in environments without async context strategy is discouraged and may lead to unexpected behaviour. * * This function is intended for Sentry SDK and SDK integration development. It is not recommended to be used in "normal" * applications directly because it comes with pitfalls. Use at your own risk! *//** * Either creates a new active isolation scope, or sets the given isolation scope as active scope in the given callback. */function withIsolationScope$1( ...rest) { const carrier = getMainCarrier(); const acs = getAsyncContextStrategy(carrier); // If a scope is defined, we want to make this the active scope instead of the default one if (rest.length === 2) { const [isolationScope, callback] = rest; if (!isolationScope) { return acs.withIsolationScope(callback); } return acs.withSetIsolationScope(isolationScope, callback); } return acs.withIsolationScope(rest[0]);}
/** * Get the currently active client. */function getClient() { return getCurrentScope().getClient();}
/** * This is an object that holds a stack of scopes. */class AsyncContextStack {
constructor(scope, isolationScope) { let assignedScope; if (!scope) { assignedScope = new Scope(); } else { assignedScope = scope; } let assignedIsolationScope; if (!isolationScope) { assignedIsolationScope = new Scope(); } else { assignedIsolationScope = isolationScope; } this._stack = [{ scope: assignedScope }]; this._isolationScope = assignedIsolationScope; } /** * Fork a scope for the stack. */ withScope(callback) { const scope = this._pushScope(); let maybePromiseResult; try { maybePromiseResult = callback(scope); } catch (e) { this._popScope(); throw e; } if (isThenable(maybePromiseResult)) { // @ts-expect-error - isThenable returns the wrong type return maybePromiseResult.then( res => { this._popScope(); return res; }, e => { this._popScope(); throw e; }, ); } this._popScope(); return maybePromiseResult; } /** * Get the client of the stack. */ getClient() { return this.getStackTop().client ; } /** * Returns the scope of the top stack. */ getScope() { return this.getStackTop().scope; } /** * Get the isolation scope for the stack. */ getIsolationScope() { return this._isolationScope; } /** * Returns the scope stack for domains or the process. */ getStack() { return this._stack; } /** * Returns the topmost scope layer in the order domain > local > process. */ getStackTop() { return this._stack[this._stack.length - 1]; } /** * Push a scope to the stack. */ _pushScope() { // We want to clone the content of prev scope const scope = this.getScope().clone(); this.getStack().push({ client: this.getClient(), scope, }); return scope; } /** * Pop a scope from the stack. */ _popScope() { if (this.getStack().length <= 1) return false; return !!this.getStack().pop(); }}
/** * Get the global async context stack. * This will be removed during the v8 cycle and is only here to make migration easier. */function getAsyncContextStack() { const registry = getMainCarrier(); // For now we continue to keep this as `hub` on the ACS, // as e.g. the Loader Script relies on this. // Eventually we may change this if/when we update the loader to not require this field anymore // Related, we also write to `hub` in {@link ./../sdk.ts registerClientOnGlobalHub} const sentry = getSentryCarrier(registry) ; if (sentry.hub) { return sentry.hub; } sentry.hub = new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope()); return sentry.hub;}
function withScope(callback) { return getAsyncContextStack().withScope(callback);}
function withSetScope(scope, callback) { const hub = getAsyncContextStack() ; return hub.withScope(() => { hub.getStackTop().scope = scope; return callback(scope); });}
function withIsolationScope(callback) { return getAsyncContextStack().withScope(() => { return callback(getAsyncContextStack().getIsolationScope()); });}
/** * Get the stack-based async context strategy. */function getStackAsyncContextStrategy() { return { withIsolationScope, withScope, withSetScope, withSetIsolationScope: (_isolationScope, callback) => { return withIsolationScope(callback); }, getCurrentScope: () => getAsyncContextStack().getScope(), getIsolationScope: () => getAsyncContextStack().getIsolationScope(), };}
/** * Get the current async context strategy. * If none has been setup, the default will be used. */function getAsyncContextStrategy(carrier) { const sentry = getSentryCarrier(carrier); if (sentry.acs) { return sentry.acs; } // Otherwise, use the default one (stack) return getStackAsyncContextStrategy();}
/** * key: bucketKey * value: [exportKey, MetricSummary] */let SPAN_METRIC_SUMMARY;
function getMetricStorageForSpan(span) { return SPAN_METRIC_SUMMARY ? SPAN_METRIC_SUMMARY.get(span) : undefined;}
/** * Fetches the metric summary if it exists for the passed span */function getMetricSummaryJsonForSpan(span) { const storage = getMetricStorageForSpan(span); if (!storage) { return undefined; } const output = {}; for (const [, [exportKey, summary]] of storage) { if (!output[exportKey]) { output[exportKey] = []; } output[exportKey].push(dropUndefinedKeys(summary)); } return output;}
/** * Updates the metric summary on a span. */function updateMetricSummaryOnSpan( span, metricType, sanitizedName, value, unit, tags, bucketKey,) { const storage = getMetricStorageForSpan(span) || new Map(); const exportKey = `${metricType}:${sanitizedName}@${unit}`; const bucketItem = storage.get(bucketKey); if (bucketItem) { const [, summary] = bucketItem; storage.set(bucketKey, [ exportKey, { min: Math.min(summary.min, value), max: Math.max(summary.max, value), count: (summary.count += 1), sum: (summary.sum += value), tags: summary.tags, }, ]); } else { storage.set(bucketKey, [ exportKey, { min: value, max: value, count: 1, sum: value, tags, }, ]); } if (!SPAN_METRIC_SUMMARY) { SPAN_METRIC_SUMMARY = new WeakMap(); } SPAN_METRIC_SUMMARY.set(span, storage);}
/** * Use this attribute to represent the source of a span. * Should be one of: custom, url, route, view, component, task, unknown * */const SEMANTIC_ATTRIBUTE_SENTRY_SOURCE = 'sentry.source';
/** * Use this attribute to represent the sample rate used for a span. */const SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE = 'sentry.sample_rate';
/** * Use this attribute to represent the operation of a span. */const SEMANTIC_ATTRIBUTE_SENTRY_OP = 'sentry.op';
/** * Use this attribute to represent the origin of a span. */const SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN = 'sentry.origin';
/** The unit of a measurement, which may be stored as a TimedEvent. */const SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT = 'sentry.measurement_unit';
/** The value of a measurement, which may be stored as a TimedEvent. */const SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE = 'sentry.measurement_value';
/** * The id of the profile that this span occured in. */const SEMANTIC_ATTRIBUTE_PROFILE_ID = 'sentry.profile_id';
const SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME = 'sentry.exclusive_time';
const SPAN_STATUS_UNSET = 0;const SPAN_STATUS_OK = 1;const SPAN_STATUS_ERROR = 2;
/** * Converts a HTTP status code into a sentry status with a message. * * @param httpStatus The HTTP response status code. * @returns The span status or unknown_error. */// https://develop.sentry.dev/sdk/event-payloads/span/function getSpanStatusFromHttpCode(httpStatus) { if (httpStatus < 400 && httpStatus >= 100) { return { code: SPAN_STATUS_OK }; } if (httpStatus >= 400 && httpStatus < 500) { switch (httpStatus) { case 401: return { code: SPAN_STATUS_ERROR, message: 'unauthenticated' }; case 403: return { code: SPAN_STATUS_ERROR, message: 'permission_denied' }; case 404: return { code: SPAN_STATUS_ERROR, message: 'not_found' }; case 409: return { code: SPAN_STATUS_ERROR, message: 'already_exists' }; case 413: return { code: SPAN_STATUS_ERROR, message: 'failed_precondition' }; case 429: return { code: SPAN_STATUS_ERROR, message: 'resource_exhausted' }; case 499: return { code: SPAN_STATUS_ERROR, message: 'cancelled' }; default: return { code: SPAN_STATUS_ERROR, message: 'invalid_argument' }; } } if (httpStatus >= 500 && httpStatus < 600) { switch (httpStatus) { case 501: return { code: SPAN_STATUS_ERROR, message: 'unimplemented' }; case 503: return { code: SPAN_STATUS_ERROR, message: 'unavailable' }; case 504: return { code: SPAN_STATUS_ERROR, message: 'deadline_exceeded' }; default: return { code: SPAN_STATUS_ERROR, message: 'internal_error' }; } } return { code: SPAN_STATUS_ERROR, message: 'unknown_error' };}
/** * Sets the Http status attributes on the current span based on the http code. * Additionally, the span's status is updated, depending on the http code. */function setHttpStatus(span, httpStatus) { span.setAttribute('http.response.status_code', httpStatus); const spanStatus = getSpanStatusFromHttpCode(httpStatus); if (spanStatus.message !== 'unknown_error') { span.setStatus(spanStatus); }}
// These are aligned with OpenTelemetry trace flagsconst TRACE_FLAG_NONE = 0x0;const TRACE_FLAG_SAMPLED = 0x1;
/** * Convert a span to a trace context, which can be sent as the `trace` context in an event. * By default, this will only include trace_id, span_id & parent_span_id. * If `includeAllData` is true, it will also include data, op, status & origin. */function spanToTransactionTraceContext(span) { const { spanId: span_id, traceId: trace_id } = span.spanContext(); const { data, op, parent_span_id, status, origin } = spanToJSON(span); return dropUndefinedKeys({ parent_span_id, span_id, trace_id, data, op, status, origin, });}
/** * Convert a span to a trace context, which can be sent as the `trace` context in a non-transaction event. */function spanToTraceContext(span) { const { spanId: span_id, traceId: trace_id } = span.spanContext(); const { parent_span_id } = spanToJSON(span); return dropUndefinedKeys({ parent_span_id, span_id, trace_id });}
/** * Convert a Span to a Sentry trace header. */function spanToTraceHeader(span) { const { traceId, spanId } = span.spanContext(); const sampled = spanIsSampled(span); return generateSentryTraceHeader(traceId, spanId, sampled);}
/** * Convert a span time input intp a timestamp in seconds. */function spanTimeInputToSeconds(input) { if (typeof input === 'number') { return ensureTimestampInSeconds(input); } if (Array.isArray(input)) { // See {@link HrTime} for the array-based time format return input[0] + input[1] / 1e9; } if (input instanceof Date) { return ensureTimestampInSeconds(input.getTime()); } return timestampInSeconds();}
/** * Converts a timestamp to second, if it was in milliseconds, or keeps it as second. */function ensureTimestampInSeconds(timestamp) { const isMs = timestamp > 9999999999; return isMs ? timestamp / 1000 : timestamp;}
/** * Convert a span to a JSON representation. */// Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json).// This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility.// And `spanToJSON` needs the Span class from `span.ts` to check here.function spanToJSON(span) { if (spanIsSentrySpan(span)) { return span.getSpanJSON(); } try { const { spanId: span_id, traceId: trace_id } = span.spanContext(); // Handle a span from @opentelemetry/sdk-base-trace's `Span` class if (spanIsOpenTelemetrySdkTraceBaseSpan(span)) { const { attributes, startTime, name, endTime, parentSpanId, status } = span; return dropUndefinedKeys({ span_id, trace_id, data: attributes, description: name, parent_span_id: parentSpanId, start_timestamp: spanTimeInputToSeconds(startTime), // This is [0,0] by default in OTEL, in which case we want to interpret this as no end time timestamp: spanTimeInputToSeconds(endTime) || undefined, status: getStatusMessage(status), op: attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP], origin: attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] , _metrics_summary: getMetricSummaryJsonForSpan(span), }); } // Finally, at least we have `spanContext()`.... return { span_id, trace_id, }; } catch (e) { return {}; }}
function spanIsOpenTelemetrySdkTraceBaseSpan(span) { const castSpan = span ; return !!castSpan.attributes && !!castSpan.startTime && !!castSpan.name && !!castSpan.endTime && !!castSpan.status;}
/** Exported only for tests. */
/** * Sadly, due to circular dependency checks we cannot actually import the Span class here and check for instanceof. * :( So instead we approximate this by checking if it has the `getSpanJSON` method. */function spanIsSentrySpan(span) { return typeof (span ).getSpanJSON === 'function';}
/** * Returns true if a span is sampled. * In most cases, you should just use `span.isRecording()` instead. * However, this has a slightly different semantic, as it also returns false if the span is finished. * So in the case where this distinction is important, use this method. */function spanIsSampled(span) { // We align our trace flags with the ones OpenTelemetry use // So we also check for sampled the same way they do. const { traceFlags } = span.spanContext(); return traceFlags === TRACE_FLAG_SAMPLED;}
/** Get the status message to use for a JSON representation of a span. */function getStatusMessage(status) { if (!status || status.code === SPAN_STATUS_UNSET) { return undefined; } if (status.code === SPAN_STATUS_OK) { return 'ok'; } return status.message || 'unknown_error';}
const CHILD_SPANS_FIELD = '_sentryChildSpans';const ROOT_SPAN_FIELD = '_sentryRootSpan';
/** * Adds an opaque child span reference to a span. */function addChildSpanToSpan(span, childSpan) { // We store the root span reference on the child span // We need this for `getRootSpan()` to work const rootSpan = span[ROOT_SPAN_FIELD] || span; addNonEnumerableProperty(childSpan , ROOT_SPAN_FIELD, rootSpan); // We store a list of child spans on the parent span // We need this for `getSpanDescendants()` to work if (span[CHILD_SPANS_FIELD] && span[CHILD_SPANS_FIELD].size < 1000) { span[CHILD_SPANS_FIELD].add(childSpan); } else { addNonEnumerableProperty(span, CHILD_SPANS_FIELD, new Set([childSpan])); }}
/** * Returns an array of the given span and all of its descendants. */function getSpanDescendants(span) { const resultSet = new Set(); function addSpanChildren(span) { // This exit condition is required to not infinitely loop in case of a circular dependency. if (resultSet.has(span)) { return; // We want to ignore unsampled spans (e.g. non recording spans) } else if (spanIsSampled(span)) { resultSet.add(span); const childSpans = span[CHILD_SPANS_FIELD] ? Array.from(span[CHILD_SPANS_FIELD]) : []; for (const childSpan of childSpans) { addSpanChildren(childSpan); } } } addSpanChildren(span); return Array.from(resultSet);}
/** * Returns the root span of a given span. */function getRootSpan(span) { return span[ROOT_SPAN_FIELD] || span;}
/** * Returns the currently active span. */function getActiveSpan() { const carrier = getMainCarrier(); const acs = getAsyncContextStrategy(carrier); if (acs.getActiveSpan) { return acs.getActiveSpan(); } return _getSpanForScope(getCurrentScope());}
/** * Updates the metric summary on the currently active span */function updateMetricSummaryOnActiveSpan( metricType, sanitizedName, value, unit, tags, bucketKey,) { const span = getActiveSpan(); if (span) { updateMetricSummaryOnSpan(span, metricType, sanitizedName, value, unit, tags, bucketKey); }}
let errorsInstrumented = false;
/** * Ensure that global errors automatically set the active span status. */function registerSpanErrorInstrumentation() { if (errorsInstrumented) { return; } errorsInstrumented = true; addGlobalErrorInstrumentationHandler(errorCallback); addGlobalUnhandledRejectionInstrumentationHandler(errorCallback);}
/** * If an error or unhandled promise occurs, we mark the active root span as failed */function errorCallback() { const activeSpan = getActiveSpan(); const rootSpan = activeSpan && getRootSpan(activeSpan); if (rootSpan) { const message = 'internal_error'; DEBUG_BUILD && logger.log(`[Tracing] Root span: ${message} -> Global error occured`); rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message }); }}
// The function name will be lost when bundling but we need to be able to identify this listener later to maintain the// node.js default exit behaviourerrorCallback.tag = 'sentry_tracingErrorCallback';
const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope';
/** Store the scope & isolation scope for a span, which can the be used when it is finished. */function setCapturedScopesOnSpan(span, scope, isolationScope) { if (span) { addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope); addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope); }}
/** * Grabs the scope and isolation scope off a span that were active when the span was started. */function getCapturedScopesOnSpan(span) { return { scope: (span )[SCOPE_ON_START_SPAN_FIELD], isolationScope: (span )[ISOLATION_SCOPE_ON_START_SPAN_FIELD], };}
// Treeshakable guard to remove all code related to tracing
/** * Determines if tracing is currently enabled. * * Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config. */function hasTracingEnabled( maybeOptions,) { if (typeof __SENTRY_TRACING__ === 'boolean' && !__SENTRY_TRACING__) { return false; } const client = getClient(); const options = maybeOptions || (client && client.getOptions()); return !!options && (options.enableTracing || 'tracesSampleRate' in options || 'tracesSampler' in options);}
/** * A Sentry Span that is non-recording, meaning it will not be sent to Sentry. */class SentryNonRecordingSpan {
constructor(spanContext = {}) { this._traceId = spanContext.traceId || uuid4(); this._spanId = spanContext.spanId || uuid4().substring(16); } /** @inheritdoc */ spanContext() { return { spanId: this._spanId, traceId: this._traceId, traceFlags: TRACE_FLAG_NONE, }; } /** @inheritdoc */ // eslint-disable-next-line @typescript-eslint/no-empty-function end(_timestamp) {} /** @inheritdoc */ setAttribute(_key, _value) { return this; } /** @inheritdoc */ setAttributes(_values) { return this; } /** @inheritdoc */ setStatus(_status) { return this; } /** @inheritdoc */ updateName(_name) { return this; } /** @inheritdoc */ isRecording() { return false; } /** @inheritdoc */ addEvent( _name, _attributesOrStartTime, _startTime, ) { return this; }}
/** * Wrap a callback function with error handling. * If an error is thrown, it will be passed to the `onError` callback and re-thrown. * * If the return value of the function is a promise, it will be handled with `maybeHandlePromiseRejection`. * * If an `onFinally` callback is provided, this will be called when the callback has finished * - so if it returns a promise, once the promise resolved/rejected, * else once the callback has finished executing. * The `onFinally` callback will _always_ be called, no matter if an error was thrown or not. */function handleCallbackErrors
( fn, onError, // eslint-disable-next-line @typescript-eslint/no-empty-function onFinally = () => {},) { let maybePromiseResult; try { maybePromiseResult = fn(); } catch (e) { onError(e); onFinally(); throw e; } return maybeHandlePromiseRejection(maybePromiseResult, onError, onFinally);}
/** * Maybe handle a promise rejection. * This expects to be given a value that _may_ be a promise, or any other value. * If it is a promise, and it rejects, it will call the `onError` callback. * Other than this, it will generally return the given value as-is. */function maybeHandlePromiseRejection( value, onError, onFinally,) { if (isThenable(value)) { // @ts-expect-error - the isThenable check returns the "wrong" type here return value.then( res => { onFinally(); return res; }, e => { onError(e); onFinally(); throw e; }, ); } onFinally(); return value;}
const DEFAULT_ENVIRONMENT = 'production';
/** * If you change this value, also update the terser plugin config to * avoid minification of the object property! */const FROZEN_DSC_FIELD = '_frozenDsc';
/** * Freeze the given DSC on the given span. */function freezeDscOnSpan(span, dsc) { const spanWithMaybeDsc = span ; addNonEnumerableProperty(spanWithMaybeDsc, FROZEN_DSC_FIELD, dsc);}
/** * Creates a dynamic sampling context from a client. * * Dispatches the `createDsc` lifecycle hook as a side effect. */function getDynamicSamplingContextFromClient(trace_id, client) { const options = client.getOptions(); const { publicKey: public_key } = client.getDsn() || {}; const dsc = dropUndefinedKeys({ environment: options.environment || DEFAULT_ENVIRONMENT, release: options.release, public_key, trace_id, }) ; client.emit('createDsc', dsc); return dsc;}
/** * Creates a dynamic sampling context from a span (and client and scope) * * @param span the span from which a few values like the root span name and sample rate are extracted. * * @returns a dynamic sampling context */function getDynamicSamplingContextFromSpan(span) { const client = getClient(); if (!client) { return {}; } const dsc = getDynamicSamplingContextFromClient(spanToJSON(span).trace_id || '', client); const rootSpan = getRootSpan(span); if (!rootSpan) { return dsc; } const frozenDsc = (rootSpan )[FROZEN_DSC_FIELD]; if (frozenDsc) { return frozenDsc; } const jsonSpan = spanToJSON(rootSpan); const attributes = jsonSpan.data || {}; const maybeSampleRate = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]; if (maybeSampleRate != null) { dsc.sample_rate = `${maybeSampleRate}`; } // We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII const source = attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]; // after JSON conversion, txn.name becomes jsonSpan.description if (source && source !== 'url') { dsc.transaction = jsonSpan.description; } dsc.sampled = String(spanIsSampled(rootSpan)); client.emit('createDsc', dsc); return dsc;}
/** * Print a log message for a started span. */function logSpanStart(span) { if (!DEBUG_BUILD) return; const { description = '< unknown name >', op = '< unknown op >', parent_span_id: parentSpanId } = spanToJSON(span); const { spanId } = span.spanContext(); const sampled = spanIsSampled(span); const rootSpan = getRootSpan(span); const isRootSpan = rootSpan === span; const header = `[Tracing] Starting ${sampled ? 'sampled' : 'unsampled'} ${isRootSpan ? 'root ' : ''}span`; const infoParts = [`op: ${op}`, `name: ${description}`, `ID: ${spanId}`]; if (parentSpanId) { infoParts.push(`parent ID: ${parentSpanId}`); } if (!isRootSpan) { const { op, description } = spanToJSON(rootSpan); infoParts.push(`root ID: ${rootSpan.spanContext().spanId}`); if (op) { infoParts.push(`root op: ${op}`); } if (description) { infoParts.push(`root description: ${description}`); } } logger.log(`${header} ${infoParts.join('\n ')}`);}
/** * Print a log message for an ended span. */function logSpanEnd(span) { if (!DEBUG_BUILD) return; const { description = '< unknown name >', op = '< unknown op >' } = spanToJSON(span); const { spanId } = span.spanContext(); const rootSpan = getRootSpan(span); const isRootSpan = rootSpan === span; const msg = `[Tracing] Finishing "${op}" ${isRootSpan ? 'root ' : ''}span "${description}" with ID ${spanId}`; logger.log(msg);}
/** * Parse a sample rate from a given value. * This will either return a boolean or number sample rate, if the sample rate is valid (between 0 and 1). * If a string is passed, we try to convert it to a number. * * Any invalid sample rate will return `undefined`. */function parseSampleRate(sampleRate) { if (typeof sampleRate === 'boolean') { return Number(sampleRate); } const rate = typeof sampleRate === 'string' ? parseFloat(sampleRate) : sampleRate; if (typeof rate !== 'number' || isNaN(rate)) { DEBUG_BUILD && logger.warn( `[Tracing] Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify( sampleRate, )} of type ${JSON.stringify(typeof sampleRate)}.`, ); return undefined; } if (rate < 0 || rate > 1) { DEBUG_BUILD && logger.warn(`[Tracing] Given sample rate is invalid. Sample rate must be between 0 and 1. Got ${rate}.`); return undefined; } return rate;}
/** * Makes a sampling decision for the given options. * * Called every time a root span is created. Only root spans which emerge with a `sampled` value of `true` will be * sent to Sentry. */function sampleSpan( options, samplingContext,) { // nothing to do if tracing is not enabled if (!hasTracingEnabled(options)) { return [false]; } // we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` nor `enableTracing` were defined, so one of these should // work; prefer the hook if so let sampleRate; if (typeof options.tracesSampler === 'function') { sampleRate = options.tracesSampler(samplingContext); } else if (samplingContext.parentSampled !== undefined) { sampleRate = samplingContext.parentSampled; } else if (typeof options.tracesSampleRate !== 'undefined') { sampleRate = options.tracesSampleRate; } else { // When `enableTracing === true`, we use a sample rate of 100% sampleRate = 1; } // Since this is coming from the user (or from a function provided by the user), who knows what we might get. // (The only valid values are booleans or numbers between 0 and 1.) const parsedSampleRate = parseSampleRate(sampleRate); if (parsedSampleRate === undefined) { DEBUG_BUILD && logger.warn('[Tracing] Discarding transaction because of invalid sample rate.'); return [false]; } // if the function returned 0 (or false), or if `tracesSampleRate` is 0, it's a sign the transaction should be dropped if (!parsedSampleRate) { DEBUG_BUILD && logger.log( `[Tracing] Discarding transaction because ${ typeof options.tracesSampler === 'function' ? 'tracesSampler returned 0 or false' : 'a negative sampling decision was inherited or tracesSampleRate is set to 0' }`, ); return [false, parsedSampleRate]; } // Now we roll the dice. Math.random is inclusive of 0, but not of 1, so strict < is safe here. In case sampleRate is // a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false. const shouldSample = Math.random() < parsedSampleRate; // if we're not going to keep it, we're done if (!shouldSample) { DEBUG_BUILD && logger.log( `[Tracing] Discarding transaction because it's not included in the random sample (sampling rate = ${Number( sampleRate, )})`, ); return [false, parsedSampleRate]; } return [true, parsedSampleRate];}
/** * Adds a measurement to the current active transaction. */function setMeasurement(name, value, unit) { const activeSpan = getActiveSpan(); const rootSpan = activeSpan && getRootSpan(activeSpan); if (rootSpan) { rootSpan.addEvent(name, { [SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: value, [SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: unit , }); }}
/** * Convert timed events to measurements. */function timedEventsToMeasurements(events) { if (!events || events.length === 0) { return undefined; } const measurements = {}; events.forEach(event => { const attributes = event.attributes || {}; const unit = attributes[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT] ; const value = attributes[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE] ; if (typeof unit === 'string' && typeof value === 'number') { measurements[event.name] = { value, unit }; } }); return measurements;}
/** * Span contains all data about a span */class SentrySpan {
/** Epoch timestamp in seconds when the span started. */ /** Epoch timestamp in seconds when the span ended. */ /** Internal keeper of the status */ /** The timed events added to this span. */ /** * You should never call the constructor manually, always use `Sentry.startSpan()` * or other span methods. * @internal * @hideconstructor * @hidden */ constructor(spanContext = {}) { this._traceId = spanContext.traceId || uuid4(); this._spanId = spanContext.spanId || uuid4().substring(16); this._startTime = spanContext.startTimestamp || timestampInSeconds(); this._attributes = {}; this.setAttributes({ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'manual', [SEMANTIC_ATTRIBUTE_SENTRY_OP]: spanContext.op, ...spanContext.attributes, }); this._name = spanContext.name; if (spanContext.parentSpanId) { this._parentSpanId = spanContext.parentSpanId; } // We want to include booleans as well here if ('sampled' in spanContext) { this._sampled = spanContext.sampled; } if (spanContext.endTimestamp) { this._endTime = spanContext.endTimestamp; } this._events = []; // If the span is already ended, ensure we finalize the span immediately if (this._endTime) { this._onSpanEnded(); } } /** @inheritdoc */ spanContext() { const { _spanId: spanId, _traceId: traceId, _sampled: sampled } = this; return { spanId, traceId, traceFlags: sampled ? TRACE_FLAG_SAMPLED : TRACE_FLAG_NONE, }; } /** @inheritdoc */ setAttribute(key, value) { if (value === undefined) { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete this._attributes[key]; } else { this._attributes[key] = value; } } /** @inheritdoc */ setAttributes(attributes) { Object.keys(attributes).forEach(key => this.setAttribute(key, attributes[key])); } /** * This should generally not be used, * but we need it for browser tracing where we want to adjust the start time afterwards. * USE THIS WITH CAUTION! * * @hidden * @internal */ updateStartTime(timeInput) { this._startTime = spanTimeInputToSeconds(timeInput); } /** * @inheritDoc */ setStatus(value) { this._status = value; return this; } /** * @inheritDoc */ updateName(name) { this._name = name; return this; } /** @inheritdoc */ end(endTimestamp) { // If already ended, skip if (this._endTime) { return; } this._endTime = spanTimeInputToSeconds(endTimestamp); logSpanEnd(this); this._onSpanEnded(); } /** * Get JSON representation of this span. * * @hidden * @internal This method is purely for internal purposes and should not be used outside * of SDK code. If you need to get a JSON representation of a span, * use `spanToJSON(span)` instead. */ getSpanJSON() { return dropUndefinedKeys({ data: this._attributes, description: this._name, op: this._attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP], parent_span_id: this._parentSpanId, span_id: this._spanId, start_timestamp: this._startTime, status: getStatusMessage(this._status), timestamp: this._endTime, trace_id: this._traceId, origin: this._attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] , _metrics_summary: getMetricSummaryJsonForSpan(this), profile_id: this._attributes[SEMANTIC_ATTRIBUTE_PROFILE_ID] , exclusive_time: this._attributes[SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME] , measurements: timedEventsToMeasurements(this._events), }); } /** @inheritdoc */ isRecording() { return !this._endTime && !!this._sampled; } /** * @inheritdoc */ addEvent( name, attributesOrStartTime, startTime, ) { DEBUG_BUILD && logger.log('[Tracing] Adding an event to span:', name); const time = isSpanTimeInput(attributesOrStartTime) ? attributesOrStartTime : startTime || timestampInSeconds(); const attributes = isSpanTimeInput(attributesOrStartTime) ? {} : attributesOrStartTime || {}; const event = { name, time: spanTimeInputToSeconds(time), attributes, }; this._events.push(event); return this; } /** Emit `spanEnd` when the span is ended. */ _onSpanEnded() { const client = getClient(); if (client) { client.emit('spanEnd', this); } // If this is a root span, send it when it is endedf if (this === getRootSpan(this)) { const transactionEvent = this._convertSpanToTransaction(); if (transactionEvent) { const scope = getCapturedScopesOnSpan(this).scope || getCurrentScope(); scope.captureEvent(transactionEvent); } } } /** * Finish the transaction & prepare the event to send to Sentry. */ _convertSpanToTransaction() { // We can only convert finished spans if (!isFullFinishedSpan(spanToJSON(this))) { return undefined; } if (!this._name) { DEBUG_BUILD && logger.warn('Transaction has no name, falling back to `<unlabeled transaction>`.'); this._name = '<unlabeled transaction>'; } const { scope: capturedSpanScope, isolationScope: capturedSpanIsolationScope } = getCapturedScopesOnSpan(this); const scope = capturedSpanScope || getCurrentScope(); const client = scope.getClient() || getClient(); if (this._sampled !== true) { // At this point if `sampled !== true` we want to discard the transaction. DEBUG_BUILD && logger.log('[Tracing] Discarding transaction because its trace was not chosen to be sampled.'); if (client) { client.recordDroppedEvent('sample_rate', 'transaction'); } return undefined; } // The transaction span itself should be filtered out const finishedSpans = getSpanDescendants(this).filter(span => span !== this); const spans = finishedSpans.map(span => spanToJSON(span)).filter(isFullFinishedSpan); const source = this._attributes[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE] ; const transaction = { contexts: { trace: spanToTransactionTraceContext(this), }, spans, start_timestamp: this._startTime, timestamp: this._endTime, transaction: this._name, type: 'transaction', sdkProcessingMetadata: { capturedSpanScope, capturedSpanIsolationScope, ...dropUndefinedKeys({ dynamicSamplingContext: getDynamicSamplingContextFromSpan(this), }), }, _metrics_summary: getMetricSummaryJsonForSpan(this), ...(source && { transaction_info: { source, }, }), }; const measurements = timedEventsToMeasurements(this._events); const hasMeasurements = measurements && Object.keys(measurements).length; if (hasMeasurements) { DEBUG_BUILD && logger.log('[Measurements] Adding measurements to transaction', JSON.stringify(measurements, undefined, 2)); transaction.measurements = measurements; } return transaction; }}
function isSpanTimeInput(value) { return (value && typeof value === 'number') || value instanceof Date || Array.isArray(value);}
// We want to filter out any incomplete SpanJSON objectsfunction isFullFinishedSpan(input) { return !!input.start_timestamp && !!input.timestamp && !!input.span_id && !!input.trace_id;}
const SUPPRESS_TRACING_KEY = '__SENTRY_SUPPRESS_TRACING__';
/** * Wraps a function with a transaction/span and finishes the span after the function is done. * The created span is the active span and will be used as parent by other spans created inside the function * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active. * * If you want to create a span that is not set as active, use {@link startInactiveSpan}. * * You'll always get a span passed to the callback, * it may just be a non-recording span if the span is not sampled or if tracing is disabled. */function startSpan(context, callback) { const acs = getAcs(); if (acs.startSpan) { return acs.startSpan(context, callback); } const spanContext = normalizeContext(context); return withScope$1(context.scope, scope => { const parentSpan = getParentSpan(scope); const shouldSkipSpan = context.onlyIfParent && !parentSpan; const activeSpan = shouldSkipSpan ? new SentryNonRecordingSpan() : createChildSpanOrTransaction({ parentSpan, spanContext, forceTransaction: context.forceTransaction, scope, }); _setSpanForScope(scope, activeSpan); return handleCallbackErrors( () => callback(activeSpan), () => { // Only update the span status if it hasn't been changed yet, and the span is not yet finished const { status } = spanToJSON(activeSpan); if (activeSpan.isRecording() && (!status || status === 'ok')) { activeSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' }); } }, () => activeSpan.end(), ); });}
/** * Similar to `Sentry.startSpan`. Wraps a function with a transaction/span, but does not finish the span * after the function is done automatically. You'll have to call `span.end()` manually. * * The created span is the active span and will be used as parent by other spans created inside the function * and can be accessed via `Sentry.getActiveSpan()`, as long as the function is executed while the scope is active. * * You'll always get a span passed to the callback, * it may just be a non-recording span if the span is not sampled or if tracing is disabled. */function startSpanManual(context, callback) { const acs = getAcs(); if (acs.startSpanManual) { return acs.startSpanManual(context, callback); } const spanContext = normalizeContext(context); return withScope$1(context.scope, scope => { const parentSpan = getParentSpan(scope); const shouldSkipSpan = context.onlyIfParent && !parentSpan; const activeSpan = shouldSkipSpan ? new SentryNonRecordingSpan() : createChildSpanOrTransaction({ parentSpan, spanContext, forceTransaction: context.forceTransaction, scope, }); _setSpanForScope(scope, activeSpan); function finishAndSetSpan() { activeSpan.end(); } return handleCallbackErrors( () => callback(activeSpan, finishAndSetSpan), () => { // Only update the span status if it hasn't been changed yet, and the span is not yet finished const { status } = spanToJSON(activeSpan); if (activeSpan.isRecording() && (!status || status === 'ok')) { activeSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' }); } }, ); });}
/** * Creates a span. This span is not set as active, so will not get automatic instrumentation spans * as children or be able to be accessed via `Sentry.getActiveSpan()`. * * If you want to create a span that is set as active, use {@link startSpan}. * * This function will always return a span, * it may just be a non-recording span if the span is not sampled or if tracing is disabled. */function startInactiveSpan(context) { const acs = getAcs(); if (acs.startInactiveSpan) { return acs.startInactiveSpan(context); } const spanContext = normalizeContext(context); const scope = context.scope || getCurrentScope(); const parentSpan = getParentSpan(scope); const shouldSkipSpan = context.onlyIfParent && !parentSpan; if (shouldSkipSpan) { return new SentryNonRecordingSpan(); } return createChildSpanOrTransaction({ parentSpan, spanContext, forceTransaction: context.forceTransaction, scope, });}
/** * Continue a trace from `sentry-trace` and `baggage` values. * These values can be obtained from incoming request headers, or in the browser from `<meta name="sentry-trace">` * and `<meta name="baggage">` HTML tags. * * Spans started with `startSpan`, `startSpanManual` and `startInactiveSpan`, within the callback will automatically * be attached to the incoming trace. */const continueTrace = ( { sentryTrace, baggage, }, callback,) => { return withScope$1(scope => { const propagationContext = propagationContextFromHeaders(sentryTrace, baggage); scope.setPropagationContext(propagationContext); return callback(); });};
function createChildSpanOrTransaction({ parentSpan, spanContext, forceTransaction, scope,}
) { if (!hasTracingEnabled()) { return new SentryNonRecordingSpan(); } const isolationScope = getIsolationScope(); let span; if (parentSpan && !forceTransaction) { span = _startChildSpan(parentSpan, scope, spanContext); addChildSpanToSpan(parentSpan, span); } else if (parentSpan) { // If we forced a transaction but have a parent span, make sure to continue from the parent span, not the scope const dsc = getDynamicSamplingContextFromSpan(parentSpan); const { traceId, spanId: parentSpanId } = parentSpan.spanContext(); const parentSampled = spanIsSampled(parentSpan); span = _startRootSpan( { traceId, parentSpanId, ...spanContext, }, scope, parentSampled, ); freezeDscOnSpan(span, dsc); } else { const { traceId, dsc, parentSpanId, sampled: parentSampled, } = { ...isolationScope.getPropagationContext(), ...scope.getPropagationContext(), }; span = _startRootSpan( { traceId, parentSpanId, ...spanContext, }, scope, parentSampled, ); if (dsc) { freezeDscOnSpan(span, dsc); } } logSpanStart(span); setCapturedScopesOnSpan(span, scope, isolationScope); return span;}
/** * This converts StartSpanOptions to SentrySpanArguments. * For the most part (for now) we accept the same options, * but some of them need to be transformed. * * Eventually the StartSpanOptions will be more aligned with OpenTelemetry. */function normalizeContext(context) { if (context.startTime) { const ctx = { ...context }; ctx.startTimestamp = spanTimeInputToSeconds(context.startTime); delete ctx.startTime; return ctx; } return context;}
function getAcs() { const carrier = getMainCarrier(); return getAsyncContextStrategy(carrier);}
function _startRootSpan(spanArguments, scope, parentSampled) { const client = getClient(); const options = (client && client.getOptions()) || {}; const { name = '', attributes } = spanArguments; const [sampled, sampleRate] = scope.getScopeData().sdkProcessingMetadata[SUPPRESS_TRACING_KEY] ? [false] : sampleSpan(options, { name, parentSampled, attributes, transactionContext: { name, parentSampled, }, }); const rootSpan = new SentrySpan({ ...spanArguments, attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom', ...spanArguments.attributes, }, sampled, }); if (sampleRate !== undefined) { rootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, sampleRate); } if (client) { client.emit('spanStart', rootSpan); } return rootSpan;}
/** * Creates a new `Span` while setting the current `Span.id` as `parentSpanId`. * This inherits the sampling decision from the parent span. */function _startChildSpan(parentSpan, scope, spanArguments) { const { spanId, traceId } = parentSpan.spanContext(); const sampled = scope.getScopeData().sdkProcessingMetadata[SUPPRESS_TRACING_KEY] ? false : spanIsSampled(parentSpan); const childSpan = sampled ? new SentrySpan({ ...spanArguments, parentSpanId: spanId, traceId, sampled, }) : new SentryNonRecordingSpan({ traceId }); addChildSpanToSpan(parentSpan, childSpan); const client = getClient(); if (client) { client.emit('spanStart', childSpan); // If it has an endTimestamp, it's already ended if (spanArguments.endTimestamp) { client.emit('spanEnd', childSpan); } } return childSpan;}
function getParentSpan(scope) { const span = _getSpanForScope(scope) ; if (!span) { return undefined; } const client = getClient(); const options = client ? client.getOptions() : {}; if (options.parentSpanIsAlwaysRootSpan) { return getRootSpan(span) ; } return span;}
/** * Apply SdkInfo (name, version, packages, integrations) to the corresponding event key. * Merge with existing data if any. **/function enhanceEventWithSdkInfo(event, sdkInfo) { if (!sdkInfo) { return event; } event.sdk = event.sdk || {}; event.sdk.name = event.sdk.name || sdkInfo.name; event.sdk.version = event.sdk.version || sdkInfo.version; event.sdk.integrations = [...(event.sdk.integrations || []), ...(sdkInfo.integrations || [])]; event.sdk.packages = [...(event.sdk.packages || []), ...(sdkInfo.packages || [])]; return event;}
/** Creates an envelope from a Session */function createSessionEnvelope( session, dsn, metadata, tunnel,) { const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata); const envelopeHeaders = { sent_at: new Date().toISOString(), ...(sdkInfo && { sdk: sdkInfo }), ...(!!tunnel && dsn && { dsn: dsnToString(dsn) }), }; const envelopeItem = 'aggregates' in session ? [{ type: 'sessions' }, session] : [{ type: 'session' }, session.toJSON()]; return createEnvelope(envelopeHeaders, [envelopeItem]);}
/** * Create an Envelope from an event. */function createEventEnvelope( event, dsn, metadata, tunnel,) { const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata); /* Note: Due to TS, event.type may be `replay_event`, theoretically. In practice, we never call `createEventEnvelope` with `replay_event` type, and we'd have to adjut a looot of types to make this work properly. We want to avoid casting this around, as that could lead to bugs (e.g. when we add another type) So the safe choice is to really guard against the replay_event type here. */ const eventType = event.type && event.type !== 'replay_event' ? event.type : 'event'; enhanceEventWithSdkInfo(event, metadata && metadata.sdk); const envelopeHeaders = createEventEnvelopeHeaders(event, sdkInfo, tunnel, dsn); // Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to // sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may // 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 // of this `delete`, lest we miss putting it back in the next time the property is in use.) delete event.sdkProcessingMetadata; const eventItem = [{ type: eventType }, event]; return createEnvelope(envelopeHeaders, [eventItem]);}
/** * Process an array of event processors, returning the processed event (or `null` if the event was dropped). */function notifyEventProcessors( processors, event, hint, index = 0,) { return new SyncPromise((resolve, reject) => { const processor = processors[index]; if (event === null || typeof processor !== 'function') { resolve(event); } else { const result = processor({ ...event }, hint) ; DEBUG_BUILD && processor.id && result === null && logger.log(`Event processor "${processor.id}" dropped event`); if (isThenable(result)) { void result .then(final => notifyEventProcessors(processors, final, hint, index + 1).then(resolve)) .then(null, reject); } else { void notifyEventProcessors(processors, result, hint, index + 1) .then(resolve) .then(null, reject); } } });}
/** * Applies data from the scope to the event and runs all event processors on it. */function applyScopeDataToEvent(event, data) { const { fingerprint, span, breadcrumbs, sdkProcessingMetadata } = data; // Apply general data applyDataToEvent(event, data); // We want to set the trace context for normal events only if there isn't already // a trace context on the event. There is a product feature in place where we link // errors with transaction and it relies on that. if (span) { applySpanToEvent(event, span); } applyFingerprintToEvent(event, fingerprint); applyBreadcrumbsToEvent(event, breadcrumbs); applySdkMetadataToEvent(event, sdkProcessingMetadata);}
/** Merge data of two scopes together. */function mergeScopeData(data, mergeData) { const { extra, tags, user, contexts, level, sdkProcessingMetadata, breadcrumbs, fingerprint, eventProcessors, attachments, propagationContext, transactionName, span, } = mergeData; mergeAndOverwriteScopeData(data, 'extra', extra); mergeAndOverwriteScopeData(data, 'tags', tags); mergeAndOverwriteScopeData(data, 'user', user); mergeAndOverwriteScopeData(data, 'contexts', contexts); mergeAndOverwriteScopeData(data, 'sdkProcessingMetadata', sdkProcessingMetadata); if (level) { data.level = level; } if (transactionName) { data.transactionName = transactionName; } if (span) { data.span = span; } if (breadcrumbs.length) { data.breadcrumbs = [...data.breadcrumbs, ...breadcrumbs]; } if (fingerprint.length) { data.fingerprint = [...data.fingerprint, ...fingerprint]; } if (eventProcessors.length) { data.eventProcessors = [...data.eventProcessors, ...eventProcessors]; } if (attachments.length) { data.attachments = [...data.attachments, ...attachments]; } data.propagationContext = { ...data.propagationContext, ...propagationContext };}
/** * Merges certain scope data. Undefined values will overwrite any existing values. * Exported only for tests. */function mergeAndOverwriteScopeData
(data, prop, mergeVal) { if (mergeVal && Object.keys(mergeVal).length) { // Clone object data[prop] = { ...data[prop] }; for (const key in mergeVal) { if (Object.prototype.hasOwnProperty.call(mergeVal, key)) { data[prop][key] = mergeVal[key]; } } }}
function applyDataToEvent(event, data) { const { extra, tags, user, contexts, level, transactionName } = data; const cleanedExtra = dropUndefinedKeys(extra); if (cleanedExtra && Object.keys(cleanedExtra).length) { event.extra = { ...cleanedExtra, ...event.extra }; } const cleanedTags = dropUndefinedKeys(tags); if (cleanedTags && Object.keys(cleanedTags).length) { event.tags = { ...cleanedTags, ...event.tags }; } const cleanedUser = dropUndefinedKeys(user); if (cleanedUser && Object.keys(cleanedUser).length) { event.user = { ...cleanedUser, ...event.user }; } const cleanedContexts = dropUndefinedKeys(contexts); if (cleanedContexts && Object.keys(cleanedContexts).length) { event.contexts = { ...cleanedContexts, ...event.contexts }; } if (level) { event.level = level; } // transaction events get their `transaction` from the root span name if (transactionName && event.type !== 'transaction') { event.transaction = transactionName; }}
function applyBreadcrumbsToEvent(event, breadcrumbs) { const mergedBreadcrumbs = [...(event.breadcrumbs || []), ...breadcrumbs]; event.breadcrumbs = mergedBreadcrumbs.length ? mergedBreadcrumbs : undefined;}
function applySdkMetadataToEvent(event, sdkProcessingMetadata) { event.sdkProcessingMetadata = { ...event.sdkProcessingMetadata, ...sdkProcessingMetadata, };}
function applySpanToEvent(event, span) { event.contexts = { trace: spanToTraceContext(span), ...event.contexts, }; event.sdkProcessingMetadata = { dynamicSamplingContext: getDynamicSamplingContextFromSpan(span), ...event.sdkProcessingMetadata, }; const rootSpan = getRootSpan(span); const transactionName = spanToJSON(rootSpan).description; if (transactionName && !event.transaction && event.type === 'transaction') { event.transaction = transactionName; }}
/** * Applies fingerprint from the scope to the event if there's one, * uses message if there's one instead or get rid of empty fingerprint */function applyFingerprintToEvent(event, fingerprint) { // Make sure it's an array first and we actually have something in place event.fingerprint = event.fingerprint ? arrayify(event.fingerprint) : []; // If we have something on the scope, then merge it with event if (fingerprint) { event.fingerprint = event.fingerprint.concat(fingerprint); } // If we have no data at all, remove empty array default if (event.fingerprint && !event.fingerprint.length) { delete event.fingerprint; }}
/** * This type makes sure that we get either a CaptureContext, OR an EventHint. * It does not allow mixing them, which could lead to unexpected outcomes, e.g. this is disallowed: * { user: { id: '123' }, mechanism: { handled: false } } *//** * Adds common information to events. * * The information includes release and environment from `options`, * breadcrumbs and context (extra, tags and user) from the scope. * * Information that is already present in the event is never overwritten. For * nested objects, such as the context, keys are merged. * * @param event The original event. * @param hint May contain additional information about the original exception. * @param scope A scope containing event metadata. * @returns A new event with more information. * @hidden */function prepareEvent( options, event, hint, scope, client, isolationScope,) { const { normalizeDepth = 3, normalizeMaxBreadth = 1000 } = options; const prepared = { ...event, event_id: event.event_id || hint.event_id || uuid4(), timestamp: event.timestamp || dateTimestampInSeconds(), }; const integrations = hint.integrations || options.integrations.map(i => i.name); applyClientOptions(prepared, options); applyIntegrationsMetadata(prepared, integrations); // Only put debug IDs onto frames for error events. if (event.type === undefined) { applyDebugIds(prepared, options.stackParser); } // If we have scope given to us, use it as the base for further modifications. // This allows us to prevent unnecessary copying of data if `captureContext` is not provided. const finalScope = getFinalScope(scope, hint.captureContext); if (hint.mechanism) { addExceptionMechanism(prepared, hint.mechanism); } const clientEventProcessors = client ? client.getEventProcessors() : []; // This should be the last thing called, since we want that // {@link Hub.addEventProcessor} gets the finished prepared event. // Merge scope data together const data = getGlobalScope().getScopeData(); if (isolationScope) { const isolationData = isolationScope.getScopeData(); mergeScopeData(data, isolationData); } if (finalScope) { const finalScopeData = finalScope.getScopeData(); mergeScopeData(data, finalScopeData); } const attachments = [...(hint.attachments || []), ...data.attachments]; if (attachments.length) { hint.attachments = attachments; } applyScopeDataToEvent(prepared, data); const eventProcessors = [ ...clientEventProcessors, // Run scope event processors _after_ all other processors ...data.eventProcessors, ]; const result = notifyEventProcessors(eventProcessors, prepared, hint); return result.then(evt => { if (evt) { // We apply the debug_meta field only after all event processors have ran, so that if any event processors modified // file names (e.g.the RewriteFrames integration) the filename -> debug ID relationship isn't destroyed. // This should not cause any PII issues, since we're only moving data that is already on the event and not adding // any new data applyDebugMeta(evt); } if (typeof normalizeDepth === 'number' && normalizeDepth > 0) { return normalizeEvent(evt, normalizeDepth, normalizeMaxBreadth); } return evt; });}
/** * Enhances event using the client configuration. * It takes care of all "static" values like environment, release and `dist`, * as well as truncating overly long values. * @param event event instance to be enhanced */function applyClientOptions(event, options) { const { environment, release, dist, maxValueLength = 250 } = options; if (!('environment' in event)) { event.environment = 'environment' in options ? environment : DEFAULT_ENVIRONMENT; } if (event.release === undefined && release !== undefined) { event.release = release; } if (event.dist === undefined && dist !== undefined) { event.dist = dist; } if (event.message) { event.message = truncate(event.message, maxValueLength); } const exception = event.exception && event.exception.values && event.exception.values[0]; if (exception && exception.value) { exception.value = truncate(exception.value, maxValueLength); } const request = event.request; if (request && request.url) { request.url = truncate(request.url, maxValueLength); }}
const debugIdStackParserCache = new WeakMap();
/** * Puts debug IDs into the stack frames of an error event. */function applyDebugIds(event, stackParser) { const debugIdMap = GLOBAL_OBJ._sentryDebugIds; if (!debugIdMap) { return; } let debugIdStackFramesCache; const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser); if (cachedDebugIdStackFrameCache) { debugIdStackFramesCache = cachedDebugIdStackFrameCache; } else { debugIdStackFramesCache = new Map(); debugIdStackParserCache.set(stackParser, debugIdStackFramesCache); } // Build a map of filename -> debug_id const filenameDebugIdMap = Object.keys(debugIdMap).reduce((acc, debugIdStackTrace) => { let parsedStack; const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace); if (cachedParsedStack) { parsedStack = cachedParsedStack; } else { parsedStack = stackParser(debugIdStackTrace); debugIdStackFramesCache.set(debugIdStackTrace, parsedStack); } for (let i = parsedStack.length - 1; i >= 0; i--) { const stackFrame = parsedStack[i]; if (stackFrame.filename) { acc[stackFrame.filename] = debugIdMap[debugIdStackTrace]; break; } } return acc; }, {}); try { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion event.exception.values.forEach(exception => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion exception.stacktrace.frames.forEach(frame => { if (frame.filename) { frame.debug_id = filenameDebugIdMap[frame.filename]; } }); }); } catch (e) { // To save bundle size we're just try catching here instead of checking for the existence of all the different objects. }}
/** * Moves debug IDs from the stack frames of an error event into the debug_meta field. */function applyDebugMeta(event) { // Extract debug IDs and filenames from the stack frames on the event. const filenameDebugIdMap = {}; try { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion event.exception.values.forEach(exception => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion exception.stacktrace.frames.forEach(frame => { if (frame.debug_id) { if (frame.abs_path) { filenameDebugIdMap[frame.abs_path] = frame.debug_id; } else if (frame.filename) { filenameDebugIdMap[frame.filename] = frame.debug_id; } delete frame.debug_id; } }); }); } catch (e) { // To save bundle size we're just try catching here instead of checking for the existence of all the different objects. } if (Object.keys(filenameDebugIdMap).length === 0) { return; } // Fill debug_meta information event.debug_meta = event.debug_meta || {}; event.debug_meta.images = event.debug_meta.images || []; const images = event.debug_meta.images; Object.keys(filenameDebugIdMap).forEach(filename => { images.push({ type: 'sourcemap', code_file: filename, debug_id: filenameDebugIdMap[filename], }); });}
/** * This function adds all used integrations to the SDK info in the event. * @param event The event that will be filled with all integrations. */function applyIntegrationsMetadata(event, integrationNames) { if (integrationNames.length > 0) { event.sdk = event.sdk || {}; event.sdk.integrations = [...(event.sdk.integrations || []), ...integrationNames]; }}
/** * Applies `normalize` function on necessary `Event` attributes to make them safe for serialization. * Normalized keys: * - `breadcrumbs.data` * - `user` * - `contexts` * - `extra` * @param event Event * @returns Normalized event */function normalizeEvent(event, depth, maxBreadth) { if (!event) { return null; } const normalized = { ...event, ...(event.breadcrumbs && { breadcrumbs: event.breadcrumbs.map(b => ({ ...b, ...(b.data && { data: normalize(b.data, depth, maxBreadth), }), })), }), ...(event.user && { user: normalize(event.user, depth, maxBreadth), }), ...(event.contexts && { contexts: normalize(event.contexts, depth, maxBreadth), }), ...(event.extra && { extra: normalize(event.extra, depth, maxBreadth), }), }; // event.contexts.trace stores information about a Transaction. Similarly, // event.spans[] stores information about child Spans. Given that a // Transaction is conceptually a Span, normalization should apply to both // Transactions and Spans consistently. // For now the decision is to skip normalization of Transactions and Spans, // so this block overwrites the normalized event to add back the original // Transaction information prior to normalization. if (event.contexts && event.contexts.trace && normalized.contexts) { normalized.contexts.trace = event.contexts.trace; // event.contexts.trace.data may contain circular/dangerous data so we need to normalize it if (event.contexts.trace.data) { normalized.contexts.trace.data = normalize(event.contexts.trace.data, depth, maxBreadth); } } // event.spans[].data may contain circular/dangerous data so we need to normalize it if (event.spans) { normalized.spans = event.spans.map(span => { return { ...span, ...(span.data && { data: normalize(span.data, depth, maxBreadth), }), }; }); } return normalized;}
function getFinalScope( scope, captureContext,) { if (!captureContext) { return scope; } const finalScope = scope ? scope.clone() : new Scope(); finalScope.update(captureContext); return finalScope;}
/** * Parse either an `EventHint` directly, or convert a `CaptureContext` to an `EventHint`. * This is used to allow to update method signatures that used to accept a `CaptureContext` but should now accept an `EventHint`. */function parseEventHintOrCaptureContext( hint,) { if (!hint) { return undefined; } // If you pass a Scope or `() => Scope` as CaptureContext, we just return this as captureContext if (hintIsScopeOrFunction(hint)) { return { captureContext: hint }; } if (hintIsScopeContext(hint)) { return { captureContext: hint, }; } return hint;}
function hintIsScopeOrFunction( hint,) { return hint instanceof Scope || typeof hint === 'function';}
const captureContextKeys = [ 'user', 'level', 'extra', 'contexts', 'tags', 'fingerprint', 'requestSession', 'propagationContext',] ;
function hintIsScopeContext(hint) { return Object.keys(hint).some(key => captureContextKeys.includes(key ));}
/** * Captures an exception event and sends it to Sentry. * * @param exception The exception to capture. * @param hint Optional additional data to attach to the Sentry event. * @returns the id of the captured Sentry event. */function captureException( // eslint-disable-next-line @typescript-eslint/no-explicit-any exception, hint,) { return getCurrentScope().captureException(exception, parseEventHintOrCaptureContext(hint));}
/** * Captures a message event and sends it to Sentry. * * @param exception The exception to capture. * @param captureContext Define the level of the message or pass in additional data to attach to the message. * @returns the id of the captured message. */function captureMessage(message, captureContext) { // This is necessary to provide explicit scopes upgrade, without changing the original // arity of the `captureMessage(message, level)` method. const level = typeof captureContext === 'string' ? captureContext : undefined; const context = typeof captureContext !== 'string' ? { captureContext } : undefined; return getCurrentScope().captureMessage(message, level, context);}
/** * Captures a manually created event and sends it to Sentry. * * @param exception The event to send to Sentry. * @param hint Optional additional data to attach to the Sentry event. * @returns the id of the captured event. */function captureEvent(event, hint) { return getCurrentScope().captureEvent(event, hint);}
/** * Sets context data with the given name. * @param name of the context * @param context Any kind of data. This data will be normalized. */// eslint-disable-next-line @typescript-eslint/no-explicit-anyfunction setContext(name, context) { getIsolationScope().setContext(name, context);}
/** * Set an object that will be merged sent as extra data with the event. * @param extras Extras object to merge into current context. */function setExtras(extras) { getIsolationScope().setExtras(extras);}
/** * Set key:value that will be sent as extra data with the event. * @param key String of extra * @param extra Any kind of data. This data will be normalized. */function setExtra(key, extra) { getIsolationScope().setExtra(key, extra);}
/** * Set an object that will be merged sent as tags data with the event. * @param tags Tags context object to merge into current context. */function setTags(tags) { getIsolationScope().setTags(tags);}
/** * Set key:value that will be sent as tags data with the event. * * Can also be used to unset a tag, by passing `undefined`. * * @param key String key of tag * @param value Value of tag */function setTag(key, value) { getIsolationScope().setTag(key, value);}
/** * Updates user context information for future events. * * @param user User context object to be set in the current context. Pass `null` to unset the user. */function setUser(user) { getIsolationScope().setUser(user);}
/** * Create a cron monitor check in and send it to Sentry. * * @param checkIn An object that describes a check in. * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want * to create a monitor automatically when sending a check in. */function captureCheckIn(checkIn, upsertMonitorConfig) { const scope = getCurrentScope(); const client = getClient(); if (!client) { DEBUG_BUILD && logger.warn('Cannot capture check-in. No client defined.'); } else if (!client.captureCheckIn) { DEBUG_BUILD && logger.warn('Cannot capture check-in. Client does not support sending check-ins.'); } else { return client.captureCheckIn(checkIn, upsertMonitorConfig, scope); } return uuid4();}
/** * Wraps a callback with a cron monitor check in. The check in will be sent to Sentry when the callback finishes. * * @param monitorSlug The distinct slug of the monitor. * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want * to create a monitor automatically when sending a check in. */function withMonitor( monitorSlug, callback, upsertMonitorConfig,) { const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig); const now = timestampInSeconds(); function finishCheckIn(status) { captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now }); } let maybePromiseResult; try { maybePromiseResult = callback(); } catch (e) { finishCheckIn('error'); throw e; } if (isThenable(maybePromiseResult)) { Promise.resolve(maybePromiseResult).then( () => { finishCheckIn('ok'); }, () => { finishCheckIn('error'); }, ); } else { finishCheckIn('ok'); } return maybePromiseResult;}
/** * Call `flush()` on the current client, if there is one. See {@link Client.flush}. * * @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause * the client to wait until all events are sent before resolving the promise. * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it * doesn't (or if there's no client defined). */async function flush(timeout) { const client = getClient(); if (client) { return client.flush(timeout); } DEBUG_BUILD && logger.warn('Cannot flush events. No client defined.'); return Promise.resolve(false);}
/** * Call `close()` on the current client, if there is one. See {@link Client.close}. * * @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this * parameter will cause the client to wait until all events are sent before disabling itself. * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it * doesn't (or if there's no client defined). */async function close(timeout) { const client = getClient(); if (client) { return client.close(timeout); } DEBUG_BUILD && logger.warn('Cannot flush events and disable SDK. No client defined.'); return Promise.resolve(false);}
/** * Returns true if Sentry has been properly initialized. */function isInitialized() { return !!getClient();}
/** * Add an event processor. * This will be added to the current isolation scope, ensuring any event that is processed in the current execution * context will have the processor applied. */function addEventProcessor(callback) { getIsolationScope().addEventProcessor(callback);}
/** * Start a session on the current isolation scope. * * @param context (optional) additional properties to be applied to the returned session object * * @returns the new active session */function startSession(context) { const client = getClient(); const isolationScope = getIsolationScope(); const currentScope = getCurrentScope(); const { release, environment = DEFAULT_ENVIRONMENT } = (client && client.getOptions()) || {}; // Will fetch userAgent if called from browser sdk const { userAgent } = GLOBAL_OBJ.navigator || {}; const session = makeSession({ release, environment, user: currentScope.getUser() || isolationScope.getUser(), ...(userAgent && { userAgent }), ...context, }); // End existing session if there's one const currentSession = isolationScope.getSession(); if (currentSession && currentSession.status === 'ok') { updateSession(currentSession, { status: 'exited' }); } endSession(); // Afterwards we set the new session on the scope isolationScope.setSession(session); // TODO (v8): Remove this and only use the isolation scope(?). // For v7 though, we can't "soft-break" people using getCurrentHub().getScope().setSession() currentScope.setSession(session); return session;}
/** * End the session on the current isolation scope. */function endSession() { const isolationScope = getIsolationScope(); const currentScope = getCurrentScope(); const session = currentScope.getSession() || isolationScope.getSession(); if (session) { closeSession(session); } _sendSessionUpdate(); // the session is over; take it off of the scope isolationScope.setSession(); // TODO (v8): Remove this and only use the isolation scope(?). // For v7 though, we can't "soft-break" people using getCurrentHub().getScope().setSession() currentScope.setSession();}
/** * Sends the current Session on the scope */function _sendSessionUpdate() { const isolationScope = getIsolationScope(); const currentScope = getCurrentScope(); const client = getClient(); // TODO (v8): Remove currentScope and only use the isolation scope(?). // For v7 though, we can't "soft-break" people using getCurrentHub().getScope().setSession() const session = currentScope.getSession() || isolationScope.getSession(); if (session && client) { client.captureSession(session); }}
/** * Sends the current session on the scope to Sentry * * @param end If set the session will be marked as exited and removed from the scope. * Defaults to `false`. */function captureSession(end = false) { // both send the update and pull the session from the scope if (end) { endSession(); return; } // only send the update _sendSessionUpdate();}
/** * @inheritdoc */class SessionFlusher {
// Cast to any so that it can use Node.js timeout // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(client, attrs) { this._client = client; this.flushTimeout = 60; this._pendingAggregates = {}; this._isEnabled = true; // Call to setInterval, so that flush is called every 60 seconds. this._intervalId = setInterval(() => this.flush(), this.flushTimeout * 1000); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (this._intervalId.unref) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access this._intervalId.unref(); } this._sessionAttrs = attrs; } /** Checks if `pendingAggregates` has entries, and if it does flushes them by calling `sendSession` */ flush() { const sessionAggregates = this.getSessionAggregates(); if (sessionAggregates.aggregates.length === 0) { return; } this._pendingAggregates = {}; this._client.sendSession(sessionAggregates); } /** Massages the entries in `pendingAggregates` and returns aggregated sessions */ getSessionAggregates() { const aggregates = Object.keys(this._pendingAggregates).map((key) => { return this._pendingAggregates[parseInt(key)]; }); const sessionAggregates = { attrs: this._sessionAttrs, aggregates, }; return dropUndefinedKeys(sessionAggregates); } /** JSDoc */ close() { clearInterval(this._intervalId); this._isEnabled = false; this.flush(); } /** * Wrapper function for _incrementSessionStatusCount that checks if the instance of SessionFlusher is enabled then * fetches the session status of the request from `Scope.getRequestSession().status` on the scope and passes them to * `_incrementSessionStatusCount` along with the start date */ incrementSessionStatusCount() { if (!this._isEnabled) { return; } const isolationScope = getIsolationScope(); const requestSession = isolationScope.getRequestSession(); if (requestSession && requestSession.status) { this._incrementSessionStatusCount(requestSession.status, new Date()); // This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in // case captureRequestSession is called more than once to prevent double count isolationScope.setRequestSession(undefined); /* eslint-enable @typescript-eslint/no-unsafe-member-access */ } } /** * Increments status bucket in pendingAggregates buffer (internal state) corresponding to status of * the session received */ _incrementSessionStatusCount(status, date) { // Truncate minutes and seconds on Session Started attribute to have one minute bucket keys const sessionStartedTrunc = new Date(date).setSeconds(0, 0); this._pendingAggregates[sessionStartedTrunc] = this._pendingAggregates[sessionStartedTrunc] || {}; // corresponds to aggregated sessions in one specific minute bucket // for example, {"started":"2021-03-16T08:00:00.000Z","exited":4, "errored": 1} const aggregationCounts = this._pendingAggregates[sessionStartedTrunc]; if (!aggregationCounts.started) { aggregationCounts.started = new Date(sessionStartedTrunc).toISOString(); } switch (status) { case 'errored': aggregationCounts.errored = (aggregationCounts.errored || 0) + 1; return aggregationCounts.errored; case 'ok': aggregationCounts.exited = (aggregationCounts.exited || 0) + 1; return aggregationCounts.exited; default: aggregationCounts.crashed = (aggregationCounts.crashed || 0) + 1; return aggregationCounts.crashed; } }}
const SENTRY_API_VERSION = '7';
/** Returns the prefix to construct Sentry ingestion API endpoints. */function getBaseApiEndpoint(dsn) { const protocol = dsn.protocol ? `${dsn.protocol}:` : ''; const port = dsn.port ? `:${dsn.port}` : ''; return `${protocol}//${dsn.host}${port}${dsn.path ? `/${dsn.path}` : ''}/api/`;}
/** Returns the ingest API endpoint for target. */function _getIngestEndpoint(dsn) { return `${getBaseApiEndpoint(dsn)}${dsn.projectId}/envelope/`;}
/** Returns a URL-encoded string with auth config suitable for a query string. */function _encodedAuth(dsn, sdkInfo) { return urlEncode({ // We send only the minimum set of required information. See // https://github.com/getsentry/sentry-javascript/issues/2572. sentry_key: dsn.publicKey, sentry_version: SENTRY_API_VERSION, ...(sdkInfo && { sentry_client: `${sdkInfo.name}/${sdkInfo.version}` }), });}
/** * Returns the envelope endpoint URL with auth in the query string. * * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests. */function getEnvelopeEndpointWithUrlEncodedAuth(dsn, tunnel, sdkInfo) { return tunnel ? tunnel : `${_getIngestEndpoint(dsn)}?${_encodedAuth(dsn, sdkInfo)}`;}
const installedIntegrations = [];
/** Map of integrations assigned to a client */
/** * Remove duplicates from the given array, preferring the last instance of any duplicate. Not guaranteed to * preseve the order of integrations in the array. * * @private */function filterDuplicates(integrations) { const integrationsByName = {}; integrations.forEach(currentInstance => { const { name } = currentInstance; const existingInstance = integrationsByName[name]; // We want integrations later in the array to overwrite earlier ones of the same type, except that we never want a // default instance to overwrite an existing user instance if (existingInstance && !existingInstance.isDefaultInstance && currentInstance.isDefaultInstance) { return; } integrationsByName[name] = currentInstance; }); return Object.keys(integrationsByName).map(k => integrationsByName[k]);}
/** Gets integrations to install */function getIntegrationsToSetup(options) { const defaultIntegrations = options.defaultIntegrations || []; const userIntegrations = options.integrations; // We flag default instances, so that later we can tell them apart from any user-created instances of the same class defaultIntegrations.forEach(integration => { integration.isDefaultInstance = true; }); let integrations; if (Array.isArray(userIntegrations)) { integrations = [...defaultIntegrations, ...userIntegrations]; } else if (typeof userIntegrations === 'function') { integrations = arrayify(userIntegrations(defaultIntegrations)); } else { integrations = defaultIntegrations; } const finalIntegrations = filterDuplicates(integrations); // The `Debug` integration prints copies of the `event` and `hint` which will be passed to `beforeSend` or // `beforeSendTransaction`. It therefore has to run after all other integrations, so that the changes of all event // processors will be reflected in the printed values. For lack of a more elegant way to guarantee that, we therefore // locate it and, assuming it exists, pop it out of its current spot and shove it onto the end of the array. const debugIndex = findIndex(finalIntegrations, integration => integration.name === 'Debug'); if (debugIndex !== -1) { const [debugInstance] = finalIntegrations.splice(debugIndex, 1); finalIntegrations.push(debugInstance); } return finalIntegrations;}
/** * Given a list of integration instances this installs them all. When `withDefaults` is set to `true` then all default * integrations are added unless they were already provided before. * @param integrations array of integration instances * @param withDefault should enable default integrations */function setupIntegrations(client, integrations) { const integrationIndex = {}; integrations.forEach(integration => { // guard against empty provided integrations if (integration) { setupIntegration(client, integration, integrationIndex); } }); return integrationIndex;}
/** * Execute the `afterAllSetup` hooks of the given integrations. */function afterSetupIntegrations(client, integrations) { for (const integration of integrations) { // guard against empty provided integrations if (integration && integration.afterAllSetup) { integration.afterAllSetup(client); } }}
/** Setup a single integration. */function setupIntegration(client, integration, integrationIndex) { if (integrationIndex[integration.name]) { DEBUG_BUILD && logger.log(`Integration skipped because it was already installed: ${integration.name}`); return; } integrationIndex[integration.name] = integration; // `setupOnce` is only called the first time if (installedIntegrations.indexOf(integration.name) === -1 && typeof integration.setupOnce === 'function') { integration.setupOnce(); installedIntegrations.push(integration.name); } // `setup` is run for each client if (integration.setup && typeof integration.setup === 'function') { integration.setup(client); } if (typeof integration.preprocessEvent === 'function') { const callback = integration.preprocessEvent.bind(integration) ; client.on('preprocessEvent', (event, hint) => callback(event, hint, client)); } if (typeof integration.processEvent === 'function') { const callback = integration.processEvent.bind(integration) ; const processor = Object.assign((event, hint) => callback(event, hint, client), { id: integration.name, }); client.addEventProcessor(processor); } DEBUG_BUILD && logger.log(`Integration installed: ${integration.name}`);}
// Polyfill for Array.findIndex(), which is not supported in ES5function findIndex(arr, callback) { for (let i = 0; i < arr.length; i++) { if (callback(arr[i]) === true) { return i; } } return -1;}
/** * Define an integration function that can be used to create an integration instance. * Note that this by design hides the implementation details of the integration, as they are considered internal. */function defineIntegration(fn) { return fn;}
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
/** * Base implementation for all JavaScript SDK clients. * * Call the constructor with the corresponding options * specific to the client subclass. To access these options later, use * {@link Client.getOptions}. * * If a Dsn is specified in the options, it will be parsed and stored. Use * {@link Client.getDsn} to retrieve the Dsn at any moment. In case the Dsn is * invalid, the constructor will throw a {@link SentryException}. Note that * without a valid Dsn, the SDK will not send any events to Sentry. * * Before sending an event, it is passed through * {@link BaseClient._prepareEvent} to add SDK information and scope data * (breadcrumbs and context). To add more custom information, override this * method and extend the resulting prepared event. * * To issue automatically created events (e.g. via instrumentation), use * {@link Client.captureEvent}. It will prepare the event and pass it through * the callback lifecycle. To issue auto-breadcrumbs, use * {@link Client.addBreadcrumb}. * * @example * class NodeClient extends BaseClient<NodeOptions> { * public constructor(options: NodeOptions) { * super(options); * } * * // ... * } */class BaseClient { /** Options passed to the SDK. */ /** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */ /** Array of set up integrations. */ /** Number of calls being processed */ /** Holds flushable */ // eslint-disable-next-line @typescript-eslint/ban-types /** * Initializes this client instance. * * @param options Options for the client. */ constructor(options) { this._options = options; this._integrations = {}; this._numProcessing = 0; this._outcomes = {}; this._hooks = {}; this._eventProcessors = []; if (options.dsn) { this._dsn = makeDsn(options.dsn); } else { DEBUG_BUILD && logger.warn('No DSN provided, client will not send events.'); } if (this._dsn) { const url = getEnvelopeEndpointWithUrlEncodedAuth( this._dsn, options.tunnel, options._metadata ? options._metadata.sdk : undefined, ); this._transport = options.transport({ recordDroppedEvent: this.recordDroppedEvent.bind(this), ...options.transportOptions, url, }); } } /** * @inheritDoc */ // eslint-disable-next-line @typescript-eslint/no-explicit-any captureException(exception, hint, scope) { // ensure we haven't captured this very object before if (checkOrSetAlreadyCaught(exception)) { DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR); return; } let eventId = hint && hint.event_id; this._process( this.eventFromException(exception, hint) .then(event => this._captureEvent(event, hint, scope)) .then(result => { eventId = result; }), ); return eventId; } /** * @inheritDoc */ captureMessage( message, level, hint, scope, ) { let eventId = hint && hint.event_id; const eventMessage = isParameterizedString(message) ? message : String(message); const promisedEvent = isPrimitive(message) ? this.eventFromMessage(eventMessage, level, hint) : this.eventFromException(message, hint); this._process( promisedEvent .then(event => this._captureEvent(event, hint, scope)) .then(result => { eventId = result; }), ); return eventId; } /** * @inheritDoc */ captureEvent(event, hint, scope) { // ensure we haven't captured this very object before if (hint && hint.originalException && checkOrSetAlreadyCaught(hint.originalException)) { DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR); return; } let eventId = hint && hint.event_id; const sdkProcessingMetadata = event.sdkProcessingMetadata || {}; const capturedSpanScope = sdkProcessingMetadata.capturedSpanScope; this._process( this._captureEvent(event, hint, capturedSpanScope || scope).then(result => { eventId = result; }), ); return eventId; } /** * @inheritDoc */ captureSession(session) { if (!(typeof session.release === 'string')) { DEBUG_BUILD && logger.warn('Discarded session because of missing or non-string release'); } else { this.sendSession(session); // After sending, we set init false to indicate it's not the first occurrence updateSession(session, { init: false }); } } /** * @inheritDoc */ getDsn() { return this._dsn; } /** * @inheritDoc */ getOptions() { return this._options; } /** * @see SdkMetadata in @sentry/types * * @return The metadata of the SDK */ getSdkMetadata() { return this._options._metadata; } /** * @inheritDoc */ getTransport() { return this._transport; } /** * @inheritDoc */ flush(timeout) { const transport = this._transport; if (transport) { this.emit('flush'); return this._isClientDoneProcessing(timeout).then(clientFinished => { return transport.flush(timeout).then(transportFlushed => clientFinished && transportFlushed); }); } else { return resolvedSyncPromise(true); } } /** * @inheritDoc */ close(timeout) { return this.flush(timeout).then(result => { this.getOptions().enabled = false; this.emit('close'); return result; }); } /** Get all installed event processors. */ getEventProcessors() { return this._eventProcessors; } /** @inheritDoc */ addEventProcessor(eventProcessor) { this._eventProcessors.push(eventProcessor); } /** @inheritdoc */ init() { if (this._isEnabled()) { this._setupIntegrations(); } } /** * Gets an installed integration by its name. * * @returns The installed integration or `undefined` if no integration with that `name` was installed. */ getIntegrationByName(integrationName) { return this._integrations[integrationName] ; } /** * @inheritDoc */ addIntegration(integration) { const isAlreadyInstalled = this._integrations[integration.name]; // This hook takes care of only installing if not already installed setupIntegration(this, integration, this._integrations); // Here we need to check manually to make sure to not run this multiple times if (!isAlreadyInstalled) { afterSetupIntegrations(this, [integration]); } } /** * @inheritDoc */ sendEvent(event, hint = {}) { this.emit('beforeSendEvent', event, hint); let env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel); for (const attachment of hint.attachments || []) { env = addItemToEnvelope(env, createAttachmentEnvelopeItem(attachment)); } const promise = this.sendEnvelope(env); if (promise) { promise.then(sendResponse => this.emit('afterSendEvent', event, sendResponse), null); } } /** * @inheritDoc */ sendSession(session) { const env = createSessionEnvelope(session, this._dsn, this._options._metadata, this._options.tunnel); // sendEnvelope should not throw // eslint-disable-next-line @typescript-eslint/no-floating-promises this.sendEnvelope(env); } /** * @inheritDoc */ recordDroppedEvent(reason, category, _event) { // Note: we use `event` in replay, where we overwrite this hook. if (this._options.sendClientReports) { // We want to track each category (error, transaction, session, replay_event) separately // but still keep the distinction between different type of outcomes. // We could use nested maps, but it's much easier to read and type this way. // A correct type for map-based implementation if we want to go that route // would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>` // With typescript 4.1 we could even use template literal types const key = `${reason}:${category}`; DEBUG_BUILD && logger.log(`Adding outcome: "${key}"`); // The following works because undefined + 1 === NaN and NaN is falsy this._outcomes[key] = this._outcomes[key] + 1 || 1; } } // Keep on() & emit() signatures in sync with types' client.ts interface /* eslint-disable @typescript-eslint/unified-signatures */ /** @inheritdoc */ /** @inheritdoc */ on(hook, callback) { if (!this._hooks[hook]) { this._hooks[hook] = []; } // @ts-expect-error We assue the types are correct this._hooks[hook].push(callback); } /** @inheritdoc */ /** @inheritdoc */ emit(hook, ...rest) { if (this._hooks[hook]) { this._hooks[hook].forEach(callback => callback(...rest)); } } /** * @inheritdoc */ sendEnvelope(envelope) { this.emit('beforeEnvelope', envelope); if (this._isEnabled() && this._transport) { return this._transport.send(envelope).then(null, reason => { DEBUG_BUILD && logger.error('Error while sending event:', reason); return reason; }); } DEBUG_BUILD && logger.error('Transport disabled'); return resolvedSyncPromise({}); } /* eslint-enable @typescript-eslint/unified-signatures */ /** Setup integrations for this client. */ _setupIntegrations() { const { integrations } = this._options; this._integrations = setupIntegrations(this, integrations); afterSetupIntegrations(this, integrations); } /** Updates existing session based on the provided event */ _updateSessionFromEvent(session, event) { let crashed = false; let errored = false; const exceptions = event.exception && event.exception.values; if (exceptions) { errored = true; for (const ex of exceptions) { const mechanism = ex.mechanism; if (mechanism && mechanism.handled === false) { crashed = true; break; } } } // A session is updated and that session update is sent in only one of the two following scenarios: // 1. Session with non terminal status and 0 errors + an error occurred -> Will set error count to 1 and send update // 2. Session with non terminal status and 1 error + a crash occurred -> Will set status crashed and send update const sessionNonTerminal = session.status === 'ok'; const shouldUpdateAndSend = (sessionNonTerminal && session.errors === 0) || (sessionNonTerminal && crashed); if (shouldUpdateAndSend) { updateSession(session, { ...(crashed && { status: 'crashed' }), errors: session.errors || Number(errored || crashed), }); this.captureSession(session); } } /** * Determine if the client is finished processing. Returns a promise because it will wait `timeout` ms before saying * "no" (resolving to `false`) in order to give the client a chance to potentially finish first. * * @param timeout The time, in ms, after which to resolve to `false` if the client is still busy. Passing `0` (or not * passing anything) will make the promise wait as long as it takes for processing to finish before resolving to * `true`. * @returns A promise which will resolve to `true` if processing is already done or finishes before the timeout, and * `false` otherwise */ _isClientDoneProcessing(timeout) { return new SyncPromise(resolve => { let ticked = 0; const tick = 1; const interval = setInterval(() => { if (this._numProcessing == 0) { clearInterval(interval); resolve(true); } else { ticked += tick; if (timeout && ticked >= timeout) { clearInterval(interval); resolve(false); } } }, tick); }); } /** Determines whether this SDK is enabled and a transport is present. */ _isEnabled() { return this.getOptions().enabled !== false && this._transport !== undefined; } /** * Adds common information to events. * * The information includes release and environment from `options`, * breadcrumbs and context (extra, tags and user) from the scope. * * Information that is already present in the event is never overwritten. For * nested objects, such as the context, keys are merged. * * @param event The original event. * @param hint May contain additional information about the original exception. * @param scope A scope containing event metadata. * @returns A new event with more information. */ _prepareEvent( event, hint, scope, isolationScope = getIsolationScope(), ) { const options = this.getOptions(); const integrations = Object.keys(this._integrations); if (!hint.integrations && integrations.length > 0) { hint.integrations = integrations; } this.emit('preprocessEvent', event, hint); return prepareEvent(options, event, hint, scope, this, isolationScope).then(evt => { if (evt === null) { return evt; } const propagationContext = { ...isolationScope.getPropagationContext(), ...(scope ? scope.getPropagationContext() : undefined), }; const trace = evt.contexts && evt.contexts.trace; if (!trace && propagationContext) { const { traceId: trace_id, spanId, parentSpanId, dsc } = propagationContext; evt.contexts = { trace: { trace_id, span_id: spanId, parent_span_id: parentSpanId, }, ...evt.contexts, }; const dynamicSamplingContext = dsc ? dsc : getDynamicSamplingContextFromClient(trace_id, this); evt.sdkProcessingMetadata = { dynamicSamplingContext, ...evt.sdkProcessingMetadata, }; } return evt; }); } /** * Processes the event and logs an error in case of rejection * @param event * @param hint * @param scope */ _captureEvent(event, hint = {}, scope) { return this._processEvent(event, hint, scope).then( finalEvent => { return finalEvent.event_id; }, reason => { if (DEBUG_BUILD) { // If something's gone wrong, log the error as a warning. If it's just us having used a `SentryError` for // control flow, log just the message (no stack) as a log-level log. const sentryError = reason ; if (sentryError.logLevel === 'log') { logger.log(sentryError.message); } else { logger.warn(sentryError); } } return undefined; }, ); } /** * Processes an event (either error or message) and sends it to Sentry. * * This also adds breadcrumbs and context information to the event. However, * platform specific meta data (such as the User's IP address) must be added * by the SDK implementor. * * * @param event The event to send to Sentry. * @param hint May contain additional information about the original exception. * @param scope A scope containing event metadata. * @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send. */ _processEvent(event, hint, scope) { const options = this.getOptions(); const { sampleRate } = options; const isTransaction = isTransactionEvent(event); const isError = isErrorEvent(event); const eventType = event.type || 'error'; const beforeSendLabel = `before send for type \`${eventType}\``; // 1.0 === 100% events are sent // 0.0 === 0% events are sent // Sampling for transaction happens somewhere else const parsedSampleRate = typeof sampleRate === 'undefined' ? undefined : parseSampleRate(sampleRate); if (isError && typeof parsedSampleRate === 'number' && Math.random() > parsedSampleRate) { this.recordDroppedEvent('sample_rate', 'error', event); return rejectedSyncPromise( new SentryError( `Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`, 'log', ), ); } const dataCategory = eventType === 'replay_event' ? 'replay' : eventType; const sdkProcessingMetadata = event.sdkProcessingMetadata || {}; const capturedSpanIsolationScope = sdkProcessingMetadata.capturedSpanIsolationScope; return this._prepareEvent(event, hint, scope, capturedSpanIsolationScope) .then(prepared => { if (prepared === null) { this.recordDroppedEvent('event_processor', dataCategory, event); throw new SentryError('An event processor returned `null`, will not send event.', 'log'); } const isInternalException = hint.data && (hint.data ).__sentry__ === true; if (isInternalException) { return prepared; } const result = processBeforeSend(options, prepared, hint); return _validateBeforeSendResult(result, beforeSendLabel); }) .then(processedEvent => { if (processedEvent === null) { this.recordDroppedEvent('before_send', dataCategory, event); throw new SentryError(`${beforeSendLabel} returned \`null\`, will not send event.`, 'log'); } const session = scope && scope.getSession(); if (!isTransaction && session) { this._updateSessionFromEvent(session, processedEvent); } // None of the Sentry built event processor will update transaction name, // so if the transaction name has been changed by an event processor, we know // it has to come from custom event processor added by a user const transactionInfo = processedEvent.transaction_info; if (isTransaction && transactionInfo && processedEvent.transaction !== event.transaction) { const source = 'custom'; processedEvent.transaction_info = { ...transactionInfo, source, }; } this.sendEvent(processedEvent, hint); return processedEvent; }) .then(null, reason => { if (reason instanceof SentryError) { throw reason; } this.captureException(reason, { data: { __sentry__: true, }, originalException: reason, }); throw new SentryError( `Event processing pipeline threw an error, original event will not be sent. Details have been sent as a new event.\nReason: ${reason}`, ); }); } /** * Occupies the client with processing and event */ _process(promise) { this._numProcessing++; void promise.then( value => { this._numProcessing--; return value; }, reason => { this._numProcessing--; return reason; }, ); } /** * Clears outcomes on this client and returns them. */ _clearOutcomes() { const outcomes = this._outcomes; this._outcomes = {}; return Object.keys(outcomes).map(key => { const [reason, category] = key.split(':') ; return { reason, category, quantity: outcomes[key], }; }); } /** * @inheritDoc */ // eslint-disable-next-line @typescript-eslint/no-explicit-any}
/** * Verifies that return value of configured `beforeSend` or `beforeSendTransaction` is of expected type, and returns the value if so. */function _validateBeforeSendResult( beforeSendResult, beforeSendLabel,) { const invalidValueError = `${beforeSendLabel} must return \`null\` or a valid event.`; if (isThenable(beforeSendResult)) { return beforeSendResult.then( event => { if (!isPlainObject(event) && event !== null) { throw new SentryError(invalidValueError); } return event; }, e => { throw new SentryError(`${beforeSendLabel} rejected with ${e}`); }, ); } else if (!isPlainObject(beforeSendResult) && beforeSendResult !== null) { throw new SentryError(invalidValueError); } return beforeSendResult;}
/** * Process the matching `beforeSendXXX` callback. */function processBeforeSend( options, event, hint,) { const { beforeSend, beforeSendTransaction } = options; if (isErrorEvent(event) && beforeSend) { return beforeSend(event, hint); } if (isTransactionEvent(event) && beforeSendTransaction) { return beforeSendTransaction(event, hint); } return event;}
function isErrorEvent(event) { return event.type === undefined;}
function isTransactionEvent(event) { return event.type === 'transaction';}
/** * Create envelope from check in item. */function createCheckInEnvelope( checkIn, dynamicSamplingContext, metadata, tunnel, dsn,) { const headers = { sent_at: new Date().toISOString(), }; if (metadata && metadata.sdk) { headers.sdk = { name: metadata.sdk.name, version: metadata.sdk.version, }; } if (!!tunnel && !!dsn) { headers.dsn = dsnToString(dsn); } if (dynamicSamplingContext) { headers.trace = dropUndefinedKeys(dynamicSamplingContext) ; } const item = createCheckInEnvelopeItem(checkIn); return createEnvelope(headers, [item]);}
function createCheckInEnvelopeItem(checkIn) { const checkInHeaders = { type: 'check_in', }; return [checkInHeaders, checkIn];}
/** * The Sentry Server Runtime Client SDK. */class ServerRuntimeClient
extends BaseClient { /** * Creates a new Edge SDK instance. * @param options Configuration options for this SDK. */ constructor(options) { // Server clients always support tracing registerSpanErrorInstrumentation(); super(options); } /** * @inheritDoc */ eventFromException(exception, hint) { return resolvedSyncPromise(eventFromUnknownInput(this, this._options.stackParser, exception, hint)); } /** * @inheritDoc */ eventFromMessage( message, level = 'info', hint, ) { return resolvedSyncPromise( eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace), ); } /** * @inheritDoc */ // eslint-disable-next-line @typescript-eslint/no-explicit-any captureException(exception, hint, scope) { // Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only // when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload // sent to the Server only when the `requestHandler` middleware is used if (this._options.autoSessionTracking && this._sessionFlusher) { const requestSession = getIsolationScope().getRequestSession(); // Necessary checks to ensure this is code block is executed only within a request // Should override the status only if `requestSession.status` is `Ok`, which is its initial stage if (requestSession && requestSession.status === 'ok') { requestSession.status = 'errored'; } } return super.captureException(exception, hint, scope); } /** * @inheritDoc */ captureEvent(event, hint, scope) { // Check if the flag `autoSessionTracking` is enabled, and if `_sessionFlusher` exists because it is initialised only // when the `requestHandler` middleware is used, and hence the expectation is to have SessionAggregates payload // sent to the Server only when the `requestHandler` middleware is used if (this._options.autoSessionTracking && this._sessionFlusher) { const eventType = event.type || 'exception'; const isException = eventType === 'exception' && event.exception && event.exception.values && event.exception.values.length > 0; // If the event is of type Exception, then a request session should be captured if (isException) { const requestSession = getIsolationScope().getRequestSession(); // Ensure that this is happening within the bounds of a request, and make sure not to override // Session Status if Errored / Crashed if (requestSession && requestSession.status === 'ok') { requestSession.status = 'errored'; } } } return super.captureEvent(event, hint, scope); } /** * * @inheritdoc */ close(timeout) { if (this._sessionFlusher) { this._sessionFlusher.close(); } return super.close(timeout); } /** Method that initialises an instance of SessionFlusher on Client */ initSessionFlusher() { const { release, environment } = this._options; if (!release) { DEBUG_BUILD && logger.warn('Cannot initialise an instance of SessionFlusher if no release is provided!'); } else { this._sessionFlusher = new SessionFlusher(this, { release, environment, }); } } /** * Create a cron monitor check in and send it to Sentry. * * @param checkIn An object that describes a check in. * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want * to create a monitor automatically when sending a check in. */ captureCheckIn(checkIn, monitorConfig, scope) { const id = 'checkInId' in checkIn && checkIn.checkInId ? checkIn.checkInId : uuid4(); if (!this._isEnabled()) { DEBUG_BUILD && logger.warn('SDK not enabled, will not capture checkin.'); return id; } const options = this.getOptions(); const { release, environment, tunnel } = options; const serializedCheckIn = { check_in_id: id, monitor_slug: checkIn.monitorSlug, status: checkIn.status, release, environment, }; if ('duration' in checkIn) { serializedCheckIn.duration = checkIn.duration; } if (monitorConfig) { serializedCheckIn.monitor_config = { schedule: monitorConfig.schedule, checkin_margin: monitorConfig.checkinMargin, max_runtime: monitorConfig.maxRuntime, timezone: monitorConfig.timezone, }; } const [dynamicSamplingContext, traceContext] = this._getTraceInfoFromScope(scope); if (traceContext) { serializedCheckIn.contexts = { trace: traceContext, }; } const envelope = createCheckInEnvelope( serializedCheckIn, dynamicSamplingContext, this.getSdkMetadata(), tunnel, this.getDsn(), ); DEBUG_BUILD && logger.info('Sending checkin:', checkIn.monitorSlug, checkIn.status); // sendEnvelope should not throw // eslint-disable-next-line @typescript-eslint/no-floating-promises this.sendEnvelope(envelope); return id; } /** * Method responsible for capturing/ending a request session by calling `incrementSessionStatusCount` to increment * appropriate session aggregates bucket */ _captureRequestSession() { if (!this._sessionFlusher) { DEBUG_BUILD && logger.warn('Discarded request mode session because autoSessionTracking option was disabled'); } else { this._sessionFlusher.incrementSessionStatusCount(); } } /** * @inheritDoc */ _prepareEvent( event, hint, scope, isolationScope, ) { if (this._options.platform) { event.platform = event.platform || this._options.platform; } if (this._options.runtime) { event.contexts = { ...event.contexts, runtime: (event.contexts || {}).runtime || this._options.runtime, }; } if (this._options.serverName) { event.server_name = event.server_name || this._options.serverName; } return super._prepareEvent(event, hint, scope, isolationScope); } /** Extract trace information from scope */ _getTraceInfoFromScope( scope, ) { if (!scope) { return [undefined, undefined]; } const span = _getSpanForScope(scope); if (span) { const rootSpan = getRootSpan(span); const samplingContext = getDynamicSamplingContextFromSpan(rootSpan); return [samplingContext, spanToTraceContext(rootSpan)]; } const { traceId, spanId, parentSpanId, dsc } = scope.getPropagationContext(); const traceContext = { trace_id: traceId, span_id: spanId, parent_span_id: parentSpanId, }; if (dsc) { return [dsc, traceContext]; } return [getDynamicSamplingContextFromClient(traceId, this), traceContext]; }}
/** A class object that can instantiate Client objects. */
/** * Internal function to create a new SDK client instance. The client is * installed and then bound to the current scope. * * @param clientClass The client class to instantiate. * @param options Options to pass to the client. */function initAndBind( clientClass, options,) { if (options.debug === true) { if (DEBUG_BUILD) { logger.enable(); } else { // use `console.warn` rather than `logger.warn` since by non-debug bundles have all `logger.x` statements stripped consoleSandbox(() => { // eslint-disable-next-line no-console console.warn('[Sentry] Cannot initialize SDK with `debug` option using a non-debug bundle.'); }); } } const scope = getCurrentScope(); scope.update(options.initialScope); const client = new clientClass(options); setCurrentClient(client); client.init();}
/** * Make the given client the current client. */function setCurrentClient(client) { getCurrentScope().setClient(client); registerClientOnGlobalHub(client);}
/** * Unfortunately, we still have to manually bind the client to the "hub" property set on the global * Sentry carrier object. This is because certain scripts (e.g. our loader script) obtain * the client via `window.__SENTRY__.hub.getClient()`. * * @see {@link ./asyncContext/stackStrategy.ts getAsyncContextStack} */function registerClientOnGlobalHub(client) { const sentryGlobal = getSentryCarrier(getMainCarrier()) ; if (sentryGlobal.hub && typeof sentryGlobal.hub.getStackTop === 'function') { sentryGlobal.hub.getStackTop().client = client; }}
const DEFAULT_TRANSPORT_BUFFER_SIZE = 64;
/** * Creates an instance of a Sentry `Transport` * * @param options * @param makeRequest */function createTransport( options, makeRequest, buffer = makePromiseBuffer( options.bufferSize || DEFAULT_TRANSPORT_BUFFER_SIZE, ),) { let rateLimits = {}; const flush = (timeout) => buffer.drain(timeout); function send(envelope) { const filteredEnvelopeItems = []; // Drop rate limited items from envelope forEachEnvelopeItem(envelope, (item, type) => { const dataCategory = envelopeItemTypeToDataCategory(type); if (isRateLimited(rateLimits, dataCategory)) { const event = getEventForEnvelopeItem(item, type); options.recordDroppedEvent('ratelimit_backoff', dataCategory, event); } else { filteredEnvelopeItems.push(item); } }); // Skip sending if envelope is empty after filtering out rate limited events if (filteredEnvelopeItems.length === 0) { return resolvedSyncPromise({}); } // eslint-disable-next-line @typescript-eslint/no-explicit-any const filteredEnvelope = createEnvelope(envelope[0], filteredEnvelopeItems ); // Creates client report for each item in an envelope const recordEnvelopeLoss = (reason) => { forEachEnvelopeItem(filteredEnvelope, (item, type) => { const event = getEventForEnvelopeItem(item, type); options.recordDroppedEvent(reason, envelopeItemTypeToDataCategory(type), event); }); }; const requestTask = () => makeRequest({ body: serializeEnvelope(filteredEnvelope) }).then( response => { // We don't want to throw on NOK responses, but we want to at least log them if (response.statusCode !== undefined && (response.statusCode < 200 || response.statusCode >= 300)) { DEBUG_BUILD && logger.warn(`Sentry responded with status code ${response.statusCode} to sent event.`); } rateLimits = updateRateLimits(rateLimits, response); return response; }, error => { recordEnvelopeLoss('network_error'); throw error; }, ); return buffer.add(requestTask).then( result => result, error => { if (error instanceof SentryError) { DEBUG_BUILD && logger.error('Skipped sending event because buffer is full.'); recordEnvelopeLoss('queue_overflow'); return resolvedSyncPromise({}); } else { throw error; } }, ); } return { send, flush, };}
function getEventForEnvelopeItem(item, type) { if (type !== 'event' && type !== 'transaction') { return undefined; } return Array.isArray(item) ? (item )[1] : undefined;}
const SDK_VERSION = '8.0.0-beta.4';
/** * Default maximum number of breadcrumbs added to an event. Can be overwritten * with {@link Options.maxBreadcrumbs}. */const DEFAULT_BREADCRUMBS = 100;
/** * Records a new breadcrumb which will be attached to future events. * * Breadcrumbs will be added to subsequent events to provide more context on * user's actions prior to an error or crash. */function addBreadcrumb(breadcrumb, hint) { const client = getClient(); const isolationScope = getIsolationScope(); if (!client) return; const { beforeBreadcrumb = null, maxBreadcrumbs = DEFAULT_BREADCRUMBS } = client.getOptions(); if (maxBreadcrumbs <= 0) return; const timestamp = dateTimestampInSeconds(); const mergedBreadcrumb = { timestamp, ...breadcrumb }; const finalBreadcrumb = beforeBreadcrumb ? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) ) : mergedBreadcrumb; if (finalBreadcrumb === null) return; if (client.emit) { client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint); } isolationScope.addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);}
let originalFunctionToString;
const INTEGRATION_NAME$f = 'FunctionToString';
const SETUP_CLIENTS$1 = new WeakMap();
const _functionToStringIntegration = (() => { return { name: INTEGRATION_NAME$f, setupOnce() { // eslint-disable-next-line @typescript-eslint/unbound-method originalFunctionToString = Function.prototype.toString; // intrinsics (like Function.prototype) might be immutable in some environments // e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) try { // eslint-disable-next-line @typescript-eslint/no-explicit-any Function.prototype.toString = function ( ...args) { const originalFunction = getOriginalFunction(this); const context = SETUP_CLIENTS$1.has(getClient() ) && originalFunction !== undefined ? originalFunction : this; return originalFunctionToString.apply(context, args); }; } catch (e) { // ignore errors here, just don't patch this } }, setup(client) { SETUP_CLIENTS$1.set(client, true); }, };}) ;
/** * Patch toString calls to return proper name for wrapped functions. * * ```js * Sentry.init({ * integrations: [ * functionToStringIntegration(), * ], * }); * ``` */const functionToStringIntegration = defineIntegration(_functionToStringIntegration);
// "Script error." is hard coded into browsers for errors that it can't read.// this is the result of a script being pulled in from an external domain and CORS.const DEFAULT_IGNORE_ERRORS = [ /^Script error\.?$/, /^Javascript error: Script error\.? on line 0$/, /^ResizeObserver loop completed with undelivered notifications.$/, /^Cannot redefine property: googletag$/,];
/** Options for the InboundFilters integration */
const INTEGRATION_NAME$e = 'InboundFilters';const _inboundFiltersIntegration = ((options = {}) => { return { name: INTEGRATION_NAME$e, processEvent(event, _hint, client) { const clientOptions = client.getOptions(); const mergedOptions = _mergeOptions(options, clientOptions); return _shouldDropEvent$1(event, mergedOptions) ? null : event; }, };}) ;
const inboundFiltersIntegration = defineIntegration(_inboundFiltersIntegration);
function _mergeOptions( internalOptions = {}, clientOptions = {},) { return { allowUrls: [...(internalOptions.allowUrls || []), ...(clientOptions.allowUrls || [])], denyUrls: [...(internalOptions.denyUrls || []), ...(clientOptions.denyUrls || [])], ignoreErrors: [ ...(internalOptions.ignoreErrors || []), ...(clientOptions.ignoreErrors || []), ...(internalOptions.disableErrorDefaults ? [] : DEFAULT_IGNORE_ERRORS), ], ignoreTransactions: [...(internalOptions.ignoreTransactions || []), ...(clientOptions.ignoreTransactions || [])], ignoreInternal: internalOptions.ignoreInternal !== undefined ? internalOptions.ignoreInternal : true, };}
function _shouldDropEvent$1(event, options) { if (options.ignoreInternal && _isSentryError(event)) { DEBUG_BUILD && logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`); return true; } if (_isIgnoredError(event, options.ignoreErrors)) { DEBUG_BUILD && logger.warn( `Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`, ); return true; } if (_isIgnoredTransaction(event, options.ignoreTransactions)) { DEBUG_BUILD && logger.warn( `Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`, ); return true; } if (_isDeniedUrl(event, options.denyUrls)) { DEBUG_BUILD && logger.warn( `Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription( event, )}.\nUrl: ${_getEventFilterUrl(event)}`, ); return true; } if (!_isAllowedUrl(event, options.allowUrls)) { DEBUG_BUILD && logger.warn( `Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription( event, )}.\nUrl: ${_getEventFilterUrl(event)}`, ); return true; } return false;}
function _isIgnoredError(event, ignoreErrors) { // If event.type, this is not an error if (event.type || !ignoreErrors || !ignoreErrors.length) { return false; } return _getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors));}
function _isIgnoredTransaction(event, ignoreTransactions) { if (event.type !== 'transaction' || !ignoreTransactions || !ignoreTransactions.length) { return false; } const name = event.transaction; return name ? stringMatchesSomePattern(name, ignoreTransactions) : false;}
function _isDeniedUrl(event, denyUrls) { // TODO: Use Glob instead? if (!denyUrls || !denyUrls.length) { return false; } const url = _getEventFilterUrl(event); return !url ? false : stringMatchesSomePattern(url, denyUrls);}
function _isAllowedUrl(event, allowUrls) { // TODO: Use Glob instead? if (!allowUrls || !allowUrls.length) { return true; } const url = _getEventFilterUrl(event); return !url ? true : stringMatchesSomePattern(url, allowUrls);}
function _getPossibleEventMessages(event) { const possibleMessages = []; if (event.message) { possibleMessages.push(event.message); } let lastException; try { // @ts-expect-error Try catching to save bundle size lastException = event.exception.values[event.exception.values.length - 1]; } catch (e) { // try catching to save bundle size checking existence of variables } if (lastException) { if (lastException.value) { possibleMessages.push(lastException.value); if (lastException.type) { possibleMessages.push(`${lastException.type}: ${lastException.value}`); } } } return possibleMessages;}
function _isSentryError(event) { try { // @ts-expect-error can't be a sentry error if undefined return event.exception.values[0].type === 'SentryError'; } catch (e) { // ignore } return false;}
function _getLastValidUrl(frames = []) { for (let i = frames.length - 1; i >= 0; i--) { const frame = frames[i]; if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') { return frame.filename || null; } } return null;}
function _getEventFilterUrl(event) { try { let frames; try { // @ts-expect-error we only care about frames if the whole thing here is defined frames = event.exception.values[0].stacktrace.frames; } catch (e) { // ignore } return frames ? _getLastValidUrl(frames) : null; } catch (oO) { DEBUG_BUILD && logger.error(`Cannot extract url for event ${getEventDescription(event)}`); return null; }}
const DEFAULT_KEY = 'cause';const DEFAULT_LIMIT = 5;
const INTEGRATION_NAME$d = 'LinkedErrors';
const _linkedErrorsIntegration = ((options = {}) => { const limit = options.limit || DEFAULT_LIMIT; const key = options.key || DEFAULT_KEY; return { name: INTEGRATION_NAME$d, preprocessEvent(event, hint, client) { const options = client.getOptions(); applyAggregateErrorsToEvent( exceptionFromError, options.stackParser, options.maxValueLength, key, limit, event, hint, ); }, };}) ;
const linkedErrorsIntegration = defineIntegration(_linkedErrorsIntegration);
const DEFAULT_OPTIONS = { include: { cookies: true, data: true, headers: true, ip: false, query_string: true, url: true, user: { id: true, username: true, email: true, }, }, transactionNamingScheme: 'methodPath' ,};
const INTEGRATION_NAME$c = 'RequestData';
const _requestDataIntegration = ((options = {}) => { const _options = { ...DEFAULT_OPTIONS, ...options, include: { ...DEFAULT_OPTIONS.include, ...options.include, user: options.include && typeof options.include.user === 'boolean' ? options.include.user : { ...DEFAULT_OPTIONS.include.user, // Unclear why TS still thinks `options.include.user` could be a boolean at this point ...((options.include || {}).user ), }, }, }; return { name: INTEGRATION_NAME$c, processEvent(event, _hint, client) { // Note: In the long run, most of the logic here should probably move into the request data utility functions. For // the moment it lives here, though, until https://github.com/getsentry/sentry-javascript/issues/5718 is addressed. // (TL;DR: Those functions touch many parts of the repo in many different ways, and need to be clened up. Once // that's happened, it will be easier to add this logic in without worrying about unexpected side effects.) const { transactionNamingScheme } = _options; const { sdkProcessingMetadata = {} } = event; const req = sdkProcessingMetadata.request; if (!req) { return event; } const addRequestDataOptions = convertReqDataIntegrationOptsToAddReqDataOpts(_options); const processedEvent = addRequestDataToEvent(event, req, addRequestDataOptions); // Transaction events already have the right `transaction` value if (event.type === 'transaction' || transactionNamingScheme === 'handler') { return processedEvent; } // In all other cases, use the request's associated transaction (if any) to overwrite the event's `transaction` // value with a high-quality one const reqWithTransaction = req ; const transaction = reqWithTransaction._sentryTransaction; if (transaction) { const name = spanToJSON(transaction).description || ''; // TODO (v8): Remove the nextjs check and just base it on `transactionNamingScheme` for all SDKs. (We have to // keep it the way it is for the moment, because changing the names of transactions in Sentry has the potential // to break things like alert rules.) const shouldIncludeMethodInTransactionName = getSDKName(client) === 'sentry.javascript.nextjs' ? name.startsWith('/api') : transactionNamingScheme !== 'path'; const [transactionValue] = extractPathForTransaction(req, { path: true, method: shouldIncludeMethodInTransactionName, customRoute: name, }); processedEvent.transaction = transactionValue; } return processedEvent; }, };}) ;
/** * Add data about a request to an event. Primarily for use in Node-based SDKs, but included in `@sentry/core` * so it can be used in cross-platform SDKs like `@sentry/nextjs`. */const requestDataIntegration = defineIntegration(_requestDataIntegration);
/** Convert this integration's options to match what `addRequestDataToEvent` expects *//** TODO: Can possibly be deleted once https://github.com/getsentry/sentry-javascript/issues/5718 is fixed */function convertReqDataIntegrationOptsToAddReqDataOpts( integrationOptions,) { const { transactionNamingScheme, include: { ip, user, ...requestOptions }, } = integrationOptions; const requestIncludeKeys = ['method']; for (const [key, value] of Object.entries(requestOptions)) { if (value) { requestIncludeKeys.push(key); } } let addReqDataUserOpt; if (user === undefined) { addReqDataUserOpt = true; } else if (typeof user === 'boolean') { addReqDataUserOpt = user; } else { const userIncludeKeys = []; for (const [key, value] of Object.entries(user)) { if (value) { userIncludeKeys.push(key); } } addReqDataUserOpt = userIncludeKeys; } return { include: { ip, user: addReqDataUserOpt, request: requestIncludeKeys.length !== 0 ? requestIncludeKeys : undefined, transaction: transactionNamingScheme, }, };}
function getSDKName(client) { try { // For a long chain like this, it's fewer bytes to combine a try-catch with assuming everything is there than to // write out a long chain of `a && a.b && a.b.c && ...` // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return client.getOptions()._metadata.sdk.name; } catch (err) { // In theory we should never get here return undefined; }}
const INTEGRATION_NAME$b = 'CaptureConsole';
const _captureConsoleIntegration = ((options = {}) => { const levels = options.levels || CONSOLE_LEVELS; return { name: INTEGRATION_NAME$b, setup(client) { if (!('console' in GLOBAL_OBJ)) { return; } addConsoleInstrumentationHandler(({ args, level }) => { if (getClient() !== client || !levels.includes(level)) { return; } consoleHandler(args, level); }); }, };}) ;
/** * Send Console API calls as Sentry Events. */const captureConsoleIntegration = defineIntegration(_captureConsoleIntegration);
function consoleHandler(args, level) { const captureContext = { level: severityLevelFromString(level), extra: { arguments: args, }, }; withScope$1(scope => { scope.addEventProcessor(event => { event.logger = 'console'; addExceptionMechanism(event, { handled: false, type: 'console', }); return event; }); if (level === 'assert' && args[0] === false) { const message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`; scope.setExtra('arguments', args.slice(1)); captureMessage(message, captureContext); return; } const error = args.find(arg => arg instanceof Error); if (error) { captureException(error, captureContext); return; } const message = safeJoin(args, ' '); captureMessage(message, captureContext); });}
const INTEGRATION_NAME$a = 'Debug';
/** * Integration to debug sent Sentry events. * This integration should not be used in production. */const _debugIntegration = ((options = {}) => { const _options = { debugger: false, stringify: false, ...options, }; return { name: INTEGRATION_NAME$a, setup(client) { client.on('beforeSendEvent', (event, hint) => { if (_options.debugger) { // eslint-disable-next-line no-debugger debugger; } /* eslint-disable no-console */ consoleSandbox(() => { if (_options.stringify) { console.log(JSON.stringify(event, null, 2)); if (hint && Object.keys(hint).length) { console.log(JSON.stringify(hint, null, 2)); } } else { console.log(event); if (hint && Object.keys(hint).length) { console.log(hint); } } }); /* eslint-enable no-console */ }); }, };}) ;
const debugIntegration = defineIntegration(_debugIntegration);
const INTEGRATION_NAME$9 = 'Dedupe';
const _dedupeIntegration = (() => { let previousEvent; return { name: INTEGRATION_NAME$9, processEvent(currentEvent) { // We want to ignore any non-error type events, e.g. transactions or replays // These should never be deduped, and also not be compared against as _previousEvent. if (currentEvent.type) { return currentEvent; } // Juuust in case something goes wrong try { if (_shouldDropEvent(currentEvent, previousEvent)) { DEBUG_BUILD && logger.warn('Event dropped due to being a duplicate of previously captured event.'); return null; } } catch (_oO) {} // eslint-disable-line no-empty return (previousEvent = currentEvent); }, };}) ;
/** * Deduplication filter. */const dedupeIntegration = defineIntegration(_dedupeIntegration);
/** only exported for tests. */function _shouldDropEvent(currentEvent, previousEvent) { if (!previousEvent) { return false; } if (_isSameMessageEvent(currentEvent, previousEvent)) { return true; } if (_isSameExceptionEvent(currentEvent, previousEvent)) { return true; } return false;}
function _isSameMessageEvent(currentEvent, previousEvent) { const currentMessage = currentEvent.message; const previousMessage = previousEvent.message; // If neither event has a message property, they were both exceptions, so bail out if (!currentMessage && !previousMessage) { return false; } // If only one event has a stacktrace, but not the other one, they are not the same if ((currentMessage && !previousMessage) || (!currentMessage && previousMessage)) { return false; } if (currentMessage !== previousMessage) { return false; } if (!_isSameFingerprint(currentEvent, previousEvent)) { return false; } if (!_isSameStacktrace(currentEvent, previousEvent)) { return false; } return true;}
function _isSameExceptionEvent(currentEvent, previousEvent) { const previousException = _getExceptionFromEvent(previousEvent); const currentException = _getExceptionFromEvent(currentEvent); if (!previousException || !currentException) { return false; } if (previousException.type !== currentException.type || previousException.value !== currentException.value) { return false; } if (!_isSameFingerprint(currentEvent, previousEvent)) { return false; } if (!_isSameStacktrace(currentEvent, previousEvent)) { return false; } return true;}
function _isSameStacktrace(currentEvent, previousEvent) { let currentFrames = _getFramesFromEvent(currentEvent); let previousFrames = _getFramesFromEvent(previousEvent); // If neither event has a stacktrace, they are assumed to be the same if (!currentFrames && !previousFrames) { return true; } // If only one event has a stacktrace, but not the other one, they are not the same if ((currentFrames && !previousFrames) || (!currentFrames && previousFrames)) { return false; } currentFrames = currentFrames ; previousFrames = previousFrames ; // If number of frames differ, they are not the same if (previousFrames.length !== currentFrames.length) { return false; } // Otherwise, compare the two for (let i = 0; i < previousFrames.length; i++) { const frameA = previousFrames[i]; const frameB = currentFrames[i]; if ( frameA.filename !== frameB.filename || frameA.lineno !== frameB.lineno || frameA.colno !== frameB.colno || frameA.function !== frameB.function ) { return false; } } return true;}
function _isSameFingerprint(currentEvent, previousEvent) { let currentFingerprint = currentEvent.fingerprint; let previousFingerprint = previousEvent.fingerprint; // If neither event has a fingerprint, they are assumed to be the same if (!currentFingerprint && !previousFingerprint) { return true; } // If only one event has a fingerprint, but not the other one, they are not the same if ((currentFingerprint && !previousFingerprint) || (!currentFingerprint && previousFingerprint)) { return false; } currentFingerprint = currentFingerprint ; previousFingerprint = previousFingerprint ; // Otherwise, compare the two try { return !!(currentFingerprint.join('') === previousFingerprint.join('')); } catch (_oO) { return false; }}
function _getExceptionFromEvent(event) { return event.exception && event.exception.values && event.exception.values[0];}
function _getFramesFromEvent(event) { const exception = event.exception; if (exception) { try { // @ts-expect-error Object could be undefined return exception.values[0].stacktrace.frames; } catch (_oO) { return undefined; } } return undefined;}
const INTEGRATION_NAME$8 = 'ExtraErrorData';
/** * Extract additional data for from original exceptions. */const _extraErrorDataIntegration = ((options = {}) => { const { depth = 3, captureErrorCause = true } = options; return { name: INTEGRATION_NAME$8, processEvent(event, hint) { return _enhanceEventWithErrorData(event, hint, depth, captureErrorCause); }, };}) ;
const extraErrorDataIntegration = defineIntegration(_extraErrorDataIntegration);
function _enhanceEventWithErrorData( event, hint = {}, depth, captureErrorCause,) { if (!hint.originalException || !isError(hint.originalException)) { return event; } const exceptionName = (hint.originalException ).name || hint.originalException.constructor.name; const errorData = _extractErrorData(hint.originalException , captureErrorCause); if (errorData) { const contexts = { ...event.contexts, }; const normalizedErrorData = normalize(errorData, depth); if (isPlainObject(normalizedErrorData)) { // We mark the error data as "already normalized" here, because we don't want other normalization procedures to // potentially truncate the data we just already normalized, with a certain depth setting. addNonEnumerableProperty(normalizedErrorData, '__sentry_skip_normalization__', true); contexts[exceptionName] = normalizedErrorData; } return { ...event, contexts, }; } return event;}
/** * Extract extra information from the Error object */function _extractErrorData(error, captureErrorCause) { // We are trying to enhance already existing event, so no harm done if it won't succeed try { const nativeKeys = [ 'name', 'message', 'stack', 'line', 'column', 'fileName', 'lineNumber', 'columnNumber', 'toJSON', ]; const extraErrorInfo = {}; // We want only enumerable properties, thus `getOwnPropertyNames` is redundant here, as we filter keys anyway. for (const key of Object.keys(error)) { if (nativeKeys.indexOf(key) !== -1) { continue; } const value = error[key]; extraErrorInfo[key] = isError(value) ? value.toString() : value; } // Error.cause is a standard property that is non enumerable, we therefore need to access it separately. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause if (captureErrorCause && error.cause !== undefined) { extraErrorInfo.cause = isError(error.cause) ? error.cause.toString() : error.cause; } // Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that) if (typeof error.toJSON === 'function') { const serializedError = error.toJSON() ; for (const key of Object.keys(serializedError)) { const value = serializedError[key]; extraErrorInfo[key] = isError(value) ? value.toString() : value; } } return extraErrorInfo; } catch (oO) { DEBUG_BUILD && logger.error('Unable to extract extra data from the Error object:', oO); } return null;}
const INTEGRATION_NAME$7 = 'RewriteFrames';
/** * Rewrite event frames paths. */const rewriteFramesIntegration = defineIntegration((options = {}) => { const root = options.root; const prefix = options.prefix || 'app:///'; const isBrowser = 'window' in GLOBAL_OBJ && GLOBAL_OBJ.window !== undefined; const iteratee = options.iteratee || generateIteratee({ isBrowser, root, prefix }); /** Process an exception event. */ function _processExceptionsEvent(event) { try { return { ...event, exception: { ...event.exception, // The check for this is performed inside `process` call itself, safe to skip here // eslint-disable-next-line @typescript-eslint/no-non-null-assertion values: event.exception.values.map(value => ({ ...value, ...(value.stacktrace && { stacktrace: _processStacktrace(value.stacktrace) }), })), }, }; } catch (_oO) { return event; } } /** Process a stack trace. */ function _processStacktrace(stacktrace) { return { ...stacktrace, frames: stacktrace && stacktrace.frames && stacktrace.frames.map(f => iteratee(f)), }; } return { name: INTEGRATION_NAME$7, processEvent(originalEvent) { let processedEvent = originalEvent; if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) { processedEvent = _processExceptionsEvent(processedEvent); } return processedEvent; }, };});
/** * Exported only for tests. */function generateIteratee({ isBrowser, root, prefix,}
) { return (frame) => { if (!frame.filename) { return frame; } // Determine if this is a Windows frame by checking for a Windows-style prefix such as `C:\` const isWindowsFrame = /^[a-zA-Z]:\\/.test(frame.filename) || // or the presence of a backslash without a forward slash (which are not allowed on Windows) (frame.filename.includes('\\') && !frame.filename.includes('/')); // Check if the frame filename begins with `/` const startsWithSlash = /^\//.test(frame.filename); if (isBrowser) { if (root) { const oldFilename = frame.filename; if (oldFilename.indexOf(root) === 0) { frame.filename = oldFilename.replace(root, prefix); } } } else { if (isWindowsFrame || startsWithSlash) { const filename = isWindowsFrame ? frame.filename .replace(/^[a-zA-Z]:/, '') // remove Windows-style prefix .replace(/\\/g, '/') // replace all `\\` instances with `/` : frame.filename; const base = root ? relative(root, filename) : basename(filename); frame.filename = `${prefix}${base}`; } } return frame; };}
const INTEGRATION_NAME$6 = 'SessionTiming';
const _sessionTimingIntegration = (() => { const startTime = Date.now(); return { name: INTEGRATION_NAME$6, processEvent(event) { const now = Date.now(); return { ...event, extra: { ...event.extra, ['session:start']: startTime, ['session:duration']: now - startTime, ['session:end']: now, }, }; }, };}) ;
/** * This function adds duration since the sessionTimingIntegration was initialized * till the time event was sent. */const sessionTimingIntegration = defineIntegration(_sessionTimingIntegration);
const COUNTER_METRIC_TYPE = 'c' ;const GAUGE_METRIC_TYPE = 'g' ;const SET_METRIC_TYPE = 's' ;const DISTRIBUTION_METRIC_TYPE = 'd' ;
/** * SDKs are required to bucket into 10 second intervals (rollup in seconds) * which is the current lower bound of metric accuracy. */const DEFAULT_FLUSH_INTERVAL = 10000;
/** * The maximum number of metrics that should be stored in memory. */const MAX_WEIGHT = 10000;
/** * Gets the metrics aggregator for a given client. * @param client The client for which to get the metrics aggregator. * @param Aggregator Optional metrics aggregator class to use to create an aggregator if one does not exist. */function getMetricsAggregatorForClient$1( client, Aggregator,) { const globalMetricsAggregators = getGlobalSingleton( 'globalMetricsAggregators', () => new WeakMap(), ); const aggregator = globalMetricsAggregators.get(client); if (aggregator) { return aggregator; } const newAggregator = new Aggregator(client); client.on('flush', () => newAggregator.flush()); client.on('close', () => newAggregator.close()); globalMetricsAggregators.set(client, newAggregator); return newAggregator;}
function addToMetricsAggregator( Aggregator, metricType, name, value, data = {},) { const client = data.client || getClient(); if (!client) { return; } const span = getActiveSpan(); const rootSpan = span ? getRootSpan(span) : undefined; const { unit, tags, timestamp } = data; const { release, environment } = client.getOptions(); const metricTags = {}; if (release) { metricTags.release = release; } if (environment) { metricTags.environment = environment; } if (rootSpan) { metricTags.transaction = spanToJSON(rootSpan).description || ''; } DEBUG_BUILD && logger.log(`Adding value of ${value} to ${metricType} metric ${name}`); const aggregator = getMetricsAggregatorForClient$1(client, Aggregator); aggregator.add(metricType, name, value, unit, { ...metricTags, ...tags }, timestamp);}
/** * Adds a value to a counter metric * * @experimental This API is experimental and might have breaking changes in the future. */function increment$1(aggregator, name, value = 1, data) { addToMetricsAggregator(aggregator, COUNTER_METRIC_TYPE, name, value, data);}
/** * Adds a value to a distribution metric * * @experimental This API is experimental and might have breaking changes in the future. */function distribution$1(aggregator, name, value, data) { addToMetricsAggregator(aggregator, DISTRIBUTION_METRIC_TYPE, name, value, data);}
/** * Adds a value to a set metric. Value must be a string or integer. * * @experimental This API is experimental and might have breaking changes in the future. */function set$1(aggregator, name, value, data) { addToMetricsAggregator(aggregator, SET_METRIC_TYPE, name, value, data);}
/** * Adds a value to a gauge metric * * @experimental This API is experimental and might have breaking changes in the future. */function gauge$1(aggregator, name, value, data) { addToMetricsAggregator(aggregator, GAUGE_METRIC_TYPE, name, value, data);}
const metrics = { increment: increment$1, distribution: distribution$1, set: set$1, gauge: gauge$1, /** * @ignore This is for internal use only. */ getMetricsAggregatorForClient: getMetricsAggregatorForClient$1,};
/** * Generate bucket key from metric properties. */function getBucketKey( metricType, name, unit, tags,) { const stringifiedTags = Object.entries(dropUndefinedKeys(tags)).sort((a, b) => a[0].localeCompare(b[0])); return `${metricType}${name}${unit}${stringifiedTags}`;}
/* eslint-disable no-bitwise *//** * Simple hash function for strings. */function simpleHash(s) { let rv = 0; for (let i = 0; i < s.length; i++) { const c = s.charCodeAt(i); rv = (rv << 5) - rv + c; rv &= rv; } return rv >>> 0;}/* eslint-enable no-bitwise */
/** * Serialize metrics buckets into a string based on statsd format. * * Example of format: * metric.name@second:1:1.2|d|#a:value,b:anothervalue|T12345677 * Segments: * name: metric.name * unit: second * value: [1, 1.2] * type of metric: d (distribution) * tags: { a: value, b: anothervalue } * timestamp: 12345677 */function serializeMetricBuckets(metricBucketItems) { let out = ''; for (const item of metricBucketItems) { const tagEntries = Object.entries(item.tags); const maybeTags = tagEntries.length > 0 ? `|#${tagEntries.map(([key, value]) => `${key}:${value}`).join(',')}` : ''; out += `${item.name}@${item.unit}:${item.metric}|${item.metricType}${maybeTags}|T${item.timestamp}\n`; } return out;}
/** * Sanitizes units * * These Regex's are straight from the normalisation docs: * https://develop.sentry.dev/sdk/metrics/#normalization */function sanitizeUnit(unit) { return unit.replace(/[^\w]+/gi, '_');}
/** * Sanitizes metric keys * * These Regex's are straight from the normalisation docs: * https://develop.sentry.dev/sdk/metrics/#normalization */function sanitizeMetricKey(key) { return key.replace(/[^\w\-.]+/gi, '_');}
/** * Sanitizes metric keys * * These Regex's are straight from the normalisation docs: * https://develop.sentry.dev/sdk/metrics/#normalization */function sanitizeTagKey(key) { return key.replace(/[^\w\-./]+/gi, '');}
/** * These Regex's are straight from the normalisation docs: * https://develop.sentry.dev/sdk/metrics/#normalization */const tagValueReplacements = [ ['\n', '\\n'], ['\r', '\\r'], ['\t', '\\t'], ['\\', '\\\\'], ['|', '\\u{7c}'], [',', '\\u{2c}'],];
function getCharOrReplacement(input) { for (const [search, replacement] of tagValueReplacements) { if (input === search) { return replacement; } } return input;}
function sanitizeTagValue(value) { return [...value].reduce((acc, char) => acc + getCharOrReplacement(char), '');}
/** * Sanitizes tags. */function sanitizeTags(unsanitizedTags) { const tags = {}; for (const key in unsanitizedTags) { if (Object.prototype.hasOwnProperty.call(unsanitizedTags, key)) { const sanitizedKey = sanitizeTagKey(key); tags[sanitizedKey] = sanitizeTagValue(String(unsanitizedTags[key])); } } return tags;}
/** * Captures aggregated metrics to the supplied client. */function captureAggregateMetrics(client, metricBucketItems) { logger.log(`Flushing aggregated metrics, number of metrics: ${metricBucketItems.length}`); const dsn = client.getDsn(); const metadata = client.getSdkMetadata(); const tunnel = client.getOptions().tunnel; const metricsEnvelope = createMetricEnvelope(metricBucketItems, dsn, metadata, tunnel); // sendEnvelope should not throw // eslint-disable-next-line @typescript-eslint/no-floating-promises client.sendEnvelope(metricsEnvelope);}
/** * Create envelope from a metric aggregate. */function createMetricEnvelope( metricBucketItems, dsn, metadata, tunnel,) { const headers = { sent_at: new Date().toISOString(), }; if (metadata && metadata.sdk) { headers.sdk = { name: metadata.sdk.name, version: metadata.sdk.version, }; } if (!!tunnel && dsn) { headers.dsn = dsnToString(dsn); } const item = createMetricEnvelopeItem(metricBucketItems); return createEnvelope(headers, [item]);}
function createMetricEnvelopeItem(metricBucketItems) { const payload = serializeMetricBuckets(metricBucketItems); const metricHeaders = { type: 'statsd', length: payload.length, }; return [metricHeaders, payload];}
/** * A metric instance representing a counter. */class CounterMetric { constructor( _value) {this._value = _value;} /** @inheritDoc */ get weight() { return 1; } /** @inheritdoc */ add(value) { this._value += value; } /** @inheritdoc */ toString() { return `${this._value}`; }}
/** * A metric instance representing a gauge. */class GaugeMetric {
constructor(value) { this._last = value; this._min = value; this._max = value; this._sum = value; this._count = 1; } /** @inheritDoc */ get weight() { return 5; } /** @inheritdoc */ add(value) { this._last = value; if (value < this._min) { this._min = value; } if (value > this._max) { this._max = value; } this._sum += value; this._count++; } /** @inheritdoc */ toString() { return `${this._last}:${this._min}:${this._max}:${this._sum}:${this._count}`; }}
/** * A metric instance representing a distribution. */class DistributionMetric {
constructor(first) { this._value = [first]; } /** @inheritDoc */ get weight() { return this._value.length; } /** @inheritdoc */ add(value) { this._value.push(value); } /** @inheritdoc */ toString() { return this._value.join(':'); }}
/** * A metric instance representing a set. */class SetMetric {
constructor( first) {this.first = first; this._value = new Set([first]); } /** @inheritDoc */ get weight() { return this._value.size; } /** @inheritdoc */ add(value) { this._value.add(value); } /** @inheritdoc */ toString() { return Array.from(this._value) .map(val => (typeof val === 'string' ? simpleHash(val) : val)) .join(':'); }}
const METRIC_MAP = { [COUNTER_METRIC_TYPE]: CounterMetric, [GAUGE_METRIC_TYPE]: GaugeMetric, [DISTRIBUTION_METRIC_TYPE]: DistributionMetric, [SET_METRIC_TYPE]: SetMetric,};
/** * A metrics aggregator that aggregates metrics in memory and flushes them periodically. */class MetricsAggregator { // TODO(@anonrig): Use FinalizationRegistry to have a proper way of flushing the buckets // when the aggregator is garbage collected. // Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry // Different metrics have different weights. We use this to limit the number of metrics // that we store in memory. // Cast to any so that it can use Node.js timeout // eslint-disable-next-line @typescript-eslint/no-explicit-any // SDKs are required to shift the flush interval by random() * rollup_in_seconds. // That shift is determined once per startup to create jittering. // An SDK is required to perform force flushing ahead of scheduled time if the memory // pressure is too high. There is no rule for this other than that SDKs should be tracking // abstract aggregation complexity (eg: a counter only carries a single float, whereas a // distribution is a float per emission). // // Force flush is used on either shutdown, flush() or when we exceed the max weight. constructor( _client) {this._client = _client; this._buckets = new Map(); this._bucketsTotalWeight = 0; this._interval = setInterval(() => this._flush(), DEFAULT_FLUSH_INTERVAL) ; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (this._interval.unref) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access this._interval.unref(); } this._flushShift = Math.floor((Math.random() * DEFAULT_FLUSH_INTERVAL) / 1000); this._forceFlush = false; } /** * @inheritDoc */ add( metricType, unsanitizedName, value, unsanitizedUnit = 'none', unsanitizedTags = {}, maybeFloatTimestamp = timestampInSeconds(), ) { const timestamp = Math.floor(maybeFloatTimestamp); const name = sanitizeMetricKey(unsanitizedName); const tags = sanitizeTags(unsanitizedTags); const unit = sanitizeUnit(unsanitizedUnit ); const bucketKey = getBucketKey(metricType, name, unit, tags); let bucketItem = this._buckets.get(bucketKey); // If this is a set metric, we need to calculate the delta from the previous weight. const previousWeight = bucketItem && metricType === SET_METRIC_TYPE ? bucketItem.metric.weight : 0; if (bucketItem) { bucketItem.metric.add(value); // TODO(abhi): Do we need this check? if (bucketItem.timestamp < timestamp) { bucketItem.timestamp = timestamp; } } else { bucketItem = { // @ts-expect-error we don't need to narrow down the type of value here, saves bundle size. metric: new METRIC_MAP[metricType](value), timestamp, metricType, name, unit, tags, }; this._buckets.set(bucketKey, bucketItem); } // If value is a string, it's a set metric so calculate the delta from the previous weight. const val = typeof value === 'string' ? bucketItem.metric.weight - previousWeight : value; updateMetricSummaryOnActiveSpan(metricType, name, val, unit, unsanitizedTags, bucketKey); // We need to keep track of the total weight of the buckets so that we can // flush them when we exceed the max weight. this._bucketsTotalWeight += bucketItem.metric.weight; if (this._bucketsTotalWeight >= MAX_WEIGHT) { this.flush(); } } /** * Flushes the current metrics to the transport via the transport. */ flush() { this._forceFlush = true; this._flush(); } /** * Shuts down metrics aggregator and clears all metrics. */ close() { this._forceFlush = true; clearInterval(this._interval); this._flush(); } /** * Flushes the buckets according to the internal state of the aggregator. * If it is a force flush, which happens on shutdown, it will flush all buckets. * Otherwise, it will only flush buckets that are older than the flush interval, * and according to the flush shift. * * This function mutates `_forceFlush` and `_bucketsTotalWeight` properties. */ _flush() { // TODO(@anonrig): Add Atomics for locking to avoid having force flush and regular flush // running at the same time. // Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics // This path eliminates the need for checking for timestamps since we're forcing a flush. // Remember to reset the flag, or it will always flush all metrics. if (this._forceFlush) { this._forceFlush = false; this._bucketsTotalWeight = 0; this._captureMetrics(this._buckets); this._buckets.clear(); return; } const cutoffSeconds = Math.floor(timestampInSeconds()) - DEFAULT_FLUSH_INTERVAL / 1000 - this._flushShift; // TODO(@anonrig): Optimization opportunity. // Convert this map to an array and store key in the bucketItem. const flushedBuckets = new Map(); for (const [key, bucket] of this._buckets) { if (bucket.timestamp <= cutoffSeconds) { flushedBuckets.set(key, bucket); this._bucketsTotalWeight -= bucket.metric.weight; } } for (const [key] of flushedBuckets) { this._buckets.delete(key); } this._captureMetrics(flushedBuckets); } /** * Only captures a subset of the buckets passed to this function. * @param flushedBuckets */ _captureMetrics(flushedBuckets) { if (flushedBuckets.size > 0) { // TODO(@anonrig): Optimization opportunity. // This copy operation can be avoided if we store the key in the bucketItem. const buckets = Array.from(flushedBuckets).map(([, bucketItem]) => bucketItem); captureAggregateMetrics(this._client, buckets); } }}
/** * Adds a value to a counter metric * * @experimental This API is experimental and might have breaking changes in the future. */function increment(name, value = 1, data) { metrics.increment(MetricsAggregator, name, value, data);}
/** * Adds a value to a distribution metric * * @experimental This API is experimental and might have breaking changes in the future. */function distribution(name, value, data) { metrics.distribution(MetricsAggregator, name, value, data);}
/** * Adds a value to a set metric. Value must be a string or integer. * * @experimental This API is experimental and might have breaking changes in the future. */function set(name, value, data) { metrics.set(MetricsAggregator, name, value, data);}
/** * Adds a value to a gauge metric * * @experimental This API is experimental and might have breaking changes in the future. */function gauge(name, value, data) { metrics.gauge(MetricsAggregator, name, value, data);}
/** * Returns the metrics aggregator for a given client. */function getMetricsAggregatorForClient(client) { return metrics.getMetricsAggregatorForClient(client, MetricsAggregator);}
const metricsDefault = { increment, distribution, set, gauge, /** * @ignore This is for internal use only. */ getMetricsAggregatorForClient,};
function getHostName() { const result = Deno.permissions.querySync({ name: 'sys', kind: 'hostname' }); return result.state === 'granted' ? Deno.hostname() : undefined;}
/** * The Sentry Deno SDK Client. * * @see DenoClientOptions for documentation on configuration options. * @see SentryClient for usage documentation. */class DenoClient extends ServerRuntimeClient { /** * Creates a new Deno SDK instance. * @param options Configuration options for this SDK. */ constructor(options) { options._metadata = options._metadata || {}; options._metadata.sdk = options._metadata.sdk || { name: 'sentry.javascript.deno', packages: [ { name: 'denoland:sentry', version: SDK_VERSION, }, ], version: SDK_VERSION, }; const clientOptions = { ...options, platform: 'javascript', runtime: { name: 'deno', version: Deno.version.deno }, serverName: options.serverName || getHostName(), }; super(clientOptions); }}
const INTEGRATION_NAME$5 = 'Breadcrumbs';
/** * Note: This `breadcrumbsIntegration` is almost the same as the one from @sentry/browser. * The Deno-version does not support browser-specific APIs like dom, xhr and history. */const _breadcrumbsIntegration = ((options = {}) => { const _options = { console: true, fetch: true, sentry: true, ...options, }; return { name: INTEGRATION_NAME$5, setup(client) { if (_options.console) { addConsoleInstrumentationHandler(_getConsoleBreadcrumbHandler(client)); } if (_options.fetch) { addFetchInstrumentationHandler(_getFetchBreadcrumbHandler(client)); } if (_options.sentry) { client.on('beforeSendEvent', _getSentryBreadcrumbHandler(client)); } }, };}) ;
/** * Adds a breadcrumbs for console, fetch, and sentry events. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.breadcrumbsIntegration(), * ], * }) * ``` */const breadcrumbsIntegration = defineIntegration(_breadcrumbsIntegration);
/** * Adds a breadcrumb for Sentry events or transactions if this option is enabled. * */function _getSentryBreadcrumbHandler(client) { return function addSentryBreadcrumb(event) { if (getClient() !== client) { return; } addBreadcrumb( { category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`, event_id: event.event_id, level: event.level, message: getEventDescription(event), }, { event, }, ); };}
/** * Creates breadcrumbs from console API calls */function _getConsoleBreadcrumbHandler(client) { return function _consoleBreadcrumb(handlerData) { if (getClient() !== client) { return; } const breadcrumb = { category: 'console', data: { arguments: handlerData.args, logger: 'console', }, level: severityLevelFromString(handlerData.level), message: safeJoin(handlerData.args, ' '), }; if (handlerData.level === 'assert') { if (handlerData.args[0] === false) { breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`; breadcrumb.data.arguments = handlerData.args.slice(1); } else { // Don't capture a breadcrumb for passed assertions return; } } addBreadcrumb(breadcrumb, { input: handlerData.args, level: handlerData.level, }); };}
/** * Creates breadcrumbs from fetch API calls */function _getFetchBreadcrumbHandler(client) { return function _fetchBreadcrumb(handlerData) { if (getClient() !== client) { return; } const { startTimestamp, endTimestamp } = handlerData; // We only capture complete fetch requests if (!endTimestamp) { return; } if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) return; } if (handlerData.error) { const data = handlerData.fetchData; const hint = { data: handlerData.error, input: handlerData.args, startTimestamp, endTimestamp, }; addBreadcrumb( { category: 'fetch', data, level: 'error', type: 'http', }, hint, ); } else { const response = handlerData.response ; const data = { ...handlerData.fetchData, status_code: response && response.status, }; const hint = { input: handlerData.args, response, startTimestamp, endTimestamp, }; addBreadcrumb( { category: 'fetch', data, type: 'http', }, hint, ); } };}
const INTEGRATION_NAME$4 = 'DenoContext';
function getOSName() { switch (Deno.build.os) { case 'darwin': return 'macOS'; case 'linux': return 'Linux'; case 'windows': return 'Windows'; default: return Deno.build.os; }}
function getOSRelease() { return Deno.permissions.querySync({ name: 'sys', kind: 'osRelease' }).state === 'granted' ? Deno.osRelease() : undefined;}
async function addDenoRuntimeContext(event) { event.contexts = { ...{ app: { app_start_time: new Date(Date.now() - performance.now()).toISOString(), }, device: { arch: Deno.build.arch, // eslint-disable-next-line no-restricted-globals processor_count: navigator.hardwareConcurrency, }, os: { name: getOSName(), version: getOSRelease(), }, v8: { name: 'v8', version: Deno.version.v8, }, typescript: { name: 'TypeScript', version: Deno.version.typescript, }, }, ...event.contexts, }; return event;}
const _denoContextIntegration = (() => { return { name: INTEGRATION_NAME$4, processEvent(event) { return addDenoRuntimeContext(event); }, };}) ;
/** * Adds Deno related context to events. This includes contexts about app, device, os, v8, and TypeScript. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.denoContextIntegration(), * ], * }) * ``` */const denoContextIntegration = defineIntegration(_denoContextIntegration);
const INTEGRATION_NAME$3 = 'ContextLines';const FILE_CONTENT_CACHE = new LRUMap(100);const DEFAULT_LINES_OF_CONTEXT = 7;
/** * Reads file contents and caches them in a global LRU cache. * * @param filename filepath to read content from. */async function readSourceFile(filename) { const cachedFile = FILE_CONTENT_CACHE.get(filename); // We have a cache hit if (cachedFile !== undefined) { return cachedFile; } let content = null; try { content = await Deno.readTextFile(filename); } catch (_) { // } FILE_CONTENT_CACHE.set(filename, content); return content;}










const _contextLinesIntegration = ((options = {}) => { const contextLines = options.frameContextLines !== undefined ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT; return { name: INTEGRATION_NAME$3, processEvent(event) { return addSourceContext(event, contextLines); }, };}) ;
/** * Adds source context to event stacktraces. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.contextLinesIntegration(), * ], * }) * ``` */const contextLinesIntegration = defineIntegration(_contextLinesIntegration);
/** Processes an event and adds context lines */async function addSourceContext(event, contextLines) { if (contextLines > 0 && event.exception && event.exception.values) { for (const exception of event.exception.values) { if (exception.stacktrace && exception.stacktrace.frames) { await addSourceContextToFrames(exception.stacktrace.frames, contextLines); } } } return event;}
/** Adds context lines to frames */async function addSourceContextToFrames(frames, contextLines) { for (const frame of frames) { // Only add context if we have a filename and it hasn't already been added if (frame.filename && frame.in_app && frame.context_line === undefined) { const permission = await Deno.permissions.query({ name: 'read', path: frame.filename, }); if (permission.state == 'granted') { const sourceFile = await readSourceFile(frame.filename); if (sourceFile) { try { const lines = sourceFile.split('\n'); addContextToFrame(lines, frame, contextLines); } catch (_) { // anomaly, being defensive in case // unlikely to ever happen in practice but can definitely happen in theory } } } } }}
const INTEGRATION_NAME$2 = 'GlobalHandlers';let isExiting = false;
const _globalHandlersIntegration = ((options) => { const _options = { error: true, unhandledrejection: true, ...options, }; return { name: INTEGRATION_NAME$2, setup(client) { if (_options.error) { installGlobalErrorHandler(client); } if (_options.unhandledrejection) { installGlobalUnhandledRejectionHandler(client); } }, };}) ;
/** * Instruments global `error` and `unhandledrejection` listeners in Deno. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.globalHandlersIntegration(), * ], * }) * ``` */const globalHandlersIntegration = defineIntegration(_globalHandlersIntegration);
function installGlobalErrorHandler(client) { globalThis.addEventListener('error', data => { if (getClient() !== client || isExiting) { return; } const stackParser = getStackParser(); const { message, error } = data; const event = eventFromUnknownInput(client, stackParser, error || message); event.level = 'fatal'; captureEvent(event, { originalException: error, mechanism: { handled: false, type: 'error', }, }); // Stop the app from exiting for now data.preventDefault(); isExiting = true; flush().then( () => { // rethrow to replicate Deno default behavior throw error; }, () => { // rethrow to replicate Deno default behavior throw error; }, ); });}
function installGlobalUnhandledRejectionHandler(client) { globalThis.addEventListener('unhandledrejection', (e) => { if (getClient() !== client || isExiting) { return; } const stackParser = getStackParser(); let error = e; // dig the object of the rejection out of known event types try { if ('reason' in e) { error = e.reason; } } catch (_oO) { // no-empty } const event = isPrimitive(error) ? eventFromRejectionWithPrimitive(error) : eventFromUnknownInput(client, stackParser, error, undefined); event.level = 'fatal'; captureEvent(event, { originalException: error, mechanism: { handled: false, type: 'unhandledrejection', }, }); // Stop the app from exiting for now e.preventDefault(); isExiting = true; flush().then( () => { // rethrow to replicate Deno default behavior throw error; }, () => { // rethrow to replicate Deno default behavior throw error; }, ); });}
/** * Create an event from a promise rejection where the `reason` is a primitive. * * @param reason: The `reason` property of the promise rejection * @returns An Event object with an appropriate `exception` value */function eventFromRejectionWithPrimitive(reason) { return { exception: { values: [ { type: 'UnhandledRejection', // String() is needed because the Primitive type includes symbols (which can't be automatically stringified) value: `Non-Error promise rejection captured with value: ${String(reason)}`, }, ], }, };}
function getStackParser() { const client = getClient(); if (!client) { return () => []; } return client.getOptions().stackParser;}
function _optionalChain(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; }const INTEGRATION_NAME$1 = 'NormalizePaths';
function appRootFromErrorStack(error) { // We know at the other end of the stack from here is the entry point that called 'init' // We assume that this stacktrace will traverse the root of the app const frames = createStackParser(nodeStackLineParser())(error.stack || ''); const paths = frames // We're only interested in frames that are in_app with filenames .filter(f => f.in_app && f.filename) .map( f => (f.filename ) .replace(/^[A-Z]:/, '') // remove Windows-style prefix .replace(/\\/g, '/') // replace all `\` instances with `/` .split('/') .filter(seg => seg !== ''), // remove empty segments ) ; if (paths.length == 0) { return undefined; } if (paths.length == 1) { // Assume the single file is in the root return dirname(paths[0].join('/')); } // Iterate over the paths and bail out when they no longer have a common root let i = 0; while (paths[0][i] && paths.every(w => w[i] === paths[0][i])) { i++; } return paths[0].slice(0, i).join('/');}
function getCwd() { // We don't want to prompt for permissions so we only get the cwd if // permissions are already granted const permission = Deno.permissions.querySync({ name: 'read', path: './' }); try { if (permission.state == 'granted') { return Deno.cwd(); } } catch (_) { // } return undefined;}
const _normalizePathsIntegration = (() => { // Cached here let appRoot; /** Get the app root, and cache it after it was first fetched. */ function getAppRoot(error) { if (appRoot === undefined) { appRoot = getCwd() || appRootFromErrorStack(error); } return appRoot; } return { name: INTEGRATION_NAME$1, processEvent(event) { // This error.stack hopefully contains paths that traverse the app cwd const error = new Error(); const appRoot = getAppRoot(error); if (appRoot) { for (const exception of _optionalChain([event, 'access', _2 => _2.exception, 'optionalAccess', _3 => _3.values]) || []) { for (const frame of _optionalChain([exception, 'access', _4 => _4.stacktrace, 'optionalAccess', _5 => _5.frames]) || []) { if (frame.filename && frame.in_app) { const startIndex = frame.filename.indexOf(appRoot); if (startIndex > -1) { const endIndex = startIndex + appRoot.length; frame.filename = `app://${frame.filename.substring(endIndex)}`; } } } } } return event; }, };}) ;
/** * Normalises paths to the app root directory. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.normalizePathsIntegration(), * ], * }) * ``` */const normalizePathsIntegration = defineIntegration(_normalizePathsIntegration);
/** * Creates a Transport that uses the Fetch API to send events to Sentry. */function makeFetchTransport(options) { const url = new URL(options.url); if (Deno.permissions.querySync({ name: 'net', host: url.host }).state !== 'granted') { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn(`Sentry SDK requires 'net' permission to send events. Run with '--allow-net=${url.host}' to grant the requires permissions.`); }); } function makeRequest(request) { const requestOptions = { body: request.body, method: 'POST', referrerPolicy: 'origin', headers: options.headers, }; try { return fetch(options.url, requestOptions).then(response => { return { statusCode: response.status, headers: { 'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'), 'retry-after': response.headers.get('Retry-After'), }, }; }); } catch (e) { return rejectedSyncPromise(e); } } return createTransport(options, makeRequest);}
/** Get the default integrations for the Deno SDK. */function getDefaultIntegrations(_options) { // We return a copy of the defaultIntegrations here to avoid mutating this return [ // Common inboundFiltersIntegration(), functionToStringIntegration(), linkedErrorsIntegration(), dedupeIntegration(), // Deno Specific breadcrumbsIntegration(), denoContextIntegration(), contextLinesIntegration(), normalizePathsIntegration(), globalHandlersIntegration(), ];}
const defaultStackParser = createStackParser(nodeStackLineParser());
/** * The Sentry Deno SDK Client. * * To use this SDK, call the {@link init} function as early as possible in the * main entry module. To set context information or send manual events, use the * provided methods. * * @example * ``` * * import { init } from 'npm:@sentry/deno'; * * init({ * dsn: '__DSN__', * // ... * }); * ``` * * @example * ``` * * import { addBreadcrumb } from 'npm:@sentry/deno'; * addBreadcrumb({ * message: 'My Breadcrumb', * // ... * }); * ``` * * @example * ``` * * import * as Sentry from 'npm:@sentry/deno'; * Sentry.captureMessage('Hello, world!'); * Sentry.captureException(new Error('Good bye')); * Sentry.captureEvent({ * message: 'Manual', * stacktrace: [ * // ... * ], * }); * ``` * * @see {@link DenoOptions} for documentation on configuration options. */function init(options = {}) { if (options.defaultIntegrations === undefined) { options.defaultIntegrations = getDefaultIntegrations(); } const clientOptions = { ...options, stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser), integrations: getIntegrationsToSetup(options), transport: options.transport || makeFetchTransport, }; initAndBind(DenoClient, clientOptions);}
/** * These functions were copied from the Deno source code here: * https://github.com/denoland/deno/blob/cd480b481ee1b4209910aa7a8f81ffa996e7b0f9/ext/cron/01_cron.ts * Below is the original license: * * MIT License * * Copyright 2018-2023 the Deno authors * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */function formatToCronSchedule( value




,) { if (value === undefined) { return '*'; } else if (typeof value === 'number') { return value.toString(); } else { const { exact } = value ; if (exact === undefined) { const { start, end, every } = value

; if (start !== undefined && end !== undefined && every !== undefined) { return `${start}-${end}/${every}`; } else if (start !== undefined && end !== undefined) { return `${start}-${end}`; } else if (start !== undefined && every !== undefined) { return `${start}/${every}`; } else if (start !== undefined) { return `${start}/1`; } else if (end === undefined && every !== undefined) { return `*/${every}`; } else { throw new TypeError('Invalid cron schedule'); } } else { if (typeof exact === 'number') { return exact.toString(); } else { return exact.join(','); } } }}
/** */function parseScheduleToString(schedule) { if (typeof schedule === 'string') { return schedule; } else { const { minute, hour, dayOfMonth, month, dayOfWeek } = schedule; return `${formatToCronSchedule(minute)} ${formatToCronSchedule(hour)} ${formatToCronSchedule( dayOfMonth, )} ${formatToCronSchedule(month)} ${formatToCronSchedule(dayOfWeek)}`; }}
const INTEGRATION_NAME = 'DenoCron';
const SETUP_CLIENTS = new WeakMap();
const _denoCronIntegration = (() => { return { name: INTEGRATION_NAME, setupOnce() { // eslint-disable-next-line deprecation/deprecation if (!Deno.cron) { // The cron API is not available in this Deno version use --unstable flag! return; } // eslint-disable-next-line deprecation/deprecation Deno.cron = new Proxy(Deno.cron, { apply(target, thisArg, argArray) { const [monitorSlug, schedule, opt1, opt2] = argArray; let options; let fn; if (typeof opt1 === 'function' && typeof opt2 !== 'function') { fn = opt1; options = opt2; } else if (typeof opt1 !== 'function' && typeof opt2 === 'function') { fn = opt2; options = opt1; } async function cronCalled() { if (!SETUP_CLIENTS.has(getClient() )) { return fn(); } await withMonitor(monitorSlug, async () => fn(), { schedule: { type: 'crontab', value: parseScheduleToString(schedule) }, // (minutes) so 12 hours - just a very high arbitrary number since we don't know the actual duration of the users cron job maxRuntime: 60 * 12, // Deno Deploy docs say that the cron job will be called within 1 minute of the scheduled time checkinMargin: 1, }); } return target.call(thisArg, monitorSlug, schedule, options || {}, cronCalled); }, }); }, setup(client) { SETUP_CLIENTS.set(client, true); }, };}) ;
/** * Instruments Deno.cron to automatically capture cron check-ins. * * Enabled by default in the Deno SDK. * * ```js * Sentry.init({ * integrations: [ * Sentry.denoCronIntegration(), * ], * }) * ``` */const denoCronIntegration = defineIntegration(_denoCronIntegration);
export { DenoClient, SDK_VERSION, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, Scope, addBreadcrumb, addEventProcessor, breadcrumbsIntegration, captureCheckIn, captureConsoleIntegration, captureEvent, captureException, captureMessage, captureSession, close, contextLinesIntegration, continueTrace, createTransport, debugIntegration, dedupeIntegration, denoContextIntegration, denoCronIntegration, endSession, extraErrorDataIntegration, flush, functionToStringIntegration, getActiveSpan, getClient, getCurrentScope, getDefaultIntegrations, getGlobalScope, getIsolationScope, getRootSpan, getSpanStatusFromHttpCode, globalHandlersIntegration, inboundFiltersIntegration, init, isInitialized, linkedErrorsIntegration, metricsDefault as metrics, normalizePathsIntegration, requestDataIntegration, rewriteFramesIntegration, sessionTimingIntegration, setContext, setCurrentClient, setExtra, setExtras, setHttpStatus, setMeasurement, setTag, setTags, setUser, spanToJSON, spanToTraceHeader, startInactiveSpan, startSession, startSpan, startSpanManual, withIsolationScope$1 as withIsolationScope, withMonitor, withScope$1 as withScope };//# sourceMappingURL=index.mjs.map